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.

1871 lines
70KB

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