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.

1881 lines
71KB

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