2 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2004-2008 Grame
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 CoreAudioPCM::destroy_aggregate_device ()
25 if (_aggregate_plugin_id == 0) {
31 AudioObjectPropertyAddress property_address;
32 property_address.mSelector = kAudioPlugInDestroyAggregateDevice;
33 property_address.mScope = kAudioObjectPropertyScopeGlobal;
34 property_address.mElement = kAudioObjectPropertyElementMaster;
37 err = AudioObjectGetPropertyDataSize(_aggregate_plugin_id, &property_address, 0, NULL, &outDataSize);
39 fprintf(stderr, "DestroyAggregateDevice : AudioObjectGetPropertyDataSize error\n");
43 err = AudioObjectGetPropertyData(_aggregate_plugin_id, &property_address, 0, NULL, &outDataSize, &_aggregate_device_id);
45 fprintf(stderr, "DestroyAggregateDevice : AudioObjectGetPropertyData error\n");
49 printf("DestroyAggregateDevice : OK (plugin: %u device:%u)\n", _aggregate_plugin_id, _aggregate_device_id);
54 CoreAudioPCM::create_aggregate_device (
55 AudioDeviceID input_device_id,
56 AudioDeviceID output_device_id,
58 AudioDeviceID* created_device)
61 AudioObjectID sub_device[32];
62 UInt32 size = sizeof(sub_device);
64 /* look up sub-devices */
65 err = GetPropertyWrapper (input_device_id, 0, 0, kAudioAggregateDevicePropertyActiveSubDeviceList, &size, sub_device);
66 std::vector<AudioDeviceID> input_device_ids;
69 input_device_ids.push_back(input_device_id);
71 uint32_t num_devices = size / sizeof(AudioObjectID);
72 for (uint32_t i = 0; i < num_devices; ++i) {
73 input_device_ids.push_back(sub_device[i]);
77 size = sizeof(sub_device);
78 err = GetPropertyWrapper (output_device_id, 0, 0, kAudioAggregateDevicePropertyActiveSubDeviceList, &size, sub_device);
79 std::vector<AudioDeviceID> output_device_ids;
82 output_device_ids.push_back(output_device_id);
84 uint32_t num_devices = size / sizeof(AudioObjectID);
85 for (uint32_t i = 0; i < num_devices; ++i) {
86 output_device_ids.push_back(sub_device[i]);
90 //---------------------------------------------------------------------------
91 // Setup SR of both devices otherwise creating AD may fail...
92 //---------------------------------------------------------------------------
93 UInt32 keptclockdomain = 0;
94 UInt32 clockdomain = 0;
95 size = sizeof(UInt32);
96 bool need_clock_drift_compensation = false;
98 for (size_t i = 0; i < input_device_ids.size(); ++i) {
99 set_device_sample_rate_id(input_device_ids[i], sample_rate, true);
101 // Check clock domain
102 err = GetPropertyWrapper (input_device_ids[i], 0, 0, kAudioDevicePropertyClockDomain, &size, &clockdomain);
104 keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
105 if (clockdomain != 0 && clockdomain != keptclockdomain) {
107 printf("AggregateDevice: devices do not share the same clock.\n");
109 need_clock_drift_compensation = true;
114 for (UInt32 i = 0; i < output_device_ids.size(); i++) {
115 set_device_sample_rate_id(output_device_ids[i], sample_rate, true);
117 // Check clock domain
118 err = GetPropertyWrapper (output_device_ids[i], 0, 0, kAudioDevicePropertyClockDomain, &size, &clockdomain);
120 keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
121 if (clockdomain != 0 && clockdomain != keptclockdomain) {
123 printf("AggregateDevice: devices do not share the same clock.\n");
125 need_clock_drift_compensation = true;
130 // If no valid clock domain was found, then assume we have to compensate...
131 if (keptclockdomain == 0) {
132 need_clock_drift_compensation = true;
135 //---------------------------------------------------------------------------
136 // Start to create a new aggregate by getting the base audio hardware plugin
137 //---------------------------------------------------------------------------
140 char device_name[256];
141 for (size_t i = 0; i < input_device_ids.size(); ++i) {
142 GetDeviceNameFromID(input_device_ids[i], device_name);
143 printf("Separated input = '%s'\n", device_name);
146 for (size_t i = 0; i < output_device_ids.size(); ++i) {
147 GetDeviceNameFromID(output_device_ids[i], device_name);
148 printf("Separated output = '%s'\n", device_name);
152 err = GetHardwarePropertyInfoWrapper (kAudioHardwarePropertyPlugInForBundleID, &size);
154 fprintf(stderr, "AggregateDevice: AudioHardwareGetPropertyInfo kAudioHardwarePropertyPlugInForBundleID error\n");
158 AudioValueTranslation pluginAVT;
160 CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
162 pluginAVT.mInputData = &inBundleRef;
163 pluginAVT.mInputDataSize = sizeof(inBundleRef);
164 pluginAVT.mOutputData = &_aggregate_plugin_id;
165 pluginAVT.mOutputDataSize = sizeof(AudioDeviceID);
167 err = GetHardwarePropertyWrapper (kAudioHardwarePropertyPlugInForBundleID, &size, &pluginAVT);
169 fprintf(stderr, "AggregateDevice: AudioHardwareGetProperty kAudioHardwarePropertyPlugInForBundleID error\n");
173 //-------------------------------------------------
174 // Create a CFDictionary for our aggregate device
175 //-------------------------------------------------
177 CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
179 CFStringRef AggregateDeviceNameRef = CFSTR("ArdourDuplex");
180 CFStringRef AggregateDeviceUIDRef = CFSTR("com.ardour.CoreAudio");
181 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
182 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
186 CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
187 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
189 //-------------------------------------------------
190 // Create a CFMutableArray for our sub-device list
191 //-------------------------------------------------
193 // we need to append the UID for each device to a CFMutableArray, so create one here
194 CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
196 std::vector<CFStringRef> captureDeviceUID;
197 for (UInt32 i = 0; i < input_device_ids.size(); i++) {
198 CFStringRef ref = GetDeviceName(input_device_ids[i]);
202 captureDeviceUID.push_back(ref);
203 CFArrayAppendValue(subDevicesArray, ref);
206 std::vector<CFStringRef> playbackDeviceUID;
207 for (UInt32 i = 0; i < output_device_ids.size(); i++) {
208 CFStringRef ref = GetDeviceName(output_device_ids[i]);
212 playbackDeviceUID.push_back(ref);
213 CFArrayAppendValue(subDevicesArray, ref);
216 //-----------------------------------------------------------------------
217 // Feed the dictionary to the plugin, to create a blank aggregate device
218 //-----------------------------------------------------------------------
220 AudioObjectPropertyAddress pluginAOPA;
221 pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
222 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
223 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
226 err = AudioObjectGetPropertyDataSize(_aggregate_plugin_id, &pluginAOPA, 0, NULL, &outDataSize);
228 fprintf(stderr, "AggregateDevice: AudioObjectGetPropertyDataSize error\n");
232 err = AudioObjectGetPropertyData(_aggregate_plugin_id, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, created_device);
234 fprintf(stderr, "AggregateDevice: AudioObjectGetPropertyData error\n");
238 // pause for a bit to make sure that everything completed correctly
239 // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
240 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
242 //-------------------------
243 // Set the sub-device list
244 //-------------------------
246 pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
247 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
248 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
249 outDataSize = sizeof(CFMutableArrayRef);
250 err = AudioObjectSetPropertyData(*created_device, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
252 fprintf(stderr, "AggregateDevice: AudioObjectSetPropertyData for sub-device list error\n");
256 // pause again to give the changes time to take effect
257 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
259 //-----------------------
260 // Set the master device
261 //-----------------------
263 // set the master device manually (this is the device which will act as the master clock for the aggregate device)
264 // pass in the UID of the device you want to use
265 pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
266 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
267 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
268 outDataSize = sizeof(CFStringRef);
269 err = AudioObjectSetPropertyData(*created_device, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID[0]);
271 fprintf(stderr, "AggregateDevice: AudioObjectSetPropertyData for playback-master device error\n");
273 err = AudioObjectSetPropertyData(*created_device, &pluginAOPA, 0, NULL, outDataSize, &playbackDeviceUID[0]);
276 fprintf(stderr, "AggregateDevice: AudioObjectSetPropertyData for capture-master device error\n");
280 // pause again to give the changes time to take effect
281 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
283 // Prepare sub-devices for clock drift compensation
284 // Workaround for bug in the HAL : until 10.6.2
285 if (need_clock_drift_compensation) {
287 AudioObjectPropertyAddress theAddressOwned = { kAudioObjectPropertyOwnedObjects, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
288 AudioObjectPropertyAddress theAddressDrift = { kAudioSubDevicePropertyDriftCompensation, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
289 UInt32 theQualifierDataSize = sizeof(AudioObjectID);
290 AudioClassID inClass = kAudioSubDeviceClassID;
291 void* theQualifierData = &inClass;
292 UInt32 subDevicesNum = 0;
295 printf("Clock drift compensation activated...\n");
298 // Get the property data size
299 err = AudioObjectGetPropertyDataSize(*created_device, &theAddressOwned, theQualifierDataSize, theQualifierData, &size);
301 fprintf(stderr, "AggregateDevice: kAudioObjectPropertyOwnedObjects error\n");
304 // Calculate the number of object IDs
305 subDevicesNum = size / sizeof(AudioObjectID);
307 printf("AggregateDevice: clock drift compensation, number of sub-devices = %d\n", subDevicesNum);
309 AudioObjectID subDevices[subDevicesNum];
310 size = sizeof(subDevices);
312 err = AudioObjectGetPropertyData(*created_device, &theAddressOwned, theQualifierDataSize, theQualifierData, &size, subDevices);
314 fprintf(stderr, "AggregateDevice: kAudioObjectPropertyOwnedObjects error\n");
317 // Set kAudioSubDevicePropertyDriftCompensation property...
318 for (UInt32 index = 0; index < subDevicesNum; ++index) {
319 UInt32 theDriftCompensationValue = 1;
320 err = AudioObjectSetPropertyData(subDevices[index], &theAddressDrift, 0, NULL, sizeof(UInt32), &theDriftCompensationValue);
322 fprintf(stderr, "AggregateDevice: kAudioSubDevicePropertyDriftCompensation error\n");
327 // pause again to give the changes time to take effect
328 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
334 // release the private AD key
335 CFRelease(AggregateDeviceNumberRef);
337 // release the CF objects we have created - we don't need them any more
338 CFRelease(aggDeviceDict);
339 CFRelease(subDevicesArray);
341 // release the device UID
342 for (size_t i = 0; i < captureDeviceUID.size(); ++i) {
343 CFRelease(captureDeviceUID[i]);
346 for (size_t i = 0; i < playbackDeviceUID.size(); ++i) {
347 CFRelease(playbackDeviceUID[i]);
351 printf("AggregateDevice: new aggregate device %u\n", *created_device);
356 destroy_aggregate_device();