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.

1764 lines
67KB

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