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.

1769 lines
66KB

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