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.

1755 lines
66KB

  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. }
  45. deviceNum = size / sizeof(AudioDeviceID);
  46. AudioDeviceID devices[deviceNum];
  47. err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
  48. if (err != noErr) {
  49. return err;
  50. }
  51. for (i = 0; i < deviceNum; i++) {
  52. char device_name[256];
  53. char internal_name[256];
  54. size = sizeof(CFStringRef);
  55. UIname = NULL;
  56. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  57. if (err == noErr) {
  58. CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
  59. } else {
  60. goto error;
  61. }
  62. size = 256;
  63. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
  64. if (err != noErr) {
  65. return err;
  66. }
  67. jack_info("Device name = \'%s\', internal_name = \'%s\' (to be used as -C, -P, or -d parameter)", device_name, internal_name);
  68. }
  69. return noErr;
  70. error:
  71. if (UIname != NULL) {
  72. CFRelease(UIname);
  73. }
  74. return err;
  75. }
  76. static void printError(OSStatus err)
  77. {
  78. switch (err) {
  79. case kAudioHardwareNoError:
  80. jack_log("error code : kAudioHardwareNoError");
  81. break;
  82. case kAudioConverterErr_FormatNotSupported:
  83. jack_log("error code : kAudioConverterErr_FormatNotSupported");
  84. break;
  85. case kAudioConverterErr_OperationNotSupported:
  86. jack_log("error code : kAudioConverterErr_OperationNotSupported");
  87. break;
  88. case kAudioConverterErr_PropertyNotSupported:
  89. jack_log("error code : kAudioConverterErr_PropertyNotSupported");
  90. break;
  91. case kAudioConverterErr_InvalidInputSize:
  92. jack_log("error code : kAudioConverterErr_InvalidInputSize");
  93. break;
  94. case kAudioConverterErr_InvalidOutputSize:
  95. jack_log("error code : kAudioConverterErr_InvalidOutputSize");
  96. break;
  97. case kAudioConverterErr_UnspecifiedError:
  98. jack_log("error code : kAudioConverterErr_UnspecifiedError");
  99. break;
  100. case kAudioConverterErr_BadPropertySizeError:
  101. jack_log("error code : kAudioConverterErr_BadPropertySizeError");
  102. break;
  103. case kAudioConverterErr_RequiresPacketDescriptionsError:
  104. jack_log("error code : kAudioConverterErr_RequiresPacketDescriptionsError");
  105. break;
  106. case kAudioConverterErr_InputSampleRateOutOfRange:
  107. jack_log("error code : kAudioConverterErr_InputSampleRateOutOfRange");
  108. break;
  109. case kAudioConverterErr_OutputSampleRateOutOfRange:
  110. jack_log("error code : kAudioConverterErr_OutputSampleRateOutOfRange");
  111. break;
  112. case kAudioHardwareNotRunningError:
  113. jack_log("error code : kAudioHardwareNotRunningError");
  114. break;
  115. case kAudioHardwareUnknownPropertyError:
  116. jack_log("error code : kAudioHardwareUnknownPropertyError");
  117. break;
  118. case kAudioHardwareIllegalOperationError:
  119. jack_log("error code : kAudioHardwareIllegalOperationError");
  120. break;
  121. case kAudioHardwareBadDeviceError:
  122. jack_log("error code : kAudioHardwareBadDeviceError");
  123. break;
  124. case kAudioHardwareBadStreamError:
  125. jack_log("error code : kAudioHardwareBadStreamError");
  126. break;
  127. case kAudioDeviceUnsupportedFormatError:
  128. jack_log("error code : kAudioDeviceUnsupportedFormatError");
  129. break;
  130. case kAudioDevicePermissionsError:
  131. jack_log("error code : kAudioDevicePermissionsError");
  132. break;
  133. case kAudioHardwareBadObjectError:
  134. jack_log("error code : kAudioHardwareBadObjectError");
  135. break;
  136. case kAudioHardwareUnsupportedOperationError:
  137. jack_log("error code : kAudioHardwareUnsupportedOperationError");
  138. break;
  139. default:
  140. jack_log("error code : unknown");
  141. break;
  142. }
  143. }
  144. OSStatus JackCoreAudioAdapter::AudioHardwareNotificationCallback(AudioHardwarePropertyID inPropertyID, void* inClientData)
  145. {
  146. JackCoreAudioAdapter* driver = (JackCoreAudioAdapter*)inClientData;
  147. switch (inPropertyID) {
  148. case kAudioHardwarePropertyDevices: {
  149. jack_log("JackCoreAudioAdapter::AudioHardwareNotificationCallback kAudioHardwarePropertyDevices");
  150. DisplayDeviceNames();
  151. break;
  152. }
  153. }
  154. return noErr;
  155. }
  156. OSStatus JackCoreAudioAdapter::SRNotificationCallback(AudioDeviceID inDevice,
  157. UInt32 inChannel,
  158. Boolean isInput,
  159. AudioDevicePropertyID inPropertyID,
  160. void* inClientData)
  161. {
  162. JackCoreAudioAdapter* driver = static_cast<JackCoreAudioAdapter*>(inClientData);
  163. switch (inPropertyID) {
  164. case kAudioDevicePropertyNominalSampleRate: {
  165. jack_log("JackCoreAudioAdapter::SRNotificationCallback kAudioDevicePropertyNominalSampleRate");
  166. driver->fState = true;
  167. break;
  168. }
  169. }
  170. return noErr;
  171. }
  172. // A better implementation would try to recover in case of hardware device change (see HALLAB HLFilePlayerWindowControllerAudioDevicePropertyListenerProc code)
  173. OSStatus JackCoreAudioAdapter::DeviceNotificationCallback(AudioDeviceID inDevice,
  174. UInt32 inChannel,
  175. Boolean isInput,
  176. AudioDevicePropertyID inPropertyID,
  177. void* inClientData)
  178. {
  179. switch (inPropertyID) {
  180. case kAudioDeviceProcessorOverload: {
  181. jack_error("JackCoreAudioAdapter::DeviceNotificationCallback kAudioDeviceProcessorOverload");
  182. break;
  183. }
  184. case kAudioDevicePropertyStreamConfiguration: {
  185. jack_error("Cannot handle kAudioDevicePropertyStreamConfiguration");
  186. return kAudioHardwareUnsupportedOperationError;
  187. }
  188. case kAudioDevicePropertyNominalSampleRate: {
  189. jack_error("Cannot handle kAudioDevicePropertyNominalSampleRate");
  190. return kAudioHardwareUnsupportedOperationError;
  191. }
  192. }
  193. return noErr;
  194. }
  195. int JackCoreAudioAdapter::AddListeners()
  196. {
  197. OSStatus err = noErr;
  198. // Add listeners
  199. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
  200. if (err != noErr) {
  201. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  202. printError(err);
  203. return -1;
  204. }
  205. err = AudioHardwareAddPropertyListener(kAudioHardwarePropertyDevices, AudioHardwareNotificationCallback, this);
  206. if (err != noErr) {
  207. jack_error("Error calling AudioHardwareAddPropertyListener with kAudioHardwarePropertyDevices");
  208. printError(err);
  209. return -1;
  210. }
  211. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback, this);
  212. if (err != noErr) {
  213. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  214. printError(err);
  215. return -1;
  216. }
  217. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback, this);
  218. if (err != noErr) {
  219. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
  220. printError(err);
  221. return -1;
  222. }
  223. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  224. if (err != noErr) {
  225. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  226. printError(err);
  227. return -1;
  228. }
  229. err = AudioDeviceAddPropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  230. if (err != noErr) {
  231. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  232. printError(err);
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. void JackCoreAudioAdapter::RemoveListeners()
  238. {
  239. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
  240. AudioHardwareRemovePropertyListener(kAudioHardwarePropertyDevices, AudioHardwareNotificationCallback);
  241. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback);
  242. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback);
  243. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  244. AudioDeviceRemovePropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  245. }
  246. OSStatus JackCoreAudioAdapter::Render(void *inRefCon,
  247. AudioUnitRenderActionFlags *ioActionFlags,
  248. const AudioTimeStamp *inTimeStamp,
  249. UInt32 inBusNumber,
  250. UInt32 inNumberFrames,
  251. AudioBufferList *ioData)
  252. {
  253. JackCoreAudioAdapter* adapter = static_cast<JackCoreAudioAdapter*>(inRefCon);
  254. OSStatus err = AudioUnitRender(adapter->fAUHAL, ioActionFlags, inTimeStamp, 1, inNumberFrames, adapter->fInputData);
  255. if (err == noErr) {
  256. jack_default_audio_sample_t* inputBuffer[adapter->fCaptureChannels];
  257. jack_default_audio_sample_t* outputBuffer[adapter->fPlaybackChannels];
  258. for (int i = 0; i < adapter->fCaptureChannels; i++) {
  259. inputBuffer[i] = (jack_default_audio_sample_t*)adapter->fInputData->mBuffers[i].mData;
  260. }
  261. for (int i = 0; i < adapter->fPlaybackChannels; i++) {
  262. outputBuffer[i] = (jack_default_audio_sample_t*)ioData->mBuffers[i].mData;
  263. }
  264. adapter->PushAndPull((jack_default_audio_sample_t**)inputBuffer, (jack_default_audio_sample_t**)outputBuffer, inNumberFrames);
  265. return noErr;
  266. } else {
  267. return err;
  268. }
  269. }
  270. JackCoreAudioAdapter::JackCoreAudioAdapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params)
  271. :JackAudioAdapterInterface(buffer_size, sample_rate), fInputData(0), fCapturing(false), fPlaying(false), fState(false)
  272. {
  273. const JSList* node;
  274. const jack_driver_param_t* param;
  275. int in_nChannels = 0;
  276. int out_nChannels = 0;
  277. char captureName[256];
  278. char playbackName[256];
  279. fCaptureUID[0] = 0;
  280. fPlaybackUID[0] = 0;
  281. fClockDriftCompensate = false;
  282. // Default values
  283. fCaptureChannels = -1;
  284. fPlaybackChannels = -1;
  285. SInt32 major;
  286. SInt32 minor;
  287. Gestalt(gestaltSystemVersionMajor, &major);
  288. Gestalt(gestaltSystemVersionMinor, &minor);
  289. // Starting with 10.6 systems, the HAL notification thread is created internally
  290. if (major == 10 && minor >= 6) {
  291. CFRunLoopRef theRunLoop = NULL;
  292. AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  293. OSStatus theError = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
  294. if (theError != noErr) {
  295. jack_error("JackCoreAudioAdapter::Open kAudioHardwarePropertyRunLoop error");
  296. }
  297. }
  298. for (node = params; node; node = jack_slist_next(node)) {
  299. param = (const jack_driver_param_t*) node->data;
  300. switch (param->character) {
  301. case 'c' :
  302. fCaptureChannels = fPlaybackChannels = param->value.ui;
  303. break;
  304. case 'i':
  305. fCaptureChannels = param->value.ui;
  306. break;
  307. case 'o':
  308. fPlaybackChannels = param->value.ui;
  309. break;
  310. case 'C':
  311. fCapturing = true;
  312. strncpy(fCaptureUID, param->value.str, 256);
  313. break;
  314. case 'P':
  315. fPlaying = true;
  316. strncpy(fPlaybackUID, param->value.str, 256);
  317. break;
  318. case 'd':
  319. strncpy(fCaptureUID, param->value.str, 256);
  320. strncpy(fPlaybackUID, param->value.str, 256);
  321. break;
  322. case 'D':
  323. fCapturing = fPlaying = true;
  324. break;
  325. case 'r':
  326. SetAdaptedSampleRate(param->value.ui);
  327. break;
  328. case 'p':
  329. SetAdaptedBufferSize(param->value.ui);
  330. break;
  331. case 'l':
  332. DisplayDeviceNames();
  333. break;
  334. case 'q':
  335. fQuality = param->value.ui;
  336. break;
  337. case 'g':
  338. fRingbufferCurSize = param->value.ui;
  339. fAdaptative = false;
  340. break;
  341. case 's':
  342. fClockDriftCompensate = true;
  343. break;
  344. }
  345. }
  346. /* duplex is the default */
  347. if (!fCapturing && !fPlaying) {
  348. fCapturing = true;
  349. fPlaying = true;
  350. }
  351. if (SetupDevices(fCaptureUID, fPlaybackUID, captureName, playbackName, fAdaptedSampleRate) < 0) {
  352. throw std::bad_alloc();
  353. }
  354. if (SetupChannels(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, true) < 0) {
  355. throw std::bad_alloc();
  356. }
  357. if (SetupBufferSize(fAdaptedBufferSize) < 0) {
  358. throw std::bad_alloc();
  359. }
  360. if (SetupSampleRate(fAdaptedSampleRate) < 0) {
  361. throw std::bad_alloc();
  362. }
  363. if (OpenAUHAL(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, fAdaptedBufferSize, fAdaptedSampleRate) < 0) {
  364. throw std::bad_alloc();
  365. }
  366. if (fCapturing && fCaptureChannels > 0) {
  367. if (SetupBuffers(fCaptureChannels) < 0) {
  368. throw std::bad_alloc();
  369. }
  370. }
  371. if (AddListeners() < 0) {
  372. throw std::bad_alloc();
  373. }
  374. GetStreamLatencies(fDeviceID, true, fInputLatencies);
  375. GetStreamLatencies(fDeviceID, false, fOutputLatencies);
  376. }
  377. OSStatus JackCoreAudioAdapter::GetDefaultDevice(AudioDeviceID* id)
  378. {
  379. OSStatus res;
  380. UInt32 theSize = sizeof(UInt32);
  381. AudioDeviceID inDefault;
  382. AudioDeviceID outDefault;
  383. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
  384. return res;
  385. }
  386. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
  387. return res;
  388. }
  389. jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
  390. // Get the device only if default input and output are the same
  391. if (inDefault != outDefault) {
  392. jack_error("Default input and output devices are not the same !!");
  393. return kAudioHardwareBadDeviceError;
  394. } else if (inDefault == 0) {
  395. jack_error("Default input and output devices are null !!");
  396. return kAudioHardwareBadDeviceError;
  397. } else {
  398. *id = inDefault;
  399. return noErr;
  400. }
  401. }
  402. OSStatus JackCoreAudioAdapter::GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput)
  403. {
  404. OSStatus err = noErr;
  405. UInt32 outSize;
  406. Boolean outWritable;
  407. channelCount = 0;
  408. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  409. if (err == noErr) {
  410. AudioBufferList bufferList[outSize];
  411. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  412. if (err == noErr) {
  413. for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++) {
  414. channelCount += bufferList->mBuffers[i].mNumberChannels;
  415. }
  416. }
  417. }
  418. return err;
  419. }
  420. OSStatus JackCoreAudioAdapter::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
  421. {
  422. UInt32 size = sizeof(AudioValueTranslation);
  423. CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
  424. AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
  425. if (inIUD == NULL) {
  426. return kAudioHardwareUnspecifiedError;
  427. } else {
  428. OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
  429. CFRelease(inIUD);
  430. jack_log("GetDeviceIDFromUID %s %ld", UID, *id);
  431. return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
  432. }
  433. }
  434. OSStatus JackCoreAudioAdapter::GetDefaultInputDevice(AudioDeviceID* id)
  435. {
  436. OSStatus res;
  437. UInt32 theSize = sizeof(UInt32);
  438. AudioDeviceID inDefault;
  439. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
  440. return res;
  441. }
  442. if (inDefault == 0) {
  443. jack_error("Error: default input device is 0, please select a correct one !!");
  444. return -1;
  445. }
  446. jack_log("GetDefaultInputDevice: input = %ld ", inDefault);
  447. *id = inDefault;
  448. return noErr;
  449. }
  450. OSStatus JackCoreAudioAdapter::GetDefaultOutputDevice(AudioDeviceID* id)
  451. {
  452. OSStatus res;
  453. UInt32 theSize = sizeof(UInt32);
  454. AudioDeviceID outDefault;
  455. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
  456. return res;
  457. }
  458. if (outDefault == 0) {
  459. jack_error("Error: default output device is 0, please select a correct one !!");
  460. return -1;
  461. }
  462. jack_log("GetDefaultOutputDevice: output = %ld", outDefault);
  463. *id = outDefault;
  464. return noErr;
  465. }
  466. OSStatus JackCoreAudioAdapter::GetDeviceNameFromID(AudioDeviceID id, char* name)
  467. {
  468. UInt32 size = 256;
  469. return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
  470. }
  471. AudioDeviceID JackCoreAudioAdapter::GetDeviceIDFromName(const char* name)
  472. {
  473. UInt32 size;
  474. Boolean isWritable;
  475. int i, deviceNum;
  476. OSStatus err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
  477. if (err != noErr) {
  478. return -1;
  479. }
  480. deviceNum = size / sizeof(AudioDeviceID);
  481. AudioDeviceID devices[deviceNum];
  482. err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
  483. if (err != noErr) {
  484. return err;
  485. }
  486. for (i = 0; i < deviceNum; i++) {
  487. char device_name[256];
  488. size = 256;
  489. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
  490. if (err != noErr) {
  491. return -1;
  492. } else if (strcmp(device_name, name) == 0) {
  493. return devices[i];
  494. }
  495. }
  496. return -1;
  497. }
  498. // Setup
  499. int JackCoreAudioAdapter::SetupDevices(const char* capture_driver_uid,
  500. const char* playback_driver_uid,
  501. char* capture_driver_name,
  502. char* playback_driver_name,
  503. jack_nframes_t samplerate)
  504. {
  505. capture_driver_name[0] = 0;
  506. playback_driver_name[0] = 0;
  507. // Duplex
  508. if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
  509. jack_log("JackCoreAudioDriver::Open duplex");
  510. // Same device for capture and playback...
  511. if (strcmp(capture_driver_uid, playback_driver_uid) == 0) {
  512. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  513. jack_log("Will take default in/out");
  514. if (GetDefaultDevice(&fDeviceID) != noErr) {
  515. jack_error("Cannot open default device");
  516. return -1;
  517. }
  518. }
  519. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  520. jack_error("Cannot get device name from device ID");
  521. return -1;
  522. }
  523. } else {
  524. // Creates aggregate device
  525. AudioDeviceID captureID, playbackID;
  526. if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr) {
  527. jack_log("Will take default input");
  528. if (GetDefaultInputDevice(&captureID) != noErr) {
  529. jack_error("Cannot open default input device");
  530. return -1;
  531. }
  532. }
  533. if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr) {
  534. jack_log("Will take default output");
  535. if (GetDefaultOutputDevice(&playbackID) != noErr) {
  536. jack_error("Cannot open default output device");
  537. return -1;
  538. }
  539. }
  540. if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
  541. return -1;
  542. }
  543. }
  544. // Capture only
  545. } else if (strcmp(capture_driver_uid, "") != 0) {
  546. jack_log("JackCoreAudioAdapter::Open capture only");
  547. if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
  548. if (GetDefaultInputDevice(&fDeviceID) != noErr) {
  549. jack_error("Cannot open default input device");
  550. return -1;
  551. }
  552. }
  553. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
  554. jack_error("Cannot get device name from device ID");
  555. return -1;
  556. }
  557. // Playback only
  558. } else if (strcmp(playback_driver_uid, "") != 0) {
  559. jack_log("JackCoreAudioAdapter::Open playback only");
  560. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  561. if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
  562. jack_error("Cannot open default output device");
  563. return -1;
  564. }
  565. }
  566. if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  567. jack_error("Cannot get device name from device ID");
  568. return -1;
  569. }
  570. // Use default driver in duplex mode
  571. } else {
  572. jack_log("JackCoreAudioAdapter::Open default driver");
  573. if (GetDefaultDevice(&fDeviceID) != noErr) {
  574. jack_error("Cannot open default device in duplex mode, so aggregate default input and default output");
  575. // Creates aggregate device
  576. AudioDeviceID captureID = -1, playbackID = -1;
  577. if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr) {
  578. jack_log("Will take default input");
  579. if (GetDefaultInputDevice(&captureID) != noErr) {
  580. jack_error("Cannot open default input device");
  581. goto built_in;
  582. }
  583. }
  584. if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr) {
  585. jack_log("Will take default output");
  586. if (GetDefaultOutputDevice(&playbackID) != noErr) {
  587. jack_error("Cannot open default output device");
  588. goto built_in;
  589. }
  590. }
  591. if (captureID > 0 && playbackID > 0) {
  592. if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
  593. goto built_in;
  594. }
  595. } else {
  596. jack_error("Cannot use default input/output");
  597. goto built_in;
  598. }
  599. }
  600. }
  601. return 0;
  602. built_in:
  603. // Aggregate built-in input and output
  604. AudioDeviceID captureID = GetDeviceIDFromName("Built-in Input");
  605. AudioDeviceID playbackID = GetDeviceIDFromName("Built-in Output");
  606. if (captureID > 0 && playbackID > 0) {
  607. if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
  608. return -1;
  609. }
  610. } else {
  611. jack_error("Cannot aggregate built-in input and output");
  612. return -1;
  613. }
  614. return 0;
  615. }
  616. int JackCoreAudioAdapter::SetupChannels(bool capturing,
  617. bool playing,
  618. int& inchannels,
  619. int& outchannels,
  620. int& in_nChannels,
  621. int& out_nChannels,
  622. bool strict)
  623. {
  624. OSStatus err = noErr;
  625. if (capturing) {
  626. err = GetTotalChannels(fDeviceID, in_nChannels, true);
  627. if (err != noErr) {
  628. jack_error("Cannot get input channel number");
  629. printError(err);
  630. return -1;
  631. } else {
  632. jack_log("Max input channels : %d", in_nChannels);
  633. }
  634. }
  635. if (playing) {
  636. err = GetTotalChannels(fDeviceID, out_nChannels, false);
  637. if (err != noErr) {
  638. jack_error("Cannot get output channel number");
  639. printError(err);
  640. return -1;
  641. } else {
  642. jack_log("Max output channels : %d", out_nChannels);
  643. }
  644. }
  645. if (inchannels > in_nChannels) {
  646. jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
  647. if (strict) {
  648. return -1;
  649. }
  650. }
  651. if (outchannels > out_nChannels) {
  652. jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
  653. if (strict) {
  654. return -1;
  655. }
  656. }
  657. if (inchannels == -1) {
  658. jack_log("Setup max in channels = %ld", in_nChannels);
  659. inchannels = in_nChannels;
  660. }
  661. if (outchannels == -1) {
  662. jack_log("Setup max out channels = %ld", out_nChannels);
  663. outchannels = out_nChannels;
  664. }
  665. return 0;
  666. }
  667. int JackCoreAudioAdapter::SetupBufferSize(jack_nframes_t buffer_size)
  668. {
  669. // Setting buffer size
  670. UInt32 outSize = sizeof(UInt32);
  671. OSStatus err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
  672. if (err != noErr) {
  673. jack_error("Cannot set buffer size %ld", buffer_size);
  674. printError(err);
  675. return -1;
  676. }
  677. return 0;
  678. }
  679. int JackCoreAudioAdapter::SetupSampleRate(jack_nframes_t samplerate)
  680. {
  681. return SetupSampleRateAux(fDeviceID, samplerate);
  682. }
  683. int JackCoreAudioAdapter::SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate)
  684. {
  685. OSStatus err = noErr;
  686. UInt32 outSize;
  687. Float64 sampleRate;
  688. // Get sample rate
  689. outSize = sizeof(Float64);
  690. err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  691. if (err != noErr) {
  692. jack_error("Cannot get current sample rate");
  693. printError(err);
  694. return -1;
  695. } else {
  696. jack_log("Current sample rate = %f", sampleRate);
  697. }
  698. // If needed, set new sample rate
  699. if (samplerate != (jack_nframes_t)sampleRate) {
  700. sampleRate = (Float64)samplerate;
  701. // To get SR change notification
  702. err = AudioDeviceAddPropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
  703. if (err != noErr) {
  704. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  705. printError(err);
  706. return -1;
  707. }
  708. err = AudioDeviceSetProperty(inDevice, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
  709. if (err != noErr) {
  710. jack_error("Cannot set sample rate = %ld", samplerate);
  711. printError(err);
  712. return -1;
  713. }
  714. // Waiting for SR change notification
  715. int count = 0;
  716. while (!fState && count++ < WAIT_COUNTER) {
  717. usleep(100000);
  718. jack_log("Wait count = %d", count);
  719. }
  720. // Remove SR change notification
  721. AudioDeviceRemovePropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
  722. }
  723. return 0;
  724. }
  725. int JackCoreAudioAdapter::SetupBuffers(int inchannels)
  726. {
  727. jack_log("JackCoreAudioAdapter::SetupBuffers: input = %ld", inchannels);
  728. // Prepare buffers
  729. fInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  730. fInputData->mNumberBuffers = inchannels;
  731. for (int i = 0; i < fCaptureChannels; i++) {
  732. fInputData->mBuffers[i].mNumberChannels = 1;
  733. fInputData->mBuffers[i].mDataByteSize = fAdaptedBufferSize * sizeof(jack_default_audio_sample_t);
  734. fInputData->mBuffers[i].mData = malloc(fAdaptedBufferSize * sizeof(jack_default_audio_sample_t));
  735. }
  736. return 0;
  737. }
  738. void JackCoreAudioAdapter::DisposeBuffers()
  739. {
  740. if (fInputData) {
  741. for (int i = 0; i < fCaptureChannels; i++) {
  742. free(fInputData->mBuffers[i].mData);
  743. }
  744. free(fInputData);
  745. fInputData = 0;
  746. }
  747. }
  748. int JackCoreAudioAdapter::OpenAUHAL(bool capturing,
  749. bool playing,
  750. int inchannels,
  751. int outchannels,
  752. int in_nChannels,
  753. int out_nChannels,
  754. jack_nframes_t buffer_size,
  755. jack_nframes_t samplerate)
  756. {
  757. ComponentResult err1;
  758. UInt32 enableIO;
  759. AudioStreamBasicDescription srcFormat, dstFormat;
  760. AudioDeviceID currAudioDeviceID;
  761. UInt32 size;
  762. 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);
  763. if (inchannels == 0 && outchannels == 0) {
  764. jack_error("No input and output channels...");
  765. return -1;
  766. }
  767. // AUHAL
  768. SInt32 major;
  769. SInt32 minor;
  770. Gestalt(gestaltSystemVersionMajor, &major);
  771. Gestalt(gestaltSystemVersionMinor, &minor);
  772. if (major == 10 && minor >= 6) {
  773. AudioComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  774. AudioComponent HALOutput = AudioComponentFindNext(NULL, &cd);
  775. err1 = AudioComponentInstanceNew(HALOutput, &fAUHAL);
  776. if (err1 != noErr) {
  777. jack_error("Error calling AudioComponentInstanceNew");
  778. printError(err1);
  779. goto error;
  780. }
  781. } else {
  782. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  783. Component HALOutput = FindNextComponent(NULL, &cd);
  784. err1 = OpenAComponent(HALOutput, &fAUHAL);
  785. if (err1 != noErr) {
  786. jack_error("Error calling OpenAComponent");
  787. printError(err1);
  788. goto error;
  789. }
  790. }
  791. err1 = AudioUnitInitialize(fAUHAL);
  792. if (err1 != noErr) {
  793. jack_error("Cannot initialize AUHAL unit");
  794. printError(err1);
  795. goto error;
  796. }
  797. // Start I/O
  798. if (capturing && inchannels > 0) {
  799. enableIO = 1;
  800. jack_log("Setup AUHAL input on");
  801. } else {
  802. enableIO = 0;
  803. jack_log("Setup AUHAL input off");
  804. }
  805. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  806. if (err1 != noErr) {
  807. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  808. printError(err1);
  809. goto error;
  810. }
  811. if (playing && outchannels > 0) {
  812. enableIO = 1;
  813. jack_log("Setup AUHAL output on");
  814. } else {
  815. enableIO = 0;
  816. jack_log("Setup AUHAL output off");
  817. }
  818. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  819. if (err1 != noErr) {
  820. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  821. printError(err1);
  822. goto error;
  823. }
  824. size = sizeof(AudioDeviceID);
  825. err1 = AudioUnitGetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &currAudioDeviceID, &size);
  826. if (err1 != noErr) {
  827. jack_error("Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
  828. printError(err1);
  829. goto error;
  830. } else {
  831. jack_log("AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID);
  832. }
  833. // Setup up choosen device, in both input and output cases
  834. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  835. if (err1 != noErr) {
  836. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  837. printError(err1);
  838. goto error;
  839. }
  840. // Set buffer size
  841. if (capturing && inchannels > 0) {
  842. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&buffer_size, sizeof(UInt32));
  843. if (err1 != noErr) {
  844. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  845. printError(err1);
  846. goto error;
  847. }
  848. }
  849. if (playing && outchannels > 0) {
  850. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&buffer_size, sizeof(UInt32));
  851. if (err1 != noErr) {
  852. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  853. printError(err1);
  854. goto error;
  855. }
  856. }
  857. // Setup channel map
  858. if (capturing && inchannels > 0 && inchannels <= in_nChannels) {
  859. SInt32 chanArr[in_nChannels];
  860. for (int i = 0; i < in_nChannels; i++) {
  861. chanArr[i] = -1;
  862. }
  863. for (int i = 0; i < inchannels; i++) {
  864. chanArr[i] = i;
  865. }
  866. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  867. if (err1 != noErr) {
  868. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  869. printError(err1);
  870. goto error;
  871. }
  872. }
  873. if (playing && outchannels > 0 && outchannels <= out_nChannels) {
  874. SInt32 chanArr[out_nChannels];
  875. for (int i = 0; i < out_nChannels; i++) {
  876. chanArr[i] = -1;
  877. }
  878. for (int i = 0; i < outchannels; i++) {
  879. chanArr[i] = i;
  880. }
  881. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  882. if (err1 != noErr) {
  883. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  884. printError(err1);
  885. goto error;
  886. }
  887. }
  888. // Setup stream converters
  889. if (capturing && inchannels > 0) {
  890. size = sizeof(AudioStreamBasicDescription);
  891. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &srcFormat, &size);
  892. if (err1 != noErr) {
  893. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  894. printError(err1);
  895. goto error;
  896. }
  897. PrintStreamDesc(&srcFormat);
  898. jack_log("Setup AUHAL input stream converter SR = %ld", samplerate);
  899. srcFormat.mSampleRate = samplerate;
  900. srcFormat.mFormatID = kAudioFormatLinearPCM;
  901. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  902. srcFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
  903. srcFormat.mFramesPerPacket = 1;
  904. srcFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
  905. srcFormat.mChannelsPerFrame = inchannels;
  906. srcFormat.mBitsPerChannel = 32;
  907. PrintStreamDesc(&srcFormat);
  908. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, sizeof(AudioStreamBasicDescription));
  909. if (err1 != noErr) {
  910. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  911. printError(err1);
  912. goto error;
  913. }
  914. }
  915. if (playing && outchannels > 0) {
  916. size = sizeof(AudioStreamBasicDescription);
  917. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &dstFormat, &size);
  918. if (err1 != noErr) {
  919. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  920. printError(err1);
  921. goto error;
  922. }
  923. PrintStreamDesc(&dstFormat);
  924. jack_log("Setup AUHAL output stream converter SR = %ld", samplerate);
  925. dstFormat.mSampleRate = samplerate;
  926. dstFormat.mFormatID = kAudioFormatLinearPCM;
  927. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  928. dstFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
  929. dstFormat.mFramesPerPacket = 1;
  930. dstFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
  931. dstFormat.mChannelsPerFrame = outchannels;
  932. dstFormat.mBitsPerChannel = 32;
  933. PrintStreamDesc(&dstFormat);
  934. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, sizeof(AudioStreamBasicDescription));
  935. if (err1 != noErr) {
  936. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  937. printError(err1);
  938. goto error;
  939. }
  940. }
  941. // Setup callbacks
  942. if (inchannels > 0 && outchannels == 0) {
  943. AURenderCallbackStruct output;
  944. output.inputProc = Render;
  945. output.inputProcRefCon = this;
  946. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  947. if (err1 != noErr) {
  948. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  949. printError(err1);
  950. goto error;
  951. }
  952. } else {
  953. AURenderCallbackStruct output;
  954. output.inputProc = Render;
  955. output.inputProcRefCon = this;
  956. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  957. if (err1 != noErr) {
  958. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  959. printError(err1);
  960. goto error;
  961. }
  962. }
  963. return 0;
  964. error:
  965. CloseAUHAL();
  966. return -1;
  967. }
  968. OSStatus JackCoreAudioAdapter::DestroyAggregateDevice()
  969. {
  970. OSStatus osErr = noErr;
  971. AudioObjectPropertyAddress pluginAOPA;
  972. pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice;
  973. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  974. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  975. UInt32 outDataSize;
  976. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  977. if (osErr != noErr) {
  978. jack_error("JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
  979. printError(osErr);
  980. return osErr;
  981. }
  982. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID);
  983. if (osErr != noErr) {
  984. jack_error("JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyData error");
  985. printError(osErr);
  986. return osErr;
  987. }
  988. return noErr;
  989. }
  990. static CFStringRef GetDeviceName(AudioDeviceID id)
  991. {
  992. UInt32 size = sizeof(CFStringRef);
  993. CFStringRef UIname;
  994. OSStatus err = AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  995. return (err == noErr) ? UIname : NULL;
  996. }
  997. OSStatus JackCoreAudioAdapter::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
  998. {
  999. OSStatus err = noErr;
  1000. AudioObjectID sub_device[32];
  1001. UInt32 outSize = sizeof(sub_device);
  1002. err = AudioDeviceGetProperty(captureDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  1003. vector<AudioDeviceID> captureDeviceIDArray;
  1004. if (err != noErr) {
  1005. jack_log("Input device does not have subdevices");
  1006. captureDeviceIDArray.push_back(captureDeviceID);
  1007. } else {
  1008. int num_devices = outSize / sizeof(AudioObjectID);
  1009. jack_log("Input device has %d subdevices", num_devices);
  1010. for (int i = 0; i < num_devices; i++) {
  1011. captureDeviceIDArray.push_back(sub_device[i]);
  1012. }
  1013. }
  1014. err = AudioDeviceGetProperty(playbackDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  1015. vector<AudioDeviceID> playbackDeviceIDArray;
  1016. if (err != noErr) {
  1017. jack_log("Output device does not have subdevices");
  1018. playbackDeviceIDArray.push_back(playbackDeviceID);
  1019. } else {
  1020. int num_devices = outSize / sizeof(AudioObjectID);
  1021. jack_log("Output device has %d subdevices", num_devices);
  1022. for (int i = 0; i < num_devices; i++) {
  1023. playbackDeviceIDArray.push_back(sub_device[i]);
  1024. }
  1025. }
  1026. return CreateAggregateDeviceAux(captureDeviceIDArray, playbackDeviceIDArray, samplerate, outAggregateDevice);
  1027. }
  1028. OSStatus JackCoreAudioAdapter::CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
  1029. {
  1030. OSStatus osErr = noErr;
  1031. UInt32 outSize;
  1032. Boolean outWritable;
  1033. // Prepare sub-devices for clock drift compensation
  1034. // Workaround for bug in the HAL : until 10.6.2
  1035. AudioObjectPropertyAddress theAddressOwned = { kAudioObjectPropertyOwnedObjects, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  1036. AudioObjectPropertyAddress theAddressDrift = { kAudioSubDevicePropertyDriftCompensation, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  1037. UInt32 theQualifierDataSize = sizeof(AudioObjectID);
  1038. AudioClassID inClass = kAudioSubDeviceClassID;
  1039. void* theQualifierData = &inClass;
  1040. UInt32 subDevicesNum = 0;
  1041. //---------------------------------------------------------------------------
  1042. // Setup SR of both devices otherwise creating AD may fail...
  1043. //---------------------------------------------------------------------------
  1044. UInt32 keptclockdomain = 0;
  1045. UInt32 clockdomain = 0;
  1046. outSize = sizeof(UInt32);
  1047. bool need_clock_drift_compensation = false;
  1048. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  1049. if (SetupSampleRateAux(captureDeviceID[i], samplerate) < 0) {
  1050. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : cannot set SR of input device");
  1051. } else {
  1052. // Check clock domain
  1053. osErr = AudioDeviceGetProperty(captureDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
  1054. if (osErr != 0) {
  1055. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
  1056. printError(osErr);
  1057. } else {
  1058. keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
  1059. jack_log("JackCoreAudioAdapter::CreateAggregateDevice : input clockdomain = %d", clockdomain);
  1060. if (clockdomain != 0 && clockdomain != keptclockdomain) {
  1061. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...");
  1062. need_clock_drift_compensation = true;
  1063. }
  1064. }
  1065. }
  1066. }
  1067. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  1068. if (SetupSampleRateAux(playbackDeviceID[i], samplerate) < 0) {
  1069. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : cannot set SR of output device");
  1070. } else {
  1071. // Check clock domain
  1072. osErr = AudioDeviceGetProperty(playbackDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
  1073. if (osErr != 0) {
  1074. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
  1075. printError(osErr);
  1076. } else {
  1077. keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
  1078. jack_log("JackCoreAudioAdapter::CreateAggregateDevice : output clockdomain = %d", clockdomain);
  1079. if (clockdomain != 0 && clockdomain != keptclockdomain) {
  1080. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...");
  1081. need_clock_drift_compensation = true;
  1082. }
  1083. }
  1084. }
  1085. }
  1086. // If no valid clock domain was found, then assume we have to compensate...
  1087. if (keptclockdomain == 0) {
  1088. need_clock_drift_compensation = true;
  1089. }
  1090. //---------------------------------------------------------------------------
  1091. // Start to create a new aggregate by getting the base audio hardware plugin
  1092. //---------------------------------------------------------------------------
  1093. char device_name[256];
  1094. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  1095. GetDeviceNameFromID(captureDeviceID[i], device_name);
  1096. jack_info("Separated input = '%s' ", device_name);
  1097. }
  1098. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  1099. GetDeviceNameFromID(playbackDeviceID[i], device_name);
  1100. jack_info("Separated output = '%s' ", device_name);
  1101. }
  1102. osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable);
  1103. if (osErr != noErr) {
  1104. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioHardwareGetPropertyInfo kAudioHardwarePropertyPlugInForBundleID error");
  1105. printError(osErr);
  1106. return osErr;
  1107. }
  1108. AudioValueTranslation pluginAVT;
  1109. CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
  1110. pluginAVT.mInputData = &inBundleRef;
  1111. pluginAVT.mInputDataSize = sizeof(inBundleRef);
  1112. pluginAVT.mOutputData = &fPluginID;
  1113. pluginAVT.mOutputDataSize = sizeof(fPluginID);
  1114. osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT);
  1115. if (osErr != noErr) {
  1116. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioHardwareGetProperty kAudioHardwarePropertyPlugInForBundleID error");
  1117. printError(osErr);
  1118. return osErr;
  1119. }
  1120. //-------------------------------------------------
  1121. // Create a CFDictionary for our aggregate device
  1122. //-------------------------------------------------
  1123. CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  1124. CFStringRef AggregateDeviceNameRef = CFSTR("JackDuplex");
  1125. CFStringRef AggregateDeviceUIDRef = CFSTR("com.grame.JackDuplex");
  1126. // add the name of the device to the dictionary
  1127. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
  1128. // add our choice of UID for the aggregate device to the dictionary
  1129. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
  1130. // add a "private aggregate key" to the dictionary
  1131. int value = 1;
  1132. CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
  1133. SInt32 system;
  1134. Gestalt(gestaltSystemVersion, &system);
  1135. jack_log("JackCoreAudioAdapter::CreateAggregateDevice : system version = %x limit = %x", system, 0x00001054);
  1136. // Starting with 10.5.4 systems, the AD can be internal... (better)
  1137. if (system < 0x00001054) {
  1138. jack_log("JackCoreAudioAdapter::CreateAggregateDevice : public aggregate device....");
  1139. } else {
  1140. jack_log("JackCoreAudioAdapter::CreateAggregateDevice : private aggregate device....");
  1141. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
  1142. }
  1143. // Prepare sub-devices for clock drift compensation
  1144. CFMutableArrayRef subDevicesArrayClock = NULL;
  1145. /*
  1146. if (fClockDriftCompensate) {
  1147. if (need_clock_drift_compensation) {
  1148. jack_info("Clock drift compensation activated...");
  1149. subDevicesArrayClock = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  1150. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  1151. CFStringRef UID = GetDeviceName(captureDeviceID[i]);
  1152. if (UID) {
  1153. CFMutableDictionaryRef subdeviceAggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  1154. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceUIDKey), UID);
  1155. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceDriftCompensationKey), AggregateDeviceNumberRef);
  1156. //CFRelease(UID);
  1157. CFArrayAppendValue(subDevicesArrayClock, subdeviceAggDeviceDict);
  1158. }
  1159. }
  1160. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  1161. CFStringRef UID = GetDeviceName(playbackDeviceID[i]);
  1162. if (UID) {
  1163. CFMutableDictionaryRef subdeviceAggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  1164. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceUIDKey), UID);
  1165. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceDriftCompensationKey), AggregateDeviceNumberRef);
  1166. //CFRelease(UID);
  1167. CFArrayAppendValue(subDevicesArrayClock, subdeviceAggDeviceDict);
  1168. }
  1169. }
  1170. // add sub-device clock array for the aggregate device to the dictionary
  1171. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceSubDeviceListKey), subDevicesArrayClock);
  1172. } else {
  1173. jack_info("Clock drift compensation was asked but is not needed (devices use the same clock domain)");
  1174. }
  1175. }
  1176. */
  1177. //-------------------------------------------------
  1178. // Create a CFMutableArray for our sub-device list
  1179. //-------------------------------------------------
  1180. // we need to append the UID for each device to a CFMutableArray, so create one here
  1181. CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  1182. vector<CFStringRef> captureDeviceUID;
  1183. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  1184. CFStringRef ref = GetDeviceName(captureDeviceID[i]);
  1185. if (ref == NULL) {
  1186. return -1;
  1187. }
  1188. captureDeviceUID.push_back(ref);
  1189. // input sub-devices in this example, so append the sub-device's UID to the CFArray
  1190. CFArrayAppendValue(subDevicesArray, ref);
  1191. }
  1192. vector<CFStringRef> playbackDeviceUID;
  1193. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  1194. CFStringRef ref = GetDeviceName(playbackDeviceID[i]);
  1195. if (ref == NULL) {
  1196. return -1;
  1197. }
  1198. playbackDeviceUID.push_back(ref);
  1199. // output sub-devices in this example, so append the sub-device's UID to the CFArray
  1200. CFArrayAppendValue(subDevicesArray, ref);
  1201. }
  1202. //-----------------------------------------------------------------------
  1203. // Feed the dictionary to the plugin, to create a blank aggregate device
  1204. //-----------------------------------------------------------------------
  1205. AudioObjectPropertyAddress pluginAOPA;
  1206. pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
  1207. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  1208. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  1209. UInt32 outDataSize;
  1210. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  1211. if (osErr != noErr) {
  1212. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectGetPropertyDataSize error");
  1213. printError(osErr);
  1214. goto error;
  1215. }
  1216. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, outAggregateDevice);
  1217. if (osErr != noErr) {
  1218. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectGetPropertyData error");
  1219. printError(osErr);
  1220. goto error;
  1221. }
  1222. // pause for a bit to make sure that everything completed correctly
  1223. // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
  1224. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  1225. //-------------------------
  1226. // Set the sub-device list
  1227. //-------------------------
  1228. pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
  1229. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  1230. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  1231. outDataSize = sizeof(CFMutableArrayRef);
  1232. osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
  1233. if (osErr != noErr) {
  1234. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectSetPropertyData for sub-device list error");
  1235. printError(osErr);
  1236. goto error;
  1237. }
  1238. // pause again to give the changes time to take effect
  1239. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  1240. //-----------------------
  1241. // Set the master device
  1242. //-----------------------
  1243. // set the master device manually (this is the device which will act as the master clock for the aggregate device)
  1244. // pass in the UID of the device you want to use
  1245. pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
  1246. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  1247. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  1248. outDataSize = sizeof(CFStringRef);
  1249. osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID[0]); // First apture is master...
  1250. if (osErr != noErr) {
  1251. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectSetPropertyData for master device error");
  1252. printError(osErr);
  1253. goto error;
  1254. }
  1255. // pause again to give the changes time to take effect
  1256. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  1257. // Prepare sub-devices for clock drift compensation
  1258. // Workaround for bug in the HAL : until 10.6.2
  1259. if (fClockDriftCompensate) {
  1260. if (need_clock_drift_compensation) {
  1261. jack_info("Clock drift compensation activated...");
  1262. // Get the property data size
  1263. osErr = AudioObjectGetPropertyDataSize(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize);
  1264. if (osErr != noErr) {
  1265. jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
  1266. printError(osErr);
  1267. }
  1268. // Calculate the number of object IDs
  1269. subDevicesNum = outSize / sizeof(AudioObjectID);
  1270. jack_info("JackCoreAudioAdapter::CreateAggregateDevice clock drift compensation, number of sub-devices = %d", subDevicesNum);
  1271. AudioObjectID subDevices[subDevicesNum];
  1272. outSize = sizeof(subDevices);
  1273. osErr = AudioObjectGetPropertyData(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize, subDevices);
  1274. if (osErr != noErr) {
  1275. jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
  1276. printError(osErr);
  1277. }
  1278. // Set kAudioSubDevicePropertyDriftCompensation property...
  1279. for (UInt32 index = 0; index < subDevicesNum; ++index) {
  1280. UInt32 theDriftCompensationValue = 1;
  1281. osErr = AudioObjectSetPropertyData(subDevices[index], &theAddressDrift, 0, NULL, sizeof(UInt32), &theDriftCompensationValue);
  1282. if (osErr != noErr) {
  1283. jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioSubDevicePropertyDriftCompensation error");
  1284. printError(osErr);
  1285. }
  1286. }
  1287. } else {
  1288. jack_info("Clock drift compensation was asked but is not needed (devices use the same clock domain)");
  1289. }
  1290. }
  1291. // pause again to give the changes time to take effect
  1292. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  1293. //----------
  1294. // Clean up
  1295. //----------
  1296. // release the private AD key
  1297. CFRelease(AggregateDeviceNumberRef);
  1298. // release the CF objects we have created - we don't need them any more
  1299. CFRelease(aggDeviceDict);
  1300. CFRelease(subDevicesArray);
  1301. if (subDevicesArrayClock) {
  1302. CFRelease(subDevicesArrayClock);
  1303. }
  1304. // release the device UID
  1305. for (UInt32 i = 0; i < captureDeviceUID.size(); i++) {
  1306. CFRelease(captureDeviceUID[i]);
  1307. }
  1308. for (UInt32 i = 0; i < playbackDeviceUID.size(); i++) {
  1309. CFRelease(playbackDeviceUID[i]);
  1310. }
  1311. jack_log("New aggregate device %ld", *outAggregateDevice);
  1312. return noErr;
  1313. error:
  1314. DestroyAggregateDevice();
  1315. return -1;
  1316. }
  1317. bool JackCoreAudioAdapter::IsAggregateDevice(AudioDeviceID device)
  1318. {
  1319. OSStatus err = noErr;
  1320. AudioObjectID sub_device[32];
  1321. UInt32 outSize = sizeof(sub_device);
  1322. err = AudioDeviceGetProperty(device, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  1323. if (err != noErr) {
  1324. jack_log("Device does not have subdevices");
  1325. return false;
  1326. } else {
  1327. int num_devices = outSize / sizeof(AudioObjectID);
  1328. jack_log("Device does has %d subdevices", num_devices);
  1329. return true;
  1330. }
  1331. }
  1332. void JackCoreAudioAdapter::CloseAUHAL()
  1333. {
  1334. AudioUnitUninitialize(fAUHAL);
  1335. CloseComponent(fAUHAL);
  1336. }
  1337. int JackCoreAudioAdapter::Open()
  1338. {
  1339. return (AudioOutputUnitStart(fAUHAL) != noErr) ? -1 : 0;
  1340. }
  1341. int JackCoreAudioAdapter::Close()
  1342. {
  1343. #ifdef JACK_MONITOR
  1344. fTable.Save(fHostBufferSize, fHostSampleRate, fAdaptedSampleRate, fAdaptedBufferSize);
  1345. #endif
  1346. AudioOutputUnitStop(fAUHAL);
  1347. DisposeBuffers();
  1348. CloseAUHAL();
  1349. RemoveListeners();
  1350. if (fPluginID > 0) {
  1351. DestroyAggregateDevice();
  1352. }
  1353. return 0;
  1354. }
  1355. int JackCoreAudioAdapter::SetSampleRate(jack_nframes_t sample_rate)
  1356. {
  1357. JackAudioAdapterInterface::SetHostSampleRate(sample_rate);
  1358. Close();
  1359. return Open();
  1360. }
  1361. int JackCoreAudioAdapter::SetBufferSize(jack_nframes_t buffer_size)
  1362. {
  1363. JackAudioAdapterInterface::SetHostBufferSize(buffer_size);
  1364. Close();
  1365. return Open();
  1366. }
  1367. OSStatus JackCoreAudioAdapter::GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies)
  1368. {
  1369. OSStatus err = noErr;
  1370. UInt32 outSize1, outSize2, outSize3;
  1371. Boolean outWritable;
  1372. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreams, &outSize1, &outWritable);
  1373. if (err == noErr) {
  1374. int stream_count = outSize1 / sizeof(UInt32);
  1375. AudioStreamID streamIDs[stream_count];
  1376. AudioBufferList bufferList[stream_count];
  1377. UInt32 streamLatency;
  1378. outSize2 = sizeof(UInt32);
  1379. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreams, &outSize1, streamIDs);
  1380. if (err != noErr) {
  1381. jack_error("GetStreamLatencies kAudioDevicePropertyStreams err = %d", err);
  1382. return err;
  1383. }
  1384. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize3, &outWritable);
  1385. if (err != noErr) {
  1386. jack_error("GetStreamLatencies kAudioDevicePropertyStreamConfiguration err = %d", err);
  1387. return err;
  1388. }
  1389. for (int i = 0; i < stream_count; i++) {
  1390. err = AudioStreamGetProperty(streamIDs[i], 0, kAudioStreamPropertyLatency, &outSize2, &streamLatency);
  1391. if (err != noErr) {
  1392. jack_error("GetStreamLatencies kAudioStreamPropertyLatency err = %d", err);
  1393. return err;
  1394. }
  1395. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize3, bufferList);
  1396. if (err != noErr) {
  1397. jack_error("GetStreamLatencies kAudioDevicePropertyStreamConfiguration err = %d", err);
  1398. return err;
  1399. }
  1400. // Push 'channel' time the stream latency
  1401. for (uint k = 0; k < bufferList->mBuffers[i].mNumberChannels; k++) {
  1402. latencies.push_back(streamLatency);
  1403. }
  1404. }
  1405. }
  1406. return err;
  1407. }
  1408. int JackCoreAudioAdapter::GetLatency(int port_index, bool input)
  1409. {
  1410. UInt32 size = sizeof(UInt32);
  1411. UInt32 value1 = 0;
  1412. UInt32 value2 = 0;
  1413. OSStatus err = AudioDeviceGetProperty(fDeviceID, 0, input, kAudioDevicePropertyLatency, &size, &value1);
  1414. if (err != noErr) {
  1415. jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error");
  1416. }
  1417. err = AudioDeviceGetProperty(fDeviceID, 0, input, kAudioDevicePropertySafetyOffset, &size, &value2);
  1418. if (err != noErr) {
  1419. jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error");
  1420. }
  1421. // TODO : add stream latency
  1422. return value1 + value2 + fAdaptedBufferSize;
  1423. }
  1424. int JackCoreAudioAdapter::GetInputLatency(int port_index)
  1425. {
  1426. if (port_index < int(fInputLatencies.size())) {
  1427. return GetLatency(port_index, true) + fInputLatencies[port_index];
  1428. } else {
  1429. // No stream latency
  1430. return GetLatency(port_index, true);
  1431. }
  1432. }
  1433. int JackCoreAudioAdapter::GetOutputLatency(int port_index)
  1434. {
  1435. if (port_index < int(fOutputLatencies.size())) {
  1436. return GetLatency(port_index, false) + fOutputLatencies[port_index];
  1437. } else {
  1438. // No stream latency
  1439. return GetLatency(port_index, false);
  1440. }
  1441. }
  1442. } // namespace
  1443. #ifdef __cplusplus
  1444. extern "C"
  1445. {
  1446. #endif
  1447. SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor()
  1448. {
  1449. jack_driver_desc_t * desc;
  1450. jack_driver_desc_filler_t filler;
  1451. jack_driver_param_value_t value;
  1452. desc = jack_driver_descriptor_construct("audioadapter", JackDriverNone, "netjack audio <==> net backend adapter", &filler);
  1453. value.i = -1;
  1454. jack_driver_descriptor_add_parameter(desc, &filler, "channels", 'c', JackDriverParamInt, &value, NULL, "Maximum number of channels", "Maximum number of channels. If -1, max possible number of channels will be used");
  1455. jack_driver_descriptor_add_parameter(desc, &filler, "in-channels", 'i', JackDriverParamInt, &value, NULL, "Maximum number of input channels", "Maximum number of input channels. If -1, max possible number of input channels will be used");
  1456. jack_driver_descriptor_add_parameter(desc, &filler, "out-channels", 'o', JackDriverParamInt, &value, NULL, "Maximum number of output channels", "Maximum number of output channels. If -1, max possible number of output channels will be used");
  1457. value.str[0] = 0;
  1458. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Input CoreAudio device name", NULL);
  1459. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Output CoreAudio device name", NULL);
  1460. value.ui = 44100U;
  1461. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  1462. value.ui = 512U;
  1463. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  1464. value.i = TRUE;
  1465. jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
  1466. value.str[0] = 0;
  1467. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "CoreAudio device name", NULL);
  1468. value.i = TRUE;
  1469. jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available CoreAudio devices", NULL);
  1470. value.ui = 0;
  1471. jack_driver_descriptor_add_parameter(desc, &filler, "quality", 'q', JackDriverParamInt, &value, NULL, "Resample algorithm quality (0 - 4)", NULL);
  1472. value.ui = 32768;
  1473. jack_driver_descriptor_add_parameter(desc, &filler, "ring-buffer", 'g', JackDriverParamInt, &value, NULL, "Fixed ringbuffer size", "Fixed ringbuffer size (if not set => automatic adaptative)");
  1474. value.i = FALSE;
  1475. jack_driver_descriptor_add_parameter(desc, &filler, "clock-drift", 's', JackDriverParamBool, &value, NULL, "Clock drift compensation", "Whether to compensate clock drift in dynamically created aggregate device");
  1476. return desc;
  1477. }
  1478. #ifdef __cplusplus
  1479. }
  1480. #endif