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.

2266 lines
88KB

  1. /*
  2. Copyright (C) 2004-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 "JackCoreAudioDriver.h"
  16. #include "JackEngineControl.h"
  17. #include "JackMachThread.h"
  18. #include "JackGraphManager.h"
  19. #include "JackError.h"
  20. #include "JackClientControl.h"
  21. #include "JackDriverLoader.h"
  22. #include "JackGlobals.h"
  23. #include "JackTools.h"
  24. #include "JackCompilerDeps.h"
  25. #include "JackLockedEngine.h"
  26. #include <sstream>
  27. #include <iostream>
  28. #include <CoreServices/CoreServices.h>
  29. #include <CoreFoundation/CFNumber.h>
  30. namespace Jack
  31. {
  32. static void Print4CharCode(const char* msg, long c)
  33. {
  34. UInt32 __4CC_number = (c);
  35. char __4CC_string[5];
  36. *((SInt32*)__4CC_string) = EndianU32_NtoB(__4CC_number);
  37. __4CC_string[4] = 0;
  38. jack_log("%s'%s'", (msg), __4CC_string);
  39. }
  40. static void PrintStreamDesc(AudioStreamBasicDescription *inDesc)
  41. {
  42. jack_log("- - - - - - - - - - - - - - - - - - - -");
  43. jack_log(" Sample Rate:%f", inDesc->mSampleRate);
  44. jack_log(" Format ID:%.*s", (int) sizeof(inDesc->mFormatID), (char*)&inDesc->mFormatID);
  45. jack_log(" Format Flags:%lX", inDesc->mFormatFlags);
  46. jack_log(" Bytes per Packet:%ld", inDesc->mBytesPerPacket);
  47. jack_log(" Frames per Packet:%ld", inDesc->mFramesPerPacket);
  48. jack_log(" Bytes per Frame:%ld", inDesc->mBytesPerFrame);
  49. jack_log(" Channels per Frame:%ld", inDesc->mChannelsPerFrame);
  50. jack_log(" Bits per Channel:%ld", inDesc->mBitsPerChannel);
  51. jack_log("- - - - - - - - - - - - - - - - - - - -");
  52. }
  53. static void printError(OSStatus err)
  54. {
  55. switch (err) {
  56. case kAudioHardwareNoError:
  57. jack_log("error code : kAudioHardwareNoError");
  58. break;
  59. case kAudioConverterErr_FormatNotSupported:
  60. jack_log("error code : kAudioConverterErr_FormatNotSupported");
  61. break;
  62. case kAudioConverterErr_OperationNotSupported:
  63. jack_log("error code : kAudioConverterErr_OperationNotSupported");
  64. break;
  65. case kAudioConverterErr_PropertyNotSupported:
  66. jack_log("error code : kAudioConverterErr_PropertyNotSupported");
  67. break;
  68. case kAudioConverterErr_InvalidInputSize:
  69. jack_log("error code : kAudioConverterErr_InvalidInputSize");
  70. break;
  71. case kAudioConverterErr_InvalidOutputSize:
  72. jack_log("error code : kAudioConverterErr_InvalidOutputSize");
  73. break;
  74. case kAudioConverterErr_UnspecifiedError:
  75. jack_log("error code : kAudioConverterErr_UnspecifiedError");
  76. break;
  77. case kAudioConverterErr_BadPropertySizeError:
  78. jack_log("error code : kAudioConverterErr_BadPropertySizeError");
  79. break;
  80. case kAudioConverterErr_RequiresPacketDescriptionsError:
  81. jack_log("error code : kAudioConverterErr_RequiresPacketDescriptionsError");
  82. break;
  83. case kAudioConverterErr_InputSampleRateOutOfRange:
  84. jack_log("error code : kAudioConverterErr_InputSampleRateOutOfRange");
  85. break;
  86. case kAudioConverterErr_OutputSampleRateOutOfRange:
  87. jack_log("error code : kAudioConverterErr_OutputSampleRateOutOfRange");
  88. break;
  89. case kAudioHardwareNotRunningError:
  90. jack_log("error code : kAudioHardwareNotRunningError");
  91. break;
  92. case kAudioHardwareUnknownPropertyError:
  93. jack_log("error code : kAudioHardwareUnknownPropertyError");
  94. break;
  95. case kAudioHardwareIllegalOperationError:
  96. jack_log("error code : kAudioHardwareIllegalOperationError");
  97. break;
  98. case kAudioHardwareBadDeviceError:
  99. jack_log("error code : kAudioHardwareBadDeviceError");
  100. break;
  101. case kAudioHardwareBadStreamError:
  102. jack_log("error code : kAudioHardwareBadStreamError");
  103. break;
  104. case kAudioDeviceUnsupportedFormatError:
  105. jack_log("error code : kAudioDeviceUnsupportedFormatError");
  106. break;
  107. case kAudioDevicePermissionsError:
  108. jack_log("error code : kAudioDevicePermissionsError");
  109. break;
  110. case kAudioHardwareBadObjectError:
  111. jack_log("error code : kAudioHardwareBadObjectError");
  112. break;
  113. case kAudioHardwareUnsupportedOperationError:
  114. jack_log("error code : kAudioHardwareUnsupportedOperationError");
  115. break;
  116. default:
  117. Print4CharCode("error code : unknown", err);
  118. break;
  119. }
  120. }
  121. static OSStatus DisplayDeviceNames()
  122. {
  123. UInt32 size;
  124. Boolean isWritable;
  125. int i, deviceNum;
  126. OSStatus err;
  127. CFStringRef UIname;
  128. err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
  129. if (err != noErr) {
  130. return err;
  131. }
  132. deviceNum = size / sizeof(AudioDeviceID);
  133. AudioDeviceID devices[deviceNum];
  134. err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
  135. if (err != noErr) {
  136. return err;
  137. }
  138. for (i = 0; i < deviceNum; i++) {
  139. char device_name[256];
  140. char internal_name[256];
  141. size = sizeof(CFStringRef);
  142. UIname = NULL;
  143. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  144. if (err == noErr) {
  145. CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
  146. } else {
  147. goto error;
  148. }
  149. size = 256;
  150. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
  151. if (err != noErr) {
  152. return err;
  153. }
  154. jack_info("Device name = \'%s\', internal name = \'%s\' (to be used as -C, -P, or -d parameter)", device_name, internal_name);
  155. }
  156. return noErr;
  157. error:
  158. if (UIname != NULL) {
  159. CFRelease(UIname);
  160. }
  161. return err;
  162. }
  163. static CFStringRef GetDeviceName(AudioDeviceID id)
  164. {
  165. UInt32 size = sizeof(CFStringRef);
  166. CFStringRef UIname;
  167. OSStatus err = AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  168. return (err == noErr) ? UIname : NULL;
  169. }
  170. static void ParseChannelList(const string& list, vector<int>& result)
  171. {
  172. stringstream ss(list);
  173. string token;
  174. int chan;
  175. while (ss >> token) {
  176. istringstream ins;
  177. ins.str(token);
  178. ins >> chan;
  179. result.push_back(chan);
  180. }
  181. }
  182. OSStatus JackCoreAudioDriver::Render(void *inRefCon,
  183. AudioUnitRenderActionFlags *ioActionFlags,
  184. const AudioTimeStamp *inTimeStamp,
  185. UInt32 inBusNumber,
  186. UInt32 inNumberFrames,
  187. AudioBufferList *ioData)
  188. {
  189. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inRefCon;
  190. driver->fActionFags = ioActionFlags;
  191. driver->fCurrentTime = (AudioTimeStamp *)inTimeStamp;
  192. driver->fDriverOutputData = ioData;
  193. // Setup threaded based log function et get RT thread parameters once...
  194. if (set_threaded_log_function()) {
  195. jack_log("set_threaded_log_function");
  196. JackMachThread::GetParams(pthread_self(), &driver->fEngineControl->fPeriod, &driver->fEngineControl->fComputation, &driver->fEngineControl->fConstraint);
  197. if (driver->fComputationGrain > 0) {
  198. jack_log("JackCoreAudioDriver::Render : RT thread computation setup to %d percent of period", int(driver->fComputationGrain * 100));
  199. driver->fEngineControl->fComputation = driver->fEngineControl->fPeriod * driver->fComputationGrain;
  200. }
  201. }
  202. // Signal waiting start function...
  203. driver->fState = true;
  204. driver->CycleTakeBeginTime();
  205. if (driver->Process() < 0) {
  206. jack_error("Process error, stopping driver.");
  207. driver->NotifyFailure(JackBackendError, "Process error, stopping driver."); // Message length limited to JACK_MESSAGE_SIZE
  208. driver->Stop();
  209. kill(JackTools::GetPID(), SIGINT);
  210. return kAudioHardwareUnsupportedOperationError;
  211. } else {
  212. return noErr;
  213. }
  214. }
  215. int JackCoreAudioDriver::Read()
  216. {
  217. if (fCaptureChannels > 0) { // Calling AudioUnitRender with no input returns a '????' error (callback setting issue ??), so hack to avoid it here...
  218. return (AudioUnitRender(fAUHAL, fActionFags, fCurrentTime, 1, fEngineControl->fBufferSize, fJackInputData) == noErr) ? 0 : -1;
  219. } else {
  220. return 0;
  221. }
  222. }
  223. int JackCoreAudioDriver::Write()
  224. {
  225. for (int i = 0; i < fPlaybackChannels; i++) {
  226. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  227. jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
  228. int size = sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
  229. memcpy((jack_default_audio_sample_t*)fDriverOutputData->mBuffers[i].mData, buffer, size);
  230. // Monitor ports
  231. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0) {
  232. memcpy(GetMonitorBuffer(i), buffer, size);
  233. }
  234. } else {
  235. memset((jack_default_audio_sample_t*)fDriverOutputData->mBuffers[i].mData, 0, sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize);
  236. }
  237. }
  238. return 0;
  239. }
  240. OSStatus JackCoreAudioDriver::SRNotificationCallback(AudioDeviceID inDevice,
  241. UInt32 inChannel,
  242. Boolean isInput,
  243. AudioDevicePropertyID inPropertyID,
  244. void* inClientData)
  245. {
  246. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  247. switch (inPropertyID) {
  248. case kAudioDevicePropertyNominalSampleRate: {
  249. jack_log("JackCoreAudioDriver::SRNotificationCallback kAudioDevicePropertyNominalSampleRate");
  250. // Check new sample rate
  251. Float64 tmp_sample_rate;
  252. UInt32 outSize = sizeof(Float64);
  253. OSStatus err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &tmp_sample_rate);
  254. if (err != noErr) {
  255. jack_error("Cannot get current sample rate");
  256. printError(err);
  257. } else {
  258. jack_log("SRNotificationCallback : checked sample rate = %f", tmp_sample_rate);
  259. }
  260. driver->fState = true;
  261. break;
  262. }
  263. }
  264. return noErr;
  265. }
  266. OSStatus JackCoreAudioDriver::BSNotificationCallback(AudioDeviceID inDevice,
  267. UInt32 inChannel,
  268. Boolean isInput,
  269. AudioDevicePropertyID inPropertyID,
  270. void* inClientData)
  271. {
  272. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  273. switch (inPropertyID) {
  274. case kAudioDevicePropertyBufferFrameSize: {
  275. jack_log("JackCoreAudioDriver::BSNotificationCallback kAudioDevicePropertyBufferFrameSize");
  276. // Check new buffer size
  277. UInt32 tmp_buffer_size;
  278. UInt32 outSize = sizeof(UInt32);
  279. OSStatus err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyBufferFrameSize, &outSize, &tmp_buffer_size);
  280. if (err != noErr) {
  281. jack_error("Cannot get current buffer size");
  282. printError(err);
  283. } else {
  284. jack_log("BSNotificationCallback : checked buffer size = %d", tmp_buffer_size);
  285. }
  286. driver->fState = true;
  287. break;
  288. }
  289. }
  290. return noErr;
  291. }
  292. // A better implementation would possibly try to recover in case of hardware device change (see HALLAB HLFilePlayerWindowControllerAudioDevicePropertyListenerProc code)
  293. OSStatus JackCoreAudioDriver::DeviceNotificationCallback(AudioDeviceID inDevice,
  294. UInt32 inChannel,
  295. Boolean isInput,
  296. AudioDevicePropertyID inPropertyID,
  297. void* inClientData)
  298. {
  299. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  300. switch (inPropertyID) {
  301. case kAudioDevicePropertyDeviceIsRunning: {
  302. UInt32 isrunning = 0;
  303. UInt32 outsize = sizeof(UInt32);
  304. if (AudioDeviceGetProperty(driver->fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyDeviceIsRunning, &outsize, &isrunning) == noErr) {
  305. jack_log("JackCoreAudioDriver::DeviceNotificationCallback kAudioDevicePropertyDeviceIsRunning = %d", isrunning);
  306. }
  307. break;
  308. }
  309. case kAudioDeviceProcessorOverload: {
  310. jack_error("JackCoreAudioDriver::DeviceNotificationCallback kAudioDeviceProcessorOverload");
  311. jack_time_t cur_time = GetMicroSeconds();
  312. driver->NotifyXRun(cur_time, float(cur_time - driver->fBeginDateUst)); // Better this value than nothing...
  313. break;
  314. }
  315. case kAudioDevicePropertyStreamConfiguration: {
  316. jack_error("Cannot handle kAudioDevicePropertyStreamConfiguration : server will quit...");
  317. driver->NotifyFailure(JackBackendError, "Another application has changed the device configuration."); // Message length limited to JACK_MESSAGE_SIZE
  318. driver->CloseAUHAL();
  319. kill(JackTools::GetPID(), SIGINT);
  320. return kAudioHardwareUnsupportedOperationError;
  321. }
  322. case kAudioDevicePropertyNominalSampleRate: {
  323. Float64 sample_rate = 0;
  324. UInt32 outsize = sizeof(Float64);
  325. OSStatus err = AudioDeviceGetProperty(driver->fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outsize, &sample_rate);
  326. if (err != noErr) {
  327. return kAudioHardwareUnsupportedOperationError;
  328. }
  329. char device_name[256];
  330. const char* digidesign_name = "Digidesign";
  331. driver->GetDeviceNameFromID(driver->fDeviceID, device_name);
  332. if (sample_rate != driver->fEngineControl->fSampleRate) {
  333. // Digidesign hardware, so "special" code : change the SR again here
  334. if (strncmp(device_name, digidesign_name, 10) == 0) {
  335. jack_log("Digidesign HW = %s", device_name);
  336. // Set sample rate again...
  337. sample_rate = driver->fEngineControl->fSampleRate;
  338. err = AudioDeviceSetProperty(driver->fDeviceID, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outsize, &sample_rate);
  339. if (err != noErr) {
  340. jack_error("Cannot set sample rate = %f", sample_rate);
  341. printError(err);
  342. } else {
  343. jack_log("Set sample rate = %f", sample_rate);
  344. }
  345. // Check new sample rate again...
  346. outsize = sizeof(Float64);
  347. err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outsize, &sample_rate);
  348. if (err != noErr) {
  349. jack_error("Cannot get current sample rate");
  350. printError(err);
  351. } else {
  352. jack_log("Checked sample rate = %f", sample_rate);
  353. }
  354. return noErr;
  355. } else {
  356. driver->NotifyFailure(JackBackendError, "Another application has changed the sample rate."); // Message length limited to JACK_MESSAGE_SIZE
  357. driver->CloseAUHAL();
  358. kill(JackTools::GetPID(), SIGINT);
  359. return kAudioHardwareUnsupportedOperationError;
  360. }
  361. }
  362. }
  363. }
  364. return noErr;
  365. }
  366. OSStatus JackCoreAudioDriver::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
  367. {
  368. UInt32 size = sizeof(AudioValueTranslation);
  369. CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
  370. AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
  371. if (inIUD == NULL) {
  372. return kAudioHardwareUnspecifiedError;
  373. } else {
  374. OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
  375. CFRelease(inIUD);
  376. jack_log("GetDeviceIDFromUID %s %ld", UID, *id);
  377. return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
  378. }
  379. }
  380. OSStatus JackCoreAudioDriver::GetDefaultDevice(AudioDeviceID* id)
  381. {
  382. OSStatus res;
  383. UInt32 theSize = sizeof(UInt32);
  384. AudioDeviceID inDefault;
  385. AudioDeviceID outDefault;
  386. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
  387. return res;
  388. }
  389. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
  390. return res;
  391. }
  392. jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
  393. // Get the device only if default input and output are the same
  394. if (inDefault != outDefault) {
  395. jack_error("Default input and output devices are not the same !!");
  396. return kAudioHardwareBadDeviceError;
  397. } else if (inDefault == 0) {
  398. jack_error("Default input and output devices are null !!");
  399. return kAudioHardwareBadDeviceError;
  400. } else {
  401. *id = inDefault;
  402. return noErr;
  403. }
  404. }
  405. OSStatus JackCoreAudioDriver::GetDefaultInputDevice(AudioDeviceID* id)
  406. {
  407. OSStatus res;
  408. UInt32 theSize = sizeof(UInt32);
  409. AudioDeviceID inDefault;
  410. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
  411. return res;
  412. }
  413. if (inDefault == 0) {
  414. jack_error("Error : input device is 0, please select a correct one !!");
  415. return -1;
  416. }
  417. jack_log("GetDefaultInputDevice: input = %ld ", inDefault);
  418. *id = inDefault;
  419. return noErr;
  420. }
  421. OSStatus JackCoreAudioDriver::GetDefaultOutputDevice(AudioDeviceID* id)
  422. {
  423. OSStatus res;
  424. UInt32 theSize = sizeof(UInt32);
  425. AudioDeviceID outDefault;
  426. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
  427. return res;
  428. }
  429. if (outDefault == 0) {
  430. jack_error("Error : output device is 0, please select a correct one !!");
  431. return -1;
  432. }
  433. jack_log("GetDefaultOutputDevice: output = %ld", outDefault);
  434. *id = outDefault;
  435. return noErr;
  436. }
  437. OSStatus JackCoreAudioDriver::GetDeviceNameFromID(AudioDeviceID id, char* name)
  438. {
  439. UInt32 size = 256;
  440. return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
  441. }
  442. OSStatus JackCoreAudioDriver::GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput)
  443. {
  444. OSStatus err = noErr;
  445. UInt32 outSize;
  446. Boolean outWritable;
  447. channelCount = 0;
  448. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  449. if (err == noErr) {
  450. AudioBufferList bufferList[outSize];
  451. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  452. if (err == noErr) {
  453. for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++) {
  454. channelCount += bufferList->mBuffers[i].mNumberChannels;
  455. }
  456. }
  457. }
  458. return err;
  459. }
  460. JackCoreAudioDriver::JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
  461. : JackAudioDriver(name, alias, engine, table),
  462. fJackInputData(NULL),
  463. fDriverOutputData(NULL),
  464. fPluginID(0),
  465. fState(false),
  466. fHogged(false),
  467. fIOUsage(1.f),
  468. fComputationGrain(-1.f),
  469. fClockDriftCompensate(false)
  470. {}
  471. JackCoreAudioDriver::~JackCoreAudioDriver()
  472. {}
  473. OSStatus JackCoreAudioDriver::DestroyAggregateDevice()
  474. {
  475. OSStatus osErr = noErr;
  476. AudioObjectPropertyAddress pluginAOPA;
  477. pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice;
  478. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  479. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  480. UInt32 outDataSize;
  481. if (fPluginID > 0) {
  482. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  483. if (osErr != noErr) {
  484. jack_error("JackCoreAudioDriver::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
  485. printError(osErr);
  486. return osErr;
  487. }
  488. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID);
  489. if (osErr != noErr) {
  490. jack_error("JackCoreAudioDriver::DestroyAggregateDevice : AudioObjectGetPropertyData error");
  491. printError(osErr);
  492. return osErr;
  493. }
  494. }
  495. return noErr;
  496. }
  497. OSStatus JackCoreAudioDriver::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
  498. {
  499. OSStatus err = noErr;
  500. AudioObjectID sub_device[32];
  501. UInt32 outSize = sizeof(sub_device);
  502. err = AudioDeviceGetProperty(captureDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  503. vector<AudioDeviceID> captureDeviceIDArray;
  504. if (err != noErr) {
  505. jack_log("Input device does not have subdevices");
  506. captureDeviceIDArray.push_back(captureDeviceID);
  507. } else {
  508. int num_devices = outSize / sizeof(AudioObjectID);
  509. jack_log("Input device has %d subdevices", num_devices);
  510. for (int i = 0; i < num_devices; i++) {
  511. captureDeviceIDArray.push_back(sub_device[i]);
  512. }
  513. }
  514. err = AudioDeviceGetProperty(playbackDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  515. vector<AudioDeviceID> playbackDeviceIDArray;
  516. if (err != noErr) {
  517. jack_log("Output device does not have subdevices");
  518. playbackDeviceIDArray.push_back(playbackDeviceID);
  519. } else {
  520. int num_devices = outSize / sizeof(AudioObjectID);
  521. jack_log("Output device has %d subdevices", num_devices);
  522. for (int i = 0; i < num_devices; i++) {
  523. playbackDeviceIDArray.push_back(sub_device[i]);
  524. }
  525. }
  526. return CreateAggregateDeviceAux(captureDeviceIDArray, playbackDeviceIDArray, samplerate, outAggregateDevice);
  527. }
  528. OSStatus JackCoreAudioDriver::CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
  529. {
  530. OSStatus osErr = noErr;
  531. UInt32 outSize;
  532. Boolean outWritable;
  533. // Prepare sub-devices for clock drift compensation
  534. // Workaround for bug in the HAL : until 10.6.2
  535. AudioObjectPropertyAddress theAddressOwned = { kAudioObjectPropertyOwnedObjects, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  536. AudioObjectPropertyAddress theAddressDrift = { kAudioSubDevicePropertyDriftCompensation, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  537. UInt32 theQualifierDataSize = sizeof(AudioObjectID);
  538. AudioClassID inClass = kAudioSubDeviceClassID;
  539. void* theQualifierData = &inClass;
  540. UInt32 subDevicesNum = 0;
  541. //---------------------------------------------------------------------------
  542. // Setup SR of both devices otherwise creating AD may fail...
  543. //---------------------------------------------------------------------------
  544. UInt32 keptclockdomain = 0;
  545. UInt32 clockdomain = 0;
  546. outSize = sizeof(UInt32);
  547. bool need_clock_drift_compensation = false;
  548. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  549. if (SetupSampleRateAux(captureDeviceID[i], samplerate) < 0) {
  550. jack_error("JackCoreAudioDriver::CreateAggregateDevice : cannot set SR of input device");
  551. } else {
  552. // Check clock domain
  553. osErr = AudioDeviceGetProperty(captureDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
  554. if (osErr != 0) {
  555. jack_error("JackCoreAudioDriver::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
  556. printError(osErr);
  557. } else {
  558. keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
  559. jack_log("JackCoreAudioDriver::CreateAggregateDevice : input clockdomain = %d", clockdomain);
  560. if (clockdomain != 0 && clockdomain != keptclockdomain) {
  561. jack_error("JackCoreAudioDriver::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...");
  562. need_clock_drift_compensation = true;
  563. }
  564. }
  565. }
  566. }
  567. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  568. if (SetupSampleRateAux(playbackDeviceID[i], samplerate) < 0) {
  569. jack_error("JackCoreAudioDriver::CreateAggregateDevice : cannot set SR of output device");
  570. } else {
  571. // Check clock domain
  572. osErr = AudioDeviceGetProperty(playbackDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
  573. if (osErr != 0) {
  574. jack_error("JackCoreAudioDriver::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
  575. printError(osErr);
  576. } else {
  577. keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
  578. jack_log("JackCoreAudioDriver::CreateAggregateDevice : output clockdomain = %d", clockdomain);
  579. if (clockdomain != 0 && clockdomain != keptclockdomain) {
  580. jack_error("JackCoreAudioDriver::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...");
  581. need_clock_drift_compensation = true;
  582. }
  583. }
  584. }
  585. }
  586. // If no valid clock domain was found, then assume we have to compensate...
  587. if (keptclockdomain == 0) {
  588. need_clock_drift_compensation = true;
  589. }
  590. //---------------------------------------------------------------------------
  591. // Start to create a new aggregate by getting the base audio hardware plugin
  592. //---------------------------------------------------------------------------
  593. char device_name[256];
  594. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  595. GetDeviceNameFromID(captureDeviceID[i], device_name);
  596. jack_info("Separated input = '%s' ", device_name);
  597. }
  598. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  599. GetDeviceNameFromID(playbackDeviceID[i], device_name);
  600. jack_info("Separated output = '%s' ", device_name);
  601. }
  602. osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable);
  603. if (osErr != noErr) {
  604. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioHardwareGetPropertyInfo kAudioHardwarePropertyPlugInForBundleID error");
  605. printError(osErr);
  606. return osErr;
  607. }
  608. AudioValueTranslation pluginAVT;
  609. CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
  610. pluginAVT.mInputData = &inBundleRef;
  611. pluginAVT.mInputDataSize = sizeof(inBundleRef);
  612. pluginAVT.mOutputData = &fPluginID;
  613. pluginAVT.mOutputDataSize = sizeof(fPluginID);
  614. osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT);
  615. if (osErr != noErr) {
  616. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioHardwareGetProperty kAudioHardwarePropertyPlugInForBundleID error");
  617. printError(osErr);
  618. return osErr;
  619. }
  620. //-------------------------------------------------
  621. // Create a CFDictionary for our aggregate device
  622. //-------------------------------------------------
  623. CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  624. CFStringRef AggregateDeviceNameRef = CFSTR("JackDuplex");
  625. CFStringRef AggregateDeviceUIDRef = CFSTR("com.grame.JackDuplex");
  626. // add the name of the device to the dictionary
  627. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
  628. // add our choice of UID for the aggregate device to the dictionary
  629. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
  630. // add a "private aggregate key" to the dictionary
  631. int value = 1;
  632. CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
  633. SInt32 system;
  634. Gestalt(gestaltSystemVersion, &system);
  635. jack_log("JackCoreAudioDriver::CreateAggregateDevice : system version = %x limit = %x", system, 0x00001054);
  636. // Starting with 10.5.4 systems, the AD can be internal... (better)
  637. if (system < 0x00001054) {
  638. jack_log("JackCoreAudioDriver::CreateAggregateDevice : public aggregate device....");
  639. } else {
  640. jack_log("JackCoreAudioDriver::CreateAggregateDevice : private aggregate device....");
  641. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
  642. }
  643. // Prepare sub-devices for clock drift compensation
  644. CFMutableArrayRef subDevicesArrayClock = NULL;
  645. /*
  646. if (fClockDriftCompensate) {
  647. if (need_clock_drift_compensation) {
  648. jack_info("Clock drift compensation activated...");
  649. subDevicesArrayClock = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  650. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  651. CFStringRef UID = GetDeviceName(captureDeviceID[i]);
  652. if (UID) {
  653. CFMutableDictionaryRef subdeviceAggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  654. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceUIDKey), UID);
  655. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceDriftCompensationKey), AggregateDeviceNumberRef);
  656. //CFRelease(UID);
  657. CFArrayAppendValue(subDevicesArrayClock, subdeviceAggDeviceDict);
  658. }
  659. }
  660. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  661. CFStringRef UID = GetDeviceName(playbackDeviceID[i]);
  662. if (UID) {
  663. CFMutableDictionaryRef subdeviceAggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  664. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceUIDKey), UID);
  665. CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceDriftCompensationKey), AggregateDeviceNumberRef);
  666. //CFRelease(UID);
  667. CFArrayAppendValue(subDevicesArrayClock, subdeviceAggDeviceDict);
  668. }
  669. }
  670. // add sub-device clock array for the aggregate device to the dictionary
  671. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceSubDeviceListKey), subDevicesArrayClock);
  672. } else {
  673. jack_info("Clock drift compensation was asked but is not needed (devices use the same clock domain)");
  674. }
  675. }
  676. */
  677. //-------------------------------------------------
  678. // Create a CFMutableArray for our sub-device list
  679. //-------------------------------------------------
  680. // we need to append the UID for each device to a CFMutableArray, so create one here
  681. CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  682. vector<CFStringRef> captureDeviceUID;
  683. for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
  684. CFStringRef ref = GetDeviceName(captureDeviceID[i]);
  685. if (ref == NULL) {
  686. return -1;
  687. }
  688. captureDeviceUID.push_back(ref);
  689. // input sub-devices in this example, so append the sub-device's UID to the CFArray
  690. CFArrayAppendValue(subDevicesArray, ref);
  691. }
  692. vector<CFStringRef> playbackDeviceUID;
  693. for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
  694. CFStringRef ref = GetDeviceName(playbackDeviceID[i]);
  695. if (ref == NULL) {
  696. return -1;
  697. }
  698. playbackDeviceUID.push_back(ref);
  699. // output sub-devices in this example, so append the sub-device's UID to the CFArray
  700. CFArrayAppendValue(subDevicesArray, ref);
  701. }
  702. //-----------------------------------------------------------------------
  703. // Feed the dictionary to the plugin, to create a blank aggregate device
  704. //-----------------------------------------------------------------------
  705. AudioObjectPropertyAddress pluginAOPA;
  706. pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
  707. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  708. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  709. UInt32 outDataSize;
  710. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  711. if (osErr != noErr) {
  712. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioObjectGetPropertyDataSize error");
  713. printError(osErr);
  714. goto error;
  715. }
  716. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, outAggregateDevice);
  717. if (osErr != noErr) {
  718. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioObjectGetPropertyData error");
  719. printError(osErr);
  720. goto error;
  721. }
  722. // pause for a bit to make sure that everything completed correctly
  723. // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
  724. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  725. //-------------------------
  726. // Set the sub-device list
  727. //-------------------------
  728. pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
  729. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  730. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  731. outDataSize = sizeof(CFMutableArrayRef);
  732. osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
  733. if (osErr != noErr) {
  734. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioObjectSetPropertyData for sub-device list error");
  735. printError(osErr);
  736. goto error;
  737. }
  738. // pause again to give the changes time to take effect
  739. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  740. //-----------------------
  741. // Set the master device
  742. //-----------------------
  743. // set the master device manually (this is the device which will act as the master clock for the aggregate device)
  744. // pass in the UID of the device you want to use
  745. pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
  746. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  747. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  748. outDataSize = sizeof(CFStringRef);
  749. osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID[0]); // First apture is master...
  750. if (osErr != noErr) {
  751. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioObjectSetPropertyData for master device error");
  752. printError(osErr);
  753. goto error;
  754. }
  755. // pause again to give the changes time to take effect
  756. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  757. // Prepare sub-devices for clock drift compensation
  758. // Workaround for bug in the HAL : until 10.6.2
  759. if (fClockDriftCompensate) {
  760. if (need_clock_drift_compensation) {
  761. jack_info("Clock drift compensation activated...");
  762. // Get the property data size
  763. osErr = AudioObjectGetPropertyDataSize(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize);
  764. if (osErr != noErr) {
  765. jack_error("JackCoreAudioDriver::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
  766. printError(osErr);
  767. }
  768. // Calculate the number of object IDs
  769. subDevicesNum = outSize / sizeof(AudioObjectID);
  770. jack_info("JackCoreAudioDriver::CreateAggregateDevice clock drift compensation, number of sub-devices = %d", subDevicesNum);
  771. AudioObjectID subDevices[subDevicesNum];
  772. outSize = sizeof(subDevices);
  773. osErr = AudioObjectGetPropertyData(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize, subDevices);
  774. if (osErr != noErr) {
  775. jack_error("JackCoreAudioDriver::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
  776. printError(osErr);
  777. }
  778. // Set kAudioSubDevicePropertyDriftCompensation property...
  779. for (UInt32 index = 0; index < subDevicesNum; ++index) {
  780. UInt32 theDriftCompensationValue = 1;
  781. osErr = AudioObjectSetPropertyData(subDevices[index], &theAddressDrift, 0, NULL, sizeof(UInt32), &theDriftCompensationValue);
  782. if (osErr != noErr) {
  783. jack_error("JackCoreAudioDriver::CreateAggregateDevice kAudioSubDevicePropertyDriftCompensation error");
  784. printError(osErr);
  785. }
  786. }
  787. } else {
  788. jack_info("Clock drift compensation was asked but is not needed (devices use the same clock domain)");
  789. }
  790. }
  791. // pause again to give the changes time to take effect
  792. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  793. //----------
  794. // Clean up
  795. //----------
  796. // release the private AD key
  797. CFRelease(AggregateDeviceNumberRef);
  798. // release the CF objects we have created - we don't need them any more
  799. CFRelease(aggDeviceDict);
  800. CFRelease(subDevicesArray);
  801. if (subDevicesArrayClock) {
  802. CFRelease(subDevicesArrayClock);
  803. }
  804. // release the device UID
  805. for (UInt32 i = 0; i < captureDeviceUID.size(); i++) {
  806. CFRelease(captureDeviceUID[i]);
  807. }
  808. for (UInt32 i = 0; i < playbackDeviceUID.size(); i++) {
  809. CFRelease(playbackDeviceUID[i]);
  810. }
  811. jack_log("New aggregate device %ld", *outAggregateDevice);
  812. return noErr;
  813. error:
  814. DestroyAggregateDevice();
  815. return -1;
  816. }
  817. int JackCoreAudioDriver::SetupDevices(const char* capture_driver_uid,
  818. const char* playback_driver_uid,
  819. char* capture_driver_name,
  820. char* playback_driver_name,
  821. jack_nframes_t samplerate)
  822. {
  823. capture_driver_name[0] = 0;
  824. playback_driver_name[0] = 0;
  825. // Duplex
  826. if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
  827. jack_log("JackCoreAudioDriver::Open duplex");
  828. // Same device for capture and playback...
  829. if (strcmp(capture_driver_uid, playback_driver_uid) == 0) {
  830. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  831. jack_log("Will take default in/out");
  832. if (GetDefaultDevice(&fDeviceID) != noErr) {
  833. jack_error("Cannot open default device");
  834. return -1;
  835. }
  836. }
  837. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  838. jack_error("Cannot get device name from device ID");
  839. return -1;
  840. }
  841. } else {
  842. // Creates aggregate device
  843. AudioDeviceID captureID, playbackID;
  844. if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr) {
  845. jack_log("Will take default input");
  846. if (GetDefaultInputDevice(&captureID) != noErr) {
  847. jack_error("Cannot open default input device");
  848. return -1;
  849. }
  850. }
  851. if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr) {
  852. jack_log("Will take default output");
  853. if (GetDefaultOutputDevice(&playbackID) != noErr) {
  854. jack_error("Cannot open default output device");
  855. return -1;
  856. }
  857. }
  858. if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
  859. return -1;
  860. }
  861. }
  862. // Capture only
  863. } else if (strcmp(capture_driver_uid, "") != 0) {
  864. jack_log("JackCoreAudioDriver::Open capture only");
  865. if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
  866. jack_log("Will take default input");
  867. if (GetDefaultInputDevice(&fDeviceID) != noErr) {
  868. jack_error("Cannot open default input device");
  869. return -1;
  870. }
  871. }
  872. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
  873. jack_error("Cannot get device name from device ID");
  874. return -1;
  875. }
  876. // Playback only
  877. } else if (strcmp(playback_driver_uid, "") != 0) {
  878. jack_log("JackCoreAudioDriver::Open playback only");
  879. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  880. jack_log("Will take default output");
  881. if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
  882. jack_error("Cannot open default output device");
  883. return -1;
  884. }
  885. }
  886. if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  887. jack_error("Cannot get device name from device ID");
  888. return -1;
  889. }
  890. // Use default driver in duplex mode
  891. } else {
  892. jack_log("JackCoreAudioDriver::Open default driver");
  893. if (GetDefaultDevice(&fDeviceID) != noErr) {
  894. jack_error("Cannot open default device in duplex mode, so aggregate default input and default output");
  895. // Creates aggregate device
  896. AudioDeviceID captureID, playbackID;
  897. if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr) {
  898. jack_log("Will take default input");
  899. if (GetDefaultInputDevice(&captureID) != noErr) {
  900. jack_error("Cannot open default input device");
  901. return -1;
  902. }
  903. }
  904. if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr) {
  905. jack_log("Will take default output");
  906. if (GetDefaultOutputDevice(&playbackID) != noErr) {
  907. jack_error("Cannot open default output device");
  908. return -1;
  909. }
  910. }
  911. if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
  912. return -1;
  913. }
  914. }
  915. }
  916. if (fHogged) {
  917. if (TakeHog()) {
  918. jack_info("Device = %ld has been hogged", fDeviceID);
  919. }
  920. }
  921. return 0;
  922. }
  923. /*
  924. Return the max possible input channels in in_nChannels and output channels in out_nChannels.
  925. */
  926. int JackCoreAudioDriver::SetupChannels(bool capturing, bool playing, int& inchannels, int& outchannels, int& in_nChannels, int& out_nChannels, bool strict)
  927. {
  928. OSStatus err = noErr;
  929. if (capturing) {
  930. err = GetTotalChannels(fDeviceID, in_nChannels, true);
  931. if (err != noErr) {
  932. jack_error("Cannot get input channel number");
  933. printError(err);
  934. return -1;
  935. } else {
  936. jack_log("Max input channels : %d", in_nChannels);
  937. }
  938. }
  939. if (playing) {
  940. err = GetTotalChannels(fDeviceID, out_nChannels, false);
  941. if (err != noErr) {
  942. jack_error("Cannot get output channel number");
  943. printError(err);
  944. return -1;
  945. } else {
  946. jack_log("Max output channels : %d", out_nChannels);
  947. }
  948. }
  949. if (inchannels > in_nChannels) {
  950. jack_error("This device hasn't required input channels inchannels = %d in_nChannels = %d", inchannels, in_nChannels);
  951. if (strict) {
  952. return -1;
  953. }
  954. }
  955. if (outchannels > out_nChannels) {
  956. jack_error("This device hasn't required output channels outchannels = %d out_nChannels = %d", outchannels, out_nChannels);
  957. if (strict) {
  958. return -1;
  959. }
  960. }
  961. if (inchannels == -1) {
  962. jack_log("Setup max in channels = %d", in_nChannels);
  963. inchannels = in_nChannels;
  964. }
  965. if (outchannels == -1) {
  966. jack_log("Setup max out channels = %d", out_nChannels);
  967. outchannels = out_nChannels;
  968. }
  969. return 0;
  970. }
  971. int JackCoreAudioDriver::SetupBufferSize(jack_nframes_t buffer_size)
  972. {
  973. // Setting buffer size
  974. OSStatus err = noErr;
  975. UInt32 tmp_buffer_size = buffer_size;
  976. UInt32 outSize = sizeof(UInt32);
  977. err = AudioDeviceGetProperty(fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyBufferFrameSize, &outSize, &tmp_buffer_size);
  978. if (err != noErr) {
  979. jack_error("Cannot get buffer size %ld", buffer_size);
  980. printError(err);
  981. return -1;
  982. } else {
  983. jack_log("Current buffer size = %ld", tmp_buffer_size);
  984. }
  985. // If needed, set new buffer size
  986. if (buffer_size != tmp_buffer_size) {
  987. tmp_buffer_size = buffer_size;
  988. // To get BS change notification
  989. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyBufferFrameSize, BSNotificationCallback, this);
  990. if (err != noErr) {
  991. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyBufferFrameSize");
  992. printError(err);
  993. return -1;
  994. }
  995. // Waiting for BS change notification
  996. int count = 0;
  997. fState = false;
  998. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyBufferFrameSize, outSize, &tmp_buffer_size);
  999. if (err != noErr) {
  1000. jack_error("Cannot set buffer size = %ld", tmp_buffer_size);
  1001. printError(err);
  1002. goto error;
  1003. }
  1004. while (!fState && count++ < WAIT_NOTIFICATION_COUNTER) {
  1005. usleep(100000);
  1006. jack_log("Wait count = %d", count);
  1007. }
  1008. if (count >= WAIT_NOTIFICATION_COUNTER) {
  1009. jack_error("Did not get buffer size notification...");
  1010. goto error;
  1011. }
  1012. // Check new buffer size
  1013. outSize = sizeof(UInt32);
  1014. err = AudioDeviceGetProperty(fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyBufferFrameSize, &outSize, &tmp_buffer_size);
  1015. if (err != noErr) {
  1016. jack_error("Cannot get current buffer size");
  1017. printError(err);
  1018. } else {
  1019. jack_log("Checked buffer size = %ld", tmp_buffer_size);
  1020. }
  1021. // Remove BS change notification
  1022. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyBufferFrameSize, BSNotificationCallback);
  1023. }
  1024. return 0;
  1025. error:
  1026. // Remove SR change notification
  1027. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyBufferFrameSize, BSNotificationCallback);
  1028. return -1;
  1029. }
  1030. int JackCoreAudioDriver::SetupSampleRate(jack_nframes_t sample_rate)
  1031. {
  1032. return SetupSampleRateAux(fDeviceID, sample_rate);
  1033. }
  1034. int JackCoreAudioDriver::SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t sample_rate)
  1035. {
  1036. OSStatus err = noErr;
  1037. UInt32 outSize;
  1038. Float64 tmp_sample_rate;
  1039. // Get sample rate
  1040. outSize = sizeof(Float64);
  1041. err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &tmp_sample_rate);
  1042. if (err != noErr) {
  1043. jack_error("Cannot get current sample rate");
  1044. printError(err);
  1045. return -1;
  1046. } else {
  1047. jack_log("Current sample rate = %f", tmp_sample_rate);
  1048. }
  1049. // If needed, set new sample rate
  1050. if (sample_rate != (jack_nframes_t)tmp_sample_rate) {
  1051. tmp_sample_rate = (Float64)sample_rate;
  1052. // To get SR change notification
  1053. err = AudioDeviceAddPropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
  1054. if (err != noErr) {
  1055. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  1056. printError(err);
  1057. return -1;
  1058. }
  1059. // Waiting for SR change notification
  1060. int count = 0;
  1061. fState = false;
  1062. err = AudioDeviceSetProperty(inDevice, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &tmp_sample_rate);
  1063. if (err != noErr) {
  1064. jack_error("Cannot set sample rate = %ld", sample_rate);
  1065. printError(err);
  1066. goto error;
  1067. }
  1068. while (!fState && count++ < WAIT_NOTIFICATION_COUNTER) {
  1069. usleep(100000);
  1070. jack_log("Wait count = %d", count);
  1071. }
  1072. if (count >= WAIT_NOTIFICATION_COUNTER) {
  1073. jack_error("Did not get sample rate notification...");
  1074. goto error;
  1075. }
  1076. // Check new sample rate
  1077. outSize = sizeof(Float64);
  1078. err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &tmp_sample_rate);
  1079. if (err != noErr) {
  1080. jack_error("Cannot get current sample rate");
  1081. printError(err);
  1082. } else {
  1083. jack_log("Checked sample rate = %f", tmp_sample_rate);
  1084. }
  1085. // Remove SR change notification
  1086. AudioDeviceRemovePropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
  1087. }
  1088. return 0;
  1089. error:
  1090. // Remove SR change notification
  1091. AudioDeviceRemovePropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
  1092. return -1;
  1093. }
  1094. int JackCoreAudioDriver::OpenAUHAL(bool capturing,
  1095. bool playing,
  1096. int inchannels,
  1097. int outchannels,
  1098. int in_nChannels,
  1099. int out_nChannels,
  1100. const vector<int>& chan_in_list,
  1101. const vector<int>& chan_out_list,
  1102. jack_nframes_t buffer_size,
  1103. jack_nframes_t sample_rate)
  1104. {
  1105. ComponentResult err1;
  1106. UInt32 enableIO;
  1107. AudioStreamBasicDescription srcFormat, dstFormat;
  1108. AudioDeviceID currAudioDeviceID;
  1109. UInt32 size;
  1110. jack_log("OpenAUHAL capturing = %d playing = %d inchannels = %d outchannels = %d in_nChannels = %d out_nChannels = %d chan_in_list = %d chan_out_list = %d",
  1111. capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, chan_in_list.size(), chan_out_list.size());
  1112. if (inchannels == 0 && outchannels == 0) {
  1113. jack_error("No input and output channels...");
  1114. return -1;
  1115. }
  1116. // AUHAL
  1117. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  1118. Component HALOutput = FindNextComponent(NULL, &cd);
  1119. err1 = OpenAComponent(HALOutput, &fAUHAL);
  1120. if (err1 != noErr) {
  1121. jack_error("Error calling OpenAComponent");
  1122. printError(err1);
  1123. goto error;
  1124. }
  1125. err1 = AudioUnitInitialize(fAUHAL);
  1126. if (err1 != noErr) {
  1127. jack_error("Cannot initialize AUHAL unit");
  1128. printError(err1);
  1129. goto error;
  1130. }
  1131. // Start I/O
  1132. if (capturing && inchannels > 0) {
  1133. enableIO = 1;
  1134. jack_log("Setup AUHAL input on");
  1135. } else {
  1136. enableIO = 0;
  1137. jack_log("Setup AUHAL input off");
  1138. }
  1139. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  1140. if (err1 != noErr) {
  1141. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  1142. printError(err1);
  1143. goto error;
  1144. }
  1145. if (playing && outchannels > 0) {
  1146. enableIO = 1;
  1147. jack_log("Setup AUHAL output on");
  1148. } else {
  1149. enableIO = 0;
  1150. jack_log("Setup AUHAL output off");
  1151. }
  1152. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  1153. if (err1 != noErr) {
  1154. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  1155. printError(err1);
  1156. goto error;
  1157. }
  1158. size = sizeof(AudioDeviceID);
  1159. err1 = AudioUnitGetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &currAudioDeviceID, &size);
  1160. if (err1 != noErr) {
  1161. jack_error("Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
  1162. printError(err1);
  1163. goto error;
  1164. } else {
  1165. jack_log("AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID);
  1166. }
  1167. // Setup up choosen device, in both input and output cases
  1168. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  1169. if (err1 != noErr) {
  1170. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  1171. printError(err1);
  1172. goto error;
  1173. }
  1174. // Set buffer size
  1175. if (capturing && inchannels > 0) {
  1176. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&buffer_size, sizeof(UInt32));
  1177. if (err1 != noErr) {
  1178. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  1179. printError(err1);
  1180. goto error;
  1181. }
  1182. }
  1183. if (playing && outchannels > 0) {
  1184. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&buffer_size, sizeof(UInt32));
  1185. if (err1 != noErr) {
  1186. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  1187. printError(err1);
  1188. goto error;
  1189. }
  1190. }
  1191. // Setup input channel map
  1192. if (capturing && inchannels > 0 && inchannels <= in_nChannels) {
  1193. SInt32 chanArr[in_nChannels];
  1194. for (int i = 0; i < in_nChannels; i++) {
  1195. chanArr[i] = -1;
  1196. }
  1197. // Explicit mapping
  1198. if (chan_in_list.size() > 0) {
  1199. for (uint i = 0; i < chan_in_list.size(); i++) {
  1200. int chan = chan_in_list[i];
  1201. if (chan < out_nChannels) {
  1202. // The wanted JACK input index for the 'chan' channel value
  1203. chanArr[chan] = i;
  1204. jack_info("Input channel = %d ==> JACK input port = %d", chan, i);
  1205. } else {
  1206. jack_info("Error input channel number is incorrect : %d", chan);
  1207. goto error;
  1208. }
  1209. }
  1210. } else {
  1211. for (int i = 0; i < inchannels; i++) {
  1212. chanArr[i] = i;
  1213. jack_info("Input channel = %d ==> JACK input port = %d", chanArr[i], i);
  1214. }
  1215. }
  1216. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  1217. if (err1 != noErr) {
  1218. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap for input");
  1219. printError(err1);
  1220. goto error;
  1221. }
  1222. }
  1223. // Setup output channel map
  1224. if (playing && outchannels > 0 && outchannels <= out_nChannels) {
  1225. SInt32 chanArr[out_nChannels];
  1226. for (int i = 0; i < out_nChannels; i++) {
  1227. chanArr[i] = -1;
  1228. }
  1229. // Explicit mapping
  1230. if (chan_out_list.size() > 0) {
  1231. for (uint i = 0; i < chan_out_list.size(); i++) {
  1232. int chan = chan_out_list[i];
  1233. if (chan < out_nChannels) {
  1234. // The wanted JACK output index for the 'chan' channel value
  1235. chanArr[chan] = i;
  1236. jack_info("JACK output port = %d ==> output channel = %d", i, chan);
  1237. } else {
  1238. jack_info("Error output channel number is incorrect : %d", chan);
  1239. goto error;
  1240. }
  1241. }
  1242. } else {
  1243. for (int i = 0; i < outchannels; i++) {
  1244. chanArr[i] = i;
  1245. jack_info("JACK output port = %d ==> output channel = %d", i, chanArr[i]);
  1246. }
  1247. }
  1248. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  1249. if (err1 != noErr) {
  1250. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap for output");
  1251. printError(err1);
  1252. goto error;
  1253. }
  1254. }
  1255. // Setup stream converters
  1256. if (capturing && inchannels > 0) {
  1257. size = sizeof(AudioStreamBasicDescription);
  1258. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, &size);
  1259. if (err1 != noErr) {
  1260. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  1261. printError(err1);
  1262. goto error;
  1263. }
  1264. PrintStreamDesc(&srcFormat);
  1265. jack_log("Setup AUHAL input stream converter SR = %ld", sample_rate);
  1266. srcFormat.mSampleRate = sample_rate;
  1267. srcFormat.mFormatID = kAudioFormatLinearPCM;
  1268. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  1269. srcFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
  1270. srcFormat.mFramesPerPacket = 1;
  1271. srcFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
  1272. srcFormat.mChannelsPerFrame = inchannels;
  1273. srcFormat.mBitsPerChannel = 32;
  1274. PrintStreamDesc(&srcFormat);
  1275. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, sizeof(AudioStreamBasicDescription));
  1276. if (err1 != noErr) {
  1277. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  1278. printError(err1);
  1279. goto error;
  1280. }
  1281. }
  1282. if (playing && outchannels > 0) {
  1283. size = sizeof(AudioStreamBasicDescription);
  1284. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, &size);
  1285. if (err1 != noErr) {
  1286. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  1287. printError(err1);
  1288. goto error;
  1289. }
  1290. PrintStreamDesc(&dstFormat);
  1291. jack_log("Setup AUHAL output stream converter SR = %ld", sample_rate);
  1292. dstFormat.mSampleRate = sample_rate;
  1293. dstFormat.mFormatID = kAudioFormatLinearPCM;
  1294. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  1295. dstFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
  1296. dstFormat.mFramesPerPacket = 1;
  1297. dstFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
  1298. dstFormat.mChannelsPerFrame = outchannels;
  1299. dstFormat.mBitsPerChannel = 32;
  1300. PrintStreamDesc(&dstFormat);
  1301. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, sizeof(AudioStreamBasicDescription));
  1302. if (err1 != noErr) {
  1303. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  1304. printError(err1);
  1305. goto error;
  1306. }
  1307. }
  1308. // Setup callbacks
  1309. if (inchannels > 0 && outchannels == 0) {
  1310. AURenderCallbackStruct output;
  1311. output.inputProc = Render;
  1312. output.inputProcRefCon = this;
  1313. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  1314. if (err1 != noErr) {
  1315. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  1316. printError(err1);
  1317. goto error;
  1318. }
  1319. } else {
  1320. AURenderCallbackStruct output;
  1321. output.inputProc = Render;
  1322. output.inputProcRefCon = this;
  1323. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  1324. if (err1 != noErr) {
  1325. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  1326. printError(err1);
  1327. goto error;
  1328. }
  1329. }
  1330. return 0;
  1331. error:
  1332. CloseAUHAL();
  1333. return -1;
  1334. }
  1335. int JackCoreAudioDriver::SetupBuffers(int inchannels)
  1336. {
  1337. // Prepare buffers
  1338. fJackInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  1339. fJackInputData->mNumberBuffers = inchannels;
  1340. for (int i = 0; i < inchannels; i++) {
  1341. fJackInputData->mBuffers[i].mNumberChannels = 1;
  1342. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(jack_default_audio_sample_t);
  1343. }
  1344. return 0;
  1345. }
  1346. void JackCoreAudioDriver::DisposeBuffers()
  1347. {
  1348. if (fJackInputData) {
  1349. free(fJackInputData);
  1350. fJackInputData = 0;
  1351. }
  1352. }
  1353. void JackCoreAudioDriver::CloseAUHAL()
  1354. {
  1355. AudioUnitUninitialize(fAUHAL);
  1356. CloseComponent(fAUHAL);
  1357. }
  1358. int JackCoreAudioDriver::AddListeners()
  1359. {
  1360. OSStatus err = noErr;
  1361. // Add listeners
  1362. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
  1363. if (err != noErr) {
  1364. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  1365. printError(err);
  1366. return -1;
  1367. }
  1368. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback, this);
  1369. if (err != noErr) {
  1370. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
  1371. printError(err);
  1372. return -1;
  1373. }
  1374. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback, this);
  1375. if (err != noErr) {
  1376. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  1377. printError(err);
  1378. return -1;
  1379. }
  1380. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback, this);
  1381. if (err != noErr) {
  1382. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
  1383. printError(err);
  1384. return -1;
  1385. }
  1386. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  1387. if (err != noErr) {
  1388. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  1389. printError(err);
  1390. return -1;
  1391. }
  1392. err = AudioDeviceAddPropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  1393. if (err != noErr) {
  1394. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  1395. printError(err);
  1396. return -1;
  1397. }
  1398. if (!fEngineControl->fSyncMode && fIOUsage != 1.f) {
  1399. UInt32 outSize = sizeof(float);
  1400. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyIOCycleUsage, outSize, &fIOUsage);
  1401. if (err != noErr) {
  1402. jack_error("Error calling AudioDeviceSetProperty kAudioDevicePropertyIOCycleUsage");
  1403. printError(err);
  1404. }
  1405. }
  1406. return 0;
  1407. }
  1408. void JackCoreAudioDriver::RemoveListeners()
  1409. {
  1410. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
  1411. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback);
  1412. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback);
  1413. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback);
  1414. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  1415. AudioDeviceRemovePropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  1416. }
  1417. int JackCoreAudioDriver::Open(jack_nframes_t buffer_size,
  1418. jack_nframes_t sample_rate,
  1419. bool capturing,
  1420. bool playing,
  1421. int inchannels,
  1422. int outchannels,
  1423. const char* chan_in_list,
  1424. const char* chan_out_list,
  1425. bool monitor,
  1426. const char* capture_driver_uid,
  1427. const char* playback_driver_uid,
  1428. jack_nframes_t capture_latency,
  1429. jack_nframes_t playback_latency,
  1430. int async_output_latency,
  1431. int computation_grain,
  1432. bool hogged,
  1433. bool clock_drift)
  1434. {
  1435. int in_nChannels = 0;
  1436. int out_nChannels = 0;
  1437. char capture_driver_name[256];
  1438. char playback_driver_name[256];
  1439. // Keep initial state
  1440. strcpy(fCaptureUID, capture_driver_uid);
  1441. strcpy(fPlaybackUID, playback_driver_uid);
  1442. fCaptureLatency = capture_latency;
  1443. fPlaybackLatency = playback_latency;
  1444. fIOUsage = float(async_output_latency) / 100.f;
  1445. fComputationGrain = float(computation_grain) / 100.f;
  1446. fHogged = hogged;
  1447. fClockDriftCompensate = clock_drift;
  1448. SInt32 major;
  1449. SInt32 minor;
  1450. Gestalt(gestaltSystemVersionMajor, &major);
  1451. Gestalt(gestaltSystemVersionMinor, &minor);
  1452. vector<int> parsed_chan_in_list;
  1453. vector<int> parsed_chan_out_list;
  1454. ParseChannelList(chan_in_list, parsed_chan_in_list);
  1455. if (parsed_chan_in_list.size() > 0) {
  1456. jack_info("Explicit input channel list size = %d", parsed_chan_in_list.size());
  1457. inchannels = parsed_chan_in_list.size();
  1458. }
  1459. ParseChannelList(chan_out_list, parsed_chan_out_list);
  1460. if (parsed_chan_out_list.size() > 0) {
  1461. jack_info("Explicit output channel list size = %d", parsed_chan_out_list.size());
  1462. outchannels = parsed_chan_out_list.size();
  1463. }
  1464. // Starting with 10.6 systems, the HAL notification thread is created internally
  1465. if (major == 10 && minor >= 6) {
  1466. CFRunLoopRef theRunLoop = NULL;
  1467. AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  1468. OSStatus osErr = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
  1469. if (osErr != noErr) {
  1470. jack_error("JackCoreAudioDriver::Open kAudioHardwarePropertyRunLoop error");
  1471. printError(osErr);
  1472. }
  1473. }
  1474. if (SetupDevices(capture_driver_uid, playback_driver_uid, capture_driver_name, playback_driver_name, sample_rate) < 0) {
  1475. goto error;
  1476. }
  1477. // Generic JackAudioDriver Open
  1478. if (JackAudioDriver::Open(buffer_size, sample_rate,
  1479. capturing, playing,
  1480. inchannels, outchannels,
  1481. monitor,
  1482. capture_driver_name,
  1483. playback_driver_name,
  1484. capture_latency,
  1485. playback_latency) != 0) {
  1486. goto error;
  1487. }
  1488. if (SetupChannels(capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, true) < 0) {
  1489. goto error;
  1490. }
  1491. if (SetupBufferSize(buffer_size) < 0) {
  1492. goto error;
  1493. }
  1494. if (SetupSampleRate(sample_rate) < 0) {
  1495. goto error;
  1496. }
  1497. if (OpenAUHAL(capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, parsed_chan_in_list, parsed_chan_out_list, buffer_size, sample_rate) < 0) {
  1498. goto error;
  1499. }
  1500. if (capturing && inchannels > 0) {
  1501. if (SetupBuffers(inchannels) < 0) {
  1502. goto error;
  1503. }
  1504. }
  1505. if (AddListeners() < 0) {
  1506. goto error;
  1507. }
  1508. // Core driver may have changed the in/out values
  1509. fCaptureChannels = inchannels;
  1510. fPlaybackChannels = outchannels;
  1511. return noErr;
  1512. error:
  1513. Close();
  1514. return -1;
  1515. }
  1516. int JackCoreAudioDriver::Close()
  1517. {
  1518. jack_log("JackCoreAudioDriver::Close");
  1519. // Generic audio driver close
  1520. int res = JackAudioDriver::Close();
  1521. RemoveListeners();
  1522. DisposeBuffers();
  1523. CloseAUHAL();
  1524. DestroyAggregateDevice();
  1525. return res;
  1526. }
  1527. void JackCoreAudioDriver::UpdateLatencies()
  1528. {
  1529. UInt32 size;
  1530. OSStatus err;
  1531. jack_latency_range_t range;
  1532. range.max = fEngineControl->fBufferSize;
  1533. range.min = fEngineControl->fBufferSize;
  1534. for (int i = 0; i < fCaptureChannels; i++) {
  1535. size = sizeof(UInt32);
  1536. UInt32 value1 = 0;
  1537. UInt32 value2 = 0;
  1538. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyLatency, &size, &value1);
  1539. if (err != noErr) {
  1540. jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error");
  1541. }
  1542. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);
  1543. if (err != noErr) {
  1544. jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error");
  1545. }
  1546. range.min = range.max = fEngineControl->fBufferSize + value1 + value2 + fCaptureLatency;
  1547. fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &range);
  1548. }
  1549. for (int i = 0; i < fPlaybackChannels; i++) {
  1550. size = sizeof(UInt32);
  1551. UInt32 value1 = 0;
  1552. UInt32 value2 = 0;
  1553. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyLatency, &size, &value1);
  1554. if (err != noErr) {
  1555. jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error");
  1556. }
  1557. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);
  1558. if (err != noErr) {
  1559. jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error");
  1560. }
  1561. // Add more latency if "async" mode is used...
  1562. range.min = range.max
  1563. = fEngineControl->fBufferSize + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize * fIOUsage) + value1 + value2 + fPlaybackLatency;
  1564. fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &range);
  1565. // Monitor port
  1566. if (fWithMonitorPorts) {
  1567. range.min = range.max = fEngineControl->fBufferSize;
  1568. fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &range);
  1569. }
  1570. }
  1571. }
  1572. int JackCoreAudioDriver::Attach()
  1573. {
  1574. OSStatus err;
  1575. JackPort* port;
  1576. jack_port_id_t port_index;
  1577. UInt32 size;
  1578. Boolean isWritable;
  1579. char channel_name[64];
  1580. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1581. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1582. jack_log("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  1583. for (int i = 0; i < fCaptureChannels; i++) {
  1584. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
  1585. if (err != noErr) {
  1586. jack_log("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error");
  1587. }
  1588. if (err == noErr && size > 0) {
  1589. err = AudioDeviceGetProperty(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
  1590. if (err != noErr) {
  1591. jack_log("AudioDeviceGetProperty kAudioDevicePropertyChannelName error");
  1592. }
  1593. snprintf(alias, sizeof(alias) - 1, "%s:%s:out_%s%u", fAliasName, fCaptureDriverName, channel_name, i + 1);
  1594. } else {
  1595. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%u", fAliasName, fCaptureDriverName, i + 1);
  1596. }
  1597. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
  1598. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  1599. jack_error("Cannot register port for %s", name);
  1600. return -1;
  1601. }
  1602. port = fGraphManager->GetPort(port_index);
  1603. port->SetAlias(alias);
  1604. fCapturePortList[i] = port_index;
  1605. }
  1606. for (int i = 0; i < fPlaybackChannels; i++) {
  1607. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
  1608. if (err != noErr) {
  1609. jack_log("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error");
  1610. }
  1611. if (err == noErr && size > 0) {
  1612. err = AudioDeviceGetProperty(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
  1613. if (err != noErr) {
  1614. jack_log("AudioDeviceGetProperty kAudioDevicePropertyChannelName error");
  1615. }
  1616. snprintf(alias, sizeof(alias) - 1, "%s:%s:in_%s%u", fAliasName, fPlaybackDriverName, channel_name, i + 1);
  1617. } else {
  1618. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%u", fAliasName, fPlaybackDriverName, i + 1);
  1619. }
  1620. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
  1621. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  1622. jack_error("Cannot register port for %s", name);
  1623. return -1;
  1624. }
  1625. port = fGraphManager->GetPort(port_index);
  1626. port->SetAlias(alias);
  1627. fPlaybackPortList[i] = port_index;
  1628. // Monitor ports
  1629. if (fWithMonitorPorts) {
  1630. jack_log("Create monitor port");
  1631. snprintf(name, sizeof(name) - 1, "%s:monitor_%u", fClientControl.fName, i + 1);
  1632. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, MonitorDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  1633. jack_error("Cannot register monitor port for %s", name);
  1634. return -1;
  1635. } else {
  1636. fMonitorPortList[i] = port_index;
  1637. }
  1638. }
  1639. }
  1640. UpdateLatencies();
  1641. // Input buffers do no change : prepare them only once
  1642. for (int i = 0; i < fCaptureChannels; i++) {
  1643. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  1644. }
  1645. return 0;
  1646. }
  1647. int JackCoreAudioDriver::Start()
  1648. {
  1649. jack_log("JackCoreAudioDriver::Start");
  1650. if (JackAudioDriver::Start() == 0) {
  1651. // Waiting for Render callback to be called (= driver has started)
  1652. fState = false;
  1653. int count = 0;
  1654. OSStatus err = AudioOutputUnitStart(fAUHAL);
  1655. if (err == noErr) {
  1656. while (!fState && count++ < WAIT_COUNTER) {
  1657. usleep(100000);
  1658. jack_log("JackCoreAudioDriver::Start wait count = %d", count);
  1659. }
  1660. if (count < WAIT_COUNTER) {
  1661. jack_info("CoreAudio driver is running...");
  1662. return 0;
  1663. }
  1664. jack_error("CoreAudio driver cannot start...");
  1665. }
  1666. JackAudioDriver::Stop();
  1667. }
  1668. return -1;
  1669. }
  1670. int JackCoreAudioDriver::Stop()
  1671. {
  1672. jack_log("JackCoreAudioDriver::Stop");
  1673. int res = (AudioOutputUnitStop(fAUHAL) == noErr) ? 0 : -1;
  1674. if (JackAudioDriver::Stop() < 0) {
  1675. res = -1;
  1676. }
  1677. return res;
  1678. }
  1679. int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  1680. {
  1681. if (SetupBufferSize(buffer_size) < 0) {
  1682. return -1;
  1683. }
  1684. JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
  1685. // CoreAudio specific
  1686. UpdateLatencies();
  1687. // Input buffers do no change : prepare them only once
  1688. for (int i = 0; i < fCaptureChannels; i++) {
  1689. fJackInputData->mBuffers[i].mNumberChannels = 1;
  1690. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(jack_default_audio_sample_t);
  1691. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  1692. }
  1693. return 0;
  1694. }
  1695. bool JackCoreAudioDriver::TakeHogAux(AudioDeviceID deviceID, bool isInput)
  1696. {
  1697. pid_t hog_pid;
  1698. OSStatus err;
  1699. UInt32 propSize = sizeof(hog_pid);
  1700. err = AudioDeviceGetProperty(deviceID, 0, isInput, kAudioDevicePropertyHogMode, &propSize, &hog_pid);
  1701. if (err) {
  1702. jack_error("Cannot read hog state...");
  1703. printError(err);
  1704. }
  1705. if (hog_pid != getpid()) {
  1706. hog_pid = getpid();
  1707. err = AudioDeviceSetProperty(deviceID, 0, 0, isInput, kAudioDevicePropertyHogMode, propSize, &hog_pid);
  1708. if (err != noErr) {
  1709. jack_error("Can't hog device = %d because it's being hogged by another program or cannot be hogged", deviceID);
  1710. return false;
  1711. }
  1712. }
  1713. return true;
  1714. }
  1715. bool JackCoreAudioDriver::TakeHog()
  1716. {
  1717. OSStatus err = noErr;
  1718. AudioObjectID sub_device[32];
  1719. UInt32 outSize = sizeof(sub_device);
  1720. err = AudioDeviceGetProperty(fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  1721. if (err != noErr) {
  1722. jack_log("Device does not have subdevices");
  1723. return TakeHogAux(fDeviceID, true);
  1724. } else {
  1725. int num_devices = outSize / sizeof(AudioObjectID);
  1726. jack_log("Device does has %d subdevices", num_devices);
  1727. for (int i = 0; i < num_devices; i++) {
  1728. if (!TakeHogAux(sub_device[i], true)) {
  1729. return false;
  1730. }
  1731. }
  1732. return true;
  1733. }
  1734. }
  1735. bool JackCoreAudioDriver::IsAggregateDevice(AudioDeviceID device)
  1736. {
  1737. UInt32 deviceType, outSize = sizeof(UInt32);
  1738. OSStatus err = AudioDeviceGetProperty(device, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyTransportType, &outSize, &deviceType);
  1739. if (err != noErr) {
  1740. jack_log("JackCoreAudioDriver::IsAggregateDevice kAudioDevicePropertyTransportType error");
  1741. return false;
  1742. } else {
  1743. return (deviceType == kAudioDeviceTransportTypeAggregate);
  1744. }
  1745. }
  1746. } // end of namespace
  1747. #ifdef __cplusplus
  1748. extern "C"
  1749. {
  1750. #endif
  1751. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  1752. {
  1753. jack_driver_desc_t * desc;
  1754. jack_driver_desc_filler_t filler;
  1755. jack_driver_param_value_t value;
  1756. desc = jack_driver_descriptor_construct("coreaudio", JackDriverMaster, "Apple CoreAudio API based audio backend", &filler);
  1757. value.i = -1;
  1758. 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");
  1759. 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");
  1760. 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");
  1761. value.str[0] = 0;
  1762. jack_driver_descriptor_add_parameter(desc, &filler, "input-list", 'n', JackDriverParamString, &value, NULL, "Input channel list", "List of input channel number to be opened");
  1763. jack_driver_descriptor_add_parameter(desc, &filler, "output-list", 'N', JackDriverParamString, &value, NULL, "Output channel list", "List of output channel number to be opened");
  1764. value.str[0] = 0;
  1765. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Input CoreAudio device name", NULL);
  1766. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Output CoreAudio device name", NULL);
  1767. value.i = 0;
  1768. jack_driver_descriptor_add_parameter(desc, &filler, "monitor", 'm', JackDriverParamBool, &value, NULL, "Provide monitor ports for the output", NULL);
  1769. value.i = TRUE;
  1770. jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
  1771. value.ui = 44100U;
  1772. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  1773. value.ui = 256U;
  1774. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  1775. value.str[0] = 0;
  1776. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "CoreAudio device name", NULL);
  1777. value.ui = 0;
  1778. jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency (frames)", NULL);
  1779. jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency (frames)", NULL);
  1780. value.i = FALSE;
  1781. jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available CoreAudio devices", NULL);
  1782. value.i = FALSE;
  1783. jack_driver_descriptor_add_parameter(desc, &filler, "hog", 'H', JackDriverParamBool, &value, NULL, "Take exclusive access of the audio device", NULL);
  1784. value.ui = 100;
  1785. jack_driver_descriptor_add_parameter(desc, &filler, "async-latency", 'L', JackDriverParamUInt, &value, NULL, "Extra output latency in asynchronous mode (percent)", NULL);
  1786. value.ui = 100;
  1787. jack_driver_descriptor_add_parameter(desc, &filler, "grain", 'G', JackDriverParamUInt, &value, NULL, "Computation grain in RT thread (percent)", NULL);
  1788. value.i = FALSE;
  1789. 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");
  1790. return desc;
  1791. }
  1792. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  1793. {
  1794. jack_nframes_t srate = 44100;
  1795. jack_nframes_t frames_per_interrupt = 256;
  1796. bool capture = false;
  1797. bool playback = false;
  1798. int chan_in = -1; // Default: if not explicitely set, then max possible will be used...
  1799. int chan_out = -1; // Default: if not explicitely set, then max possible will be used...
  1800. const char* chan_in_list = "";
  1801. const char* chan_out_list = "";
  1802. bool monitor = false;
  1803. const char* capture_driver_uid = "";
  1804. const char* playback_driver_uid = "";
  1805. const JSList *node;
  1806. const jack_driver_param_t *param;
  1807. jack_nframes_t systemic_input_latency = 0;
  1808. jack_nframes_t systemic_output_latency = 0;
  1809. int async_output_latency = 100;
  1810. int computation_grain = -1;
  1811. bool hogged = false;
  1812. bool clock_drift = false;
  1813. for (node = params; node; node = jack_slist_next(node)) {
  1814. param = (const jack_driver_param_t *) node->data;
  1815. switch (param->character) {
  1816. case 'd':
  1817. capture_driver_uid = param->value.str;
  1818. playback_driver_uid = param->value.str;
  1819. break;
  1820. case 'D':
  1821. capture = true;
  1822. playback = true;
  1823. break;
  1824. case 'c':
  1825. chan_in = chan_out = param->value.i;
  1826. break;
  1827. case 'i':
  1828. chan_in = param->value.i;
  1829. break;
  1830. case 'o':
  1831. chan_out = param->value.i;
  1832. break;
  1833. case 'n':
  1834. chan_in_list = param->value.str;
  1835. break;
  1836. case 'N':
  1837. chan_out_list = param->value.str;
  1838. break;
  1839. case 'C':
  1840. capture = true;
  1841. if (strcmp(param->value.str, "none") != 0) {
  1842. capture_driver_uid = param->value.str;
  1843. }
  1844. break;
  1845. case 'P':
  1846. playback = true;
  1847. if (strcmp(param->value.str, "none") != 0) {
  1848. playback_driver_uid = param->value.str;
  1849. }
  1850. break;
  1851. case 'm':
  1852. monitor = param->value.i;
  1853. break;
  1854. case 'r':
  1855. srate = param->value.ui;
  1856. break;
  1857. case 'p':
  1858. frames_per_interrupt = (unsigned int)param->value.ui;
  1859. break;
  1860. case 'I':
  1861. systemic_input_latency = param->value.ui;
  1862. break;
  1863. case 'O':
  1864. systemic_output_latency = param->value.ui;
  1865. break;
  1866. case 'l':
  1867. Jack::DisplayDeviceNames();
  1868. return NULL;
  1869. case 'H':
  1870. hogged = true;
  1871. break;
  1872. case 'L':
  1873. async_output_latency = param->value.ui;
  1874. break;
  1875. case 'G':
  1876. computation_grain = param->value.ui;
  1877. break;
  1878. case 's':
  1879. clock_drift = true;
  1880. break;
  1881. }
  1882. }
  1883. /* duplex is the default */
  1884. if (!capture && !playback) {
  1885. capture = true;
  1886. playback = true;
  1887. }
  1888. if (strcmp(chan_in_list, "") != 0 && chan_in >= 0) {
  1889. printf("Input channel list and in channels are both specified, input channel list will take over...\n");
  1890. }
  1891. if (strcmp(chan_out_list, "") != 0 && chan_out >= 0) {
  1892. printf("Output channel list and out channels are both specified, output channel list will take over...\n");
  1893. }
  1894. Jack::JackCoreAudioDriver* driver = new Jack::JackCoreAudioDriver("system", "coreaudio", engine, table);
  1895. if (driver->Open(frames_per_interrupt,
  1896. srate, capture,
  1897. playback, chan_in,
  1898. chan_out, chan_in_list,
  1899. chan_out_list, monitor,
  1900. capture_driver_uid,
  1901. playback_driver_uid,
  1902. systemic_input_latency,
  1903. systemic_output_latency,
  1904. async_output_latency,
  1905. computation_grain,
  1906. hogged, clock_drift) == 0) {
  1907. return driver;
  1908. } else {
  1909. delete driver;
  1910. return NULL;
  1911. }
  1912. }
  1913. #ifdef __cplusplus
  1914. }
  1915. #endif