Aggregate device code added to JackCoreAudioAdapter.
[jack2.git] / macosx / coreaudio / JackCoreAudioDriver.cpp
blobec73428e913486bd5a873df45cd94ae4f3050775
1 /*
2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackCoreAudioDriver.h"
21 #include "JackEngineControl.h"
22 #include "JackMachThread.h"
23 #include "JackGraphManager.h"
24 #include "JackError.h"
25 #include "JackClientControl.h"
26 #include "JackDriverLoader.h"
27 #include "JackGlobals.h"
28 #include "JackTools.h"
29 #include "JackCompilerDeps.h"
31 #include <iostream>
32 #include <CoreServices/CoreServices.h>
33 #include <CoreFoundation/CFNumber.h>
35 namespace Jack
38 static void Print4CharCode(const char* msg, long c)
40 UInt32 __4CC_number = (c);
41 char __4CC_string[5];
42 *((SInt32*)__4CC_string) = EndianU32_NtoB(__4CC_number);
43 __4CC_string[4] = 0;
44 jack_log("%s'%s'", (msg), __4CC_string);
47 static void printError(OSStatus err)
49 switch (err) {
50 case kAudioHardwareNoError:
51 jack_log("error code : kAudioHardwareNoError");
52 break;
53 case kAudioConverterErr_FormatNotSupported:
54 jack_log("error code : kAudioConverterErr_FormatNotSupported");
55 break;
56 case kAudioConverterErr_OperationNotSupported:
57 jack_log("error code : kAudioConverterErr_OperationNotSupported");
58 break;
59 case kAudioConverterErr_PropertyNotSupported:
60 jack_log("error code : kAudioConverterErr_PropertyNotSupported");
61 break;
62 case kAudioConverterErr_InvalidInputSize:
63 jack_log("error code : kAudioConverterErr_InvalidInputSize");
64 break;
65 case kAudioConverterErr_InvalidOutputSize:
66 jack_log("error code : kAudioConverterErr_InvalidOutputSize");
67 break;
68 case kAudioConverterErr_UnspecifiedError:
69 jack_log("error code : kAudioConverterErr_UnspecifiedError");
70 break;
71 case kAudioConverterErr_BadPropertySizeError:
72 jack_log("error code : kAudioConverterErr_BadPropertySizeError");
73 break;
74 case kAudioConverterErr_RequiresPacketDescriptionsError:
75 jack_log("error code : kAudioConverterErr_RequiresPacketDescriptionsError");
76 break;
77 case kAudioConverterErr_InputSampleRateOutOfRange:
78 jack_log("error code : kAudioConverterErr_InputSampleRateOutOfRange");
79 break;
80 case kAudioConverterErr_OutputSampleRateOutOfRange:
81 jack_log("error code : kAudioConverterErr_OutputSampleRateOutOfRange");
82 break;
83 case kAudioHardwareNotRunningError:
84 jack_log("error code : kAudioHardwareNotRunningError");
85 break;
86 case kAudioHardwareUnknownPropertyError:
87 jack_log("error code : kAudioHardwareUnknownPropertyError");
88 break;
89 case kAudioHardwareIllegalOperationError:
90 jack_log("error code : kAudioHardwareIllegalOperationError");
91 break;
92 case kAudioHardwareBadDeviceError:
93 jack_log("error code : kAudioHardwareBadDeviceError");
94 break;
95 case kAudioHardwareBadStreamError:
96 jack_log("error code : kAudioHardwareBadStreamError");
97 break;
98 case kAudioDeviceUnsupportedFormatError:
99 jack_log("error code : kAudioDeviceUnsupportedFormatError");
100 break;
101 case kAudioDevicePermissionsError:
102 jack_log("error code : kAudioDevicePermissionsError");
103 break;
104 case kAudioHardwareBadObjectError:
105 jack_log("error code : kAudioHardwareBadObjectError");
106 break;
107 case kAudioHardwareUnsupportedOperationError:
108 jack_log("error code : kAudioHardwareUnsupportedOperationError");
109 break;
110 default:
111 Print4CharCode("error code : unknown", err);
112 break;
116 static OSStatus DisplayDeviceNames()
118 UInt32 size;
119 Boolean isWritable;
120 int i, deviceNum;
121 OSStatus err;
122 CFStringRef UIname;
124 err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
125 if (err != noErr)
126 return err;
128 deviceNum = size / sizeof(AudioDeviceID);
129 AudioDeviceID devices[deviceNum];
131 err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
132 if (err != noErr)
133 return err;
135 for (i = 0; i < deviceNum; i++) {
136 char device_name[256];
137 char internal_name[256];
139 size = sizeof(CFStringRef);
140 UIname = NULL;
141 err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
142 if (err == noErr) {
143 CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
144 } else {
145 goto error;
148 size = 256;
149 err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
150 if (err != noErr)
151 return err;
153 jack_info("Device name = \'%s\', internal_name = \'%s\' (to be used as -C, -P, or -d parameter)", device_name, internal_name);
156 return noErr;
158 error:
159 if (UIname != NULL)
160 CFRelease(UIname);
161 return err;
164 static CFStringRef GetDeviceName(AudioDeviceID id)
166 UInt32 size = sizeof(CFStringRef);
167 CFStringRef UIname;
168 OSStatus err = AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
169 return (err == noErr) ? UIname : NULL;
172 OSStatus JackCoreAudioDriver::Render(void *inRefCon,
173 AudioUnitRenderActionFlags *ioActionFlags,
174 const AudioTimeStamp *inTimeStamp,
175 UInt32 inBusNumber,
176 UInt32 inNumberFrames,
177 AudioBufferList *ioData)
179 JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inRefCon;
180 driver->fActionFags = ioActionFlags;
181 driver->fCurrentTime = (AudioTimeStamp *)inTimeStamp;
182 driver->fDriverOutputData = ioData;
183 driver->CycleTakeBeginTime();
184 return driver->Process();
187 int JackCoreAudioDriver::Read()
189 AudioUnitRender(fAUHAL, fActionFags, fCurrentTime, 1, fEngineControl->fBufferSize, fJackInputData);
190 return 0;
193 int JackCoreAudioDriver::Write()
195 for (int i = 0; i < fPlaybackChannels; i++) {
196 if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
197 float* buffer = GetOutputBuffer(i);
198 int size = sizeof(float) * fEngineControl->fBufferSize;
199 memcpy((float*)fDriverOutputData->mBuffers[i].mData, buffer, size);
200 // Monitor ports
201 if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
202 memcpy(GetMonitorBuffer(i), buffer, size);
203 } else {
204 memset((float*)fDriverOutputData->mBuffers[i].mData, 0, sizeof(float) * fEngineControl->fBufferSize);
207 return 0;
210 // Will run only once
211 OSStatus JackCoreAudioDriver::MeasureCallback(AudioDeviceID inDevice,
212 const AudioTimeStamp* inNow,
213 const AudioBufferList* inInputData,
214 const AudioTimeStamp* inInputTime,
215 AudioBufferList* outOutputData,
216 const AudioTimeStamp* inOutputTime,
217 void* inClientData)
219 JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
220 AudioDeviceStop(driver->fDeviceID, MeasureCallback);
222 jack_log("JackCoreAudioDriver::MeasureCallback called");
223 JackMachThread::GetParams(pthread_self(), &driver->fEngineControl->fPeriod, &driver->fEngineControl->fComputation, &driver->fEngineControl->fConstraint);
225 if (driver->fComputationGrain > 0) {
226 jack_log("JackCoreAudioDriver::MeasureCallback : RT thread computation setup to %d percent of period", int(driver->fComputationGrain * 100));
227 driver->fEngineControl->fComputation = driver->fEngineControl->fPeriod * driver->fComputationGrain;
230 // Setup threadded based log function
231 set_threaded_log_function();
232 return noErr;
235 OSStatus JackCoreAudioDriver::SRNotificationCallback(AudioDeviceID inDevice,
236 UInt32 inChannel,
237 Boolean isInput,
238 AudioDevicePropertyID inPropertyID,
239 void* inClientData)
241 JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
243 switch (inPropertyID) {
245 case kAudioDevicePropertyNominalSampleRate: {
246 jack_log("JackCoreAudioDriver::SRNotificationCallback kAudioDevicePropertyNominalSampleRate");
247 driver->fState = true;
248 break;
252 return noErr;
255 // A better implementation would possibly try to recover in case of hardware device change (see HALLAB HLFilePlayerWindowControllerAudioDevicePropertyListenerProc code)
256 OSStatus JackCoreAudioDriver::DeviceNotificationCallback(AudioDeviceID inDevice,
257 UInt32 inChannel,
258 Boolean isInput,
259 AudioDevicePropertyID inPropertyID,
260 void* inClientData)
262 JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
264 switch (inPropertyID) {
266 case kAudioDeviceProcessorOverload: {
267 jack_error("JackCoreAudioDriver::DeviceNotificationCallback kAudioDeviceProcessorOverload");
268 jack_time_t cur_time = GetMicroSeconds();
269 driver->NotifyXRun(cur_time, float(cur_time - driver->fBeginDateUst)); // Better this value than nothing...
270 break;
273 case kAudioDevicePropertyStreamConfiguration: {
274 jack_error("Cannot handle kAudioDevicePropertyStreamConfiguration : server will quit...");
275 driver->NotifyFailure(JackBackendError, "Another application has changed the device configuration."); // Message length limited to JACK_MESSAGE_SIZE
276 driver->CloseAUHAL();
277 kill(JackTools::GetPID(), SIGINT);
278 return kAudioHardwareUnsupportedOperationError;
281 case kAudioDevicePropertyNominalSampleRate: {
282 jack_error("Cannot handle kAudioDevicePropertyNominalSampleRate : server will quit...");
283 driver->NotifyFailure(JackBackendError, "Another application has changed the sample rate."); // Message length limited to JACK_MESSAGE_SIZE
284 driver->CloseAUHAL();
285 kill(JackTools::GetPID(), SIGINT);
286 return kAudioHardwareUnsupportedOperationError;
290 return noErr;
293 OSStatus JackCoreAudioDriver::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
295 UInt32 size = sizeof(AudioValueTranslation);
296 CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
297 AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
299 if (inIUD == NULL) {
300 return kAudioHardwareUnspecifiedError;
301 } else {
302 OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
303 CFRelease(inIUD);
304 jack_log("GetDeviceIDFromUID %s %ld", UID, *id);
305 return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
309 OSStatus JackCoreAudioDriver::GetDefaultDevice(AudioDeviceID* id)
311 OSStatus res;
312 UInt32 theSize = sizeof(UInt32);
313 AudioDeviceID inDefault;
314 AudioDeviceID outDefault;
316 if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
317 return res;
319 if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
320 return res;
322 jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
324 // Get the device only if default input and ouput are the same
325 if (inDefault == outDefault) {
326 *id = inDefault;
327 return noErr;
328 } else {
329 jack_error("Default input and output devices are not the same !!");
330 return kAudioHardwareBadDeviceError;
334 OSStatus JackCoreAudioDriver::GetDefaultInputDevice(AudioDeviceID* id)
336 OSStatus res;
337 UInt32 theSize = sizeof(UInt32);
338 AudioDeviceID inDefault;
340 if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
341 return res;
343 jack_log("GetDefaultInputDevice: input = %ld ", inDefault);
344 *id = inDefault;
345 return noErr;
348 OSStatus JackCoreAudioDriver::GetDefaultOutputDevice(AudioDeviceID* id)
350 OSStatus res;
351 UInt32 theSize = sizeof(UInt32);
352 AudioDeviceID outDefault;
354 if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
355 return res;
357 jack_log("GetDefaultOutputDevice: output = %ld", outDefault);
358 *id = outDefault;
359 return noErr;
362 OSStatus JackCoreAudioDriver::GetDeviceNameFromID(AudioDeviceID id, char* name)
364 UInt32 size = 256;
365 return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
368 OSStatus JackCoreAudioDriver::GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput)
370 OSStatus err = noErr;
371 UInt32 outSize;
372 Boolean outWritable;
373 AudioBufferList* bufferList = 0;
375 channelCount = 0;
376 err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
377 if (err == noErr) {
378 bufferList = (AudioBufferList*)malloc(outSize);
379 err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
380 if (err == noErr) {
381 for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++)
382 channelCount += bufferList->mBuffers[i].mNumberChannels;
385 if (bufferList)
386 free(bufferList);
389 return err;
392 JackCoreAudioDriver::JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
393 : JackAudioDriver(name, alias, engine, table), fJackInputData(NULL), fDriverOutputData(NULL), fPluginID(0), fState(false), fIOUsage(1.f),fComputationGrain(-1.f)
396 JackCoreAudioDriver::~JackCoreAudioDriver()
399 OSStatus JackCoreAudioDriver::DestroyAggregateDevice()
401 OSStatus osErr = noErr;
402 AudioObjectPropertyAddress pluginAOPA;
403 pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice;
404 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
405 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
406 UInt32 outDataSize;
408 osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
409 if (osErr != noErr) {
410 jack_error("JackCoreAudioDriver::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
411 printError(osErr);
412 return osErr;
415 osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID);
416 if (osErr != noErr) {
417 jack_error("JackCoreAudioDriver::DestroyAggregateDevice : AudioObjectGetPropertyData error");
418 printError(osErr);
419 return osErr;
422 return noErr;
425 OSStatus JackCoreAudioDriver::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, AudioDeviceID* outAggregateDevice)
427 OSStatus osErr = noErr;
428 UInt32 outSize;
429 Boolean outWritable;
431 //---------------------------------------------------------------------------
432 // Start to create a new aggregate by getting the base audio hardware plugin
433 //---------------------------------------------------------------------------
435 jack_info("Separated input and output devices, so create a private aggregate device to handle them...");
437 osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable);
438 if (osErr != noErr)
439 return osErr;
441 AudioValueTranslation pluginAVT;
443 CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
445 pluginAVT.mInputData = &inBundleRef;
446 pluginAVT.mInputDataSize = sizeof(inBundleRef);
447 pluginAVT.mOutputData = &fPluginID;
448 pluginAVT.mOutputDataSize = sizeof(fPluginID);
450 osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT);
451 if (osErr != noErr)
452 return osErr;
454 //-------------------------------------------------
455 // Create a CFDictionary for our aggregate device
456 //-------------------------------------------------
458 CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
460 CFStringRef AggregateDeviceNameRef = CFSTR("JackDuplex");
461 CFStringRef AggregateDeviceUIDRef = CFSTR("com.grame.JackDuplex");
463 // add the name of the device to the dictionary
464 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
466 // add our choice of UID for the aggregate device to the dictionary
467 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
469 // add a "private aggregate key" to the dictionary
470 int value = 1;
471 CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
472 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
474 //-------------------------------------------------
475 // Create a CFMutableArray for our sub-device list
476 //-------------------------------------------------
478 CFStringRef captureDeviceUID = GetDeviceName(captureDeviceID);
479 CFStringRef playbackDeviceUID = GetDeviceName(playbackDeviceID);
481 if (captureDeviceUID == NULL || playbackDeviceUID == NULL)
482 return -1;
484 // we need to append the UID for each device to a CFMutableArray, so create one here
485 CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
487 // two sub-devices in this example, so append the sub-device's UID to the CFArray
488 CFArrayAppendValue(subDevicesArray, captureDeviceUID);
489 CFArrayAppendValue(subDevicesArray, playbackDeviceUID);
491 //-----------------------------------------------------------------------
492 // Feed the dictionary to the plugin, to create a blank aggregate device
493 //-----------------------------------------------------------------------
495 AudioObjectPropertyAddress pluginAOPA;
496 pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
497 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
498 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
499 UInt32 outDataSize;
501 osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
502 if (osErr != noErr)
503 return osErr;
505 osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, outAggregateDevice);
506 if (osErr != noErr)
507 return osErr;
509 // pause for a bit to make sure that everything completed correctly
510 // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
511 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
513 //-------------------------
514 // Set the sub-device list
515 //-------------------------
517 pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
518 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
519 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
520 outDataSize = sizeof(CFMutableArrayRef);
521 osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
522 if (osErr != noErr)
523 return osErr;
525 // pause again to give the changes time to take effect
526 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
528 //-----------------------
529 // Set the master device
530 //-----------------------
532 // set the master device manually (this is the device which will act as the master clock for the aggregate device)
533 // pass in the UID of the device you want to use
534 pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
535 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
536 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
537 outDataSize = sizeof(CFStringRef);
538 osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID); // capture is master...
539 if (osErr != noErr)
540 return osErr;
542 // pause again to give the changes time to take effect
543 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
545 //----------
546 // Clean up
547 //----------
549 CFRelease(AggregateDeviceNumberRef);
551 // release the CF objects we have created - we don't need them any more
552 CFRelease(aggDeviceDict);
553 CFRelease(subDevicesArray);
555 // release the device UID
556 CFRelease(captureDeviceUID);
557 CFRelease(playbackDeviceUID);
559 jack_log("New aggregate device %ld", *outAggregateDevice);
560 return noErr;
563 int JackCoreAudioDriver::SetupDevices(const char* capture_driver_uid, const char* playback_driver_uid, char* capture_driver_name, char* playback_driver_name)
565 capture_driver_name[0] = 0;
566 playback_driver_name[0] = 0;
568 // Duplex
569 if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
570 jack_log("JackCoreAudioDriver::Open duplex");
572 // Same device for capture and playback...
573 if (strcmp(capture_driver_uid, playback_driver_uid) == 0) {
575 if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
576 jack_log("Will take default in/out");
577 if (GetDefaultDevice(&fDeviceID) != noErr) {
578 jack_error("Cannot open default device");
579 return -1;
582 if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
583 jack_error("Cannot get device name from device ID");
584 return -1;
587 } else {
589 // Creates aggregate device
590 AudioDeviceID captureID, playbackID;
591 if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr)
592 return -1;
593 if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr)
594 return -1;
595 if (CreateAggregateDevice(captureID, playbackID, &fDeviceID) != noErr)
596 return -1;
599 // Capture only
600 } else if (strcmp(capture_driver_uid, "") != 0) {
601 jack_log("JackCoreAudioDriver::Open capture only");
602 if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
603 jack_log("Will take default input");
604 if (GetDefaultInputDevice(&fDeviceID) != noErr) {
605 jack_error("Cannot open default device");
606 return -1;
609 if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
610 jack_error("Cannot get device name from device ID");
611 return -1;
614 // Playback only
615 } else if (strcmp(playback_driver_uid, "") != 0) {
616 jack_log("JackCoreAudioDriver::Open playback only");
617 if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
618 jack_log("Will take default output");
619 if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
620 jack_error("Cannot open default device");
621 return -1;
624 if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
625 jack_error("Cannot get device name from device ID");
626 return -1;
629 // Use default driver in duplex mode
630 } else {
631 jack_log("JackCoreAudioDriver::Open default driver");
632 if (GetDefaultDevice(&fDeviceID) != noErr) {
633 jack_error("Cannot open default device");
634 return -1;
636 if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
637 jack_error("Cannot get device name from device ID");
638 return -1;
642 return 0;
646 Return the max possible input channels in in_nChannels and output channels in out_nChannels.
648 int JackCoreAudioDriver::SetupChannels(bool capturing, bool playing, int& inchannels, int& outchannels, int& in_nChannels, int& out_nChannels, bool strict)
650 OSStatus err = noErr;
652 if (capturing) {
653 err = GetTotalChannels(fDeviceID, in_nChannels, true);
654 if (err != noErr) {
655 jack_error("Cannot get input channel number");
656 printError(err);
657 return -1;
658 } else {
659 jack_log("Max input channels : %d", in_nChannels);
663 if (playing) {
664 err = GetTotalChannels(fDeviceID, out_nChannels, false);
665 if (err != noErr) {
666 jack_error("Cannot get output channel number");
667 printError(err);
668 return -1;
669 } else {
670 jack_log("Max output channels : %d", out_nChannels);
674 if (inchannels > in_nChannels) {
675 jack_error("This device hasn't required input channels inchannels = %d in_nChannels = %d", inchannels, in_nChannels);
676 if (strict)
677 return -1;
680 if (outchannels > out_nChannels) {
681 jack_error("This device hasn't required output channels outchannels = %d out_nChannels = %d", outchannels, out_nChannels);
682 if (strict)
683 return -1;
686 if (inchannels == 0) {
687 jack_log("Setup max in channels = %d", in_nChannels);
688 inchannels = in_nChannels;
691 if (outchannels == 0) {
692 jack_log("Setup max out channels = %d", out_nChannels);
693 outchannels = out_nChannels;
696 return 0;
699 int JackCoreAudioDriver::SetupBufferSizeAndSampleRate(jack_nframes_t buffer_size, jack_nframes_t samplerate)
701 OSStatus err = noErr;
702 UInt32 outSize;
703 Float64 sampleRate;
705 // Setting buffer size
706 outSize = sizeof(UInt32);
707 err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
708 if (err != noErr) {
709 jack_error("Cannot set buffer size %ld", buffer_size);
710 printError(err);
711 return -1;
714 // Get sample rate
715 outSize = sizeof(Float64);
716 err = AudioDeviceGetProperty(fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
717 if (err != noErr) {
718 jack_error("Cannot get current sample rate");
719 printError(err);
720 return -1;
723 // If needed, set new sample rate
724 if (samplerate != (jack_nframes_t)sampleRate) {
725 sampleRate = (Float64)samplerate;
727 // To get SR change notification
728 err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
729 if (err != noErr) {
730 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
731 printError(err);
732 return -1;
734 err = AudioDeviceSetProperty(fDeviceID, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
735 if (err != noErr) {
736 jack_error("Cannot set sample rate = %ld", samplerate);
737 printError(err);
738 return -1;
741 // Waiting for SR change notification
742 int count = 0;
743 while (!fState && count++ < 100) {
744 usleep(100000);
745 jack_log("Wait count = %d", count);
748 // Remove SR change notification
749 AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
752 return 0;
755 int JackCoreAudioDriver::OpenAUHAL(bool capturing,
756 bool playing,
757 int inchannels,
758 int outchannels,
759 int in_nChannels,
760 int out_nChannels,
761 jack_nframes_t buffer_size,
762 jack_nframes_t samplerate,
763 bool strict)
765 ComponentResult err1;
766 UInt32 enableIO;
767 AudioStreamBasicDescription srcFormat, dstFormat;
769 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);
771 if (inchannels == 0 && outchannels == 0) {
772 jack_error("No input and output channels...");
773 return -1;
776 // AUHAL
777 ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
778 Component HALOutput = FindNextComponent(NULL, &cd);
780 err1 = OpenAComponent(HALOutput, &fAUHAL);
781 if (err1 != noErr) {
782 jack_error("Error calling OpenAComponent");
783 printError(err1);
784 return -1;
787 err1 = AudioUnitInitialize(fAUHAL);
788 if (err1 != noErr) {
789 jack_error("Cannot initialize AUHAL unit");
790 printError(err1);
791 return -1;
794 // Start I/O
795 if (capturing && inchannels > 0) {
796 enableIO = 1;
797 jack_log("Setup AUHAL input on");
798 } else {
799 enableIO = 0;
800 jack_log("Setup AUHAL input off");
803 err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
804 if (err1 != noErr) {
805 jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
806 printError(err1);
807 if (strict)
808 return -1;
811 if (playing && outchannels > 0) {
812 enableIO = 1;
813 jack_log("Setup AUHAL output on");
814 } else {
815 enableIO = 0;
816 jack_log("Setup AUHAL output off");
819 err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
820 if (err1 != noErr) {
821 jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
822 printError(err1);
823 if (strict)
824 return -1;
827 AudioDeviceID currAudioDeviceID;
828 UInt32 size = sizeof(AudioDeviceID);
829 err1 = AudioUnitGetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &currAudioDeviceID, &size);
830 if (err1 != noErr) {
831 jack_error("Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
832 printError(err1);
833 if (strict)
834 return -1;
835 } else {
836 jack_log("AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID);
839 // Setup up choosen device, in both input and output cases
840 err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
841 if (err1 != noErr) {
842 jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
843 printError(err1);
844 if (strict)
845 return -1;
848 // Set buffer size
849 if (capturing && inchannels > 0) {
850 err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&buffer_size, sizeof(UInt32));
851 if (err1 != noErr) {
852 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
853 printError(err1);
854 if (strict)
855 return -1;
859 if (playing && outchannels > 0) {
860 err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&buffer_size, sizeof(UInt32));
861 if (err1 != noErr) {
862 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
863 printError(err1);
864 if (strict)
865 return -1;
869 // Setup channel map
870 if (capturing && inchannels > 0 && inchannels < in_nChannels) {
871 SInt32 chanArr[in_nChannels];
872 for (int i = 0; i < in_nChannels; i++) {
873 chanArr[i] = -1;
875 for (int i = 0; i < inchannels; i++) {
876 chanArr[i] = i;
878 AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
879 if (err1 != noErr) {
880 jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
881 printError(err1);
885 if (playing && outchannels > 0 && outchannels < out_nChannels) {
886 SInt32 chanArr[out_nChannels];
887 for (int i = 0; i < out_nChannels; i++) {
888 chanArr[i] = -1;
890 for (int i = 0; i < outchannels; i++) {
891 chanArr[i] = i;
893 err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
894 if (err1 != noErr) {
895 jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
896 printError(err1);
900 // Setup stream converters
901 jack_log("Setup AUHAL input stream converter SR = %ld", samplerate);
902 srcFormat.mSampleRate = samplerate;
903 srcFormat.mFormatID = kAudioFormatLinearPCM;
904 srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
905 srcFormat.mBytesPerPacket = sizeof(float);
906 srcFormat.mFramesPerPacket = 1;
907 srcFormat.mBytesPerFrame = sizeof(float);
908 srcFormat.mChannelsPerFrame = outchannels;
909 srcFormat.mBitsPerChannel = 32;
911 err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(AudioStreamBasicDescription));
912 if (err1 != noErr) {
913 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
914 printError(err1);
917 jack_log("Setup AUHAL output stream converter SR = %ld", samplerate);
918 dstFormat.mSampleRate = samplerate;
919 dstFormat.mFormatID = kAudioFormatLinearPCM;
920 dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
921 dstFormat.mBytesPerPacket = sizeof(float);
922 dstFormat.mFramesPerPacket = 1;
923 dstFormat.mBytesPerFrame = sizeof(float);
924 dstFormat.mChannelsPerFrame = inchannels;
925 dstFormat.mBitsPerChannel = 32;
927 err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
928 if (err1 != noErr) {
929 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
930 printError(err1);
933 // Setup callbacks
934 if (inchannels > 0 && outchannels == 0) {
935 AURenderCallbackStruct output;
936 output.inputProc = Render;
937 output.inputProcRefCon = this;
938 err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
939 if (err1 != noErr) {
940 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
941 printError(err1);
942 return -1;
944 } else {
945 AURenderCallbackStruct output;
946 output.inputProc = Render;
947 output.inputProcRefCon = this;
948 err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
949 if (err1 != noErr) {
950 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
951 printError(err1);
952 return -1;
956 return 0;
959 int JackCoreAudioDriver::SetupBuffers(int inchannels)
961 // Prepare buffers
962 fJackInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
963 if (fJackInputData == 0) {
964 jack_error("Cannot allocate memory for input buffers");
965 return -1;
967 fJackInputData->mNumberBuffers = inchannels;
968 for (int i = 0; i < fCaptureChannels; i++) {
969 fJackInputData->mBuffers[i].mNumberChannels = 1;
970 fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
972 return 0;
975 void JackCoreAudioDriver::DisposeBuffers()
977 if (fJackInputData) {
978 free(fJackInputData);
979 fJackInputData = 0;
983 void JackCoreAudioDriver::CloseAUHAL()
985 AudioUnitUninitialize(fAUHAL);
986 CloseComponent(fAUHAL);
989 int JackCoreAudioDriver::AddListeners()
991 OSStatus err = noErr;
993 // Add listeners
994 err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
995 if (err != noErr) {
996 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
997 printError(err);
998 return -1;
1001 err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback, this);
1002 if (err != noErr) {
1003 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
1004 printError(err);
1005 return -1;
1008 err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback, this);
1009 if (err != noErr) {
1010 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
1011 printError(err);
1012 return -1;
1015 err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback, this);
1016 if (err != noErr) {
1017 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
1018 printError(err);
1019 return -1;
1022 err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
1023 if (err != noErr) {
1024 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
1025 printError(err);
1026 return -1;
1029 err = AudioDeviceAddPropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
1030 if (err != noErr) {
1031 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
1032 printError(err);
1033 return -1;
1036 if (!fEngineControl->fSyncMode && fIOUsage != 1.f) {
1037 UInt32 outSize = sizeof(float);
1038 err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyIOCycleUsage, outSize, &fIOUsage);
1039 if (err != noErr) {
1040 jack_error("Error calling AudioDeviceSetProperty kAudioDevicePropertyIOCycleUsage");
1041 printError(err);
1045 return 0;
1048 void JackCoreAudioDriver::RemoveListeners()
1050 AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
1051 AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback);
1052 AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback);
1053 AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback);
1054 AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
1055 AudioDeviceRemovePropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
1058 int JackCoreAudioDriver::Open(jack_nframes_t buffer_size,
1059 jack_nframes_t samplerate,
1060 bool capturing,
1061 bool playing,
1062 int inchannels,
1063 int outchannels,
1064 bool monitor,
1065 const char* capture_driver_uid,
1066 const char* playback_driver_uid,
1067 jack_nframes_t capture_latency,
1068 jack_nframes_t playback_latency,
1069 int async_output_latency,
1070 int computation_grain)
1072 int in_nChannels = 0;
1073 int out_nChannels = 0;
1074 char capture_driver_name[256];
1075 char playback_driver_name[256];
1077 // Keep initial state
1078 fCapturing = capturing;
1079 fPlaying = playing;
1080 fInChannels = inchannels;
1081 fOutChannels = outchannels;
1082 fMonitor = monitor;
1083 strcpy(fCaptureUID, capture_driver_uid);
1084 strcpy(fPlaybackUID, playback_driver_uid);
1085 fCaptureLatency = capture_latency;
1086 fPlaybackLatency = playback_latency;
1087 fIOUsage = float(async_output_latency) / 100.f;
1088 fComputationGrain = float(computation_grain) / 100.f;
1090 SInt32 major;
1091 SInt32 minor;
1092 Gestalt(gestaltSystemVersionMajor, &major);
1093 Gestalt(gestaltSystemVersionMinor, &minor);
1095 // Starting with 10.6 systems, the HAL notification thread is created internally
1096 if (major == 10 && minor >=6) {
1097 CFRunLoopRef theRunLoop = NULL;
1098 AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
1099 OSStatus theError = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
1100 if (theError != noErr) {
1101 jack_error("JackCoreAudioDriver::Open kAudioHardwarePropertyRunLoop error");
1105 if (SetupDevices(capture_driver_uid, playback_driver_uid, capture_driver_name, playback_driver_name) < 0)
1106 return -1;
1108 // Generic JackAudioDriver Open
1109 if (JackAudioDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency) != 0)
1110 return -1;
1112 if (SetupChannels(capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, true) < 0)
1113 return -1;
1115 if (SetupBufferSizeAndSampleRate(buffer_size, samplerate) < 0)
1116 return -1;
1118 if (OpenAUHAL(capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, buffer_size, samplerate, true) < 0)
1119 goto error;
1121 if (capturing && inchannels > 0)
1122 if (SetupBuffers(inchannels) < 0)
1123 goto error;
1125 if (AddListeners() < 0)
1126 goto error;
1128 // Core driver may have changed the in/out values
1129 fCaptureChannels = inchannels;
1130 fPlaybackChannels = outchannels;
1131 return noErr;
1133 error:
1134 Close();
1135 return -1;
1138 int JackCoreAudioDriver::Close()
1140 jack_log("JackCoreAudioDriver::Close");
1141 Stop();
1142 JackAudioDriver::Close();
1143 RemoveListeners();
1144 DisposeBuffers();
1145 CloseAUHAL();
1146 if (fPluginID > 0)
1147 DestroyAggregateDevice();
1148 return 0;
1151 int JackCoreAudioDriver::Attach()
1153 OSStatus err;
1154 JackPort* port;
1155 jack_port_id_t port_index;
1156 UInt32 size;
1157 Boolean isWritable;
1158 char channel_name[64];
1159 char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
1160 char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
1161 unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
1163 jack_log("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
1165 for (int i = 0; i < fCaptureChannels; i++) {
1167 err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
1168 if (err != noErr)
1169 jack_log("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error ");
1170 if (err == noErr && size > 0) {
1171 err = AudioDeviceGetProperty(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
1172 if (err != noErr)
1173 jack_log("AudioDeviceGetProperty kAudioDevicePropertyChannelName error ");
1174 snprintf(alias, sizeof(alias) - 1, "%s:%s:out_%s%u", fAliasName, fCaptureDriverName, channel_name, i + 1);
1175 } else {
1176 snprintf(alias, sizeof(alias) - 1, "%s:%s:out%u", fAliasName, fCaptureDriverName, i + 1);
1179 snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
1181 if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
1182 jack_error("Cannot register port for %s", name);
1183 return -1;
1186 size = sizeof(UInt32);
1187 UInt32 value1 = 0;
1188 UInt32 value2 = 0;
1189 err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyLatency, &size, &value1);
1190 if (err != noErr)
1191 jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error ");
1192 err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);
1193 if (err != noErr)
1194 jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error ");
1196 port = fGraphManager->GetPort(port_index);
1197 port->SetAlias(alias);
1198 port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fCaptureLatency);
1199 fCapturePortList[i] = port_index;
1202 port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
1204 for (int i = 0; i < fPlaybackChannels; i++) {
1206 err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
1207 if (err != noErr)
1208 jack_log("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error ");
1209 if (err == noErr && size > 0) {
1210 err = AudioDeviceGetProperty(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
1211 if (err != noErr)
1212 jack_log("AudioDeviceGetProperty kAudioDevicePropertyChannelName error ");
1213 snprintf(alias, sizeof(alias) - 1, "%s:%s:in_%s%u", fAliasName, fPlaybackDriverName, channel_name, i + 1);
1214 } else {
1215 snprintf(alias, sizeof(alias) - 1, "%s:%s:in%u", fAliasName, fPlaybackDriverName, i + 1);
1218 snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
1220 if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
1221 jack_error("Cannot register port for %s", name);
1222 return -1;
1225 size = sizeof(UInt32);
1226 UInt32 value1 = 0;
1227 UInt32 value2 = 0;
1228 err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyLatency, &size, &value1);
1229 if (err != noErr)
1230 jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error ");
1231 err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);
1232 if (err != noErr)
1233 jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error ");
1235 port = fGraphManager->GetPort(port_index);
1236 port->SetAlias(alias);
1237 // Add more latency if "async" mode is used...
1238 port->SetLatency(fEngineControl->fBufferSize + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize * fIOUsage) + value1 + value2 + fPlaybackLatency);
1239 fPlaybackPortList[i] = port_index;
1241 // Monitor ports
1242 if (fWithMonitorPorts) {
1243 jack_log("Create monitor port ");
1244 snprintf(name, sizeof(name) - 1, "%s:monitor_%u", fClientControl.fName, i + 1);
1245 if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize)) == NO_PORT) {
1246 jack_error("Cannot register monitor port for %s", name);
1247 return -1;
1248 } else {
1249 port = fGraphManager->GetPort(port_index);
1250 port->SetAlias(alias);
1251 port->SetLatency(fEngineControl->fBufferSize);
1252 fMonitorPortList[i] = port_index;
1257 // Input buffers do no change : prepare them only once
1258 for (int i = 0; i < fCaptureChannels; i++) {
1259 fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
1262 return 0;
1265 int JackCoreAudioDriver::Start()
1267 jack_log("JackCoreAudioDriver::Start");
1268 JackAudioDriver::Start();
1270 #ifdef MAC_OS_X_VERSION_10_5
1271 OSStatus err = AudioDeviceCreateIOProcID(fDeviceID, MeasureCallback, this, &fMesureCallbackID);
1272 #else
1273 OSStatus err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
1274 #endif
1276 OSStatus err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
1278 if (err != noErr)
1279 return -1;
1281 err = AudioOutputUnitStart(fAUHAL);
1282 if (err != noErr)
1283 return -1;
1285 if ((err = AudioDeviceStart(fDeviceID, MeasureCallback)) != noErr) {
1286 jack_error("Cannot start MeasureCallback");
1287 printError(err);
1288 return -1;
1291 return 0;
1294 int JackCoreAudioDriver::Stop()
1296 jack_log("JackCoreAudioDriver::Stop");
1297 AudioDeviceStop(fDeviceID, MeasureCallback);
1299 #ifdef MAC_OS_X_VERSION_10_5
1300 AudioDeviceDestroyIOProcID(fDeviceID, fMesureCallbackID);
1301 #else
1302 AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
1303 #endif
1305 AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
1306 return (AudioOutputUnitStop(fAUHAL) == noErr) ? 0 : -1;
1309 int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
1311 OSStatus err;
1312 UInt32 outSize = sizeof(UInt32);
1314 err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
1315 if (err != noErr) {
1316 jack_error("Cannot set buffer size %ld", buffer_size);
1317 printError(err);
1318 return -1;
1321 JackAudioDriver::SetBufferSize(buffer_size); // never fails
1323 // Input buffers do no change : prepare them only once
1324 for (int i = 0; i < fCaptureChannels; i++) {
1325 fJackInputData->mBuffers[i].mNumberChannels = 1;
1326 fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
1327 fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
1330 return 0;
1333 } // end of namespace
1336 #ifdef __cplusplus
1337 extern "C"
1339 #endif
1341 SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
1343 jack_driver_desc_t *desc;
1344 unsigned int i;
1345 desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
1347 strcpy(desc->name, "coreaudio"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
1348 strcpy(desc->desc, "Apple CoreAudio API based audio backend"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
1350 desc->nparams = 15;
1351 desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
1353 i = 0;
1354 strcpy(desc->params[i].name, "channels");
1355 desc->params[i].character = 'c';
1356 desc->params[i].type = JackDriverParamInt;
1357 desc->params[i].value.ui = 0;
1358 strcpy(desc->params[i].short_desc, "Maximum number of channels");
1359 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1361 i++;
1362 strcpy(desc->params[i].name, "inchannels");
1363 desc->params[i].character = 'i';
1364 desc->params[i].type = JackDriverParamInt;
1365 desc->params[i].value.ui = 0;
1366 strcpy(desc->params[i].short_desc, "Maximum number of input channels");
1367 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1369 i++;
1370 strcpy(desc->params[i].name, "outchannels");
1371 desc->params[i].character = 'o';
1372 desc->params[i].type = JackDriverParamInt;
1373 desc->params[i].value.ui = 0;
1374 strcpy(desc->params[i].short_desc, "Maximum number of output channels");
1375 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1377 i++;
1378 strcpy(desc->params[i].name, "capture");
1379 desc->params[i].character = 'C';
1380 desc->params[i].type = JackDriverParamString;
1381 strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
1382 strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
1383 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1385 i++;
1386 strcpy(desc->params[i].name, "playback");
1387 desc->params[i].character = 'P';
1388 desc->params[i].type = JackDriverParamString;
1389 strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
1390 strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
1391 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1393 i++;
1394 strcpy (desc->params[i].name, "monitor");
1395 desc->params[i].character = 'm';
1396 desc->params[i].type = JackDriverParamBool;
1397 desc->params[i].value.i = 0;
1398 strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
1399 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1401 i++;
1402 strcpy(desc->params[i].name, "duplex");
1403 desc->params[i].character = 'D';
1404 desc->params[i].type = JackDriverParamBool;
1405 desc->params[i].value.i = TRUE;
1406 strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
1407 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1409 i++;
1410 strcpy(desc->params[i].name, "rate");
1411 desc->params[i].character = 'r';
1412 desc->params[i].type = JackDriverParamUInt;
1413 desc->params[i].value.ui = 44100U;
1414 strcpy(desc->params[i].short_desc, "Sample rate");
1415 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1417 i++;
1418 strcpy(desc->params[i].name, "period");
1419 desc->params[i].character = 'p';
1420 desc->params[i].type = JackDriverParamUInt;
1421 desc->params[i].value.ui = 128U;
1422 strcpy(desc->params[i].short_desc, "Frames per period");
1423 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1425 i++;
1426 strcpy(desc->params[i].name, "device");
1427 desc->params[i].character = 'd';
1428 desc->params[i].type = JackDriverParamString;
1429 strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
1430 strcpy(desc->params[i].short_desc, "CoreAudio device name");
1431 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1433 i++;
1434 strcpy(desc->params[i].name, "input-latency");
1435 desc->params[i].character = 'I';
1436 desc->params[i].type = JackDriverParamUInt;
1437 desc->params[i].value.i = 0;
1438 strcpy(desc->params[i].short_desc, "Extra input latency (frames)");
1439 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1441 i++;
1442 strcpy(desc->params[i].name, "output-latency");
1443 desc->params[i].character = 'O';
1444 desc->params[i].type = JackDriverParamUInt;
1445 desc->params[i].value.i = 0;
1446 strcpy(desc->params[i].short_desc, "Extra output latency (frames)");
1447 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1449 i++;
1450 strcpy(desc->params[i].name, "list-devices");
1451 desc->params[i].character = 'l';
1452 desc->params[i].type = JackDriverParamBool;
1453 desc->params[i].value.i = TRUE;
1454 strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
1455 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1457 i++;
1458 strcpy(desc->params[i].name, "async-latency");
1459 desc->params[i].character = 'L';
1460 desc->params[i].type = JackDriverParamUInt;
1461 desc->params[i].value.i = 100;
1462 strcpy(desc->params[i].short_desc, "Extra output latency in aynchronous mode (percent)");
1463 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1465 i++;
1466 strcpy(desc->params[i].name, "grain");
1467 desc->params[i].character = 'G';
1468 desc->params[i].type = JackDriverParamUInt;
1469 desc->params[i].value.i = 100;
1470 strcpy(desc->params[i].short_desc, "Computation grain in RT thread (percent)");
1471 strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
1473 return desc;
1476 SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
1478 jack_nframes_t srate = 44100;
1479 jack_nframes_t frames_per_interrupt = 128;
1480 int capture = FALSE;
1481 int playback = FALSE;
1482 int chan_in = 0;
1483 int chan_out = 0;
1484 bool monitor = false;
1485 const char* capture_driver_uid = "";
1486 const char* playback_driver_uid = "";
1487 const JSList *node;
1488 const jack_driver_param_t *param;
1489 jack_nframes_t systemic_input_latency = 0;
1490 jack_nframes_t systemic_output_latency = 0;
1491 int async_output_latency = 100;
1492 int computation_grain = -1;
1494 for (node = params; node; node = jack_slist_next(node)) {
1495 param = (const jack_driver_param_t *) node->data;
1497 switch (param->character) {
1499 case 'd':
1500 capture_driver_uid = strdup(param->value.str);
1501 playback_driver_uid = strdup(param->value.str);
1502 break;
1504 case 'D':
1505 capture = TRUE;
1506 playback = TRUE;
1507 break;
1509 case 'c':
1510 chan_in = chan_out = (int) param->value.ui;
1511 break;
1513 case 'i':
1514 chan_in = (int) param->value.ui;
1515 break;
1517 case 'o':
1518 chan_out = (int) param->value.ui;
1519 break;
1521 case 'C':
1522 capture = TRUE;
1523 if (strcmp(param->value.str, "none") != 0) {
1524 capture_driver_uid = strdup(param->value.str);
1526 break;
1528 case 'P':
1529 playback = TRUE;
1530 if (strcmp(param->value.str, "none") != 0) {
1531 playback_driver_uid = strdup(param->value.str);
1533 break;
1535 case 'm':
1536 monitor = param->value.i;
1537 break;
1539 case 'r':
1540 srate = param->value.ui;
1541 break;
1543 case 'p':
1544 frames_per_interrupt = (unsigned int) param->value.ui;
1545 break;
1547 case 'I':
1548 systemic_input_latency = param->value.ui;
1549 break;
1551 case 'O':
1552 systemic_output_latency = param->value.ui;
1553 break;
1555 case 'l':
1556 Jack::DisplayDeviceNames();
1557 break;
1559 case 'L':
1560 async_output_latency = param->value.ui;
1561 break;
1563 case 'G':
1564 computation_grain = param->value.ui;
1565 break;
1569 /* duplex is the default */
1570 if (!capture && !playback) {
1571 capture = TRUE;
1572 playback = TRUE;
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) == 0) {
1578 return driver;
1579 } else {
1580 delete driver;
1581 return NULL;
1585 #ifdef __cplusplus
1587 #endif