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.

1701 lines
64KB

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