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.

1328 lines
50KB

  1. /*
  2. Copyright (C) 2004-2006 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 "driver_interface.h"
  24. #include <iostream>
  25. namespace Jack
  26. {
  27. static void PrintStreamDesc(AudioStreamBasicDescription *inDesc)
  28. {
  29. JackLog("- - - - - - - - - - - - - - - - - - - -\n");
  30. JackLog(" Sample Rate:%f\n", inDesc->mSampleRate);
  31. JackLog(" Format ID:%.*s\n", (int) sizeof(inDesc->mFormatID), (char*)&inDesc->mFormatID);
  32. JackLog(" Format Flags:%lX\n", inDesc->mFormatFlags);
  33. JackLog(" Bytes per Packet:%ld\n", inDesc->mBytesPerPacket);
  34. JackLog(" Frames per Packet:%ld\n", inDesc->mFramesPerPacket);
  35. JackLog(" Bytes per Frame:%ld\n", inDesc->mBytesPerFrame);
  36. JackLog(" Channels per Frame:%ld\n", inDesc->mChannelsPerFrame);
  37. JackLog(" Bits per Channel:%ld\n", inDesc->mBitsPerChannel);
  38. JackLog("- - - - - - - - - - - - - - - - - - - -\n");
  39. }
  40. static void printError(OSStatus err)
  41. {
  42. switch (err) {
  43. case kAudioHardwareNoError:
  44. JackLog("error code : kAudioHardwareNoError\n");
  45. break;
  46. case kAudioConverterErr_FormatNotSupported:
  47. JackLog("error code : kAudioConverterErr_FormatNotSupported\n");
  48. break;
  49. case kAudioConverterErr_OperationNotSupported:
  50. JackLog("error code : kAudioConverterErr_OperationNotSupported\n");
  51. break;
  52. case kAudioConverterErr_PropertyNotSupported:
  53. JackLog("error code : kAudioConverterErr_PropertyNotSupported\n");
  54. break;
  55. case kAudioConverterErr_InvalidInputSize:
  56. JackLog("error code : kAudioConverterErr_InvalidInputSize\n");
  57. break;
  58. case kAudioConverterErr_InvalidOutputSize:
  59. JackLog("error code : kAudioConverterErr_InvalidOutputSize\n");
  60. break;
  61. case kAudioConverterErr_UnspecifiedError:
  62. JackLog("error code : kAudioConverterErr_UnspecifiedError\n");
  63. break;
  64. case kAudioConverterErr_BadPropertySizeError:
  65. JackLog("error code : kAudioConverterErr_BadPropertySizeError\n");
  66. break;
  67. case kAudioConverterErr_RequiresPacketDescriptionsError:
  68. JackLog("error code : kAudioConverterErr_RequiresPacketDescriptionsError\n");
  69. break;
  70. case kAudioConverterErr_InputSampleRateOutOfRange:
  71. JackLog("error code : kAudioConverterErr_InputSampleRateOutOfRange\n");
  72. break;
  73. case kAudioConverterErr_OutputSampleRateOutOfRange:
  74. JackLog("error code : kAudioConverterErr_OutputSampleRateOutOfRange\n");
  75. break;
  76. case kAudioHardwareNotRunningError:
  77. JackLog("error code : kAudioHardwareNotRunningError\n");
  78. break;
  79. case kAudioHardwareUnknownPropertyError:
  80. JackLog("error code : kAudioHardwareUnknownPropertyError\n");
  81. break;
  82. case kAudioHardwareIllegalOperationError:
  83. JackLog("error code : kAudioHardwareIllegalOperationError\n");
  84. break;
  85. case kAudioHardwareBadDeviceError:
  86. JackLog("error code : kAudioHardwareBadDeviceError\n");
  87. break;
  88. case kAudioHardwareBadStreamError:
  89. JackLog("error code : kAudioHardwareBadStreamError\n");
  90. break;
  91. case kAudioDeviceUnsupportedFormatError:
  92. JackLog("error code : kAudioDeviceUnsupportedFormatError\n");
  93. break;
  94. case kAudioDevicePermissionsError:
  95. JackLog("error code : kAudioDevicePermissionsError\n");
  96. break;
  97. case kAudioHardwareBadObjectError:
  98. JackLog("error code : kAudioHardwareBadObjectError\n");
  99. break;
  100. case kAudioHardwareUnsupportedOperationError:
  101. JackLog("error code : kAudioHardwareUnsupportedOperationError\n");
  102. break;
  103. default:
  104. JackLog("error code : unknown\n");
  105. break;
  106. }
  107. }
  108. static OSStatus DisplayDeviceNames()
  109. {
  110. UInt32 size;
  111. Boolean isWritable;
  112. int i, deviceNum;
  113. OSStatus err;
  114. CFStringRef UIname;
  115. err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
  116. if (err != noErr)
  117. return err;
  118. deviceNum = size / sizeof(AudioDeviceID);
  119. AudioDeviceID devices[deviceNum];
  120. err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
  121. if (err != noErr)
  122. return err;
  123. for (i = 0; i < deviceNum; i++) {
  124. char device_name[256];
  125. char internal_name[256];
  126. size = sizeof(CFStringRef);
  127. UIname = NULL;
  128. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  129. if (err == noErr) {
  130. CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
  131. } else {
  132. goto error;
  133. }
  134. size = 256;
  135. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
  136. if (err != noErr)
  137. return err;
  138. printf("Device name = \'%s\', internal_name = \'%s\' (to be used as -C, -P, or -d parameter)\n", device_name, internal_name);
  139. }
  140. return noErr;
  141. error:
  142. if (UIname != NULL)
  143. CFRelease(UIname);
  144. return err;
  145. }
  146. OSStatus JackCoreAudioDriver::Render(void *inRefCon,
  147. AudioUnitRenderActionFlags *ioActionFlags,
  148. const AudioTimeStamp *inTimeStamp,
  149. UInt32 inBusNumber,
  150. UInt32 inNumberFrames,
  151. AudioBufferList *ioData)
  152. {
  153. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inRefCon;
  154. driver->fLastWaitUst = GetMicroSeconds(); // Take callback date here
  155. driver->fActionFags = ioActionFlags;
  156. driver->fCurrentTime = (AudioTimeStamp *)inTimeStamp;
  157. driver->fDriverOutputData = ioData;
  158. return driver->Process();
  159. }
  160. int JackCoreAudioDriver::Read()
  161. {
  162. AudioUnitRender(fAUHAL, fActionFags, fCurrentTime, 1, fEngineControl->fBufferSize, fJackInputData);
  163. return 0;
  164. }
  165. int JackCoreAudioDriver::Write()
  166. {
  167. for (int i = 0; i < fPlaybackChannels; i++) {
  168. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  169. float* buffer = GetOutputBuffer(i);
  170. int size = sizeof(float) * fEngineControl->fBufferSize;
  171. memcpy((float*)fDriverOutputData->mBuffers[i].mData, buffer, size);
  172. // Monitor ports
  173. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  174. memcpy(GetMonitorBuffer(i), buffer, size);
  175. } else {
  176. memset((float*)fDriverOutputData->mBuffers[i].mData, 0, sizeof(float) * fEngineControl->fBufferSize);
  177. }
  178. }
  179. return 0;
  180. }
  181. // Will run only once
  182. OSStatus JackCoreAudioDriver::MeasureCallback(AudioDeviceID inDevice,
  183. const AudioTimeStamp* inNow,
  184. const AudioBufferList* inInputData,
  185. const AudioTimeStamp* inInputTime,
  186. AudioBufferList* outOutputData,
  187. const AudioTimeStamp* inOutputTime,
  188. void* inClientData)
  189. {
  190. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  191. AudioDeviceStop(driver->fDeviceID, MeasureCallback);
  192. AudioDeviceRemoveIOProc(driver->fDeviceID, MeasureCallback);
  193. JackLog("JackCoreAudioDriver::MeasureCallback called\n");
  194. JackMachThread::GetParams(&driver->fEngineControl->fPeriod, &driver->fEngineControl->fComputation, &driver->fEngineControl->fConstraint);
  195. return noErr;
  196. }
  197. OSStatus JackCoreAudioDriver::SRNotificationCallback(AudioDeviceID inDevice,
  198. UInt32 inChannel,
  199. Boolean isInput,
  200. AudioDevicePropertyID inPropertyID,
  201. void* inClientData)
  202. {
  203. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  204. switch (inPropertyID) {
  205. case kAudioDevicePropertyNominalSampleRate: {
  206. JackLog("JackCoreAudioDriver::SRNotificationCallback kAudioDevicePropertyNominalSampleRate \n");
  207. driver->fState = true;
  208. break;
  209. }
  210. }
  211. return noErr;
  212. }
  213. // A better implementation would try to recover in case of hardware device change (see HALLAB HLFilePlayerWindowControllerAudioDevicePropertyListenerProc code)
  214. OSStatus JackCoreAudioDriver::DeviceNotificationCallback(AudioDeviceID inDevice,
  215. UInt32 inChannel,
  216. Boolean isInput,
  217. AudioDevicePropertyID inPropertyID,
  218. void* inClientData)
  219. {
  220. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  221. switch (inPropertyID) {
  222. case kAudioDeviceProcessorOverload:
  223. JackLog("JackCoreAudioDriver::DeviceNotificationCallback kAudioDeviceProcessorOverload\n");
  224. #ifdef DEBUG
  225. //driver->fLogFile->Capture(AudioGetCurrentHostTime() - AudioConvertNanosToHostTime(LOG_SAMPLE_DURATION * 1000000), AudioGetCurrentHostTime(), true, "Captured Latency Log for I/O Cycle Overload\n");
  226. #endif
  227. driver->NotifyXRun(GetMicroSeconds());
  228. break;
  229. case kAudioDevicePropertyDeviceIsRunning: {
  230. UInt32 outSize = sizeof(UInt32);
  231. driver->fStopTime = CFAbsoluteTimeGetCurrent();
  232. OSStatus err = AudioDeviceGetProperty(driver->fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyDeviceIsRunning, &outSize, &driver->fRunning);
  233. JackLog("JackCoreAudioDriver::DeviceNotificationCallback kAudioDevicePropertyDeviceIsRunning res = %ld\n", driver->fRunning);
  234. if (err != noErr) {
  235. jack_error("Cannot getkAudioDevicePropertyDeviceIsRunning");
  236. printError(err);
  237. }
  238. break;
  239. }
  240. case kAudioDevicePropertyStreamConfiguration:
  241. JackLog("JackCoreAudioDriver::DeviceNotificationCallback kAudioDevicePropertyStreamConfiguration \n");
  242. //JackLog("GetTotalNumberChannels input = %ld\n", GetTotalNumberChannels(driver->fDeviceID, true));
  243. //JackLog("GetTotalNumberChannels output = %ld\n", GetTotalNumberChannels(driver->fDeviceID, false));
  244. break;
  245. case kAudioDevicePropertyNominalSampleRate: {
  246. OSStatus err;
  247. UInt32 outSize = sizeof(Float64);
  248. Float64 sampleRate;
  249. return noErr; // for now
  250. err = AudioOutputUnitStop(driver->fAUHAL);
  251. if (err != noErr)
  252. jack_error("Error calling AudioOutputUnitStop");
  253. AudioStreamBasicDescription srcFormat, dstFormat;
  254. err = AudioDeviceGetProperty(driver->fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  255. if (err != noErr) {
  256. jack_error("Cannot get current sample rate");
  257. printError(err);
  258. return kAudioHardwareUnsupportedOperationError;
  259. }
  260. JackLog("JackCoreAudioDriver::DeviceNotificationCallback kAudioDevicePropertyNominalSampleRate %ld\n", long(sampleRate));
  261. if (sampleRate != driver->fEngineControl->fSampleRate) {
  262. // To get SR change notification
  263. /*
  264. driver->fState = false;
  265. err = AudioDeviceAddPropertyListener(driver->fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, driver);
  266. if (err != noErr) {
  267. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  268. printError(err);
  269. return kAudioHardwareUnsupportedOperationError;
  270. }
  271. */
  272. //sampleRate = driver->fEngineControl->fSampleRate;
  273. err = AudioDeviceSetProperty(driver->fDeviceID, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
  274. if (err != noErr) {
  275. jack_error("Cannot set sample rate = %ld", sampleRate);
  276. printError(err);
  277. return kAudioHardwareUnsupportedOperationError;
  278. }
  279. JackLog("JackCoreAudioDriver::DeviceNotificationCallback sampleRate %ld\n", driver->fEngineControl->fSampleRate);
  280. /*
  281. // Waiting for SR change notification
  282. int count = 0;
  283. while (!driver->fState && count++ < 100) {
  284. usleep(100000);
  285. JackLog("Wait count = %ld\n", count);
  286. }
  287. // Remove SR change notification
  288. AudioDeviceRemovePropertyListener(driver->fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
  289. */
  290. }
  291. /*
  292. // Update SR for input
  293. //Float64 sampleRate = driver->fEngineControl->fSampleRate;
  294. outSize = sizeof(AudioStreamBasicDescription);
  295. err = AudioUnitGetProperty(driver->fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, &outSize);
  296. if (err != noErr) {
  297. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  298. printError(err);
  299. }
  300. srcFormat.mSampleRate = sampleRate;
  301. //srcFormat.mSampleRate = driver->fEngineControl->fSampleRate;
  302. err = AudioUnitSetProperty(driver->fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, outSize);
  303. if (err != noErr) {
  304. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  305. printError(err);
  306. }
  307. // Update SR for output
  308. err = AudioUnitGetProperty(driver->fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, &outSize);
  309. if (err != noErr) {
  310. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  311. printError(err);
  312. }
  313. dstFormat.mSampleRate = sampleRate;
  314. //dstFormat.mSampleRate = driver->fEngineControl->fSampleRate;
  315. err = AudioUnitSetProperty(driver->fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, outSize);
  316. if (err != noErr) {
  317. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  318. printError(err);
  319. }
  320. */
  321. err = AudioOutputUnitStart(driver->fAUHAL);
  322. if (err != noErr)
  323. jack_error("Error calling AudioOutputUnitStart");
  324. break;
  325. }
  326. }
  327. return noErr;
  328. }
  329. OSStatus JackCoreAudioDriver::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
  330. {
  331. UInt32 size = sizeof(AudioValueTranslation);
  332. CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
  333. AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
  334. if (inIUD == NULL) {
  335. return kAudioHardwareUnspecifiedError;
  336. } else {
  337. OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
  338. CFRelease(inIUD);
  339. JackLog("get_device_id_from_uid %s %ld \n", UID, *id);
  340. return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
  341. }
  342. }
  343. OSStatus JackCoreAudioDriver::GetDefaultDevice(AudioDeviceID* id)
  344. {
  345. OSStatus res;
  346. UInt32 theSize = sizeof(UInt32);
  347. AudioDeviceID inDefault;
  348. AudioDeviceID outDefault;
  349. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
  350. return res;
  351. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
  352. return res;
  353. JackLog("GetDefaultDevice: input = %ld output = %ld\n", inDefault, outDefault);
  354. // Get the device only if default input and ouput are the same
  355. if (inDefault == outDefault) {
  356. *id = inDefault;
  357. return noErr;
  358. } else {
  359. jack_error("Default input and output devices are not the same !!");
  360. return kAudioHardwareBadDeviceError;
  361. }
  362. }
  363. OSStatus JackCoreAudioDriver::GetDefaultInputDevice(AudioDeviceID* id)
  364. {
  365. OSStatus res;
  366. UInt32 theSize = sizeof(UInt32);
  367. AudioDeviceID inDefault;
  368. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
  369. return res;
  370. JackLog("GetDefaultInputDevice: input = %ld \n", inDefault);
  371. *id = inDefault;
  372. return noErr;
  373. }
  374. OSStatus JackCoreAudioDriver::GetDefaultOutputDevice(AudioDeviceID* id)
  375. {
  376. OSStatus res;
  377. UInt32 theSize = sizeof(UInt32);
  378. AudioDeviceID outDefault;
  379. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
  380. return res;
  381. JackLog("GetDefaultOutputDevice: output = %ld\n", outDefault);
  382. *id = outDefault;
  383. return noErr;
  384. }
  385. OSStatus JackCoreAudioDriver::GetDeviceNameFromID(AudioDeviceID id, char* name)
  386. {
  387. UInt32 size = 256;
  388. return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
  389. }
  390. OSStatus JackCoreAudioDriver::GetTotalChannels(AudioDeviceID device, long* channelCount, bool isInput)
  391. {
  392. OSStatus err = noErr;
  393. UInt32 outSize;
  394. Boolean outWritable;
  395. AudioBufferList* bufferList = 0;
  396. *channelCount = 0;
  397. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  398. if (err == noErr) {
  399. bufferList = (AudioBufferList*)malloc(outSize);
  400. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  401. if (err == noErr) {
  402. for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++)
  403. *channelCount += bufferList->mBuffers[i].mNumberChannels;
  404. }
  405. if (bufferList)
  406. free(bufferList);
  407. }
  408. return err;
  409. }
  410. JackCoreAudioDriver::JackCoreAudioDriver(const char* name, JackEngine* engine, JackSynchro** table)
  411. : JackAudioDriver(name, engine, table), fJackInputData(NULL), fDriverOutputData(NULL), fState(false), fStopTime(0), fRunning(true)
  412. {
  413. #ifdef DEBUG
  414. //fLogFile = new CALatencyLog("jackmp_latency", ".txt");
  415. #endif
  416. fThread = JackGlobals::MakeThread(this);
  417. }
  418. JackCoreAudioDriver::~JackCoreAudioDriver()
  419. {
  420. #ifdef DEBUG
  421. //delete fLogFile;
  422. #endif
  423. fThread->Kill();
  424. delete fThread;
  425. }
  426. bool JackCoreAudioDriver::Execute()
  427. {
  428. while (true) {
  429. JackLog("Check device running...\n");
  430. if (!fRunning && CFAbsoluteTimeGetCurrent() > fStopTime + 3.0) {
  431. jack_error("Critical error : device not running anymore...");
  432. // Send notification to be used in JackPilot or JackRouter plugin
  433. CFStringRef ref = CFStringCreateWithCString(NULL, fEngineControl->fServerName, kCFStringEncodingMacRoman);
  434. CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
  435. CFSTR("com.grame.jackserver.stop"),
  436. ref,
  437. NULL,
  438. kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
  439. CFRelease(ref);
  440. }
  441. usleep(2000000);
  442. }
  443. return false;
  444. }
  445. int JackCoreAudioDriver::Open(jack_nframes_t nframes,
  446. jack_nframes_t samplerate,
  447. bool capturing,
  448. bool playing,
  449. int inchannels,
  450. int outchannels,
  451. bool monitor,
  452. const char* capture_driver_uid,
  453. const char* playback_driver_uid,
  454. jack_nframes_t capture_latency,
  455. jack_nframes_t playback_latency)
  456. {
  457. OSStatus err = noErr;
  458. ComponentResult err1;
  459. UInt32 outSize;
  460. UInt32 enableIO;
  461. AudioStreamBasicDescription srcFormat, dstFormat;
  462. Float64 sampleRate;
  463. long in_nChannels = 0;
  464. long out_nChannels = 0;
  465. char capture_driver_name[256];
  466. char playback_driver_name[256];
  467. capture_driver_name[0] = 0;
  468. playback_driver_name[0] = 0;
  469. JackLog("JackCoreAudioDriver::Open nframes = %ld in = %ld out = %ld capture name = %s playback name = %s\n",
  470. nframes, inchannels, outchannels, capture_driver_uid, playback_driver_uid);
  471. // Duplex
  472. if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
  473. JackLog("JackCoreAudioDriver::Open duplex \n");
  474. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  475. if (GetDefaultDevice(&fDeviceID) != noErr) {
  476. jack_error("Cannot open default device");
  477. return -1;
  478. }
  479. }
  480. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  481. jack_error("Cannot get device name from device ID");
  482. return -1;
  483. }
  484. // Capture only
  485. } else if (strcmp(capture_driver_uid, "") != 0) {
  486. JackLog("JackCoreAudioDriver::Open capture only \n");
  487. if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
  488. if (GetDefaultInputDevice(&fDeviceID) != noErr) {
  489. jack_error("Cannot open default device");
  490. return -1;
  491. }
  492. }
  493. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
  494. jack_error("Cannot get device name from device ID");
  495. return -1;
  496. }
  497. // Playback only
  498. } else if (strcmp(playback_driver_uid, "") != 0) {
  499. JackLog("JackCoreAudioDriver::Open playback only \n");
  500. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  501. if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
  502. jack_error("Cannot open default device");
  503. return -1;
  504. }
  505. }
  506. if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  507. jack_error("Cannot get device name from device ID");
  508. return -1;
  509. }
  510. // Use default driver in duplex mode
  511. } else {
  512. JackLog("JackCoreAudioDriver::Open default driver \n");
  513. if (GetDefaultDevice(&fDeviceID) != noErr) {
  514. jack_error("Cannot open default device");
  515. return -1;
  516. }
  517. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  518. jack_error("Cannot get device name from device ID");
  519. return -1;
  520. }
  521. }
  522. // Generic JackAudioDriver Open
  523. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency) != 0) {
  524. return -1;
  525. }
  526. if (capturing) {
  527. err = GetTotalChannels(fDeviceID, &in_nChannels, true);
  528. if (err != noErr) {
  529. jack_error("Cannot get input channel number");
  530. printError(err);
  531. return -1;
  532. }
  533. }
  534. if (playing) {
  535. err = GetTotalChannels(fDeviceID, &out_nChannels, false);
  536. if (err != noErr) {
  537. jack_error("Cannot get output channel number");
  538. printError(err);
  539. return -1;
  540. }
  541. }
  542. if (inchannels > in_nChannels) {
  543. jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
  544. return -1;
  545. }
  546. if (outchannels > out_nChannels) {
  547. jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
  548. return -1;
  549. }
  550. if (inchannels == 0) {
  551. JackLog("Setup max in channels = %ld\n", in_nChannels);
  552. inchannels = in_nChannels;
  553. }
  554. if (outchannels == 0) {
  555. JackLog("Setup max out channels = %ld\n", out_nChannels);
  556. outchannels = out_nChannels;
  557. }
  558. // Setting buffer size
  559. outSize = sizeof(UInt32);
  560. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &fEngineControl->fBufferSize);
  561. if (err != noErr) {
  562. jack_error("Cannot set buffer size %ld", nframes);
  563. printError(err);
  564. return -1;
  565. }
  566. // Get sample rate
  567. outSize = sizeof(Float64);
  568. err = AudioDeviceGetProperty(fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  569. if (err != noErr) {
  570. jack_error("Cannot get current sample rate");
  571. printError(err);
  572. return -1;
  573. }
  574. // If needed, set new sample rate
  575. if (samplerate != (jack_nframes_t)sampleRate) {
  576. sampleRate = (Float64)samplerate;
  577. // To get SR change notification
  578. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
  579. if (err != noErr) {
  580. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  581. printError(err);
  582. return -1;
  583. }
  584. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
  585. if (err != noErr) {
  586. jack_error("Cannot set sample rate = %ld", samplerate);
  587. printError(err);
  588. return -1;
  589. }
  590. // Waiting for SR change notification
  591. int count = 0;
  592. while (!fState && count++ < 100) {
  593. usleep(100000);
  594. JackLog("Wait count = %ld\n", count);
  595. }
  596. // Remove SR change notification
  597. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
  598. }
  599. // AUHAL
  600. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  601. Component HALOutput = FindNextComponent(NULL, &cd);
  602. err1 = OpenAComponent(HALOutput, &fAUHAL);
  603. if (err1 != noErr) {
  604. jack_error("Error calling OpenAComponent");
  605. printError(err1);
  606. goto error;
  607. }
  608. err1 = AudioUnitInitialize(fAUHAL);
  609. if (err1 != noErr) {
  610. jack_error("Cannot initialize AUHAL unit");
  611. printError(err1);
  612. goto error;
  613. }
  614. // Start I/O
  615. enableIO = 1;
  616. if (capturing && inchannels > 0) {
  617. JackLog("Setup AUHAL input\n");
  618. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  619. if (err1 != noErr) {
  620. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  621. printError(err1);
  622. goto error;
  623. }
  624. }
  625. if (playing && outchannels > 0) {
  626. JackLog("Setup AUHAL output\n");
  627. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  628. if (err1 != noErr) {
  629. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  630. printError(err1);
  631. goto error;
  632. }
  633. }
  634. // Setup up choosen device, in both input and output cases
  635. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  636. if (err1 != noErr) {
  637. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  638. printError(err1);
  639. goto error;
  640. }
  641. // Set buffer size
  642. if (capturing && inchannels > 0) {
  643. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&nframes, sizeof(UInt32));
  644. if (err1 != noErr) {
  645. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  646. printError(err1);
  647. goto error;
  648. }
  649. }
  650. if (playing && outchannels > 0) {
  651. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&nframes, sizeof(UInt32));
  652. if (err1 != noErr) {
  653. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  654. printError(err1);
  655. goto error;
  656. }
  657. }
  658. // Setup channel map
  659. if (capturing && inchannels > 0 && inchannels < in_nChannels) {
  660. SInt32 chanArr[in_nChannels];
  661. for (int i = 0; i < in_nChannels; i++) {
  662. chanArr[i] = -1;
  663. }
  664. for (int i = 0; i < inchannels; i++) {
  665. chanArr[i] = i;
  666. }
  667. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  668. if (err1 != noErr) {
  669. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  670. printError(err1);
  671. }
  672. }
  673. if (playing && outchannels > 0 && outchannels < out_nChannels) {
  674. SInt32 chanArr[out_nChannels];
  675. for (int i = 0; i < out_nChannels; i++) {
  676. chanArr[i] = -1;
  677. }
  678. for (int i = 0; i < outchannels; i++) {
  679. chanArr[i] = i;
  680. }
  681. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  682. if (err1 != noErr) {
  683. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  684. printError(err1);
  685. }
  686. }
  687. // Setup stream converters
  688. JackLog("Setup AUHAL input stream converter SR = %ld\n", samplerate);
  689. srcFormat.mSampleRate = samplerate;
  690. srcFormat.mFormatID = kAudioFormatLinearPCM;
  691. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  692. srcFormat.mBytesPerPacket = sizeof(float);
  693. srcFormat.mFramesPerPacket = 1;
  694. srcFormat.mBytesPerFrame = sizeof(float);
  695. srcFormat.mChannelsPerFrame = outchannels;
  696. srcFormat.mBitsPerChannel = 32;
  697. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(AudioStreamBasicDescription));
  698. if (err1 != noErr) {
  699. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  700. printError(err1);
  701. }
  702. JackLog("Setup AUHAL output stream converter SR = %ld\n", samplerate);
  703. dstFormat.mSampleRate = samplerate;
  704. dstFormat.mFormatID = kAudioFormatLinearPCM;
  705. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  706. dstFormat.mBytesPerPacket = sizeof(float);
  707. dstFormat.mFramesPerPacket = 1;
  708. dstFormat.mBytesPerFrame = sizeof(float);
  709. dstFormat.mChannelsPerFrame = inchannels;
  710. dstFormat.mBitsPerChannel = 32;
  711. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
  712. if (err1 != noErr) {
  713. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  714. printError(err1);
  715. }
  716. // Setup callbacks
  717. if (inchannels > 0 && outchannels == 0) {
  718. AURenderCallbackStruct output;
  719. output.inputProc = Render;
  720. output.inputProcRefCon = this;
  721. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  722. if (err1 != noErr) {
  723. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  724. printError(err1);
  725. goto error;
  726. }
  727. } else {
  728. AURenderCallbackStruct output;
  729. output.inputProc = Render;
  730. output.inputProcRefCon = this;
  731. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  732. if (err1 != noErr) {
  733. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  734. printError(err1);
  735. goto error;
  736. }
  737. }
  738. // Prepare buffers
  739. if (capturing && inchannels > 0) {
  740. fJackInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  741. if (fJackInputData == 0) {
  742. jack_error("Cannot allocate memory for input buffers");
  743. goto error;
  744. }
  745. fJackInputData->mNumberBuffers = inchannels;
  746. for (int i = 0; i < fCaptureChannels; i++) {
  747. fJackInputData->mBuffers[i].mNumberChannels = 1;
  748. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  749. }
  750. }
  751. // Add listeners
  752. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
  753. if (err != noErr) {
  754. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  755. printError(err1);
  756. goto error;
  757. }
  758. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback, this);
  759. if (err != noErr) {
  760. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
  761. printError(err1);
  762. goto error;
  763. }
  764. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback, this);
  765. if (err != noErr) {
  766. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  767. printError(err1);
  768. goto error;
  769. }
  770. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback, this);
  771. if (err != noErr) {
  772. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
  773. printError(err1);
  774. goto error;
  775. }
  776. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  777. if (err != noErr) {
  778. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  779. printError(err1);
  780. goto error;
  781. }
  782. err = AudioDeviceAddPropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  783. if (err != noErr) {
  784. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  785. printError(err1);
  786. goto error;
  787. }
  788. fDriverOutputData = 0;
  789. // Start checking thread...
  790. fThread->Start();
  791. // Core driver may have changed the in/out values
  792. fCaptureChannels = inchannels;
  793. fPlaybackChannels = outchannels;
  794. return noErr;
  795. error:
  796. AudioUnitUninitialize(fAUHAL);
  797. CloseComponent(fAUHAL);
  798. return -1;
  799. }
  800. int JackCoreAudioDriver::Close()
  801. {
  802. JackLog("JackCoreAudioDriver::Close\n");
  803. JackAudioDriver::Close();
  804. // Possibly (if MeasureCallback has not been called)
  805. AudioDeviceStop(fDeviceID, MeasureCallback);
  806. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  807. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
  808. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback);
  809. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback);
  810. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback);
  811. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  812. AudioDeviceRemovePropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  813. free(fJackInputData);
  814. AudioUnitUninitialize(fAUHAL);
  815. CloseComponent(fAUHAL);
  816. // Kill checking thread...
  817. fThread->Kill();
  818. return 0;
  819. }
  820. int JackCoreAudioDriver::Attach()
  821. {
  822. OSStatus err;
  823. JackPort* port;
  824. jack_port_id_t port_index;
  825. UInt32 size;
  826. Boolean isWritable;
  827. char channel_name[64];
  828. char buf[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  829. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  830. JackLog("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  831. for (int i = 0; i < fCaptureChannels; i++) {
  832. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
  833. if (err != noErr)
  834. JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
  835. if (err == noErr && size > 0) {
  836. err = AudioDeviceGetProperty(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
  837. if (err != noErr)
  838. JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  839. snprintf(buf, sizeof(buf) - 1, "%s:%s:out_%s%u", fClientControl->fName, fCaptureDriverName, channel_name, i + 1);
  840. } else {
  841. snprintf(buf, sizeof(buf) - 1, "%s:%s:out%u", fClientControl->fName, fCaptureDriverName, i + 1);
  842. }
  843. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags)) == NO_PORT) {
  844. jack_error("Cannot register port for %s", buf);
  845. return -1;
  846. }
  847. size = sizeof(UInt32);
  848. UInt32 value1 = 0;
  849. UInt32 value2 = 0;
  850. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyLatency, &size, &value1);
  851. if (err != noErr)
  852. JackLog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  853. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);
  854. if (err != noErr)
  855. JackLog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  856. port = fGraphManager->GetPort(port_index);
  857. port->Rename("system:capture_%d", i + 1);
  858. port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fCaptureLatency);
  859. fCapturePortList[i] = port_index;
  860. }
  861. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  862. for (int i = 0; i < fPlaybackChannels; i++) {
  863. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
  864. if (err != noErr)
  865. JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
  866. if (err == noErr && size > 0) {
  867. err = AudioDeviceGetProperty(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
  868. if (err != noErr)
  869. JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  870. snprintf(buf, sizeof(buf) - 1, "%s:%s:in_%s%u", fClientControl->fName, fPlaybackDriverName, channel_name, i + 1);
  871. } else {
  872. snprintf(buf, sizeof(buf) - 1, "%s:%s:in%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  873. }
  874. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags)) == NO_PORT) {
  875. jack_error("Cannot register port for %s", buf);
  876. return -1;
  877. }
  878. size = sizeof(UInt32);
  879. UInt32 value1 = 0;
  880. UInt32 value2 = 0;
  881. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyLatency, &size, &value1);
  882. if (err != noErr)
  883. JackLog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  884. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);
  885. if (err != noErr)
  886. JackLog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  887. port = fGraphManager->GetPort(port_index);
  888. port->Rename("system:playback_%d", i + 1);
  889. port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fPlaybackLatency);
  890. fPlaybackPortList[i] = port_index;
  891. // Monitor ports
  892. if (fWithMonitorPorts) {
  893. JackLog("Create monitor port \n");
  894. snprintf(buf, sizeof(buf) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  895. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput)) == NO_PORT) {
  896. jack_error("Cannot register monitor port for %s", buf);
  897. return -1;
  898. } else {
  899. port = fGraphManager->GetPort(port_index);
  900. port->SetLatency(fEngineControl->fBufferSize);
  901. fMonitorPortList[i] = port_index;
  902. }
  903. }
  904. }
  905. // Input buffers do no change : prepare them only once
  906. for (int i = 0; i < fCaptureChannels; i++) {
  907. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  908. }
  909. return 0;
  910. }
  911. int JackCoreAudioDriver::Start()
  912. {
  913. JackLog("JackCoreAudioDriver::Start\n");
  914. JackAudioDriver::Start();
  915. OSStatus err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
  916. if (err != noErr)
  917. return -1;
  918. err = AudioOutputUnitStart(fAUHAL);
  919. if (err != noErr)
  920. return -1;
  921. if ((err = AudioDeviceStart(fDeviceID, MeasureCallback)) != noErr) {
  922. jack_error("Cannot start MeasureCallback");
  923. printError(err);
  924. return -1;
  925. }
  926. return 0;
  927. }
  928. int JackCoreAudioDriver::Stop()
  929. {
  930. AudioDeviceStop(fDeviceID, MeasureCallback);
  931. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  932. JackLog("JackCoreAudioDriver::Stop\n");
  933. return (AudioOutputUnitStop(fAUHAL) == noErr) ? 0 : -1;
  934. }
  935. int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  936. {
  937. OSStatus err;
  938. UInt32 outSize = sizeof(UInt32);
  939. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
  940. if (err != noErr) {
  941. jack_error("Cannot set buffer size %ld", buffer_size);
  942. printError(err);
  943. return -1;
  944. }
  945. JackAudioDriver::SetBufferSize(buffer_size); // never fails
  946. // Input buffers do no change : prepare them only once
  947. for (int i = 0; i < fCaptureChannels; i++) {
  948. fJackInputData->mBuffers[i].mNumberChannels = 1;
  949. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  950. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  951. }
  952. return 0;
  953. }
  954. } // end of namespace
  955. #ifdef __cplusplus
  956. extern "C"
  957. {
  958. #endif
  959. jack_driver_desc_t* driver_get_descriptor() {
  960. jack_driver_desc_t *desc;
  961. unsigned int i;
  962. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  963. strcpy(desc->name, "coreaudio");
  964. desc->nparams = 13;
  965. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  966. i = 0;
  967. strcpy(desc->params[i].name, "channels");
  968. desc->params[i].character = 'c';
  969. desc->params[i].type = JackDriverParamInt;
  970. desc->params[i].value.ui = 0;
  971. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  972. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  973. i++;
  974. strcpy(desc->params[i].name, "inchannels");
  975. desc->params[i].character = 'i';
  976. desc->params[i].type = JackDriverParamInt;
  977. desc->params[i].value.ui = 0;
  978. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  979. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  980. i++;
  981. strcpy(desc->params[i].name, "outchannels");
  982. desc->params[i].character = 'o';
  983. desc->params[i].type = JackDriverParamInt;
  984. desc->params[i].value.ui = 0;
  985. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  986. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  987. i++;
  988. strcpy(desc->params[i].name, "capture");
  989. desc->params[i].character = 'C';
  990. desc->params[i].type = JackDriverParamString;
  991. strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
  992. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
  993. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  994. i++;
  995. strcpy(desc->params[i].name, "playback");
  996. desc->params[i].character = 'P';
  997. desc->params[i].type = JackDriverParamString;
  998. strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
  999. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
  1000. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1001. i++;
  1002. strcpy (desc->params[i].name, "monitor");
  1003. desc->params[i].character = 'm';
  1004. desc->params[i].type = JackDriverParamBool;
  1005. desc->params[i].value.i = 0;
  1006. strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
  1007. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1008. i++;
  1009. strcpy(desc->params[i].name, "duplex");
  1010. desc->params[i].character = 'D';
  1011. desc->params[i].type = JackDriverParamBool;
  1012. desc->params[i].value.i = TRUE;
  1013. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  1014. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1015. i++;
  1016. strcpy(desc->params[i].name, "rate");
  1017. desc->params[i].character = 'r';
  1018. desc->params[i].type = JackDriverParamUInt;
  1019. desc->params[i].value.ui = 44100U;
  1020. strcpy(desc->params[i].short_desc, "Sample rate");
  1021. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1022. i++;
  1023. strcpy(desc->params[i].name, "period");
  1024. desc->params[i].character = 'p';
  1025. desc->params[i].type = JackDriverParamUInt;
  1026. desc->params[i].value.ui = 128U;
  1027. strcpy(desc->params[i].short_desc, "Frames per period");
  1028. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1029. i++;
  1030. strcpy(desc->params[i].name, "device");
  1031. desc->params[i].character = 'd';
  1032. desc->params[i].type = JackDriverParamString;
  1033. desc->params[i].value.ui = 128U;
  1034. strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
  1035. strcpy(desc->params[i].short_desc, "CoreAudio device name");
  1036. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1037. i++;
  1038. strcpy(desc->params[i].name, "input-latency");
  1039. desc->params[i].character = 'I';
  1040. desc->params[i].type = JackDriverParamUInt;
  1041. desc->params[i].value.i = 0;
  1042. strcpy(desc->params[i].short_desc, "Extra input latency");
  1043. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1044. i++;
  1045. strcpy(desc->params[i].name, "output-latency");
  1046. desc->params[i].character = 'O';
  1047. desc->params[i].type = JackDriverParamUInt;
  1048. desc->params[i].value.i = 0;
  1049. strcpy(desc->params[i].short_desc, "Extra output latency");
  1050. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1051. i++;
  1052. strcpy(desc->params[i].name, "list-devices");
  1053. desc->params[i].character = 'l';
  1054. desc->params[i].type = JackDriverParamBool;
  1055. desc->params[i].value.i = TRUE;
  1056. strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
  1057. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1058. return desc;
  1059. }
  1060. Jack::JackDriverClientInterface* driver_initialize(Jack::JackEngine* engine, Jack::JackSynchro** table, const JSList* params) {
  1061. jack_nframes_t srate = 44100;
  1062. jack_nframes_t frames_per_interrupt = 128;
  1063. int capture = FALSE;
  1064. int playback = FALSE;
  1065. int chan_in = 0;
  1066. int chan_out = 0;
  1067. bool monitor = false;
  1068. char* capture_pcm_name = "";
  1069. char* playback_pcm_name = "";
  1070. const JSList *node;
  1071. const jack_driver_param_t *param;
  1072. jack_nframes_t systemic_input_latency = 0;
  1073. jack_nframes_t systemic_output_latency = 0;
  1074. for (node = params; node; node = jack_slist_next(node)) {
  1075. param = (const jack_driver_param_t *) node->data;
  1076. switch (param->character) {
  1077. case 'd':
  1078. capture_pcm_name = strdup(param->value.str);
  1079. playback_pcm_name = strdup(param->value.str);
  1080. break;
  1081. case 'D':
  1082. capture = TRUE;
  1083. playback = TRUE;
  1084. break;
  1085. case 'c':
  1086. chan_in = chan_out = (int) param->value.ui;
  1087. break;
  1088. case 'i':
  1089. chan_in = (int) param->value.ui;
  1090. break;
  1091. case 'o':
  1092. chan_out = (int) param->value.ui;
  1093. break;
  1094. case 'C':
  1095. capture = TRUE;
  1096. if (strcmp(param->value.str, "none") != 0) {
  1097. capture_pcm_name = strdup(param->value.str);
  1098. }
  1099. break;
  1100. case 'P':
  1101. playback = TRUE;
  1102. if (strcmp(param->value.str, "none") != 0) {
  1103. playback_pcm_name = strdup(param->value.str);
  1104. }
  1105. break;
  1106. case 'm':
  1107. monitor = param->value.i;
  1108. break;
  1109. case 'r':
  1110. srate = param->value.ui;
  1111. break;
  1112. case 'p':
  1113. frames_per_interrupt = (unsigned int) param->value.ui;
  1114. break;
  1115. case 'I':
  1116. systemic_input_latency = param->value.ui;
  1117. break;
  1118. case 'O':
  1119. systemic_output_latency = param->value.ui;
  1120. break;
  1121. case 'l':
  1122. Jack::DisplayDeviceNames();
  1123. break;
  1124. }
  1125. }
  1126. /* duplex is the default */
  1127. if (!capture && !playback) {
  1128. capture = TRUE;
  1129. playback = TRUE;
  1130. }
  1131. Jack::JackDriverClientInterface* driver = new Jack::JackCoreAudioDriver("coreaudio", engine, table);
  1132. if (driver->Open(frames_per_interrupt, srate, capture, playback, chan_in, chan_out, monitor, capture_pcm_name, playback_pcm_name, systemic_input_latency, systemic_output_latency) == 0) {
  1133. return driver;
  1134. } else {
  1135. delete driver;
  1136. return NULL;
  1137. }
  1138. }
  1139. #ifdef __cplusplus
  1140. }
  1141. #endif