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.

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