20 #include "JackCoreAudioAdapter.h"
21 #include "JackError.h"
24 #include <CoreServices/CoreServices.h>
29 static void PrintStreamDesc(AudioStreamBasicDescription *inDesc)
31 jack_log(
"- - - - - - - - - - - - - - - - - - - -");
32 jack_log(
" Sample Rate:%f", inDesc->mSampleRate);
33 jack_log(
" Format ID:%.*s", (
int)
sizeof(inDesc->mFormatID), (
char*)&inDesc->mFormatID);
34 jack_log(
" Format Flags:%lX", inDesc->mFormatFlags);
35 jack_log(
" Bytes per Packet:%ld", inDesc->mBytesPerPacket);
36 jack_log(
" Frames per Packet:%ld", inDesc->mFramesPerPacket);
37 jack_log(
" Bytes per Frame:%ld", inDesc->mBytesPerFrame);
38 jack_log(
" Channels per Frame:%ld", inDesc->mChannelsPerFrame);
39 jack_log(
" Bits per Channel:%ld", inDesc->mBitsPerChannel);
40 jack_log(
"- - - - - - - - - - - - - - - - - - - -");
43 static OSStatus DisplayDeviceNames()
51 err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
56 deviceNum = size /
sizeof(AudioDeviceID);
57 AudioDeviceID devices[deviceNum];
59 err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
64 for (i = 0; i < deviceNum; i++) {
65 char device_name[256];
66 char internal_name[256];
68 size =
sizeof(CFStringRef);
70 err = AudioDeviceGetProperty(devices[i], 0,
false, kAudioDevicePropertyDeviceUID, &size, &UIname);
72 CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
78 err = AudioDeviceGetProperty(devices[i], 0,
false, kAudioDevicePropertyDeviceName, &size, device_name);
83 jack_info(
"Device name = \'%s\', internal_name = \'%s\' (to be used as -C, -P, or -d parameter)", device_name, internal_name);
95 static void printError(OSStatus err)
98 case kAudioHardwareNoError:
99 jack_log(
"error code : kAudioHardwareNoError");
101 case kAudioConverterErr_FormatNotSupported:
102 jack_log(
"error code : kAudioConverterErr_FormatNotSupported");
104 case kAudioConverterErr_OperationNotSupported:
105 jack_log(
"error code : kAudioConverterErr_OperationNotSupported");
107 case kAudioConverterErr_PropertyNotSupported:
108 jack_log(
"error code : kAudioConverterErr_PropertyNotSupported");
110 case kAudioConverterErr_InvalidInputSize:
111 jack_log(
"error code : kAudioConverterErr_InvalidInputSize");
113 case kAudioConverterErr_InvalidOutputSize:
114 jack_log(
"error code : kAudioConverterErr_InvalidOutputSize");
116 case kAudioConverterErr_UnspecifiedError:
117 jack_log(
"error code : kAudioConverterErr_UnspecifiedError");
119 case kAudioConverterErr_BadPropertySizeError:
120 jack_log(
"error code : kAudioConverterErr_BadPropertySizeError");
122 case kAudioConverterErr_RequiresPacketDescriptionsError:
123 jack_log(
"error code : kAudioConverterErr_RequiresPacketDescriptionsError");
125 case kAudioConverterErr_InputSampleRateOutOfRange:
126 jack_log(
"error code : kAudioConverterErr_InputSampleRateOutOfRange");
128 case kAudioConverterErr_OutputSampleRateOutOfRange:
129 jack_log(
"error code : kAudioConverterErr_OutputSampleRateOutOfRange");
131 case kAudioHardwareNotRunningError:
132 jack_log(
"error code : kAudioHardwareNotRunningError");
134 case kAudioHardwareUnknownPropertyError:
135 jack_log(
"error code : kAudioHardwareUnknownPropertyError");
137 case kAudioHardwareIllegalOperationError:
138 jack_log(
"error code : kAudioHardwareIllegalOperationError");
140 case kAudioHardwareBadDeviceError:
141 jack_log(
"error code : kAudioHardwareBadDeviceError");
143 case kAudioHardwareBadStreamError:
144 jack_log(
"error code : kAudioHardwareBadStreamError");
146 case kAudioDeviceUnsupportedFormatError:
147 jack_log(
"error code : kAudioDeviceUnsupportedFormatError");
149 case kAudioDevicePermissionsError:
150 jack_log(
"error code : kAudioDevicePermissionsError");
152 case kAudioHardwareBadObjectError:
153 jack_log(
"error code : kAudioHardwareBadObjectError");
155 case kAudioHardwareUnsupportedOperationError:
156 jack_log(
"error code : kAudioHardwareUnsupportedOperationError");
164 OSStatus JackCoreAudioAdapter::AudioHardwareNotificationCallback(AudioHardwarePropertyID inPropertyID,
void* inClientData)
166 JackCoreAudioAdapter* driver = (JackCoreAudioAdapter*)inClientData;
168 switch (inPropertyID) {
170 case kAudioHardwarePropertyDevices: {
171 jack_log(
"JackCoreAudioAdapter::AudioHardwareNotificationCallback kAudioHardwarePropertyDevices");
172 DisplayDeviceNames();
180 OSStatus JackCoreAudioAdapter::SRNotificationCallback(AudioDeviceID inDevice,
183 AudioDevicePropertyID inPropertyID,
186 JackCoreAudioAdapter* driver =
static_cast<JackCoreAudioAdapter*
>(inClientData);
188 switch (inPropertyID) {
190 case kAudioDevicePropertyNominalSampleRate: {
191 jack_log(
"JackCoreAudioAdapter::SRNotificationCallback kAudioDevicePropertyNominalSampleRate");
192 driver->fState =
true;
201 OSStatus JackCoreAudioAdapter::DeviceNotificationCallback(AudioDeviceID inDevice,
204 AudioDevicePropertyID inPropertyID,
208 switch (inPropertyID) {
210 case kAudioDeviceProcessorOverload: {
211 jack_error(
"JackCoreAudioAdapter::DeviceNotificationCallback kAudioDeviceProcessorOverload");
215 case kAudioDevicePropertyStreamConfiguration: {
216 jack_error(
"Cannot handle kAudioDevicePropertyStreamConfiguration");
217 return kAudioHardwareUnsupportedOperationError;
220 case kAudioDevicePropertyNominalSampleRate: {
221 jack_error(
"Cannot handle kAudioDevicePropertyNominalSampleRate");
222 return kAudioHardwareUnsupportedOperationError;
229 int JackCoreAudioAdapter::AddListeners()
231 OSStatus err = noErr;
234 err = AudioDeviceAddPropertyListener(fDeviceID, 0,
true, kAudioDeviceProcessorOverload, DeviceNotificationCallback,
this);
236 jack_error(
"Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
241 err = AudioHardwareAddPropertyListener(kAudioHardwarePropertyDevices, AudioHardwareNotificationCallback,
this);
243 jack_error(
"Error calling AudioHardwareAddPropertyListener with kAudioHardwarePropertyDevices");
248 err = AudioDeviceAddPropertyListener(fDeviceID, 0,
true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback,
this);
250 jack_error(
"Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
255 err = AudioDeviceAddPropertyListener(fDeviceID, 0,
true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback,
this);
257 jack_error(
"Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
262 err = AudioDeviceAddPropertyListener(fDeviceID, 0,
true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback,
this);
264 jack_error(
"Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
269 err = AudioDeviceAddPropertyListener(fDeviceID, 0,
false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback,
this);
271 jack_error(
"Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
279 void JackCoreAudioAdapter::RemoveListeners()
281 AudioDeviceRemovePropertyListener(fDeviceID, 0,
true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
282 AudioHardwareRemovePropertyListener(kAudioHardwarePropertyDevices, AudioHardwareNotificationCallback);
283 AudioDeviceRemovePropertyListener(fDeviceID, 0,
true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback);
284 AudioDeviceRemovePropertyListener(fDeviceID, 0,
true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback);
285 AudioDeviceRemovePropertyListener(fDeviceID, 0,
true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
286 AudioDeviceRemovePropertyListener(fDeviceID, 0,
false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
289 OSStatus JackCoreAudioAdapter::Render(
void *inRefCon,
290 AudioUnitRenderActionFlags *ioActionFlags,
291 const AudioTimeStamp *inTimeStamp,
293 UInt32 inNumberFrames,
294 AudioBufferList *ioData)
296 JackCoreAudioAdapter* adapter =
static_cast<JackCoreAudioAdapter*
>(inRefCon);
297 OSStatus err = AudioUnitRender(adapter->fAUHAL, ioActionFlags, inTimeStamp, 1, inNumberFrames, adapter->fInputData);
300 jack_default_audio_sample_t* inputBuffer[adapter->fCaptureChannels];
301 jack_default_audio_sample_t* outputBuffer[adapter->fPlaybackChannels];
303 for (
int i = 0; i < adapter->fCaptureChannels; i++) {
304 inputBuffer[i] = (jack_default_audio_sample_t*)adapter->fInputData->mBuffers[i].mData;
306 for (
int i = 0; i < adapter->fPlaybackChannels; i++) {
307 outputBuffer[i] = (jack_default_audio_sample_t*)ioData->mBuffers[i].mData;
310 adapter->PushAndPull((jack_default_audio_sample_t**)inputBuffer, (jack_default_audio_sample_t**)outputBuffer, inNumberFrames);
317 JackCoreAudioAdapter::JackCoreAudioAdapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate,
const JSList* params)
318 :JackAudioAdapterInterface(buffer_size, sample_rate), fInputData(0), fCapturing(false), fPlaying(false), fState(false)
322 int in_nChannels = 0;
323 int out_nChannels = 0;
324 char captureName[256];
325 char playbackName[256];
328 fClockDriftCompensate =
false;
331 fCaptureChannels = -1;
332 fPlaybackChannels = -1;
336 Gestalt(gestaltSystemVersionMajor, &major);
337 Gestalt(gestaltSystemVersionMinor, &minor);
340 if (major == 10 && minor >= 6) {
341 CFRunLoopRef theRunLoop = NULL;
342 AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
343 OSStatus theError = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL,
sizeof(CFRunLoopRef), &theRunLoop);
344 if (theError != noErr) {
345 jack_error(
"JackCoreAudioAdapter::Open kAudioHardwarePropertyRunLoop error");
349 for (node = params; node; node = jack_slist_next(node)) {
352 switch (param->character) {
355 fCaptureChannels = fPlaybackChannels = param->value.ui;
359 fCaptureChannels = param->value.ui;
363 fPlaybackChannels = param->value.ui;
368 strncpy(fCaptureUID, param->value.str, 256);
373 strncpy(fPlaybackUID, param->value.str, 256);
377 strncpy(fCaptureUID, param->value.str, 256);
378 strncpy(fPlaybackUID, param->value.str, 256);
382 fCapturing = fPlaying =
true;
386 SetAdaptedSampleRate(param->value.ui);
390 SetAdaptedBufferSize(param->value.ui);
394 DisplayDeviceNames();
398 fQuality = param->value.ui;
402 fRingbufferCurSize = param->value.ui;
407 fClockDriftCompensate =
true;
413 if (!fCapturing && !fPlaying) {
418 if (SetupDevices(fCaptureUID, fPlaybackUID, captureName, playbackName, fAdaptedSampleRate) < 0) {
419 throw std::bad_alloc();
422 if (SetupChannels(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels,
true) < 0) {
423 throw std::bad_alloc();
426 if (SetupBufferSize(fAdaptedBufferSize) < 0) {
427 throw std::bad_alloc();
430 if (SetupSampleRate(fAdaptedSampleRate) < 0) {
431 throw std::bad_alloc();
434 if (OpenAUHAL(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, fAdaptedBufferSize, fAdaptedSampleRate) < 0) {
435 throw std::bad_alloc();
438 if (fCapturing && fCaptureChannels > 0) {
439 if (SetupBuffers(fCaptureChannels) < 0) {
440 throw std::bad_alloc();
444 if (AddListeners() < 0) {
445 throw std::bad_alloc();
448 GetStreamLatencies(fDeviceID,
true, fInputLatencies);
449 GetStreamLatencies(fDeviceID,
false, fOutputLatencies);
452 OSStatus JackCoreAudioAdapter::GetDefaultDevice(AudioDeviceID*
id)
455 UInt32 theSize =
sizeof(UInt32);
456 AudioDeviceID inDefault;
457 AudioDeviceID outDefault;
459 if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
463 if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
467 jack_log(
"GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
470 if (inDefault != outDefault) {
471 jack_error(
"Default input and output devices are not the same !!");
472 return kAudioHardwareBadDeviceError;
473 }
else if (inDefault == 0) {
474 jack_error(
"Default input and output devices are null !!");
475 return kAudioHardwareBadDeviceError;
482 OSStatus JackCoreAudioAdapter::GetTotalChannels(AudioDeviceID device,
int& channelCount,
bool isInput)
484 OSStatus err = noErr;
489 err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
491 AudioBufferList bufferList[outSize];
492 err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
494 for (
unsigned int i = 0; i < bufferList->mNumberBuffers; i++) {
495 channelCount += bufferList->mBuffers[i].mNumberChannels;
503 OSStatus JackCoreAudioAdapter::GetDeviceIDFromUID(
const char* UID, AudioDeviceID*
id)
505 UInt32 size =
sizeof(AudioValueTranslation);
506 CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
507 AudioValueTranslation value = { &inIUD,
sizeof(CFStringRef),
id,
sizeof(AudioDeviceID) };
510 return kAudioHardwareUnspecifiedError;
512 OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
514 jack_log(
"GetDeviceIDFromUID %s %ld", UID, *
id);
515 return (*
id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
519 OSStatus JackCoreAudioAdapter::GetDefaultInputDevice(AudioDeviceID*
id)
522 UInt32 theSize =
sizeof(UInt32);
523 AudioDeviceID inDefault;
525 if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
529 if (inDefault == 0) {
530 jack_error(
"Error: default input device is 0, please select a correct one !!");
533 jack_log(
"GetDefaultInputDevice: input = %ld ", inDefault);
538 OSStatus JackCoreAudioAdapter::GetDefaultOutputDevice(AudioDeviceID*
id)
541 UInt32 theSize =
sizeof(UInt32);
542 AudioDeviceID outDefault;
544 if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
548 if (outDefault == 0) {
549 jack_error(
"Error: default output device is 0, please select a correct one !!");
552 jack_log(
"GetDefaultOutputDevice: output = %ld", outDefault);
557 OSStatus JackCoreAudioAdapter::GetDeviceNameFromID(AudioDeviceID
id,
char* name)
560 return AudioDeviceGetProperty(
id, 0,
false, kAudioDevicePropertyDeviceName, &size, name);
563 AudioDeviceID JackCoreAudioAdapter::GetDeviceIDFromName(
const char* name)
569 OSStatus err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
574 deviceNum = size /
sizeof(AudioDeviceID);
575 AudioDeviceID devices[deviceNum];
577 err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
582 for (i = 0; i < deviceNum; i++) {
583 char device_name[256];
585 err = AudioDeviceGetProperty(devices[i], 0,
false, kAudioDevicePropertyDeviceName, &size, device_name);
588 }
else if (strcmp(device_name, name) == 0) {
597 int JackCoreAudioAdapter::SetupDevices(
const char* capture_driver_uid,
598 const char* playback_driver_uid,
599 char* capture_driver_name,
600 char* playback_driver_name,
601 jack_nframes_t samplerate)
603 capture_driver_name[0] = 0;
604 playback_driver_name[0] = 0;
607 if (strcmp(capture_driver_uid,
"") != 0 && strcmp(playback_driver_uid,
"") != 0) {
608 jack_log(
"JackCoreAudioDriver::Open duplex");
611 if (strcmp(capture_driver_uid, playback_driver_uid) == 0) {
613 if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
614 jack_log(
"Will take default in/out");
615 if (GetDefaultDevice(&fDeviceID) != noErr) {
620 if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
621 jack_error(
"Cannot get device name from device ID");
628 AudioDeviceID captureID, playbackID;
630 if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr) {
631 jack_log(
"Will take default input");
632 if (GetDefaultInputDevice(&captureID) != noErr) {
633 jack_error(
"Cannot open default input device");
638 if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr) {
639 jack_log(
"Will take default output");
640 if (GetDefaultOutputDevice(&playbackID) != noErr) {
641 jack_error(
"Cannot open default output device");
646 if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
652 }
else if (strcmp(capture_driver_uid,
"") != 0) {
653 jack_log(
"JackCoreAudioAdapter::Open capture only");
654 if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
655 if (GetDefaultInputDevice(&fDeviceID) != noErr) {
656 jack_error(
"Cannot open default input device");
660 if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
661 jack_error(
"Cannot get device name from device ID");
666 }
else if (strcmp(playback_driver_uid,
"") != 0) {
667 jack_log(
"JackCoreAudioAdapter::Open playback only");
668 if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
669 if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
670 jack_error(
"Cannot open default output device");
674 if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
675 jack_error(
"Cannot get device name from device ID");
681 jack_log(
"JackCoreAudioAdapter::Open default driver");
682 if (GetDefaultDevice(&fDeviceID) != noErr) {
683 jack_error(
"Cannot open default device in duplex mode, so aggregate default input and default output");
686 AudioDeviceID captureID = -1, playbackID = -1;
688 if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr) {
689 jack_log(
"Will take default input");
690 if (GetDefaultInputDevice(&captureID) != noErr) {
691 jack_error(
"Cannot open default input device");
696 if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr) {
697 jack_log(
"Will take default output");
698 if (GetDefaultOutputDevice(&playbackID) != noErr) {
699 jack_error(
"Cannot open default output device");
704 if (captureID > 0 && playbackID > 0) {
705 if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
709 jack_error(
"Cannot use default input/output");
720 AudioDeviceID captureID = GetDeviceIDFromName(
"Built-in Input");
721 AudioDeviceID playbackID = GetDeviceIDFromName(
"Built-in Output");
723 if (captureID > 0 && playbackID > 0) {
724 if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
728 jack_error(
"Cannot aggregate built-in input and output");
735 int JackCoreAudioAdapter::SetupChannels(
bool capturing,
743 OSStatus err = noErr;
746 err = GetTotalChannels(fDeviceID, in_nChannels,
true);
748 jack_error(
"Cannot get input channel number");
752 jack_log(
"Max input channels : %d", in_nChannels);
757 err = GetTotalChannels(fDeviceID, out_nChannels,
false);
759 jack_error(
"Cannot get output channel number");
763 jack_log(
"Max output channels : %d", out_nChannels);
767 if (inchannels > in_nChannels) {
768 jack_error(
"This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
774 if (outchannels > out_nChannels) {
775 jack_error(
"This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
781 if (inchannels == -1) {
782 jack_log(
"Setup max in channels = %ld", in_nChannels);
783 inchannels = in_nChannels;
786 if (outchannels == -1) {
787 jack_log(
"Setup max out channels = %ld", out_nChannels);
788 outchannels = out_nChannels;
794 int JackCoreAudioAdapter::SetupBufferSize(jack_nframes_t buffer_size)
797 UInt32 outSize =
sizeof(UInt32);
798 OSStatus err = AudioDeviceSetProperty(fDeviceID, NULL, 0,
false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
800 jack_error(
"Cannot set buffer size %ld", buffer_size);
808 int JackCoreAudioAdapter::SetupSampleRate(jack_nframes_t samplerate)
810 return SetupSampleRateAux(fDeviceID, samplerate);
813 int JackCoreAudioAdapter::SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate)
815 OSStatus err = noErr;
820 outSize =
sizeof(Float64);
821 err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
827 jack_log(
"Current sample rate = %f", sampleRate);
831 if (samplerate != (jack_nframes_t)sampleRate) {
832 sampleRate = (Float64)samplerate;
835 err = AudioDeviceAddPropertyListener(inDevice, 0,
true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback,
this);
837 jack_error(
"Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
841 err = AudioDeviceSetProperty(inDevice, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
843 jack_error(
"Cannot set sample rate = %ld", samplerate);
850 while (!fState && count++ < WAIT_COUNTER) {
856 AudioDeviceRemovePropertyListener(inDevice, 0,
true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
862 int JackCoreAudioAdapter::SetupBuffers(
int inchannels)
864 jack_log(
"JackCoreAudioAdapter::SetupBuffers: input = %ld", inchannels);
867 fInputData = (AudioBufferList*)malloc(
sizeof(UInt32) + inchannels *
sizeof(AudioBuffer));
868 fInputData->mNumberBuffers = inchannels;
869 for (
int i = 0; i < fCaptureChannels; i++) {
870 fInputData->mBuffers[i].mNumberChannels = 1;
871 fInputData->mBuffers[i].mDataByteSize = fAdaptedBufferSize *
sizeof(jack_default_audio_sample_t);
872 fInputData->mBuffers[i].mData = malloc(fAdaptedBufferSize *
sizeof(jack_default_audio_sample_t));
877 void JackCoreAudioAdapter::DisposeBuffers()
880 for (
int i = 0; i < fCaptureChannels; i++) {
881 free(fInputData->mBuffers[i].mData);
888 int JackCoreAudioAdapter::OpenAUHAL(
bool capturing,
894 jack_nframes_t buffer_size,
895 jack_nframes_t samplerate)
897 ComponentResult err1;
899 AudioStreamBasicDescription srcFormat, dstFormat;
900 AudioDeviceID currAudioDeviceID;
903 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);
905 if (inchannels == 0 && outchannels == 0) {
906 jack_error(
"No input and output channels...");
911 #ifdef MAC_OS_X_VERSION_10_5
912 ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
913 Component HALOutput = FindNextComponent(NULL, &cd);
914 err1 = OpenAComponent(HALOutput, &fAUHAL);
921 AudioComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
922 AudioComponent HALOutput = AudioComponentFindNext(NULL, &cd);
923 err1 = AudioComponentInstanceNew(HALOutput, &fAUHAL);
925 jack_error(
"Error calling AudioComponentInstanceNew");
931 err1 = AudioUnitInitialize(fAUHAL);
939 if (capturing && inchannels > 0) {
947 err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO,
sizeof(enableIO));
949 jack_error(
"Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
954 if (playing && outchannels > 0) {
962 err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO,
sizeof(enableIO));
964 jack_error(
"Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
969 size =
sizeof(AudioDeviceID);
970 err1 = AudioUnitGetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &currAudioDeviceID, &size);
972 jack_error(
"Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
976 jack_log(
"AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID);
980 err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID,
sizeof(AudioDeviceID));
982 jack_error(
"Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
988 if (capturing && inchannels > 0) {
989 err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&buffer_size,
sizeof(UInt32));
991 jack_error(
"Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
997 if (playing && outchannels > 0) {
998 err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&buffer_size,
sizeof(UInt32));
1000 jack_error(
"Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
1007 if (capturing && inchannels > 0 && inchannels <= in_nChannels) {
1008 SInt32 chanArr[in_nChannels];
1009 for (
int i = 0; i < in_nChannels; i++) {
1012 for (
int i = 0; i < inchannels; i++) {
1015 AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr,
sizeof(SInt32) * in_nChannels);
1016 if (err1 != noErr) {
1017 jack_error(
"Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
1023 if (playing && outchannels > 0 && outchannels <= out_nChannels) {
1024 SInt32 chanArr[out_nChannels];
1025 for (
int i = 0; i < out_nChannels; i++) {
1028 for (
int i = 0; i < outchannels; i++) {
1031 err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr,
sizeof(SInt32) * out_nChannels);
1032 if (err1 != noErr) {
1033 jack_error(
"Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
1040 if (capturing && inchannels > 0) {
1042 size =
sizeof(AudioStreamBasicDescription);
1043 err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &srcFormat, &size);
1044 if (err1 != noErr) {
1045 jack_error(
"Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
1049 PrintStreamDesc(&srcFormat);
1051 jack_log(
"Setup AUHAL input stream converter SR = %ld", samplerate);
1052 srcFormat.mSampleRate = samplerate;
1053 srcFormat.mFormatID = kAudioFormatLinearPCM;
1054 srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
1055 srcFormat.mBytesPerPacket =
sizeof(jack_default_audio_sample_t);
1056 srcFormat.mFramesPerPacket = 1;
1057 srcFormat.mBytesPerFrame =
sizeof(jack_default_audio_sample_t);
1058 srcFormat.mChannelsPerFrame = inchannels;
1059 srcFormat.mBitsPerChannel = 32;
1060 PrintStreamDesc(&srcFormat);
1062 err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat,
sizeof(AudioStreamBasicDescription));
1064 if (err1 != noErr) {
1065 jack_error(
"Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
1071 if (playing && outchannels > 0) {
1073 size =
sizeof(AudioStreamBasicDescription);
1074 err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &dstFormat, &size);
1075 if (err1 != noErr) {
1076 jack_error(
"Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
1080 PrintStreamDesc(&dstFormat);
1082 jack_log(
"Setup AUHAL output stream converter SR = %ld", samplerate);
1083 dstFormat.mSampleRate = samplerate;
1084 dstFormat.mFormatID = kAudioFormatLinearPCM;
1085 dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
1086 dstFormat.mBytesPerPacket =
sizeof(jack_default_audio_sample_t);
1087 dstFormat.mFramesPerPacket = 1;
1088 dstFormat.mBytesPerFrame =
sizeof(jack_default_audio_sample_t);
1089 dstFormat.mChannelsPerFrame = outchannels;
1090 dstFormat.mBitsPerChannel = 32;
1091 PrintStreamDesc(&dstFormat);
1093 err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat,
sizeof(AudioStreamBasicDescription));
1095 if (err1 != noErr) {
1096 jack_error(
"Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
1103 if (inchannels > 0 && outchannels == 0) {
1104 AURenderCallbackStruct output;
1105 output.inputProc = Render;
1106 output.inputProcRefCon =
this;
1107 err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output,
sizeof(output));
1108 if (err1 != noErr) {
1109 jack_error(
"Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
1114 AURenderCallbackStruct output;
1115 output.inputProc = Render;
1116 output.inputProcRefCon =
this;
1117 err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output,
sizeof(output));
1118 if (err1 != noErr) {
1119 jack_error(
"Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
1132 OSStatus JackCoreAudioAdapter::DestroyAggregateDevice()
1134 OSStatus osErr = noErr;
1135 AudioObjectPropertyAddress pluginAOPA;
1136 pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice;
1137 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
1138 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
1141 osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
1142 if (osErr != noErr) {
1143 jack_error(
"JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
1148 osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID);
1149 if (osErr != noErr) {
1150 jack_error(
"JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyData error");
1158 static CFStringRef GetDeviceName(AudioDeviceID
id)
1160 UInt32 size =
sizeof(CFStringRef);
1162 OSStatus err = AudioDeviceGetProperty(
id, 0,
false, kAudioDevicePropertyDeviceUID, &size, &UIname);
1163 return (err == noErr) ? UIname : NULL;
1166 OSStatus JackCoreAudioAdapter::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
1168 OSStatus err = noErr;
1169 AudioObjectID sub_device[32];
1170 UInt32 outSize =
sizeof(sub_device);
1172 err = AudioDeviceGetProperty(captureDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
1173 vector<AudioDeviceID> captureDeviceIDArray;
1176 jack_log(
"Input device does not have subdevices");
1177 captureDeviceIDArray.push_back(captureDeviceID);
1179 int num_devices = outSize /
sizeof(AudioObjectID);
1180 jack_log(
"Input device has %d subdevices", num_devices);
1181 for (
int i = 0; i < num_devices; i++) {
1182 captureDeviceIDArray.push_back(sub_device[i]);
1186 err = AudioDeviceGetProperty(playbackDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
1187 vector<AudioDeviceID> playbackDeviceIDArray;
1190 jack_log(
"Output device does not have subdevices");
1191 playbackDeviceIDArray.push_back(playbackDeviceID);
1193 int num_devices = outSize /
sizeof(AudioObjectID);
1194 jack_log(
"Output device has %d subdevices", num_devices);
1195 for (
int i = 0; i < num_devices; i++) {
1196 playbackDeviceIDArray.push_back(sub_device[i]);
1200 return CreateAggregateDeviceAux(captureDeviceIDArray, playbackDeviceIDArray, samplerate, outAggregateDevice);
1203 OSStatus JackCoreAudioAdapter::CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
1205 OSStatus osErr = noErr;
1207 Boolean outWritable;
1211 AudioObjectPropertyAddress theAddressOwned = { kAudioObjectPropertyOwnedObjects, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
1212 AudioObjectPropertyAddress theAddressDrift = { kAudioSubDevicePropertyDriftCompensation, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
1213 UInt32 theQualifierDataSize =
sizeof(AudioObjectID);
1214 AudioClassID inClass = kAudioSubDeviceClassID;
1215 void* theQualifierData = &inClass;
1216 UInt32 subDevicesNum = 0;
1221 UInt32 keptclockdomain = 0;
1222 UInt32 clockdomain = 0;
1223 outSize =
sizeof(UInt32);
1224 bool need_clock_drift_compensation =
false;
1226 for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
1227 if (SetupSampleRateAux(captureDeviceID[i], samplerate) < 0) {
1228 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : cannot set SR of input device");
1231 osErr = AudioDeviceGetProperty(captureDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
1233 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
1236 keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
1237 jack_log(
"JackCoreAudioAdapter::CreateAggregateDevice : input clockdomain = %d", clockdomain);
1238 if (clockdomain != 0 && clockdomain != keptclockdomain) {
1239 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...");
1240 need_clock_drift_compensation =
true;
1246 for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
1247 if (SetupSampleRateAux(playbackDeviceID[i], samplerate) < 0) {
1248 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : cannot set SR of output device");
1251 osErr = AudioDeviceGetProperty(playbackDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
1253 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
1256 keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
1257 jack_log(
"JackCoreAudioAdapter::CreateAggregateDevice : output clockdomain = %d", clockdomain);
1258 if (clockdomain != 0 && clockdomain != keptclockdomain) {
1259 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...");
1260 need_clock_drift_compensation =
true;
1267 if (keptclockdomain == 0) {
1268 need_clock_drift_compensation =
true;
1275 char device_name[256];
1276 for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
1277 GetDeviceNameFromID(captureDeviceID[i], device_name);
1278 jack_info(
"Separated input = '%s' ", device_name);
1281 for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
1282 GetDeviceNameFromID(playbackDeviceID[i], device_name);
1283 jack_info(
"Separated output = '%s' ", device_name);
1286 osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable);
1287 if (osErr != noErr) {
1288 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : AudioHardwareGetPropertyInfo kAudioHardwarePropertyPlugInForBundleID error");
1293 AudioValueTranslation pluginAVT;
1295 CFStringRef inBundleRef = CFSTR(
"com.apple.audio.CoreAudio");
1297 pluginAVT.mInputData = &inBundleRef;
1298 pluginAVT.mInputDataSize =
sizeof(inBundleRef);
1299 pluginAVT.mOutputData = &fPluginID;
1300 pluginAVT.mOutputDataSize =
sizeof(fPluginID);
1302 osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT);
1303 if (osErr != noErr) {
1304 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : AudioHardwareGetProperty kAudioHardwarePropertyPlugInForBundleID error");
1313 CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
1315 CFStringRef AggregateDeviceNameRef = CFSTR(
"JackDuplex");
1316 CFStringRef AggregateDeviceUIDRef = CFSTR(
"com.grame.JackDuplex");
1319 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
1322 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
1326 CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
1329 Gestalt(gestaltSystemVersion, &system);
1331 jack_log(
"JackCoreAudioAdapter::CreateAggregateDevice : system version = %x limit = %x", system, 0x00001054);
1334 if (system < 0x00001054) {
1335 jack_log(
"JackCoreAudioAdapter::CreateAggregateDevice : public aggregate device....");
1337 jack_log(
"JackCoreAudioAdapter::CreateAggregateDevice : private aggregate device....");
1338 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
1342 CFMutableArrayRef subDevicesArrayClock = NULL;
1385 CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
1387 vector<CFStringRef> captureDeviceUID;
1388 for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
1389 CFStringRef ref = GetDeviceName(captureDeviceID[i]);
1393 captureDeviceUID.push_back(ref);
1395 CFArrayAppendValue(subDevicesArray, ref);
1398 vector<CFStringRef> playbackDeviceUID;
1399 for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
1400 CFStringRef ref = GetDeviceName(playbackDeviceID[i]);
1404 playbackDeviceUID.push_back(ref);
1406 CFArrayAppendValue(subDevicesArray, ref);
1413 AudioObjectPropertyAddress pluginAOPA;
1414 pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
1415 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
1416 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
1419 osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
1420 if (osErr != noErr) {
1421 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectGetPropertyDataSize error");
1426 osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA,
sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, outAggregateDevice);
1427 if (osErr != noErr) {
1428 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectGetPropertyData error");
1435 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1,
false);
1441 pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
1442 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
1443 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
1444 outDataSize =
sizeof(CFMutableArrayRef);
1445 osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
1446 if (osErr != noErr) {
1447 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectSetPropertyData for sub-device list error");
1453 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1,
false);
1461 pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
1462 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
1463 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
1464 outDataSize =
sizeof(CFStringRef);
1465 osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID[0]);
1466 if (osErr != noErr) {
1467 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectSetPropertyData for master device error");
1473 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1,
false);
1478 if (fClockDriftCompensate) {
1479 if (need_clock_drift_compensation) {
1480 jack_info(
"Clock drift compensation activated...");
1483 osErr = AudioObjectGetPropertyDataSize(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize);
1484 if (osErr != noErr) {
1485 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
1490 subDevicesNum = outSize /
sizeof(AudioObjectID);
1491 jack_info(
"JackCoreAudioAdapter::CreateAggregateDevice clock drift compensation, number of sub-devices = %d", subDevicesNum);
1492 AudioObjectID subDevices[subDevicesNum];
1493 outSize =
sizeof(subDevices);
1495 osErr = AudioObjectGetPropertyData(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize, subDevices);
1496 if (osErr != noErr) {
1497 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
1502 for (UInt32 index = 0; index < subDevicesNum; ++index) {
1503 UInt32 theDriftCompensationValue = 1;
1504 osErr = AudioObjectSetPropertyData(subDevices[index], &theAddressDrift, 0, NULL,
sizeof(UInt32), &theDriftCompensationValue);
1505 if (osErr != noErr) {
1506 jack_error(
"JackCoreAudioAdapter::CreateAggregateDevice kAudioSubDevicePropertyDriftCompensation error");
1511 jack_info(
"Clock drift compensation was asked but is not needed (devices use the same clock domain)");
1516 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1,
false);
1523 CFRelease(AggregateDeviceNumberRef);
1526 CFRelease(aggDeviceDict);
1527 CFRelease(subDevicesArray);
1529 if (subDevicesArrayClock) {
1530 CFRelease(subDevicesArrayClock);
1534 for (UInt32 i = 0; i < captureDeviceUID.size(); i++) {
1535 CFRelease(captureDeviceUID[i]);
1538 for (UInt32 i = 0; i < playbackDeviceUID.size(); i++) {
1539 CFRelease(playbackDeviceUID[i]);
1542 jack_log(
"New aggregate device %ld", *outAggregateDevice);
1546 DestroyAggregateDevice();
1551 bool JackCoreAudioAdapter::IsAggregateDevice(AudioDeviceID device)
1553 OSStatus err = noErr;
1554 AudioObjectID sub_device[32];
1555 UInt32 outSize =
sizeof(sub_device);
1556 err = AudioDeviceGetProperty(device, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
1559 jack_log(
"Device does not have subdevices");
1562 int num_devices = outSize /
sizeof(AudioObjectID);
1563 jack_log(
"Device does has %d subdevices", num_devices);
1568 void JackCoreAudioAdapter::CloseAUHAL()
1570 AudioUnitUninitialize(fAUHAL);
1571 CloseComponent(fAUHAL);
1574 int JackCoreAudioAdapter::Open()
1576 return (AudioOutputUnitStart(fAUHAL) != noErr) ? -1 : 0;
1579 int JackCoreAudioAdapter::Close()
1582 fTable.Save(fHostBufferSize, fHostSampleRate, fAdaptedSampleRate, fAdaptedBufferSize);
1584 AudioOutputUnitStop(fAUHAL);
1588 if (fPluginID > 0) {
1589 DestroyAggregateDevice();
1594 int JackCoreAudioAdapter::SetSampleRate(jack_nframes_t sample_rate)
1596 JackAudioAdapterInterface::SetHostSampleRate(sample_rate);
1601 int JackCoreAudioAdapter::SetBufferSize(jack_nframes_t buffer_size)
1603 JackAudioAdapterInterface::SetHostBufferSize(buffer_size);
1608 OSStatus JackCoreAudioAdapter::GetStreamLatencies(AudioDeviceID device,
bool isInput, vector<int>& latencies)
1610 OSStatus err = noErr;
1611 UInt32 outSize1, outSize2, outSize3;
1612 Boolean outWritable;
1614 err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreams, &outSize1, &outWritable);
1616 int stream_count = outSize1 /
sizeof(UInt32);
1617 AudioStreamID streamIDs[stream_count];
1618 AudioBufferList bufferList[stream_count];
1619 UInt32 streamLatency;
1620 outSize2 =
sizeof(UInt32);
1622 err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreams, &outSize1, streamIDs);
1624 jack_error(
"GetStreamLatencies kAudioDevicePropertyStreams err = %d", err);
1628 err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize3, &outWritable);
1630 jack_error(
"GetStreamLatencies kAudioDevicePropertyStreamConfiguration err = %d", err);
1634 for (
int i = 0; i < stream_count; i++) {
1635 err = AudioStreamGetProperty(streamIDs[i], 0, kAudioStreamPropertyLatency, &outSize2, &streamLatency);
1637 jack_error(
"GetStreamLatencies kAudioStreamPropertyLatency err = %d", err);
1640 err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize3, bufferList);
1642 jack_error(
"GetStreamLatencies kAudioDevicePropertyStreamConfiguration err = %d", err);
1646 for (uint k = 0; k < bufferList->mBuffers[i].mNumberChannels; k++) {
1647 latencies.push_back(streamLatency);
1654 int JackCoreAudioAdapter::GetLatency(
int port_index,
bool input)
1656 UInt32 size =
sizeof(UInt32);
1660 OSStatus err = AudioDeviceGetProperty(fDeviceID, 0, input, kAudioDevicePropertyLatency, &size, &value1);
1662 jack_log(
"AudioDeviceGetProperty kAudioDevicePropertyLatency error");
1664 err = AudioDeviceGetProperty(fDeviceID, 0, input, kAudioDevicePropertySafetyOffset, &size, &value2);
1666 jack_log(
"AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error");
1671 return value1 + value2 + fAdaptedBufferSize;
1674 int JackCoreAudioAdapter::GetInputLatency(
int port_index)
1676 if (port_index <
int(fInputLatencies.size())) {
1677 return GetLatency(port_index,
true) + fInputLatencies[port_index];
1680 return GetLatency(port_index,
true);
1684 int JackCoreAudioAdapter::GetOutputLatency(
int port_index)
1686 if (port_index <
int(fOutputLatencies.size())) {
1687 return GetLatency(port_index,
false) + fOutputLatencies[port_index];
1690 return GetLatency(port_index,
false);
1707 desc = jack_driver_descriptor_construct(
"audioadapter", JackDriverNone,
"netjack audio <==> net backend adapter", &filler);
1710 jack_driver_descriptor_add_parameter(desc, &filler,
"channels",
'c', JackDriverParamInt, &value, NULL,
"Maximum number of channels",
"Maximum number of channels. If -1, max possible number of channels will be used");
1711 jack_driver_descriptor_add_parameter(desc, &filler,
"in-channels",
'i', JackDriverParamInt, &value, NULL,
"Maximum number of input channels",
"Maximum number of input channels. If -1, max possible number of input channels will be used");
1712 jack_driver_descriptor_add_parameter(desc, &filler,
"out-channels",
'o', JackDriverParamInt, &value, NULL,
"Maximum number of output channels",
"Maximum number of output channels. If -1, max possible number of output channels will be used");
1715 jack_driver_descriptor_add_parameter(desc, &filler,
"capture",
'C', JackDriverParamString, &value, NULL,
"Input CoreAudio device name", NULL);
1716 jack_driver_descriptor_add_parameter(desc, &filler,
"playback",
'P', JackDriverParamString, &value, NULL,
"Output CoreAudio device name", NULL);
1719 jack_driver_descriptor_add_parameter(desc, &filler,
"rate",
'r', JackDriverParamUInt, &value, NULL,
"Sample rate", NULL);
1722 jack_driver_descriptor_add_parameter(desc, &filler,
"period",
'p', JackDriverParamUInt, &value, NULL,
"Frames per period", NULL);
1725 jack_driver_descriptor_add_parameter(desc, &filler,
"duplex",
'D', JackDriverParamBool, &value, NULL,
"Provide both capture and playback ports", NULL);
1728 jack_driver_descriptor_add_parameter(desc, &filler,
"device",
'd', JackDriverParamString, &value, NULL,
"CoreAudio device name", NULL);
1731 jack_driver_descriptor_add_parameter(desc, &filler,
"list-devices",
'l', JackDriverParamBool, &value, NULL,
"Display available CoreAudio devices", NULL);
1734 jack_driver_descriptor_add_parameter(desc, &filler,
"quality",
'q', JackDriverParamInt, &value, NULL,
"Resample algorithm quality (0 - 4)", NULL);
1737 jack_driver_descriptor_add_parameter(desc, &filler,
"ring-buffer",
'g', JackDriverParamInt, &value, NULL,
"Fixed ringbuffer size",
"Fixed ringbuffer size (if not set => automatic adaptative)");
1740 jack_driver_descriptor_add_parameter(desc, &filler,
"clock-drift",
's', JackDriverParamBool, &value, NULL,
"Clock drift compensation",
"Whether to compensate clock drift in dynamically created aggregate device");
SERVER_EXPORT void jack_error(const char *fmt,...)
SERVER_EXPORT void jack_info(const char *fmt,...)
SERVER_EXPORT void jack_log(const char *fmt,...)