| 1 |
// NOTICE: |
| 2 |
// Avoid running TestDeviceSwitcher with TestDevicePlugger or active full-duplex streams |
| 3 |
// sequentially! |
| 4 |
// |
| 5 |
// The TestDeviceSwitcher cannot work with any test that will create an aggregate device that is |
| 6 |
// soon being destroyed. The TestDeviceSwitcher will cache the available devices, upon it's |
| 7 |
// created, as the candidates for the default device. Therefore, those created aggregate devices |
| 8 |
// may be cached in TestDeviceSwitcher. However, those aggregate devices may be destroyed when |
| 9 |
// TestDeviceSwitcher is using them or they are in the cached list of TestDeviceSwitcher. |
| 10 |
// |
| 11 |
// Running those tests by setting `test-threads=1` doesn't really help (e.g., |
| 12 |
// `cargo test test_register_device_changed_callback -- --ignored --nocapture --test-threads=1`). |
| 13 |
// The aggregate device won't be destroyed immediately when `kAudioPlugInDestroyAggregateDevice` |
| 14 |
// is set. As a result, the following tests requiring changing the devices will be run separately |
| 15 |
// in the run_tests.sh script and marked by `ignore` by default. |
| 16 |
|
| 17 |
use super::utils::{ |
| 18 |
get_devices_info_in_scope, test_create_device_change_listener, test_device_in_scope, |
| 19 |
test_get_default_device, test_get_devices_in_scope, |
| 20 |
test_get_stream_with_default_data_callback_by_type, test_ops_stream_operation, |
| 21 |
test_set_default_device, Scope, StreamType, TestDevicePlugger, TestDeviceSwitcher, |
| 22 |
}; |
| 23 |
use super::*; |
| 24 |
use std::sync::{LockResult, WaitTimeoutResult}; |
| 25 |
use std::thread; |
| 26 |
use std::time; |
| 27 |
|
| 28 |
// Switch default devices used by the active streams, to test stream reinitialization |
| 29 |
// ================================================================================================ |
| 30 |
#[ignore] |
| 31 |
#[test] |
| 32 |
fn test_switch_device() { |
| 33 |
test_switch_device_in_scope(Scope::Input); |
| 34 |
test_switch_device_in_scope(Scope::Output); |
| 35 |
} |
| 36 |
|
| 37 |
fn test_switch_device_in_scope(scope: Scope) { |
| 38 |
println!( |
| 39 |
"Switch default device for {:?} while the stream is working.", |
| 40 |
scope |
| 41 |
); |
| 42 |
|
| 43 |
// Do nothing if there is no 2 available devices at least. |
| 44 |
let devices = test_get_devices_in_scope(scope.clone()); |
| 45 |
if devices.len() < 2 { |
| 46 |
println!("Need 2 devices for {:?} at least. Skip.", scope); |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
let mut device_switcher = TestDeviceSwitcher::new(scope.clone()); |
| 51 |
|
| 52 |
let notifier = Arc::new(Notifier::new(0)); |
| 53 |
let also_notifier = notifier.clone(); |
| 54 |
let listener = run_serially(|| { |
| 55 |
test_create_device_change_listener(scope.clone(), move |_addresses| { |
| 56 |
let mut cnt = notifier.lock().unwrap(); |
| 57 |
*cnt += 1; |
| 58 |
notifier.notify(cnt); |
| 59 |
NO_ERR |
| 60 |
}) |
| 61 |
}); |
| 62 |
run_serially(|| listener.start()); |
| 63 |
|
| 64 |
let changed_watcher = Watcher::new(&also_notifier); |
| 65 |
test_get_started_stream_in_scope(scope.clone(), move |_stream| loop { |
| 66 |
let start_cnt = changed_watcher.lock().unwrap().clone(); |
| 67 |
device_switcher.next(); |
| 68 |
let mut guard = changed_watcher.lock().unwrap(); |
| 69 |
guard = changed_watcher |
| 70 |
.wait_while(guard, |cnt| *cnt == start_cnt) |
| 71 |
.unwrap(); |
| 72 |
if *guard >= devices.len() { |
| 73 |
break; |
| 74 |
} |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
#[ignore] |
| 79 |
#[test] |
| 80 |
fn test_switch_while_paused() { |
| 81 |
test_switch_device_in_scope_while_paused(Scope::Input); |
| 82 |
test_switch_device_in_scope_while_paused(Scope::Output); |
| 83 |
} |
| 84 |
|
| 85 |
fn test_switch_device_in_scope_while_paused(scope: Scope) { |
| 86 |
println!( |
| 87 |
"Switch default device for {:?} while the stream is paused.", |
| 88 |
scope |
| 89 |
); |
| 90 |
|
| 91 |
// Do nothing if there is no 2 available devices at least. |
| 92 |
let devices = test_get_devices_in_scope(scope.clone()); |
| 93 |
if devices.len() < 2 { |
| 94 |
println!("Need 2 devices for {:?} at least. Skip.", scope); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
let mut device_switcher = TestDeviceSwitcher::new(scope.clone()); |
| 99 |
|
| 100 |
let notifier = Arc::new(Notifier::new(0)); |
| 101 |
let also_notifier = notifier.clone(); |
| 102 |
let listener = run_serially(|| { |
| 103 |
test_create_device_change_listener(scope.clone(), move |_addresses| { |
| 104 |
let mut cnt = notifier.lock().unwrap(); |
| 105 |
*cnt += 1; |
| 106 |
notifier.notify(cnt); |
| 107 |
NO_ERR |
| 108 |
}) |
| 109 |
}); |
| 110 |
run_serially(|| listener.start()); |
| 111 |
|
| 112 |
let changed_watcher = Watcher::new(&also_notifier); |
| 113 |
test_get_started_stream_in_scope(scope.clone(), move |stream| loop { |
| 114 |
// pause the stream, change device, start the stream |
| 115 |
// peek under the hood to the find the device in use currently |
| 116 |
let stm = unsafe { &mut *(stream as *mut AudioUnitStream) }; |
| 117 |
let before = if scope == Scope::Output { |
| 118 |
stm.core_stream_data.output_unit |
| 119 |
} else { |
| 120 |
stm.core_stream_data.input_unit |
| 121 |
}; |
| 122 |
|
| 123 |
let check_devices = |current| { |
| 124 |
stm.queue.run_sync(|| { |
| 125 |
let (bus, unit, id) = if scope == Scope::Output { |
| 126 |
( |
| 127 |
AU_OUT_BUS, |
| 128 |
stm.core_stream_data.output_unit, |
| 129 |
stm.core_stream_data.output_device.id, |
| 130 |
) |
| 131 |
} else { |
| 132 |
( |
| 133 |
AU_IN_BUS, |
| 134 |
stm.core_stream_data.input_unit, |
| 135 |
stm.core_stream_data.input_device.id, |
| 136 |
) |
| 137 |
}; |
| 138 |
let mut device_id: AudioDeviceID = 0; |
| 139 |
let mut size = std::mem::size_of::<AudioDeviceID>(); |
| 140 |
let status = audio_unit_get_property( |
| 141 |
unit, |
| 142 |
kAudioOutputUnitProperty_CurrentDevice, |
| 143 |
kAudioUnitScope_Global, |
| 144 |
bus, |
| 145 |
&mut device_id, |
| 146 |
&mut size, |
| 147 |
); |
| 148 |
if status != NO_ERR { |
| 149 |
panic!("Could not get device ID from audiounit"); |
| 150 |
} |
| 151 |
assert_eq!(id, current); |
| 152 |
assert_eq!(device_id, current); |
| 153 |
}); |
| 154 |
}; |
| 155 |
|
| 156 |
check_devices(device_switcher.current()); |
| 157 |
|
| 158 |
// Pause the stream, and change the default device |
| 159 |
assert_eq!(unsafe { OPS.stream_stop.unwrap()(stream) }, ffi::CUBEB_OK); |
| 160 |
|
| 161 |
let start_cnt = changed_watcher.lock().unwrap().clone(); |
| 162 |
device_switcher.next(); |
| 163 |
let mut guard = changed_watcher.lock().unwrap(); |
| 164 |
guard = changed_watcher |
| 165 |
.wait_while(guard, |cnt| *cnt == start_cnt) |
| 166 |
.unwrap(); |
| 167 |
if *guard >= devices.len() { |
| 168 |
break; |
| 169 |
} |
| 170 |
|
| 171 |
// Wait until the switch is effective and the stream has been marked for delayed |
| 172 |
// reinitialization. delayed_reinit can only be access from the queue thread. Depending on |
| 173 |
// the setup this can take some time so we sleep a bit to not hammer the main thread. |
| 174 |
let mut switched = false; |
| 175 |
while !switched { |
| 176 |
switched = stm.queue.run_sync(|| stm.delayed_reinit).unwrap(); |
| 177 |
if !switched { |
| 178 |
let ten_millis = time::Duration::from_millis(10); |
| 179 |
thread::sleep(ten_millis); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// Start the stream, and check that the device in use isn't the same as before pausing |
| 184 |
assert_eq!(unsafe { OPS.stream_start.unwrap()(stream) }, ffi::CUBEB_OK); |
| 185 |
|
| 186 |
check_devices(device_switcher.current()); |
| 187 |
|
| 188 |
let after = if scope == Scope::Output { |
| 189 |
stm.core_stream_data.output_unit |
| 190 |
} else { |
| 191 |
stm.core_stream_data.input_unit |
| 192 |
}; |
| 193 |
assert_ne!(before, after); |
| 194 |
}); |
| 195 |
} |
| 196 |
|
| 197 |
fn test_get_started_stream_in_scope<F>(scope: Scope, operation: F) |
| 198 |
where |
| 199 |
F: FnOnce(*mut ffi::cubeb_stream), |
| 200 |
{ |
| 201 |
use std::f32::consts::PI; |
| 202 |
const SAMPLE_FREQUENCY: u32 = 48_000; |
| 203 |
|
| 204 |
// Make sure the parameters meet the requirements of AudioUnitContext::stream_init |
| 205 |
// (in the comments). |
| 206 |
let mut stream_params = ffi::cubeb_stream_params::default(); |
| 207 |
stream_params.format = ffi::CUBEB_SAMPLE_S16NE; |
| 208 |
stream_params.rate = SAMPLE_FREQUENCY; |
| 209 |
stream_params.prefs = ffi::CUBEB_STREAM_PREF_NONE; |
| 210 |
stream_params.channels = 1; |
| 211 |
stream_params.layout = ffi::CUBEB_LAYOUT_MONO; |
| 212 |
|
| 213 |
let (input_params, output_params) = match scope { |
| 214 |
Scope::Input => ( |
| 215 |
&mut stream_params as *mut ffi::cubeb_stream_params, |
| 216 |
ptr::null_mut(), |
| 217 |
), |
| 218 |
Scope::Output => ( |
| 219 |
ptr::null_mut(), |
| 220 |
&mut stream_params as *mut ffi::cubeb_stream_params, |
| 221 |
), |
| 222 |
}; |
| 223 |
|
| 224 |
extern "C" fn state_callback( |
| 225 |
stream: *mut ffi::cubeb_stream, |
| 226 |
user_ptr: *mut c_void, |
| 227 |
state: ffi::cubeb_state, |
| 228 |
) { |
| 229 |
assert!(!stream.is_null()); |
| 230 |
assert!(!user_ptr.is_null()); |
| 231 |
assert_ne!(state, ffi::CUBEB_STATE_ERROR); |
| 232 |
} |
| 233 |
|
| 234 |
extern "C" fn input_data_callback( |
| 235 |
stream: *mut ffi::cubeb_stream, |
| 236 |
user_ptr: *mut c_void, |
| 237 |
input_buffer: *const c_void, |
| 238 |
output_buffer: *mut c_void, |
| 239 |
nframes: i64, |
| 240 |
) -> i64 { |
| 241 |
assert!(!stream.is_null()); |
| 242 |
assert!(!user_ptr.is_null()); |
| 243 |
assert!(!input_buffer.is_null()); |
| 244 |
assert!(output_buffer.is_null()); |
| 245 |
nframes |
| 246 |
} |
| 247 |
|
| 248 |
let mut position: i64 = 0; // TODO: Use Atomic instead. |
| 249 |
|
| 250 |
fn f32_to_i16_sample(x: f32) -> i16 { |
| 251 |
(x * f32::from(i16::max_value())) as i16 |
| 252 |
} |
| 253 |
|
| 254 |
extern "C" fn output_data_callback( |
| 255 |
stream: *mut ffi::cubeb_stream, |
| 256 |
user_ptr: *mut c_void, |
| 257 |
input_buffer: *const c_void, |
| 258 |
output_buffer: *mut c_void, |
| 259 |
nframes: i64, |
| 260 |
) -> i64 { |
| 261 |
assert!(!stream.is_null()); |
| 262 |
assert!(!user_ptr.is_null()); |
| 263 |
assert!(input_buffer.is_null()); |
| 264 |
assert!(!output_buffer.is_null()); |
| 265 |
|
| 266 |
let buffer = unsafe { |
| 267 |
let ptr = output_buffer as *mut i16; |
| 268 |
let len = nframes as usize; |
| 269 |
slice::from_raw_parts_mut(ptr, len) |
| 270 |
}; |
| 271 |
|
| 272 |
let position = unsafe { &mut *(user_ptr as *mut i64) }; |
| 273 |
|
| 274 |
// Generate tone on the fly. |
| 275 |
for data in buffer.iter_mut() { |
| 276 |
let t1 = (2.0 * PI * 350.0 * (*position) as f32 / SAMPLE_FREQUENCY as f32).sin(); |
| 277 |
let t2 = (2.0 * PI * 440.0 * (*position) as f32 / SAMPLE_FREQUENCY as f32).sin(); |
| 278 |
*data = f32_to_i16_sample(0.5 * (t1 + t2)); |
| 279 |
*position += 1; |
| 280 |
} |
| 281 |
|
| 282 |
nframes |
| 283 |
} |
| 284 |
|
| 285 |
test_ops_stream_operation( |
| 286 |
"stream", |
| 287 |
ptr::null_mut(), // Use default input device. |
| 288 |
input_params, |
| 289 |
ptr::null_mut(), // Use default output device. |
| 290 |
output_params, |
| 291 |
4096, // TODO: Get latency by get_min_latency instead ? |
| 292 |
match scope { |
| 293 |
Scope::Input => Some(input_data_callback), |
| 294 |
Scope::Output => Some(output_data_callback), |
| 295 |
}, |
| 296 |
Some(state_callback), |
| 297 |
&mut position as *mut i64 as *mut c_void, |
| 298 |
|stream| { |
| 299 |
assert_eq!(unsafe { OPS.stream_start.unwrap()(stream) }, ffi::CUBEB_OK); |
| 300 |
operation(stream); |
| 301 |
assert_eq!(unsafe { OPS.stream_stop.unwrap()(stream) }, ffi::CUBEB_OK); |
| 302 |
}, |
| 303 |
); |
| 304 |
} |
| 305 |
|
| 306 |
// Plug and unplug devices, to test device collection changed callback |
| 307 |
// ================================================================================================ |
| 308 |
#[ignore] |
| 309 |
#[test] |
| 310 |
fn test_plug_and_unplug_device() { |
| 311 |
test_plug_and_unplug_device_in_scope(Scope::Input); |
| 312 |
test_plug_and_unplug_device_in_scope(Scope::Output); |
| 313 |
} |
| 314 |
|
| 315 |
fn test_plug_and_unplug_device_in_scope(scope: Scope) { |
| 316 |
let default_device = test_get_default_device(scope.clone()); |
| 317 |
if default_device.is_none() { |
| 318 |
println!("No device for {:?} to test", scope); |
| 319 |
return; |
| 320 |
} |
| 321 |
|
| 322 |
println!("Run test for {:?}", scope); |
| 323 |
println!("NOTICE: The test will hang if the default input or output is an aggregate device.\nWe will fix this later."); |
| 324 |
|
| 325 |
let default_device = default_device.unwrap(); |
| 326 |
let is_input = test_device_in_scope(default_device, Scope::Input); |
| 327 |
let is_output = test_device_in_scope(default_device, Scope::Output); |
| 328 |
|
| 329 |
let mut context = AudioUnitContext::new(); |
| 330 |
|
| 331 |
// Register the devices-changed callbacks. |
| 332 |
#[derive(Clone, PartialEq)] |
| 333 |
struct Counts { |
| 334 |
input: u32, |
| 335 |
output: u32, |
| 336 |
} |
| 337 |
impl Counts { |
| 338 |
fn new() -> Self { |
| 339 |
Self { |
| 340 |
input: 0, |
| 341 |
output: 0, |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
let counts = Arc::new(Notifier::new(Counts::new())); |
| 346 |
let counts_notifier_ptr = counts.as_ref() as *const Notifier<Counts>; |
| 347 |
|
| 348 |
assert!(context |
| 349 |
.register_device_collection_changed( |
| 350 |
DeviceType::INPUT, |
| 351 |
Some(input_changed_callback), |
| 352 |
counts_notifier_ptr as *mut c_void, |
| 353 |
) |
| 354 |
.is_ok()); |
| 355 |
|
| 356 |
assert!(context |
| 357 |
.register_device_collection_changed( |
| 358 |
DeviceType::OUTPUT, |
| 359 |
Some(output_changed_callback), |
| 360 |
counts_notifier_ptr as *mut c_void, |
| 361 |
) |
| 362 |
.is_ok()); |
| 363 |
|
| 364 |
let counts_watcher = Watcher::new(&counts); |
| 365 |
|
| 366 |
let mut device_plugger = TestDevicePlugger::new(scope).unwrap(); |
| 367 |
|
| 368 |
{ |
| 369 |
// Simulate adding devices and monitor the devices-changed callbacks. |
| 370 |
let mut counts_guard = counts.lock().unwrap(); |
| 371 |
let counts_start = counts_guard.clone(); |
| 372 |
|
| 373 |
assert!(device_plugger.plug().is_ok()); |
| 374 |
|
| 375 |
counts_guard = counts_watcher |
| 376 |
.wait_while(counts_guard, |counts| { |
| 377 |
(is_input && counts.input == counts_start.input) |
| 378 |
|| (is_output && counts.output == counts_start.output) |
| 379 |
}) |
| 380 |
.unwrap(); |
| 381 |
|
| 382 |
// Check changed count. |
| 383 |
assert_eq!(counts_guard.input, if is_input { 1 } else { 0 }); |
| 384 |
assert_eq!(counts_guard.output, if is_output { 1 } else { 0 }); |
| 385 |
} |
| 386 |
|
| 387 |
{ |
| 388 |
// Simulate removing devices and monitor the devices-changed callbacks. |
| 389 |
let mut counts_guard = counts.lock().unwrap(); |
| 390 |
let counts_start = counts_guard.clone(); |
| 391 |
|
| 392 |
assert!(device_plugger.unplug().is_ok()); |
| 393 |
|
| 394 |
counts_guard = counts_watcher |
| 395 |
.wait_while(counts_guard, |counts| { |
| 396 |
(is_input && counts.input == counts_start.input) |
| 397 |
|| (is_output && counts.output == counts_start.output) |
| 398 |
}) |
| 399 |
.unwrap(); |
| 400 |
|
| 401 |
// Check changed count. |
| 402 |
assert_eq!(counts_guard.input, if is_input { 2 } else { 0 }); |
| 403 |
assert_eq!(counts_guard.output, if is_output { 2 } else { 0 }); |
| 404 |
} |
| 405 |
|
| 406 |
extern "C" fn input_changed_callback(context: *mut ffi::cubeb, data: *mut c_void) { |
| 407 |
println!( |
| 408 |
"Input device collection @ {:p} is changed. Data @ {:p}", |
| 409 |
context, data |
| 410 |
); |
| 411 |
let notifier = unsafe { &*(data as *const Notifier<Counts>) }; |
| 412 |
{ |
| 413 |
let mut counts = notifier.lock().unwrap(); |
| 414 |
counts.input += 1; |
| 415 |
notifier.notify(counts); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
extern "C" fn output_changed_callback(context: *mut ffi::cubeb, data: *mut c_void) { |
| 420 |
println!( |
| 421 |
"output device collection @ {:p} is changed. Data @ {:p}", |
| 422 |
context, data |
| 423 |
); |
| 424 |
let notifier = unsafe { &*(data as *const Notifier<Counts>) }; |
| 425 |
{ |
| 426 |
let mut counts = notifier.lock().unwrap(); |
| 427 |
counts.output += 1; |
| 428 |
notifier.notify(counts); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
context.register_device_collection_changed(DeviceType::OUTPUT, None, ptr::null_mut()); |
| 433 |
context.register_device_collection_changed(DeviceType::INPUT, None, ptr::null_mut()); |
| 434 |
} |
| 435 |
|
| 436 |
// Switch default devices used by the active streams, to test device changed callback |
| 437 |
// ================================================================================================ |
| 438 |
#[ignore] |
| 439 |
#[test] |
| 440 |
fn test_register_device_changed_callback_to_check_default_device_changed_input() { |
| 441 |
test_register_device_changed_callback_to_check_default_device_changed(StreamType::INPUT); |
| 442 |
} |
| 443 |
|
| 444 |
#[ignore] |
| 445 |
#[test] |
| 446 |
fn test_register_device_changed_callback_to_check_default_device_changed_output() { |
| 447 |
test_register_device_changed_callback_to_check_default_device_changed(StreamType::OUTPUT); |
| 448 |
} |
| 449 |
|
| 450 |
#[ignore] |
| 451 |
#[test] |
| 452 |
fn test_register_device_changed_callback_to_check_default_device_changed_duplex() { |
| 453 |
test_register_device_changed_callback_to_check_default_device_changed(StreamType::DUPLEX); |
| 454 |
} |
| 455 |
|
| 456 |
fn test_register_device_changed_callback_to_check_default_device_changed(stm_type: StreamType) { |
| 457 |
println!("NOTICE: The test will hang if the default input or output is an aggregate device.\nWe will fix this later."); |
| 458 |
|
| 459 |
let inputs = if stm_type.contains(StreamType::INPUT) { |
| 460 |
let devices = test_get_devices_in_scope(Scope::Input).len(); |
| 461 |
if devices >= 2 { |
| 462 |
Some(devices) |
| 463 |
} else { |
| 464 |
None |
| 465 |
} |
| 466 |
} else { |
| 467 |
None |
| 468 |
}; |
| 469 |
|
| 470 |
let outputs = if stm_type.contains(StreamType::OUTPUT) { |
| 471 |
let devices = test_get_devices_in_scope(Scope::Output).len(); |
| 472 |
if devices >= 2 { |
| 473 |
Some(devices) |
| 474 |
} else { |
| 475 |
None |
| 476 |
} |
| 477 |
} else { |
| 478 |
None |
| 479 |
}; |
| 480 |
|
| 481 |
if inputs.is_none() && outputs.is_none() { |
| 482 |
println!("No enough devices to run the test!"); |
| 483 |
return; |
| 484 |
} |
| 485 |
|
| 486 |
let changed_count = Arc::new(Notifier::new(0u32)); |
| 487 |
let notifier_ptr = changed_count.as_ref() as *const Notifier<u32>; |
| 488 |
|
| 489 |
test_get_stream_with_device_changed_callback( |
| 490 |
"stream: test callback for default device changed", |
| 491 |
stm_type, |
| 492 |
None, // Use default input device. |
| 493 |
None, // Use default output device. |
| 494 |
notifier_ptr as *mut c_void, |
| 495 |
state_callback, |
| 496 |
device_changed_callback, |
| 497 |
|stream| { |
| 498 |
// If the duplex stream uses different input and output device, |
| 499 |
// an aggregate device will be created and it will work for this duplex stream. |
| 500 |
// This aggregate device will be added into the device list, but it won't |
| 501 |
// be assigned to the default device, since the device list for setting |
| 502 |
// default device is cached upon {input, output}_device_switcher is initialized. |
| 503 |
|
| 504 |
let changed_watcher = Watcher::new(&changed_count); |
| 505 |
|
| 506 |
if let Some(devices) = inputs { |
| 507 |
let mut device_switcher = TestDeviceSwitcher::new(Scope::Input); |
| 508 |
for _ in 0..devices { |
| 509 |
// While the stream is re-initializing for the default device switch, |
| 510 |
// switching for the default device again will be ignored. |
| 511 |
while stream.switching_device.load(atomic::Ordering::SeqCst) { |
| 512 |
std::hint::spin_loop() |
| 513 |
} |
| 514 |
let guard = changed_watcher.lock().unwrap(); |
| 515 |
let start_cnt = guard.clone(); |
| 516 |
device_switcher.next(); |
| 517 |
changed_watcher |
| 518 |
.wait_while(guard, |cnt| *cnt == start_cnt) |
| 519 |
.unwrap(); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
if let Some(devices) = outputs { |
| 524 |
let mut device_switcher = TestDeviceSwitcher::new(Scope::Output); |
| 525 |
for _ in 0..devices { |
| 526 |
// While the stream is re-initializing for the default device switch, |
| 527 |
// switching for the default device again will be ignored. |
| 528 |
while stream.switching_device.load(atomic::Ordering::SeqCst) { |
| 529 |
std::hint::spin_loop() |
| 530 |
} |
| 531 |
let guard = changed_watcher.lock().unwrap(); |
| 532 |
let start_cnt = guard.clone(); |
| 533 |
device_switcher.next(); |
| 534 |
changed_watcher |
| 535 |
.wait_while(guard, |cnt| *cnt == start_cnt) |
| 536 |
.unwrap(); |
| 537 |
} |
| 538 |
} |
| 539 |
}, |
| 540 |
); |
| 541 |
|
| 542 |
extern "C" fn state_callback( |
| 543 |
stream: *mut ffi::cubeb_stream, |
| 544 |
_user_ptr: *mut c_void, |
| 545 |
state: ffi::cubeb_state, |
| 546 |
) { |
| 547 |
assert!(!stream.is_null()); |
| 548 |
assert_ne!(state, ffi::CUBEB_STATE_ERROR); |
| 549 |
} |
| 550 |
|
| 551 |
extern "C" fn device_changed_callback(data: *mut c_void) { |
| 552 |
println!("Device change callback. data @ {:p}", data); |
| 553 |
let notifier = unsafe { &*(data as *const Notifier<u32>) }; |
| 554 |
let mut count_guard = notifier.lock().unwrap(); |
| 555 |
*count_guard += 1; |
| 556 |
notifier.notify(count_guard); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
// Unplug the devices used by the active streams, to test |
| 561 |
// 1) device changed callback, or state callback |
| 562 |
// 2) stream reinitialization that may race with stream destroying |
| 563 |
// ================================================================================================ |
| 564 |
|
| 565 |
// Input-only stream |
| 566 |
// ----------------- |
| 567 |
|
| 568 |
// Unplug the non-default input device for an input stream |
| 569 |
// ------------------------------------------------------------------------------------------------ |
| 570 |
|
| 571 |
#[ignore] |
| 572 |
#[test] |
| 573 |
fn test_destroy_input_stream_after_unplugging_a_nondefault_input_device() { |
| 574 |
// The stream can be destroyed before running device-changed event handler |
| 575 |
test_unplug_a_device_on_an_active_stream(StreamType::INPUT, Scope::Input, false, 0); |
| 576 |
} |
| 577 |
|
| 578 |
#[ignore] |
| 579 |
#[test] |
| 580 |
fn test_suspend_input_stream_by_unplugging_a_nondefault_input_device() { |
| 581 |
// Expect to get an error state callback by device-changed event handler |
| 582 |
test_unplug_a_device_on_an_active_stream(StreamType::INPUT, Scope::Input, false, 2000); |
| 583 |
} |
| 584 |
|
| 585 |
// Unplug the default input device for an input stream |
| 586 |
// ------------------------------------------------------------------------------------------------ |
| 587 |
#[ignore] |
| 588 |
#[test] |
| 589 |
fn test_destroy_input_stream_after_unplugging_a_default_input_device() { |
| 590 |
// Expect to get an device-changed callback by device-changed event handler, |
| 591 |
// which will reinitialize the stream behind the scenes, at the same when |
| 592 |
// the stream is being destroyed |
| 593 |
test_unplug_a_device_on_an_active_stream(StreamType::INPUT, Scope::Input, true, 0); |
| 594 |
} |
| 595 |
|
| 596 |
#[ignore] |
| 597 |
#[test] |
| 598 |
fn test_reinit_input_stream_by_unplugging_a_default_input_device() { |
| 599 |
// Expect to get an device-changed callback by device-changed event handler, |
| 600 |
// which will reinitialize the stream behind the scenes |
| 601 |
test_unplug_a_device_on_an_active_stream(StreamType::INPUT, Scope::Input, true, 2000); |
| 602 |
} |
| 603 |
|
| 604 |
// Output-only stream |
| 605 |
// ------------------ |
| 606 |
|
| 607 |
// Unplug the non-default output device for an output stream |
| 608 |
// ------------------------------------------------------------------------------------------------ |
| 609 |
#[ignore] |
| 610 |
#[test] |
| 611 |
fn test_destroy_output_stream_after_unplugging_a_nondefault_output_device() { |
| 612 |
test_unplug_a_device_on_an_active_stream(StreamType::OUTPUT, Scope::Output, false, 0); |
| 613 |
} |
| 614 |
|
| 615 |
#[ignore] |
| 616 |
#[test] |
| 617 |
fn test_suspend_output_stream_by_unplugging_a_nondefault_output_device() { |
| 618 |
test_unplug_a_device_on_an_active_stream(StreamType::OUTPUT, Scope::Output, false, 2000); |
| 619 |
} |
| 620 |
|
| 621 |
// Unplug the default output device for an output stream |
| 622 |
// ------------------------------------------------------------------------------------------------ |
| 623 |
|
| 624 |
#[ignore] |
| 625 |
#[test] |
| 626 |
fn test_destroy_output_stream_after_unplugging_a_default_output_device() { |
| 627 |
// Expect to get an device-changed callback by device-changed event handler, |
| 628 |
// which will reinitialize the stream behind the scenes, at the same when |
| 629 |
// the stream is being destroyed |
| 630 |
test_unplug_a_device_on_an_active_stream(StreamType::OUTPUT, Scope::Output, true, 0); |
| 631 |
} |
| 632 |
|
| 633 |
#[ignore] |
| 634 |
#[test] |
| 635 |
fn test_reinit_output_stream_by_unplugging_a_default_output_device() { |
| 636 |
// Expect to get an device-changed callback by device-changed event handler, |
| 637 |
// which will reinitialize the stream behind the scenes |
| 638 |
test_unplug_a_device_on_an_active_stream(StreamType::OUTPUT, Scope::Output, true, 2000); |
| 639 |
} |
| 640 |
|
| 641 |
// Duplex stream |
| 642 |
// ------------- |
| 643 |
|
| 644 |
// Unplug the non-default input device for a duplex stream |
| 645 |
// ------------------------------------------------------------------------------------------------ |
| 646 |
|
| 647 |
#[ignore] |
| 648 |
#[test] |
| 649 |
fn test_destroy_duplex_stream_after_unplugging_a_nondefault_input_device() { |
| 650 |
// The stream can be destroyed before running device-changed event handler |
| 651 |
test_unplug_a_device_on_an_active_stream(StreamType::DUPLEX, Scope::Input, false, 0); |
| 652 |
} |
| 653 |
|
| 654 |
#[ignore] |
| 655 |
#[test] |
| 656 |
fn test_suspend_duplex_stream_by_unplugging_a_nondefault_input_device() { |
| 657 |
// Expect to get an error state callback by device-changed event handler |
| 658 |
test_unplug_a_device_on_an_active_stream(StreamType::DUPLEX, Scope::Input, false, 2000); |
| 659 |
} |
| 660 |
|
| 661 |
// Unplug the non-default output device for a duplex stream |
| 662 |
// ------------------------------------------------------------------------------------------------ |
| 663 |
|
| 664 |
#[ignore] |
| 665 |
#[test] |
| 666 |
fn test_destroy_duplex_stream_after_unplugging_a_nondefault_output_device() { |
| 667 |
test_unplug_a_device_on_an_active_stream(StreamType::DUPLEX, Scope::Output, false, 0); |
| 668 |
} |
| 669 |
|
| 670 |
#[ignore] |
| 671 |
#[test] |
| 672 |
fn test_suspend_duplex_stream_by_unplugging_a_nondefault_output_device() { |
| 673 |
test_unplug_a_device_on_an_active_stream(StreamType::DUPLEX, Scope::Output, false, 2000); |
| 674 |
} |
| 675 |
|
| 676 |
// Unplug the non-default in-out device for a duplex stream |
| 677 |
// ------------------------------------------------------------------------------------------------ |
| 678 |
// TODO: Implement an in-out TestDevicePlugger |
| 679 |
|
| 680 |
// Unplug the default input device for a duplex stream |
| 681 |
// ------------------------------------------------------------------------------------------------ |
| 682 |
|
| 683 |
#[ignore] |
| 684 |
#[test] |
| 685 |
fn test_destroy_duplex_stream_after_unplugging_a_default_input_device() { |
| 686 |
// Expect to get an device-changed callback by device-changed event handler, |
| 687 |
// which will reinitialize the stream behind the scenes, at the same when |
| 688 |
// the stream is being destroyed |
| 689 |
test_unplug_a_device_on_an_active_stream(StreamType::DUPLEX, Scope::Input, true, 0); |
| 690 |
} |
| 691 |
|
| 692 |
#[ignore] |
| 693 |
#[test] |
| 694 |
fn test_reinit_duplex_stream_by_unplugging_a_default_input_device() { |
| 695 |
// Expect to get an device-changed callback by device-changed event handler, |
| 696 |
// which will reinitialize the stream behind the scenes |
| 697 |
test_unplug_a_device_on_an_active_stream(StreamType::DUPLEX, Scope::Input, true, 2000); |
| 698 |
} |
| 699 |
|
| 700 |
// Unplug the default ouput device for a duplex stream |
| 701 |
// ------------------------------------------------------------------------------------------------ |
| 702 |
|
| 703 |
#[ignore] |
| 704 |
#[test] |
| 705 |
fn test_destroy_duplex_stream_after_unplugging_a_default_output_device() { |
| 706 |
// Expect to get an device-changed callback by device-changed event handler, |
| 707 |
// which will reinitialize the stream behind the scenes, at the same when |
| 708 |
// the stream is being destroyed |
| 709 |
test_unplug_a_device_on_an_active_stream(StreamType::DUPLEX, Scope::Output, true, 0); |
| 710 |
} |
| 711 |
|
| 712 |
#[ignore] |
| 713 |
#[test] |
| 714 |
fn test_reinit_duplex_stream_by_unplugging_a_default_output_device() { |
| 715 |
// Expect to get an device-changed callback by device-changed event handler, |
| 716 |
// which will reinitialize the stream behind the scenes |
| 717 |
test_unplug_a_device_on_an_active_stream(StreamType::DUPLEX, Scope::Output, true, 2000); |
| 718 |
} |
| 719 |
|
| 720 |
fn test_unplug_a_device_on_an_active_stream( |
| 721 |
stream_type: StreamType, |
| 722 |
device_scope: Scope, |
| 723 |
set_device_to_default: bool, |
| 724 |
wait_up_to_ms: u64, |
| 725 |
) { |
| 726 |
let has_input = test_get_default_device(Scope::Input).is_some(); |
| 727 |
let has_output = test_get_default_device(Scope::Output).is_some(); |
| 728 |
|
| 729 |
if stream_type.contains(StreamType::INPUT) && !has_input { |
| 730 |
println!("No input device for input or duplex stream."); |
| 731 |
return; |
| 732 |
} |
| 733 |
|
| 734 |
if stream_type.contains(StreamType::OUTPUT) && !has_output { |
| 735 |
println!("No output device for output or duplex stream."); |
| 736 |
return; |
| 737 |
} |
| 738 |
|
| 739 |
let default_device_before_plugging = test_get_default_device(device_scope.clone()).unwrap(); |
| 740 |
println!( |
| 741 |
"Before plugging, default {:?} device is {}", |
| 742 |
device_scope, default_device_before_plugging |
| 743 |
); |
| 744 |
|
| 745 |
let mut plugger = TestDevicePlugger::new(device_scope.clone()).unwrap(); |
| 746 |
assert!(plugger.plug().is_ok()); |
| 747 |
assert_ne!(plugger.get_device_id(), kAudioObjectUnknown); |
| 748 |
println!( |
| 749 |
"Create plugger device: {} for {:?}", |
| 750 |
plugger.get_device_id(), |
| 751 |
device_scope |
| 752 |
); |
| 753 |
|
| 754 |
let default_device_after_plugging = test_get_default_device(device_scope.clone()).unwrap(); |
| 755 |
println!( |
| 756 |
"After plugging, default {:?} device is {}", |
| 757 |
device_scope, default_device_after_plugging |
| 758 |
); |
| 759 |
|
| 760 |
// The new device, plugger, is possible to be set to the default device. |
| 761 |
// Before running the test, we need to set the default device to the correct one. |
| 762 |
if set_device_to_default { |
| 763 |
// plugger should be the default device for the test. |
| 764 |
// If it's not, then set it to the default device. |
| 765 |
if default_device_after_plugging != plugger.get_device_id() { |
| 766 |
let prev_def_dev = |
| 767 |
test_set_default_device(plugger.get_device_id(), device_scope.clone()).unwrap(); |
| 768 |
assert_eq!(prev_def_dev, default_device_after_plugging); |
| 769 |
} |
| 770 |
} else { |
| 771 |
// plugger should NOT be the default device for the test. |
| 772 |
// If it is, reset the default device to another one. |
| 773 |
if default_device_after_plugging == plugger.get_device_id() { |
| 774 |
let prev_def_dev = |
| 775 |
test_set_default_device(default_device_before_plugging, device_scope.clone()) |
| 776 |
.unwrap(); |
| 777 |
assert_eq!(prev_def_dev, default_device_after_plugging); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
// Ignore the return devices' info since we only need to print them. |
| 782 |
let _ = get_devices_info_in_scope(device_scope.clone()); |
| 783 |
println!( |
| 784 |
"Current default {:?} device is {}", |
| 785 |
device_scope, |
| 786 |
test_get_default_device(device_scope.clone()).unwrap() |
| 787 |
); |
| 788 |
|
| 789 |
let (input_device, output_device) = match device_scope { |
| 790 |
Scope::Input => ( |
| 791 |
if set_device_to_default { |
| 792 |
None // default input device. |
| 793 |
} else { |
| 794 |
Some(plugger.get_device_id()) |
| 795 |
}, |
| 796 |
None, |
| 797 |
), |
| 798 |
Scope::Output => ( |
| 799 |
None, |
| 800 |
if set_device_to_default { |
| 801 |
None // default output device. |
| 802 |
} else { |
| 803 |
Some(plugger.get_device_id()) |
| 804 |
}, |
| 805 |
), |
| 806 |
}; |
| 807 |
|
| 808 |
#[derive(Clone, PartialEq)] |
| 809 |
struct Data { |
| 810 |
changed_count: u32, |
| 811 |
states: Vec<ffi::cubeb_state>, |
| 812 |
} |
| 813 |
|
| 814 |
impl Data { |
| 815 |
fn new() -> Self { |
| 816 |
Self { |
| 817 |
changed_count: 0, |
| 818 |
states: vec![], |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
let notifier = Arc::new(Notifier::new(Data::new())); |
| 824 |
let notifier_ptr = notifier.as_ref() as *const Notifier<Data>; |
| 825 |
|
| 826 |
test_get_stream_with_device_changed_callback( |
| 827 |
"stream: test stream reinit/destroy after unplugging a device", |
| 828 |
stream_type, |
| 829 |
input_device, |
| 830 |
output_device, |
| 831 |
notifier_ptr as *mut c_void, |
| 832 |
state_callback, |
| 833 |
device_changed_callback, |
| 834 |
|stream| { |
| 835 |
assert_eq!(stream.start(), Ok(())); |
| 836 |
|
| 837 |
println!( |
| 838 |
"Stream runs on the device {} for {:?}", |
| 839 |
plugger.get_device_id(), |
| 840 |
device_scope |
| 841 |
); |
| 842 |
|
| 843 |
let dev = plugger.get_device_id(); |
| 844 |
let start_changed_count = { |
| 845 |
let guard = notifier.lock().unwrap(); |
| 846 |
assert_eq!(guard.states.last().unwrap(), &ffi::CUBEB_STATE_STARTED); |
| 847 |
guard.changed_count.clone() |
| 848 |
}; |
| 849 |
|
| 850 |
assert!(plugger.unplug().is_ok()); |
| 851 |
|
| 852 |
let changed_watcher = Watcher::new(¬ifier); |
| 853 |
|
| 854 |
if set_device_to_default { |
| 855 |
// The stream will be reinitialized if it follows the default input or output device. |
| 856 |
println!("Waiting for default device to change and reinit"); |
| 857 |
let mut data_guard = notifier.lock().unwrap(); |
| 858 |
data_guard = changed_watcher |
| 859 |
.wait_while(data_guard, |data| { |
| 860 |
data.changed_count == start_changed_count |
| 861 |
|| data.states.last().unwrap_or(&ffi::CUBEB_STATE_ERROR) |
| 862 |
!= &ffi::CUBEB_STATE_STARTED |
| 863 |
}) |
| 864 |
.unwrap(); |
| 865 |
} else if wait_up_to_ms > 0 { |
| 866 |
// stream can be dropped immediately before device-changed callback |
| 867 |
// so we only check the states if we wait for it explicitly. |
| 868 |
println!("Waiting for non-default device to enter error state"); |
| 869 |
let mut data_guard = notifier.lock().unwrap(); |
| 870 |
let (new_guard, timeout_res) = changed_watcher |
| 871 |
.wait_timeout_while(data_guard, Duration::from_millis(wait_up_to_ms), |data| { |
| 872 |
data.states.last().unwrap_or(&ffi::CUBEB_STATE_STARTED) |
| 873 |
!= &ffi::CUBEB_STATE_ERROR |
| 874 |
}) |
| 875 |
.unwrap(); |
| 876 |
assert!(!timeout_res.timed_out()); |
| 877 |
data_guard = new_guard; |
| 878 |
} |
| 879 |
|
| 880 |
println!( |
| 881 |
"Device {} for {:?} has been unplugged. The default {:?} device now is {}", |
| 882 |
dev, |
| 883 |
device_scope, |
| 884 |
device_scope, |
| 885 |
test_get_default_device(device_scope.clone()).unwrap() |
| 886 |
); |
| 887 |
|
| 888 |
println!("The stream is going to be destroyed soon"); |
| 889 |
}, |
| 890 |
); |
| 891 |
|
| 892 |
extern "C" fn state_callback( |
| 893 |
stream: *mut ffi::cubeb_stream, |
| 894 |
user_ptr: *mut c_void, |
| 895 |
state: ffi::cubeb_state, |
| 896 |
) { |
| 897 |
println!("Device change callback. user_ptr @ {:p}", user_ptr); |
| 898 |
assert!(!stream.is_null()); |
| 899 |
println!( |
| 900 |
"state: {}", |
| 901 |
match state { |
| 902 |
ffi::CUBEB_STATE_STARTED => "started", |
| 903 |
ffi::CUBEB_STATE_STOPPED => "stopped", |
| 904 |
ffi::CUBEB_STATE_DRAINED => "drained", |
| 905 |
ffi::CUBEB_STATE_ERROR => "error", |
| 906 |
_ => "unknown", |
| 907 |
} |
| 908 |
); |
| 909 |
let notifier = unsafe { &mut *(user_ptr as *mut Notifier<Data>) }; |
| 910 |
let mut data_guard = notifier.lock().unwrap(); |
| 911 |
data_guard.states.push(state); |
| 912 |
notifier.notify(data_guard); |
| 913 |
} |
| 914 |
|
| 915 |
extern "C" fn device_changed_callback(user_ptr: *mut c_void) { |
| 916 |
println!("Device change callback. user_ptr @ {:p}", user_ptr); |
| 917 |
let notifier = unsafe { &mut *(user_ptr as *mut Notifier<Data>) }; |
| 918 |
let mut data_guard = notifier.lock().unwrap(); |
| 919 |
data_guard.changed_count += 1; |
| 920 |
notifier.notify(data_guard); |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
struct Notifier<T> { |
| 925 |
value: Mutex<T>, |
| 926 |
cvar: Condvar, |
| 927 |
} |
| 928 |
|
| 929 |
impl<T> Notifier<T> { |
| 930 |
fn new(value: T) -> Self { |
| 931 |
Self { |
| 932 |
value: Mutex::new(value), |
| 933 |
cvar: Condvar::new(), |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
fn lock(&self) -> LockResult<MutexGuard<'_, T>> { |
| 938 |
self.value.lock() |
| 939 |
} |
| 940 |
|
| 941 |
fn notify(&self, _guard: MutexGuard<'_, T>) { |
| 942 |
self.cvar.notify_all(); |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
struct Watcher<T: Clone + PartialEq> { |
| 947 |
notifier: Arc<Notifier<T>>, |
| 948 |
} |
| 949 |
|
| 950 |
impl<T: Clone + PartialEq> Watcher<T> { |
| 951 |
fn new(value: &Arc<Notifier<T>>) -> Self { |
| 952 |
Self { |
| 953 |
notifier: Arc::clone(value), |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
fn lock(&self) -> LockResult<MutexGuard<'_, T>> { |
| 958 |
self.notifier.lock() |
| 959 |
} |
| 960 |
|
| 961 |
fn wait_while<'a, F>( |
| 962 |
&self, |
| 963 |
guard: MutexGuard<'a, T>, |
| 964 |
condition: F, |
| 965 |
) -> LockResult<MutexGuard<'a, T>> |
| 966 |
where |
| 967 |
F: FnMut(&mut T) -> bool, |
| 968 |
{ |
| 969 |
self.notifier.cvar.wait_while(guard, condition) |
| 970 |
} |
| 971 |
|
| 972 |
fn wait_timeout_while<'a, F>( |
| 973 |
&self, |
| 974 |
guard: MutexGuard<'a, T>, |
| 975 |
dur: Duration, |
| 976 |
condition: F, |
| 977 |
) -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> |
| 978 |
where |
| 979 |
F: FnMut(&mut T) -> bool, |
| 980 |
{ |
| 981 |
self.notifier.cvar.wait_timeout_while(guard, dur, condition) |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
fn test_get_stream_with_device_changed_callback<F>( |
| 986 |
name: &'static str, |
| 987 |
stm_type: StreamType, |
| 988 |
input_device: Option<AudioObjectID>, |
| 989 |
output_device: Option<AudioObjectID>, |
| 990 |
data: *mut c_void, |
| 991 |
state_callback: extern "C" fn(*mut ffi::cubeb_stream, *mut c_void, ffi::cubeb_state), |
| 992 |
device_changed_callback: extern "C" fn(*mut c_void), |
| 993 |
operation: F, |
| 994 |
) where |
| 995 |
F: FnOnce(&mut AudioUnitStream), |
| 996 |
{ |
| 997 |
test_get_stream_with_default_data_callback_by_type( |
| 998 |
name, |
| 999 |
stm_type, |
| 1000 |
input_device, |
| 1001 |
output_device, |
| 1002 |
state_callback, |
| 1003 |
data, |
| 1004 |
|stream| { |
| 1005 |
assert!(stream |
| 1006 |
.register_device_changed_callback(Some(device_changed_callback)) |
| 1007 |
.is_ok()); |
| 1008 |
operation(stream); |
| 1009 |
assert!(stream.register_device_changed_callback(None).is_ok()); |
| 1010 |
}, |
| 1011 |
); |
| 1012 |
} |