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.

1816 lines
68KB

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