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.

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