Jack2  1.9.9
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  JackCoreAudioAdapter* adapter = static_cast<JackCoreAudioAdapter*>(inRefCon);
297  OSStatus err = AudioUnitRender(adapter->fAUHAL, ioActionFlags, inTimeStamp, 1, inNumberFrames, adapter->fInputData);
298 
299  if (err == noErr) {
300  jack_default_audio_sample_t* inputBuffer[adapter->fCaptureChannels];
301  jack_default_audio_sample_t* outputBuffer[adapter->fPlaybackChannels];
302 
303  for (int i = 0; i < adapter->fCaptureChannels; i++) {
304  inputBuffer[i] = (jack_default_audio_sample_t*)adapter->fInputData->mBuffers[i].mData;
305  }
306  for (int i = 0; i < adapter->fPlaybackChannels; i++) {
307  outputBuffer[i] = (jack_default_audio_sample_t*)ioData->mBuffers[i].mData;
308  }
309 
310  adapter->PushAndPull((jack_default_audio_sample_t**)inputBuffer, (jack_default_audio_sample_t**)outputBuffer, inNumberFrames);
311  return noErr;
312  } else {
313  return err;
314  }
315 }
316 
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)
319 {
320  const JSList* node;
321  const jack_driver_param_t* param;
322  int in_nChannels = 0;
323  int out_nChannels = 0;
324  char captureName[256];
325  char playbackName[256];
326  fCaptureUID[0] = 0;
327  fPlaybackUID[0] = 0;
328  fClockDriftCompensate = false;
329 
330  // Default values
331  fCaptureChannels = -1;
332  fPlaybackChannels = -1;
333 
334  SInt32 major;
335  SInt32 minor;
336  Gestalt(gestaltSystemVersionMajor, &major);
337  Gestalt(gestaltSystemVersionMinor, &minor);
338 
339  // Starting with 10.6 systems, the HAL notification thread is created internally
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");
346  }
347  }
348 
349  for (node = params; node; node = jack_slist_next(node)) {
350  param = (const jack_driver_param_t*) node->data;
351 
352  switch (param->character) {
353 
354  case 'c' :
355  fCaptureChannels = fPlaybackChannels = param->value.ui;
356  break;
357 
358  case 'i':
359  fCaptureChannels = param->value.ui;
360  break;
361 
362  case 'o':
363  fPlaybackChannels = param->value.ui;
364  break;
365 
366  case 'C':
367  fCapturing = true;
368  strncpy(fCaptureUID, param->value.str, 256);
369  break;
370 
371  case 'P':
372  fPlaying = true;
373  strncpy(fPlaybackUID, param->value.str, 256);
374  break;
375 
376  case 'd':
377  strncpy(fCaptureUID, param->value.str, 256);
378  strncpy(fPlaybackUID, param->value.str, 256);
379  break;
380 
381  case 'D':
382  fCapturing = fPlaying = true;
383  break;
384 
385  case 'r':
386  SetAdaptedSampleRate(param->value.ui);
387  break;
388 
389  case 'p':
390  SetAdaptedBufferSize(param->value.ui);
391  break;
392 
393  case 'l':
394  DisplayDeviceNames();
395  break;
396 
397  case 'q':
398  fQuality = param->value.ui;
399  break;
400 
401  case 'g':
402  fRingbufferCurSize = param->value.ui;
403  fAdaptative = false;
404  break;
405 
406  case 's':
407  fClockDriftCompensate = true;
408  break;
409  }
410  }
411 
412  /* duplex is the default */
413  if (!fCapturing && !fPlaying) {
414  fCapturing = true;
415  fPlaying = true;
416  }
417 
418  if (SetupDevices(fCaptureUID, fPlaybackUID, captureName, playbackName, fAdaptedSampleRate) < 0) {
419  throw std::bad_alloc();
420  }
421 
422  if (SetupChannels(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, true) < 0) {
423  throw std::bad_alloc();
424  }
425 
426  if (SetupBufferSize(fAdaptedBufferSize) < 0) {
427  throw std::bad_alloc();
428  }
429 
430  if (SetupSampleRate(fAdaptedSampleRate) < 0) {
431  throw std::bad_alloc();
432  }
433 
434  if (OpenAUHAL(fCapturing, fPlaying, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, fAdaptedBufferSize, fAdaptedSampleRate) < 0) {
435  throw std::bad_alloc();
436  }
437 
438  if (fCapturing && fCaptureChannels > 0) {
439  if (SetupBuffers(fCaptureChannels) < 0) {
440  throw std::bad_alloc();
441  }
442  }
443 
444  if (AddListeners() < 0) {
445  throw std::bad_alloc();
446  }
447 
448  GetStreamLatencies(fDeviceID, true, fInputLatencies);
449  GetStreamLatencies(fDeviceID, false, fOutputLatencies);
450 }
451 
452 OSStatus JackCoreAudioAdapter::GetDefaultDevice(AudioDeviceID* id)
453 {
454  OSStatus res;
455  UInt32 theSize = sizeof(UInt32);
456  AudioDeviceID inDefault;
457  AudioDeviceID outDefault;
458 
459  if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
460  return res;
461  }
462 
463  if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
464  return res;
465  }
466 
467  jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
468 
469  // Get the device only if default input and output are the same
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;
476  } else {
477  *id = inDefault;
478  return noErr;
479  }
480 }
481 
482 OSStatus JackCoreAudioAdapter::GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput)
483 {
484  OSStatus err = noErr;
485  UInt32 outSize;
486  Boolean outWritable;
487 
488  channelCount = 0;
489  err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
490  if (err == noErr) {
491  AudioBufferList bufferList[outSize];
492  err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
493  if (err == noErr) {
494  for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++) {
495  channelCount += bufferList->mBuffers[i].mNumberChannels;
496  }
497  }
498  }
499 
500  return err;
501 }
502 
503 OSStatus JackCoreAudioAdapter::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
504 {
505  UInt32 size = sizeof(AudioValueTranslation);
506  CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
507  AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
508 
509  if (inIUD == NULL) {
510  return kAudioHardwareUnspecifiedError;
511  } else {
512  OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
513  CFRelease(inIUD);
514  jack_log("GetDeviceIDFromUID %s %ld", UID, *id);
515  return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
516  }
517 }
518 
519 OSStatus JackCoreAudioAdapter::GetDefaultInputDevice(AudioDeviceID* id)
520 {
521  OSStatus res;
522  UInt32 theSize = sizeof(UInt32);
523  AudioDeviceID inDefault;
524 
525  if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr) {
526  return res;
527  }
528 
529  if (inDefault == 0) {
530  jack_error("Error: default input device is 0, please select a correct one !!");
531  return -1;
532  }
533  jack_log("GetDefaultInputDevice: input = %ld ", inDefault);
534  *id = inDefault;
535  return noErr;
536 }
537 
538 OSStatus JackCoreAudioAdapter::GetDefaultOutputDevice(AudioDeviceID* id)
539 {
540  OSStatus res;
541  UInt32 theSize = sizeof(UInt32);
542  AudioDeviceID outDefault;
543 
544  if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr) {
545  return res;
546  }
547 
548  if (outDefault == 0) {
549  jack_error("Error: default output device is 0, please select a correct one !!");
550  return -1;
551  }
552  jack_log("GetDefaultOutputDevice: output = %ld", outDefault);
553  *id = outDefault;
554  return noErr;
555 }
556 
557 OSStatus JackCoreAudioAdapter::GetDeviceNameFromID(AudioDeviceID id, char* name)
558 {
559  UInt32 size = 256;
560  return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
561 }
562 
563 AudioDeviceID JackCoreAudioAdapter::GetDeviceIDFromName(const char* name)
564 {
565  UInt32 size;
566  Boolean isWritable;
567  int i, deviceNum;
568 
569  OSStatus err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
570  if (err != noErr) {
571  return -1;
572  }
573 
574  deviceNum = size / sizeof(AudioDeviceID);
575  AudioDeviceID devices[deviceNum];
576 
577  err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
578  if (err != noErr) {
579  return err;
580  }
581 
582  for (i = 0; i < deviceNum; i++) {
583  char device_name[256];
584  size = 256;
585  err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
586  if (err != noErr) {
587  return -1;
588  } else if (strcmp(device_name, name) == 0) {
589  return devices[i];
590  }
591  }
592 
593  return -1;
594 }
595 
596 // Setup
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)
602 {
603  capture_driver_name[0] = 0;
604  playback_driver_name[0] = 0;
605 
606  // Duplex
607  if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
608  jack_log("JackCoreAudioDriver::Open duplex");
609 
610  // Same device for capture and playback...
611  if (strcmp(capture_driver_uid, playback_driver_uid) == 0) {
612 
613  if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
614  jack_log("Will take default in/out");
615  if (GetDefaultDevice(&fDeviceID) != noErr) {
616  jack_error("Cannot open default device");
617  return -1;
618  }
619  }
620  if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
621  jack_error("Cannot get device name from device ID");
622  return -1;
623  }
624 
625  } else {
626 
627  // Creates aggregate device
628  AudioDeviceID captureID, playbackID;
629 
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");
634  return -1;
635  }
636  }
637 
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");
642  return -1;
643  }
644  }
645 
646  if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
647  return -1;
648  }
649  }
650 
651  // Capture only
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");
657  return -1;
658  }
659  }
660  if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
661  jack_error("Cannot get device name from device ID");
662  return -1;
663  }
664 
665  // Playback only
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");
671  return -1;
672  }
673  }
674  if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
675  jack_error("Cannot get device name from device ID");
676  return -1;
677  }
678 
679  // Use default driver in duplex mode
680  } else {
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");
684 
685  // Creates aggregate device
686  AudioDeviceID captureID = -1, playbackID = -1;
687 
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");
692  goto built_in;
693  }
694  }
695 
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");
700  goto built_in;
701  }
702  }
703 
704  if (captureID > 0 && playbackID > 0) {
705  if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
706  goto built_in;
707  }
708  } else {
709  jack_error("Cannot use default input/output");
710  goto built_in;
711  }
712  }
713  }
714 
715  return 0;
716 
717 built_in:
718 
719  // Aggregate built-in input and output
720  AudioDeviceID captureID = GetDeviceIDFromName("Built-in Input");
721  AudioDeviceID playbackID = GetDeviceIDFromName("Built-in Output");
722 
723  if (captureID > 0 && playbackID > 0) {
724  if (CreateAggregateDevice(captureID, playbackID, samplerate, &fDeviceID) != noErr) {
725  return -1;
726  }
727  } else {
728  jack_error("Cannot aggregate built-in input and output");
729  return -1;
730  }
731 
732  return 0;
733 }
734 
735 int JackCoreAudioAdapter::SetupChannels(bool capturing,
736  bool playing,
737  int& inchannels,
738  int& outchannels,
739  int& in_nChannels,
740  int& out_nChannels,
741  bool strict)
742 {
743  OSStatus err = noErr;
744 
745  if (capturing) {
746  err = GetTotalChannels(fDeviceID, in_nChannels, true);
747  if (err != noErr) {
748  jack_error("Cannot get input channel number");
749  printError(err);
750  return -1;
751  } else {
752  jack_log("Max input channels : %d", in_nChannels);
753  }
754  }
755 
756  if (playing) {
757  err = GetTotalChannels(fDeviceID, out_nChannels, false);
758  if (err != noErr) {
759  jack_error("Cannot get output channel number");
760  printError(err);
761  return -1;
762  } else {
763  jack_log("Max output channels : %d", out_nChannels);
764  }
765  }
766 
767  if (inchannels > in_nChannels) {
768  jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
769  if (strict) {
770  return -1;
771  }
772  }
773 
774  if (outchannels > out_nChannels) {
775  jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
776  if (strict) {
777  return -1;
778  }
779  }
780 
781  if (inchannels == -1) {
782  jack_log("Setup max in channels = %ld", in_nChannels);
783  inchannels = in_nChannels;
784  }
785 
786  if (outchannels == -1) {
787  jack_log("Setup max out channels = %ld", out_nChannels);
788  outchannels = out_nChannels;
789  }
790 
791  return 0;
792 }
793 
794 int JackCoreAudioAdapter::SetupBufferSize(jack_nframes_t buffer_size)
795 {
796  // Setting buffer size
797  UInt32 outSize = sizeof(UInt32);
798  OSStatus err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
799  if (err != noErr) {
800  jack_error("Cannot set buffer size %ld", buffer_size);
801  printError(err);
802  return -1;
803  }
804 
805  return 0;
806 }
807 
808 int JackCoreAudioAdapter::SetupSampleRate(jack_nframes_t samplerate)
809 {
810  return SetupSampleRateAux(fDeviceID, samplerate);
811 }
812 
813 int JackCoreAudioAdapter::SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate)
814 {
815  OSStatus err = noErr;
816  UInt32 outSize;
817  Float64 sampleRate;
818 
819  // Get sample rate
820  outSize = sizeof(Float64);
821  err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
822  if (err != noErr) {
823  jack_error("Cannot get current sample rate");
824  printError(err);
825  return -1;
826  } else {
827  jack_log("Current sample rate = %f", sampleRate);
828  }
829 
830  // If needed, set new sample rate
831  if (samplerate != (jack_nframes_t)sampleRate) {
832  sampleRate = (Float64)samplerate;
833 
834  // To get SR change notification
835  err = AudioDeviceAddPropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
836  if (err != noErr) {
837  jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
838  printError(err);
839  return -1;
840  }
841  err = AudioDeviceSetProperty(inDevice, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
842  if (err != noErr) {
843  jack_error("Cannot set sample rate = %ld", samplerate);
844  printError(err);
845  return -1;
846  }
847 
848  // Waiting for SR change notification
849  int count = 0;
850  while (!fState && count++ < WAIT_COUNTER) {
851  usleep(100000);
852  jack_log("Wait count = %d", count);
853  }
854 
855  // Remove SR change notification
856  AudioDeviceRemovePropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
857  }
858 
859  return 0;
860 }
861 
862 int JackCoreAudioAdapter::SetupBuffers(int inchannels)
863 {
864  jack_log("JackCoreAudioAdapter::SetupBuffers: input = %ld", inchannels);
865 
866  // Prepare buffers
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));
873  }
874  return 0;
875 }
876 
877 void JackCoreAudioAdapter::DisposeBuffers()
878 {
879  if (fInputData) {
880  for (int i = 0; i < fCaptureChannels; i++) {
881  free(fInputData->mBuffers[i].mData);
882  }
883  free(fInputData);
884  fInputData = 0;
885  }
886 }
887 
888 int JackCoreAudioAdapter::OpenAUHAL(bool capturing,
889  bool playing,
890  int inchannels,
891  int outchannels,
892  int in_nChannels,
893  int out_nChannels,
894  jack_nframes_t buffer_size,
895  jack_nframes_t samplerate)
896 {
897  ComponentResult err1;
898  UInt32 enableIO;
899  AudioStreamBasicDescription srcFormat, dstFormat;
900  AudioDeviceID currAudioDeviceID;
901  UInt32 size;
902 
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);
904 
905  if (inchannels == 0 && outchannels == 0) {
906  jack_error("No input and output channels...");
907  return -1;
908  }
909 
910  // AUHAL
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);
915  if (err1 != noErr) {
916  jack_error("Error calling OpenAComponent");
917  printError(err1);
918  goto error;
919  }
920 #else
921  AudioComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
922  AudioComponent HALOutput = AudioComponentFindNext(NULL, &cd);
923  err1 = AudioComponentInstanceNew(HALOutput, &fAUHAL);
924  if (err1 != noErr) {
925  jack_error("Error calling AudioComponentInstanceNew");
926  printError(err1);
927  goto error;
928  }
929 #endif
930 
931  err1 = AudioUnitInitialize(fAUHAL);
932  if (err1 != noErr) {
933  jack_error("Cannot initialize AUHAL unit");
934  printError(err1);
935  goto error;
936  }
937 
938  // Start I/O
939  if (capturing && inchannels > 0) {
940  enableIO = 1;
941  jack_log("Setup AUHAL input on");
942  } else {
943  enableIO = 0;
944  jack_log("Setup AUHAL input off");
945  }
946 
947  err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
948  if (err1 != noErr) {
949  jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
950  printError(err1);
951  goto error;
952  }
953 
954  if (playing && outchannels > 0) {
955  enableIO = 1;
956  jack_log("Setup AUHAL output on");
957  } else {
958  enableIO = 0;
959  jack_log("Setup AUHAL output off");
960  }
961 
962  err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
963  if (err1 != noErr) {
964  jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
965  printError(err1);
966  goto error;
967  }
968 
969  size = sizeof(AudioDeviceID);
970  err1 = AudioUnitGetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &currAudioDeviceID, &size);
971  if (err1 != noErr) {
972  jack_error("Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
973  printError(err1);
974  goto error;
975  } else {
976  jack_log("AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID);
977  }
978 
979  // Setup up choosen device, in both input and output cases
980  err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
981  if (err1 != noErr) {
982  jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
983  printError(err1);
984  goto error;
985  }
986 
987  // Set buffer size
988  if (capturing && inchannels > 0) {
989  err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&buffer_size, sizeof(UInt32));
990  if (err1 != noErr) {
991  jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
992  printError(err1);
993  goto error;
994  }
995  }
996 
997  if (playing && outchannels > 0) {
998  err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&buffer_size, sizeof(UInt32));
999  if (err1 != noErr) {
1000  jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
1001  printError(err1);
1002  goto error;
1003  }
1004  }
1005 
1006  // Setup channel map
1007  if (capturing && inchannels > 0 && inchannels <= in_nChannels) {
1008  SInt32 chanArr[in_nChannels];
1009  for (int i = 0; i < in_nChannels; i++) {
1010  chanArr[i] = -1;
1011  }
1012  for (int i = 0; i < inchannels; i++) {
1013  chanArr[i] = i;
1014  }
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");
1018  printError(err1);
1019  goto error;
1020  }
1021  }
1022 
1023  if (playing && outchannels > 0 && outchannels <= out_nChannels) {
1024  SInt32 chanArr[out_nChannels];
1025  for (int i = 0; i < out_nChannels; i++) {
1026  chanArr[i] = -1;
1027  }
1028  for (int i = 0; i < outchannels; i++) {
1029  chanArr[i] = i;
1030  }
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");
1034  printError(err1);
1035  goto error;
1036  }
1037  }
1038 
1039  // Setup stream converters
1040  if (capturing && inchannels > 0) {
1041 
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");
1046  printError(err1);
1047  goto error;
1048  }
1049  PrintStreamDesc(&srcFormat);
1050 
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);
1061 
1062  err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, sizeof(AudioStreamBasicDescription));
1063 
1064  if (err1 != noErr) {
1065  jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
1066  printError(err1);
1067  goto error;
1068  }
1069  }
1070 
1071  if (playing && outchannels > 0) {
1072 
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");
1077  printError(err1);
1078  goto error;
1079  }
1080  PrintStreamDesc(&dstFormat);
1081 
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);
1092 
1093  err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, sizeof(AudioStreamBasicDescription));
1094 
1095  if (err1 != noErr) {
1096  jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
1097  printError(err1);
1098  goto error;
1099  }
1100  }
1101 
1102  // Setup callbacks
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");
1110  printError(err1);
1111  goto error;
1112  }
1113  } else {
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");
1120  printError(err1);
1121  goto error;
1122  }
1123  }
1124 
1125  return 0;
1126 
1127 error:
1128  CloseAUHAL();
1129  return -1;
1130 }
1131 
1132 OSStatus JackCoreAudioAdapter::DestroyAggregateDevice()
1133 {
1134  OSStatus osErr = noErr;
1135  AudioObjectPropertyAddress pluginAOPA;
1136  pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice;
1137  pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
1138  pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
1139  UInt32 outDataSize;
1140 
1141  osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
1142  if (osErr != noErr) {
1143  jack_error("JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
1144  printError(osErr);
1145  return osErr;
1146  }
1147 
1148  osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID);
1149  if (osErr != noErr) {
1150  jack_error("JackCoreAudioAdapter::DestroyAggregateDevice : AudioObjectGetPropertyData error");
1151  printError(osErr);
1152  return osErr;
1153  }
1154 
1155  return noErr;
1156 }
1157 
1158 static CFStringRef GetDeviceName(AudioDeviceID id)
1159 {
1160  UInt32 size = sizeof(CFStringRef);
1161  CFStringRef UIname;
1162  OSStatus err = AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
1163  return (err == noErr) ? UIname : NULL;
1164 }
1165 
1166 OSStatus JackCoreAudioAdapter::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
1167 {
1168  OSStatus err = noErr;
1169  AudioObjectID sub_device[32];
1170  UInt32 outSize = sizeof(sub_device);
1171 
1172  err = AudioDeviceGetProperty(captureDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
1173  vector<AudioDeviceID> captureDeviceIDArray;
1174 
1175  if (err != noErr) {
1176  jack_log("Input device does not have subdevices");
1177  captureDeviceIDArray.push_back(captureDeviceID);
1178  } else {
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]);
1183  }
1184  }
1185 
1186  err = AudioDeviceGetProperty(playbackDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
1187  vector<AudioDeviceID> playbackDeviceIDArray;
1188 
1189  if (err != noErr) {
1190  jack_log("Output device does not have subdevices");
1191  playbackDeviceIDArray.push_back(playbackDeviceID);
1192  } else {
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]);
1197  }
1198  }
1199 
1200  return CreateAggregateDeviceAux(captureDeviceIDArray, playbackDeviceIDArray, samplerate, outAggregateDevice);
1201 }
1202 
1203 OSStatus JackCoreAudioAdapter::CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice)
1204 {
1205  OSStatus osErr = noErr;
1206  UInt32 outSize;
1207  Boolean outWritable;
1208 
1209  // Prepare sub-devices for clock drift compensation
1210  // Workaround for bug in the HAL : until 10.6.2
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;
1217 
1218  //---------------------------------------------------------------------------
1219  // Setup SR of both devices otherwise creating AD may fail...
1220  //---------------------------------------------------------------------------
1221  UInt32 keptclockdomain = 0;
1222  UInt32 clockdomain = 0;
1223  outSize = sizeof(UInt32);
1224  bool need_clock_drift_compensation = false;
1225 
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");
1229  } else {
1230  // Check clock domain
1231  osErr = AudioDeviceGetProperty(captureDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
1232  if (osErr != 0) {
1233  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
1234  printError(osErr);
1235  } else {
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;
1241  }
1242  }
1243  }
1244  }
1245 
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");
1249  } else {
1250  // Check clock domain
1251  osErr = AudioDeviceGetProperty(playbackDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain);
1252  if (osErr != 0) {
1253  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : kAudioDevicePropertyClockDomain error");
1254  printError(osErr);
1255  } else {
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;
1261  }
1262  }
1263  }
1264  }
1265 
1266  // If no valid clock domain was found, then assume we have to compensate...
1267  if (keptclockdomain == 0) {
1268  need_clock_drift_compensation = true;
1269  }
1270 
1271  //---------------------------------------------------------------------------
1272  // Start to create a new aggregate by getting the base audio hardware plugin
1273  //---------------------------------------------------------------------------
1274 
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);
1279  }
1280 
1281  for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
1282  GetDeviceNameFromID(playbackDeviceID[i], device_name);
1283  jack_info("Separated output = '%s' ", device_name);
1284  }
1285 
1286  osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable);
1287  if (osErr != noErr) {
1288  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioHardwareGetPropertyInfo kAudioHardwarePropertyPlugInForBundleID error");
1289  printError(osErr);
1290  return osErr;
1291  }
1292 
1293  AudioValueTranslation pluginAVT;
1294 
1295  CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
1296 
1297  pluginAVT.mInputData = &inBundleRef;
1298  pluginAVT.mInputDataSize = sizeof(inBundleRef);
1299  pluginAVT.mOutputData = &fPluginID;
1300  pluginAVT.mOutputDataSize = sizeof(fPluginID);
1301 
1302  osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT);
1303  if (osErr != noErr) {
1304  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioHardwareGetProperty kAudioHardwarePropertyPlugInForBundleID error");
1305  printError(osErr);
1306  return osErr;
1307  }
1308 
1309  //-------------------------------------------------
1310  // Create a CFDictionary for our aggregate device
1311  //-------------------------------------------------
1312 
1313  CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
1314 
1315  CFStringRef AggregateDeviceNameRef = CFSTR("JackDuplex");
1316  CFStringRef AggregateDeviceUIDRef = CFSTR("com.grame.JackDuplex");
1317 
1318  // add the name of the device to the dictionary
1319  CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
1320 
1321  // add our choice of UID for the aggregate device to the dictionary
1322  CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
1323 
1324  // add a "private aggregate key" to the dictionary
1325  int value = 1;
1326  CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
1327 
1328  SInt32 system;
1329  Gestalt(gestaltSystemVersion, &system);
1330 
1331  jack_log("JackCoreAudioAdapter::CreateAggregateDevice : system version = %x limit = %x", system, 0x00001054);
1332 
1333  // Starting with 10.5.4 systems, the AD can be internal... (better)
1334  if (system < 0x00001054) {
1335  jack_log("JackCoreAudioAdapter::CreateAggregateDevice : public aggregate device....");
1336  } else {
1337  jack_log("JackCoreAudioAdapter::CreateAggregateDevice : private aggregate device....");
1338  CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
1339  }
1340 
1341  // Prepare sub-devices for clock drift compensation
1342  CFMutableArrayRef subDevicesArrayClock = NULL;
1343 
1344  /*
1345  if (fClockDriftCompensate) {
1346  if (need_clock_drift_compensation) {
1347  jack_info("Clock drift compensation activated...");
1348  subDevicesArrayClock = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
1349 
1350  for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
1351  CFStringRef UID = GetDeviceName(captureDeviceID[i]);
1352  if (UID) {
1353  CFMutableDictionaryRef subdeviceAggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
1354  CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceUIDKey), UID);
1355  CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceDriftCompensationKey), AggregateDeviceNumberRef);
1356  //CFRelease(UID);
1357  CFArrayAppendValue(subDevicesArrayClock, subdeviceAggDeviceDict);
1358  }
1359  }
1360 
1361  for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
1362  CFStringRef UID = GetDeviceName(playbackDeviceID[i]);
1363  if (UID) {
1364  CFMutableDictionaryRef subdeviceAggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
1365  CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceUIDKey), UID);
1366  CFDictionaryAddValue(subdeviceAggDeviceDict, CFSTR(kAudioSubDeviceDriftCompensationKey), AggregateDeviceNumberRef);
1367  //CFRelease(UID);
1368  CFArrayAppendValue(subDevicesArrayClock, subdeviceAggDeviceDict);
1369  }
1370  }
1371 
1372  // add sub-device clock array for the aggregate device to the dictionary
1373  CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceSubDeviceListKey), subDevicesArrayClock);
1374  } else {
1375  jack_info("Clock drift compensation was asked but is not needed (devices use the same clock domain)");
1376  }
1377  }
1378  */
1379 
1380  //-------------------------------------------------
1381  // Create a CFMutableArray for our sub-device list
1382  //-------------------------------------------------
1383 
1384  // we need to append the UID for each device to a CFMutableArray, so create one here
1385  CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
1386 
1387  vector<CFStringRef> captureDeviceUID;
1388  for (UInt32 i = 0; i < captureDeviceID.size(); i++) {
1389  CFStringRef ref = GetDeviceName(captureDeviceID[i]);
1390  if (ref == NULL) {
1391  return -1;
1392  }
1393  captureDeviceUID.push_back(ref);
1394  // input sub-devices in this example, so append the sub-device's UID to the CFArray
1395  CFArrayAppendValue(subDevicesArray, ref);
1396  }
1397 
1398  vector<CFStringRef> playbackDeviceUID;
1399  for (UInt32 i = 0; i < playbackDeviceID.size(); i++) {
1400  CFStringRef ref = GetDeviceName(playbackDeviceID[i]);
1401  if (ref == NULL) {
1402  return -1;
1403  }
1404  playbackDeviceUID.push_back(ref);
1405  // output sub-devices in this example, so append the sub-device's UID to the CFArray
1406  CFArrayAppendValue(subDevicesArray, ref);
1407  }
1408 
1409  //-----------------------------------------------------------------------
1410  // Feed the dictionary to the plugin, to create a blank aggregate device
1411  //-----------------------------------------------------------------------
1412 
1413  AudioObjectPropertyAddress pluginAOPA;
1414  pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
1415  pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
1416  pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
1417  UInt32 outDataSize;
1418 
1419  osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
1420  if (osErr != noErr) {
1421  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectGetPropertyDataSize error");
1422  printError(osErr);
1423  goto error;
1424  }
1425 
1426  osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, outAggregateDevice);
1427  if (osErr != noErr) {
1428  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectGetPropertyData error");
1429  printError(osErr);
1430  goto error;
1431  }
1432 
1433  // pause for a bit to make sure that everything completed correctly
1434  // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
1435  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
1436 
1437  //-------------------------
1438  // Set the sub-device list
1439  //-------------------------
1440 
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");
1448  printError(osErr);
1449  goto error;
1450  }
1451 
1452  // pause again to give the changes time to take effect
1453  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
1454 
1455  //-----------------------
1456  // Set the master device
1457  //-----------------------
1458 
1459  // set the master device manually (this is the device which will act as the master clock for the aggregate device)
1460  // pass in the UID of the device you want to use
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]); // First apture is master...
1466  if (osErr != noErr) {
1467  jack_error("JackCoreAudioAdapter::CreateAggregateDevice : AudioObjectSetPropertyData for master device error");
1468  printError(osErr);
1469  goto error;
1470  }
1471 
1472  // pause again to give the changes time to take effect
1473  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
1474 
1475  // Prepare sub-devices for clock drift compensation
1476  // Workaround for bug in the HAL : until 10.6.2
1477 
1478  if (fClockDriftCompensate) {
1479  if (need_clock_drift_compensation) {
1480  jack_info("Clock drift compensation activated...");
1481 
1482  // Get the property data size
1483  osErr = AudioObjectGetPropertyDataSize(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize);
1484  if (osErr != noErr) {
1485  jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
1486  printError(osErr);
1487  }
1488 
1489  // Calculate the number of object IDs
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);
1494 
1495  osErr = AudioObjectGetPropertyData(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize, subDevices);
1496  if (osErr != noErr) {
1497  jack_error("JackCoreAudioAdapter::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error");
1498  printError(osErr);
1499  }
1500 
1501  // Set kAudioSubDevicePropertyDriftCompensation property...
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");
1507  printError(osErr);
1508  }
1509  }
1510  } else {
1511  jack_info("Clock drift compensation was asked but is not needed (devices use the same clock domain)");
1512  }
1513  }
1514 
1515  // pause again to give the changes time to take effect
1516  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
1517 
1518  //----------
1519  // Clean up
1520  //----------
1521 
1522  // release the private AD key
1523  CFRelease(AggregateDeviceNumberRef);
1524 
1525  // release the CF objects we have created - we don't need them any more
1526  CFRelease(aggDeviceDict);
1527  CFRelease(subDevicesArray);
1528 
1529  if (subDevicesArrayClock) {
1530  CFRelease(subDevicesArrayClock);
1531  }
1532 
1533  // release the device UID
1534  for (UInt32 i = 0; i < captureDeviceUID.size(); i++) {
1535  CFRelease(captureDeviceUID[i]);
1536  }
1537 
1538  for (UInt32 i = 0; i < playbackDeviceUID.size(); i++) {
1539  CFRelease(playbackDeviceUID[i]);
1540  }
1541 
1542  jack_log("New aggregate device %ld", *outAggregateDevice);
1543  return noErr;
1544 
1545 error:
1546  DestroyAggregateDevice();
1547  return -1;
1548 }
1549 
1550 
1551 bool JackCoreAudioAdapter::IsAggregateDevice(AudioDeviceID device)
1552 {
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);
1557 
1558  if (err != noErr) {
1559  jack_log("Device does not have subdevices");
1560  return false;
1561  } else {
1562  int num_devices = outSize / sizeof(AudioObjectID);
1563  jack_log("Device does has %d subdevices", num_devices);
1564  return true;
1565  }
1566 }
1567 
1568 void JackCoreAudioAdapter::CloseAUHAL()
1569 {
1570  AudioUnitUninitialize(fAUHAL);
1571  CloseComponent(fAUHAL);
1572 }
1573 
1574 int JackCoreAudioAdapter::Open()
1575 {
1576  return (AudioOutputUnitStart(fAUHAL) != noErr) ? -1 : 0;
1577 }
1578 
1579 int JackCoreAudioAdapter::Close()
1580 {
1581 #ifdef JACK_MONITOR
1582  fTable.Save(fHostBufferSize, fHostSampleRate, fAdaptedSampleRate, fAdaptedBufferSize);
1583 #endif
1584  AudioOutputUnitStop(fAUHAL);
1585  DisposeBuffers();
1586  CloseAUHAL();
1587  RemoveListeners();
1588  if (fPluginID > 0) {
1589  DestroyAggregateDevice();
1590  }
1591  return 0;
1592 }
1593 
1594 int JackCoreAudioAdapter::SetSampleRate(jack_nframes_t sample_rate)
1595 {
1596  JackAudioAdapterInterface::SetHostSampleRate(sample_rate);
1597  Close();
1598  return Open();
1599 }
1600 
1601 int JackCoreAudioAdapter::SetBufferSize(jack_nframes_t buffer_size)
1602 {
1603  JackAudioAdapterInterface::SetHostBufferSize(buffer_size);
1604  Close();
1605  return Open();
1606 }
1607 
1608 OSStatus JackCoreAudioAdapter::GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies)
1609 {
1610  OSStatus err = noErr;
1611  UInt32 outSize1, outSize2, outSize3;
1612  Boolean outWritable;
1613 
1614  err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreams, &outSize1, &outWritable);
1615  if (err == noErr) {
1616  int stream_count = outSize1 / sizeof(UInt32);
1617  AudioStreamID streamIDs[stream_count];
1618  AudioBufferList bufferList[stream_count];
1619  UInt32 streamLatency;
1620  outSize2 = sizeof(UInt32);
1621 
1622  err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreams, &outSize1, streamIDs);
1623  if (err != noErr) {
1624  jack_error("GetStreamLatencies kAudioDevicePropertyStreams err = %d", err);
1625  return err;
1626  }
1627 
1628  err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize3, &outWritable);
1629  if (err != noErr) {
1630  jack_error("GetStreamLatencies kAudioDevicePropertyStreamConfiguration err = %d", err);
1631  return err;
1632  }
1633 
1634  for (int i = 0; i < stream_count; i++) {
1635  err = AudioStreamGetProperty(streamIDs[i], 0, kAudioStreamPropertyLatency, &outSize2, &streamLatency);
1636  if (err != noErr) {
1637  jack_error("GetStreamLatencies kAudioStreamPropertyLatency err = %d", err);
1638  return err;
1639  }
1640  err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize3, bufferList);
1641  if (err != noErr) {
1642  jack_error("GetStreamLatencies kAudioDevicePropertyStreamConfiguration err = %d", err);
1643  return err;
1644  }
1645  // Push 'channel' time the stream latency
1646  for (uint k = 0; k < bufferList->mBuffers[i].mNumberChannels; k++) {
1647  latencies.push_back(streamLatency);
1648  }
1649  }
1650  }
1651  return err;
1652 }
1653 
1654 int JackCoreAudioAdapter::GetLatency(int port_index, bool input)
1655 {
1656  UInt32 size = sizeof(UInt32);
1657  UInt32 value1 = 0;
1658  UInt32 value2 = 0;
1659 
1660  OSStatus err = AudioDeviceGetProperty(fDeviceID, 0, input, kAudioDevicePropertyLatency, &size, &value1);
1661  if (err != noErr) {
1662  jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error");
1663  }
1664  err = AudioDeviceGetProperty(fDeviceID, 0, input, kAudioDevicePropertySafetyOffset, &size, &value2);
1665  if (err != noErr) {
1666  jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error");
1667  }
1668 
1669  // TODO : add stream latency
1670 
1671  return value1 + value2 + fAdaptedBufferSize;
1672 }
1673 
1674 int JackCoreAudioAdapter::GetInputLatency(int port_index)
1675 {
1676  if (port_index < int(fInputLatencies.size())) {
1677  return GetLatency(port_index, true) + fInputLatencies[port_index];
1678  } else {
1679  // No stream latency
1680  return GetLatency(port_index, true);
1681  }
1682 }
1683 
1684 int JackCoreAudioAdapter::GetOutputLatency(int port_index)
1685 {
1686  if (port_index < int(fOutputLatencies.size())) {
1687  return GetLatency(port_index, false) + fOutputLatencies[port_index];
1688  } else {
1689  // No stream latency
1690  return GetLatency(port_index, false);
1691  }
1692 }
1693 
1694 } // namespace
1695 
1696 #ifdef __cplusplus
1697 extern "C"
1698 {
1699 #endif
1700 
1701  SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor()
1702  {
1703  jack_driver_desc_t * desc;
1706 
1707  desc = jack_driver_descriptor_construct("audioadapter", JackDriverNone, "netjack audio <==> net backend adapter", &filler);
1708 
1709  value.i = -1;
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");
1713 
1714  value.str[0] = 0;
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);
1717 
1718  value.ui = 44100U;
1719  jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
1720 
1721  value.ui = 512U;
1722  jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
1723 
1724  value.i = TRUE;
1725  jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
1726 
1727  value.str[0] = 0;
1728  jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "CoreAudio device name", NULL);
1729 
1730  value.i = TRUE;
1731  jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available CoreAudio devices", NULL);
1732 
1733  value.ui = 0;
1734  jack_driver_descriptor_add_parameter(desc, &filler, "quality", 'q', JackDriverParamInt, &value, NULL, "Resample algorithm quality (0 - 4)", NULL);
1735 
1736  value.ui = 32768;
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)");
1738 
1739  value.i = FALSE;
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");
1741 
1742  return desc;
1743  }
1744 
1745 
1746 #ifdef __cplusplus
1747 }
1748 #endif
1749 
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
SERVER_EXPORT void jack_info(const char *fmt,...)
Definition: JackError.cpp:99
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107