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.

1756 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. return static_cast<JackCoreAudioAdapter*>(inRefCon)->Render(ioActionFlags, inTimeStamp, inNumberFrames, ioData);
  254. }
  255. OSStatus JackCoreAudioAdapter::Render(AudioUnitRenderActionFlags *ioActionFlags,
  256. const AudioTimeStamp *inTimeStamp,
  257. UInt32 inNumberFrames,
  258. AudioBufferList *ioData)
  259. {
  260. OSStatus err = AudioUnitRender(fAUHAL, ioActionFlags, inTimeStamp, 1, inNumberFrames, fInputData);
  261. if (err == noErr) {
  262. jack_default_audio_sample_t* inputBuffer[fCaptureChannels];
  263. jack_default_audio_sample_t* outputBuffer[fPlaybackChannels];
  264. for (int i = 0; i < fCaptureChannels; i++) {
  265. inputBuffer[i] = (jack_default_audio_sample_t*)fInputData->mBuffers[i].mData;
  266. }
  267. for (int i = 0; i < fPlaybackChannels; i++) {
  268. outputBuffer[i] = (jack_default_audio_sample_t*)ioData->mBuffers[i].mData;
  269. }
  270. PushAndPull((jack_default_audio_sample_t**)inputBuffer, (jack_default_audio_sample_t**)outputBuffer, inNumberFrames);
  271. return noErr;
  272. } else {
  273. return err;
  274. }
  275. }
  276. JackCoreAudioAdapter::JackCoreAudioAdapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params)
  277. :JackAudioAdapterInterface(buffer_size, sample_rate), fInputData(0), fCapturing(false), fPlaying(false), fState(false)
  278. {
  279. const JSList* node;
  280. const jack_driver_param_t* param;
  281. int in_nChannels = 0;
  282. int out_nChannels = 0;
  283. char captureName[256];
  284. char playbackName[256];
  285. fCaptureUID[0] = 0;
  286. fPlaybackUID[0] = 0;
  287. fClockDriftCompensate = false;
  288. // Default values
  289. fCaptureChannels = -1;
  290. fPlaybackChannels = -1;
  291. SInt32 major;
  292. SInt32 minor;
  293. Gestalt(gestaltSystemVersionMajor, &major);
  294. Gestalt(gestaltSystemVersionMinor, &minor);
  295. // Starting with 10.6 systems, the HAL notification thread is created internally
  296. if (major == 10 && minor >= 6) {
  297. CFRunLoopRef theRunLoop = NULL;
  298. AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  299. OSStatus theError = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
  300. if (theError != noErr) {
  301. jack_error("JackCoreAudioAdapter::Open kAudioHardwarePropertyRunLoop error");
  302. }
  303. }
  304. for (node = params; node; node = jack_slist_next(node)) {
  305. param = (const jack_driver_param_t*) node->data;
  306. switch (param->character) {
  307. case 'i':
  308. fCaptureChannels = param->value.ui;
  309. break;
  310. case 'o':
  311. fPlaybackChannels = param->value.ui;
  312. break;
  313. case 'C':
  314. fCapturing = true;
  315. strncpy(fCaptureUID, param->value.str, 256);
  316. break;
  317. case 'P':
  318. fPlaying = true;
  319. strncpy(fPlaybackUID, param->value.str, 256);
  320. break;
  321. case 'd':
  322. strncpy(fCaptureUID, param->value.str, 256);
  323. strncpy(fPlaybackUID, param->value.str, 256);
  324. break;
  325. case 'D':
  326. fCapturing = fPlaying = true;
  327. break;
  328. case 'r':
  329. SetAdaptedSampleRate(param->value.ui);
  330. break;
  331. case 'p':
  332. SetAdaptedBufferSize(param->value.ui);
  333. break;
  334. case 'l':
  335. DisplayDeviceNames();
  336. break;
  337. case 'q':
  338. fQuality = param->value.ui;
  339. break;
  340. case 'g':
  341. fRingbufferCurSize = param->value.ui;
  342. fAdaptative = false;
  343. break;
  344. case 's':
  345. fClockDriftCompensate = true;
  346. break;
  347. }
  348. }
  349. /* duplex is the default */
  350. if (!fCapturing && !fPlaying) {
  351. fCapturing = true;
  352. fPlaying = true;
  353. }
  354. if (SetupDevices(fCaptureUID, fPlaybackUID, captureName, playbackName, fAdaptedSampleRate) < 0) {
  355. throw std::bad_alloc();
  356. }
  357. if (SetupChannels(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, true) < 0) {
  358. throw std::bad_alloc();
  359. }
  360. if (SetupBufferSize(fAdaptedBufferSize) < 0) {
  361. throw std::bad_alloc();
  362. }
  363. if (SetupSampleRate(fAdaptedSampleRate) < 0) {
  364. throw std::bad_alloc();
  365. }
  366. if (OpenAUHAL(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, fAdaptedBufferSize, fAdaptedSampleRate) < 0) {
  367. throw std::bad_alloc();
  368. }
  369. if (fCapturing && fCaptureChannels > 0) {
  370. if (SetupBuffers(fCaptureChannels) < 0) {
  371. throw std::bad_alloc();
  372. }
  373. }
  374. if (AddListeners() < 0) {
  375. throw std::bad_alloc();
  376. }
  377. GetStreamLatencies(fDeviceID, true, fInputLatencies);
  378. GetStreamLatencies(fDeviceID, false, fOutputLatencies);
  379. }
  380. OSStatus JackCoreAudioAdapter::GetDefaultDevice(AudioDeviceID* id)
  381. {
  382. OSStatus res;
  383. UInt32 theSize = sizeof(UInt32);
  384. AudioDeviceID inDefault;
  385. AudioDeviceID outDefault;
  386. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
  387. return res;
  388. }
  389. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
  390. return res;
  391. }
  392. jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
  393. // Get the device only if default input and output are the same
  394. if (inDefault != outDefault) {
  395. jack_error("Default input and output devices are not the same !!");
  396. return kAudioHardwareBadDeviceError;
  397. } else if (inDefault == 0) {
  398. jack_error("Default input and output devices are null !!");
  399. return kAudioHardwareBadDeviceError;
  400. } else {
  401. *id = inDefault;
  402. return noErr;
  403. }
  404. }
  405. OSStatus JackCoreAudioAdapter::GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput)
  406. {
  407. OSStatus err = noErr;
  408. UInt32 outSize;
  409. Boolean outWritable;
  410. channelCount = 0;
  411. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  412. if (err == noErr) {
  413. AudioBufferList bufferList[outSize];
  414. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  415. if (err == noErr) {
  416. for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++) {
  417. channelCount += bufferList->mBuffers[i].mNumberChannels;
  418. }
  419. }
  420. }
  421. return err;
  422. }
  423. OSStatus JackCoreAudioAdapter::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
  424. {
  425. UInt32 size = sizeof(AudioValueTranslation);
  426. CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
  427. AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
  428. if (inIUD == NULL) {
  429. return kAudioHardwareUnspecifiedError;
  430. } else {
  431. OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
  432. CFRelease(inIUD);
  433. jack_log("GetDeviceIDFromUID %s %ld", UID, *id);
  434. return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
  435. }
  436. }
  437. OSStatus JackCoreAudioAdapter::GetDefaultInputDevice(AudioDeviceID* id)
  438. {
  439. OSStatus res;
  440. UInt32 theSize = sizeof(UInt32);
  441. AudioDeviceID inDefault;
  442. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
  443. return res;
  444. }
  445. if (inDefault == 0) {
  446. jack_error("Error: default input device is 0, please select a correct one !!");
  447. return -1;
  448. }
  449. jack_log("GetDefaultInputDevice: input = %ld ", inDefault);
  450. *id = inDefault;
  451. return noErr;
  452. }
  453. OSStatus JackCoreAudioAdapter::GetDefaultOutputDevice(AudioDeviceID* id)
  454. {
  455. OSStatus res;
  456. UInt32 theSize = sizeof(UInt32);
  457. AudioDeviceID outDefault;
  458. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
  459. return res;
  460. }
  461. if (outDefault == 0) {
  462. jack_error("Error: default output device is 0, please select a correct one !!");
  463. return -1;
  464. }
  465. jack_log("GetDefaultOutputDevice: output = %ld", outDefault);
  466. *id = outDefault;
  467. return noErr;
  468. }
  469. OSStatus JackCoreAudioAdapter::GetDeviceNameFromID(AudioDeviceID id, char* name)
  470. {
  471. UInt32 size = 256;
  472. return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
  473. }
  474. AudioDeviceID JackCoreAudioAdapter::GetDeviceIDFromName(const char* name)
  475. {
  476. UInt32 size;
  477. Boolean isWritable;
  478. int i, deviceNum;
  479. OSStatus err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
  480. if (err != noErr) {
  481. return -1;
  482. }
  483. deviceNum = size / sizeof(AudioDeviceID);
  484. AudioDeviceID devices[deviceNum];
  485. err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
  486. if (err != noErr) {
  487. return err;
  488. }
  489. for (i = 0; i < deviceNum; i++) {
  490. char device_name[256];
  491. size = 256;
  492. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
  493. if (err != noErr) {
  494. return -1;
  495. } else if (strcmp(device_name, name) == 0) {
  496. return devices[i];
  497. }
  498. }
  499. return -1;
  500. }
  501. // Setup
  502. int JackCoreAudioAdapter::SetupDevices(const char* capture_driver_uid,
  503. const char* playback_driver_uid,
  504. char* capture_driver_name,
  505. char* playback_driver_name,
  506. jack_nframes_t samplerate)
  507. {
  508. capture_driver_name[0] = 0;
  509. playback_driver_name[0] = 0;
  510. // Duplex
  511. if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
  512. jack_log("JackCoreAudioDriver::Open duplex");
  513. // Same device for capture and playback...
  514. if (strcmp(capture_driver_uid, playback_driver_uid) == 0) {
  515. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  516. jack_log("Will take default in/out");
  517. if (GetDefaultDevice(&fDeviceID) != noErr) {
  518. jack_error("Cannot open default device");
  519. return -1;
  520. }
  521. }
  522. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  523. jack_error("Cannot get device name from device ID");
  524. return -1;
  525. }
  526. } else {
  527. // Creates aggregate device
  528. AudioDeviceID captureID, playbackID;
  529. if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr) {
  530. jack_log("Will take default input");
  531. if (GetDefaultInputDevice(&captureID) != noErr) {
  532. jack_error("Cannot open default input device");
  533. return -1;
  534. }
  535. }
  536. if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr) {
  537. jack_log("Will take default output");
  538. if (GetDefaultOutputDevice(&playbackID) != noErr) {
  539. jack_error("Cannot open default output device");
  540. return -1;
  541. }
  542. }
  543. if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
  544. return -1;
  545. }
  546. }
  547. // Capture only
  548. } else if (strcmp(capture_driver_uid, "") != 0) {
  549. jack_log("JackCoreAudioAdapter::Open capture only");
  550. if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
  551. if (GetDefaultInputDevice(&fDeviceID) != noErr) {
  552. jack_error("Cannot open default input device");
  553. return -1;
  554. }
  555. }
  556. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
  557. jack_error("Cannot get device name from device ID");
  558. return -1;
  559. }
  560. // Playback only
  561. } else if (strcmp(playback_driver_uid, "") != 0) {
  562. jack_log("JackCoreAudioAdapter::Open playback only");
  563. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  564. if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
  565. jack_error("Cannot open default output device");
  566. return -1;
  567. }
  568. }
  569. if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  570. jack_error("Cannot get device name from device ID");
  571. return -1;
  572. }
  573. // Use default driver in duplex mode
  574. } else {
  575. jack_log("JackCoreAudioAdapter::Open default driver");
  576. if (GetDefaultDevice(&fDeviceID) != noErr) {
  577. jack_error("Cannot open default device in duplex mode, so aggregate default input and default output");
  578. // Creates aggregate device
  579. AudioDeviceID captureID = -1, playbackID = -1;
  580. if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr) {
  581. jack_log("Will take default input");
  582. if (GetDefaultInputDevice(&captureID) != noErr) {
  583. jack_error("Cannot open default input device");
  584. goto built_in;
  585. }
  586. }
  587. if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr) {
  588. jack_log("Will take default output");
  589. if (GetDefaultOutputDevice(&playbackID) != noErr) {
  590. jack_error("Cannot open default output device");
  591. goto built_in;
  592. }
  593. }
  594. if (captureID > 0 && playbackID > 0) {
  595. if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
  596. goto built_in;
  597. }
  598. } else {
  599. jack_error("Cannot use default input/output");
  600. goto built_in;
  601. }
  602. }
  603. }
  604. return 0;
  605. built_in:
  606. // Aggregate built-in input and output
  607. AudioDeviceID captureID = GetDeviceIDFromName("Built-in Input");
  608. AudioDeviceID playbackID = GetDeviceIDFromName("Built-in Output");
  609. if (captureID > 0 && playbackID > 0) {
  610. if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
  611. return -1;
  612. }
  613. } else {
  614. jack_error("Cannot aggregate built-in input and output");
  615. return -1;
  616. }
  617. return 0;
  618. }
  619. int JackCoreAudioAdapter::SetupChannels(bool capturing,
  620. bool playing,
  621. int& inchannels,
  622. int& outchannels,
  623. int& in_nChannels,
  624. int& out_nChannels,
  625. bool strict)
  626. {
  627. OSStatus err = noErr;
  628. if (capturing) {
  629. err = GetTotalChannels(fDeviceID, in_nChannels, true);
  630. if (err != noErr) {
  631. jack_error("Cannot get input channel number");
  632. printError(err);
  633. return -1;
  634. } else {
  635. jack_log("Max input channels : %d", in_nChannels);
  636. }
  637. }
  638. if (playing) {
  639. err = GetTotalChannels(fDeviceID, out_nChannels, false);
  640. if (err != noErr) {
  641. jack_error("Cannot get output channel number");
  642. printError(err);
  643. return -1;
  644. } else {
  645. jack_log("Max output channels : %d", out_nChannels);
  646. }
  647. }
  648. if (inchannels > in_nChannels) {
  649. jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
  650. if (strict) {
  651. return -1;
  652. }
  653. }
  654. if (outchannels > out_nChannels) {
  655. jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
  656. if (strict) {
  657. return -1;
  658. }
  659. }
  660. if (inchannels == -1) {
  661. jack_log("Setup max in channels = %ld", in_nChannels);
  662. inchannels = in_nChannels;
  663. }
  664. if (outchannels == -1) {
  665. jack_log("Setup max out channels = %ld", out_nChannels);
  666. outchannels = out_nChannels;
  667. }
  668. return 0;
  669. }
  670. int JackCoreAudioAdapter::SetupBufferSize(jack_nframes_t buffer_size)
  671. {
  672. // Setting buffer size
  673. UInt32 outSize = sizeof(UInt32);
  674. OSStatus err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
  675. if (err != noErr) {
  676. jack_error("Cannot set buffer size %ld", buffer_size);
  677. printError(err);
  678. return -1;
  679. }
  680. return 0;
  681. }
  682. int JackCoreAudioAdapter::SetupSampleRate(jack_nframes_t samplerate)
  683. {
  684. return SetupSampleRateAux(fDeviceID, samplerate);
  685. }
  686. int JackCoreAudioAdapter::SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate)
  687. {
  688. OSStatus err = noErr;
  689. UInt32 outSize;
  690. Float64 sampleRate;
  691. // Get sample rate
  692. outSize = sizeof(Float64);
  693. err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  694. if (err != noErr) {
  695. jack_error("Cannot get current sample rate");
  696. printError(err);
  697. return -1;
  698. } else {
  699. jack_log("Current sample rate = %f", sampleRate);
  700. }
  701. // If needed, set new sample rate
  702. if (samplerate != (jack_nframes_t)sampleRate) {
  703. sampleRate = (Float64)samplerate;
  704. // To get SR change notification
  705. err = AudioDeviceAddPropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
  706. if (err != noErr) {
  707. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  708. printError(err);
  709. return -1;
  710. }
  711. err = AudioDeviceSetProperty(inDevice, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
  712. if (err != noErr) {
  713. jack_error("Cannot set sample rate = %ld", samplerate);
  714. printError(err);
  715. return -1;
  716. }
  717. // Waiting for SR change notification
  718. int count = 0;
  719. while (!fState && count++ < WAIT_COUNTER) {
  720. usleep(100000);
  721. jack_log("Wait count = %d", count);
  722. }
  723. // Remove SR change notification
  724. AudioDeviceRemovePropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
  725. }
  726. return 0;
  727. }
  728. int JackCoreAudioAdapter::SetupBuffers(int inchannels)
  729. {
  730. jack_log("JackCoreAudioAdapter::SetupBuffers: input = %ld", inchannels);
  731. // Prepare buffers
  732. fInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  733. fInputData->mNumberBuffers = inchannels;
  734. for (int i = 0; i < fCaptureChannels; i++) {
  735. fInputData->mBuffers[i].mNumberChannels = 1;
  736. fInputData->mBuffers[i].mDataByteSize = fAdaptedBufferSize * sizeof(jack_default_audio_sample_t);
  737. fInputData->mBuffers[i].mData = malloc(fAdaptedBufferSize * sizeof(jack_default_audio_sample_t));
  738. }
  739. return 0;
  740. }
  741. void JackCoreAudioAdapter::DisposeBuffers()
  742. {
  743. if (fInputData) {
  744. for (int i = 0; i < fCaptureChannels; i++) {
  745. free(fInputData->mBuffers[i].mData);
  746. }
  747. free(fInputData);
  748. fInputData = 0;
  749. }
  750. }
  751. int JackCoreAudioAdapter::OpenAUHAL(bool capturing,
  752. bool playing,
  753. int inchannels,
  754. int outchannels,
  755. int in_nChannels,
  756. int out_nChannels,
  757. jack_nframes_t buffer_size,
  758. jack_nframes_t samplerate)
  759. {
  760. ComponentResult err1;
  761. UInt32 enableIO;
  762. AudioStreamBasicDescription srcFormat, dstFormat;
  763. AudioDeviceID currAudioDeviceID;
  764. UInt32 size;
  765. 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);
  766. if (inchannels == 0 && outchannels == 0) {
  767. jack_error("No input and output channels...");
  768. return -1;
  769. }
  770. // AUHAL
  771. #ifdef MAC_OS_X_VERSION_10_5
  772. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  773. Component HALOutput = FindNextComponent(NULL, &cd);
  774. err1 = OpenAComponent(HALOutput, &fAUHAL);
  775. if (err1 != noErr) {
  776. jack_error("Error calling OpenAComponent");
  777. printError(err1);
  778. goto error;
  779. }
  780. #else
  781. AudioComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  782. AudioComponent HALOutput = AudioComponentFindNext(NULL, &cd);
  783. err1 = AudioComponentInstanceNew(HALOutput, &fAUHAL);
  784. if (err1 != noErr) {
  785. jack_error("Error calling AudioComponentInstanceNew");
  786. printError(err1);
  787. goto error;
  788. }
  789. #endif
  790. err1 = AudioUnitInitialize(fAUHAL);
  791. if (err1 != noErr) {
  792. jack_error("Cannot initialize AUHAL unit");
  793. printError(err1);
  794. goto error;
  795. }
  796. // Start I/O
  797. if (capturing && inchannels > 0) {
  798. enableIO = 1;
  799. jack_log("Setup AUHAL input on");
  800. } else {
  801. enableIO = 0;
  802. jack_log("Setup AUHAL input off");
  803. }
  804. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  805. if (err1 != noErr) {
  806. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  807. printError(err1);
  808. goto error;
  809. }
  810. if (playing && outchannels > 0) {
  811. enableIO = 1;
  812. jack_log("Setup AUHAL output on");
  813. } else {
  814. enableIO = 0;
  815. jack_log("Setup AUHAL output off");
  816. }
  817. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  818. if (err1 != noErr) {
  819. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  820. printError(err1);
  821. goto error;
  822. }
  823. size = sizeof(AudioDeviceID);
  824. err1 = AudioUnitGetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &currAudioDeviceID, &size);
  825. if (err1 != noErr) {
  826. jack_error("Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
  827. printError(err1);
  828. goto error;
  829. } else {
  830. jack_log("AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID);
  831. }
  832. // Setup up chosen device, in both input and output cases
  833. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  834. if (err1 != noErr) {
  835. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  836. printError(err1);
  837. goto error;
  838. }
  839. // Set buffer size
  840. if (capturing && inchannels > 0) {
  841. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&buffer_size, sizeof(UInt32));
  842. if (err1 != noErr) {
  843. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  844. printError(err1);
  845. goto error;
  846. }
  847. }
  848. if (playing && outchannels > 0) {
  849. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&buffer_size, sizeof(UInt32));
  850. if (err1 != noErr) {
  851. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  852. printError(err1);
  853. goto error;
  854. }
  855. }
  856. // Setup channel map
  857. if (capturing && inchannels > 0 && inchannels <= in_nChannels) {
  858. SInt32 chanArr[in_nChannels];
  859. for (int i = 0; i < in_nChannels; i++) {
  860. chanArr[i] = -1;
  861. }
  862. for (int i = 0; i < inchannels; i++) {
  863. chanArr[i] = i;
  864. }
  865. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  866. if (err1 != noErr) {
  867. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  868. printError(err1);
  869. goto error;
  870. }
  871. }
  872. if (playing && outchannels > 0 && outchannels <= out_nChannels) {
  873. SInt32 chanArr[out_nChannels];
  874. for (int i = 0; i < out_nChannels; i++) {
  875. chanArr[i] = -1;
  876. }
  877. for (int i = 0; i < outchannels; i++) {
  878. chanArr[i] = i;
  879. }
  880. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  881. if (err1 != noErr) {
  882. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  883. printError(err1);
  884. goto error;
  885. }
  886. }
  887. // Setup stream converters
  888. if (capturing && inchannels > 0) {
  889. size = sizeof(AudioStreamBasicDescription);
  890. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &srcFormat, &size);
  891. if (err1 != noErr) {
  892. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  893. printError(err1);
  894. goto error;
  895. }
  896. PrintStreamDesc(&srcFormat);
  897. jack_log("Setup AUHAL input stream converter SR = %ld", samplerate);
  898. srcFormat.mSampleRate = samplerate;
  899. srcFormat.mFormatID = kAudioFormatLinearPCM;
  900. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  901. srcFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
  902. srcFormat.mFramesPerPacket = 1;
  903. srcFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
  904. srcFormat.mChannelsPerFrame = inchannels;
  905. srcFormat.mBitsPerChannel = 32;
  906. PrintStreamDesc(&srcFormat);
  907. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, sizeof(AudioStreamBasicDescription));
  908. if (err1 != noErr) {
  909. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  910. printError(err1);
  911. goto error;
  912. }
  913. }
  914. if (playing && outchannels > 0) {
  915. size = sizeof(AudioStreamBasicDescription);
  916. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &dstFormat, &size);
  917. if (err1 != noErr) {
  918. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  919. printError(err1);
  920. goto error;
  921. }
  922. PrintStreamDesc(&dstFormat);
  923. jack_log("Setup AUHAL output stream converter SR = %ld", samplerate);
  924. dstFormat.mSampleRate = samplerate;
  925. dstFormat.mFormatID = kAudioFormatLinearPCM;
  926. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  927. dstFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
  928. dstFormat.mFramesPerPacket = 1;
  929. dstFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
  930. dstFormat.mChannelsPerFrame = outchannels;
  931. dstFormat.mBitsPerChannel = 32;
  932. PrintStreamDesc(&dstFormat);
  933. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, sizeof(AudioStreamBasicDescription));
  934. if (err1 != noErr) {
  935. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  936. printError(err1);
  937. goto error;
  938. }
  939. }
  940. // Setup callbacks
  941. if (inchannels > 0 && outchannels == 0) {
  942. AURenderCallbackStruct output;
  943. output.inputProc = Render;
  944. output.inputProcRefCon = this;
  945. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  946. if (err1 != noErr) {
  947. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  948. printError(err1);
  949. goto error;
  950. }
  951. } else {
  952. AURenderCallbackStruct output;
  953. output.inputProc = Render;
  954. output.inputProcRefCon = this;
  955. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  956. if (err1 != noErr) {
  957. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  958. printError(err1);
  959. goto error;
  960. }
  961. }
  962. return 0;
  963. error:
  964. CloseAUHAL();
  965. return -1;
  966. }
  967. OSStatus JackCoreAudioAdapter::DestroyAggregateDevice()
  968. {
  969. OSStatus osErr = noErr;
  970. AudioObjectPropertyAddress pluginAOPA;
  971. pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice;
  972. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  973. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  974. UInt32 outDataSize;
  975. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  976. if (osErr != noErr) {
  977. jack_error("JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
  978. printError(osErr);
  979. return osErr;
  980. }
  981. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID);
  982. if (osErr != noErr) {
  983. jack_error("JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyData error");
  984. printError(osErr);
  985. return osErr;
  986. }
  987. return noErr;
  988. }
  989. static CFStringRef GetDeviceName(AudioDeviceID id)
  990. {
  991. UInt32 size = sizeof(CFStringRef);
  992. CFStringRef UIname;
  993. OSStatus err = AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  994. return (err == noErr) ? UIname : NULL;
  995. }
  996. OSStatus JackCoreAudioAdapter::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
  997. {
  998. OSStatus err = noErr;
  999. AudioObjectID sub_device[32];
  1000. UInt32 outSize = sizeof(sub_device);
  1001. err = AudioDeviceGetProperty(captureDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  1002. vector<AudioDeviceID> captureDeviceIDArray;
  1003. if (err != noErr) {
  1004. jack_log("Input device does not have subdevices");
  1005. captureDeviceIDArray.push_back(captureDeviceID);
  1006. } else {
  1007. int num_devices = outSize / sizeof(AudioObjectID);
  1008. jack_log("Input device has %d subdevices", num_devices);
  1009. for (int i = 0; i < num_devices; i++) {
  1010. captureDeviceIDArray.push_back(sub_device[i]);
  1011. }
  1012. }
  1013. outSize = sizeof(sub_device);
  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, &playbackDeviceUID[0]); // First playback 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, "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");
  1455. 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");
  1456. value.str[0] = 0;
  1457. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Input CoreAudio device name", NULL);
  1458. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Output CoreAudio device name", NULL);
  1459. value.ui = 44100U;
  1460. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  1461. value.ui = 512U;
  1462. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  1463. value.i = true;
  1464. jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
  1465. value.str[0] = 0;
  1466. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "CoreAudio device name", NULL);
  1467. value.i = true;
  1468. jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available CoreAudio devices", NULL);
  1469. value.ui = 0;
  1470. jack_driver_descriptor_add_parameter(desc, &filler, "quality", 'q', JackDriverParamInt, &value, NULL, "Resample algorithm quality (0 - 4)", NULL);
  1471. value.ui = 32768;
  1472. jack_driver_descriptor_add_parameter(desc, &filler, "ring-buffer", 'g', JackDriverParamInt, &value, NULL, "Fixed ringbuffer size", "Fixed ringbuffer size (if not set => automatic adaptative)");
  1473. value.i = false;
  1474. 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");
  1475. value.i = false;
  1476. jack_driver_descriptor_add_parameter(desc, &filler, "auto-connect", 'c', JackDriverParamBool, &value, NULL, "Auto connect audioadapter to system ports", NULL);
  1477. return desc;
  1478. }
  1479. #ifdef __cplusplus
  1480. }
  1481. #endif