jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1362 lines
51KB

  1. /*
  2. Copyright (C) 2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackCoreAudioAdapter.h"
  16. #include "JackError.h"
  17. #include <unistd.h>
  18. #include <CoreServices/CoreServices.h>
  19. namespace Jack
  20. {
  21. static void PrintStreamDesc(AudioStreamBasicDescription *inDesc)
  22. {
  23. jack_log("- - - - - - - - - - - - - - - - - - - -");
  24. jack_log(" Sample Rate:%f", inDesc->mSampleRate);
  25. jack_log(" Format ID:%.*s", (int) sizeof(inDesc->mFormatID), (char*)&inDesc->mFormatID);
  26. jack_log(" Format Flags:%lX", inDesc->mFormatFlags);
  27. jack_log(" Bytes per Packet:%ld", inDesc->mBytesPerPacket);
  28. jack_log(" Frames per Packet:%ld", inDesc->mFramesPerPacket);
  29. jack_log(" Bytes per Frame:%ld", inDesc->mBytesPerFrame);
  30. jack_log(" Channels per Frame:%ld", inDesc->mChannelsPerFrame);
  31. jack_log(" Bits per Channel:%ld", inDesc->mBitsPerChannel);
  32. jack_log("- - - - - - - - - - - - - - - - - - - -");
  33. }
  34. static OSStatus DisplayDeviceNames()
  35. {
  36. UInt32 size;
  37. Boolean isWritable;
  38. int i, deviceNum;
  39. OSStatus err;
  40. CFStringRef UIname;
  41. err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
  42. if (err != noErr)
  43. return err;
  44. deviceNum = size / sizeof(AudioDeviceID);
  45. AudioDeviceID devices[deviceNum];
  46. err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
  47. if (err != noErr)
  48. return err;
  49. for (i = 0; i < deviceNum; i++) {
  50. char device_name[256];
  51. char internal_name[256];
  52. size = sizeof(CFStringRef);
  53. UIname = NULL;
  54. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  55. if (err == noErr) {
  56. CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
  57. } else {
  58. goto error;
  59. }
  60. size = 256;
  61. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
  62. if (err != noErr)
  63. return err;
  64. jack_info("Device name = \'%s\', internal_name = \'%s\' (to be used as -C, -P, or -d parameter)", device_name, internal_name);
  65. }
  66. return noErr;
  67. error:
  68. if (UIname != NULL)
  69. CFRelease(UIname);
  70. return err;
  71. }
  72. static void printError(OSStatus err)
  73. {
  74. switch (err) {
  75. case kAudioHardwareNoError:
  76. jack_log("error code : kAudioHardwareNoError");
  77. break;
  78. case kAudioConverterErr_FormatNotSupported:
  79. jack_log("error code : kAudioConverterErr_FormatNotSupported");
  80. break;
  81. case kAudioConverterErr_OperationNotSupported:
  82. jack_log("error code : kAudioConverterErr_OperationNotSupported");
  83. break;
  84. case kAudioConverterErr_PropertyNotSupported:
  85. jack_log("error code : kAudioConverterErr_PropertyNotSupported");
  86. break;
  87. case kAudioConverterErr_InvalidInputSize:
  88. jack_log("error code : kAudioConverterErr_InvalidInputSize");
  89. break;
  90. case kAudioConverterErr_InvalidOutputSize:
  91. jack_log("error code : kAudioConverterErr_InvalidOutputSize");
  92. break;
  93. case kAudioConverterErr_UnspecifiedError:
  94. jack_log("error code : kAudioConverterErr_UnspecifiedError");
  95. break;
  96. case kAudioConverterErr_BadPropertySizeError:
  97. jack_log("error code : kAudioConverterErr_BadPropertySizeError");
  98. break;
  99. case kAudioConverterErr_RequiresPacketDescriptionsError:
  100. jack_log("error code : kAudioConverterErr_RequiresPacketDescriptionsError");
  101. break;
  102. case kAudioConverterErr_InputSampleRateOutOfRange:
  103. jack_log("error code : kAudioConverterErr_InputSampleRateOutOfRange");
  104. break;
  105. case kAudioConverterErr_OutputSampleRateOutOfRange:
  106. jack_log("error code : kAudioConverterErr_OutputSampleRateOutOfRange");
  107. break;
  108. case kAudioHardwareNotRunningError:
  109. jack_log("error code : kAudioHardwareNotRunningError");
  110. break;
  111. case kAudioHardwareUnknownPropertyError:
  112. jack_log("error code : kAudioHardwareUnknownPropertyError");
  113. break;
  114. case kAudioHardwareIllegalOperationError:
  115. jack_log("error code : kAudioHardwareIllegalOperationError");
  116. break;
  117. case kAudioHardwareBadDeviceError:
  118. jack_log("error code : kAudioHardwareBadDeviceError");
  119. break;
  120. case kAudioHardwareBadStreamError:
  121. jack_log("error code : kAudioHardwareBadStreamError");
  122. break;
  123. case kAudioDeviceUnsupportedFormatError:
  124. jack_log("error code : kAudioDeviceUnsupportedFormatError");
  125. break;
  126. case kAudioDevicePermissionsError:
  127. jack_log("error code : kAudioDevicePermissionsError");
  128. break;
  129. case kAudioHardwareBadObjectError:
  130. jack_log("error code : kAudioHardwareBadObjectError");
  131. break;
  132. case kAudioHardwareUnsupportedOperationError:
  133. jack_log("error code : kAudioHardwareUnsupportedOperationError");
  134. break;
  135. default:
  136. jack_log("error code : unknown");
  137. break;
  138. }
  139. }
  140. OSStatus JackCoreAudioAdapter::SRNotificationCallback(AudioDeviceID inDevice,
  141. UInt32 inChannel,
  142. Boolean isInput,
  143. AudioDevicePropertyID inPropertyID,
  144. void* inClientData)
  145. {
  146. JackCoreAudioAdapter* driver = static_cast<JackCoreAudioAdapter*>(inClientData);
  147. switch (inPropertyID) {
  148. case kAudioDevicePropertyNominalSampleRate: {
  149. jack_log("JackCoreAudioDriver::SRNotificationCallback kAudioDevicePropertyNominalSampleRate");
  150. driver->fState = true;
  151. break;
  152. }
  153. }
  154. return noErr;
  155. }
  156. // A better implementation would try to recover in case of hardware device change (see HALLAB HLFilePlayerWindowControllerAudioDevicePropertyListenerProc code)
  157. OSStatus JackCoreAudioAdapter::DeviceNotificationCallback(AudioDeviceID inDevice,
  158. UInt32 inChannel,
  159. Boolean isInput,
  160. AudioDevicePropertyID inPropertyID,
  161. void* inClientData)
  162. {
  163. switch (inPropertyID) {
  164. case kAudioDeviceProcessorOverload: {
  165. jack_error("JackCoreAudioAdapter::DeviceNotificationCallback kAudioDeviceProcessorOverload");
  166. break;
  167. }
  168. case kAudioDevicePropertyStreamConfiguration: {
  169. jack_error("Cannot handle kAudioDevicePropertyStreamConfiguration");
  170. return kAudioHardwareUnsupportedOperationError;
  171. }
  172. case kAudioDevicePropertyNominalSampleRate: {
  173. jack_error("Cannot handle kAudioDevicePropertyNominalSampleRate");
  174. return kAudioHardwareUnsupportedOperationError;
  175. }
  176. }
  177. return noErr;
  178. }
  179. int JackCoreAudioAdapter::AddListeners()
  180. {
  181. OSStatus err = noErr;
  182. // Add listeners
  183. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
  184. if (err != noErr) {
  185. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  186. printError(err);
  187. return -1;
  188. }
  189. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback, this);
  190. if (err != noErr) {
  191. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
  192. printError(err);
  193. return -1;
  194. }
  195. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback, this);
  196. if (err != noErr) {
  197. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  198. printError(err);
  199. return -1;
  200. }
  201. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback, this);
  202. if (err != noErr) {
  203. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
  204. printError(err);
  205. return -1;
  206. }
  207. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  208. if (err != noErr) {
  209. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  210. printError(err);
  211. return -1;
  212. }
  213. err = AudioDeviceAddPropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  214. if (err != noErr) {
  215. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  216. printError(err);
  217. return -1;
  218. }
  219. return 0;
  220. }
  221. void JackCoreAudioAdapter::RemoveListeners()
  222. {
  223. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
  224. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback);
  225. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback);
  226. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback);
  227. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  228. AudioDeviceRemovePropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  229. }
  230. OSStatus JackCoreAudioAdapter::Render(void *inRefCon,
  231. AudioUnitRenderActionFlags *ioActionFlags,
  232. const AudioTimeStamp *inTimeStamp,
  233. UInt32 inBusNumber,
  234. UInt32 inNumberFrames,
  235. AudioBufferList *ioData)
  236. {
  237. JackCoreAudioAdapter* adapter = static_cast<JackCoreAudioAdapter*>(inRefCon);
  238. AudioUnitRender(adapter->fAUHAL, ioActionFlags, inTimeStamp, 1, inNumberFrames, adapter->fInputData);
  239. float* inputBuffer[adapter->fCaptureChannels];
  240. float* outputBuffer[adapter->fPlaybackChannels];
  241. for (int i = 0; i < adapter->fCaptureChannels; i++) {
  242. inputBuffer[i] = (float*)adapter->fInputData->mBuffers[i].mData;
  243. }
  244. for (int i = 0; i < adapter->fPlaybackChannels; i++) {
  245. outputBuffer[i] = (float*)ioData->mBuffers[i].mData;
  246. }
  247. adapter->PushAndPull((float**)inputBuffer, (float**)outputBuffer, inNumberFrames);
  248. return noErr;
  249. }
  250. JackCoreAudioAdapter::JackCoreAudioAdapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params)
  251. :JackAudioAdapterInterface(buffer_size, sample_rate), fInputData(0), fCapturing(false), fPlaying(false), fState(false)
  252. {
  253. const JSList* node;
  254. const jack_driver_param_t* param;
  255. int in_nChannels = 0;
  256. int out_nChannels = 0;
  257. char captureName[256];
  258. char playbackName[256];
  259. fCaptureUID[0] = 0;
  260. fPlaybackUID[0] = 0;
  261. // Default values
  262. fCaptureChannels = -1;
  263. fPlaybackChannels = -1;
  264. SInt32 major;
  265. SInt32 minor;
  266. Gestalt(gestaltSystemVersionMajor, &major);
  267. Gestalt(gestaltSystemVersionMinor, &minor);
  268. // Starting with 10.6 systems, the HAL notification thread is created internally
  269. if (major == 10 && minor >= 6) {
  270. CFRunLoopRef theRunLoop = NULL;
  271. AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  272. OSStatus theError = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
  273. if (theError != noErr) {
  274. jack_error("JackCoreAudioAdapter::Open kAudioHardwarePropertyRunLoop error");
  275. }
  276. }
  277. for (node = params; node; node = jack_slist_next(node)) {
  278. param = (const jack_driver_param_t*) node->data;
  279. switch (param->character) {
  280. case 'c' :
  281. fCaptureChannels = fPlaybackChannels = param->value.ui;
  282. break;
  283. case 'i':
  284. fCaptureChannels = param->value.ui;
  285. break;
  286. case 'o':
  287. fPlaybackChannels = param->value.ui;
  288. break;
  289. case 'C':
  290. fCapturing = true;
  291. strncpy(fCaptureUID, param->value.str, 256);
  292. break;
  293. case 'P':
  294. fPlaying = true;
  295. strncpy(fPlaybackUID, param->value.str, 256);
  296. break;
  297. case 'd':
  298. strncpy(fCaptureUID, param->value.str, 256);
  299. strncpy(fPlaybackUID, param->value.str, 256);
  300. break;
  301. case 'D':
  302. fCapturing = fPlaying = true;
  303. break;
  304. case 'r':
  305. SetAdaptedSampleRate(param->value.ui);
  306. break;
  307. case 'p':
  308. SetAdaptedBufferSize(param->value.ui);
  309. break;
  310. case 'l':
  311. DisplayDeviceNames();
  312. break;
  313. case 'q':
  314. fQuality = param->value.ui;
  315. break;
  316. case 'g':
  317. fRingbufferCurSize = param->value.ui;
  318. fAdaptative = false;
  319. break;
  320. }
  321. }
  322. /* duplex is the default */
  323. if (!fCapturing && !fPlaying) {
  324. fCapturing = true;
  325. fPlaying = true;
  326. }
  327. if (SetupDevices(fCaptureUID, fPlaybackUID, captureName, playbackName, fAdaptedSampleRate) < 0)
  328. throw -1;
  329. if (SetupChannels(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, true) < 0)
  330. throw -1;
  331. if (SetupBufferSize(fAdaptedBufferSize) < 0)
  332. throw -1;
  333. if (SetupSampleRate(fAdaptedSampleRate) < 0)
  334. throw -1;
  335. if (OpenAUHAL(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, fAdaptedBufferSize, fAdaptedSampleRate) < 0)
  336. throw -1;
  337. if (fCapturing && fCaptureChannels > 0)
  338. if (SetupBuffers(fCaptureChannels) < 0)
  339. throw -1;
  340. if (AddListeners() < 0)
  341. throw -1;
  342. }
  343. OSStatus JackCoreAudioAdapter::GetDefaultDevice(AudioDeviceID* id)
  344. {
  345. OSStatus res;
  346. UInt32 theSize = sizeof(UInt32);
  347. AudioDeviceID inDefault;
  348. AudioDeviceID outDefault;
  349. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
  350. return res;
  351. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
  352. return res;
  353. jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
  354. // Get the device only if default input and output are the same
  355. if (inDefault == outDefault) {
  356. *id = inDefault;
  357. return noErr;
  358. } else {
  359. jack_error("Default input and output devices are not the same !!");
  360. return kAudioHardwareBadDeviceError;
  361. }
  362. }
  363. OSStatus JackCoreAudioAdapter::GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput)
  364. {
  365. OSStatus err = noErr;
  366. UInt32 outSize;
  367. Boolean outWritable;
  368. AudioBufferList* bufferList = 0;
  369. channelCount = 0;
  370. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  371. if (err == noErr) {
  372. bufferList = (AudioBufferList*)malloc(outSize);
  373. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  374. if (err == noErr) {
  375. for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++)
  376. channelCount += bufferList->mBuffers[i].mNumberChannels;
  377. }
  378. if (bufferList)
  379. free(bufferList);
  380. }
  381. return err;
  382. }
  383. OSStatus JackCoreAudioAdapter::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
  384. {
  385. UInt32 size = sizeof(AudioValueTranslation);
  386. CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
  387. AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
  388. if (inIUD == NULL) {
  389. return kAudioHardwareUnspecifiedError;
  390. } else {
  391. OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
  392. CFRelease(inIUD);
  393. jack_log("GetDeviceIDFromUID %s %ld", UID, *id);
  394. return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
  395. }
  396. }
  397. OSStatus JackCoreAudioAdapter::GetDefaultInputDevice(AudioDeviceID* id)
  398. {
  399. OSStatus res;
  400. UInt32 theSize = sizeof(UInt32);
  401. AudioDeviceID inDefault;
  402. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
  403. return res;
  404. jack_log("GetDefaultInputDevice: input = %ld ", inDefault);
  405. *id = inDefault;
  406. return noErr;
  407. }
  408. OSStatus JackCoreAudioAdapter::GetDefaultOutputDevice(AudioDeviceID* id)
  409. {
  410. OSStatus res;
  411. UInt32 theSize = sizeof(UInt32);
  412. AudioDeviceID outDefault;
  413. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
  414. return res;
  415. jack_log("GetDefaultOutputDevice: output = %ld", outDefault);
  416. *id = outDefault;
  417. return noErr;
  418. }
  419. OSStatus JackCoreAudioAdapter::GetDeviceNameFromID(AudioDeviceID id, char* name)
  420. {
  421. UInt32 size = 256;
  422. return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
  423. }
  424. // Setup
  425. int JackCoreAudioAdapter::SetupDevices(const char* capture_driver_uid,
  426. const char* playback_driver_uid,
  427. char* capture_driver_name,
  428. char* playback_driver_name,
  429. jack_nframes_t samplerate)
  430. {
  431. capture_driver_name[0] = 0;
  432. playback_driver_name[0] = 0;
  433. // Duplex
  434. if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
  435. // Same device for capture and playback...
  436. if (strcmp(capture_driver_uid, playback_driver_uid) == 0) {
  437. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  438. jack_log("Will take default in/out");
  439. if (GetDefaultDevice(&fDeviceID) != noErr) {
  440. jack_error("Cannot open default device");
  441. return -1;
  442. }
  443. }
  444. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  445. jack_error("Cannot get device name from device ID");
  446. return -1;
  447. }
  448. } else {
  449. // Creates aggregate device
  450. AudioDeviceID captureID, playbackID;
  451. if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr)
  452. return -1;
  453. if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr)
  454. return -1;
  455. if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr)
  456. return -1;
  457. }
  458. // Capture only
  459. } else if (strcmp(capture_driver_uid, "") != 0) {
  460. jack_log("JackCoreAudioAdapter::Open capture only");
  461. if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
  462. if (GetDefaultInputDevice(&fDeviceID) != noErr) {
  463. jack_error("Cannot open default device");
  464. return -1;
  465. }
  466. }
  467. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
  468. jack_error("Cannot get device name from device ID");
  469. return -1;
  470. }
  471. // Playback only
  472. } else if (strcmp(playback_driver_uid, "") != 0) {
  473. jack_log("JackCoreAudioAdapter::Open playback only");
  474. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  475. if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
  476. jack_error("Cannot open default device");
  477. return -1;
  478. }
  479. }
  480. if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  481. jack_error("Cannot get device name from device ID");
  482. return -1;
  483. }
  484. // Use default driver in duplex mode
  485. } else {
  486. jack_log("JackCoreAudioAdapter::Open default driver");
  487. if (GetDefaultDevice(&fDeviceID) != noErr) {
  488. jack_error("Cannot open default device");
  489. return -1;
  490. }
  491. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  492. jack_error("Cannot get device name from device ID");
  493. return -1;
  494. }
  495. }
  496. return 0;
  497. }
  498. int JackCoreAudioAdapter::SetupChannels(bool capturing,
  499. bool playing,
  500. int& inchannels,
  501. int& outchannels,
  502. int& in_nChannels,
  503. int& out_nChannels,
  504. bool strict)
  505. {
  506. OSStatus err = noErr;
  507. if (capturing) {
  508. err = GetTotalChannels(fDeviceID, in_nChannels, true);
  509. if (err != noErr) {
  510. jack_error("Cannot get input channel number");
  511. printError(err);
  512. return -1;
  513. } else {
  514. jack_log("Max input channels : %d", in_nChannels);
  515. }
  516. }
  517. if (playing) {
  518. err = GetTotalChannels(fDeviceID, out_nChannels, false);
  519. if (err != noErr) {
  520. jack_error("Cannot get output channel number");
  521. printError(err);
  522. return -1;
  523. } else {
  524. jack_log("Max output channels : %d", out_nChannels);
  525. }
  526. }
  527. if (inchannels > in_nChannels) {
  528. jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
  529. if (strict)
  530. return -1;
  531. }
  532. if (outchannels > out_nChannels) {
  533. jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
  534. if (strict)
  535. return -1;
  536. }
  537. if (inchannels == -1) {
  538. jack_log("Setup max in channels = %ld", in_nChannels);
  539. inchannels = in_nChannels;
  540. }
  541. if (outchannels == -1) {
  542. jack_log("Setup max out channels = %ld", out_nChannels);
  543. outchannels = out_nChannels;
  544. }
  545. return 0;
  546. }
  547. int JackCoreAudioAdapter::SetupBufferSize(jack_nframes_t buffer_size)
  548. {
  549. // Setting buffer size
  550. UInt32 outSize = sizeof(UInt32);
  551. OSStatus err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
  552. if (err != noErr) {
  553. jack_error("Cannot set buffer size %ld", buffer_size);
  554. printError(err);
  555. return -1;
  556. }
  557. return 0;
  558. }
  559. int JackCoreAudioAdapter::SetupSampleRate(jack_nframes_t samplerate)
  560. {
  561. return SetupSampleRateAux(fDeviceID, samplerate);
  562. }
  563. int JackCoreAudioAdapter::SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate)
  564. {
  565. OSStatus err = noErr;
  566. UInt32 outSize;
  567. Float64 sampleRate;
  568. // Get sample rate
  569. outSize = sizeof(Float64);
  570. err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  571. if (err != noErr) {
  572. jack_error("Cannot get current sample rate");
  573. printError(err);
  574. return -1;
  575. }
  576. // If needed, set new sample rate
  577. if (samplerate != (jack_nframes_t)sampleRate) {
  578. sampleRate = (Float64)samplerate;
  579. // To get SR change notification
  580. err = AudioDeviceAddPropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
  581. if (err != noErr) {
  582. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  583. printError(err);
  584. return -1;
  585. }
  586. err = AudioDeviceSetProperty(inDevice, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
  587. if (err != noErr) {
  588. jack_error("Cannot set sample rate = %ld", samplerate);
  589. printError(err);
  590. return -1;
  591. }
  592. // Waiting for SR change notification
  593. int count = 0;
  594. while (!fState && count++ < WAIT_COUNTER) {
  595. usleep(100000);
  596. jack_log("Wait count = %d", count);
  597. }
  598. // Remove SR change notification
  599. AudioDeviceRemovePropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
  600. }
  601. return 0;
  602. }
  603. int JackCoreAudioAdapter::SetupBuffers(int inchannels)
  604. {
  605. jack_log("JackCoreAudioAdapter::SetupBuffers: input = %ld", inchannels);
  606. // Prepare buffers
  607. fInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  608. if (fInputData == 0) {
  609. jack_error("Cannot allocate memory for input buffers");
  610. return -1;
  611. }
  612. fInputData->mNumberBuffers = inchannels;
  613. for (int i = 0; i < fCaptureChannels; i++) {
  614. fInputData->mBuffers[i].mNumberChannels = 1;
  615. fInputData->mBuffers[i].mDataByteSize = fAdaptedBufferSize * sizeof(float);
  616. fInputData->mBuffers[i].mData = malloc(fAdaptedBufferSize * sizeof(float));
  617. }
  618. return 0;
  619. }
  620. void JackCoreAudioAdapter::DisposeBuffers()
  621. {
  622. if (fInputData) {
  623. for (int i = 0; i < fCaptureChannels; i++)
  624. free(fInputData->mBuffers[i].mData);
  625. free(fInputData);
  626. fInputData = 0;
  627. }
  628. }
  629. int JackCoreAudioAdapter::OpenAUHAL(bool capturing,
  630. bool playing,
  631. int inchannels,
  632. int outchannels,
  633. int in_nChannels,
  634. int out_nChannels,
  635. jack_nframes_t buffer_size,
  636. jack_nframes_t samplerate)
  637. {
  638. ComponentResult err1;
  639. UInt32 enableIO;
  640. AudioStreamBasicDescription srcFormat, dstFormat;
  641. AudioDeviceID currAudioDeviceID;
  642. UInt32 size;
  643. jack_log("OpenAUHAL capturing = %d playing = %d inchannels = %d outchannels = %d in_nChannels = %d out_nChannels = %d", capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels);
  644. if (inchannels == 0 && outchannels == 0) {
  645. jack_error("No input and output channels...");
  646. return -1;
  647. }
  648. // AUHAL
  649. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  650. Component HALOutput = FindNextComponent(NULL, &cd);
  651. err1 = OpenAComponent(HALOutput, &fAUHAL);
  652. if (err1 != noErr) {
  653. jack_error("Error calling OpenAComponent");
  654. printError(err1);
  655. goto error;
  656. }
  657. err1 = AudioUnitInitialize(fAUHAL);
  658. if (err1 != noErr) {
  659. jack_error("Cannot initialize AUHAL unit");
  660. printError(err1);
  661. goto error;
  662. }
  663. // Start I/O
  664. if (capturing && inchannels > 0) {
  665. enableIO = 1;
  666. jack_log("Setup AUHAL input on");
  667. } else {
  668. enableIO = 0;
  669. jack_log("Setup AUHAL input off");
  670. }
  671. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  672. if (err1 != noErr) {
  673. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  674. printError(err1);
  675. goto error;
  676. }
  677. if (playing && outchannels > 0) {
  678. enableIO = 1;
  679. jack_log("Setup AUHAL output on");
  680. } else {
  681. enableIO = 0;
  682. jack_log("Setup AUHAL output off");
  683. }
  684. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  685. if (err1 != noErr) {
  686. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  687. printError(err1);
  688. goto error;
  689. }
  690. size = sizeof(AudioDeviceID);
  691. err1 = AudioUnitGetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &currAudioDeviceID, &size);
  692. if (err1 != noErr) {
  693. jack_error("Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
  694. printError(err1);
  695. goto error;
  696. } else {
  697. jack_log("AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID);
  698. }
  699. // Setup up choosen device, in both input and output cases
  700. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  701. if (err1 != noErr) {
  702. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  703. printError(err1);
  704. goto error;
  705. }
  706. // Set buffer size
  707. if (capturing && inchannels > 0) {
  708. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&buffer_size, sizeof(UInt32));
  709. if (err1 != noErr) {
  710. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  711. printError(err1);
  712. goto error;
  713. }
  714. }
  715. if (playing && outchannels > 0) {
  716. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&buffer_size, sizeof(UInt32));
  717. if (err1 != noErr) {
  718. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  719. printError(err1);
  720. goto error;
  721. }
  722. }
  723. // Setup channel map
  724. if (capturing && inchannels > 0 && inchannels < in_nChannels) {
  725. SInt32 chanArr[in_nChannels];
  726. for (int i = 0; i < in_nChannels; i++) {
  727. chanArr[i] = -1;
  728. }
  729. for (int i = 0; i < inchannels; i++) {
  730. chanArr[i] = i;
  731. }
  732. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  733. if (err1 != noErr) {
  734. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  735. printError(err1);
  736. goto error;
  737. }
  738. }
  739. if (playing && outchannels > 0 && outchannels < out_nChannels) {
  740. SInt32 chanArr[out_nChannels];
  741. for (int i = 0; i < out_nChannels; i++) {
  742. chanArr[i] = -1;
  743. }
  744. for (int i = 0; i < outchannels; i++) {
  745. chanArr[i] = i;
  746. }
  747. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  748. if (err1 != noErr) {
  749. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  750. printError(err1);
  751. goto error;
  752. }
  753. }
  754. // Setup stream converters
  755. if (capturing && inchannels > 0) {
  756. size = sizeof(AudioStreamBasicDescription);
  757. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &srcFormat, &size);
  758. if (err1 != noErr) {
  759. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  760. printError(err1);
  761. goto error;
  762. }
  763. PrintStreamDesc(&srcFormat);
  764. jack_log("Setup AUHAL input stream converter SR = %ld", samplerate);
  765. srcFormat.mSampleRate = samplerate;
  766. srcFormat.mFormatID = kAudioFormatLinearPCM;
  767. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  768. srcFormat.mBytesPerPacket = sizeof(float);
  769. srcFormat.mFramesPerPacket = 1;
  770. srcFormat.mBytesPerFrame = sizeof(float);
  771. srcFormat.mChannelsPerFrame = inchannels;
  772. srcFormat.mBitsPerChannel = 32;
  773. PrintStreamDesc(&srcFormat);
  774. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, sizeof(AudioStreamBasicDescription));
  775. if (err1 != noErr) {
  776. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  777. printError(err1);
  778. goto error;
  779. }
  780. }
  781. if (playing && outchannels > 0) {
  782. size = sizeof(AudioStreamBasicDescription);
  783. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &dstFormat, &size);
  784. if (err1 != noErr) {
  785. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  786. printError(err1);
  787. goto error;
  788. }
  789. PrintStreamDesc(&dstFormat);
  790. jack_log("Setup AUHAL output stream converter SR = %ld", samplerate);
  791. dstFormat.mSampleRate = samplerate;
  792. dstFormat.mFormatID = kAudioFormatLinearPCM;
  793. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  794. dstFormat.mBytesPerPacket = sizeof(float);
  795. dstFormat.mFramesPerPacket = 1;
  796. dstFormat.mBytesPerFrame = sizeof(float);
  797. dstFormat.mChannelsPerFrame = outchannels;
  798. dstFormat.mBitsPerChannel = 32;
  799. PrintStreamDesc(&dstFormat);
  800. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, sizeof(AudioStreamBasicDescription));
  801. if (err1 != noErr) {
  802. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  803. printError(err1);
  804. goto error;
  805. }
  806. }
  807. // Setup callbacks
  808. if (inchannels > 0 && outchannels == 0) {
  809. AURenderCallbackStruct output;
  810. output.inputProc = Render;
  811. output.inputProcRefCon = this;
  812. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  813. if (err1 != noErr) {
  814. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  815. printError(err1);
  816. goto error;
  817. }
  818. } else {
  819. AURenderCallbackStruct output;
  820. output.inputProc = Render;
  821. output.inputProcRefCon = this;
  822. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  823. if (err1 != noErr) {
  824. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  825. printError(err1);
  826. goto error;
  827. }
  828. }
  829. return 0;
  830. error:
  831. CloseAUHAL();
  832. return -1;
  833. }
  834. OSStatus JackCoreAudioAdapter::DestroyAggregateDevice()
  835. {
  836. OSStatus osErr = noErr;
  837. AudioObjectPropertyAddress pluginAOPA;
  838. pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice;
  839. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  840. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  841. UInt32 outDataSize;
  842. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  843. if (osErr != noErr) {
  844. jack_error("JackCoreAudioDriver::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
  845. printError(osErr);
  846. return osErr;
  847. }
  848. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID);
  849. if (osErr != noErr) {
  850. jack_error("JackCoreAudioDriver::DestroyAggregateDevice : AudioObjectGetPropertyData error");
  851. printError(osErr);
  852. return osErr;
  853. }
  854. return noErr;
  855. }
  856. static CFStringRef GetDeviceName(AudioDeviceID id)
  857. {
  858. UInt32 size = sizeof(CFStringRef);
  859. CFStringRef UIname;
  860. OSStatus err = AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  861. return (err == noErr) ? UIname : NULL;
  862. }
  863. OSStatus JackCoreAudioAdapter::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
  864. {
  865. OSStatus osErr = noErr;
  866. UInt32 outSize;
  867. Boolean outWritable;
  868. // Check devices...
  869. if (IsAggregateDevice(captureDeviceID) || IsAggregateDevice(playbackDeviceID)) {
  870. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : cannot agregate devices that are already aggregate devices...");
  871. return -1;
  872. }
  873. //---------------------------------------------------------------------------
  874. // Setup SR of both devices otherwise creating AD may fail...
  875. //---------------------------------------------------------------------------
  876. if (SetupSampleRateAux(captureDeviceID, samplerate) < 0) {
  877. jack_error("JackCoreAudioDriver::CreateAggregateDevice : cannot set SR of input device");
  878. }
  879. if (SetupSampleRateAux(playbackDeviceID, samplerate) < 0) {
  880. jack_error("JackCoreAudioDriver::CreateAggregateDevice : cannot set SR of output device");
  881. }
  882. //---------------------------------------------------------------------------
  883. // Start to create a new aggregate by getting the base audio hardware plugin
  884. //---------------------------------------------------------------------------
  885. char capture_name[256];
  886. char playback_name[256];
  887. GetDeviceNameFromID(captureDeviceID, capture_name);
  888. GetDeviceNameFromID(playbackDeviceID, playback_name);
  889. jack_info("Separated input = '%s' and output = '%s' devices, create a private aggregate device to handle them...", capture_name, playback_name);
  890. osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable);
  891. if (osErr != noErr)
  892. return osErr;
  893. AudioValueTranslation pluginAVT;
  894. CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
  895. pluginAVT.mInputData = &inBundleRef;
  896. pluginAVT.mInputDataSize = sizeof(inBundleRef);
  897. pluginAVT.mOutputData = &fPluginID;
  898. pluginAVT.mOutputDataSize = sizeof(fPluginID);
  899. osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT);
  900. if (osErr != noErr)
  901. return osErr;
  902. //-------------------------------------------------
  903. // Create a CFDictionary for our aggregate device
  904. //-------------------------------------------------
  905. CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  906. CFStringRef AggregateDeviceNameRef = CFSTR("JackDuplex");
  907. CFStringRef AggregateDeviceUIDRef = CFSTR("com.grame.JackDuplex");
  908. // add the name of the device to the dictionary
  909. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
  910. // add our choice of UID for the aggregate device to the dictionary
  911. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
  912. // add a "private aggregate key" to the dictionary
  913. int value = 1;
  914. CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
  915. SInt32 system;
  916. Gestalt(gestaltSystemVersion, &system);
  917. jack_log("JackCoreAudioDriver::CreateAggregateDevice : system version = %x limit = %x", system, 0x00001054);
  918. // Starting with 10.5.4 systems, the AD can be internal... (better)
  919. if (system < 0x00001054) {
  920. jack_log("JackCoreAudioDriver::CreateAggregateDevice : public aggregate device....");
  921. } else {
  922. jack_log("JackCoreAudioDriver::CreateAggregateDevice : private aggregate device....");
  923. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
  924. }
  925. //-------------------------------------------------
  926. // Create a CFMutableArray for our sub-device list
  927. //-------------------------------------------------
  928. CFStringRef captureDeviceUID = GetDeviceName(captureDeviceID);
  929. CFStringRef playbackDeviceUID = GetDeviceName(playbackDeviceID);
  930. if (captureDeviceUID == NULL || playbackDeviceUID == NULL)
  931. return -1;
  932. // we need to append the UID for each device to a CFMutableArray, so create one here
  933. CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  934. // two sub-devices in this example, so append the sub-device's UID to the CFArray
  935. CFArrayAppendValue(subDevicesArray, captureDeviceUID);
  936. CFArrayAppendValue(subDevicesArray, playbackDeviceUID);
  937. //-----------------------------------------------------------------------
  938. // Feed the dictionary to the plugin, to create a blank aggregate device
  939. //-----------------------------------------------------------------------
  940. AudioObjectPropertyAddress pluginAOPA;
  941. pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
  942. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  943. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  944. UInt32 outDataSize;
  945. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  946. if (osErr != noErr)
  947. return osErr;
  948. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, outAggregateDevice);
  949. if (osErr != noErr)
  950. return osErr;
  951. // pause for a bit to make sure that everything completed correctly
  952. // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
  953. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  954. //-------------------------
  955. // Set the sub-device list
  956. //-------------------------
  957. pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
  958. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  959. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  960. outDataSize = sizeof(CFMutableArrayRef);
  961. osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
  962. if (osErr != noErr)
  963. return osErr;
  964. // pause again to give the changes time to take effect
  965. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  966. //-----------------------
  967. // Set the master device
  968. //-----------------------
  969. // set the master device manually (this is the device which will act as the master clock for the aggregate device)
  970. // pass in the UID of the device you want to use
  971. pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
  972. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  973. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  974. outDataSize = sizeof(CFStringRef);
  975. osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID); // capture is master...
  976. if (osErr != noErr)
  977. return osErr;
  978. // pause again to give the changes time to take effect
  979. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  980. //----------
  981. // Clean up
  982. //----------
  983. CFRelease(AggregateDeviceNumberRef);
  984. // release the CF objects we have created - we don't need them any more
  985. CFRelease(aggDeviceDict);
  986. CFRelease(subDevicesArray);
  987. // release the device UID
  988. CFRelease(captureDeviceUID);
  989. CFRelease(playbackDeviceUID);
  990. jack_log("New aggregate device %ld", *outAggregateDevice);
  991. return noErr;
  992. }
  993. bool JackCoreAudioAdapter::IsAggregateDevice(AudioDeviceID device)
  994. {
  995. OSStatus err = noErr;
  996. AudioObjectID sub_device[32];
  997. UInt32 outSize = sizeof(sub_device);
  998. err = AudioDeviceGetProperty(device, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  999. if (err != noErr) {
  1000. jack_log("Device does not have subdevices");
  1001. return false;
  1002. } else {
  1003. int num_devices = outSize / sizeof(AudioObjectID);
  1004. jack_log("Device does has %d subdevices", num_devices);
  1005. return true;
  1006. }
  1007. }
  1008. void JackCoreAudioAdapter::CloseAUHAL()
  1009. {
  1010. AudioUnitUninitialize(fAUHAL);
  1011. CloseComponent(fAUHAL);
  1012. }
  1013. int JackCoreAudioAdapter::Open()
  1014. {
  1015. return (AudioOutputUnitStart(fAUHAL) != noErr) ? -1 : 0;
  1016. }
  1017. int JackCoreAudioAdapter::Close()
  1018. {
  1019. #ifdef JACK_MONITOR
  1020. fTable.Save(fHostBufferSize, fHostSampleRate, fAdaptedSampleRate, fAdaptedBufferSize);
  1021. #endif
  1022. AudioOutputUnitStop(fAUHAL);
  1023. DisposeBuffers();
  1024. CloseAUHAL();
  1025. RemoveListeners();
  1026. if (fPluginID > 0)
  1027. DestroyAggregateDevice();
  1028. return 0;
  1029. }
  1030. int JackCoreAudioAdapter::SetSampleRate ( jack_nframes_t sample_rate ) {
  1031. JackAudioAdapterInterface::SetHostSampleRate ( sample_rate );
  1032. Close();
  1033. return Open();
  1034. }
  1035. int JackCoreAudioAdapter::SetBufferSize ( jack_nframes_t buffer_size ) {
  1036. JackAudioAdapterInterface::SetHostBufferSize ( buffer_size );
  1037. Close();
  1038. return Open();
  1039. }
  1040. } // namespace
  1041. #ifdef __cplusplus
  1042. extern "C"
  1043. {
  1044. #endif
  1045. SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor()
  1046. {
  1047. jack_driver_desc_t *desc;
  1048. unsigned int i;
  1049. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  1050. strcpy(desc->name, "audioadapter"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  1051. strcpy(desc->desc, "netjack audio <==> net backend adapter"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
  1052. desc->nparams = 12;
  1053. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  1054. i = 0;
  1055. strcpy(desc->params[i].name, "channels");
  1056. desc->params[i].character = 'c';
  1057. desc->params[i].type = JackDriverParamInt;
  1058. desc->params[i].value.ui = -1;
  1059. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  1060. strcpy(desc->params[i].long_desc, "Maximum number of channels. If -1, max possible number of channels will be used");
  1061. i++;
  1062. strcpy(desc->params[i].name, "inchannels");
  1063. desc->params[i].character = 'i';
  1064. desc->params[i].type = JackDriverParamInt;
  1065. desc->params[i].value.ui = -1;
  1066. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  1067. strcpy(desc->params[i].long_desc, "Maximum number of input channels. If -1, max possible number of input channels will be used");
  1068. i++;
  1069. strcpy(desc->params[i].name, "outchannels");
  1070. desc->params[i].character = 'o';
  1071. desc->params[i].type = JackDriverParamInt;
  1072. desc->params[i].value.ui = -1;
  1073. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  1074. strcpy(desc->params[i].long_desc, "Maximum number of output channels. If -1, max possible number of output channels will be used");
  1075. i++;
  1076. strcpy(desc->params[i].name, "capture");
  1077. desc->params[i].character = 'C';
  1078. desc->params[i].type = JackDriverParamString;
  1079. strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
  1080. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
  1081. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1082. i++;
  1083. strcpy(desc->params[i].name, "playback");
  1084. desc->params[i].character = 'P';
  1085. desc->params[i].type = JackDriverParamString;
  1086. strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
  1087. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
  1088. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1089. i++;
  1090. strcpy(desc->params[i].name, "rate");
  1091. desc->params[i].character = 'r';
  1092. desc->params[i].type = JackDriverParamUInt;
  1093. desc->params[i].value.ui = 44100U;
  1094. strcpy(desc->params[i].short_desc, "Sample rate");
  1095. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1096. i++;
  1097. strcpy(desc->params[i].name, "periodsize");
  1098. desc->params[i].character = 'p';
  1099. desc->params[i].type = JackDriverParamUInt;
  1100. desc->params[i].value.ui = 512U;
  1101. strcpy(desc->params[i].short_desc, "Period size");
  1102. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1103. i++;
  1104. strcpy(desc->params[i].name, "duplex");
  1105. desc->params[i].character = 'D';
  1106. desc->params[i].type = JackDriverParamBool;
  1107. desc->params[i].value.i = TRUE;
  1108. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  1109. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1110. i++;
  1111. strcpy(desc->params[i].name, "device");
  1112. desc->params[i].character = 'd';
  1113. desc->params[i].type = JackDriverParamString;
  1114. strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
  1115. strcpy(desc->params[i].short_desc, "CoreAudio device name");
  1116. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1117. i++;
  1118. strcpy(desc->params[i].name, "list-devices");
  1119. desc->params[i].character = 'l';
  1120. desc->params[i].type = JackDriverParamBool;
  1121. desc->params[i].value.i = TRUE;
  1122. strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
  1123. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1124. i++;
  1125. strcpy(desc->params[i].name, "quality");
  1126. desc->params[i].character = 'q';
  1127. desc->params[i].type = JackDriverParamInt;
  1128. desc->params[i].value.ui = 0;
  1129. strcpy(desc->params[i].short_desc, "Resample algorithm quality (0 - 4)");
  1130. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1131. i++;
  1132. strcpy(desc->params[i].name, "ring-buffer");
  1133. desc->params[i].character = 'g';
  1134. desc->params[i].type = JackDriverParamInt;
  1135. desc->params[i].value.ui = 32768;
  1136. strcpy(desc->params[i].short_desc, "Fixed ringbuffer size");
  1137. strcpy(desc->params[i].long_desc, "Fixed ringbuffer size (if not set => automatic adaptative)");
  1138. return desc;
  1139. }
  1140. #ifdef __cplusplus
  1141. }
  1142. #endif