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",
50 (unsigned int)_aggregate_plugin_id,
51 (unsigned int)_aggregate_device_id);
56 CoreAudioPCM::create_aggregate_device (
57 AudioDeviceID input_device_id,
58 AudioDeviceID output_device_id,
60 AudioDeviceID* created_device)
63 AudioObjectID sub_device[32];
64 UInt32 size = sizeof(sub_device);
66 /* look up sub-devices */
67 err = GetPropertyWrapper (input_device_id, 0, 0, kAudioAggregateDevicePropertyActiveSubDeviceList, &size, sub_device);
68 std::vector<AudioDeviceID> input_device_ids;
71 input_device_ids.push_back(input_device_id);
73 uint32_t num_devices = size / sizeof(AudioObjectID);
74 for (uint32_t i = 0; i < num_devices; ++i) {
75 input_device_ids.push_back(sub_device[i]);
79 size = sizeof(sub_device);
80 err = GetPropertyWrapper (output_device_id, 0, 0, kAudioAggregateDevicePropertyActiveSubDeviceList, &size, sub_device);
81 std::vector<AudioDeviceID> output_device_ids;
84 output_device_ids.push_back(output_device_id);
86 uint32_t num_devices = size / sizeof(AudioObjectID);
87 for (uint32_t i = 0; i < num_devices; ++i) {
88 output_device_ids.push_back(sub_device[i]);
92 //---------------------------------------------------------------------------
93 // Setup SR of both devices otherwise creating AD may fail...
94 //---------------------------------------------------------------------------
95 UInt32 keptclockdomain = 0;
96 UInt32 clockdomain = 0;
97 size = sizeof(UInt32);
98 bool need_clock_drift_compensation = false;
100 for (size_t i = 0; i < input_device_ids.size(); ++i) {
101 set_device_sample_rate_id(input_device_ids[i], sample_rate, true);
103 // Check clock domain
104 err = GetPropertyWrapper (input_device_ids[i], 0, 0, kAudioDevicePropertyClockDomain, &size, &clockdomain);
106 keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
107 if (clockdomain != 0 && clockdomain != keptclockdomain) {
109 printf("AggregateDevice: devices do not share the same clock.\n");
111 need_clock_drift_compensation = true;
116 for (UInt32 i = 0; i < output_device_ids.size(); i++) {
117 set_device_sample_rate_id(output_device_ids[i], sample_rate, true);
119 // Check clock domain
120 err = GetPropertyWrapper (output_device_ids[i], 0, 0, kAudioDevicePropertyClockDomain, &size, &clockdomain);
122 keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain;
123 if (clockdomain != 0 && clockdomain != keptclockdomain) {
125 printf("AggregateDevice: devices do not share the same clock.\n");
127 need_clock_drift_compensation = true;
132 // If no valid clock domain was found, then assume we have to compensate...
133 if (keptclockdomain == 0) {
134 need_clock_drift_compensation = true;
137 //---------------------------------------------------------------------------
138 // Start to create a new aggregate by getting the base audio hardware plugin
139 //---------------------------------------------------------------------------
142 char device_name[256];
143 for (size_t i = 0; i < input_device_ids.size(); ++i) {
144 GetDeviceNameFromID(input_device_ids[i], device_name);
145 printf("Separated input = '%s'\n", device_name);
148 for (size_t i = 0; i < output_device_ids.size(); ++i) {
149 GetDeviceNameFromID(output_device_ids[i], device_name);
150 printf("Separated output = '%s'\n", device_name);
154 err = GetHardwarePropertyInfoWrapper (kAudioHardwarePropertyPlugInForBundleID, &size);
156 fprintf(stderr, "AggregateDevice: AudioHardwareGetPropertyInfo kAudioHardwarePropertyPlugInForBundleID error\n");
160 AudioValueTranslation pluginAVT;
162 CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
164 pluginAVT.mInputData = &inBundleRef;
165 pluginAVT.mInputDataSize = sizeof(inBundleRef);
166 pluginAVT.mOutputData = &_aggregate_plugin_id;
167 pluginAVT.mOutputDataSize = sizeof(AudioDeviceID);
169 err = GetHardwarePropertyWrapper (kAudioHardwarePropertyPlugInForBundleID, &size, &pluginAVT);
171 fprintf(stderr, "AggregateDevice: AudioHardwareGetProperty kAudioHardwarePropertyPlugInForBundleID error\n");
175 //-------------------------------------------------
176 // Create a CFDictionary for our aggregate device
177 //-------------------------------------------------
179 CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
181 CFStringRef AggregateDeviceNameRef = CFSTR("ArdourDuplex");
182 CFStringRef AggregateDeviceUIDRef = CFSTR("com.ardour.CoreAudio");
183 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
184 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
188 CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
189 CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
191 //-------------------------------------------------
192 // Create a CFMutableArray for our sub-device list
193 //-------------------------------------------------
195 // we need to append the UID for each device to a CFMutableArray, so create one here
196 CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
198 std::vector<CFStringRef> captureDeviceUID;
199 for (UInt32 i = 0; i < input_device_ids.size(); i++) {
200 CFStringRef ref = GetDeviceName(input_device_ids[i]);
204 captureDeviceUID.push_back(ref);
205 CFArrayAppendValue(subDevicesArray, ref);
208 std::vector<CFStringRef> playbackDeviceUID;
209 for (UInt32 i = 0; i < output_device_ids.size(); i++) {
210 CFStringRef ref = GetDeviceName(output_device_ids[i]);
214 playbackDeviceUID.push_back(ref);
215 CFArrayAppendValue(subDevicesArray, ref);
218 //-----------------------------------------------------------------------
219 // Feed the dictionary to the plugin, to create a blank aggregate device
220 //-----------------------------------------------------------------------
222 AudioObjectPropertyAddress pluginAOPA;
223 pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
224 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
225 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
228 err = AudioObjectGetPropertyDataSize(_aggregate_plugin_id, &pluginAOPA, 0, NULL, &outDataSize);
230 fprintf(stderr, "AggregateDevice: AudioObjectGetPropertyDataSize error\n");
234 err = AudioObjectGetPropertyData(_aggregate_plugin_id, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, created_device);
236 fprintf(stderr, "AggregateDevice: AudioObjectGetPropertyData error\n");
240 // pause for a bit to make sure that everything completed correctly
241 // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
242 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
244 //-------------------------
245 // Set the sub-device list
246 //-------------------------
248 pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
249 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
250 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
251 outDataSize = sizeof(CFMutableArrayRef);
252 err = AudioObjectSetPropertyData(*created_device, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
254 fprintf(stderr, "AggregateDevice: AudioObjectSetPropertyData for sub-device list error\n");
258 // pause again to give the changes time to take effect
259 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
261 //-----------------------
262 // Set the master device
263 //-----------------------
265 // set the master device manually (this is the device which will act as the master clock for the aggregate device)
266 // pass in the UID of the device you want to use
267 pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
268 pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
269 pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
270 outDataSize = sizeof(CFStringRef);
271 err = AudioObjectSetPropertyData(*created_device, &pluginAOPA, 0, NULL, outDataSize, &playbackDeviceUID[0]);
273 fprintf(stderr, "AggregateDevice: AudioObjectSetPropertyData for playback-master device error\n");
275 err = AudioObjectSetPropertyData(*created_device, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID[0]);
278 fprintf(stderr, "AggregateDevice: AudioObjectSetPropertyData for capture-master device error\n");
282 // pause again to give the changes time to take effect
283 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
285 // Prepare sub-devices for clock drift compensation
286 // Workaround for bug in the HAL : until 10.6.2
287 if (need_clock_drift_compensation) {
289 AudioObjectPropertyAddress theAddressOwned = { kAudioObjectPropertyOwnedObjects, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
290 AudioObjectPropertyAddress theAddressDrift = { kAudioSubDevicePropertyDriftCompensation, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
291 UInt32 theQualifierDataSize = sizeof(AudioObjectID);
292 AudioClassID inClass = kAudioSubDeviceClassID;
293 void* theQualifierData = &inClass;
294 UInt32 subDevicesNum = 0;
297 printf("Clock drift compensation activated...\n");
300 // Get the property data size
301 err = AudioObjectGetPropertyDataSize(*created_device, &theAddressOwned, theQualifierDataSize, theQualifierData, &size);
303 fprintf(stderr, "AggregateDevice: kAudioObjectPropertyOwnedObjects error\n");
306 // Calculate the number of object IDs
307 subDevicesNum = size / sizeof(AudioObjectID);
309 printf("AggregateDevice: clock drift compensation, number of sub-devices = %u\n", (unsigned int)subDevicesNum);
311 AudioObjectID subDevices[subDevicesNum];
312 size = sizeof(subDevices);
314 err = AudioObjectGetPropertyData(*created_device, &theAddressOwned, theQualifierDataSize, theQualifierData, &size, subDevices);
316 fprintf(stderr, "AggregateDevice: kAudioObjectPropertyOwnedObjects error\n");
319 // Set kAudioSubDevicePropertyDriftCompensation property...
320 for (UInt32 index = 0; index < subDevicesNum; ++index) {
321 UInt32 theDriftCompensationValue = 1;
322 err = AudioObjectSetPropertyData(subDevices[index], &theAddressDrift, 0, NULL, sizeof(UInt32), &theDriftCompensationValue);
324 fprintf(stderr, "AggregateDevice: kAudioSubDevicePropertyDriftCompensation error\n");
329 // pause again to give the changes time to take effect
330 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
336 // release the private AD key
337 CFRelease(AggregateDeviceNumberRef);
339 // release the CF objects we have created - we don't need them any more
340 CFRelease(aggDeviceDict);
341 CFRelease(subDevicesArray);
343 // release the device UID
344 for (size_t i = 0; i < captureDeviceUID.size(); ++i) {
345 CFRelease(captureDeviceUID[i]);
348 for (size_t i = 0; i < playbackDeviceUID.size(); ++i) {
349 CFRelease(playbackDeviceUID[i]);
353 printf("AggregateDevice: new aggregate device %u\n", (unsigned int)*created_device);
358 destroy_aggregate_device();