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.

2278 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. void JackCoreAudioDriver::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. } else {
  1205. jack_info("Error input channel number is incorrect : %d", chan);
  1206. goto error;
  1207. }
  1208. }
  1209. } else {
  1210. for (int i = 0; i < inchannels; i++) {
  1211. chanArr[i] = i;
  1212. }
  1213. }
  1214. int jack_input = 0;
  1215. for (int i = 0; i < in_nChannels; i++) {
  1216. if (chanArr[i] >= 0) {
  1217. jack_info("Input channel = %d ==> JACK input port = %d", i, jack_input);
  1218. jack_input++;
  1219. }
  1220. }
  1221. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  1222. if (err1 != noErr) {
  1223. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap for input");
  1224. printError(err1);
  1225. goto error;
  1226. }
  1227. }
  1228. // Setup output channel map
  1229. if (playing && outchannels > 0 && outchannels <= out_nChannels) {
  1230. SInt32 chanArr[out_nChannels];
  1231. for (int i = 0; i < out_nChannels; i++) {
  1232. chanArr[i] = -1;
  1233. }
  1234. // Explicit mapping
  1235. if (chan_out_list.size() > 0) {
  1236. for (uint i = 0; i < chan_out_list.size(); i++) {
  1237. int chan = chan_out_list[i];
  1238. if (chan < out_nChannels) {
  1239. // The wanted JACK output index for the 'chan' channel value
  1240. chanArr[chan] = i;
  1241. } else {
  1242. jack_info("Error output channel number is incorrect : %d", chan);
  1243. goto error;
  1244. }
  1245. }
  1246. } else {
  1247. for (int i = 0; i < outchannels; i++) {
  1248. chanArr[i] = i;
  1249. }
  1250. }
  1251. int jack_output = 0;
  1252. for (int i = 0; i < out_nChannels; i++) {
  1253. if (chanArr[i] >= 0) {
  1254. jack_info("JACK output port = %d ==> output channel = %d", jack_output, i);
  1255. jack_output++;
  1256. }
  1257. }
  1258. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  1259. if (err1 != noErr) {
  1260. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap for output");
  1261. printError(err1);
  1262. goto error;
  1263. }
  1264. }
  1265. // Setup stream converters
  1266. if (capturing && inchannels > 0) {
  1267. size = sizeof(AudioStreamBasicDescription);
  1268. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, &size);
  1269. if (err1 != noErr) {
  1270. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  1271. printError(err1);
  1272. goto error;
  1273. }
  1274. PrintStreamDesc(&srcFormat);
  1275. jack_log("Setup AUHAL input stream converter SR = %ld", sample_rate);
  1276. srcFormat.mSampleRate = sample_rate;
  1277. srcFormat.mFormatID = kAudioFormatLinearPCM;
  1278. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  1279. srcFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
  1280. srcFormat.mFramesPerPacket = 1;
  1281. srcFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
  1282. srcFormat.mChannelsPerFrame = inchannels;
  1283. srcFormat.mBitsPerChannel = 32;
  1284. PrintStreamDesc(&srcFormat);
  1285. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, sizeof(AudioStreamBasicDescription));
  1286. if (err1 != noErr) {
  1287. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  1288. printError(err1);
  1289. goto error;
  1290. }
  1291. }
  1292. if (playing && outchannels > 0) {
  1293. size = sizeof(AudioStreamBasicDescription);
  1294. err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, &size);
  1295. if (err1 != noErr) {
  1296. jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  1297. printError(err1);
  1298. goto error;
  1299. }
  1300. PrintStreamDesc(&dstFormat);
  1301. jack_log("Setup AUHAL output stream converter SR = %ld", sample_rate);
  1302. dstFormat.mSampleRate = sample_rate;
  1303. dstFormat.mFormatID = kAudioFormatLinearPCM;
  1304. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  1305. dstFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
  1306. dstFormat.mFramesPerPacket = 1;
  1307. dstFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
  1308. dstFormat.mChannelsPerFrame = outchannels;
  1309. dstFormat.mBitsPerChannel = 32;
  1310. PrintStreamDesc(&dstFormat);
  1311. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, sizeof(AudioStreamBasicDescription));
  1312. if (err1 != noErr) {
  1313. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  1314. printError(err1);
  1315. goto error;
  1316. }
  1317. }
  1318. // Setup callbacks
  1319. if (inchannels > 0 && outchannels == 0) {
  1320. AURenderCallbackStruct output;
  1321. output.inputProc = Render;
  1322. output.inputProcRefCon = this;
  1323. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  1324. if (err1 != noErr) {
  1325. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  1326. printError(err1);
  1327. goto error;
  1328. }
  1329. } else {
  1330. AURenderCallbackStruct output;
  1331. output.inputProc = Render;
  1332. output.inputProcRefCon = this;
  1333. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  1334. if (err1 != noErr) {
  1335. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  1336. printError(err1);
  1337. goto error;
  1338. }
  1339. }
  1340. return 0;
  1341. error:
  1342. CloseAUHAL();
  1343. return -1;
  1344. }
  1345. int JackCoreAudioDriver::SetupBuffers(int inchannels)
  1346. {
  1347. // Prepare buffers
  1348. fJackInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  1349. fJackInputData->mNumberBuffers = inchannels;
  1350. for (int i = 0; i < inchannels; i++) {
  1351. fJackInputData->mBuffers[i].mNumberChannels = 1;
  1352. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(jack_default_audio_sample_t);
  1353. }
  1354. return 0;
  1355. }
  1356. void JackCoreAudioDriver::DisposeBuffers()
  1357. {
  1358. if (fJackInputData) {
  1359. free(fJackInputData);
  1360. fJackInputData = 0;
  1361. }
  1362. }
  1363. void JackCoreAudioDriver::CloseAUHAL()
  1364. {
  1365. AudioUnitUninitialize(fAUHAL);
  1366. CloseComponent(fAUHAL);
  1367. }
  1368. int JackCoreAudioDriver::AddListeners()
  1369. {
  1370. OSStatus err = noErr;
  1371. // Add listeners
  1372. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
  1373. if (err != noErr) {
  1374. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  1375. printError(err);
  1376. return -1;
  1377. }
  1378. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback, this);
  1379. if (err != noErr) {
  1380. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
  1381. printError(err);
  1382. return -1;
  1383. }
  1384. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback, this);
  1385. if (err != noErr) {
  1386. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  1387. printError(err);
  1388. return -1;
  1389. }
  1390. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback, this);
  1391. if (err != noErr) {
  1392. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
  1393. printError(err);
  1394. return -1;
  1395. }
  1396. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  1397. if (err != noErr) {
  1398. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  1399. printError(err);
  1400. return -1;
  1401. }
  1402. err = AudioDeviceAddPropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  1403. if (err != noErr) {
  1404. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  1405. printError(err);
  1406. return -1;
  1407. }
  1408. if (!fEngineControl->fSyncMode && fIOUsage != 1.f) {
  1409. UInt32 outSize = sizeof(float);
  1410. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyIOCycleUsage, outSize, &fIOUsage);
  1411. if (err != noErr) {
  1412. jack_error("Error calling AudioDeviceSetProperty kAudioDevicePropertyIOCycleUsage");
  1413. printError(err);
  1414. }
  1415. }
  1416. return 0;
  1417. }
  1418. void JackCoreAudioDriver::RemoveListeners()
  1419. {
  1420. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
  1421. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback);
  1422. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback);
  1423. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback);
  1424. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  1425. AudioDeviceRemovePropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  1426. }
  1427. int JackCoreAudioDriver::Open(jack_nframes_t buffer_size,
  1428. jack_nframes_t sample_rate,
  1429. bool capturing,
  1430. bool playing,
  1431. int inchannels,
  1432. int outchannels,
  1433. const char* chan_in_list,
  1434. const char* chan_out_list,
  1435. bool monitor,
  1436. const char* capture_driver_uid,
  1437. const char* playback_driver_uid,
  1438. jack_nframes_t capture_latency,
  1439. jack_nframes_t playback_latency,
  1440. int async_output_latency,
  1441. int computation_grain,
  1442. bool hogged,
  1443. bool clock_drift)
  1444. {
  1445. int in_nChannels = 0;
  1446. int out_nChannels = 0;
  1447. char capture_driver_name[256];
  1448. char playback_driver_name[256];
  1449. // Keep initial state
  1450. strcpy(fCaptureUID, capture_driver_uid);
  1451. strcpy(fPlaybackUID, playback_driver_uid);
  1452. fCaptureLatency = capture_latency;
  1453. fPlaybackLatency = playback_latency;
  1454. fIOUsage = float(async_output_latency) / 100.f;
  1455. fComputationGrain = float(computation_grain) / 100.f;
  1456. fHogged = hogged;
  1457. fClockDriftCompensate = clock_drift;
  1458. SInt32 major;
  1459. SInt32 minor;
  1460. Gestalt(gestaltSystemVersionMajor, &major);
  1461. Gestalt(gestaltSystemVersionMinor, &minor);
  1462. vector<int> parsed_chan_in_list;
  1463. vector<int> parsed_chan_out_list;
  1464. ParseChannelList(chan_in_list, parsed_chan_in_list);
  1465. if (parsed_chan_in_list.size() > 0) {
  1466. jack_info("Explicit input channel list size = %d", parsed_chan_in_list.size());
  1467. inchannels = parsed_chan_in_list.size();
  1468. }
  1469. ParseChannelList(chan_out_list, parsed_chan_out_list);
  1470. if (parsed_chan_out_list.size() > 0) {
  1471. jack_info("Explicit output channel list size = %d", parsed_chan_out_list.size());
  1472. outchannels = parsed_chan_out_list.size();
  1473. }
  1474. // Starting with 10.6 systems, the HAL notification thread is created internally
  1475. if (major == 10 && minor >= 6) {
  1476. CFRunLoopRef theRunLoop = NULL;
  1477. AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  1478. OSStatus osErr = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
  1479. if (osErr != noErr) {
  1480. jack_error("JackCoreAudioDriver::Open kAudioHardwarePropertyRunLoop error");
  1481. printError(osErr);
  1482. }
  1483. }
  1484. if (SetupDevices(capture_driver_uid, playback_driver_uid, capture_driver_name, playback_driver_name, sample_rate) < 0) {
  1485. goto error;
  1486. }
  1487. // Generic JackAudioDriver Open
  1488. if (JackAudioDriver::Open(buffer_size, sample_rate,
  1489. capturing, playing,
  1490. inchannels, outchannels,
  1491. monitor,
  1492. capture_driver_name,
  1493. playback_driver_name,
  1494. capture_latency,
  1495. playback_latency) != 0) {
  1496. goto error;
  1497. }
  1498. if (SetupChannels(capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, true) < 0) {
  1499. goto error;
  1500. }
  1501. if (SetupBufferSize(buffer_size) < 0) {
  1502. goto error;
  1503. }
  1504. if (SetupSampleRate(sample_rate) < 0) {
  1505. goto error;
  1506. }
  1507. if (OpenAUHAL(capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, parsed_chan_in_list, parsed_chan_out_list, buffer_size, sample_rate) < 0) {
  1508. goto error;
  1509. }
  1510. if (capturing && inchannels > 0) {
  1511. if (SetupBuffers(inchannels) < 0) {
  1512. goto error;
  1513. }
  1514. }
  1515. if (AddListeners() < 0) {
  1516. goto error;
  1517. }
  1518. // Core driver may have changed the in/out values
  1519. fCaptureChannels = inchannels;
  1520. fPlaybackChannels = outchannels;
  1521. return noErr;
  1522. error:
  1523. Close();
  1524. return -1;
  1525. }
  1526. int JackCoreAudioDriver::Close()
  1527. {
  1528. jack_log("JackCoreAudioDriver::Close");
  1529. // Generic audio driver close
  1530. int res = JackAudioDriver::Close();
  1531. RemoveListeners();
  1532. DisposeBuffers();
  1533. CloseAUHAL();
  1534. DestroyAggregateDevice();
  1535. return res;
  1536. }
  1537. void JackCoreAudioDriver::UpdateLatencies()
  1538. {
  1539. UInt32 size;
  1540. OSStatus err;
  1541. jack_latency_range_t range;
  1542. range.max = fEngineControl->fBufferSize;
  1543. range.min = fEngineControl->fBufferSize;
  1544. for (int i = 0; i < fCaptureChannels; i++) {
  1545. size = sizeof(UInt32);
  1546. UInt32 value1 = 0;
  1547. UInt32 value2 = 0;
  1548. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyLatency, &size, &value1);
  1549. if (err != noErr) {
  1550. jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error");
  1551. }
  1552. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);
  1553. if (err != noErr) {
  1554. jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error");
  1555. }
  1556. range.min = range.max = fEngineControl->fBufferSize + value1 + value2 + fCaptureLatency;
  1557. fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &range);
  1558. }
  1559. for (int i = 0; i < fPlaybackChannels; i++) {
  1560. size = sizeof(UInt32);
  1561. UInt32 value1 = 0;
  1562. UInt32 value2 = 0;
  1563. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyLatency, &size, &value1);
  1564. if (err != noErr) {
  1565. jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error");
  1566. }
  1567. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);
  1568. if (err != noErr) {
  1569. jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error");
  1570. }
  1571. // Add more latency if "async" mode is used...
  1572. range.min = range.max
  1573. = fEngineControl->fBufferSize + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize * fIOUsage) + value1 + value2 + fPlaybackLatency;
  1574. fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &range);
  1575. // Monitor port
  1576. if (fWithMonitorPorts) {
  1577. range.min = range.max = fEngineControl->fBufferSize;
  1578. fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &range);
  1579. }
  1580. }
  1581. }
  1582. int JackCoreAudioDriver::Attach()
  1583. {
  1584. OSStatus err;
  1585. JackPort* port;
  1586. jack_port_id_t port_index;
  1587. UInt32 size;
  1588. Boolean isWritable;
  1589. char channel_name[64];
  1590. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1591. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1592. jack_log("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  1593. for (int i = 0; i < fCaptureChannels; i++) {
  1594. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
  1595. if (err != noErr) {
  1596. jack_log("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error");
  1597. }
  1598. if (err == noErr && size > 0) {
  1599. err = AudioDeviceGetProperty(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
  1600. if (err != noErr) {
  1601. jack_log("AudioDeviceGetProperty kAudioDevicePropertyChannelName error");
  1602. }
  1603. snprintf(alias, sizeof(alias) - 1, "%s:%s:out_%s%u", fAliasName, fCaptureDriverName, channel_name, i + 1);
  1604. } else {
  1605. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%u", fAliasName, fCaptureDriverName, i + 1);
  1606. }
  1607. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
  1608. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  1609. jack_error("Cannot register port for %s", name);
  1610. return -1;
  1611. }
  1612. port = fGraphManager->GetPort(port_index);
  1613. port->SetAlias(alias);
  1614. fCapturePortList[i] = port_index;
  1615. }
  1616. for (int i = 0; i < fPlaybackChannels; i++) {
  1617. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
  1618. if (err != noErr) {
  1619. jack_log("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error");
  1620. }
  1621. if (err == noErr && size > 0) {
  1622. err = AudioDeviceGetProperty(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
  1623. if (err != noErr) {
  1624. jack_log("AudioDeviceGetProperty kAudioDevicePropertyChannelName error");
  1625. }
  1626. snprintf(alias, sizeof(alias) - 1, "%s:%s:in_%s%u", fAliasName, fPlaybackDriverName, channel_name, i + 1);
  1627. } else {
  1628. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%u", fAliasName, fPlaybackDriverName, i + 1);
  1629. }
  1630. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
  1631. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  1632. jack_error("Cannot register port for %s", name);
  1633. return -1;
  1634. }
  1635. port = fGraphManager->GetPort(port_index);
  1636. port->SetAlias(alias);
  1637. fPlaybackPortList[i] = port_index;
  1638. // Monitor ports
  1639. if (fWithMonitorPorts) {
  1640. jack_log("Create monitor port");
  1641. snprintf(name, sizeof(name) - 1, "%s:monitor_%u", fClientControl.fName, i + 1);
  1642. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, MonitorDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  1643. jack_error("Cannot register monitor port for %s", name);
  1644. return -1;
  1645. } else {
  1646. fMonitorPortList[i] = port_index;
  1647. }
  1648. }
  1649. }
  1650. UpdateLatencies();
  1651. // Input buffers do no change : prepare them only once
  1652. for (int i = 0; i < fCaptureChannels; i++) {
  1653. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  1654. }
  1655. return 0;
  1656. }
  1657. int JackCoreAudioDriver::Start()
  1658. {
  1659. jack_log("JackCoreAudioDriver::Start");
  1660. if (JackAudioDriver::Start() == 0) {
  1661. // Waiting for Render callback to be called (= driver has started)
  1662. fState = false;
  1663. int count = 0;
  1664. OSStatus err = AudioOutputUnitStart(fAUHAL);
  1665. if (err == noErr) {
  1666. while (!fState && count++ < WAIT_COUNTER) {
  1667. usleep(100000);
  1668. jack_log("JackCoreAudioDriver::Start wait count = %d", count);
  1669. }
  1670. if (count < WAIT_COUNTER) {
  1671. jack_info("CoreAudio driver is running...");
  1672. return 0;
  1673. }
  1674. jack_error("CoreAudio driver cannot start...");
  1675. }
  1676. JackAudioDriver::Stop();
  1677. }
  1678. return -1;
  1679. }
  1680. int JackCoreAudioDriver::Stop()
  1681. {
  1682. jack_log("JackCoreAudioDriver::Stop");
  1683. int res = (AudioOutputUnitStop(fAUHAL) == noErr) ? 0 : -1;
  1684. if (JackAudioDriver::Stop() < 0) {
  1685. res = -1;
  1686. }
  1687. return res;
  1688. }
  1689. int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  1690. {
  1691. if (SetupBufferSize(buffer_size) < 0) {
  1692. return -1;
  1693. }
  1694. JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
  1695. // CoreAudio specific
  1696. UpdateLatencies();
  1697. // Input buffers do no change : prepare them only once
  1698. for (int i = 0; i < fCaptureChannels; i++) {
  1699. fJackInputData->mBuffers[i].mNumberChannels = 1;
  1700. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(jack_default_audio_sample_t);
  1701. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  1702. }
  1703. return 0;
  1704. }
  1705. bool JackCoreAudioDriver::TakeHogAux(AudioDeviceID deviceID, bool isInput)
  1706. {
  1707. pid_t hog_pid;
  1708. OSStatus err;
  1709. UInt32 propSize = sizeof(hog_pid);
  1710. err = AudioDeviceGetProperty(deviceID, 0, isInput, kAudioDevicePropertyHogMode, &propSize, &hog_pid);
  1711. if (err) {
  1712. jack_error("Cannot read hog state...");
  1713. printError(err);
  1714. }
  1715. if (hog_pid != getpid()) {
  1716. hog_pid = getpid();
  1717. err = AudioDeviceSetProperty(deviceID, 0, 0, isInput, kAudioDevicePropertyHogMode, propSize, &hog_pid);
  1718. if (err != noErr) {
  1719. jack_error("Can't hog device = %d because it's being hogged by another program or cannot be hogged", deviceID);
  1720. return false;
  1721. }
  1722. }
  1723. return true;
  1724. }
  1725. bool JackCoreAudioDriver::TakeHog()
  1726. {
  1727. OSStatus err = noErr;
  1728. AudioObjectID sub_device[32];
  1729. UInt32 outSize = sizeof(sub_device);
  1730. err = AudioDeviceGetProperty(fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  1731. if (err != noErr) {
  1732. jack_log("Device does not have subdevices");
  1733. return TakeHogAux(fDeviceID, true);
  1734. } else {
  1735. int num_devices = outSize / sizeof(AudioObjectID);
  1736. jack_log("Device does has %d subdevices", num_devices);
  1737. for (int i = 0; i < num_devices; i++) {
  1738. if (!TakeHogAux(sub_device[i], true)) {
  1739. return false;
  1740. }
  1741. }
  1742. return true;
  1743. }
  1744. }
  1745. bool JackCoreAudioDriver::IsAggregateDevice(AudioDeviceID device)
  1746. {
  1747. UInt32 deviceType, outSize = sizeof(UInt32);
  1748. OSStatus err = AudioDeviceGetProperty(device, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyTransportType, &outSize, &deviceType);
  1749. if (err != noErr) {
  1750. jack_log("JackCoreAudioDriver::IsAggregateDevice kAudioDevicePropertyTransportType error");
  1751. return false;
  1752. } else {
  1753. return (deviceType == kAudioDeviceTransportTypeAggregate);
  1754. }
  1755. }
  1756. } // end of namespace
  1757. #ifdef __cplusplus
  1758. extern "C"
  1759. {
  1760. #endif
  1761. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  1762. {
  1763. jack_driver_desc_t * desc;
  1764. jack_driver_desc_filler_t filler;
  1765. jack_driver_param_value_t value;
  1766. desc = jack_driver_descriptor_construct("coreaudio", "Apple CoreAudio API based audio backend", &filler);
  1767. value.i = -1;
  1768. 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");
  1769. 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");
  1770. 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");
  1771. value.str[0] = 0;
  1772. jack_driver_descriptor_add_parameter(desc, &filler, "input-list", 'n', JackDriverParamString, &value, NULL, "Input channel list", "List of input channel number to be opened");
  1773. jack_driver_descriptor_add_parameter(desc, &filler, "output-list", 'N', JackDriverParamString, &value, NULL, "Output channel list", "List of output channel number to be opened");
  1774. value.str[0] = 0;
  1775. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Input CoreAudio device name", NULL);
  1776. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Output CoreAudio device name", NULL);
  1777. value.i = 0;
  1778. jack_driver_descriptor_add_parameter(desc, &filler, "monitor", 'm', JackDriverParamBool, &value, NULL, "Provide monitor ports for the output", NULL);
  1779. value.i = TRUE;
  1780. jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
  1781. value.ui = 44100U;
  1782. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  1783. value.ui = 256U;
  1784. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  1785. value.str[0] = 0;
  1786. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "CoreAudio device name", NULL);
  1787. value.ui = 0;
  1788. jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency (frames)", NULL);
  1789. jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency (frames)", NULL);
  1790. value.i = FALSE;
  1791. jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available CoreAudio devices", NULL);
  1792. value.i = FALSE;
  1793. jack_driver_descriptor_add_parameter(desc, &filler, "hog", 'H', JackDriverParamBool, &value, NULL, "Take exclusive access of the audio device", NULL);
  1794. value.ui = 100;
  1795. jack_driver_descriptor_add_parameter(desc, &filler, "async-latency", 'L', JackDriverParamUInt, &value, NULL, "Extra output latency in asynchronous mode (percent)", NULL);
  1796. value.ui = 100;
  1797. jack_driver_descriptor_add_parameter(desc, &filler, "grain", 'G', JackDriverParamUInt, &value, NULL, "Computation grain in RT thread (percent)", NULL);
  1798. value.i = FALSE;
  1799. 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");
  1800. return desc;
  1801. }
  1802. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  1803. {
  1804. jack_nframes_t srate = 44100;
  1805. jack_nframes_t frames_per_interrupt = 256;
  1806. bool capture = false;
  1807. bool playback = false;
  1808. int chan_in = -1; // Default: if not explicitely set, then max possible will be used...
  1809. int chan_out = -1; // Default: if not explicitely set, then max possible will be used...
  1810. const char* chan_in_list = "";
  1811. const char* chan_out_list = "";
  1812. bool monitor = false;
  1813. const char* capture_driver_uid = "";
  1814. const char* playback_driver_uid = "";
  1815. const JSList *node;
  1816. const jack_driver_param_t *param;
  1817. jack_nframes_t systemic_input_latency = 0;
  1818. jack_nframes_t systemic_output_latency = 0;
  1819. int async_output_latency = 100;
  1820. int computation_grain = -1;
  1821. bool hogged = false;
  1822. bool clock_drift = false;
  1823. for (node = params; node; node = jack_slist_next(node)) {
  1824. param = (const jack_driver_param_t *) node->data;
  1825. switch (param->character) {
  1826. case 'd':
  1827. capture_driver_uid = param->value.str;
  1828. playback_driver_uid = param->value.str;
  1829. break;
  1830. case 'D':
  1831. capture = true;
  1832. playback = true;
  1833. break;
  1834. case 'c':
  1835. chan_in = chan_out = param->value.i;
  1836. break;
  1837. case 'i':
  1838. chan_in = param->value.i;
  1839. break;
  1840. case 'o':
  1841. chan_out = param->value.i;
  1842. break;
  1843. case 'n':
  1844. chan_in_list = param->value.str;
  1845. break;
  1846. case 'N':
  1847. chan_out_list = param->value.str;
  1848. break;
  1849. case 'C':
  1850. capture = true;
  1851. if (strcmp(param->value.str, "none") != 0) {
  1852. capture_driver_uid = param->value.str;
  1853. }
  1854. break;
  1855. case 'P':
  1856. playback = true;
  1857. if (strcmp(param->value.str, "none") != 0) {
  1858. playback_driver_uid = param->value.str;
  1859. }
  1860. break;
  1861. case 'm':
  1862. monitor = param->value.i;
  1863. break;
  1864. case 'r':
  1865. srate = param->value.ui;
  1866. break;
  1867. case 'p':
  1868. frames_per_interrupt = (unsigned int)param->value.ui;
  1869. break;
  1870. case 'I':
  1871. systemic_input_latency = param->value.ui;
  1872. break;
  1873. case 'O':
  1874. systemic_output_latency = param->value.ui;
  1875. break;
  1876. case 'l':
  1877. Jack::DisplayDeviceNames();
  1878. return NULL;
  1879. case 'H':
  1880. hogged = true;
  1881. break;
  1882. case 'L':
  1883. async_output_latency = param->value.ui;
  1884. break;
  1885. case 'G':
  1886. computation_grain = param->value.ui;
  1887. break;
  1888. case 's':
  1889. clock_drift = true;
  1890. break;
  1891. }
  1892. }
  1893. /* duplex is the default */
  1894. if (!capture && !playback) {
  1895. capture = true;
  1896. playback = true;
  1897. }
  1898. if (strcmp(chan_in_list, "") != 0 && chan_in >= 0) {
  1899. printf("Input channel list and in channels are both specified, input channel list will take over...\n");
  1900. }
  1901. if (strcmp(chan_out_list, "") != 0 && chan_out >= 0) {
  1902. printf("Output channel list and out channels are both specified, output channel list will take over...\n");
  1903. }
  1904. Jack::JackCoreAudioDriver* driver = new Jack::JackCoreAudioDriver("system", "coreaudio", engine, table);
  1905. if (driver->Open(frames_per_interrupt,
  1906. srate, capture,
  1907. playback, chan_in,
  1908. chan_out, chan_in_list,
  1909. chan_out_list, monitor,
  1910. capture_driver_uid,
  1911. playback_driver_uid,
  1912. systemic_input_latency,
  1913. systemic_output_latency,
  1914. async_output_latency,
  1915. computation_grain,
  1916. hogged, clock_drift) == 0) {
  1917. return driver;
  1918. } else {
  1919. delete driver;
  1920. return NULL;
  1921. }
  1922. }
  1923. #ifdef __cplusplus
  1924. }
  1925. #endif