| 1 |
use super::*; |
| 2 |
|
| 3 |
// Common Utils |
| 4 |
// ------------------------------------------------------------------------------------------------ |
| 5 |
pub extern "C" fn noop_data_callback( |
| 6 |
stream: *mut ffi::cubeb_stream, |
| 7 |
_user_ptr: *mut c_void, |
| 8 |
_input_buffer: *const c_void, |
| 9 |
output_buffer: *mut c_void, |
| 10 |
nframes: i64, |
| 11 |
) -> i64 { |
| 12 |
assert!(!stream.is_null()); |
| 13 |
|
| 14 |
// Feed silence data to output buffer |
| 15 |
if !output_buffer.is_null() { |
| 16 |
let stm = unsafe { &mut *(stream as *mut AudioUnitStream) }; |
| 17 |
let channels = stm.core_stream_data.output_stream_params.channels(); |
| 18 |
let samples = nframes as usize * channels as usize; |
| 19 |
let sample_size = cubeb_sample_size(stm.core_stream_data.output_stream_params.format()); |
| 20 |
unsafe { |
| 21 |
ptr::write_bytes(output_buffer, 0, samples * sample_size); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
nframes |
| 26 |
} |
| 27 |
|
| 28 |
pub extern "C" fn draining_data_callback( |
| 29 |
stream: *mut ffi::cubeb_stream, |
| 30 |
_user_ptr: *mut c_void, |
| 31 |
_input_buffer: *const c_void, |
| 32 |
output_buffer: *mut c_void, |
| 33 |
nframes: i64, |
| 34 |
) -> i64 { |
| 35 |
assert!(!stream.is_null()); |
| 36 |
|
| 37 |
// Feed silence data to output buffer |
| 38 |
if !output_buffer.is_null() { |
| 39 |
let stm = unsafe { &mut *(stream as *mut AudioUnitStream) }; |
| 40 |
let channels = stm.core_stream_data.output_stream_params.channels(); |
| 41 |
let samples = nframes as usize * channels as usize; |
| 42 |
let sample_size = cubeb_sample_size(stm.core_stream_data.output_stream_params.format()); |
| 43 |
unsafe { |
| 44 |
ptr::write_bytes(output_buffer, 0, samples * sample_size); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
nframes - 1 |
| 49 |
} |
| 50 |
|
| 51 |
#[derive(Default)] |
| 52 |
pub struct StateCallbackData { |
| 53 |
started_cnt: AtomicU32, |
| 54 |
stopped_cnt: AtomicU32, |
| 55 |
drained_cnt: AtomicU32, |
| 56 |
error_cnt: AtomicU32, |
| 57 |
} |
| 58 |
|
| 59 |
impl StateCallbackData { |
| 60 |
pub fn started_cnt(&self) -> u32 { |
| 61 |
self.started_cnt.load(Ordering::SeqCst) |
| 62 |
} |
| 63 |
pub fn stopped_cnt(&self) -> u32 { |
| 64 |
self.stopped_cnt.load(Ordering::SeqCst) |
| 65 |
} |
| 66 |
pub fn drained_cnt(&self) -> u32 { |
| 67 |
self.drained_cnt.load(Ordering::SeqCst) |
| 68 |
} |
| 69 |
pub fn error_cnt(&self) -> u32 { |
| 70 |
self.error_cnt.load(Ordering::SeqCst) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
pub extern "C" fn state_tracking_cb( |
| 75 |
stream: *mut ffi::cubeb_stream, |
| 76 |
_usr_ptr: *mut c_void, |
| 77 |
state: u32, |
| 78 |
) { |
| 79 |
let data = unsafe { (_usr_ptr as *mut StateCallbackData).as_mut() }.unwrap(); |
| 80 |
match state { |
| 81 |
ffi::CUBEB_STATE_STARTED => { |
| 82 |
data.started_cnt.fetch_add(1, Ordering::SeqCst); |
| 83 |
cubeb_log!("({:p}) state is now started", stream); |
| 84 |
} |
| 85 |
ffi::CUBEB_STATE_STOPPED => { |
| 86 |
data.stopped_cnt.fetch_add(1, Ordering::SeqCst); |
| 87 |
cubeb_log!("({:p}) state is now stopped", stream); |
| 88 |
} |
| 89 |
ffi::CUBEB_STATE_DRAINED => { |
| 90 |
data.drained_cnt.fetch_add(1, Ordering::SeqCst); |
| 91 |
cubeb_log!("({:p}) state is now drained", stream); |
| 92 |
} |
| 93 |
ffi::CUBEB_STATE_ERROR => { |
| 94 |
data.error_cnt.fetch_add(1, Ordering::SeqCst); |
| 95 |
cubeb_log!("({:p}) state is now error", stream); |
| 96 |
} |
| 97 |
_ => unreachable!("unknown state"), |
| 98 |
}; |
| 99 |
} |
| 100 |
|
| 101 |
#[derive(Clone, Debug, PartialEq)] |
| 102 |
pub enum Scope { |
| 103 |
Input, |
| 104 |
Output, |
| 105 |
} |
| 106 |
|
| 107 |
impl From<Scope> for DeviceType { |
| 108 |
fn from(scope: Scope) -> Self { |
| 109 |
match scope { |
| 110 |
Scope::Input => DeviceType::INPUT, |
| 111 |
Scope::Output => DeviceType::OUTPUT, |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#[derive(Clone)] |
| 117 |
pub enum PropertyScope { |
| 118 |
Input, |
| 119 |
Output, |
| 120 |
} |
| 121 |
|
| 122 |
pub fn test_get_default_device(scope: Scope) -> Option<AudioObjectID> { |
| 123 |
debug_assert_not_running_serially(); |
| 124 |
run_serially_forward_panics(|| { |
| 125 |
let address = AudioObjectPropertyAddress { |
| 126 |
mSelector: match scope { |
| 127 |
Scope::Input => kAudioHardwarePropertyDefaultInputDevice, |
| 128 |
Scope::Output => kAudioHardwarePropertyDefaultOutputDevice, |
| 129 |
}, |
| 130 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 131 |
mElement: kAudioObjectPropertyElementMaster, |
| 132 |
}; |
| 133 |
|
| 134 |
let mut devid: AudioObjectID = kAudioObjectUnknown; |
| 135 |
let mut size = mem::size_of::<AudioObjectID>(); |
| 136 |
let status = unsafe { |
| 137 |
AudioObjectGetPropertyData( |
| 138 |
kAudioObjectSystemObject, |
| 139 |
&address, |
| 140 |
0, |
| 141 |
ptr::null(), |
| 142 |
&mut size as *mut usize as *mut UInt32, |
| 143 |
&mut devid as *mut AudioObjectID as *mut c_void, |
| 144 |
) |
| 145 |
}; |
| 146 |
|
| 147 |
if status != NO_ERR || devid == kAudioObjectUnknown { |
| 148 |
return None; |
| 149 |
} |
| 150 |
Some(devid) |
| 151 |
}) |
| 152 |
} |
| 153 |
|
| 154 |
// TODO: Create a GetProperty trait and add a default implementation for it, then implement it |
| 155 |
// for TestAudioUnit so the member method like `get_buffer_frame_size` can reuse the trait |
| 156 |
// method get_property_data. |
| 157 |
#[derive(Debug)] |
| 158 |
pub struct TestAudioUnit(AudioUnit); |
| 159 |
|
| 160 |
impl TestAudioUnit { |
| 161 |
fn new(unit: AudioUnit) -> Self { |
| 162 |
assert!(!unit.is_null()); |
| 163 |
Self(unit) |
| 164 |
} |
| 165 |
pub fn get_inner(&self) -> AudioUnit { |
| 166 |
self.0 |
| 167 |
} |
| 168 |
pub fn get_buffer_frame_size( |
| 169 |
&self, |
| 170 |
scope: Scope, |
| 171 |
prop_scope: PropertyScope, |
| 172 |
) -> std::result::Result<u32, OSStatus> { |
| 173 |
test_audiounit_get_buffer_frame_size(self.0, scope, prop_scope) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
impl Drop for TestAudioUnit { |
| 178 |
fn drop(&mut self) { |
| 179 |
run_serially_forward_panics(|| unsafe { |
| 180 |
AudioUnitUninitialize(self.0); |
| 181 |
AudioComponentInstanceDispose(self.0); |
| 182 |
}); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// TODO: 1. Return Result with custom errors. |
| 187 |
// 2. Allow to create a in-out unit. |
| 188 |
pub fn test_get_default_audiounit(scope: Scope) -> Option<TestAudioUnit> { |
| 189 |
debug_assert_not_running_serially(); |
| 190 |
let device = test_get_default_device(scope.clone()); |
| 191 |
let unit = test_create_audiounit(ComponentSubType::HALOutput); |
| 192 |
if device.is_none() || unit.is_none() { |
| 193 |
return None; |
| 194 |
} |
| 195 |
let unit = unit.unwrap(); |
| 196 |
let device = device.unwrap(); |
| 197 |
match scope { |
| 198 |
Scope::Input => { |
| 199 |
if test_enable_audiounit_in_scope(unit.get_inner(), Scope::Input, true).is_err() |
| 200 |
|| test_enable_audiounit_in_scope(unit.get_inner(), Scope::Output, false).is_err() |
| 201 |
{ |
| 202 |
return None; |
| 203 |
} |
| 204 |
} |
| 205 |
Scope::Output => { |
| 206 |
if test_enable_audiounit_in_scope(unit.get_inner(), Scope::Input, false).is_err() |
| 207 |
|| test_enable_audiounit_in_scope(unit.get_inner(), Scope::Output, true).is_err() |
| 208 |
{ |
| 209 |
return None; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
let status = run_serially(|| unsafe { |
| 215 |
AudioUnitSetProperty( |
| 216 |
unit.get_inner(), |
| 217 |
kAudioOutputUnitProperty_CurrentDevice, |
| 218 |
kAudioUnitScope_Global, |
| 219 |
0, // Global bus |
| 220 |
&device as *const AudioObjectID as *const c_void, |
| 221 |
mem::size_of::<AudioObjectID>() as u32, |
| 222 |
) |
| 223 |
}); |
| 224 |
if status == NO_ERR { |
| 225 |
Some(unit) |
| 226 |
} else { |
| 227 |
None |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
pub enum ComponentSubType { |
| 232 |
HALOutput, |
| 233 |
DefaultOutput, |
| 234 |
} |
| 235 |
|
| 236 |
// TODO: Return Result with custom errors. |
| 237 |
// Surprisingly the AudioUnit can be created even when there is no any device on the platform, |
| 238 |
// no matter its subtype is HALOutput or DefaultOutput. |
| 239 |
pub fn test_create_audiounit(unit_type: ComponentSubType) -> Option<TestAudioUnit> { |
| 240 |
debug_assert_not_running_serially(); |
| 241 |
let desc = AudioComponentDescription { |
| 242 |
componentType: kAudioUnitType_Output, |
| 243 |
componentSubType: match unit_type { |
| 244 |
ComponentSubType::HALOutput => kAudioUnitSubType_HALOutput, |
| 245 |
ComponentSubType::DefaultOutput => kAudioUnitSubType_DefaultOutput, |
| 246 |
}, |
| 247 |
componentManufacturer: kAudioUnitManufacturer_Apple, |
| 248 |
componentFlags: 0, |
| 249 |
componentFlagsMask: 0, |
| 250 |
}; |
| 251 |
let comp = run_serially(|| unsafe { AudioComponentFindNext(ptr::null_mut(), &desc) }); |
| 252 |
if comp.is_null() { |
| 253 |
return None; |
| 254 |
} |
| 255 |
let mut unit: AudioUnit = ptr::null_mut(); |
| 256 |
let status = run_serially(|| unsafe { AudioComponentInstanceNew(comp, &mut unit) }); |
| 257 |
// TODO: Is unit possible to be null when no error returns ? |
| 258 |
if status != NO_ERR || unit.is_null() { |
| 259 |
None |
| 260 |
} else { |
| 261 |
Some(TestAudioUnit::new(unit)) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
fn test_enable_audiounit_in_scope( |
| 266 |
unit: AudioUnit, |
| 267 |
scope: Scope, |
| 268 |
enable: bool, |
| 269 |
) -> std::result::Result<(), OSStatus> { |
| 270 |
debug_assert_not_running_serially(); |
| 271 |
assert!(!unit.is_null()); |
| 272 |
let (scope, element) = match scope { |
| 273 |
Scope::Input => (kAudioUnitScope_Input, AU_IN_BUS), |
| 274 |
Scope::Output => (kAudioUnitScope_Output, AU_OUT_BUS), |
| 275 |
}; |
| 276 |
let on_off: u32 = if enable { 1 } else { 0 }; |
| 277 |
let status = run_serially(|| unsafe { |
| 278 |
AudioUnitSetProperty( |
| 279 |
unit, |
| 280 |
kAudioOutputUnitProperty_EnableIO, |
| 281 |
scope, |
| 282 |
element, |
| 283 |
&on_off as *const u32 as *const c_void, |
| 284 |
mem::size_of::<u32>() as u32, |
| 285 |
) |
| 286 |
}); |
| 287 |
if status == NO_ERR { |
| 288 |
Ok(()) |
| 289 |
} else { |
| 290 |
Err(status) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
pub enum DeviceFilter { |
| 295 |
ExcludeCubebAggregateAndVPIO, |
| 296 |
ExcludeVPIO, |
| 297 |
IncludeAll, |
| 298 |
} |
| 299 |
pub fn test_get_all_devices(filter: DeviceFilter) -> Vec<AudioObjectID> { |
| 300 |
debug_assert_not_running_serially(); |
| 301 |
// To avoid races, the devices getter and the device name filtering have |
| 302 |
// to run in the same serial task. If not, a device may exist when the |
| 303 |
// getter runs but not when getting its uid. |
| 304 |
run_serially_forward_panics(|| { |
| 305 |
let mut devices = Vec::new(); |
| 306 |
let address = AudioObjectPropertyAddress { |
| 307 |
mSelector: kAudioHardwarePropertyDevices, |
| 308 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 309 |
mElement: kAudioObjectPropertyElementMaster, |
| 310 |
}; |
| 311 |
let mut size: usize = 0; |
| 312 |
let status = unsafe { |
| 313 |
AudioObjectGetPropertyDataSize( |
| 314 |
kAudioObjectSystemObject, |
| 315 |
&address, |
| 316 |
0, |
| 317 |
ptr::null(), |
| 318 |
&mut size as *mut usize as *mut u32, |
| 319 |
) |
| 320 |
}; |
| 321 |
// size will be 0 if there is no device at all. |
| 322 |
if status != NO_ERR || size == 0 { |
| 323 |
return devices; |
| 324 |
} |
| 325 |
assert_eq!(size % mem::size_of::<AudioObjectID>(), 0); |
| 326 |
let elements = size / mem::size_of::<AudioObjectID>(); |
| 327 |
devices.resize(elements, kAudioObjectUnknown); |
| 328 |
let status = unsafe { |
| 329 |
AudioObjectGetPropertyData( |
| 330 |
kAudioObjectSystemObject, |
| 331 |
&address, |
| 332 |
0, |
| 333 |
ptr::null(), |
| 334 |
&mut size as *mut usize as *mut u32, |
| 335 |
devices.as_mut_ptr() as *mut c_void, |
| 336 |
) |
| 337 |
}; |
| 338 |
if status != NO_ERR { |
| 339 |
devices.clear(); |
| 340 |
return devices; |
| 341 |
} |
| 342 |
for device in devices.iter() { |
| 343 |
assert_ne!(*device, kAudioObjectUnknown); |
| 344 |
} |
| 345 |
|
| 346 |
match filter { |
| 347 |
DeviceFilter::ExcludeCubebAggregateAndVPIO => { |
| 348 |
devices.retain(|&device| { |
| 349 |
if let Ok(uid) = get_device_global_uid(device) { |
| 350 |
let uid = uid.into_string(); |
| 351 |
!uid.contains(PRIVATE_AGGREGATE_DEVICE_NAME) |
| 352 |
&& !uid.contains(VOICEPROCESSING_AGGREGATE_DEVICE_NAME) |
| 353 |
} else { |
| 354 |
true |
| 355 |
} |
| 356 |
}); |
| 357 |
} |
| 358 |
DeviceFilter::ExcludeVPIO => { |
| 359 |
devices.retain(|&device| { |
| 360 |
if let Ok(uid) = get_device_global_uid(device) { |
| 361 |
let uid = uid.into_string(); |
| 362 |
!uid.contains(VOICEPROCESSING_AGGREGATE_DEVICE_NAME) |
| 363 |
} else { |
| 364 |
true |
| 365 |
} |
| 366 |
}); |
| 367 |
} |
| 368 |
_ => {} |
| 369 |
} |
| 370 |
|
| 371 |
devices |
| 372 |
}) |
| 373 |
} |
| 374 |
|
| 375 |
pub fn test_get_devices_in_scope(scope: Scope) -> Vec<AudioObjectID> { |
| 376 |
debug_assert_not_running_serially(); |
| 377 |
let mut devices = test_get_all_devices(DeviceFilter::ExcludeCubebAggregateAndVPIO); |
| 378 |
devices.retain(|device| test_device_in_scope(*device, scope.clone())); |
| 379 |
devices |
| 380 |
} |
| 381 |
|
| 382 |
pub fn get_devices_info_in_scope(scope: Scope) -> Vec<TestDeviceInfo> { |
| 383 |
debug_assert_not_running_serially(); |
| 384 |
fn print_info(info: &TestDeviceInfo) { |
| 385 |
println!("{:>4}: {}\n\tuid: {}", info.id, info.label, info.uid); |
| 386 |
} |
| 387 |
|
| 388 |
println!( |
| 389 |
"\n{:?} devices\n\ |
| 390 |
--------------------", |
| 391 |
scope |
| 392 |
); |
| 393 |
|
| 394 |
let mut infos = vec![]; |
| 395 |
let devices = test_get_devices_in_scope(scope.clone()); |
| 396 |
for device in devices { |
| 397 |
infos.push(TestDeviceInfo::new(device, scope.clone())); |
| 398 |
print_info(infos.last().unwrap()); |
| 399 |
} |
| 400 |
println!(); |
| 401 |
|
| 402 |
infos |
| 403 |
} |
| 404 |
|
| 405 |
#[derive(Debug)] |
| 406 |
pub struct TestDeviceInfo { |
| 407 |
pub id: AudioObjectID, |
| 408 |
pub label: String, |
| 409 |
pub uid: String, |
| 410 |
} |
| 411 |
impl TestDeviceInfo { |
| 412 |
pub fn new(id: AudioObjectID, scope: Scope) -> Self { |
| 413 |
Self { |
| 414 |
id, |
| 415 |
label: Self::get_label(id, scope.clone()), |
| 416 |
uid: Self::get_uid(id, scope), |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
fn get_label(id: AudioObjectID, scope: Scope) -> String { |
| 421 |
match run_serially_forward_panics(|| get_device_uid(id, scope.into())) { |
| 422 |
Ok(uid) => uid.into_string(), |
| 423 |
Err(status) => format!("Unknow. Error: {}", status).to_string(), |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
fn get_uid(id: AudioObjectID, scope: Scope) -> String { |
| 428 |
match run_serially_forward_panics(|| get_device_label(id, scope.into())) { |
| 429 |
Ok(label) => label.into_string(), |
| 430 |
Err(status) => format!("Unknown. Error: {}", status).to_string(), |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
pub fn test_device_channels_in_scope( |
| 436 |
id: AudioObjectID, |
| 437 |
scope: Scope, |
| 438 |
) -> std::result::Result<u32, OSStatus> { |
| 439 |
debug_assert_not_running_serially(); |
| 440 |
let address = AudioObjectPropertyAddress { |
| 441 |
mSelector: kAudioDevicePropertyStreams, |
| 442 |
mScope: match scope { |
| 443 |
Scope::Input => kAudioDevicePropertyScopeInput, |
| 444 |
Scope::Output => kAudioDevicePropertyScopeOutput, |
| 445 |
}, |
| 446 |
mElement: kAudioObjectPropertyElementMaster, |
| 447 |
}; |
| 448 |
let mut size: usize = 0; |
| 449 |
let status = run_serially(|| unsafe { |
| 450 |
AudioObjectGetPropertyDataSize( |
| 451 |
id, |
| 452 |
&address, |
| 453 |
0, |
| 454 |
ptr::null(), |
| 455 |
&mut size as *mut usize as *mut u32, |
| 456 |
) |
| 457 |
}); |
| 458 |
if status != NO_ERR { |
| 459 |
return Err(status); |
| 460 |
} |
| 461 |
if size == 0 { |
| 462 |
return Ok(0); |
| 463 |
} |
| 464 |
let mut stream_list = vec![0, (size / mem::size_of::<AudioObjectID>()) as u32]; |
| 465 |
let status = run_serially(|| unsafe { |
| 466 |
AudioObjectGetPropertyData( |
| 467 |
id, |
| 468 |
&address, |
| 469 |
0, |
| 470 |
ptr::null(), |
| 471 |
&mut size as *mut usize as *mut u32, |
| 472 |
stream_list.as_mut_ptr() as *mut c_void, |
| 473 |
) |
| 474 |
}); |
| 475 |
if status != NO_ERR { |
| 476 |
return Err(status); |
| 477 |
} |
| 478 |
let channels = stream_list |
| 479 |
.iter() |
| 480 |
.filter(|s: &&AudioObjectID| { |
| 481 |
if scope != Scope::Input { |
| 482 |
return true; |
| 483 |
} |
| 484 |
let address = AudioObjectPropertyAddress { |
| 485 |
mSelector: kAudioStreamPropertyTerminalType, |
| 486 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 487 |
mElement: kAudioObjectPropertyElementMaster, |
| 488 |
}; |
| 489 |
let mut ttype: u32 = 0; |
| 490 |
let status = unsafe { |
| 491 |
AudioObjectGetPropertyData( |
| 492 |
**s, |
| 493 |
&address, |
| 494 |
0, |
| 495 |
ptr::null(), |
| 496 |
&mut mem::size_of::<u32>() as *mut usize as *mut u32, |
| 497 |
&mut ttype as *mut u32 as *mut c_void, |
| 498 |
) |
| 499 |
}; |
| 500 |
if status != NO_ERR { |
| 501 |
return false; |
| 502 |
} |
| 503 |
ttype == kAudioStreamTerminalTypeMicrophone |
| 504 |
|| (INPUT_MICROPHONE..OUTPUT_UNDEFINED).contains(&ttype) |
| 505 |
}) |
| 506 |
.map(|s: &AudioObjectID| { |
| 507 |
let address = AudioObjectPropertyAddress { |
| 508 |
mSelector: kAudioStreamPropertyVirtualFormat, |
| 509 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 510 |
mElement: kAudioObjectPropertyElementMaster, |
| 511 |
}; |
| 512 |
let mut format = AudioStreamBasicDescription::default(); |
| 513 |
let status = unsafe { |
| 514 |
AudioObjectGetPropertyData( |
| 515 |
*s, |
| 516 |
&address, |
| 517 |
0, |
| 518 |
ptr::null(), |
| 519 |
&mut mem::size_of::<AudioStreamBasicDescription>() as *mut usize as *mut u32, |
| 520 |
&mut format as *mut AudioStreamBasicDescription as *mut c_void, |
| 521 |
) |
| 522 |
}; |
| 523 |
if status != NO_ERR { |
| 524 |
return 0; |
| 525 |
} |
| 526 |
format.mChannelsPerFrame |
| 527 |
}) |
| 528 |
.sum(); |
| 529 |
Ok(channels) |
| 530 |
} |
| 531 |
|
| 532 |
pub fn test_device_in_scope(id: AudioObjectID, scope: Scope) -> bool { |
| 533 |
debug_assert_not_running_serially(); |
| 534 |
let channels = test_device_channels_in_scope(id, scope); |
| 535 |
channels.is_ok() && channels.unwrap() > 0 |
| 536 |
} |
| 537 |
|
| 538 |
pub fn test_get_all_onwed_devices(id: AudioDeviceID) -> Vec<AudioObjectID> { |
| 539 |
assert_ne!(id, kAudioObjectUnknown); |
| 540 |
debug_assert_running_serially(); |
| 541 |
|
| 542 |
let address = AudioObjectPropertyAddress { |
| 543 |
mSelector: kAudioObjectPropertyOwnedObjects, |
| 544 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 545 |
mElement: kAudioObjectPropertyElementMaster, |
| 546 |
}; |
| 547 |
|
| 548 |
let qualifier_data_size = mem::size_of::<AudioObjectID>(); |
| 549 |
let class_id: AudioClassID = kAudioSubDeviceClassID; |
| 550 |
let qualifier_data = &class_id; |
| 551 |
let mut size: usize = 0; |
| 552 |
|
| 553 |
assert_eq!( |
| 554 |
unsafe { |
| 555 |
AudioObjectGetPropertyDataSize( |
| 556 |
id, |
| 557 |
&address, |
| 558 |
qualifier_data_size as u32, |
| 559 |
qualifier_data as *const u32 as *const c_void, |
| 560 |
&mut size as *mut usize as *mut u32, |
| 561 |
) |
| 562 |
}, |
| 563 |
NO_ERR |
| 564 |
); |
| 565 |
assert_ne!(size, 0); |
| 566 |
|
| 567 |
let elements = size / mem::size_of::<AudioObjectID>(); |
| 568 |
let mut devices: Vec<AudioObjectID> = allocate_array(elements); |
| 569 |
|
| 570 |
assert_eq!( |
| 571 |
unsafe { |
| 572 |
AudioObjectGetPropertyData( |
| 573 |
id, |
| 574 |
&address, |
| 575 |
qualifier_data_size as u32, |
| 576 |
qualifier_data as *const u32 as *const c_void, |
| 577 |
&mut size as *mut usize as *mut u32, |
| 578 |
devices.as_mut_ptr() as *mut c_void, |
| 579 |
) |
| 580 |
}, |
| 581 |
NO_ERR |
| 582 |
); |
| 583 |
|
| 584 |
devices |
| 585 |
} |
| 586 |
|
| 587 |
pub fn test_get_master_device(id: AudioObjectID) -> String { |
| 588 |
assert_ne!(id, kAudioObjectUnknown); |
| 589 |
debug_assert_running_serially(); |
| 590 |
|
| 591 |
let address = AudioObjectPropertyAddress { |
| 592 |
mSelector: kAudioAggregateDevicePropertyMainSubDevice, |
| 593 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 594 |
mElement: kAudioObjectPropertyElementMaster, |
| 595 |
}; |
| 596 |
|
| 597 |
let mut master: CFStringRef = ptr::null_mut(); |
| 598 |
let mut size = mem::size_of::<CFStringRef>(); |
| 599 |
assert_eq!( |
| 600 |
audio_object_get_property_data(id, &address, &mut size, &mut master), |
| 601 |
NO_ERR |
| 602 |
); |
| 603 |
assert!(!master.is_null()); |
| 604 |
|
| 605 |
let master = StringRef::new(master as _); |
| 606 |
master.into_string() |
| 607 |
} |
| 608 |
|
| 609 |
pub fn test_get_drift_compensations(id: AudioObjectID) -> std::result::Result<u32, OSStatus> { |
| 610 |
debug_assert_running_serially(); |
| 611 |
let address = AudioObjectPropertyAddress { |
| 612 |
mSelector: kAudioSubDevicePropertyDriftCompensation, |
| 613 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 614 |
mElement: kAudioObjectPropertyElementMaster, |
| 615 |
}; |
| 616 |
let mut size = mem::size_of::<u32>(); |
| 617 |
let mut compensation = u32::max_value(); |
| 618 |
let status = unsafe { |
| 619 |
AudioObjectGetPropertyData( |
| 620 |
id, |
| 621 |
&address, |
| 622 |
0, |
| 623 |
ptr::null(), |
| 624 |
&mut size as *mut usize as *mut u32, |
| 625 |
&mut compensation as *mut u32 as *mut c_void, |
| 626 |
) |
| 627 |
}; |
| 628 |
if status == NO_ERR { |
| 629 |
Ok(compensation) |
| 630 |
} else { |
| 631 |
Err(status) |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
pub fn test_audiounit_scope_is_enabled(unit: AudioUnit, scope: Scope) -> bool { |
| 636 |
assert!(!unit.is_null()); |
| 637 |
debug_assert_not_running_serially(); |
| 638 |
let mut has_io: UInt32 = 0; |
| 639 |
let (scope, element) = match scope { |
| 640 |
Scope::Input => (kAudioUnitScope_Input, AU_IN_BUS), |
| 641 |
Scope::Output => (kAudioUnitScope_Output, AU_OUT_BUS), |
| 642 |
}; |
| 643 |
let mut size = mem::size_of::<UInt32>(); |
| 644 |
assert_eq!( |
| 645 |
run_serially(|| audio_unit_get_property( |
| 646 |
unit, |
| 647 |
kAudioOutputUnitProperty_HasIO, |
| 648 |
scope, |
| 649 |
element, |
| 650 |
&mut has_io, |
| 651 |
&mut size |
| 652 |
)), |
| 653 |
NO_ERR |
| 654 |
); |
| 655 |
has_io != 0 |
| 656 |
} |
| 657 |
|
| 658 |
pub fn test_audiounit_get_buffer_frame_size( |
| 659 |
unit: AudioUnit, |
| 660 |
scope: Scope, |
| 661 |
prop_scope: PropertyScope, |
| 662 |
) -> std::result::Result<u32, OSStatus> { |
| 663 |
debug_assert_not_running_serially(); |
| 664 |
let element = match scope { |
| 665 |
Scope::Input => AU_IN_BUS, |
| 666 |
Scope::Output => AU_OUT_BUS, |
| 667 |
}; |
| 668 |
let prop_scope = match prop_scope { |
| 669 |
PropertyScope::Input => kAudioUnitScope_Input, |
| 670 |
PropertyScope::Output => kAudioUnitScope_Output, |
| 671 |
}; |
| 672 |
let mut buffer_frames: u32 = 0; |
| 673 |
let mut size = mem::size_of::<u32>(); |
| 674 |
let status = run_serially(|| unsafe { |
| 675 |
AudioUnitGetProperty( |
| 676 |
unit, |
| 677 |
kAudioDevicePropertyBufferFrameSize, |
| 678 |
prop_scope, |
| 679 |
element, |
| 680 |
&mut buffer_frames as *mut u32 as *mut c_void, |
| 681 |
&mut size as *mut usize as *mut u32, |
| 682 |
) |
| 683 |
}); |
| 684 |
if status == NO_ERR { |
| 685 |
Ok(buffer_frames) |
| 686 |
} else { |
| 687 |
Err(status) |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
// Surprisingly it's ok to set |
| 692 |
// 1. a unknown device |
| 693 |
// 2. a non-input/non-output device |
| 694 |
// 3. the current default input/output device |
| 695 |
// as the new default input/output device by apple's API. We need to check the above things by ourselves. |
| 696 |
// This function returns an Ok containing the previous default device id on success. |
| 697 |
// Otherwise, it returns an Err containing the error code with OSStatus type |
| 698 |
pub fn test_set_default_device( |
| 699 |
device: AudioObjectID, |
| 700 |
scope: Scope, |
| 701 |
) -> std::result::Result<AudioObjectID, OSStatus> { |
| 702 |
debug_assert_not_running_serially(); |
| 703 |
assert!(test_device_in_scope(device, scope.clone())); |
| 704 |
let default = test_get_default_device(scope.clone()).unwrap(); |
| 705 |
if default == device { |
| 706 |
// Do nothing if device is already the default device |
| 707 |
return Ok(device); |
| 708 |
} |
| 709 |
|
| 710 |
let address = AudioObjectPropertyAddress { |
| 711 |
mSelector: match scope { |
| 712 |
Scope::Input => kAudioHardwarePropertyDefaultInputDevice, |
| 713 |
Scope::Output => kAudioHardwarePropertyDefaultOutputDevice, |
| 714 |
}, |
| 715 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 716 |
mElement: kAudioObjectPropertyElementMaster, |
| 717 |
}; |
| 718 |
let size = mem::size_of::<AudioObjectID>(); |
| 719 |
let status = run_serially(|| unsafe { |
| 720 |
AudioObjectSetPropertyData( |
| 721 |
kAudioObjectSystemObject, |
| 722 |
&address, |
| 723 |
0, |
| 724 |
ptr::null(), |
| 725 |
size as u32, |
| 726 |
&device as *const AudioObjectID as *const c_void, |
| 727 |
) |
| 728 |
}); |
| 729 |
let new_default = test_get_default_device(scope.clone()).unwrap(); |
| 730 |
if new_default == default { |
| 731 |
Err(-1) |
| 732 |
} else if status == NO_ERR { |
| 733 |
Ok(default) |
| 734 |
} else { |
| 735 |
Err(status) |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
pub struct TestDeviceSwitcher { |
| 740 |
scope: Scope, |
| 741 |
devices: Vec<AudioObjectID>, |
| 742 |
current_device_index: usize, |
| 743 |
} |
| 744 |
|
| 745 |
impl TestDeviceSwitcher { |
| 746 |
pub fn new(scope: Scope) -> Self { |
| 747 |
let infos = get_devices_info_in_scope(scope.clone()); |
| 748 |
let devices: Vec<AudioObjectID> = infos.into_iter().map(|info| info.id).collect(); |
| 749 |
let current = test_get_default_device(scope.clone()).unwrap(); |
| 750 |
let index = devices |
| 751 |
.iter() |
| 752 |
.position(|device| *device == current) |
| 753 |
.unwrap(); |
| 754 |
Self { |
| 755 |
scope, |
| 756 |
devices, |
| 757 |
current_device_index: index, |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
pub fn next(&mut self) { |
| 762 |
let current = self.devices[self.current_device_index]; |
| 763 |
let next_index = (self.current_device_index + 1) % self.devices.len(); |
| 764 |
let next = self.devices[next_index]; |
| 765 |
println!( |
| 766 |
"Switch device for {:?}: {} -> {}", |
| 767 |
self.scope, current, next |
| 768 |
); |
| 769 |
match self.set_device(next) { |
| 770 |
Ok(prev) => { |
| 771 |
assert_eq!(prev, current); |
| 772 |
self.current_device_index = next_index; |
| 773 |
} |
| 774 |
_ => { |
| 775 |
self.devices.remove(next_index); |
| 776 |
if next_index < self.current_device_index { |
| 777 |
self.current_device_index -= 1; |
| 778 |
} |
| 779 |
self.next(); |
| 780 |
} |
| 781 |
} |
| 782 |
} |
| 783 |
pub fn current(&self) -> AudioObjectID { |
| 784 |
self.devices[self.current_device_index] |
| 785 |
} |
| 786 |
|
| 787 |
fn set_device(&self, device: AudioObjectID) -> std::result::Result<AudioObjectID, OSStatus> { |
| 788 |
test_set_default_device(device, self.scope.clone()) |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
pub fn test_create_device_change_listener<F>(scope: Scope, listener: F) -> TestPropertyListener<F> |
| 793 |
where |
| 794 |
F: Fn(&[AudioObjectPropertyAddress]) -> OSStatus, |
| 795 |
{ |
| 796 |
debug_assert_running_serially(); |
| 797 |
let address = AudioObjectPropertyAddress { |
| 798 |
mSelector: match scope { |
| 799 |
Scope::Input => kAudioHardwarePropertyDefaultInputDevice, |
| 800 |
Scope::Output => kAudioHardwarePropertyDefaultOutputDevice, |
| 801 |
}, |
| 802 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 803 |
mElement: kAudioObjectPropertyElementMaster, |
| 804 |
}; |
| 805 |
|
| 806 |
TestPropertyListener::new(kAudioObjectSystemObject, address, listener) |
| 807 |
} |
| 808 |
|
| 809 |
pub struct TestPropertyListener<F> |
| 810 |
where |
| 811 |
F: Fn(&[AudioObjectPropertyAddress]) -> OSStatus, |
| 812 |
{ |
| 813 |
device: AudioObjectID, |
| 814 |
property: AudioObjectPropertyAddress, |
| 815 |
callback: F, |
| 816 |
} |
| 817 |
|
| 818 |
impl<F> TestPropertyListener<F> |
| 819 |
where |
| 820 |
F: Fn(&[AudioObjectPropertyAddress]) -> OSStatus, |
| 821 |
{ |
| 822 |
pub fn new(device: AudioObjectID, property: AudioObjectPropertyAddress, callback: F) -> Self { |
| 823 |
Self { |
| 824 |
device, |
| 825 |
property, |
| 826 |
callback, |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
pub fn start(&self) -> std::result::Result<(), OSStatus> { |
| 831 |
debug_assert_running_serially(); |
| 832 |
let status = unsafe { |
| 833 |
AudioObjectAddPropertyListener( |
| 834 |
self.device, |
| 835 |
&self.property, |
| 836 |
Some(Self::render), |
| 837 |
self as *const Self as *mut c_void, |
| 838 |
) |
| 839 |
}; |
| 840 |
if status == NO_ERR { |
| 841 |
Ok(()) |
| 842 |
} else { |
| 843 |
Err(status) |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
pub fn stop(&self) -> std::result::Result<(), OSStatus> { |
| 848 |
debug_assert_running_serially(); |
| 849 |
let status = unsafe { |
| 850 |
AudioObjectRemovePropertyListener( |
| 851 |
self.device, |
| 852 |
&self.property, |
| 853 |
Some(Self::render), |
| 854 |
self as *const Self as *mut c_void, |
| 855 |
) |
| 856 |
}; |
| 857 |
if status == NO_ERR { |
| 858 |
Ok(()) |
| 859 |
} else { |
| 860 |
Err(status) |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
extern "C" fn render( |
| 865 |
id: AudioObjectID, |
| 866 |
number_of_addresses: u32, |
| 867 |
addresses: *const AudioObjectPropertyAddress, |
| 868 |
data: *mut c_void, |
| 869 |
) -> OSStatus { |
| 870 |
let listener = unsafe { &*(data as *mut Self) }; |
| 871 |
assert_eq!(id, listener.device); |
| 872 |
let addrs = unsafe { slice::from_raw_parts(addresses, number_of_addresses as usize) }; |
| 873 |
(listener.callback)(addrs) |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
impl<F> Drop for TestPropertyListener<F> |
| 878 |
where |
| 879 |
F: Fn(&[AudioObjectPropertyAddress]) -> OSStatus, |
| 880 |
{ |
| 881 |
fn drop(&mut self) { |
| 882 |
run_serially(|| self.stop()); |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
// TODO: It doesn't work if default input or output is an aggregate device! Probably we need to do |
| 887 |
// the same thing as what audiounit_set_aggregate_sub_device_list does. |
| 888 |
#[derive(Debug)] |
| 889 |
pub struct TestDevicePlugger { |
| 890 |
scope: Scope, |
| 891 |
plugin_id: AudioObjectID, |
| 892 |
device_id: AudioObjectID, |
| 893 |
} |
| 894 |
|
| 895 |
impl TestDevicePlugger { |
| 896 |
pub fn new(scope: Scope) -> std::result::Result<Self, OSStatus> { |
| 897 |
let plugin_id = Self::get_system_plugin_id()?; |
| 898 |
Ok(Self { |
| 899 |
scope, |
| 900 |
plugin_id, |
| 901 |
device_id: kAudioObjectUnknown, |
| 902 |
}) |
| 903 |
} |
| 904 |
|
| 905 |
pub fn get_device_id(&self) -> AudioObjectID { |
| 906 |
self.device_id |
| 907 |
} |
| 908 |
|
| 909 |
pub fn plug(&mut self) -> std::result::Result<(), OSStatus> { |
| 910 |
self.device_id = self.create_aggregate_device()?; |
| 911 |
Ok(()) |
| 912 |
} |
| 913 |
|
| 914 |
pub fn unplug(&mut self) -> std::result::Result<(), OSStatus> { |
| 915 |
self.destroy_aggregate_device() |
| 916 |
} |
| 917 |
|
| 918 |
fn is_plugging(&self) -> bool { |
| 919 |
self.device_id != kAudioObjectUnknown |
| 920 |
} |
| 921 |
|
| 922 |
fn destroy_aggregate_device(&mut self) -> std::result::Result<(), OSStatus> { |
| 923 |
debug_assert_not_running_serially(); |
| 924 |
assert_ne!(self.plugin_id, kAudioObjectUnknown); |
| 925 |
assert_ne!(self.device_id, kAudioObjectUnknown); |
| 926 |
|
| 927 |
let address = AudioObjectPropertyAddress { |
| 928 |
mSelector: kAudioPlugInDestroyAggregateDevice, |
| 929 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 930 |
mElement: kAudioObjectPropertyElementMaster, |
| 931 |
}; |
| 932 |
|
| 933 |
let mut size: usize = 0; |
| 934 |
let status = run_serially(|| unsafe { |
| 935 |
AudioObjectGetPropertyDataSize( |
| 936 |
self.plugin_id, |
| 937 |
&address, |
| 938 |
0, |
| 939 |
ptr::null(), |
| 940 |
&mut size as *mut usize as *mut u32, |
| 941 |
) |
| 942 |
}); |
| 943 |
if status != NO_ERR { |
| 944 |
return Err(status); |
| 945 |
} |
| 946 |
assert_ne!(size, 0); |
| 947 |
|
| 948 |
let status = run_serially(|| unsafe { |
| 949 |
// This call can simulate removing a device. |
| 950 |
AudioObjectGetPropertyData( |
| 951 |
self.plugin_id, |
| 952 |
&address, |
| 953 |
0, |
| 954 |
ptr::null(), |
| 955 |
&mut size as *mut usize as *mut u32, |
| 956 |
&mut self.device_id as *mut AudioDeviceID as *mut c_void, |
| 957 |
) |
| 958 |
}); |
| 959 |
if status == NO_ERR { |
| 960 |
self.device_id = kAudioObjectUnknown; |
| 961 |
Ok(()) |
| 962 |
} else { |
| 963 |
Err(status) |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
fn create_aggregate_device(&self) -> std::result::Result<AudioObjectID, OSStatus> { |
| 968 |
debug_assert_not_running_serially(); |
| 969 |
use std::time::{SystemTime, UNIX_EPOCH}; |
| 970 |
|
| 971 |
const TEST_AGGREGATE_DEVICE_NAME: &str = "TestAggregateDevice"; |
| 972 |
|
| 973 |
assert_ne!(self.plugin_id, kAudioObjectUnknown); |
| 974 |
|
| 975 |
let sub_devices = Self::get_sub_devices(self.scope.clone()); |
| 976 |
if sub_devices.is_none() { |
| 977 |
return Err(kAudioCodecUnspecifiedError as OSStatus); |
| 978 |
} |
| 979 |
let sub_devices = sub_devices.unwrap(); |
| 980 |
|
| 981 |
let address = AudioObjectPropertyAddress { |
| 982 |
mSelector: kAudioPlugInCreateAggregateDevice, |
| 983 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 984 |
mElement: kAudioObjectPropertyElementMaster, |
| 985 |
}; |
| 986 |
|
| 987 |
let mut size: usize = 0; |
| 988 |
let status = run_serially(|| unsafe { |
| 989 |
AudioObjectGetPropertyDataSize( |
| 990 |
self.plugin_id, |
| 991 |
&address, |
| 992 |
0, |
| 993 |
ptr::null(), |
| 994 |
&mut size as *mut usize as *mut u32, |
| 995 |
) |
| 996 |
}); |
| 997 |
if status != NO_ERR { |
| 998 |
return Err(status); |
| 999 |
} |
| 1000 |
assert_ne!(size, 0); |
| 1001 |
|
| 1002 |
let sys_time = SystemTime::now(); |
| 1003 |
let time_id = sys_time.duration_since(UNIX_EPOCH).unwrap().as_nanos(); |
| 1004 |
let device_name = format!("{}_{}", TEST_AGGREGATE_DEVICE_NAME, time_id); |
| 1005 |
let device_uid = format!("org.mozilla.{}", device_name); |
| 1006 |
|
| 1007 |
let mut device_id = kAudioObjectUnknown; |
| 1008 |
let status = unsafe { |
| 1009 |
let device_dict = CFDictionaryCreateMutable( |
| 1010 |
kCFAllocatorDefault, |
| 1011 |
0, |
| 1012 |
&kCFTypeDictionaryKeyCallBacks, |
| 1013 |
&kCFTypeDictionaryValueCallBacks, |
| 1014 |
); |
| 1015 |
|
| 1016 |
// Set the name of this device. |
| 1017 |
let device_name = cfstringref_from_string(&device_name); |
| 1018 |
CFDictionaryAddValue( |
| 1019 |
device_dict, |
| 1020 |
cfstringref_from_static_string(AGGREGATE_DEVICE_NAME_KEY) as *const c_void, |
| 1021 |
device_name as *const c_void, |
| 1022 |
); |
| 1023 |
CFRelease(device_name as *const c_void); |
| 1024 |
|
| 1025 |
// Set the uid of this device. |
| 1026 |
let device_uid = cfstringref_from_string(&device_uid); |
| 1027 |
CFDictionaryAddValue( |
| 1028 |
device_dict, |
| 1029 |
cfstringref_from_static_string(AGGREGATE_DEVICE_UID_KEY) as *const c_void, |
| 1030 |
device_uid as *const c_void, |
| 1031 |
); |
| 1032 |
CFRelease(device_uid as *const c_void); |
| 1033 |
|
| 1034 |
// Make this device NOT private to the process creating it. |
| 1035 |
// On MacOS 14 devicechange events are not triggered when it is private. |
| 1036 |
let private_value: i32 = 0; |
| 1037 |
let device_private_key = CFNumberCreate( |
| 1038 |
kCFAllocatorDefault, |
| 1039 |
i64::from(kCFNumberIntType), |
| 1040 |
&private_value as *const i32 as *const c_void, |
| 1041 |
); |
| 1042 |
CFDictionaryAddValue( |
| 1043 |
device_dict, |
| 1044 |
cfstringref_from_static_string(AGGREGATE_DEVICE_PRIVATE_KEY) as *const c_void, |
| 1045 |
device_private_key as *const c_void, |
| 1046 |
); |
| 1047 |
CFRelease(device_private_key as *const c_void); |
| 1048 |
|
| 1049 |
// Set this device to be a stacked aggregate (i.e. multi-output device). |
| 1050 |
let stacked_value: i32 = 0; // 1 for normal aggregate device. |
| 1051 |
let device_stacked_key = CFNumberCreate( |
| 1052 |
kCFAllocatorDefault, |
| 1053 |
i64::from(kCFNumberIntType), |
| 1054 |
&stacked_value as *const i32 as *const c_void, |
| 1055 |
); |
| 1056 |
CFDictionaryAddValue( |
| 1057 |
device_dict, |
| 1058 |
cfstringref_from_static_string(AGGREGATE_DEVICE_STACKED_KEY) as *const c_void, |
| 1059 |
device_stacked_key as *const c_void, |
| 1060 |
); |
| 1061 |
CFRelease(device_stacked_key as *const c_void); |
| 1062 |
|
| 1063 |
// Set sub devices for this device. |
| 1064 |
CFDictionaryAddValue( |
| 1065 |
device_dict, |
| 1066 |
cfstringref_from_static_string(AGGREGATE_DEVICE_SUB_DEVICE_LIST_KEY) |
| 1067 |
as *const c_void, |
| 1068 |
sub_devices as *const c_void, |
| 1069 |
); |
| 1070 |
CFRelease(sub_devices as *const c_void); |
| 1071 |
|
| 1072 |
// This call can simulate adding a device. |
| 1073 |
let status = { |
| 1074 |
run_serially(|| { |
| 1075 |
AudioObjectGetPropertyData( |
| 1076 |
self.plugin_id, |
| 1077 |
&address, |
| 1078 |
mem::size_of_val(&device_dict) as u32, |
| 1079 |
&device_dict as *const CFMutableDictionaryRef as *const c_void, |
| 1080 |
&mut size as *mut usize as *mut u32, |
| 1081 |
&mut device_id as *mut AudioDeviceID as *mut c_void, |
| 1082 |
) |
| 1083 |
}) |
| 1084 |
}; |
| 1085 |
CFRelease(device_dict as *const c_void); |
| 1086 |
status |
| 1087 |
}; |
| 1088 |
if status == NO_ERR { |
| 1089 |
assert_ne!(device_id, kAudioObjectUnknown); |
| 1090 |
Ok(device_id) |
| 1091 |
} else { |
| 1092 |
Err(status) |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
fn get_system_plugin_id() -> std::result::Result<AudioObjectID, OSStatus> { |
| 1097 |
debug_assert_not_running_serially(); |
| 1098 |
let address = AudioObjectPropertyAddress { |
| 1099 |
mSelector: kAudioHardwarePropertyPlugInForBundleID, |
| 1100 |
mScope: kAudioObjectPropertyScopeGlobal, |
| 1101 |
mElement: kAudioObjectPropertyElementMaster, |
| 1102 |
}; |
| 1103 |
|
| 1104 |
let mut size: usize = 0; |
| 1105 |
let status = run_serially(|| unsafe { |
| 1106 |
AudioObjectGetPropertyDataSize( |
| 1107 |
kAudioObjectSystemObject, |
| 1108 |
&address, |
| 1109 |
0, |
| 1110 |
ptr::null(), |
| 1111 |
&mut size as *mut usize as *mut u32, |
| 1112 |
) |
| 1113 |
}); |
| 1114 |
if status != NO_ERR { |
| 1115 |
return Err(status); |
| 1116 |
} |
| 1117 |
assert_ne!(size, 0); |
| 1118 |
|
| 1119 |
let mut plugin_id = kAudioObjectUnknown; |
| 1120 |
let mut in_bundle_ref = cfstringref_from_static_string("com.apple.audio.CoreAudio"); |
| 1121 |
let mut translation_value = AudioValueTranslation { |
| 1122 |
mInputData: &mut in_bundle_ref as *mut CFStringRef as *mut c_void, |
| 1123 |
mInputDataSize: mem::size_of::<CFStringRef>() as u32, |
| 1124 |
mOutputData: &mut plugin_id as *mut AudioObjectID as *mut c_void, |
| 1125 |
mOutputDataSize: mem::size_of::<AudioObjectID>() as u32, |
| 1126 |
}; |
| 1127 |
assert_eq!(size, mem::size_of_val(&translation_value)); |
| 1128 |
|
| 1129 |
let status = unsafe { |
| 1130 |
let status = run_serially(|| { |
| 1131 |
AudioObjectGetPropertyData( |
| 1132 |
kAudioObjectSystemObject, |
| 1133 |
&address, |
| 1134 |
0, |
| 1135 |
ptr::null(), |
| 1136 |
&mut size as *mut usize as *mut u32, |
| 1137 |
&mut translation_value as *mut AudioValueTranslation as *mut c_void, |
| 1138 |
) |
| 1139 |
}); |
| 1140 |
CFRelease(in_bundle_ref as *const c_void); |
| 1141 |
status |
| 1142 |
}; |
| 1143 |
if status == NO_ERR { |
| 1144 |
assert_ne!(plugin_id, kAudioObjectUnknown); |
| 1145 |
Ok(plugin_id) |
| 1146 |
} else { |
| 1147 |
Err(status) |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
// TODO: This doesn't work as what we expect when the default deivce in the scope is an |
| 1152 |
// aggregate device. We should get the list of all the active sub devices and put |
| 1153 |
// them into the array, if the device is an aggregate device. See the code in |
| 1154 |
// AggregateDevice::get_sub_devices and audiounit_set_aggregate_sub_device_list. |
| 1155 |
fn get_sub_devices(scope: Scope) -> Option<CFArrayRef> { |
| 1156 |
debug_assert_not_running_serially(); |
| 1157 |
let device = test_get_default_device(scope); |
| 1158 |
device?; |
| 1159 |
let device = device.unwrap(); |
| 1160 |
let uid = run_serially(|| get_device_global_uid(device)); |
| 1161 |
if uid.is_err() { |
| 1162 |
return None; |
| 1163 |
} |
| 1164 |
let uid = uid.unwrap(); |
| 1165 |
unsafe { |
| 1166 |
let list = CFArrayCreateMutable(ptr::null(), 0, &kCFTypeArrayCallBacks); |
| 1167 |
let sub_device_dict = CFDictionaryCreateMutable( |
| 1168 |
ptr::null(), |
| 1169 |
0, |
| 1170 |
&kCFTypeDictionaryKeyCallBacks, |
| 1171 |
&kCFTypeDictionaryValueCallBacks, |
| 1172 |
); |
| 1173 |
CFDictionaryAddValue( |
| 1174 |
sub_device_dict, |
| 1175 |
cfstringref_from_static_string(SUB_DEVICE_UID_KEY) as *const c_void, |
| 1176 |
uid.get_raw() as *const c_void, |
| 1177 |
); |
| 1178 |
CFArrayAppendValue(list, sub_device_dict as *const c_void); |
| 1179 |
CFRelease(sub_device_dict as *const c_void); |
| 1180 |
Some(list) |
| 1181 |
} |
| 1182 |
} |
| 1183 |
} |
| 1184 |
|
| 1185 |
impl Drop for TestDevicePlugger { |
| 1186 |
fn drop(&mut self) { |
| 1187 |
if self.is_plugging() { |
| 1188 |
self.unplug(); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
// Test Templates |
| 1194 |
// ------------------------------------------------------------------------------------------------ |
| 1195 |
pub fn test_ops_context_operation<F>(name: &'static str, operation: F) |
| 1196 |
where |
| 1197 |
F: FnOnce(*mut ffi::cubeb), |
| 1198 |
{ |
| 1199 |
debug_assert_not_running_serially(); |
| 1200 |
let name_c_string = CString::new(name).expect("Failed to create context name"); |
| 1201 |
let mut context = ptr::null_mut::<ffi::cubeb>(); |
| 1202 |
assert_eq!( |
| 1203 |
unsafe { OPS.init.unwrap()(&mut context, name_c_string.as_ptr()) }, |
| 1204 |
ffi::CUBEB_OK |
| 1205 |
); |
| 1206 |
assert!(!context.is_null()); |
| 1207 |
operation(context); |
| 1208 |
unsafe { OPS.destroy.unwrap()(context) } |
| 1209 |
} |
| 1210 |
|
| 1211 |
// The in-out stream initializeed with different device will create an aggregate_device and |
| 1212 |
// result in firing device-collection-changed callbacks. Run in-out streams with tests |
| 1213 |
// capturing device-collection-changed callbacks may cause troubles. |
| 1214 |
pub fn test_ops_stream_operation_on_context<F>( |
| 1215 |
name: &'static str, |
| 1216 |
context_ptr: *mut ffi::cubeb, |
| 1217 |
input_device: ffi::cubeb_devid, |
| 1218 |
input_stream_params: *mut ffi::cubeb_stream_params, |
| 1219 |
output_device: ffi::cubeb_devid, |
| 1220 |
output_stream_params: *mut ffi::cubeb_stream_params, |
| 1221 |
latency_frames: u32, |
| 1222 |
data_callback: ffi::cubeb_data_callback, |
| 1223 |
state_callback: ffi::cubeb_state_callback, |
| 1224 |
user_ptr: *mut c_void, |
| 1225 |
operation: F, |
| 1226 |
) where |
| 1227 |
F: FnOnce(*mut ffi::cubeb_stream), |
| 1228 |
{ |
| 1229 |
// Do nothing if there is no input/output device to perform input/output tests. |
| 1230 |
if !input_stream_params.is_null() && test_get_default_device(Scope::Input).is_none() { |
| 1231 |
println!("No input device to perform input tests for \"{}\".", name); |
| 1232 |
return; |
| 1233 |
} |
| 1234 |
|
| 1235 |
if !output_stream_params.is_null() && test_get_default_device(Scope::Output).is_none() { |
| 1236 |
println!("No output device to perform output tests for \"{}\".", name); |
| 1237 |
return; |
| 1238 |
} |
| 1239 |
|
| 1240 |
let mut stream: *mut ffi::cubeb_stream = ptr::null_mut(); |
| 1241 |
let stream_name = CString::new(name).expect("Failed to create stream name"); |
| 1242 |
assert_eq!( |
| 1243 |
unsafe { |
| 1244 |
OPS.stream_init.unwrap()( |
| 1245 |
context_ptr, |
| 1246 |
&mut stream, |
| 1247 |
stream_name.as_ptr(), |
| 1248 |
input_device, |
| 1249 |
input_stream_params, |
| 1250 |
output_device, |
| 1251 |
output_stream_params, |
| 1252 |
latency_frames, |
| 1253 |
data_callback, |
| 1254 |
state_callback, |
| 1255 |
user_ptr, |
| 1256 |
) |
| 1257 |
}, |
| 1258 |
ffi::CUBEB_OK |
| 1259 |
); |
| 1260 |
assert!(!stream.is_null()); |
| 1261 |
operation(stream); |
| 1262 |
unsafe { |
| 1263 |
OPS.stream_destroy.unwrap()(stream); |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
pub fn test_ops_stream_operation<F>( |
| 1268 |
name: &'static str, |
| 1269 |
input_device: ffi::cubeb_devid, |
| 1270 |
input_stream_params: *mut ffi::cubeb_stream_params, |
| 1271 |
output_device: ffi::cubeb_devid, |
| 1272 |
output_stream_params: *mut ffi::cubeb_stream_params, |
| 1273 |
latency_frames: u32, |
| 1274 |
data_callback: ffi::cubeb_data_callback, |
| 1275 |
state_callback: ffi::cubeb_state_callback, |
| 1276 |
user_ptr: *mut c_void, |
| 1277 |
operation: F, |
| 1278 |
) where |
| 1279 |
F: FnOnce(*mut ffi::cubeb_stream), |
| 1280 |
{ |
| 1281 |
test_ops_context_operation("context: stream operation", |context_ptr| { |
| 1282 |
test_ops_stream_operation_on_context( |
| 1283 |
name, |
| 1284 |
context_ptr, |
| 1285 |
input_device, |
| 1286 |
input_stream_params, |
| 1287 |
output_device, |
| 1288 |
output_stream_params, |
| 1289 |
latency_frames, |
| 1290 |
data_callback, |
| 1291 |
state_callback, |
| 1292 |
user_ptr, |
| 1293 |
operation, |
| 1294 |
); |
| 1295 |
}); |
| 1296 |
} |
| 1297 |
|
| 1298 |
pub fn test_get_raw_context<F>(operation: F) |
| 1299 |
where |
| 1300 |
F: FnOnce(&mut AudioUnitContext), |
| 1301 |
{ |
| 1302 |
let mut context = AudioUnitContext::new(); |
| 1303 |
operation(&mut context); |
| 1304 |
} |
| 1305 |
|
| 1306 |
pub fn test_get_default_raw_stream<F>(operation: F) |
| 1307 |
where |
| 1308 |
F: FnOnce(&mut AudioUnitStream), |
| 1309 |
{ |
| 1310 |
test_get_raw_stream(ptr::null_mut(), None, None, 0, operation); |
| 1311 |
} |
| 1312 |
|
| 1313 |
fn test_get_raw_stream<F>( |
| 1314 |
user_ptr: *mut c_void, |
| 1315 |
data_callback: ffi::cubeb_data_callback, |
| 1316 |
state_callback: ffi::cubeb_state_callback, |
| 1317 |
latency_frames: u32, |
| 1318 |
operation: F, |
| 1319 |
) where |
| 1320 |
F: FnOnce(&mut AudioUnitStream), |
| 1321 |
{ |
| 1322 |
let mut context = AudioUnitContext::new(); |
| 1323 |
|
| 1324 |
// Add a stream to the context since we are about to create one. |
| 1325 |
// AudioUnitStream::drop() will check the context has at least one stream. |
| 1326 |
let global_latency_frames = context.update_latency_by_adding_stream(latency_frames); |
| 1327 |
|
| 1328 |
let mut stream = AudioUnitStream::new( |
| 1329 |
&mut context, |
| 1330 |
user_ptr, |
| 1331 |
data_callback, |
| 1332 |
state_callback, |
| 1333 |
global_latency_frames, |
| 1334 |
); |
| 1335 |
stream.core_stream_data = CoreStreamData::new(&stream, None, None); |
| 1336 |
|
| 1337 |
operation(&mut stream); |
| 1338 |
} |
| 1339 |
|
| 1340 |
pub fn test_get_stream_with_default_data_callback_by_type<F>( |
| 1341 |
name: &'static str, |
| 1342 |
stm_type: StreamType, |
| 1343 |
input_device: Option<AudioObjectID>, |
| 1344 |
output_device: Option<AudioObjectID>, |
| 1345 |
state_callback: extern "C" fn(*mut ffi::cubeb_stream, *mut c_void, ffi::cubeb_state), |
| 1346 |
data: *mut c_void, |
| 1347 |
operation: F, |
| 1348 |
) where |
| 1349 |
F: FnOnce(&mut AudioUnitStream), |
| 1350 |
{ |
| 1351 |
debug_assert_not_running_serially(); |
| 1352 |
let mut input_params = get_dummy_stream_params(Scope::Input); |
| 1353 |
let mut output_params = get_dummy_stream_params(Scope::Output); |
| 1354 |
|
| 1355 |
let in_params = if stm_type.contains(StreamType::INPUT) { |
| 1356 |
&mut input_params as *mut ffi::cubeb_stream_params |
| 1357 |
} else { |
| 1358 |
ptr::null_mut() |
| 1359 |
}; |
| 1360 |
let out_params = if stm_type.contains(StreamType::OUTPUT) { |
| 1361 |
&mut output_params as *mut ffi::cubeb_stream_params |
| 1362 |
} else { |
| 1363 |
ptr::null_mut() |
| 1364 |
}; |
| 1365 |
let in_device = if let Some(id) = input_device { |
| 1366 |
id as ffi::cubeb_devid |
| 1367 |
} else { |
| 1368 |
ptr::null_mut() |
| 1369 |
}; |
| 1370 |
let out_device = if let Some(id) = output_device { |
| 1371 |
id as ffi::cubeb_devid |
| 1372 |
} else { |
| 1373 |
ptr::null_mut() |
| 1374 |
}; |
| 1375 |
|
| 1376 |
test_ops_stream_operation_with_default_data_callback( |
| 1377 |
name, |
| 1378 |
in_device, |
| 1379 |
in_params, |
| 1380 |
out_device, |
| 1381 |
out_params, |
| 1382 |
state_callback, |
| 1383 |
data, |
| 1384 |
|stream| { |
| 1385 |
let stm = unsafe { &mut *(stream as *mut AudioUnitStream) }; |
| 1386 |
operation(stm); |
| 1387 |
}, |
| 1388 |
); |
| 1389 |
} |
| 1390 |
|
| 1391 |
bitflags! { |
| 1392 |
pub struct StreamType: u8 { |
| 1393 |
const INPUT = 0x01; |
| 1394 |
const OUTPUT = 0x02; |
| 1395 |
const DUPLEX = 0x03; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
|
| 1399 |
fn get_dummy_stream_params(scope: Scope) -> ffi::cubeb_stream_params { |
| 1400 |
// The stream format for input and output must be same. |
| 1401 |
const STREAM_FORMAT: u32 = ffi::CUBEB_SAMPLE_FLOAT32NE; |
| 1402 |
|
| 1403 |
// Make sure the parameters meet the requirements of AudioUnitContext::stream_init |
| 1404 |
// (in the comments). |
| 1405 |
let mut stream_params = ffi::cubeb_stream_params::default(); |
| 1406 |
stream_params.prefs = ffi::CUBEB_STREAM_PREF_NONE; |
| 1407 |
let (format, rate, channels, layout) = match scope { |
| 1408 |
Scope::Input => (STREAM_FORMAT, 48000, 1, ffi::CUBEB_LAYOUT_MONO), |
| 1409 |
Scope::Output => (STREAM_FORMAT, 44100, 2, ffi::CUBEB_LAYOUT_STEREO), |
| 1410 |
}; |
| 1411 |
stream_params.format = format; |
| 1412 |
stream_params.rate = rate; |
| 1413 |
stream_params.channels = channels; |
| 1414 |
stream_params.layout = layout; |
| 1415 |
stream_params |
| 1416 |
} |
| 1417 |
|
| 1418 |
fn test_ops_stream_operation_with_default_data_callback<F>( |
| 1419 |
name: &'static str, |
| 1420 |
input_device: ffi::cubeb_devid, |
| 1421 |
input_stream_params: *mut ffi::cubeb_stream_params, |
| 1422 |
output_device: ffi::cubeb_devid, |
| 1423 |
output_stream_params: *mut ffi::cubeb_stream_params, |
| 1424 |
state_callback: extern "C" fn(*mut ffi::cubeb_stream, *mut c_void, ffi::cubeb_state), |
| 1425 |
data: *mut c_void, |
| 1426 |
operation: F, |
| 1427 |
) where |
| 1428 |
F: FnOnce(*mut ffi::cubeb_stream), |
| 1429 |
{ |
| 1430 |
test_ops_stream_operation( |
| 1431 |
name, |
| 1432 |
input_device, |
| 1433 |
input_stream_params, |
| 1434 |
output_device, |
| 1435 |
output_stream_params, |
| 1436 |
4096, // TODO: Get latency by get_min_latency instead ? |
| 1437 |
Some(noop_data_callback), |
| 1438 |
Some(state_callback), |
| 1439 |
data, |
| 1440 |
operation, |
| 1441 |
); |
| 1442 |
} |