Jack2  1.9.10
JackCoreAudioAdapter.cpp
1 /*
2 Copyright (C) 2008 Grame
3 
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.
8 
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.
13 
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.
17 
18 */
19 
20 #include "JackCoreAudioAdapter.h"
21 #include "JackError.h"
22 #include <unistd.h>
23 
24 #include <CoreServices/CoreServices.h>
25 
26 namespace Jack
27 {
28 
29 static void PrintStreamDesc(AudioStreamBasicDescription *inDesc)
30 {
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("- - - - - - - - - - - - - - - - - - - -");
41 }
42 
43 static OSStatus DisplayDeviceNames()
44 {
45  UInt32 size;
46  Boolean isWritable;
47  int i, deviceNum;
48  OSStatus err;
49  CFStringRef UIname;
50 
51  err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
52  if (err != noErr) {
53  return err;
54  }
55 
56  deviceNum = size / sizeof(AudioDeviceID);
57  AudioDeviceID devices[deviceNum];
58 
59  err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
60  if (err != noErr) {
61  return err;
62  }
63 
64  for (i = 0; i < deviceNum; i++) {
65  char device_name[256];
66  char internal_name[256];
67 
68  size = sizeof(CFStringRef);
69  UIname = NULL;
70  err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
71  if (err == noErr) {
72  CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
73  } else {
74  goto error;
75  }
76 
77  size = 256;
78  err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
79  if (err != noErr) {
80  return err;
81  }
82 
83  jack_info("Device name = \'%s\', internal_name = \'%s\' (to be used as -C, -P, or -d parameter)", device_name, internal_name);
84  }
85 
86  return noErr;
87 
88 error:
89  if (UIname != NULL) {
90  CFRelease(UIname);
91  }
92  return err;
93 }
94 
95 static void printError(OSStatus err)
96 {
97  switch (err) {
98  case kAudioHardwareNoError:
99  jack_log("error code : kAudioHardwareNoError");
100  break;
101  case kAudioConverterErr_FormatNotSupported:
102  jack_log("error code : kAudioConverterErr_FormatNotSupported");
103  break;
104  case kAudioConverterErr_OperationNotSupported:
105  jack_log("error code : kAudioConverterErr_OperationNotSupported");
106  break;
107  case kAudioConverterErr_PropertyNotSupported:
108  jack_log("error code : kAudioConverterErr_PropertyNotSupported");
109  break;
110  case kAudioConverterErr_InvalidInputSize:
111  jack_log("error code : kAudioConverterErr_InvalidInputSize");
112  break;
113  case kAudioConverterErr_InvalidOutputSize:
114  jack_log("error code : kAudioConverterErr_InvalidOutputSize");
115  break;
116  case kAudioConverterErr_UnspecifiedError:
117  jack_log("error code : kAudioConverterErr_UnspecifiedError");
118  break;
119  case kAudioConverterErr_BadPropertySizeError:
120  jack_log("error code : kAudioConverterErr_BadPropertySizeError");
121  break;
122  case kAudioConverterErr_RequiresPacketDescriptionsError:
123  jack_log("error code : kAudioConverterErr_RequiresPacketDescriptionsError");
124  break;
125  case kAudioConverterErr_InputSampleRateOutOfRange:
126  jack_log("error code : kAudioConverterErr_InputSampleRateOutOfRange");
127  break;
128  case kAudioConverterErr_OutputSampleRateOutOfRange:
129  jack_log("error code : kAudioConverterErr_OutputSampleRateOutOfRange");
130  break;
131  case kAudioHardwareNotRunningError:
132  jack_log("error code : kAudioHardwareNotRunningError");
133  break;
134  case kAudioHardwareUnknownPropertyError:
135  jack_log("error code : kAudioHardwareUnknownPropertyError");
136  break;
137  case kAudioHardwareIllegalOperationError:
138  jack_log("error code : kAudioHardwareIllegalOperationError");
139  break;
140  case kAudioHardwareBadDeviceError:
141  jack_log("error code : kAudioHardwareBadDeviceError");
142  break;
143  case kAudioHardwareBadStreamError:
144  jack_log("error code : kAudioHardwareBadStreamError");
145  break;
146  case kAudioDeviceUnsupportedFormatError:
147  jack_log("error code : kAudioDeviceUnsupportedFormatError");
148  break;
149  case kAudioDevicePermissionsError:
150  jack_log("error code : kAudioDevicePermissionsError");
151  break;
152  case kAudioHardwareBadObjectError:
153  jack_log("error code : kAudioHardwareBadObjectError");
154  break;
155  case kAudioHardwareUnsupportedOperationError:
156  jack_log("error code : kAudioHardwareUnsupportedOperationError");
157  break;
158  default:
159  jack_log("error code : unknown");
160  break;
161  }
162 }
163 
164 OSStatus JackCoreAudioAdapter::AudioHardwareNotificationCallback(AudioHardwarePropertyID inPropertyID, void* inClientData)
165 {
166  JackCoreAudioAdapter* driver = (JackCoreAudioAdapter*)inClientData;
167 
168  switch (inPropertyID) {
169 
170  case kAudioHardwarePropertyDevices: {
171  jack_log("JackCoreAudioAdapter::AudioHardwareNotificationCallback kAudioHardwarePropertyDevices");
172  DisplayDeviceNames();
173  break;
174  }
175  }
176 
177  return noErr;
178 }
179 
180 OSStatus JackCoreAudioAdapter::SRNotificationCallback(AudioDeviceID inDevice,
181  UInt32 inChannel,
182  Boolean isInput,
183  AudioDevicePropertyID inPropertyID,
184  void* inClientData)
185 {
186  JackCoreAudioAdapter* driver = static_cast<JackCoreAudioAdapter*>(inClientData);
187 
188  switch (inPropertyID) {
189 
190  case kAudioDevicePropertyNominalSampleRate: {
191  jack_log("JackCoreAudioAdapter::SRNotificationCallback kAudioDevicePropertyNominalSampleRate");
192  driver->fState = true;
193  break;
194  }
195  }
196 
197  return noErr;
198 }
199 
200 // A better implementation would try to recover in case of hardware device change (see HALLAB HLFilePlayerWindowControllerAudioDevicePropertyListenerProc code)
201 OSStatus JackCoreAudioAdapter::DeviceNotificationCallback(AudioDeviceID inDevice,
202  UInt32 inChannel,
203  Boolean isInput,
204  AudioDevicePropertyID inPropertyID,
205  void* inClientData)
206 {
207 
208  switch (inPropertyID) {
209 
210  case kAudioDeviceProcessorOverload: {
211  jack_error("JackCoreAudioAdapter::DeviceNotificationCallback kAudioDeviceProcessorOverload");
212  break;
213  }
214 
215  case kAudioDevicePropertyStreamConfiguration: {
216  jack_error("Cannot handle kAudioDevicePropertyStreamConfiguration");
217  return kAudioHardwareUnsupportedOperationError;
218  }
219 
220  case kAudioDevicePropertyNominalSampleRate: {
221  jack_error("Cannot handle kAudioDevicePropertyNominalSampleRate");
222  return kAudioHardwareUnsupportedOperationError;
223  }
224 
225  }
226  return noErr;
227 }
228 
229 int JackCoreAudioAdapter::AddListeners()
230 {
231  OSStatus err = noErr;
232 
233  // Add listeners
234  err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
235  if (err != noErr) {
236  jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
237  printError(err);
238  return -1;
239  }
240 
241  err = AudioHardwareAddPropertyListener(kAudioHardwarePropertyDevices, AudioHardwareNotificationCallback, this);
242  if (err != noErr) {
243  jack_error("Error calling AudioHardwareAddPropertyListener with kAudioHardwarePropertyDevices");
244  printError(err);
245  return -1;
246  }
247 
248  err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback, this);
249  if (err != noErr) {
250  jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
251  printError(err);
252  return -1;
253  }
254 
255  err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback, this);
256  if (err != noErr) {
257  jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
258  printError(err);
259  return -1;
260  }
261 
262  err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
263  if (err != noErr) {
264  jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
265  printError(err);
266  return -1;
267  }
268 
269  err = AudioDeviceAddPropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
270  if (err != noErr) {
271  jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
272  printError(err);
273  return -1;
274  }
275 
276  return 0;
277 }
278 
279 void JackCoreAudioAdapter::RemoveListeners()
280 {
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);
287 }
288 
289 OSStatus JackCoreAudioAdapter::Render(void *inRefCon,
290  AudioUnitRenderActionFlags *ioActionFlags,
291  const AudioTimeStamp *inTimeStamp,
292  UInt32 inBusNumber,
293  UInt32 inNumberFrames,
294  AudioBufferList *ioData)
295 {
296  return static_cast<JackCoreAudioAdapter*>(inRefCon)->Render(ioActionFlags, inTimeStamp, inNumberFrames, ioData);
297 }
298 
299 OSStatus JackCoreAudioAdapter::Render(AudioUnitRenderActionFlags *ioActionFlags,
300  const AudioTimeStamp *inTimeStamp,
301  UInt32 inNumberFrames,
302  AudioBufferList *ioData)
303 {
304  OSStatus err = AudioUnitRender(fAUHAL, ioActionFlags, inTimeStamp, 1, inNumberFrames, fInputData);
305 
306  if (err == noErr) {
307  jack_default_audio_sample_t* inputBuffer[fCaptureChannels];
308  jack_default_audio_sample_t* outputBuffer[fPlaybackChannels];
309 
310  for (int i = 0; i < fCaptureChannels; i++) {
311  inputBuffer[i] = (jack_default_audio_sample_t*)fInputData->mBuffers[i].mData;
312  }
313  for (int i = 0; i < fPlaybackChannels; i++) {
314  outputBuffer[i] = (jack_default_audio_sample_t*)ioData->mBuffers[i].mData;
315  }
316 
317  PushAndPull((jack_default_audio_sample_t**)inputBuffer, (jack_default_audio_sample_t**)outputBuffer, inNumberFrames);
318  return noErr;
319  } else {
320  return err;
321  }
322 }
323 
324 JackCoreAudioAdapter::JackCoreAudioAdapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params)
325  :JackAudioAdapterInterface(buffer_size, sample_rate), fInputData(0), fCapturing(false), fPlaying(false), fState(false)
326 {
327  const JSList* node;
328  const jack_driver_param_t* param;
329  int in_nChannels = 0;
330  int out_nChannels = 0;
331  char captureName[256];
332  char playbackName[256];
333  fCaptureUID[0] = 0;
334  fPlaybackUID[0] = 0;
335  fClockDriftCompensate = false;
336 
337  // Default values
338  fCaptureChannels = -1;
339  fPlaybackChannels = -1;
340 
341  SInt32 major;
342  SInt32 minor;
343  Gestalt(gestaltSystemVersionMajor, &major);
344  Gestalt(gestaltSystemVersionMinor, &minor);
345 
346  // Starting with 10.6 systems, the HAL notification thread is created internally
347  if (major == 10 && minor >= 6) {
348  CFRunLoopRef theRunLoop = NULL;
349  AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
350  OSStatus theError = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
351  if (theError != noErr) {
352  jack_error("JackCoreAudioAdapter::Open kAudioHardwarePropertyRunLoop error");
353  }
354  }
355 
356  for (node = params; node; node = jack_slist_next(node)) {
357  param = (const jack_driver_param_t*) node->data;
358 
359  switch (param->character) {
360 
361  case 'i':
362  fCaptureChannels = param->value.ui;
363  break;
364 
365  case 'o':
366  fPlaybackChannels = param->value.ui;
367  break;
368 
369  case 'C':
370  fCapturing = true;
371  strncpy(fCaptureUID, param->value.str, 256);
372  break;
373 
374  case 'P':
375  fPlaying = true;
376  strncpy(fPlaybackUID, param->value.str, 256);
377  break;
378 
379  case 'd':
380  strncpy(fCaptureUID, param->value.str, 256);
381  strncpy(fPlaybackUID, param->value.str, 256);
382  break;
383 
384  case 'D':
385  fCapturing = fPlaying = true;
386  break;
387 
388  case 'r':
389  SetAdaptedSampleRate(param->value.ui);
390  break;
391 
392  case 'p':
393  SetAdaptedBufferSize(param->value.ui);
394  break;
395 
396  case 'l':
397  DisplayDeviceNames();
398  break;
399 
400  case 'q':
401  fQuality = param->value.ui;
402  break;
403 
404  case 'g':
405  fRingbufferCurSize = param->value.ui;
406  fAdaptative = false;
407  break;
408 
409  case 's':
410  fClockDriftCompensate = true;
411  break;
412  }
413  }
414 
415  /* duplex is the default */
416  if (!fCapturing && !fPlaying) {
417  fCapturing = true;
418  fPlaying = true;
419  }
420 
421  if (SetupDevices(fCaptureUID, fPlaybackUID, captureName, playbackName, fAdaptedSampleRate) < 0) {
422  throw std::bad_alloc();
423  }
424 
425  if (SetupChannels(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, true) < 0) {
426  throw std::bad_alloc();
427  }
428 
429  if (SetupBufferSize(fAdaptedBufferSize) < 0) {
430  throw std::bad_alloc();
431  }
432 
433  if (SetupSampleRate(fAdaptedSampleRate) < 0) {
434  throw std::bad_alloc();
435  }
436 
437  if (OpenAUHAL(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, fAdaptedBufferSize, fAdaptedSampleRate) < 0) {
438  throw std::bad_alloc();
439  }
440 
441  if (fCapturing && fCaptureChannels > 0) {
442  if (SetupBuffers(fCaptureChannels) < 0) {
443  throw std::bad_alloc();
444  }
445  }
446 
447  if (AddListeners() < 0) {
448  throw std::bad_alloc();
449  }
450 
451  GetStreamLatencies(fDeviceID, true, fInputLatencies);
452  GetStreamLatencies(fDeviceID, false, fOutputLatencies);
453 }
454 
455 OSStatus JackCoreAudioAdapter::GetDefaultDevice(AudioDeviceID* id)
456 {
457  OSStatus res;
458  UInt32 theSize = sizeof(UInt32);
459  AudioDeviceID inDefault;
460  AudioDeviceID outDefault;
461 
462  if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
463  return res;
464  }
465 
466  if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
467  return res;
468  }
469 
470  jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
471 
472  // Get the device only if default input and output are the same
473  if (inDefault != outDefault) {
474  jack_error("Default input and output devices are not the same !!");
475  return kAudioHardwareBadDeviceError;
476  } else if (inDefault == 0) {
477  jack_error("Default input and output devices are null !!");
478  return kAudioHardwareBadDeviceError;
479  } else {
480  *id = inDefault;
481  return noErr;
482  }
483 }
484 
485 OSStatus JackCoreAudioAdapter::GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput)
486 {
487  OSStatus err = noErr;
488  UInt32 outSize;
489  Boolean outWritable;
490 
491  channelCount = 0;
492  err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
493  if (err == noErr) {
494  AudioBufferList bufferList[outSize];
495  err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
496  if (err == noErr) {
497  for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++) {
498  channelCount += bufferList->mBuffers[i].mNumberChannels;
499  }
500  }
501  }
502 
503  return err;
504 }
505 
506 OSStatus JackCoreAudioAdapter::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
507 {
508  UInt32 size = sizeof(AudioValueTranslation);
509  CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
510  AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
511 
512  if (inIUD == NULL) {
513  return kAudioHardwareUnspecifiedError;
514  } else {
515  OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
516  CFRelease(inIUD);
517  jack_log("GetDeviceIDFromUID %s %ld", UID, *id);
518  return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
519  }
520 }
521 
522 OSStatus JackCoreAudioAdapter::GetDefaultInputDevice(AudioDeviceID* id)
523 {
524  OSStatus res;
525  UInt32 theSize = sizeof(UInt32);
526  AudioDeviceID inDefault;
527 
528  if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
529  return res;
530  }
531 
532  if (inDefault == 0) {
533  jack_error("Error: default input device is 0, please select a correct one !!");
534  return -1;
535  }
536  jack_log("GetDefaultInputDevice: input = %ld ", inDefault);
537  *id = inDefault;
538  return noErr;
539 }
540 
541 OSStatus JackCoreAudioAdapter::GetDefaultOutputDevice(AudioDeviceID* id)
542 {
543  OSStatus res;
544  UInt32 theSize = sizeof(UInt32);
545  AudioDeviceID outDefault;
546 
547  if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
548  return res;
549  }
550 
551  if (outDefault == 0) {
552  jack_error("Error: default output device is 0, please select a correct one !!");
553  return -1;
554  }
555  jack_log("GetDefaultOutputDevice: output = %ld", outDefault);
556  *id = outDefault;
557  return noErr;
558 }
559 
560 OSStatus JackCoreAudioAdapter::GetDeviceNameFromID(AudioDeviceID id, char* name)
561 {
562  UInt32 size = 256;
563  return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
564 }
565 
566 AudioDeviceID JackCoreAudioAdapter::GetDeviceIDFromName(const char* name)
567 {
568  UInt32 size;
569  Boolean isWritable;
570  int i, deviceNum;
571 
572  OSStatus err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
573  if (err != noErr) {
574  return -1;
575  }
576 
577  deviceNum = size / sizeof(AudioDeviceID);
578  AudioDeviceID devices[deviceNum];
579 
580  err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
581  if (err != noErr) {
582  return err;
583  }
584 
585  for (i = 0; i < deviceNum; i++) {
586  char device_name[256];
587  size = 256;
588  err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
589  if (err != noErr) {
590  return -1;
591  } else if (strcmp(device_name, name) == 0) {
592  return devices[i];
593  }
594  }
595 
596  return -1;
597 }
598 
599 // Setup
600 int JackCoreAudioAdapter::SetupDevices(const char* capture_driver_uid,
601  const char* playback_driver_uid,
602  char* capture_driver_name,
603  char* playback_driver_name,
604  jack_nframes_t samplerate)
605 {
606  capture_driver_name[0] = 0;
607  playback_driver_name[0] = 0;
608 
609  // Duplex
610  if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
611  jack_log("JackCoreAudioDriver::Open duplex");
612 
613  // Same device for capture and playback...
614  if (strcmp(capture_driver_uid, playback_driver_uid) == 0) {
615 
616  if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
617  jack_log("Will take default in/out");
618  if (GetDefaultDevice(&fDeviceID) != noErr) {
619  jack_error("Cannot open default device");
620  return -1;
621  }
622  }
623  if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
624  jack_error("Cannot get device name from device ID");
625  return -1;
626  }
627 
628  } else {
629 
630  // Creates aggregate device
631  AudioDeviceID captureID, playbackID;
632 
633  if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr) {
634  jack_log("Will take default input");
635  if (GetDefaultInputDevice(&captureID) != noErr) {
636  jack_error("Cannot open default input device");
637  return -1;
638  }
639  }
640 
641  if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr) {
642  jack_log("Will take default output");
643  if (GetDefaultOutputDevice(&playbackID) != noErr) {
644  jack_error("Cannot open default output device");
645  return -1;
646  }
647  }
648 
649  if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
650  return -1;
651  }
652  }
653 
654  // Capture only
655  } else if (strcmp(capture_driver_uid, "") != 0) {
656  jack_log("JackCoreAudioAdapter::Open capture only");
657  if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
658  if (GetDefaultInputDevice(&fDeviceID) != noErr) {
659  jack_error("Cannot open default input device");
660  return -1;
661  }
662  }
663  if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
664  jack_error("Cannot get device name from device ID");
665  return -1;
666  }
667 
668  // Playback only
669  } else if (strcmp(playback_driver_uid, "") != 0) {
670  jack_log("JackCoreAudioAdapter::Open playback only");
671  if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
672  if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
673  jack_error("Cannot open default output device");
674  return -1;
675  }
676  }
677  if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
678  jack_error("Cannot get device name from device ID");
679  return -1;
680  }
681 
682  // Use default driver in duplex mode
683  } else {
684  jack_log("JackCoreAudioAdapter::Open default driver");
685  if (GetDefaultDevice(&fDeviceID) != noErr) {
686  jack_error("Cannot open default device in duplex mode, so aggregate default input and default output");
687 
688  // Creates aggregate device
689  AudioDeviceID captureID = -1, playbackID = -1;
690 
691  if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr) {
692  jack_log("Will take default input");
693  if (GetDefaultInputDevice(&captureID) != noErr) {
694  jack_error("Cannot open default input device");
695  goto built_in;
696  }
697  }
698 
699  if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr) {
700  jack_log("Will take default output");
701  if (GetDefaultOutputDevice(&playbackID) != noErr) {
702  jack_error("Cannot open default output device");
703  goto built_in;
704  }
705  }
706 
707  if (captureID > 0 && playbackID > 0) {
708  if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
709  goto built_in;
710  }
711  } else {
712  jack_error("Cannot use default input/output");
713  goto built_in;
714  }
715  }
716  }
717 
718  return 0;
719 
720 built_in:
721 
722  // Aggregate built-in input and output
723  AudioDeviceID captureID = GetDeviceIDFromName("Built-in Input");
724  AudioDeviceID playbackID = GetDeviceIDFromName("Built-in Output");
725 
726  if (captureID > 0 && playbackID > 0) {
727  if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
728  return -1;
729  }
730  } else {
731  jack_error("Cannot aggregate built-in input and output");
732  return -1;
733  }
734 
735  return 0;
736 }
737 
738 int JackCoreAudioAdapter::SetupChannels(bool capturing,
739  bool playing,
740  int& inchannels,
741  int& outchannels,
742  int& in_nChannels,
743  int& out_nChannels,
744  bool strict)
745 {
746  OSStatus err = noErr;
747 
748  if (capturing) {
749  err = GetTotalChannels(fDeviceID, in_nChannels, true);
750  if (err != noErr) {
751  jack_error("Cannot get input channel number");
752  printError(err);
753  return -1;
754  } else {
755  jack_log("Max input channels : %d", in_nChannels);
756  }
757  }
758 
759  if (playing) {
760  err = GetTotalChannels(fDeviceID, out_nChannels, false);
761  if (err != noErr) {
762  jack_error("Cannot get output channel number");
763  printError(err);
764  return -1;
765  } else {
766  jack_log("Max output channels : %d", out_nChannels);
767  }
768  }
769 
770  if (inchannels > in_nChannels) {
771  jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
772  if (strict) {
773  return -1;
774  }
775  }
776 
777  if (outchannels > out_nChannels) {
778  jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
779  if (strict) {
780  return -1;
781  }
782  }
783 
784  if (inchannels == -1) {
785  jack_log("Setup max in channels = %ld", in_nChannels);
786  inchannels = in_nChannels;
787  }
788 
789  if (outchannels == -1) {
790  jack_log("Setup max out channels = %ld", out_nChannels);
791  outchannels = out_nChannels;
792  }
793 
794  return 0;
795 }
796 
797 int JackCoreAudioAdapter::SetupBufferSize(jack_nframes_t buffer_size)
798 {
799  // Setting buffer size
800  UInt32 outSize = sizeof(UInt32);
801  OSStatus err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
802  if (err != noErr) {
803  jack_error("Cannot set buffer size %ld", buffer_size);
804  printError(err);
805  return -1;
806  }
807 
808  return 0;
809 }
810 
811 int JackCoreAudioAdapter::SetupSampleRate(jack_nframes_t samplerate)
812 {
813  return SetupSampleRateAux(fDeviceID, samplerate);
814 }
815 
816 int JackCoreAudioAdapter::SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate)
817 {
818  OSStatus err = noErr;
819  UInt32 outSize;
820  Float64 sampleRate;
821 
822  // Get sample rate
823  outSize = sizeof(Float64);
824  err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
825  if (err != noErr) {
826  jack_error("Cannot get current sample rate");
827  printError(err);
828  return -1;
829  } else {
830  jack_log("Current sample rate = %f", sampleRate);
831  }
832 
833  // If needed, set new sample rate
834  if (samplerate != (jack_nframes_t)sampleRate) {
835  sampleRate = (Float64)samplerate;
836 
837  // To get SR change notification
838  err = AudioDeviceAddPropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
839  if (err != noErr) {
840  jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
841  printError(err);
842  return -1;
843  }
844  err = AudioDeviceSetProperty(inDevice, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
845  if (err != noErr) {
846  jack_error("Cannot set sample rate = %ld", samplerate);
847  printError(err);
848  return -1;
849  }
850 
851  // Waiting for SR change notification
852  int count = 0;
853  while (!fState && count++ < WAIT_COUNTER) {
854  usleep(100000);
855  jack_log("Wait count = %d", count);
856  }
857 
858  // Remove SR change notification
859  AudioDeviceRemovePropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
860  }
861 
862  return 0;
863 }
864 
865 int JackCoreAudioAdapter::SetupBuffers(int inchannels)
866 {
867  jack_log("JackCoreAudioAdapter::SetupBuffers: input = %ld", inchannels);
868 
869  // Prepare buffers
870  fInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
871  fInputData->mNumberBuffers = inchannels;
872  for (int i = 0; i < fCaptureChannels; i++) {
873  fInputData->mBuffers[i].mNumberChannels = 1;
874  fInputData->mBuffers[i].mDataByteSize = fAdaptedBufferSize * sizeof(jack_default_audio_sample_t);
875  fInputData->mBuffers[i].mData = malloc(fAdaptedBufferSize * sizeof(jack_default_audio_sample_t));
876  }
877  return 0;
878 }
879 
880 void JackCoreAudioAdapter::DisposeBuffers()
881 {
882  if (fInputData) {
883  for (int i = 0; i < fCaptureChannels; i++) {
884  free(fInputData->mBuffers[i].mData);
885  }
886  free(fInputData);
887  fInputData = 0;
888  }
889 }
890 
891 int JackCoreAudioAdapter::OpenAUHAL(bool capturing,
892  bool playing,
893  int inchannels,
894  int outchannels,
895  int in_nChannels,
896  int out_nChannels,
897  jack_nframes_t buffer_size,
898  jack_nframes_t samplerate)
899 {
900  ComponentResult err1;
901  UInt32 enableIO;
902  AudioStreamBasicDescription srcFormat, dstFormat;
903  AudioDeviceID currAudioDeviceID;
904  UInt32 size;
905 
906  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);
907 
908  if (inchannels == 0 && outchannels == 0) {
909  jack_error("No input and output channels...");
910  return -1;
911  }
912 
913  // AUHAL
914 #ifdef MAC_OS_X_VERSION_10_5
915  ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
916  Component HALOutput = FindNextComponent(NULL, &cd);
917  err1 = OpenAComponent(HALOutput, &fAUHAL);
918  if (err1 != noErr) {
919  jack_error("Error calling OpenAComponent");
920  printError(err1);
921  goto error;
922  }
923 #else
924  AudioComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
925  AudioComponent HALOutput = AudioComponentFindNext(NULL, &cd);
926  err1 = AudioComponentInstanceNew(HALOutput, &fAUHAL);
927  if (err1 != noErr) {
928  jack_error("Error calling AudioComponentInstanceNew");
929  printError(err1);
930  goto error;
931  }
932 #endif
933 
934  err1 = AudioUnitInitialize(fAUHAL);
935  if (err1 != noErr) {
936  jack_error("Cannot initialize AUHAL unit");
937  printError(err1);
938  goto error;
939  }
940 
941  // Start I/O
942  if (capturing && inchannels > 0) {
943  enableIO = 1;
944  jack_log("Setup AUHAL input on");
945  } else {
946  enableIO = 0;
947  jack_log("Setup AUHAL input off");
948  }
949 
950  err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
951  if (err1 != noErr) {
952  jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
953  printError(err1);
954  goto error;
955  }
956 
957  if (playing && outchannels > 0) {
958  enableIO = 1;
959  jack_log("Setup AUHAL output on");
960  } else {
961  enableIO = 0;
962  jack_log("Setup AUHAL output off");
963  }
964 
965  err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
966  if (err1 != noErr) {
967  jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
968  printError(err1);
969  goto error;
970  }
971 
972  size = sizeof(AudioDeviceID);
973  err1 = AudioUnitGetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &currAudioDeviceID, &size);
974  if (err1 != noErr) {
975  jack_error("Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
976  printError(err1);
977  goto error;
978  } else {
979  jack_log("AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID);
980  }
981 
982  // Setup up choosen device, in both input and output cases
983  err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
984  if (err1 != noErr) {
985  jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
986  printError(err1);
987  goto error;
988  }
989 
990  // Set buffer size
991  if (capturing && inchannels > 0) {
992  err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&buffer_size, sizeof(UInt32));
993  if (err1 != noErr) {
994  jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
995  printError(err1);
996  goto error;
997  }
998  }
999 
1000  if (playing && outchannels > 0) {
1001  err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&buffer_size, sizeof(UInt32));
1002  if (err1 != noErr) {
1003  jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
1004  printError(err1);
1005  goto error;
1006  }
1007  }
1008 
1009  // Setup channel map
1010  if (capturing && inchannels > 0 && inchannels <= in_nChannels) {
1011  SInt32 chanArr[in_nChannels];
1012  for (int i = 0; i < in_nChannels; i++) {
1013  chanArr[i] = -1;
1014  }
1015  for (int i = 0; i < inchannels; i++) {
1016  chanArr[i] = i;
1017  }
1018  AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
1019  if (err1 != noErr) {
1020  jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
1021  printError(err1);
1022  goto error;
1023  }
1024  }
1025 
1026  if (playing && outchannels > 0 && outchannels <= out_nChannels) {
1027  SInt32 chanArr[out_nChannels];
1028  for (int i = 0; i < out_nChannels; i++) {
1029  chanArr[i] = -1;
1030  }
1031  for (int i = 0; i < outchannels; i++) {
1032  chanArr[i] = i;
1033  }
1034  err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
1035  if (err1 != noErr) {
1036  jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
1037  printError(err1);
1038  goto error;
1039  }
1040  }
1041 
1042  // Setup stream converters
1043  if (capturing && inchannels > 0) {
1044 
1045  size = sizeof(AudioStreamBasicDescription);
1046  err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &srcFormat, &size);
1047  if (err1 != noErr) {
1048  jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
1049  printError(err1);
1050  goto error;
1051  }
1052  PrintStreamDesc(&srcFormat);
1053 
1054  jack_log("Setup AUHAL input stream converter SR = %ld", samplerate);
1055  srcFormat.mSampleRate = samplerate;
1056  srcFormat.mFormatID = kAudioFormatLinearPCM;
1057  srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
1058  srcFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
1059  srcFormat.mFramesPerPacket = 1;
1060  srcFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
1061  srcFormat.mChannelsPerFrame = inchannels;
1062  srcFormat.mBitsPerChannel = 32;
1063  PrintStreamDesc(&srcFormat);
1064 
1065  err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, sizeof(AudioStreamBasicDescription));
1066 
1067  if (err1 != noErr) {
1068  jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
1069  printError(err1);
1070  goto error;
1071  }
1072  }
1073 
1074  if (playing && outchannels > 0) {
1075 
1076  size = sizeof(AudioStreamBasicDescription);
1077  err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &dstFormat, &size);
1078  if (err1 != noErr) {
1079  jack_error("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
1080  printError(err1);
1081  goto error;
1082  }
1083  PrintStreamDesc(&dstFormat);
1084 
1085  jack_log("Setup AUHAL output stream converter SR = %ld", samplerate);
1086  dstFormat.mSampleRate = samplerate;
1087  dstFormat.mFormatID = kAudioFormatLinearPCM;
1088  dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
1089  dstFormat.mBytesPerPacket = sizeof(jack_default_audio_sample_t);
1090  dstFormat.mFramesPerPacket = 1;
1091  dstFormat.mBytesPerFrame = sizeof(jack_default_audio_sample_t);
1092  dstFormat.mChannelsPerFrame = outchannels;
1093  dstFormat.mBitsPerChannel = 32;
1094  PrintStreamDesc(&dstFormat);
1095 
1096  err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, sizeof(AudioStreamBasicDescription));
1097 
1098  if (err1 != noErr) {
1099  jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
1100  printError(err1);
1101  goto error;
1102  }
1103  }
1104 
1105  // Setup callbacks
1106  if (inchannels > 0 && outchannels == 0) {
1107  AURenderCallbackStruct output;
1108  output.inputProc = Render;
1109  output.inputProcRefCon = this;
1110  err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
1111  if (err1 != noErr) {
1112  jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
1113  printError(err1);
1114  goto error;
1115  }
1116  } else {
1117  AURenderCallbackStruct output;
1118  output.inputProc = Render;
1119  output.inputProcRefCon = this;
1120  err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
1121  if (err1 != noErr) {
1122  jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
1123  printError(err1);
1124  goto error;
1125  }
1126  }
1127 
1128  return 0;
1129 
1130 error:
1131  CloseAUHAL();
1132  return -1;
1133 }
1134 
1135 OSStatus JackCoreAudioAdapter::DestroyAggregateDevice()
1136 {
1137  OSStatus osErr = noErr;
1138  AudioObjectPropertyAddress pluginAOPA;
1139  pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice;
1140  pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
1141  pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
1142  UInt32 outDataSize;
1143 
1144  osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
1145  if (osErr != noErr) {
1146  jack_error("JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
1147  printError(osErr);
1148  return osErr;
1149  }
1150 
1151  osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID);
1152  if (osErr != noErr) {
1153  jack_error("JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyData error");
1154  printError(osErr);
1155  return osErr;
1156  }
1157 
1158  return noErr;
1159 }
1160 
1161 static CFStringRef GetDeviceName(AudioDeviceID id)
1162 {
1163  UInt32 size = sizeof(CFStringRef);
1164  CFStringRef UIname;
1165  OSStatus err = AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
1166  return (err == noErr) ? UIname : NULL;
1167 }
1168 
1169 OSStatus JackCoreAudioAdapter::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
1170 {
1171  OSStatus err = noErr;
1172  AudioObjectID sub_device[32];
1173  UInt32 outSize = sizeof(sub_device);
1174 
1175  err = AudioDeviceGetProperty(captureDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
1176  vector<AudioDeviceID> captureDeviceIDArray;
1177 
1178  if (err != noErr) {
1179  jack_log("Input device does not have subdevices");
1180  captureDeviceIDArray.push_back(captureDeviceID);
1181  } else {
1182  int num_devices = outSize / sizeof(AudioObjectID);
1183  jack_log("Input device has %d subdevices", num_devices);
1184  for (int i = 0; i < num_devices; i++) {
1185  captureDeviceIDArray.push_back(sub_device[i]);
1186  }
1187  }
1188 
1189  outSize = sizeof(sub_device);
1190  err = AudioDeviceGetProperty(playbackDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
1191  vector<AudioDeviceID> playbackDeviceIDArray;
1192 
1193  if (err != noErr) {
1194  jack_log("Output device does not have subdevices");
1195  playbackDeviceIDArray.push_back(playbackDeviceID);
1196  } else {
1197  int num_devices = outSize / sizeof(AudioObjectID);
1198  jack_log("Output device has %d subdevices", num_devices);
1199  for (int i = 0; i < num_devices; i++) {
1200  playbackDeviceIDArray.push_back(sub_device[i]);
1201  }
1202  }
1203 
1204  return CreateAggregateDeviceAux(captureDeviceIDArray, playbackDeviceIDArray, samplerate, outAggregateDevice);
1205 }
1206 
1207 OSStatus JackCoreAudioAdapter::CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
1208 {
1209  OSStatus osErr = noErr;
1210  UInt32 outSize;
1211  Boolean outWritable;
1212 
1213  // Prepare sub-devices for clock drift compensation
1214  // Workaround for bug in the HAL : until 10.6.2
1215  AudioObjectPropertyAddress theAddressOwned = { kAudioObjectPropertyOwnedObjects, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
1216  AudioObjectPropertyAddress theAddressDrift = { kAudioSubDevicePropertyDriftCompensation, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
1217  UInt32 theQualifierDataSize = sizeof(AudioObjectID);
1218  AudioClassID inClass = kAudioSubDeviceClassID;
1219  void* theQualifierData = &inClass;
1220  UInt32 subDevicesNum = 0;
1221 
1222  //---------------------------------------------------------------------------
1223  // Setup SR of both devices otherwise creating AD may fail...
1224  //---------------------------------------------------------------------------
1225  UInt32 keptclockdomain = 0;
1226  UInt32 clockdomain = 0;
1227  outSize = sizeof(UInt32);
1228  bool need_clock_drift_compensation = false;
1229 
1230  for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
1231  if (SetupSampleRateAux(captureDeviceID[i], samplerate) < 0) {
1232  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : cannot set SR of input device");
1233  } else {
1234  // Check clock domain
1235  osErr = AudioDeviceGetProperty(captureDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
1236  if (osErr != 0) {
1237  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
1238  printError(osErr);
1239  } else {
1240  keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
1241  jack_log("JackCoreAudioAdapter::CreateAggregateDevice : input clockdomain = %d", clockdomain);
1242  if (clockdomain != 0 && clockdomain != keptclockdomain) {
1243  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...");
1244  need_clock_drift_compensation = true;
1245  }
1246  }
1247  }
1248  }
1249 
1250  for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
1251  if (SetupSampleRateAux(playbackDeviceID[i], samplerate) < 0) {
1252  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : cannot set SR of output device");
1253  } else {
1254  // Check clock domain
1255  osErr = AudioDeviceGetProperty(playbackDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
1256  if (osErr != 0) {
1257  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
1258  printError(osErr);
1259  } else {
1260  keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
1261  jack_log("JackCoreAudioAdapter::CreateAggregateDevice : output clockdomain = %d", clockdomain);
1262  if (clockdomain != 0 && clockdomain != keptclockdomain) {
1263  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...");
1264  need_clock_drift_compensation = true;
1265  }
1266  }
1267  }
1268  }
1269 
1270  // If no valid clock domain was found, then assume we have to compensate...
1271  if (keptclockdomain == 0) {
1272  need_clock_drift_compensation = true;
1273  }
1274 
1275  //---------------------------------------------------------------------------
1276  // Start to create a new aggregate by getting the base audio hardware plugin
1277  //---------------------------------------------------------------------------
1278 
1279  char device_name[256];
1280  for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
1281  GetDeviceNameFromID(captureDeviceID[i], device_name);
1282  jack_info("Separated input = '%s' ", device_name);
1283  }
1284 
1285  for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
1286  GetDeviceNameFromID(playbackDeviceID[i], device_name);
1287  jack_info("Separated output = '%s' ", device_name);
1288  }
1289 
1290  osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable);
1291  if (osErr != noErr) {
1292  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioHardwareGetPropertyInfo kAudioHardwarePropertyPlugInForBundleID error");
1293  printError(osErr);
1294  return osErr;
1295  }
1296 
1297  AudioValueTranslation pluginAVT;
1298 
1299  CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
1300 
1301  pluginAVT.mInputData = &inBundleRef;
1302  pluginAVT.mInputDataSize = sizeof(inBundleRef);
1303  pluginAVT.mOutputData = &fPluginID;
1304  pluginAVT.mOutputDataSize = sizeof(fPluginID);
1305 
1306  osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT);
1307  if (osErr != noErr) {
1308  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioHardwareGetProperty kAudioHardwarePropertyPlugInForBundleID error");
1309  printError(osErr);
1310  return osErr;
1311  }
1312 
1313  //-------------------------------------------------
1314  // Create a CFDictionary for our aggregate device
1315  //-------------------------------------------------
1316 
1317  CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
1318 
1319  CFStringRef AggregateDeviceNameRef = CFSTR("JackDuplex");
1320  CFStringRef AggregateDeviceUIDRef = CFSTR("com.grame.JackDuplex");
1321 
1322  // add the name of the device to the dictionary
1323  CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
1324 
1325  // add our choice of UID for the aggregate device to the dictionary
1326  CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
1327 
1328  // add a "private aggregate key" to the dictionary
1329  int value = 1;
1330  CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
1331 
1332  SInt32 system;
1333  Gestalt(gestaltSystemVersion, &system);
1334 
1335  jack_log("JackCoreAudioAdapter::CreateAggregateDevice : system version = %x limit = %x", system, 0x00001054);
1336 
1337  // Starting with 10.5.4 systems, the AD can be internal... (better)
1338  if (system < 0x00001054) {
1339  jack_log("JackCoreAudioAdapter::CreateAggregateDevice : public aggregate device....");
1340  } else {
1341  jack_log("JackCoreAudioAdapter::CreateAggregateDevice : private aggregate device....");
1342  CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
1343  }
1344 
1345  // Prepare sub-devices for clock drift compensation
1346  CFMutableArrayRef subDevicesArrayClock = NULL;
1347 
1348  /*
1349  if (fClockDriftCompensate) {
1350  if (need_clock_drift_compensation) {
1351  jack_info("Clock drift compensation activated...");
1352  subDevicesArrayClock = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
1353 
1354  for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
1355  CFStringRef UID = GetDeviceName(captureDeviceID[i]);
1356  if (UID) {
1357  CFMutableDictionaryRef subdeviceAggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
1358  CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceUIDKey), UID);
1359  CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceDriftCompensationKey), AggregateDeviceNumberRef);
1360  //CFRelease(UID);
1361  CFArrayAppendValue(subDevicesArrayClock, subdeviceAggDeviceDict);
1362  }
1363  }
1364 
1365  for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
1366  CFStringRef UID = GetDeviceName(playbackDeviceID[i]);
1367  if (UID) {
1368  CFMutableDictionaryRef subdeviceAggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
1369  CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceUIDKey), UID);
1370  CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceDriftCompensationKey), AggregateDeviceNumberRef);
1371  //CFRelease(UID);
1372  CFArrayAppendValue(subDevicesArrayClock, subdeviceAggDeviceDict);
1373  }
1374  }
1375 
1376  // add sub-device clock array for the aggregate device to the dictionary
1377  CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceSubDeviceListKey), subDevicesArrayClock);
1378  } else {
1379  jack_info("Clock drift compensation was asked but is not needed (devices use the same clock domain)");
1380  }
1381  }
1382  */
1383 
1384  //-------------------------------------------------
1385  // Create a CFMutableArray for our sub-device list
1386  //-------------------------------------------------
1387 
1388  // we need to append the UID for each device to a CFMutableArray, so create one here
1389  CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
1390 
1391  vector<CFStringRef> captureDeviceUID;
1392  for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
1393  CFStringRef ref = GetDeviceName(captureDeviceID[i]);
1394  if (ref == NULL) {
1395  return -1;
1396  }
1397  captureDeviceUID.push_back(ref);
1398  // input sub-devices in this example, so append the sub-device's UID to the CFArray
1399  CFArrayAppendValue(subDevicesArray, ref);
1400  }
1401 
1402  vector<CFStringRef> playbackDeviceUID;
1403  for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
1404  CFStringRef ref = GetDeviceName(playbackDeviceID[i]);
1405  if (ref == NULL) {
1406  return -1;
1407  }
1408  playbackDeviceUID.push_back(ref);
1409  // output sub-devices in this example, so append the sub-device's UID to the CFArray
1410  CFArrayAppendValue(subDevicesArray, ref);
1411  }
1412 
1413  //-----------------------------------------------------------------------
1414  // Feed the dictionary to the plugin, to create a blank aggregate device
1415  //-----------------------------------------------------------------------
1416 
1417  AudioObjectPropertyAddress pluginAOPA;
1418  pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
1419  pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
1420  pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
1421  UInt32 outDataSize;
1422 
1423  osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
1424  if (osErr != noErr) {
1425  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectGetPropertyDataSize error");
1426  printError(osErr);
1427  goto error;
1428  }
1429 
1430  osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, outAggregateDevice);
1431  if (osErr != noErr) {
1432  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectGetPropertyData error");
1433  printError(osErr);
1434  goto error;
1435  }
1436 
1437  // pause for a bit to make sure that everything completed correctly
1438  // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
1439  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
1440 
1441  //-------------------------
1442  // Set the sub-device list
1443  //-------------------------
1444 
1445  pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
1446  pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
1447  pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
1448  outDataSize = sizeof(CFMutableArrayRef);
1449  osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
1450  if (osErr != noErr) {
1451  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectSetPropertyData for sub-device list error");
1452  printError(osErr);
1453  goto error;
1454  }
1455 
1456  // pause again to give the changes time to take effect
1457  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
1458 
1459  //-----------------------
1460  // Set the master device
1461  //-----------------------
1462 
1463  // set the master device manually (this is the device which will act as the master clock for the aggregate device)
1464  // pass in the UID of the device you want to use
1465  pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
1466  pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
1467  pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
1468  outDataSize = sizeof(CFStringRef);
1469  osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID[0]); // First capture is master...
1470  if (osErr != noErr) {
1471  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectSetPropertyData for master device error");
1472  printError(osErr);
1473  goto error;
1474  }
1475 
1476  // pause again to give the changes time to take effect
1477  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
1478 
1479  // Prepare sub-devices for clock drift compensation
1480  // Workaround for bug in the HAL : until 10.6.2
1481 
1482  if (fClockDriftCompensate) {
1483  if (need_clock_drift_compensation) {
1484  jack_info("Clock drift compensation activated...");
1485 
1486  // Get the property data size
1487  osErr = AudioObjectGetPropertyDataSize(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize);
1488  if (osErr != noErr) {
1489  jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
1490  printError(osErr);
1491  }
1492 
1493  // Calculate the number of object IDs
1494  subDevicesNum = outSize / sizeof(AudioObjectID);
1495  jack_info("JackCoreAudioAdapter::CreateAggregateDevice clock drift compensation, number of sub-devices = %d", subDevicesNum);
1496  AudioObjectID subDevices[subDevicesNum];
1497  outSize = sizeof(subDevices);
1498 
1499  osErr = AudioObjectGetPropertyData(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize, subDevices);
1500  if (osErr != noErr) {
1501  jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
1502  printError(osErr);
1503  }
1504 
1505  // Set kAudioSubDevicePropertyDriftCompensation property...
1506  for (UInt32 index = 0; index < subDevicesNum; ++index) {
1507  UInt32 theDriftCompensationValue = 1;
1508  osErr = AudioObjectSetPropertyData(subDevices[index], &theAddressDrift, 0, NULL, sizeof(UInt32), &theDriftCompensationValue);
1509  if (osErr != noErr) {
1510  jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioSubDevicePropertyDriftCompensation error");
1511  printError(osErr);
1512  }
1513  }
1514  } else {
1515  jack_info("Clock drift compensation was asked but is not needed (devices use the same clock domain)");
1516  }
1517  }
1518 
1519  // pause again to give the changes time to take effect
1520  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
1521 
1522  //----------
1523  // Clean up
1524  //----------
1525 
1526  // release the private AD key
1527  CFRelease(AggregateDeviceNumberRef);
1528 
1529  // release the CF objects we have created - we don't need them any more
1530  CFRelease(aggDeviceDict);
1531  CFRelease(subDevicesArray);
1532 
1533  if (subDevicesArrayClock) {
1534  CFRelease(subDevicesArrayClock);
1535  }
1536 
1537  // release the device UID
1538  for (UInt32 i = 0; i < captureDeviceUID.size(); i++) {
1539  CFRelease(captureDeviceUID[i]);
1540  }
1541 
1542  for (UInt32 i = 0; i < playbackDeviceUID.size(); i++) {
1543  CFRelease(playbackDeviceUID[i]);
1544  }
1545 
1546  jack_log("New aggregate device %ld", *outAggregateDevice);
1547  return noErr;
1548 
1549 error:
1550  DestroyAggregateDevice();
1551  return -1;
1552 }
1553 
1554 
1555 bool JackCoreAudioAdapter::IsAggregateDevice(AudioDeviceID device)
1556 {
1557  OSStatus err = noErr;
1558  AudioObjectID sub_device[32];
1559  UInt32 outSize = sizeof(sub_device);
1560  err = AudioDeviceGetProperty(device, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
1561 
1562  if (err != noErr) {
1563  jack_log("Device does not have subdevices");
1564  return false;
1565  } else {
1566  int num_devices = outSize / sizeof(AudioObjectID);
1567  jack_log("Device does has %d subdevices", num_devices);
1568  return true;
1569  }
1570 }
1571 
1572 void JackCoreAudioAdapter::CloseAUHAL()
1573 {
1574  AudioUnitUninitialize(fAUHAL);
1575  CloseComponent(fAUHAL);
1576 }
1577 
1578 int JackCoreAudioAdapter::Open()
1579 {
1580  return (AudioOutputUnitStart(fAUHAL) != noErr) ? -1 : 0;
1581 }
1582 
1583 int JackCoreAudioAdapter::Close()
1584 {
1585 #ifdef JACK_MONITOR
1586  fTable.Save(fHostBufferSize, fHostSampleRate, fAdaptedSampleRate, fAdaptedBufferSize);
1587 #endif
1588  AudioOutputUnitStop(fAUHAL);
1589  DisposeBuffers();
1590  CloseAUHAL();
1591  RemoveListeners();
1592  if (fPluginID > 0) {
1593  DestroyAggregateDevice();
1594  }
1595  return 0;
1596 }
1597 
1598 int JackCoreAudioAdapter::SetSampleRate(jack_nframes_t sample_rate)
1599 {
1600  JackAudioAdapterInterface::SetHostSampleRate(sample_rate);
1601  Close();
1602  return Open();
1603 }
1604 
1605 int JackCoreAudioAdapter::SetBufferSize(jack_nframes_t buffer_size)
1606 {
1607  JackAudioAdapterInterface::SetHostBufferSize(buffer_size);
1608  Close();
1609  return Open();
1610 }
1611 
1612 OSStatus JackCoreAudioAdapter::GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies)
1613 {
1614  OSStatus err = noErr;
1615  UInt32 outSize1, outSize2, outSize3;
1616  Boolean outWritable;
1617 
1618  err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreams, &outSize1, &outWritable);
1619  if (err == noErr) {
1620  int stream_count = outSize1 / sizeof(UInt32);
1621  AudioStreamID streamIDs[stream_count];
1622  AudioBufferList bufferList[stream_count];
1623  UInt32 streamLatency;
1624  outSize2 = sizeof(UInt32);
1625 
1626  err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreams, &outSize1, streamIDs);
1627  if (err != noErr) {
1628  jack_error("GetStreamLatencies kAudioDevicePropertyStreams err = %d", err);
1629  return err;
1630  }
1631 
1632  err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize3, &outWritable);
1633  if (err != noErr) {
1634  jack_error("GetStreamLatencies kAudioDevicePropertyStreamConfiguration err = %d", err);
1635  return err;
1636  }
1637 
1638  for (int i = 0; i < stream_count; i++) {
1639  err = AudioStreamGetProperty(streamIDs[i], 0, kAudioStreamPropertyLatency, &outSize2, &streamLatency);
1640  if (err != noErr) {
1641  jack_error("GetStreamLatencies kAudioStreamPropertyLatency err = %d", err);
1642  return err;
1643  }
1644  err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize3, bufferList);
1645  if (err != noErr) {
1646  jack_error("GetStreamLatencies kAudioDevicePropertyStreamConfiguration err = %d", err);
1647  return err;
1648  }
1649  // Push 'channel' time the stream latency
1650  for (uint k = 0; k < bufferList->mBuffers[i].mNumberChannels; k++) {
1651  latencies.push_back(streamLatency);
1652  }
1653  }
1654  }
1655  return err;
1656 }
1657 
1658 int JackCoreAudioAdapter::GetLatency(int port_index, bool input)
1659 {
1660  UInt32 size = sizeof(UInt32);
1661  UInt32 value1 = 0;
1662  UInt32 value2 = 0;
1663 
1664  OSStatus err = AudioDeviceGetProperty(fDeviceID, 0, input, kAudioDevicePropertyLatency, &size, &value1);
1665  if (err != noErr) {
1666  jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error");
1667  }
1668  err = AudioDeviceGetProperty(fDeviceID, 0, input, kAudioDevicePropertySafetyOffset, &size, &value2);
1669  if (err != noErr) {
1670  jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error");
1671  }
1672 
1673  // TODO : add stream latency
1674 
1675  return value1 + value2 + fAdaptedBufferSize;
1676 }
1677 
1678 int JackCoreAudioAdapter::GetInputLatency(int port_index)
1679 {
1680  if (port_index < int(fInputLatencies.size())) {
1681  return GetLatency(port_index, true) + fInputLatencies[port_index];
1682  } else {
1683  // No stream latency
1684  return GetLatency(port_index, true);
1685  }
1686 }
1687 
1688 int JackCoreAudioAdapter::GetOutputLatency(int port_index)
1689 {
1690  if (port_index < int(fOutputLatencies.size())) {
1691  return GetLatency(port_index, false) + fOutputLatencies[port_index];
1692  } else {
1693  // No stream latency
1694  return GetLatency(port_index, false);
1695  }
1696 }
1697 
1698 } // namespace
1699 
1700 #ifdef __cplusplus
1701 extern "C"
1702 {
1703 #endif
1704 
1705  SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor()
1706  {
1707  jack_driver_desc_t * desc;
1710 
1711  desc = jack_driver_descriptor_construct("audioadapter", JackDriverNone, "netjack audio <==> net backend adapter", &filler);
1712 
1713  value.i = -1;
1714  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");
1715  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");
1716 
1717  value.str[0] = 0;
1718  jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Input CoreAudio device name", NULL);
1719  jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Output CoreAudio device name", NULL);
1720 
1721  value.ui = 44100U;
1722  jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
1723 
1724  value.ui = 512U;
1725  jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
1726 
1727  value.i = true;
1728  jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
1729 
1730  value.str[0] = 0;
1731  jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "CoreAudio device name", NULL);
1732 
1733  value.i = true;
1734  jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available CoreAudio devices", NULL);
1735 
1736  value.ui = 0;
1737  jack_driver_descriptor_add_parameter(desc, &filler, "quality", 'q', JackDriverParamInt, &value, NULL, "Resample algorithm quality (0 - 4)", NULL);
1738 
1739  value.ui = 32768;
1740  jack_driver_descriptor_add_parameter(desc, &filler, "ring-buffer", 'g', JackDriverParamInt, &value, NULL, "Fixed ringbuffer size", "Fixed ringbuffer size (if not set => automatic adaptative)");
1741 
1742  value.i = false;
1743  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");
1744 
1745  value.i = false;
1746  jack_driver_descriptor_add_parameter(desc, &filler, "auto-connect", 'c', JackDriverParamBool, &value, NULL, "Auto connect audioadapter to system ports", NULL);
1747 
1748  return desc;
1749  }
1750 
1751 
1752 #ifdef __cplusplus
1753 }
1754 #endif
1755 
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
SERVER_EXPORT void jack_info(const char *fmt,...)
Definition: JackError.cpp:100
int32_t i
member used for JackParamInt
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108