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.

1740 lines
65KB

  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. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  769. Component HALOutput = FindNextComponent(NULL, &cd);
  770. err1 = OpenAComponent(HALOutput, &fAUHAL);
  771. if (err1 != noErr) {
  772. jack_error("Error calling OpenAComponent");
  773. printError(err1);
  774. goto error;
  775. }
  776. err1 = AudioUnitInitialize(fAUHAL);
  777. if (err1 != noErr) {
  778. jack_error("Cannot initialize AUHAL unit");
  779. printError(err1);
  780. goto error;
  781. }
  782. // Start I/O
  783. if (capturing && inchannels > 0) {
  784. enableIO = 1;
  785. jack_log("Setup AUHAL input on");
  786. } else {
  787. enableIO = 0;
  788. jack_log("Setup AUHAL input off");
  789. }
  790. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  791. if (err1 != noErr) {
  792. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  793. printError(err1);
  794. goto error;
  795. }
  796. if (playing && outchannels > 0) {
  797. enableIO = 1;
  798. jack_log("Setup AUHAL output on");
  799. } else {
  800. enableIO = 0;
  801. jack_log("Setup AUHAL output off");
  802. }
  803. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  804. if (err1 != noErr) {
  805. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  806. printError(err1);
  807. goto error;
  808. }
  809. size = sizeof(AudioDeviceID);
  810. err1 = AudioUnitGetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &currAudioDeviceID, &size);
  811. if (err1 != noErr) {
  812. jack_error("Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
  813. printError(err1);
  814. goto error;
  815. } else {
  816. jack_log("AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID);
  817. }
  818. // Setup up choosen device, in both input and output cases
  819. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  820. if (err1 != noErr) {
  821. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  822. printError(err1);
  823. goto error;
  824. }
  825. // Set buffer size
  826. if (capturing && inchannels > 0) {
  827. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&buffer_size, sizeof(UInt32));
  828. if (err1 != noErr) {
  829. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  830. printError(err1);
  831. goto error;
  832. }
  833. }
  834. if (playing && outchannels > 0) {
  835. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&buffer_size, sizeof(UInt32));
  836. if (err1 != noErr) {
  837. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  838. printError(err1);
  839. goto error;
  840. }
  841. }
  842. // Setup channel map
  843. if (capturing && inchannels > 0 && inchannels <= in_nChannels) {
  844. SInt32 chanArr[in_nChannels];
  845. for (int i = 0; i < in_nChannels; i++) {
  846. chanArr[i] = -1;
  847. }
  848. for (int i = 0; i < inchannels; i++) {
  849. chanArr[i] = i;
  850. }
  851. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  852. if (err1 != noErr) {
  853. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  854. printError(err1);
  855. goto error;
  856. }
  857. }
  858. if (playing && outchannels > 0 && outchannels <= out_nChannels) {
  859. SInt32 chanArr[out_nChannels];
  860. for (int i = 0; i < out_nChannels; i++) {
  861. chanArr[i] = -1;
  862. }
  863. for (int i = 0; i < outchannels; i++) {
  864. chanArr[i] = i;
  865. }
  866. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  867. if (err1 != noErr) {
  868. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  869. printError(err1);
  870. goto error;
  871. }
  872. }
  873. // Setup stream converters
  874. if (capturing && inchannels > 0) {
  875. size = sizeof(AudioStreamBasicDescription);
  876. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &srcFormat, &size);
  877. if (err1 != noErr) {
  878. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  879. printError(err1);
  880. goto error;
  881. }
  882. PrintStreamDesc(&srcFormat);
  883. jack_log("Setup AUHAL input stream converter SR = %ld", samplerate);
  884. srcFormat.mSampleRate = samplerate;
  885. srcFormat.mFormatID = kAudioFormatLinearPCM;
  886. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  887. srcFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
  888. srcFormat.mFramesPerPacket = 1;
  889. srcFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
  890. srcFormat.mChannelsPerFrame = inchannels;
  891. srcFormat.mBitsPerChannel = 32;
  892. PrintStreamDesc(&srcFormat);
  893. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, sizeof(AudioStreamBasicDescription));
  894. if (err1 != noErr) {
  895. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  896. printError(err1);
  897. goto error;
  898. }
  899. }
  900. if (playing && outchannels > 0) {
  901. size = sizeof(AudioStreamBasicDescription);
  902. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &dstFormat, &size);
  903. if (err1 != noErr) {
  904. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  905. printError(err1);
  906. goto error;
  907. }
  908. PrintStreamDesc(&dstFormat);
  909. jack_log("Setup AUHAL output stream converter SR = %ld", samplerate);
  910. dstFormat.mSampleRate = samplerate;
  911. dstFormat.mFormatID = kAudioFormatLinearPCM;
  912. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  913. dstFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
  914. dstFormat.mFramesPerPacket = 1;
  915. dstFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
  916. dstFormat.mChannelsPerFrame = outchannels;
  917. dstFormat.mBitsPerChannel = 32;
  918. PrintStreamDesc(&dstFormat);
  919. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, sizeof(AudioStreamBasicDescription));
  920. if (err1 != noErr) {
  921. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  922. printError(err1);
  923. goto error;
  924. }
  925. }
  926. // Setup callbacks
  927. if (inchannels > 0 && outchannels == 0) {
  928. AURenderCallbackStruct output;
  929. output.inputProc = Render;
  930. output.inputProcRefCon = this;
  931. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  932. if (err1 != noErr) {
  933. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  934. printError(err1);
  935. goto error;
  936. }
  937. } else {
  938. AURenderCallbackStruct output;
  939. output.inputProc = Render;
  940. output.inputProcRefCon = this;
  941. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  942. if (err1 != noErr) {
  943. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  944. printError(err1);
  945. goto error;
  946. }
  947. }
  948. return 0;
  949. error:
  950. CloseAUHAL();
  951. return -1;
  952. }
  953. OSStatus JackCoreAudioAdapter::DestroyAggregateDevice()
  954. {
  955. OSStatus osErr = noErr;
  956. AudioObjectPropertyAddress pluginAOPA;
  957. pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice;
  958. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  959. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  960. UInt32 outDataSize;
  961. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  962. if (osErr != noErr) {
  963. jack_error("JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
  964. printError(osErr);
  965. return osErr;
  966. }
  967. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID);
  968. if (osErr != noErr) {
  969. jack_error("JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyData error");
  970. printError(osErr);
  971. return osErr;
  972. }
  973. return noErr;
  974. }
  975. static CFStringRef GetDeviceName(AudioDeviceID id)
  976. {
  977. UInt32 size = sizeof(CFStringRef);
  978. CFStringRef UIname;
  979. OSStatus err = AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  980. return (err == noErr) ? UIname : NULL;
  981. }
  982. OSStatus JackCoreAudioAdapter::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
  983. {
  984. OSStatus err = noErr;
  985. AudioObjectID sub_device[32];
  986. UInt32 outSize = sizeof(sub_device);
  987. err = AudioDeviceGetProperty(captureDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  988. vector<AudioDeviceID> captureDeviceIDArray;
  989. if (err != noErr) {
  990. jack_log("Input device does not have subdevices");
  991. captureDeviceIDArray.push_back(captureDeviceID);
  992. } else {
  993. int num_devices = outSize / sizeof(AudioObjectID);
  994. jack_log("Input device has %d subdevices", num_devices);
  995. for (int i = 0; i < num_devices; i++) {
  996. captureDeviceIDArray.push_back(sub_device[i]);
  997. }
  998. }
  999. err = AudioDeviceGetProperty(playbackDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  1000. vector<AudioDeviceID> playbackDeviceIDArray;
  1001. if (err != noErr) {
  1002. jack_log("Output device does not have subdevices");
  1003. playbackDeviceIDArray.push_back(playbackDeviceID);
  1004. } else {
  1005. int num_devices = outSize / sizeof(AudioObjectID);
  1006. jack_log("Output device has %d subdevices", num_devices);
  1007. for (int i = 0; i < num_devices; i++) {
  1008. playbackDeviceIDArray.push_back(sub_device[i]);
  1009. }
  1010. }
  1011. return CreateAggregateDeviceAux(captureDeviceIDArray, playbackDeviceIDArray, samplerate, outAggregateDevice);
  1012. }
  1013. OSStatus JackCoreAudioAdapter::CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
  1014. {
  1015. OSStatus osErr = noErr;
  1016. UInt32 outSize;
  1017. Boolean outWritable;
  1018. // Prepare sub-devices for clock drift compensation
  1019. // Workaround for bug in the HAL : until 10.6.2
  1020. AudioObjectPropertyAddress theAddressOwned = { kAudioObjectPropertyOwnedObjects, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  1021. AudioObjectPropertyAddress theAddressDrift = { kAudioSubDevicePropertyDriftCompensation, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  1022. UInt32 theQualifierDataSize = sizeof(AudioObjectID);
  1023. AudioClassID inClass = kAudioSubDeviceClassID;
  1024. void* theQualifierData = &inClass;
  1025. UInt32 subDevicesNum = 0;
  1026. //---------------------------------------------------------------------------
  1027. // Setup SR of both devices otherwise creating AD may fail...
  1028. //---------------------------------------------------------------------------
  1029. UInt32 keptclockdomain = 0;
  1030. UInt32 clockdomain = 0;
  1031. outSize = sizeof(UInt32);
  1032. bool need_clock_drift_compensation = false;
  1033. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  1034. if (SetupSampleRateAux(captureDeviceID[i], samplerate) < 0) {
  1035. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : cannot set SR of input device");
  1036. } else {
  1037. // Check clock domain
  1038. osErr = AudioDeviceGetProperty(captureDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
  1039. if (osErr != 0) {
  1040. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
  1041. printError(osErr);
  1042. } else {
  1043. keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
  1044. jack_log("JackCoreAudioAdapter::CreateAggregateDevice : input clockdomain = %d", clockdomain);
  1045. if (clockdomain != 0 && clockdomain != keptclockdomain) {
  1046. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...");
  1047. need_clock_drift_compensation = true;
  1048. }
  1049. }
  1050. }
  1051. }
  1052. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  1053. if (SetupSampleRateAux(playbackDeviceID[i], samplerate) < 0) {
  1054. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : cannot set SR of output device");
  1055. } else {
  1056. // Check clock domain
  1057. osErr = AudioDeviceGetProperty(playbackDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
  1058. if (osErr != 0) {
  1059. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
  1060. printError(osErr);
  1061. } else {
  1062. keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
  1063. jack_log("JackCoreAudioAdapter::CreateAggregateDevice : output clockdomain = %d", clockdomain);
  1064. if (clockdomain != 0 && clockdomain != keptclockdomain) {
  1065. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...");
  1066. need_clock_drift_compensation = true;
  1067. }
  1068. }
  1069. }
  1070. }
  1071. // If no valid clock domain was found, then assume we have to compensate...
  1072. if (keptclockdomain == 0) {
  1073. need_clock_drift_compensation = true;
  1074. }
  1075. //---------------------------------------------------------------------------
  1076. // Start to create a new aggregate by getting the base audio hardware plugin
  1077. //---------------------------------------------------------------------------
  1078. char device_name[256];
  1079. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  1080. GetDeviceNameFromID(captureDeviceID[i], device_name);
  1081. jack_info("Separated input = '%s' ", device_name);
  1082. }
  1083. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  1084. GetDeviceNameFromID(playbackDeviceID[i], device_name);
  1085. jack_info("Separated output = '%s' ", device_name);
  1086. }
  1087. osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable);
  1088. if (osErr != noErr) {
  1089. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioHardwareGetPropertyInfo kAudioHardwarePropertyPlugInForBundleID error");
  1090. printError(osErr);
  1091. return osErr;
  1092. }
  1093. AudioValueTranslation pluginAVT;
  1094. CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
  1095. pluginAVT.mInputData = &inBundleRef;
  1096. pluginAVT.mInputDataSize = sizeof(inBundleRef);
  1097. pluginAVT.mOutputData = &fPluginID;
  1098. pluginAVT.mOutputDataSize = sizeof(fPluginID);
  1099. osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT);
  1100. if (osErr != noErr) {
  1101. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioHardwareGetProperty kAudioHardwarePropertyPlugInForBundleID error");
  1102. printError(osErr);
  1103. return osErr;
  1104. }
  1105. //-------------------------------------------------
  1106. // Create a CFDictionary for our aggregate device
  1107. //-------------------------------------------------
  1108. CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  1109. CFStringRef AggregateDeviceNameRef = CFSTR("JackDuplex");
  1110. CFStringRef AggregateDeviceUIDRef = CFSTR("com.grame.JackDuplex");
  1111. // add the name of the device to the dictionary
  1112. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
  1113. // add our choice of UID for the aggregate device to the dictionary
  1114. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
  1115. // add a "private aggregate key" to the dictionary
  1116. int value = 1;
  1117. CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
  1118. SInt32 system;
  1119. Gestalt(gestaltSystemVersion, &system);
  1120. jack_log("JackCoreAudioAdapter::CreateAggregateDevice : system version = %x limit = %x", system, 0x00001054);
  1121. // Starting with 10.5.4 systems, the AD can be internal... (better)
  1122. if (system < 0x00001054) {
  1123. jack_log("JackCoreAudioAdapter::CreateAggregateDevice : public aggregate device....");
  1124. } else {
  1125. jack_log("JackCoreAudioAdapter::CreateAggregateDevice : private aggregate device....");
  1126. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
  1127. }
  1128. // Prepare sub-devices for clock drift compensation
  1129. CFMutableArrayRef subDevicesArrayClock = NULL;
  1130. /*
  1131. if (fClockDriftCompensate) {
  1132. if (need_clock_drift_compensation) {
  1133. jack_info("Clock drift compensation activated...");
  1134. subDevicesArrayClock = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  1135. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  1136. CFStringRef UID = GetDeviceName(captureDeviceID[i]);
  1137. if (UID) {
  1138. CFMutableDictionaryRef subdeviceAggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  1139. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceUIDKey), UID);
  1140. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceDriftCompensationKey), AggregateDeviceNumberRef);
  1141. //CFRelease(UID);
  1142. CFArrayAppendValue(subDevicesArrayClock, subdeviceAggDeviceDict);
  1143. }
  1144. }
  1145. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  1146. CFStringRef UID = GetDeviceName(playbackDeviceID[i]);
  1147. if (UID) {
  1148. CFMutableDictionaryRef subdeviceAggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  1149. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceUIDKey), UID);
  1150. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceDriftCompensationKey), AggregateDeviceNumberRef);
  1151. //CFRelease(UID);
  1152. CFArrayAppendValue(subDevicesArrayClock, subdeviceAggDeviceDict);
  1153. }
  1154. }
  1155. // add sub-device clock array for the aggregate device to the dictionary
  1156. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceSubDeviceListKey), subDevicesArrayClock);
  1157. } else {
  1158. jack_info("Clock drift compensation was asked but is not needed (devices use the same clock domain)");
  1159. }
  1160. }
  1161. */
  1162. //-------------------------------------------------
  1163. // Create a CFMutableArray for our sub-device list
  1164. //-------------------------------------------------
  1165. // we need to append the UID for each device to a CFMutableArray, so create one here
  1166. CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  1167. vector<CFStringRef> captureDeviceUID;
  1168. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  1169. CFStringRef ref = GetDeviceName(captureDeviceID[i]);
  1170. if (ref == NULL) {
  1171. return -1;
  1172. }
  1173. captureDeviceUID.push_back(ref);
  1174. // input sub-devices in this example, so append the sub-device's UID to the CFArray
  1175. CFArrayAppendValue(subDevicesArray, ref);
  1176. }
  1177. vector<CFStringRef> playbackDeviceUID;
  1178. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  1179. CFStringRef ref = GetDeviceName(playbackDeviceID[i]);
  1180. if (ref == NULL) {
  1181. return -1;
  1182. }
  1183. playbackDeviceUID.push_back(ref);
  1184. // output sub-devices in this example, so append the sub-device's UID to the CFArray
  1185. CFArrayAppendValue(subDevicesArray, ref);
  1186. }
  1187. //-----------------------------------------------------------------------
  1188. // Feed the dictionary to the plugin, to create a blank aggregate device
  1189. //-----------------------------------------------------------------------
  1190. AudioObjectPropertyAddress pluginAOPA;
  1191. pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
  1192. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  1193. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  1194. UInt32 outDataSize;
  1195. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  1196. if (osErr != noErr) {
  1197. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectGetPropertyDataSize error");
  1198. printError(osErr);
  1199. goto error;
  1200. }
  1201. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, outAggregateDevice);
  1202. if (osErr != noErr) {
  1203. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectGetPropertyData error");
  1204. printError(osErr);
  1205. goto error;
  1206. }
  1207. // pause for a bit to make sure that everything completed correctly
  1208. // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
  1209. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  1210. //-------------------------
  1211. // Set the sub-device list
  1212. //-------------------------
  1213. pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
  1214. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  1215. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  1216. outDataSize = sizeof(CFMutableArrayRef);
  1217. osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
  1218. if (osErr != noErr) {
  1219. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectSetPropertyData for sub-device list error");
  1220. printError(osErr);
  1221. goto error;
  1222. }
  1223. // pause again to give the changes time to take effect
  1224. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  1225. //-----------------------
  1226. // Set the master device
  1227. //-----------------------
  1228. // set the master device manually (this is the device which will act as the master clock for the aggregate device)
  1229. // pass in the UID of the device you want to use
  1230. pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
  1231. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  1232. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  1233. outDataSize = sizeof(CFStringRef);
  1234. osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID[0]); // First apture is master...
  1235. if (osErr != noErr) {
  1236. jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectSetPropertyData for master device error");
  1237. printError(osErr);
  1238. goto error;
  1239. }
  1240. // pause again to give the changes time to take effect
  1241. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  1242. // Prepare sub-devices for clock drift compensation
  1243. // Workaround for bug in the HAL : until 10.6.2
  1244. if (fClockDriftCompensate) {
  1245. if (need_clock_drift_compensation) {
  1246. jack_info("Clock drift compensation activated...");
  1247. // Get the property data size
  1248. osErr = AudioObjectGetPropertyDataSize(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize);
  1249. if (osErr != noErr) {
  1250. jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
  1251. printError(osErr);
  1252. }
  1253. // Calculate the number of object IDs
  1254. subDevicesNum = outSize / sizeof(AudioObjectID);
  1255. jack_info("JackCoreAudioAdapter::CreateAggregateDevice clock drift compensation, number of sub-devices = %d", subDevicesNum);
  1256. AudioObjectID subDevices[subDevicesNum];
  1257. outSize = sizeof(subDevices);
  1258. osErr = AudioObjectGetPropertyData(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize, subDevices);
  1259. if (osErr != noErr) {
  1260. jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
  1261. printError(osErr);
  1262. }
  1263. // Set kAudioSubDevicePropertyDriftCompensation property...
  1264. for (UInt32 index = 0; index < subDevicesNum; ++index) {
  1265. UInt32 theDriftCompensationValue = 1;
  1266. osErr = AudioObjectSetPropertyData(subDevices[index], &theAddressDrift, 0, NULL, sizeof(UInt32), &theDriftCompensationValue);
  1267. if (osErr != noErr) {
  1268. jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioSubDevicePropertyDriftCompensation error");
  1269. printError(osErr);
  1270. }
  1271. }
  1272. } else {
  1273. jack_info("Clock drift compensation was asked but is not needed (devices use the same clock domain)");
  1274. }
  1275. }
  1276. // pause again to give the changes time to take effect
  1277. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  1278. //----------
  1279. // Clean up
  1280. //----------
  1281. // release the private AD key
  1282. CFRelease(AggregateDeviceNumberRef);
  1283. // release the CF objects we have created - we don't need them any more
  1284. CFRelease(aggDeviceDict);
  1285. CFRelease(subDevicesArray);
  1286. if (subDevicesArrayClock) {
  1287. CFRelease(subDevicesArrayClock);
  1288. }
  1289. // release the device UID
  1290. for (UInt32 i = 0; i < captureDeviceUID.size(); i++) {
  1291. CFRelease(captureDeviceUID[i]);
  1292. }
  1293. for (UInt32 i = 0; i < playbackDeviceUID.size(); i++) {
  1294. CFRelease(playbackDeviceUID[i]);
  1295. }
  1296. jack_log("New aggregate device %ld", *outAggregateDevice);
  1297. return noErr;
  1298. error:
  1299. DestroyAggregateDevice();
  1300. return -1;
  1301. }
  1302. bool JackCoreAudioAdapter::IsAggregateDevice(AudioDeviceID device)
  1303. {
  1304. OSStatus err = noErr;
  1305. AudioObjectID sub_device[32];
  1306. UInt32 outSize = sizeof(sub_device);
  1307. err = AudioDeviceGetProperty(device, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  1308. if (err != noErr) {
  1309. jack_log("Device does not have subdevices");
  1310. return false;
  1311. } else {
  1312. int num_devices = outSize / sizeof(AudioObjectID);
  1313. jack_log("Device does has %d subdevices", num_devices);
  1314. return true;
  1315. }
  1316. }
  1317. void JackCoreAudioAdapter::CloseAUHAL()
  1318. {
  1319. AudioUnitUninitialize(fAUHAL);
  1320. CloseComponent(fAUHAL);
  1321. }
  1322. int JackCoreAudioAdapter::Open()
  1323. {
  1324. return (AudioOutputUnitStart(fAUHAL) != noErr) ? -1 : 0;
  1325. }
  1326. int JackCoreAudioAdapter::Close()
  1327. {
  1328. #ifdef JACK_MONITOR
  1329. fTable.Save(fHostBufferSize, fHostSampleRate, fAdaptedSampleRate, fAdaptedBufferSize);
  1330. #endif
  1331. AudioOutputUnitStop(fAUHAL);
  1332. DisposeBuffers();
  1333. CloseAUHAL();
  1334. RemoveListeners();
  1335. if (fPluginID > 0) {
  1336. DestroyAggregateDevice();
  1337. }
  1338. return 0;
  1339. }
  1340. int JackCoreAudioAdapter::SetSampleRate(jack_nframes_t sample_rate)
  1341. {
  1342. JackAudioAdapterInterface::SetHostSampleRate(sample_rate);
  1343. Close();
  1344. return Open();
  1345. }
  1346. int JackCoreAudioAdapter::SetBufferSize(jack_nframes_t buffer_size)
  1347. {
  1348. JackAudioAdapterInterface::SetHostBufferSize(buffer_size);
  1349. Close();
  1350. return Open();
  1351. }
  1352. OSStatus JackCoreAudioAdapter::GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies)
  1353. {
  1354. OSStatus err = noErr;
  1355. UInt32 outSize1, outSize2, outSize3;
  1356. Boolean outWritable;
  1357. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreams, &outSize1, &outWritable);
  1358. if (err == noErr) {
  1359. int stream_count = outSize1 / sizeof(UInt32);
  1360. AudioStreamID streamIDs[stream_count];
  1361. AudioBufferList bufferList[stream_count];
  1362. UInt32 streamLatency;
  1363. outSize2 = sizeof(UInt32);
  1364. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreams, &outSize1, streamIDs);
  1365. if (err != noErr) {
  1366. jack_error("GetStreamLatencies kAudioDevicePropertyStreams err = %d", err);
  1367. return err;
  1368. }
  1369. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize3, &outWritable);
  1370. if (err != noErr) {
  1371. jack_error("GetStreamLatencies kAudioDevicePropertyStreamConfiguration err = %d", err);
  1372. return err;
  1373. }
  1374. for (int i = 0; i < stream_count; i++) {
  1375. err = AudioStreamGetProperty(streamIDs[i], 0, kAudioStreamPropertyLatency, &outSize2, &streamLatency);
  1376. if (err != noErr) {
  1377. jack_error("GetStreamLatencies kAudioStreamPropertyLatency err = %d", err);
  1378. return err;
  1379. }
  1380. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize3, bufferList);
  1381. if (err != noErr) {
  1382. jack_error("GetStreamLatencies kAudioDevicePropertyStreamConfiguration err = %d", err);
  1383. return err;
  1384. }
  1385. // Push 'channel' time the stream latency
  1386. for (uint k = 0; k < bufferList->mBuffers[i].mNumberChannels; k++) {
  1387. latencies.push_back(streamLatency);
  1388. }
  1389. }
  1390. }
  1391. return err;
  1392. }
  1393. int JackCoreAudioAdapter::GetLatency(int port_index, bool input)
  1394. {
  1395. UInt32 size = sizeof(UInt32);
  1396. UInt32 value1 = 0;
  1397. UInt32 value2 = 0;
  1398. OSStatus err = AudioDeviceGetProperty(fDeviceID, 0, input, kAudioDevicePropertyLatency, &size, &value1);
  1399. if (err != noErr) {
  1400. jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error");
  1401. }
  1402. err = AudioDeviceGetProperty(fDeviceID, 0, input, kAudioDevicePropertySafetyOffset, &size, &value2);
  1403. if (err != noErr) {
  1404. jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error");
  1405. }
  1406. // TODO : add stream latency
  1407. return value1 + value2 + fAdaptedBufferSize;
  1408. }
  1409. int JackCoreAudioAdapter::GetInputLatency(int port_index)
  1410. {
  1411. if (port_index < int(fInputLatencies.size())) {
  1412. return GetLatency(port_index, true) + fInputLatencies[port_index];
  1413. } else {
  1414. // No stream latency
  1415. return GetLatency(port_index, true);
  1416. }
  1417. }
  1418. int JackCoreAudioAdapter::GetOutputLatency(int port_index)
  1419. {
  1420. if (port_index < int(fOutputLatencies.size())) {
  1421. return GetLatency(port_index, false) + fOutputLatencies[port_index];
  1422. } else {
  1423. // No stream latency
  1424. return GetLatency(port_index, false);
  1425. }
  1426. }
  1427. } // namespace
  1428. #ifdef __cplusplus
  1429. extern "C"
  1430. {
  1431. #endif
  1432. SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor()
  1433. {
  1434. jack_driver_desc_t * desc;
  1435. jack_driver_desc_filler_t filler;
  1436. jack_driver_param_value_t value;
  1437. desc = jack_driver_descriptor_construct("audioadapter", JackDriverNone, "netjack audio <==> net backend adapter", &filler);
  1438. value.i = -1;
  1439. 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");
  1440. 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");
  1441. 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");
  1442. value.str[0] = 0;
  1443. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Input CoreAudio device name", NULL);
  1444. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Output CoreAudio device name", NULL);
  1445. value.ui = 44100U;
  1446. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  1447. value.ui = 512U;
  1448. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  1449. value.i = TRUE;
  1450. jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
  1451. value.str[0] = 0;
  1452. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "CoreAudio device name", NULL);
  1453. value.i = TRUE;
  1454. jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available CoreAudio devices", NULL);
  1455. value.ui = 0;
  1456. jack_driver_descriptor_add_parameter(desc, &filler, "quality", 'q', JackDriverParamInt, &value, NULL, "Resample algorithm quality (0 - 4)", NULL);
  1457. value.ui = 32768;
  1458. jack_driver_descriptor_add_parameter(desc, &filler, "ring-buffer", 'g', JackDriverParamInt, &value, NULL, "Fixed ringbuffer size", "Fixed ringbuffer size (if not set => automatic adaptative)");
  1459. value.i = FALSE;
  1460. 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");
  1461. return desc;
  1462. }
  1463. #ifdef __cplusplus
  1464. }
  1465. #endif