| 1 |
use super::*; |
| 2 |
use std::time::{SystemTime, UNIX_EPOCH}; |
| 3 |
|
| 4 |
pub const DRIFT_COMPENSATION: u32 = 1; |
| 5 |
|
| 6 |
#[derive(Debug)] |
| 7 |
pub struct AggregateDevice { |
| 8 |
plugin_id: AudioObjectID, |
| 9 |
device_id: AudioObjectID, |
| 10 |
// For log only |
| 11 |
input_id: AudioObjectID, |
| 12 |
output_id: AudioObjectID, |
| 13 |
} |
| 14 |
|
| 15 |
#[derive(Debug)] |
| 16 |
pub enum Error { |
| 17 |
OS(OSStatus), |
| 18 |
Timeout(std::time::Duration), |
| 19 |
LessThan2Devices(usize), |
| 20 |
} |
| 21 |
|
| 22 |
impl From<OSStatus> for Error { |
| 23 |
fn from(status: OSStatus) -> Self { |
| 24 |
Error::OS(status) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
impl From<std::time::Duration> for Error { |
| 29 |
fn from(duration: std::time::Duration) -> Self { |
| 30 |
Error::Timeout(duration) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
impl From<usize> for Error { |
| 35 |
fn from(number: usize) -> Self { |
| 36 |
Error::LessThan2Devices(number) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
impl std::fmt::Display for Error { |
| 41 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 42 |
match self { |
| 43 |
Error::OS(status) => write!(f, "OSStatus({})", status), |
| 44 |
Error::Timeout(duration) => write!(f, "Timeout({:?})", duration), |
| 45 |
Error::LessThan2Devices(number) => write!(f, "LessThan2Devices({} only)", number), |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl AggregateDevice { |
| 51 |
// Aggregate Device is a virtual audio interface which utilizes inputs and outputs |
| 52 |
// of one or more physical audio interfaces. It is possible to use the clock of |
| 53 |
// one of the devices as a master clock for all the combined devices and enable |
| 54 |
// drift compensation for the devices that are not designated clock master. |
| 55 |
// |
| 56 |
// Creating a new aggregate device programmatically requires [0][1]: |
| 57 |
// 1. Locate the base plug-in ("com.apple.audio.CoreAudio") |
| 58 |
// 2. Create a dictionary that describes the aggregate device |
| 59 |
// (don't add sub-devices in that step, prone to fail [0]) |
| 60 |
// 3. Ask the base plug-in to create the aggregate device (blank) |
| 61 |
// 4. Add the array of sub-devices. |
| 62 |
// 5. Set the master device (1st output device in our case) |
| 63 |
// 6. Enable drift compensation for the non-master devices |
| 64 |
// |
| 65 |
// [0] https://lists.apple.com/archives/coreaudio-api/2006/Apr/msg00092.html |
| 66 |
// [1] https://lists.apple.com/archives/coreaudio-api/2005/Jul/msg00150.html |
| 67 |
// [2] CoreAudio.framework/Headers/AudioHardware.h |
| 68 |
pub fn new( |
| 69 |
input_id: AudioObjectID, |
| 70 |
output_id: AudioObjectID, |
| 71 |
) -> std::result::Result<Self, Error> { |
| 72 |
debug_assert_running_serially(); |
| 73 |
let plugin_id = Self::get_system_plugin_id()?; |
| 74 |
let device_id = Self::create_blank_device_sync(plugin_id)?; |
| 75 |
|
| 76 |
let mut cleanup = finally(|| { |
| 77 |
let r = Self::destroy_device(plugin_id, device_id); |
| 78 |
assert!(r.is_ok()); |
| 79 |
}); |
| 80 |
|
| 81 |
Self::set_sub_devices_sync(device_id, input_id, output_id)?; |
| 82 |
Self::set_master_device(device_id, output_id)?; |
| 83 |
Self::activate_clock_drift_compensation(device_id)?; |
| 84 |
Self::workaround_for_airpod(device_id, input_id, output_id)?; |
| 85 |
|
| 86 |
cleanup.dismiss(); |
| 87 |
|
| 88 |
cubeb_log!( |
| 89 |
"Add devices input {} and output {} into an aggregate device {}", |
| 90 |
input_id, |
| 91 |
output_id, |
| 92 |
device_id |
| 93 |
); |
| 94 |
Ok(Self { |
| 95 |
plugin_id, |
| 96 |
device_id, |
| 97 |
input_id, |
| 98 |
output_id, |
| 99 |
}) |
| 100 |
} |
| 101 |
|
| 102 |
pub fn get_device_id(&self) -> AudioObjectID { |
| 103 |
self.device_id |
| 104 |
} |
| 105 |
|
| 106 |
// The following APIs are set to `pub` for testing purpose. |
| 107 |
pub fn get_system_plugin_id() -> std::result::Result<AudioObjectID, Error> { |
| 108 |
let address = AudioObjectPropertyAddress { |
| 109 |
mSelector: kAudioHardwarePropertyPlugInForBundleID, |
| 110 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 111 |
mElement: kAudioObjectPropertyElementMaster, |
| 112 |
}; |
| 113 |
|
| 114 |
let mut size: usize = 0; |
| 115 |
let status = |
| 116 |
audio_object_get_property_data_size(kAudioObjectSystemObject, &address, &mut size); |
| 117 |
if status != NO_ERR { |
| 118 |
return Err(Error::from(status)); |
| 119 |
} |
| 120 |
assert_ne!(size, 0); |
| 121 |
|
| 122 |
let mut plugin_id = kAudioObjectUnknown; |
| 123 |
let mut in_bundle_ref = cfstringref_from_static_string("com.apple.audio.CoreAudio"); |
| 124 |
let mut translation_value = AudioValueTranslation { |
| 125 |
mInputData: &mut in_bundle_ref as *mut CFStringRef as *mut c_void, |
| 126 |
mInputDataSize: mem::size_of::<CFStringRef>() as u32, |
| 127 |
mOutputData: &mut plugin_id as *mut AudioObjectID as *mut c_void, |
| 128 |
mOutputDataSize: mem::size_of::<AudioObjectID>() as u32, |
| 129 |
}; |
| 130 |
assert_eq!(size, mem::size_of_val(&translation_value)); |
| 131 |
|
| 132 |
let status = audio_object_get_property_data( |
| 133 |
kAudioObjectSystemObject, |
| 134 |
&address, |
| 135 |
&mut size, |
| 136 |
&mut translation_value, |
| 137 |
); |
| 138 |
unsafe { |
| 139 |
CFRelease(in_bundle_ref as *const c_void); |
| 140 |
} |
| 141 |
if status == NO_ERR { |
| 142 |
assert_ne!(plugin_id, kAudioObjectUnknown); |
| 143 |
Ok(plugin_id) |
| 144 |
} else { |
| 145 |
Err(Error::from(status)) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
pub fn create_blank_device_sync( |
| 150 |
plugin_id: AudioObjectID, |
| 151 |
) -> std::result::Result<AudioObjectID, Error> { |
| 152 |
debug_assert_running_serially(); |
| 153 |
let waiting_time = Duration::new(5, 0); |
| 154 |
|
| 155 |
let condvar_pair = Box::new((Mutex::new(()), Condvar::new())); |
| 156 |
let data_ptr = Box::into_raw(condvar_pair); |
| 157 |
sync_callback_registry_register(data_ptr as usize); |
| 158 |
|
| 159 |
let address = get_property_address( |
| 160 |
Property::HardwareDevices, |
| 161 |
DeviceType::INPUT | DeviceType::OUTPUT, |
| 162 |
); |
| 163 |
|
| 164 |
let status = audio_object_add_property_listener( |
| 165 |
kAudioObjectSystemObject, |
| 166 |
&address, |
| 167 |
devices_changed_callback, |
| 168 |
data_ptr as *mut c_void, |
| 169 |
); |
| 170 |
assert_eq!(status, NO_ERR); |
| 171 |
|
| 172 |
let _teardown = finally(|| { |
| 173 |
let status = audio_object_remove_property_listener( |
| 174 |
kAudioObjectSystemObject, |
| 175 |
&address, |
| 176 |
devices_changed_callback, |
| 177 |
data_ptr as *mut c_void, |
| 178 |
); |
| 179 |
assert_eq!(status, NO_ERR); |
| 180 |
sync_callback_registry_unregister(data_ptr as usize); |
| 181 |
unsafe { drop(Box::from_raw(data_ptr)) }; |
| 182 |
}); |
| 183 |
|
| 184 |
let device = Self::create_blank_device(plugin_id)?; |
| 185 |
|
| 186 |
// Wait until the aggregate is created. |
| 187 |
let (lock, cvar) = unsafe { &*data_ptr }; |
| 188 |
let guard = lock.lock().unwrap(); |
| 189 |
let (_guard, timeout_res) = cvar |
| 190 |
.wait_timeout_while(guard, waiting_time, |()| !get_devices().contains(&device)) |
| 191 |
.unwrap(); |
| 192 |
if timeout_res.timed_out() { |
| 193 |
cubeb_log!( |
| 194 |
"Time out for waiting the creation of aggregate device {}!", |
| 195 |
device |
| 196 |
); |
| 197 |
return Err(Error::from(waiting_time)); |
| 198 |
} |
| 199 |
|
| 200 |
extern "C" fn devices_changed_callback( |
| 201 |
id: AudioObjectID, |
| 202 |
_number_of_addresses: u32, |
| 203 |
_addresses: *const AudioObjectPropertyAddress, |
| 204 |
data: *mut c_void, |
| 205 |
) -> OSStatus { |
| 206 |
assert_eq!(id, kAudioObjectSystemObject); |
| 207 |
with_sync_callback_ptr(data, || { |
| 208 |
let pair = unsafe { &*(data as *const (Mutex<()>, Condvar)) }; |
| 209 |
let (lock, cvar) = pair; |
| 210 |
let _guard = lock.lock().unwrap(); |
| 211 |
cvar.notify_one(); |
| 212 |
}); |
| 213 |
NO_ERR |
| 214 |
} |
| 215 |
|
| 216 |
Ok(device) |
| 217 |
} |
| 218 |
|
| 219 |
pub fn create_blank_device( |
| 220 |
plugin_id: AudioObjectID, |
| 221 |
) -> std::result::Result<AudioObjectID, Error> { |
| 222 |
assert_ne!(plugin_id, kAudioObjectUnknown); |
| 223 |
debug_assert_running_serially(); |
| 224 |
|
| 225 |
let address = AudioObjectPropertyAddress { |
| 226 |
mSelector: kAudioPlugInCreateAggregateDevice, |
| 227 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 228 |
mElement: kAudioObjectPropertyElementMaster, |
| 229 |
}; |
| 230 |
|
| 231 |
let mut size: usize = 0; |
| 232 |
let status = audio_object_get_property_data_size(plugin_id, &address, &mut size); |
| 233 |
if status != NO_ERR { |
| 234 |
return Err(Error::from(status)); |
| 235 |
} |
| 236 |
assert_ne!(size, 0); |
| 237 |
|
| 238 |
let sys_time = SystemTime::now(); |
| 239 |
let time_id = sys_time.duration_since(UNIX_EPOCH).unwrap().as_nanos(); |
| 240 |
let device_name = format!("{}_{}", PRIVATE_AGGREGATE_DEVICE_NAME, time_id); |
| 241 |
let device_uid = format!("org.mozilla.{}", device_name); |
| 242 |
|
| 243 |
let mut device_id = kAudioObjectUnknown; |
| 244 |
let status = unsafe { |
| 245 |
let device_dict = CFMutableDictRef::default(); |
| 246 |
|
| 247 |
// Set the name of the device. |
| 248 |
let device_name = cfstringref_from_string(&device_name); |
| 249 |
device_dict.add_value( |
| 250 |
cfstringref_from_static_string(AGGREGATE_DEVICE_NAME_KEY) as *const c_void, |
| 251 |
device_name as *const c_void, |
| 252 |
); |
| 253 |
CFRelease(device_name as *const c_void); |
| 254 |
|
| 255 |
// Set the uid of the device. |
| 256 |
let device_uid = cfstringref_from_string(&device_uid); |
| 257 |
device_dict.add_value( |
| 258 |
cfstringref_from_static_string(AGGREGATE_DEVICE_UID_KEY) as *const c_void, |
| 259 |
device_uid as *const c_void, |
| 260 |
); |
| 261 |
CFRelease(device_uid as *const c_void); |
| 262 |
|
| 263 |
// Make the device private to the process creating it. |
| 264 |
let private_value: i32 = 1; |
| 265 |
let device_private_key = CFNumberCreate( |
| 266 |
kCFAllocatorDefault, |
| 267 |
i64::from(kCFNumberIntType), |
| 268 |
&private_value as *const i32 as *const c_void, |
| 269 |
); |
| 270 |
device_dict.add_value( |
| 271 |
cfstringref_from_static_string(AGGREGATE_DEVICE_PRIVATE_KEY) as *const c_void, |
| 272 |
device_private_key as *const c_void, |
| 273 |
); |
| 274 |
CFRelease(device_private_key as *const c_void); |
| 275 |
|
| 276 |
// Set the device to a stacked aggregate (i.e. multi-output device). |
| 277 |
let stacked_value: i32 = 0; // 1 for normal aggregate device. |
| 278 |
let device_stacked_key = CFNumberCreate( |
| 279 |
kCFAllocatorDefault, |
| 280 |
i64::from(kCFNumberIntType), |
| 281 |
&stacked_value as *const i32 as *const c_void, |
| 282 |
); |
| 283 |
device_dict.add_value( |
| 284 |
cfstringref_from_static_string(AGGREGATE_DEVICE_STACKED_KEY) as *const c_void, |
| 285 |
device_stacked_key as *const c_void, |
| 286 |
); |
| 287 |
CFRelease(device_stacked_key as *const c_void); |
| 288 |
|
| 289 |
// This call will fire `audiounit_collection_changed_callback` indirectly! |
| 290 |
audio_object_get_property_data_with_qualifier( |
| 291 |
plugin_id, |
| 292 |
&address, |
| 293 |
mem::size_of_val(&device_dict), |
| 294 |
&device_dict, |
| 295 |
&mut size, |
| 296 |
&mut device_id, |
| 297 |
) |
| 298 |
}; |
| 299 |
if status == NO_ERR { |
| 300 |
assert_ne!(device_id, kAudioObjectUnknown); |
| 301 |
Ok(device_id) |
| 302 |
} else { |
| 303 |
Err(Error::from(status)) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
pub fn set_sub_devices_sync( |
| 308 |
device_id: AudioDeviceID, |
| 309 |
input_id: AudioDeviceID, |
| 310 |
output_id: AudioDeviceID, |
| 311 |
) -> std::result::Result<(), Error> { |
| 312 |
debug_assert_running_serially(); |
| 313 |
let address = AudioObjectPropertyAddress { |
| 314 |
mSelector: kAudioAggregateDevicePropertyFullSubDeviceList, |
| 315 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 316 |
mElement: kAudioObjectPropertyElementMaster, |
| 317 |
}; |
| 318 |
|
| 319 |
let waiting_time = Duration::new(5, 0); |
| 320 |
|
| 321 |
let condvar_pair = Box::new((Mutex::new(AudioObjectID::default()), Condvar::new())); |
| 322 |
let data_ptr = Box::into_raw(condvar_pair); |
| 323 |
sync_callback_registry_register(data_ptr as usize); |
| 324 |
|
| 325 |
let status = audio_object_add_property_listener( |
| 326 |
device_id, |
| 327 |
&address, |
| 328 |
devices_changed_callback, |
| 329 |
data_ptr as *mut c_void, |
| 330 |
); |
| 331 |
if status != NO_ERR { |
| 332 |
sync_callback_registry_unregister(data_ptr as usize); |
| 333 |
unsafe { drop(Box::from_raw(data_ptr)) }; |
| 334 |
return Err(Error::from(status)); |
| 335 |
} |
| 336 |
|
| 337 |
let _teardown = finally(|| { |
| 338 |
let status = audio_object_remove_property_listener( |
| 339 |
device_id, |
| 340 |
&address, |
| 341 |
devices_changed_callback, |
| 342 |
data_ptr as *mut c_void, |
| 343 |
); |
| 344 |
// kAudioHardwareBadObjectError implies device_id is somehow dead, |
| 345 |
// so its listener should receive nothing. |
| 346 |
assert!(status == NO_ERR || status == (kAudioHardwareBadObjectError as OSStatus)); |
| 347 |
sync_callback_registry_unregister(data_ptr as usize); |
| 348 |
unsafe { drop(Box::from_raw(data_ptr)) }; |
| 349 |
}); |
| 350 |
|
| 351 |
Self::set_sub_devices(device_id, input_id, output_id)?; |
| 352 |
|
| 353 |
// Wait until the sub devices are added. |
| 354 |
let (lock, cvar) = unsafe { &*data_ptr }; |
| 355 |
let device = lock.lock().unwrap(); |
| 356 |
if *device != device_id { |
| 357 |
let (dev, timeout_res) = cvar.wait_timeout(device, waiting_time).unwrap(); |
| 358 |
if timeout_res.timed_out() { |
| 359 |
cubeb_log!( |
| 360 |
"Time out for waiting for adding devices({}, {}) to aggregate device {}!", |
| 361 |
input_id, |
| 362 |
output_id, |
| 363 |
device_id |
| 364 |
); |
| 365 |
} |
| 366 |
if *dev != device_id { |
| 367 |
return Err(Error::from(waiting_time)); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
extern "C" fn devices_changed_callback( |
| 372 |
id: AudioObjectID, |
| 373 |
_number_of_addresses: u32, |
| 374 |
_addresses: *const AudioObjectPropertyAddress, |
| 375 |
data: *mut c_void, |
| 376 |
) -> OSStatus { |
| 377 |
with_sync_callback_ptr(data, || { |
| 378 |
let pair = unsafe { &*(data as *const (Mutex<AudioObjectID>, Condvar)) }; |
| 379 |
let (lock, cvar) = pair; |
| 380 |
let mut device = lock.lock().unwrap(); |
| 381 |
*device = id; |
| 382 |
cvar.notify_one(); |
| 383 |
}); |
| 384 |
NO_ERR |
| 385 |
} |
| 386 |
|
| 387 |
Ok(()) |
| 388 |
} |
| 389 |
|
| 390 |
pub fn set_sub_devices( |
| 391 |
device_id: AudioDeviceID, |
| 392 |
input_id: AudioDeviceID, |
| 393 |
output_id: AudioDeviceID, |
| 394 |
) -> std::result::Result<(), Error> { |
| 395 |
assert_ne!(device_id, kAudioObjectUnknown); |
| 396 |
assert_ne!(input_id, kAudioObjectUnknown); |
| 397 |
assert_ne!(output_id, kAudioObjectUnknown); |
| 398 |
assert_ne!(input_id, output_id); |
| 399 |
debug_assert_running_serially(); |
| 400 |
|
| 401 |
let output_sub_devices = Self::get_sub_devices_or_self(output_id)?; |
| 402 |
let input_sub_devices = Self::get_sub_devices_or_self(input_id)?; |
| 403 |
|
| 404 |
unsafe { |
| 405 |
let sub_devices = CFArrayCreateMutable(ptr::null(), 0, &kCFTypeArrayCallBacks); |
| 406 |
// The order of the items in the array is significant and is used to determine the order of the streams |
| 407 |
// of the AudioAggregateDevice. |
| 408 |
for device in input_sub_devices { |
| 409 |
let uid = get_device_global_uid(device)?; |
| 410 |
CFArrayAppendValue(sub_devices, uid.get_raw() as *const c_void); |
| 411 |
} |
| 412 |
|
| 413 |
for device in output_sub_devices { |
| 414 |
let uid = get_device_global_uid(device)?; |
| 415 |
CFArrayAppendValue(sub_devices, uid.get_raw() as *const c_void); |
| 416 |
} |
| 417 |
|
| 418 |
let address = AudioObjectPropertyAddress { |
| 419 |
mSelector: kAudioAggregateDevicePropertyFullSubDeviceList, |
| 420 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 421 |
mElement: kAudioObjectPropertyElementMaster, |
| 422 |
}; |
| 423 |
|
| 424 |
let size = mem::size_of::<CFMutableArrayRef>(); |
| 425 |
let status = audio_object_set_property_data(device_id, &address, size, &sub_devices); |
| 426 |
CFRelease(sub_devices as *const c_void); |
| 427 |
if status == NO_ERR { |
| 428 |
Ok(()) |
| 429 |
} else { |
| 430 |
Err(Error::from(status)) |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
pub fn get_sub_devices( |
| 436 |
device_id: AudioDeviceID, |
| 437 |
) -> std::result::Result<Vec<AudioObjectID>, Error> { |
| 438 |
assert_ne!(device_id, kAudioObjectUnknown); |
| 439 |
debug_assert_running_serially(); |
| 440 |
|
| 441 |
let mut sub_devices = Vec::new(); |
| 442 |
let address = AudioObjectPropertyAddress { |
| 443 |
mSelector: kAudioAggregateDevicePropertyActiveSubDeviceList, |
| 444 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 445 |
mElement: kAudioObjectPropertyElementMaster, |
| 446 |
}; |
| 447 |
let mut size: usize = 0; |
| 448 |
let status = audio_object_get_property_data_size(device_id, &address, &mut size); |
| 449 |
|
| 450 |
if status != NO_ERR { |
| 451 |
return Err(Error::from(status)); |
| 452 |
} |
| 453 |
|
| 454 |
let count = size / mem::size_of::<AudioObjectID>(); |
| 455 |
sub_devices = allocate_array(count); |
| 456 |
let status = audio_object_get_property_data( |
| 457 |
device_id, |
| 458 |
&address, |
| 459 |
&mut size, |
| 460 |
sub_devices.as_mut_ptr(), |
| 461 |
); |
| 462 |
|
| 463 |
if status == NO_ERR { |
| 464 |
Ok(sub_devices) |
| 465 |
} else { |
| 466 |
Err(Error::from(status)) |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
pub fn get_sub_devices_or_self( |
| 471 |
device_id: AudioDeviceID, |
| 472 |
) -> std::result::Result<Vec<AudioObjectID>, Error> { |
| 473 |
AggregateDevice::get_sub_devices(device_id).or_else(|e| match e { |
| 474 |
Error::OS(status) if status == kAudioHardwareUnknownPropertyError as OSStatus => { |
| 475 |
Ok(vec![device_id]) |
| 476 |
} |
| 477 |
_ => Err(e), |
| 478 |
}) |
| 479 |
} |
| 480 |
|
| 481 |
pub fn get_master_device_uid(device_id: AudioDeviceID) -> std::result::Result<String, Error> { |
| 482 |
debug_assert_running_serially(); |
| 483 |
let address = AudioObjectPropertyAddress { |
| 484 |
mSelector: kAudioAggregateDevicePropertyMainSubDevice, |
| 485 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 486 |
mElement: kAudioObjectPropertyElementMaster, |
| 487 |
}; |
| 488 |
|
| 489 |
let mut master: CFStringRef = ptr::null_mut(); |
| 490 |
let mut size = mem::size_of::<CFStringRef>(); |
| 491 |
let status = audio_object_get_property_data(device_id, &address, &mut size, &mut master); |
| 492 |
if status != NO_ERR { |
| 493 |
return Err(Error::from(status)); |
| 494 |
} |
| 495 |
|
| 496 |
if master.is_null() { |
| 497 |
return Ok(String::default()); |
| 498 |
} |
| 499 |
|
| 500 |
let master = StringRef::new(master as _); |
| 501 |
Ok(master.into_string()) |
| 502 |
} |
| 503 |
|
| 504 |
pub fn set_master_device( |
| 505 |
device_id: AudioDeviceID, |
| 506 |
primary_id: AudioDeviceID, |
| 507 |
) -> std::result::Result<(), Error> { |
| 508 |
assert_ne!(device_id, kAudioObjectUnknown); |
| 509 |
assert_ne!(primary_id, kAudioObjectUnknown); |
| 510 |
debug_assert_running_serially(); |
| 511 |
|
| 512 |
cubeb_log!( |
| 513 |
"Set master device of the aggregate device {} to device {}", |
| 514 |
device_id, |
| 515 |
primary_id |
| 516 |
); |
| 517 |
|
| 518 |
let address = AudioObjectPropertyAddress { |
| 519 |
mSelector: kAudioAggregateDevicePropertyMainSubDevice, |
| 520 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 521 |
mElement: kAudioObjectPropertyElementMaster, |
| 522 |
}; |
| 523 |
|
| 524 |
// The master device will be the 1st sub device of the primary device. |
| 525 |
let output_sub_devices = Self::get_sub_devices_or_self(primary_id)?; |
| 526 |
assert!(!output_sub_devices.is_empty()); |
| 527 |
let master_sub_device_uid = get_device_global_uid(output_sub_devices[0]).unwrap(); |
| 528 |
let master_sub_device = master_sub_device_uid.get_raw(); |
| 529 |
let size = mem::size_of::<CFStringRef>(); |
| 530 |
let status = audio_object_set_property_data(device_id, &address, size, &master_sub_device); |
| 531 |
if status == NO_ERR { |
| 532 |
Ok(()) |
| 533 |
} else { |
| 534 |
Err(Error::from(status)) |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
pub fn activate_clock_drift_compensation( |
| 539 |
device_id: AudioObjectID, |
| 540 |
) -> std::result::Result<(), Error> { |
| 541 |
assert_ne!(device_id, kAudioObjectUnknown); |
| 542 |
debug_assert_running_serially(); |
| 543 |
let address = AudioObjectPropertyAddress { |
| 544 |
mSelector: kAudioObjectPropertyOwnedObjects, |
| 545 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 546 |
mElement: kAudioObjectPropertyElementMaster, |
| 547 |
}; |
| 548 |
|
| 549 |
let qualifier_data_size = mem::size_of::<AudioObjectID>(); |
| 550 |
let class_id: AudioClassID = kAudioSubDeviceClassID; |
| 551 |
let qualifier_data = &class_id; |
| 552 |
|
| 553 |
let mut size: usize = 0; |
| 554 |
let status = audio_object_get_property_data_size_with_qualifier( |
| 555 |
device_id, |
| 556 |
&address, |
| 557 |
qualifier_data_size, |
| 558 |
qualifier_data, |
| 559 |
&mut size, |
| 560 |
); |
| 561 |
if status != NO_ERR { |
| 562 |
return Err(Error::from(status)); |
| 563 |
} |
| 564 |
assert!(size > 0); |
| 565 |
let subdevices_num = size / mem::size_of::<AudioObjectID>(); |
| 566 |
if subdevices_num < 2 { |
| 567 |
cubeb_log!( |
| 568 |
"Aggregate-device {} contains {} sub-devices only.\ |
| 569 |
We should have at least one input and one output device.", |
| 570 |
device_id, |
| 571 |
subdevices_num |
| 572 |
); |
| 573 |
return Err(Error::LessThan2Devices(subdevices_num)); |
| 574 |
} |
| 575 |
let mut sub_devices: Vec<AudioObjectID> = allocate_array(subdevices_num); |
| 576 |
let status = audio_object_get_property_data_with_qualifier( |
| 577 |
device_id, |
| 578 |
&address, |
| 579 |
qualifier_data_size, |
| 580 |
qualifier_data, |
| 581 |
&mut size, |
| 582 |
sub_devices.as_mut_ptr(), |
| 583 |
); |
| 584 |
if status != NO_ERR { |
| 585 |
return Err(Error::from(status)); |
| 586 |
} |
| 587 |
|
| 588 |
let master_sub_device_uid = Self::get_master_device_uid(device_id)?; |
| 589 |
|
| 590 |
let address = AudioObjectPropertyAddress { |
| 591 |
mSelector: kAudioSubDevicePropertyDriftCompensation, |
| 592 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 593 |
mElement: kAudioObjectPropertyElementMaster, |
| 594 |
}; |
| 595 |
|
| 596 |
for &device in &sub_devices { |
| 597 |
let uid = get_device_global_uid(device) |
| 598 |
.map(|sr| sr.into_string()) |
| 599 |
.unwrap_or_default(); |
| 600 |
if uid == master_sub_device_uid { |
| 601 |
continue; |
| 602 |
} |
| 603 |
let status = audio_object_set_property_data( |
| 604 |
device, |
| 605 |
&address, |
| 606 |
mem::size_of::<u32>(), |
| 607 |
&DRIFT_COMPENSATION, |
| 608 |
); |
| 609 |
if status != NO_ERR { |
| 610 |
cubeb_log!( |
| 611 |
"Failed to set drift compensation for {}. Ignore it.", |
| 612 |
device |
| 613 |
); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
Ok(()) |
| 618 |
} |
| 619 |
|
| 620 |
pub fn destroy_device( |
| 621 |
plugin_id: AudioObjectID, |
| 622 |
mut device_id: AudioDeviceID, |
| 623 |
) -> std::result::Result<(), Error> { |
| 624 |
assert_ne!(plugin_id, kAudioObjectUnknown); |
| 625 |
assert_ne!(device_id, kAudioObjectUnknown); |
| 626 |
debug_assert_running_serially(); |
| 627 |
|
| 628 |
let address = AudioObjectPropertyAddress { |
| 629 |
mSelector: kAudioPlugInDestroyAggregateDevice, |
| 630 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 631 |
mElement: kAudioObjectPropertyElementMaster, |
| 632 |
}; |
| 633 |
|
| 634 |
let mut size: usize = 0; |
| 635 |
let status = audio_object_get_property_data_size(plugin_id, &address, &mut size); |
| 636 |
if status != NO_ERR { |
| 637 |
return Err(Error::from(status)); |
| 638 |
} |
| 639 |
assert!(size > 0); |
| 640 |
|
| 641 |
let status = audio_object_get_property_data(plugin_id, &address, &mut size, &mut device_id); |
| 642 |
if status == NO_ERR { |
| 643 |
Ok(()) |
| 644 |
} else { |
| 645 |
Err(Error::from(status)) |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
pub fn workaround_for_airpod( |
| 650 |
device_id: AudioDeviceID, |
| 651 |
input_id: AudioDeviceID, |
| 652 |
output_id: AudioDeviceID, |
| 653 |
) -> std::result::Result<(), Error> { |
| 654 |
assert_ne!(device_id, kAudioObjectUnknown); |
| 655 |
assert_ne!(input_id, kAudioObjectUnknown); |
| 656 |
assert_ne!(output_id, kAudioObjectUnknown); |
| 657 |
assert_ne!(input_id, output_id); |
| 658 |
debug_assert_running_serially(); |
| 659 |
|
| 660 |
let label = get_device_label(input_id, DeviceType::INPUT)?; |
| 661 |
let input_label = label.into_string(); |
| 662 |
|
| 663 |
let label = get_device_label(output_id, DeviceType::OUTPUT)?; |
| 664 |
let output_label = label.into_string(); |
| 665 |
|
| 666 |
if input_label.contains("AirPods") && output_label.contains("AirPods") { |
| 667 |
let input_rate = |
| 668 |
get_device_sample_rate(input_id, DeviceType::INPUT | DeviceType::OUTPUT)?; |
| 669 |
cubeb_log!( |
| 670 |
"The nominal rate of the input device {}: {}", |
| 671 |
input_id, |
| 672 |
input_rate |
| 673 |
); |
| 674 |
|
| 675 |
let output_rate = |
| 676 |
match get_device_sample_rate(output_id, DeviceType::INPUT | DeviceType::OUTPUT) { |
| 677 |
Ok(rate) => format!("{}", rate), |
| 678 |
Err(e) => format!("Error {}", e), |
| 679 |
}; |
| 680 |
cubeb_log!( |
| 681 |
"The nominal rate of the output device {}: {}", |
| 682 |
output_id, |
| 683 |
output_rate |
| 684 |
); |
| 685 |
|
| 686 |
let addr = AudioObjectPropertyAddress { |
| 687 |
mSelector: kAudioDevicePropertyNominalSampleRate, |
| 688 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 689 |
mElement: kAudioObjectPropertyElementMaster, |
| 690 |
}; |
| 691 |
|
| 692 |
let status = audio_object_set_property_data( |
| 693 |
device_id, |
| 694 |
&addr, |
| 695 |
mem::size_of::<f64>(), |
| 696 |
&input_rate, |
| 697 |
); |
| 698 |
if status != NO_ERR { |
| 699 |
return Err(Error::from(status)); |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
Ok(()) |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
impl Default for AggregateDevice { |
| 708 |
fn default() -> Self { |
| 709 |
Self { |
| 710 |
plugin_id: kAudioObjectUnknown, |
| 711 |
device_id: kAudioObjectUnknown, |
| 712 |
input_id: kAudioObjectUnknown, |
| 713 |
output_id: kAudioObjectUnknown, |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
impl Drop for AggregateDevice { |
| 719 |
fn drop(&mut self) { |
| 720 |
debug_assert_running_serially(); |
| 721 |
if self.plugin_id != kAudioObjectUnknown && self.device_id != kAudioObjectUnknown { |
| 722 |
if let Err(r) = Self::destroy_device(self.plugin_id, self.device_id) { |
| 723 |
cubeb_log!( |
| 724 |
"Failed to destroyed aggregate device {}. Error: {}", |
| 725 |
self.device_id, |
| 726 |
r |
| 727 |
); |
| 728 |
} else { |
| 729 |
cubeb_log!( |
| 730 |
"Destroyed aggregate device {} (input {}, output {})", |
| 731 |
self.device_id, |
| 732 |
self.input_id, |
| 733 |
self.output_id |
| 734 |
); |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
} |