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.

1750 lines
66KB

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