| 1 |
use super::utils::{ |
| 2 |
test_audiounit_get_buffer_frame_size, test_audiounit_scope_is_enabled, test_create_audiounit, |
| 3 |
test_device_channels_in_scope, test_device_in_scope, test_get_all_devices, |
| 4 |
test_get_default_audiounit, test_get_default_device, test_get_default_raw_stream, |
| 5 |
test_get_devices_in_scope, test_get_raw_context, ComponentSubType, DeviceFilter, PropertyScope, |
| 6 |
Scope, |
| 7 |
}; |
| 8 |
use super::*; |
| 9 |
|
| 10 |
// make_sized_audio_channel_layout |
| 11 |
// ------------------------------------ |
| 12 |
#[test] |
| 13 |
fn test_make_sized_audio_channel_layout() { |
| 14 |
for channels in 1..10 { |
| 15 |
let size = mem::size_of::<AudioChannelLayout>() |
| 16 |
+ (channels - 1) * mem::size_of::<AudioChannelDescription>(); |
| 17 |
let _ = make_sized_audio_channel_layout(size); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
#[test] |
| 22 |
#[should_panic] |
| 23 |
fn test_make_sized_audio_channel_layout_with_wrong_size() { |
| 24 |
// let _ = make_sized_audio_channel_layout(0); |
| 25 |
let one_channel_size = mem::size_of::<AudioChannelLayout>(); |
| 26 |
let padding_size = 10; |
| 27 |
assert_ne!(mem::size_of::<AudioChannelDescription>(), padding_size); |
| 28 |
let wrong_size = one_channel_size + padding_size; |
| 29 |
let _ = make_sized_audio_channel_layout(wrong_size); |
| 30 |
} |
| 31 |
|
| 32 |
// active_streams |
| 33 |
// update_latency_by_adding_stream |
| 34 |
// update_latency_by_removing_stream |
| 35 |
// ------------------------------------ |
| 36 |
#[test] |
| 37 |
fn test_increase_and_decrease_context_streams() { |
| 38 |
use std::thread; |
| 39 |
const STREAMS: u32 = 10; |
| 40 |
|
| 41 |
let context = AudioUnitContext::new(); |
| 42 |
let context_ptr_value = &context as *const AudioUnitContext as usize; |
| 43 |
|
| 44 |
let mut join_handles = vec![]; |
| 45 |
for i in 0..STREAMS { |
| 46 |
join_handles.push(thread::spawn(move || { |
| 47 |
let context = unsafe { &*(context_ptr_value as *const AudioUnitContext) }; |
| 48 |
|
| 49 |
context.update_latency_by_adding_stream(i) |
| 50 |
})); |
| 51 |
} |
| 52 |
let mut latencies = vec![]; |
| 53 |
for handle in join_handles { |
| 54 |
latencies.push(handle.join().unwrap()); |
| 55 |
} |
| 56 |
assert_eq!(context.active_streams(), STREAMS); |
| 57 |
check_streams(&context, STREAMS); |
| 58 |
|
| 59 |
check_latency(&context, Some(latencies[0])); |
| 60 |
for i in 0..latencies.len() - 1 { |
| 61 |
assert_eq!(latencies[i], latencies[i + 1]); |
| 62 |
} |
| 63 |
|
| 64 |
let mut join_handles = vec![]; |
| 65 |
for _ in 0..STREAMS { |
| 66 |
join_handles.push(thread::spawn(move || { |
| 67 |
let context = unsafe { &*(context_ptr_value as *const AudioUnitContext) }; |
| 68 |
context.update_latency_by_removing_stream(); |
| 69 |
})); |
| 70 |
} |
| 71 |
for handle in join_handles { |
| 72 |
let _ = handle.join(); |
| 73 |
} |
| 74 |
check_streams(&context, 0); |
| 75 |
|
| 76 |
check_latency(&context, None); |
| 77 |
} |
| 78 |
|
| 79 |
fn check_streams(context: &AudioUnitContext, number: u32) { |
| 80 |
let guard = context.latency_controller.lock().unwrap(); |
| 81 |
assert_eq!(guard.streams, number); |
| 82 |
} |
| 83 |
|
| 84 |
fn check_latency(context: &AudioUnitContext, latency: Option<u32>) { |
| 85 |
let guard = context.latency_controller.lock().unwrap(); |
| 86 |
assert_eq!(guard.latency, latency); |
| 87 |
} |
| 88 |
|
| 89 |
// make_silent |
| 90 |
// ------------------------------------ |
| 91 |
#[test] |
| 92 |
fn test_make_silent() { |
| 93 |
let mut array = allocate_array::<u32>(10); |
| 94 |
for data in array.iter_mut() { |
| 95 |
*data = 0xFFFF; |
| 96 |
} |
| 97 |
|
| 98 |
let mut buffer = AudioBuffer::default(); |
| 99 |
buffer.mData = array.as_mut_ptr() as *mut c_void; |
| 100 |
buffer.mDataByteSize = (array.len() * mem::size_of::<u32>()) as u32; |
| 101 |
buffer.mNumberChannels = 1; |
| 102 |
|
| 103 |
audiounit_make_silent(&mut buffer); |
| 104 |
for data in array { |
| 105 |
assert_eq!(data, 0); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// minimum_resampling_input_frames |
| 110 |
// ------------------------------------ |
| 111 |
#[test] |
| 112 |
fn test_minimum_resampling_input_frames() { |
| 113 |
let input_rate = 48000_f64; |
| 114 |
let output_rate = 44100_f64; |
| 115 |
|
| 116 |
let frames = 100; |
| 117 |
let times = input_rate / output_rate; |
| 118 |
let expected = (frames as f64 * times).ceil() as usize; |
| 119 |
|
| 120 |
assert_eq!( |
| 121 |
minimum_resampling_input_frames(input_rate, output_rate, frames), |
| 122 |
expected |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
#[should_panic] |
| 128 |
fn test_minimum_resampling_input_frames_zero_input_rate() { |
| 129 |
minimum_resampling_input_frames(0_f64, 44100_f64, 1); |
| 130 |
} |
| 131 |
|
| 132 |
#[test] |
| 133 |
#[should_panic] |
| 134 |
fn test_minimum_resampling_input_frames_zero_output_rate() { |
| 135 |
minimum_resampling_input_frames(48000_f64, 0_f64, 1); |
| 136 |
} |
| 137 |
|
| 138 |
#[test] |
| 139 |
fn test_minimum_resampling_input_frames_equal_input_output_rate() { |
| 140 |
let frames = 100; |
| 141 |
assert_eq!( |
| 142 |
minimum_resampling_input_frames(44100_f64, 44100_f64, frames), |
| 143 |
frames |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
// create_device_info |
| 148 |
// ------------------------------------ |
| 149 |
#[test] |
| 150 |
fn test_create_device_info_from_unknown_input_device() { |
| 151 |
if let Some(default_device_id) = test_get_default_device(Scope::Input) { |
| 152 |
let default_device = |
| 153 |
run_serially(|| create_device_info(kAudioObjectUnknown, DeviceType::INPUT).unwrap()); |
| 154 |
assert_eq!(default_device.id, default_device_id); |
| 155 |
assert_eq!( |
| 156 |
default_device.flags, |
| 157 |
device_flags::DEV_INPUT | device_flags::DEV_SELECTED_DEFAULT |
| 158 |
); |
| 159 |
} else { |
| 160 |
println!("No input device to perform test."); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
#[test] |
| 165 |
fn test_create_device_info_from_unknown_output_device() { |
| 166 |
if let Some(default_device_id) = test_get_default_device(Scope::Output) { |
| 167 |
let default_device = |
| 168 |
run_serially(|| create_device_info(kAudioObjectUnknown, DeviceType::OUTPUT)).unwrap(); |
| 169 |
assert_eq!(default_device.id, default_device_id); |
| 170 |
assert_eq!( |
| 171 |
default_device.flags, |
| 172 |
device_flags::DEV_OUTPUT | device_flags::DEV_SELECTED_DEFAULT |
| 173 |
); |
| 174 |
} else { |
| 175 |
println!("No output device to perform test."); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
#[should_panic] |
| 181 |
fn test_set_device_info_to_system_input_device() { |
| 182 |
let _device = run_serially_forward_panics(|| { |
| 183 |
create_device_info(kAudioObjectSystemObject, DeviceType::INPUT) |
| 184 |
}); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
#[should_panic] |
| 189 |
fn test_set_device_info_to_system_output_device() { |
| 190 |
let _device = run_serially_forward_panics(|| { |
| 191 |
create_device_info(kAudioObjectSystemObject, DeviceType::OUTPUT) |
| 192 |
}); |
| 193 |
} |
| 194 |
|
| 195 |
// FIXME: Is it ok to set input device to a nonexistent device ? |
| 196 |
#[ignore] |
| 197 |
#[test] |
| 198 |
#[should_panic] |
| 199 |
fn test_set_device_info_to_nonexistent_input_device() { |
| 200 |
let nonexistent_id = std::u32::MAX; |
| 201 |
let _device = |
| 202 |
run_serially_forward_panics(|| create_device_info(nonexistent_id, DeviceType::INPUT)); |
| 203 |
} |
| 204 |
|
| 205 |
// FIXME: Is it ok to set output device to a nonexistent device ? |
| 206 |
#[ignore] |
| 207 |
#[test] |
| 208 |
#[should_panic] |
| 209 |
fn test_set_device_info_to_nonexistent_output_device() { |
| 210 |
let nonexistent_id = std::u32::MAX; |
| 211 |
let _device = |
| 212 |
run_serially_forward_panics(|| create_device_info(nonexistent_id, DeviceType::OUTPUT)); |
| 213 |
} |
| 214 |
|
| 215 |
// add_listener (for default output device) |
| 216 |
// ------------------------------------ |
| 217 |
#[test] |
| 218 |
fn test_add_listener_unknown_device() { |
| 219 |
extern "C" fn callback( |
| 220 |
_id: AudioObjectID, |
| 221 |
_number_of_addresses: u32, |
| 222 |
_addresses: *const AudioObjectPropertyAddress, |
| 223 |
_data: *mut c_void, |
| 224 |
) -> OSStatus { |
| 225 |
assert!(false, "Should not be called."); |
| 226 |
kAudioHardwareUnspecifiedError as OSStatus |
| 227 |
} |
| 228 |
|
| 229 |
test_get_default_raw_stream(|stream| { |
| 230 |
let listener = device_property_listener::new( |
| 231 |
kAudioObjectUnknown, |
| 232 |
get_property_address( |
| 233 |
Property::HardwareDefaultOutputDevice, |
| 234 |
DeviceType::INPUT | DeviceType::OUTPUT, |
| 235 |
), |
| 236 |
callback, |
| 237 |
); |
| 238 |
let res = stream |
| 239 |
.queue |
| 240 |
.run_sync(|| stream.add_device_listener(&listener)) |
| 241 |
.unwrap(); |
| 242 |
assert_eq!(res, kAudioHardwareBadObjectError as OSStatus); |
| 243 |
}); |
| 244 |
} |
| 245 |
|
| 246 |
// remove_listener (for default output device) |
| 247 |
// ------------------------------------ |
| 248 |
#[test] |
| 249 |
fn test_add_listener_then_remove_system_device() { |
| 250 |
extern "C" fn callback( |
| 251 |
_id: AudioObjectID, |
| 252 |
_number_of_addresses: u32, |
| 253 |
_addresses: *const AudioObjectPropertyAddress, |
| 254 |
_data: *mut c_void, |
| 255 |
) -> OSStatus { |
| 256 |
assert!(false, "Should not be called."); |
| 257 |
kAudioHardwareUnspecifiedError as OSStatus |
| 258 |
} |
| 259 |
|
| 260 |
test_get_default_raw_stream(|stream| { |
| 261 |
let listener = device_property_listener::new( |
| 262 |
kAudioObjectSystemObject, |
| 263 |
get_property_address( |
| 264 |
Property::HardwareDefaultOutputDevice, |
| 265 |
DeviceType::INPUT | DeviceType::OUTPUT, |
| 266 |
), |
| 267 |
callback, |
| 268 |
); |
| 269 |
let res = stream |
| 270 |
.queue |
| 271 |
.run_sync(|| stream.add_device_listener(&listener)) |
| 272 |
.unwrap(); |
| 273 |
assert_eq!(res, NO_ERR); |
| 274 |
let res = stream |
| 275 |
.queue |
| 276 |
.run_sync(|| stream.remove_device_listener(&listener)) |
| 277 |
.unwrap(); |
| 278 |
assert_eq!(res, NO_ERR); |
| 279 |
}); |
| 280 |
} |
| 281 |
|
| 282 |
#[test] |
| 283 |
fn test_remove_listener_without_adding_any_listener_before_system_device() { |
| 284 |
extern "C" fn callback( |
| 285 |
_id: AudioObjectID, |
| 286 |
_number_of_addresses: u32, |
| 287 |
_addresses: *const AudioObjectPropertyAddress, |
| 288 |
_data: *mut c_void, |
| 289 |
) -> OSStatus { |
| 290 |
assert!(false, "Should not be called."); |
| 291 |
kAudioHardwareUnspecifiedError as OSStatus |
| 292 |
} |
| 293 |
|
| 294 |
test_get_default_raw_stream(|stream| { |
| 295 |
let listener = device_property_listener::new( |
| 296 |
kAudioObjectSystemObject, |
| 297 |
get_property_address( |
| 298 |
Property::HardwareDefaultOutputDevice, |
| 299 |
DeviceType::INPUT | DeviceType::OUTPUT, |
| 300 |
), |
| 301 |
callback, |
| 302 |
); |
| 303 |
let res = stream |
| 304 |
.queue |
| 305 |
.run_sync(|| stream.remove_device_listener(&listener)) |
| 306 |
.unwrap(); |
| 307 |
assert_eq!(res, NO_ERR); |
| 308 |
}); |
| 309 |
} |
| 310 |
|
| 311 |
#[test] |
| 312 |
fn test_remove_listener_unknown_device() { |
| 313 |
extern "C" fn callback( |
| 314 |
_id: AudioObjectID, |
| 315 |
_number_of_addresses: u32, |
| 316 |
_addresses: *const AudioObjectPropertyAddress, |
| 317 |
_data: *mut c_void, |
| 318 |
) -> OSStatus { |
| 319 |
assert!(false, "Should not be called."); |
| 320 |
kAudioHardwareUnspecifiedError as OSStatus |
| 321 |
} |
| 322 |
|
| 323 |
test_get_default_raw_stream(|stream| { |
| 324 |
let listener = device_property_listener::new( |
| 325 |
kAudioObjectUnknown, |
| 326 |
get_property_address( |
| 327 |
Property::HardwareDefaultOutputDevice, |
| 328 |
DeviceType::INPUT | DeviceType::OUTPUT, |
| 329 |
), |
| 330 |
callback, |
| 331 |
); |
| 332 |
let res = stream |
| 333 |
.queue |
| 334 |
.run_sync(|| stream.remove_device_listener(&listener)) |
| 335 |
.unwrap(); |
| 336 |
assert_eq!(res, kAudioHardwareBadObjectError as OSStatus); |
| 337 |
}); |
| 338 |
} |
| 339 |
|
| 340 |
// get_default_device_id |
| 341 |
// ------------------------------------ |
| 342 |
#[test] |
| 343 |
fn test_get_default_device_id() { |
| 344 |
if test_get_default_device(Scope::Input).is_some() { |
| 345 |
assert_ne!( |
| 346 |
run_serially(|| get_default_device_id(DeviceType::INPUT)).unwrap(), |
| 347 |
kAudioObjectUnknown, |
| 348 |
); |
| 349 |
} |
| 350 |
|
| 351 |
if test_get_default_device(Scope::Output).is_some() { |
| 352 |
assert_ne!( |
| 353 |
run_serially(|| get_default_device_id(DeviceType::OUTPUT)).unwrap(), |
| 354 |
kAudioObjectUnknown, |
| 355 |
); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
#[test] |
| 360 |
#[should_panic] |
| 361 |
fn test_get_default_device_id_with_unknown_type() { |
| 362 |
assert!(run_serially_forward_panics(|| get_default_device_id(DeviceType::UNKNOWN)).is_err()); |
| 363 |
} |
| 364 |
|
| 365 |
#[test] |
| 366 |
#[should_panic] |
| 367 |
fn test_get_default_device_id_with_inout_type() { |
| 368 |
assert!(run_serially_forward_panics(|| get_default_device_id( |
| 369 |
DeviceType::INPUT | DeviceType::OUTPUT |
| 370 |
)) |
| 371 |
.is_err()); |
| 372 |
} |
| 373 |
|
| 374 |
// convert_channel_layout |
| 375 |
// ------------------------------------ |
| 376 |
#[test] |
| 377 |
fn test_convert_channel_layout() { |
| 378 |
let pairs = [ |
| 379 |
( |
| 380 |
vec![kAudioChannelLabel_Mono], |
| 381 |
vec![mixer::Channel::FrontCenter], |
| 382 |
), |
| 383 |
( |
| 384 |
vec![kAudioChannelLabel_Mono, kAudioChannelLabel_LFEScreen], |
| 385 |
vec![mixer::Channel::FrontCenter, mixer::Channel::LowFrequency], |
| 386 |
), |
| 387 |
( |
| 388 |
vec![kAudioChannelLabel_Left, kAudioChannelLabel_Right], |
| 389 |
vec![mixer::Channel::FrontLeft, mixer::Channel::FrontRight], |
| 390 |
), |
| 391 |
( |
| 392 |
vec![ |
| 393 |
kAudioChannelLabel_Left, |
| 394 |
kAudioChannelLabel_Right, |
| 395 |
kAudioChannelLabel_Unknown, |
| 396 |
], |
| 397 |
vec![ |
| 398 |
mixer::Channel::FrontLeft, |
| 399 |
mixer::Channel::FrontRight, |
| 400 |
mixer::Channel::Discrete, |
| 401 |
], |
| 402 |
), |
| 403 |
( |
| 404 |
vec![ |
| 405 |
kAudioChannelLabel_Left, |
| 406 |
kAudioChannelLabel_Right, |
| 407 |
kAudioChannelLabel_Unused, |
| 408 |
], |
| 409 |
vec![ |
| 410 |
mixer::Channel::FrontLeft, |
| 411 |
mixer::Channel::FrontRight, |
| 412 |
mixer::Channel::Silence, |
| 413 |
], |
| 414 |
), |
| 415 |
( |
| 416 |
vec![ |
| 417 |
kAudioChannelLabel_Left, |
| 418 |
kAudioChannelLabel_Right, |
| 419 |
kAudioChannelLabel_ForeignLanguage, |
| 420 |
], |
| 421 |
vec![ |
| 422 |
mixer::Channel::FrontLeft, |
| 423 |
mixer::Channel::FrontRight, |
| 424 |
mixer::Channel::Silence, |
| 425 |
], |
| 426 |
), |
| 427 |
// The SMPTE layouts. |
| 428 |
( |
| 429 |
vec![ |
| 430 |
kAudioChannelLabel_Left, |
| 431 |
kAudioChannelLabel_Right, |
| 432 |
kAudioChannelLabel_LFEScreen, |
| 433 |
], |
| 434 |
vec![ |
| 435 |
mixer::Channel::FrontLeft, |
| 436 |
mixer::Channel::FrontRight, |
| 437 |
mixer::Channel::LowFrequency, |
| 438 |
], |
| 439 |
), |
| 440 |
( |
| 441 |
vec![ |
| 442 |
kAudioChannelLabel_Left, |
| 443 |
kAudioChannelLabel_Right, |
| 444 |
kAudioChannelLabel_Center, |
| 445 |
], |
| 446 |
vec![ |
| 447 |
mixer::Channel::FrontLeft, |
| 448 |
mixer::Channel::FrontRight, |
| 449 |
mixer::Channel::FrontCenter, |
| 450 |
], |
| 451 |
), |
| 452 |
( |
| 453 |
vec![ |
| 454 |
kAudioChannelLabel_Left, |
| 455 |
kAudioChannelLabel_Right, |
| 456 |
kAudioChannelLabel_Center, |
| 457 |
kAudioChannelLabel_LFEScreen, |
| 458 |
], |
| 459 |
vec![ |
| 460 |
mixer::Channel::FrontLeft, |
| 461 |
mixer::Channel::FrontRight, |
| 462 |
mixer::Channel::FrontCenter, |
| 463 |
mixer::Channel::LowFrequency, |
| 464 |
], |
| 465 |
), |
| 466 |
( |
| 467 |
vec![ |
| 468 |
kAudioChannelLabel_Left, |
| 469 |
kAudioChannelLabel_Right, |
| 470 |
kAudioChannelLabel_CenterSurround, |
| 471 |
], |
| 472 |
vec![ |
| 473 |
mixer::Channel::FrontLeft, |
| 474 |
mixer::Channel::FrontRight, |
| 475 |
mixer::Channel::BackCenter, |
| 476 |
], |
| 477 |
), |
| 478 |
( |
| 479 |
vec![ |
| 480 |
kAudioChannelLabel_Left, |
| 481 |
kAudioChannelLabel_Right, |
| 482 |
kAudioChannelLabel_CenterSurround, |
| 483 |
kAudioChannelLabel_LFEScreen, |
| 484 |
], |
| 485 |
vec![ |
| 486 |
mixer::Channel::FrontLeft, |
| 487 |
mixer::Channel::FrontRight, |
| 488 |
mixer::Channel::BackCenter, |
| 489 |
mixer::Channel::LowFrequency, |
| 490 |
], |
| 491 |
), |
| 492 |
( |
| 493 |
vec![ |
| 494 |
kAudioChannelLabel_Left, |
| 495 |
kAudioChannelLabel_Right, |
| 496 |
kAudioChannelLabel_Center, |
| 497 |
kAudioChannelLabel_CenterSurround, |
| 498 |
], |
| 499 |
vec![ |
| 500 |
mixer::Channel::FrontLeft, |
| 501 |
mixer::Channel::FrontRight, |
| 502 |
mixer::Channel::FrontCenter, |
| 503 |
mixer::Channel::BackCenter, |
| 504 |
], |
| 505 |
), |
| 506 |
( |
| 507 |
vec![ |
| 508 |
kAudioChannelLabel_Left, |
| 509 |
kAudioChannelLabel_Right, |
| 510 |
kAudioChannelLabel_Center, |
| 511 |
kAudioChannelLabel_CenterSurround, |
| 512 |
kAudioChannelLabel_LFEScreen, |
| 513 |
], |
| 514 |
vec![ |
| 515 |
mixer::Channel::FrontLeft, |
| 516 |
mixer::Channel::FrontRight, |
| 517 |
mixer::Channel::FrontCenter, |
| 518 |
mixer::Channel::BackCenter, |
| 519 |
mixer::Channel::LowFrequency, |
| 520 |
], |
| 521 |
), |
| 522 |
( |
| 523 |
vec![ |
| 524 |
kAudioChannelLabel_Left, |
| 525 |
kAudioChannelLabel_Right, |
| 526 |
kAudioChannelLabel_LeftSurroundDirect, |
| 527 |
kAudioChannelLabel_RightSurroundDirect, |
| 528 |
], |
| 529 |
vec![ |
| 530 |
mixer::Channel::FrontLeft, |
| 531 |
mixer::Channel::FrontRight, |
| 532 |
mixer::Channel::SideLeft, |
| 533 |
mixer::Channel::SideRight, |
| 534 |
], |
| 535 |
), |
| 536 |
( |
| 537 |
vec![ |
| 538 |
kAudioChannelLabel_Left, |
| 539 |
kAudioChannelLabel_Right, |
| 540 |
kAudioChannelLabel_LeftSurroundDirect, |
| 541 |
kAudioChannelLabel_RightSurroundDirect, |
| 542 |
kAudioChannelLabel_LFEScreen, |
| 543 |
], |
| 544 |
vec![ |
| 545 |
mixer::Channel::FrontLeft, |
| 546 |
mixer::Channel::FrontRight, |
| 547 |
mixer::Channel::SideLeft, |
| 548 |
mixer::Channel::SideRight, |
| 549 |
mixer::Channel::LowFrequency, |
| 550 |
], |
| 551 |
), |
| 552 |
( |
| 553 |
vec![ |
| 554 |
kAudioChannelLabel_Left, |
| 555 |
kAudioChannelLabel_Right, |
| 556 |
kAudioChannelLabel_LeftSurround, |
| 557 |
kAudioChannelLabel_RightSurround, |
| 558 |
], |
| 559 |
vec![ |
| 560 |
mixer::Channel::FrontLeft, |
| 561 |
mixer::Channel::FrontRight, |
| 562 |
mixer::Channel::BackLeft, |
| 563 |
mixer::Channel::BackRight, |
| 564 |
], |
| 565 |
), |
| 566 |
( |
| 567 |
vec![ |
| 568 |
kAudioChannelLabel_Left, |
| 569 |
kAudioChannelLabel_Right, |
| 570 |
kAudioChannelLabel_LeftSurround, |
| 571 |
kAudioChannelLabel_RightSurround, |
| 572 |
kAudioChannelLabel_LFEScreen, |
| 573 |
], |
| 574 |
vec![ |
| 575 |
mixer::Channel::FrontLeft, |
| 576 |
mixer::Channel::FrontRight, |
| 577 |
mixer::Channel::BackLeft, |
| 578 |
mixer::Channel::BackRight, |
| 579 |
mixer::Channel::LowFrequency, |
| 580 |
], |
| 581 |
), |
| 582 |
( |
| 583 |
vec![ |
| 584 |
kAudioChannelLabel_Left, |
| 585 |
kAudioChannelLabel_Right, |
| 586 |
kAudioChannelLabel_Center, |
| 587 |
kAudioChannelLabel_LeftSurroundDirect, |
| 588 |
kAudioChannelLabel_RightSurroundDirect, |
| 589 |
], |
| 590 |
vec![ |
| 591 |
mixer::Channel::FrontLeft, |
| 592 |
mixer::Channel::FrontRight, |
| 593 |
mixer::Channel::FrontCenter, |
| 594 |
mixer::Channel::SideLeft, |
| 595 |
mixer::Channel::SideRight, |
| 596 |
], |
| 597 |
), |
| 598 |
( |
| 599 |
vec![ |
| 600 |
kAudioChannelLabel_Left, |
| 601 |
kAudioChannelLabel_Right, |
| 602 |
kAudioChannelLabel_Center, |
| 603 |
kAudioChannelLabel_LeftSurroundDirect, |
| 604 |
kAudioChannelLabel_RightSurroundDirect, |
| 605 |
kAudioChannelLabel_LFEScreen, |
| 606 |
], |
| 607 |
vec![ |
| 608 |
mixer::Channel::FrontLeft, |
| 609 |
mixer::Channel::FrontRight, |
| 610 |
mixer::Channel::FrontCenter, |
| 611 |
mixer::Channel::SideLeft, |
| 612 |
mixer::Channel::SideRight, |
| 613 |
mixer::Channel::LowFrequency, |
| 614 |
], |
| 615 |
), |
| 616 |
( |
| 617 |
vec![ |
| 618 |
kAudioChannelLabel_Left, |
| 619 |
kAudioChannelLabel_Right, |
| 620 |
kAudioChannelLabel_LeftSurround, |
| 621 |
kAudioChannelLabel_RightSurround, |
| 622 |
kAudioChannelLabel_Center, |
| 623 |
], |
| 624 |
vec![ |
| 625 |
mixer::Channel::FrontLeft, |
| 626 |
mixer::Channel::FrontRight, |
| 627 |
mixer::Channel::BackLeft, |
| 628 |
mixer::Channel::BackRight, |
| 629 |
mixer::Channel::FrontCenter, |
| 630 |
], |
| 631 |
), |
| 632 |
( |
| 633 |
vec![ |
| 634 |
kAudioChannelLabel_Left, |
| 635 |
kAudioChannelLabel_Right, |
| 636 |
kAudioChannelLabel_LeftSurround, |
| 637 |
kAudioChannelLabel_RightSurround, |
| 638 |
kAudioChannelLabel_Center, |
| 639 |
kAudioChannelLabel_LFEScreen, |
| 640 |
], |
| 641 |
vec![ |
| 642 |
mixer::Channel::FrontLeft, |
| 643 |
mixer::Channel::FrontRight, |
| 644 |
mixer::Channel::BackLeft, |
| 645 |
mixer::Channel::BackRight, |
| 646 |
mixer::Channel::FrontCenter, |
| 647 |
mixer::Channel::LowFrequency, |
| 648 |
], |
| 649 |
), |
| 650 |
( |
| 651 |
vec![ |
| 652 |
kAudioChannelLabel_Left, |
| 653 |
kAudioChannelLabel_Right, |
| 654 |
kAudioChannelLabel_Center, |
| 655 |
kAudioChannelLabel_LFEScreen, |
| 656 |
kAudioChannelLabel_CenterSurround, |
| 657 |
kAudioChannelLabel_LeftSurroundDirect, |
| 658 |
kAudioChannelLabel_RightSurroundDirect, |
| 659 |
], |
| 660 |
vec![ |
| 661 |
mixer::Channel::FrontLeft, |
| 662 |
mixer::Channel::FrontRight, |
| 663 |
mixer::Channel::FrontCenter, |
| 664 |
mixer::Channel::LowFrequency, |
| 665 |
mixer::Channel::BackCenter, |
| 666 |
mixer::Channel::SideLeft, |
| 667 |
mixer::Channel::SideRight, |
| 668 |
], |
| 669 |
), |
| 670 |
( |
| 671 |
vec![ |
| 672 |
kAudioChannelLabel_Left, |
| 673 |
kAudioChannelLabel_Right, |
| 674 |
kAudioChannelLabel_Center, |
| 675 |
kAudioChannelLabel_LFEScreen, |
| 676 |
kAudioChannelLabel_LeftSurround, |
| 677 |
kAudioChannelLabel_RightSurround, |
| 678 |
kAudioChannelLabel_LeftSurroundDirect, |
| 679 |
kAudioChannelLabel_RightSurroundDirect, |
| 680 |
], |
| 681 |
vec![ |
| 682 |
mixer::Channel::FrontLeft, |
| 683 |
mixer::Channel::FrontRight, |
| 684 |
mixer::Channel::FrontCenter, |
| 685 |
mixer::Channel::LowFrequency, |
| 686 |
mixer::Channel::BackLeft, |
| 687 |
mixer::Channel::BackRight, |
| 688 |
mixer::Channel::SideLeft, |
| 689 |
mixer::Channel::SideRight, |
| 690 |
], |
| 691 |
), |
| 692 |
]; |
| 693 |
|
| 694 |
const MAX_CHANNELS: usize = 10; |
| 695 |
// A Rust mapping structure of the AudioChannelLayout with MAX_CHANNELS channels |
| 696 |
// https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.13.sdk/System/Library/Frameworks/CoreAudio.framework/Versions/A/Headers/CoreAudioTypes.h#L1332 |
| 697 |
#[repr(C)] |
| 698 |
struct TestLayout { |
| 699 |
tag: AudioChannelLayoutTag, |
| 700 |
map: AudioChannelBitmap, |
| 701 |
number_channel_descriptions: UInt32, |
| 702 |
channel_descriptions: [AudioChannelDescription; MAX_CHANNELS], |
| 703 |
} |
| 704 |
|
| 705 |
impl Default for TestLayout { |
| 706 |
fn default() -> Self { |
| 707 |
Self { |
| 708 |
tag: AudioChannelLayoutTag::default(), |
| 709 |
map: AudioChannelBitmap::default(), |
| 710 |
number_channel_descriptions: UInt32::default(), |
| 711 |
channel_descriptions: [AudioChannelDescription::default(); MAX_CHANNELS], |
| 712 |
} |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
let mut layout = TestLayout::default(); |
| 717 |
layout.tag = kAudioChannelLayoutTag_UseChannelDescriptions; |
| 718 |
|
| 719 |
for (labels, expected_layout) in pairs.iter() { |
| 720 |
assert!(labels.len() <= MAX_CHANNELS); |
| 721 |
layout.number_channel_descriptions = labels.len() as u32; |
| 722 |
for (idx, label) in labels.iter().enumerate() { |
| 723 |
layout.channel_descriptions[idx].mChannelLabel = *label; |
| 724 |
} |
| 725 |
let layout_ref = unsafe { &(*(&layout as *const TestLayout as *const AudioChannelLayout)) }; |
| 726 |
assert_eq!( |
| 727 |
&audiounit_convert_channel_layout(layout_ref).unwrap(), |
| 728 |
expected_layout |
| 729 |
); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
// get_preferred_channel_layout |
| 734 |
// ------------------------------------ |
| 735 |
#[test] |
| 736 |
fn test_get_preferred_channel_layout_output() { |
| 737 |
match test_get_default_audiounit(Scope::Output) { |
| 738 |
Some(unit) => assert!(!run_serially(|| audiounit_get_preferred_channel_layout( |
| 739 |
unit.get_inner() |
| 740 |
)) |
| 741 |
.unwrap() |
| 742 |
.is_empty()), |
| 743 |
None => println!("No output audiounit for test."), |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
// get_current_channel_layout |
| 748 |
// ------------------------------------ |
| 749 |
#[test] |
| 750 |
fn test_get_current_channel_layout_output() { |
| 751 |
match test_get_default_audiounit(Scope::Output) { |
| 752 |
Some(unit) => { |
| 753 |
assert!( |
| 754 |
!run_serially_forward_panics(|| audiounit_get_current_channel_layout( |
| 755 |
unit.get_inner() |
| 756 |
)) |
| 757 |
.unwrap() |
| 758 |
.is_empty() |
| 759 |
) |
| 760 |
} |
| 761 |
None => println!("No output audiounit for test."), |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
// create_stream_description |
| 766 |
// ------------------------------------ |
| 767 |
#[test] |
| 768 |
fn test_create_stream_description() { |
| 769 |
let mut channels = 0; |
| 770 |
for (bits, format, flags) in [ |
| 771 |
( |
| 772 |
16_u32, |
| 773 |
ffi::CUBEB_SAMPLE_S16LE, |
| 774 |
kAudioFormatFlagIsSignedInteger, |
| 775 |
), |
| 776 |
( |
| 777 |
16_u32, |
| 778 |
ffi::CUBEB_SAMPLE_S16BE, |
| 779 |
kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsBigEndian, |
| 780 |
), |
| 781 |
(32_u32, ffi::CUBEB_SAMPLE_FLOAT32LE, kAudioFormatFlagIsFloat), |
| 782 |
( |
| 783 |
32_u32, |
| 784 |
ffi::CUBEB_SAMPLE_FLOAT32BE, |
| 785 |
kAudioFormatFlagIsFloat | kAudioFormatFlagIsBigEndian, |
| 786 |
), |
| 787 |
] |
| 788 |
.iter() |
| 789 |
{ |
| 790 |
let bytes = bits / 8; |
| 791 |
channels += 1; |
| 792 |
|
| 793 |
let mut raw = ffi::cubeb_stream_params::default(); |
| 794 |
raw.format = *format; |
| 795 |
raw.rate = 48_000; |
| 796 |
raw.channels = channels; |
| 797 |
raw.layout = ffi::CUBEB_LAYOUT_UNDEFINED; |
| 798 |
raw.prefs = ffi::CUBEB_STREAM_PREF_NONE; |
| 799 |
let params = StreamParams::from(raw); |
| 800 |
let description = create_stream_description(¶ms).unwrap(); |
| 801 |
assert_eq!(description.mFormatID, kAudioFormatLinearPCM); |
| 802 |
assert_eq!( |
| 803 |
description.mFormatFlags, |
| 804 |
flags | kLinearPCMFormatFlagIsPacked |
| 805 |
); |
| 806 |
assert_eq!(description.mSampleRate as u32, raw.rate); |
| 807 |
assert_eq!(description.mChannelsPerFrame, raw.channels); |
| 808 |
assert_eq!(description.mBytesPerFrame, bytes * raw.channels); |
| 809 |
assert_eq!(description.mFramesPerPacket, 1); |
| 810 |
assert_eq!(description.mBytesPerPacket, bytes * raw.channels); |
| 811 |
assert_eq!(description.mReserved, 0); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
// create_blank_audiounit |
| 816 |
// ------------------------------------ |
| 817 |
#[test] |
| 818 |
fn test_create_blank_audiounit() { |
| 819 |
let unit = create_blank_audiounit().unwrap(); |
| 820 |
assert!(!unit.is_null()); |
| 821 |
// Destroy the AudioUnit |
| 822 |
unsafe { |
| 823 |
AudioUnitUninitialize(unit); |
| 824 |
AudioComponentInstanceDispose(unit); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
// enable_audiounit_scope |
| 829 |
// ------------------------------------ |
| 830 |
#[test] |
| 831 |
fn test_enable_audiounit_scope() { |
| 832 |
// It's ok to enable and disable the scopes of input or output |
| 833 |
// for the unit whose subtype is kAudioUnitSubType_HALOutput |
| 834 |
// even when there is no available input or output devices. |
| 835 |
if let Some(unit) = test_create_audiounit(ComponentSubType::HALOutput) { |
| 836 |
assert!(run_serially_forward_panics(|| enable_audiounit_scope( |
| 837 |
unit.get_inner(), |
| 838 |
DeviceType::OUTPUT, |
| 839 |
true |
| 840 |
)) |
| 841 |
.is_ok()); |
| 842 |
assert!(run_serially_forward_panics(|| enable_audiounit_scope( |
| 843 |
unit.get_inner(), |
| 844 |
DeviceType::OUTPUT, |
| 845 |
false |
| 846 |
)) |
| 847 |
.is_ok()); |
| 848 |
assert!(run_serially_forward_panics(|| enable_audiounit_scope( |
| 849 |
unit.get_inner(), |
| 850 |
DeviceType::INPUT, |
| 851 |
true |
| 852 |
)) |
| 853 |
.is_ok()); |
| 854 |
assert!(run_serially_forward_panics(|| enable_audiounit_scope( |
| 855 |
unit.get_inner(), |
| 856 |
DeviceType::INPUT, |
| 857 |
false |
| 858 |
)) |
| 859 |
.is_ok()); |
| 860 |
} else { |
| 861 |
println!("No audiounit to perform test."); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
#[test] |
| 866 |
fn test_enable_audiounit_scope_for_default_output_unit() { |
| 867 |
if let Some(unit) = test_create_audiounit(ComponentSubType::DefaultOutput) { |
| 868 |
assert_eq!( |
| 869 |
run_serially(|| enable_audiounit_scope(unit.get_inner(), DeviceType::OUTPUT, true)) |
| 870 |
.unwrap_err(), |
| 871 |
kAudioUnitErr_InvalidProperty |
| 872 |
); |
| 873 |
assert_eq!( |
| 874 |
run_serially(|| enable_audiounit_scope(unit.get_inner(), DeviceType::OUTPUT, false)) |
| 875 |
.unwrap_err(), |
| 876 |
kAudioUnitErr_InvalidProperty |
| 877 |
); |
| 878 |
assert_eq!( |
| 879 |
run_serially(|| enable_audiounit_scope(unit.get_inner(), DeviceType::INPUT, true)) |
| 880 |
.unwrap_err(), |
| 881 |
kAudioUnitErr_InvalidProperty |
| 882 |
); |
| 883 |
assert_eq!( |
| 884 |
run_serially(|| enable_audiounit_scope(unit.get_inner(), DeviceType::INPUT, false)) |
| 885 |
.unwrap_err(), |
| 886 |
kAudioUnitErr_InvalidProperty |
| 887 |
); |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
#[test] |
| 892 |
#[should_panic] |
| 893 |
fn test_enable_audiounit_scope_with_null_unit() { |
| 894 |
let unit: AudioUnit = ptr::null_mut(); |
| 895 |
assert!( |
| 896 |
run_serially_forward_panics(|| enable_audiounit_scope(unit, DeviceType::INPUT, false)) |
| 897 |
.is_err() |
| 898 |
); |
| 899 |
} |
| 900 |
|
| 901 |
// create_audiounit |
| 902 |
// ------------------------------------ |
| 903 |
#[test] |
| 904 |
fn test_for_create_audiounit() { |
| 905 |
let flags_list = [device_flags::DEV_INPUT, device_flags::DEV_OUTPUT]; |
| 906 |
|
| 907 |
let default_input = test_get_default_device(Scope::Input); |
| 908 |
let default_output = test_get_default_device(Scope::Output); |
| 909 |
|
| 910 |
for flags in flags_list.iter() { |
| 911 |
let mut device = device_info::default(); |
| 912 |
device.flags |= *flags; |
| 913 |
|
| 914 |
// Check the output scope is enabled. |
| 915 |
if device.flags.contains(device_flags::DEV_OUTPUT) && default_output.is_some() { |
| 916 |
device.id = default_output.unwrap(); |
| 917 |
let unit = run_serially(|| create_audiounit(&device).unwrap()); |
| 918 |
assert!(!unit.is_null()); |
| 919 |
assert!(test_audiounit_scope_is_enabled(unit, Scope::Output)); |
| 920 |
|
| 921 |
// Destroy the AudioUnit. |
| 922 |
run_serially(|| unsafe { |
| 923 |
AudioUnitUninitialize(unit); |
| 924 |
AudioComponentInstanceDispose(unit); |
| 925 |
}); |
| 926 |
} |
| 927 |
|
| 928 |
// Check the input scope is enabled. |
| 929 |
if device.flags.contains(device_flags::DEV_INPUT) && default_input.is_some() { |
| 930 |
let device_id = default_input.unwrap(); |
| 931 |
device.id = device_id; |
| 932 |
let unit = run_serially(|| create_audiounit(&device).unwrap()); |
| 933 |
assert!(!unit.is_null()); |
| 934 |
assert!(test_audiounit_scope_is_enabled(unit, Scope::Input)); |
| 935 |
// Destroy the AudioUnit. |
| 936 |
run_serially(|| unsafe { |
| 937 |
AudioUnitUninitialize(unit); |
| 938 |
AudioComponentInstanceDispose(unit); |
| 939 |
}); |
| 940 |
} |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
#[test] |
| 945 |
#[should_panic] |
| 946 |
fn test_create_audiounit_with_unknown_scope() { |
| 947 |
let device = device_info::default(); |
| 948 |
let _unit = run_serially_forward_panics(|| create_audiounit(&device)); |
| 949 |
} |
| 950 |
|
| 951 |
// set_buffer_size_sync |
| 952 |
// ------------------------------------ |
| 953 |
#[test] |
| 954 |
fn test_set_buffer_size_sync() { |
| 955 |
test_set_buffer_size_by_scope(Scope::Input); |
| 956 |
test_set_buffer_size_by_scope(Scope::Output); |
| 957 |
fn test_set_buffer_size_by_scope(scope: Scope) { |
| 958 |
let unit = test_get_default_audiounit(scope.clone()); |
| 959 |
if unit.is_none() { |
| 960 |
println!("No audiounit for {:?}.", scope); |
| 961 |
return; |
| 962 |
} |
| 963 |
let unit = unit.unwrap(); |
| 964 |
let prop_scope = match scope { |
| 965 |
Scope::Input => PropertyScope::Output, |
| 966 |
Scope::Output => PropertyScope::Input, |
| 967 |
}; |
| 968 |
let mut buffer_frames = test_audiounit_get_buffer_frame_size( |
| 969 |
unit.get_inner(), |
| 970 |
scope.clone(), |
| 971 |
prop_scope.clone(), |
| 972 |
) |
| 973 |
.unwrap(); |
| 974 |
assert_ne!(buffer_frames, 0); |
| 975 |
buffer_frames *= 2; |
| 976 |
assert!(run_serially(|| set_buffer_size_sync( |
| 977 |
unit.get_inner(), |
| 978 |
scope.clone().into(), |
| 979 |
buffer_frames |
| 980 |
)) |
| 981 |
.is_ok()); |
| 982 |
let new_buffer_frames = |
| 983 |
test_audiounit_get_buffer_frame_size(unit.get_inner(), scope.clone(), prop_scope) |
| 984 |
.unwrap(); |
| 985 |
assert_eq!(buffer_frames, new_buffer_frames); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
#[test] |
| 990 |
#[should_panic] |
| 991 |
fn test_set_buffer_size_sync_for_input_with_null_input_unit() { |
| 992 |
test_set_buffer_size_sync_by_scope_with_null_unit(Scope::Input); |
| 993 |
} |
| 994 |
|
| 995 |
#[test] |
| 996 |
#[should_panic] |
| 997 |
fn test_set_buffer_size_sync_for_output_with_null_output_unit() { |
| 998 |
test_set_buffer_size_sync_by_scope_with_null_unit(Scope::Output); |
| 999 |
} |
| 1000 |
|
| 1001 |
fn test_set_buffer_size_sync_by_scope_with_null_unit(scope: Scope) { |
| 1002 |
let unit: AudioUnit = ptr::null_mut(); |
| 1003 |
assert!( |
| 1004 |
run_serially_forward_panics(|| set_buffer_size_sync(unit, scope.into(), 2048)).is_err() |
| 1005 |
); |
| 1006 |
} |
| 1007 |
|
| 1008 |
// get_volume, set_volume |
| 1009 |
// ------------------------------------ |
| 1010 |
#[test] |
| 1011 |
fn test_stream_get_volume() { |
| 1012 |
if let Some(unit) = test_get_default_audiounit(Scope::Output) { |
| 1013 |
let expected_volume: f32 = 0.5; |
| 1014 |
run_serially(|| set_volume(unit.get_inner(), expected_volume)); |
| 1015 |
assert_eq!( |
| 1016 |
expected_volume, |
| 1017 |
run_serially(|| get_volume(unit.get_inner()).unwrap()) |
| 1018 |
); |
| 1019 |
} else { |
| 1020 |
println!("No output audiounit."); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
// convert_uint32_into_string |
| 1025 |
// ------------------------------------ |
| 1026 |
#[test] |
| 1027 |
fn test_convert_uint32_into_string() { |
| 1028 |
let empty = convert_uint32_into_string(0); |
| 1029 |
assert_eq!(empty, CString::default()); |
| 1030 |
|
| 1031 |
let data: u32 = ('R' as u32) << 24 | ('U' as u32) << 16 | ('S' as u32) << 8 | 'T' as u32; |
| 1032 |
let data_string = convert_uint32_into_string(data); |
| 1033 |
assert_eq!(data_string, CString::new("RUST").unwrap()); |
| 1034 |
} |
| 1035 |
|
| 1036 |
// get_channel_count |
| 1037 |
// ------------------------------------ |
| 1038 |
#[test] |
| 1039 |
fn test_get_channel_count() { |
| 1040 |
test_channel_count(Scope::Input); |
| 1041 |
test_channel_count(Scope::Output); |
| 1042 |
|
| 1043 |
fn test_channel_count(scope: Scope) { |
| 1044 |
if let Some(device) = test_get_default_device(scope.clone()) { |
| 1045 |
let channels = |
| 1046 |
run_serially(|| get_channel_count(device, DeviceType::from(scope.clone()))) |
| 1047 |
.unwrap(); |
| 1048 |
assert!(channels > 0); |
| 1049 |
assert_eq!( |
| 1050 |
channels, |
| 1051 |
test_device_channels_in_scope(device, scope).unwrap() |
| 1052 |
); |
| 1053 |
} else { |
| 1054 |
println!("No device for {:?}.", scope); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
#[test] |
| 1060 |
fn test_get_channel_count_of_input_for_a_output_only_deivce() { |
| 1061 |
let devices = test_get_devices_in_scope(Scope::Output); |
| 1062 |
for device in devices { |
| 1063 |
// Skip in-out devices. |
| 1064 |
if test_device_in_scope(device, Scope::Input) { |
| 1065 |
continue; |
| 1066 |
} |
| 1067 |
let count = run_serially(|| get_channel_count(device, DeviceType::INPUT)).unwrap(); |
| 1068 |
assert_eq!(count, 0); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
#[test] |
| 1073 |
fn test_get_channel_count_of_output_for_a_input_only_deivce() { |
| 1074 |
let devices = test_get_devices_in_scope(Scope::Input); |
| 1075 |
for device in devices { |
| 1076 |
// Skip in-out devices. |
| 1077 |
if test_device_in_scope(device, Scope::Output) { |
| 1078 |
continue; |
| 1079 |
} |
| 1080 |
let count = run_serially(|| get_channel_count(device, DeviceType::OUTPUT)).unwrap(); |
| 1081 |
assert_eq!(count, 0); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
#[test] |
| 1086 |
#[should_panic] |
| 1087 |
fn test_get_channel_count_of_unknown_device() { |
| 1088 |
assert!(run_serially_forward_panics(|| get_channel_count( |
| 1089 |
kAudioObjectUnknown, |
| 1090 |
DeviceType::OUTPUT |
| 1091 |
)) |
| 1092 |
.is_err()); |
| 1093 |
} |
| 1094 |
|
| 1095 |
#[test] |
| 1096 |
fn test_get_channel_count_of_inout_type() { |
| 1097 |
test_channel_count(Scope::Input); |
| 1098 |
test_channel_count(Scope::Output); |
| 1099 |
|
| 1100 |
fn test_channel_count(scope: Scope) { |
| 1101 |
if let Some(device) = test_get_default_device(scope.clone()) { |
| 1102 |
run_serially_forward_panics(|| { |
| 1103 |
assert_eq!( |
| 1104 |
get_channel_count(device, DeviceType::INPUT | DeviceType::OUTPUT), |
| 1105 |
get_channel_count(device, DeviceType::INPUT).map(|c| c + get_channel_count( |
| 1106 |
device, |
| 1107 |
DeviceType::OUTPUT |
| 1108 |
) |
| 1109 |
.unwrap_or(0)) |
| 1110 |
); |
| 1111 |
}); |
| 1112 |
} else { |
| 1113 |
println!("No device for {:?}.", scope); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
#[test] |
| 1119 |
#[should_panic] |
| 1120 |
fn test_get_channel_count_of_unknwon_type() { |
| 1121 |
test_channel_count(Scope::Input); |
| 1122 |
test_channel_count(Scope::Output); |
| 1123 |
|
| 1124 |
fn test_channel_count(scope: Scope) { |
| 1125 |
if let Some(device) = test_get_default_device(scope.clone()) { |
| 1126 |
assert!(run_serially_forward_panics(|| get_channel_count( |
| 1127 |
device, |
| 1128 |
DeviceType::UNKNOWN |
| 1129 |
) |
| 1130 |
.is_err())); |
| 1131 |
} else { |
| 1132 |
panic!("Panic by default: No device for {:?}.", scope); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
} |
| 1136 |
|
| 1137 |
fn is_vpio(id: AudioObjectID) -> bool { |
| 1138 |
debug_assert_running_serially(); |
| 1139 |
get_device_global_uid(id) |
| 1140 |
.map(|uid| uid.into_string()) |
| 1141 |
.map(|uid| uid.contains(VOICEPROCESSING_AGGREGATE_DEVICE_NAME)) |
| 1142 |
.unwrap_or(false) |
| 1143 |
} |
| 1144 |
|
| 1145 |
fn get_nonvpio_input_channel_counts() -> Vec<u32> { |
| 1146 |
debug_assert_running_serially(); |
| 1147 |
get_devices() |
| 1148 |
.into_iter() |
| 1149 |
.filter(|&id| !is_vpio(id)) |
| 1150 |
.filter_map(|id| get_channel_count(id, DeviceType::INPUT).ok()) |
| 1151 |
.collect() |
| 1152 |
} |
| 1153 |
|
| 1154 |
#[test] |
| 1155 |
#[ignore] |
| 1156 |
fn test_get_channel_count_of_input_devices_with_vpio() { |
| 1157 |
let non_vpio_channel_counts = |
| 1158 |
run_serially_forward_panics(|| get_nonvpio_input_channel_counts()); |
| 1159 |
|
| 1160 |
let queue = Queue::new_with_target( |
| 1161 |
"test_get_channel_count_of_input_devices_with_vpio", |
| 1162 |
get_serial_queue_singleton(), |
| 1163 |
); |
| 1164 |
let mut shared = SharedVoiceProcessingUnitManager::new(queue.clone()); |
| 1165 |
let _vpio = queue.run_sync(|| shared.take_or_create()).unwrap().unwrap(); |
| 1166 |
|
| 1167 |
let vpio_channel_counts = run_serially_forward_panics(|| get_nonvpio_input_channel_counts()); |
| 1168 |
assert_eq!(non_vpio_channel_counts, vpio_channel_counts); |
| 1169 |
} |
| 1170 |
|
| 1171 |
#[test] |
| 1172 |
#[ignore] |
| 1173 |
fn test_get_channel_count_of_input_devices_with_aggregate_device_and_vpio() { |
| 1174 |
let input_device = test_get_default_device(Scope::Input); |
| 1175 |
let output_device = test_get_default_device(Scope::Output); |
| 1176 |
if input_device.is_none() || output_device.is_none() || input_device == output_device { |
| 1177 |
println!("No input or output device to create an aggregate device."); |
| 1178 |
return; |
| 1179 |
} |
| 1180 |
|
| 1181 |
#[derive(Default)] |
| 1182 |
struct State { |
| 1183 |
aggr: Option<AggregateDevice>, |
| 1184 |
vpio_mgr: Option<SharedVoiceProcessingUnitManager>, |
| 1185 |
vpio: Option<OwningHandle<VoiceProcessingUnit>>, |
| 1186 |
} |
| 1187 |
impl Drop for State { |
| 1188 |
fn drop(&mut self) { |
| 1189 |
let mut aggr = self.aggr.take(); |
| 1190 |
let mut vpio = self.vpio.take(); |
| 1191 |
run_serially_forward_panics(move || { |
| 1192 |
aggr.take(); |
| 1193 |
vpio.take(); |
| 1194 |
}); |
| 1195 |
} |
| 1196 |
} |
| 1197 |
let state = Arc::new(Mutex::new(State::default())); |
| 1198 |
|
| 1199 |
// Set up an AggregateDevice with input and output. |
| 1200 |
let initial_channel_counts = run_serially_forward_panics(|| get_nonvpio_input_channel_counts()); |
| 1201 |
let s1 = state.clone(); |
| 1202 |
let aggr_channel_counts = run_serially_forward_panics(|| { |
| 1203 |
let mut state = s1.lock().unwrap(); |
| 1204 |
state.aggr = |
| 1205 |
Some(AggregateDevice::new(input_device.unwrap(), output_device.unwrap()).unwrap()); |
| 1206 |
|
| 1207 |
get_nonvpio_input_channel_counts() |
| 1208 |
}); |
| 1209 |
assert_eq!(initial_channel_counts.len() + 1, aggr_channel_counts.len()); |
| 1210 |
|
| 1211 |
let queue = Queue::new_with_target( |
| 1212 |
"test_get_channel_count_of_input_devices_with_aggregate_device_and_vpio", |
| 1213 |
get_serial_queue_singleton(), |
| 1214 |
); |
| 1215 |
{ |
| 1216 |
let mut s = state.lock().unwrap(); |
| 1217 |
s.vpio_mgr = Some(SharedVoiceProcessingUnitManager::new(queue.clone())); |
| 1218 |
} |
| 1219 |
let s2 = state.clone(); |
| 1220 |
let aggr_vpio_channel_counts = run_serially_forward_panics(|| { |
| 1221 |
let mut state = s2.lock().unwrap(); |
| 1222 |
state.vpio = state.vpio_mgr.as_mut().map(|m| m.take_or_create().unwrap()); |
| 1223 |
|
| 1224 |
get_nonvpio_input_channel_counts() |
| 1225 |
}); |
| 1226 |
assert_eq!(aggr_channel_counts, aggr_vpio_channel_counts); |
| 1227 |
} |
| 1228 |
|
| 1229 |
// get_range_of_sample_rates |
| 1230 |
// ------------------------------------ |
| 1231 |
#[test] |
| 1232 |
fn test_get_range_of_sample_rates() { |
| 1233 |
test_get_range_of_sample_rates_in_scope(Scope::Input); |
| 1234 |
test_get_range_of_sample_rates_in_scope(Scope::Output); |
| 1235 |
|
| 1236 |
fn test_get_range_of_sample_rates_in_scope(scope: Scope) { |
| 1237 |
if let Some(device) = test_get_default_device(scope.clone()) { |
| 1238 |
let ranges = test_get_available_samplerate_of_device(device); |
| 1239 |
for range in ranges { |
| 1240 |
// Surprisingly, we can get the input/output sample rates from a non-input/non-output device. |
| 1241 |
check_samplerates(range); |
| 1242 |
} |
| 1243 |
} else { |
| 1244 |
println!("No device for {:?}.", scope); |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
fn test_get_available_samplerate_of_device(id: AudioObjectID) -> Vec<(f64, f64)> { |
| 1249 |
let scopes = [ |
| 1250 |
DeviceType::INPUT, |
| 1251 |
DeviceType::OUTPUT, |
| 1252 |
DeviceType::INPUT | DeviceType::OUTPUT, |
| 1253 |
]; |
| 1254 |
let mut ranges = Vec::new(); |
| 1255 |
for scope in scopes.iter() { |
| 1256 |
ranges.push( |
| 1257 |
run_serially_forward_panics(|| get_range_of_sample_rates(id, *scope)).unwrap(), |
| 1258 |
); |
| 1259 |
} |
| 1260 |
ranges |
| 1261 |
} |
| 1262 |
|
| 1263 |
fn check_samplerates((min, max): (f64, f64)) { |
| 1264 |
assert!(min > 0.0); |
| 1265 |
assert!(max > 0.0); |
| 1266 |
assert!(min <= max); |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
// get_presentation_latency |
| 1271 |
// ------------------------------------ |
| 1272 |
#[test] |
| 1273 |
fn test_get_device_presentation_latency() { |
| 1274 |
test_get_device_presentation_latencies_in_scope(Scope::Input); |
| 1275 |
test_get_device_presentation_latencies_in_scope(Scope::Output); |
| 1276 |
|
| 1277 |
fn test_get_device_presentation_latencies_in_scope(scope: Scope) { |
| 1278 |
if let Some(device) = test_get_default_device(scope.clone()) { |
| 1279 |
// TODO: The latencies very from devices to devices. Check nothing here. |
| 1280 |
let latency = |
| 1281 |
run_serially_forward_panics(|| get_fixed_latency(device, scope.clone().into())); |
| 1282 |
println!( |
| 1283 |
"present latency on the device {} in scope {:?}: {}", |
| 1284 |
device, scope, latency |
| 1285 |
); |
| 1286 |
} else { |
| 1287 |
println!("No device for {:?}.", scope); |
| 1288 |
} |
| 1289 |
} |
| 1290 |
} |
| 1291 |
|
| 1292 |
// get_device_group_id |
| 1293 |
// ------------------------------------ |
| 1294 |
#[test] |
| 1295 |
fn test_get_device_group_id() { |
| 1296 |
if let Some(device) = test_get_default_device(Scope::Input) { |
| 1297 |
match run_serially_forward_panics(|| get_device_group_id(device, DeviceType::INPUT)) { |
| 1298 |
Ok(id) => println!("input group id: {:?}", id), |
| 1299 |
Err(e) => println!("No input group id. Error: {}", e), |
| 1300 |
} |
| 1301 |
} else { |
| 1302 |
println!("No input device."); |
| 1303 |
} |
| 1304 |
|
| 1305 |
if let Some(device) = test_get_default_device(Scope::Output) { |
| 1306 |
match run_serially_forward_panics(|| get_device_group_id(device, DeviceType::OUTPUT)) { |
| 1307 |
Ok(id) => println!("output group id: {:?}", id), |
| 1308 |
Err(e) => println!("No output group id. Error: {}", e), |
| 1309 |
} |
| 1310 |
} else { |
| 1311 |
println!("No output device."); |
| 1312 |
} |
| 1313 |
} |
| 1314 |
|
| 1315 |
#[test] |
| 1316 |
fn test_get_same_group_id_for_builtin_device_pairs() { |
| 1317 |
use std::collections::HashMap; |
| 1318 |
|
| 1319 |
// These device sources have custom group id. See `get_custom_group_id`. |
| 1320 |
const IMIC: u32 = 0x696D_6963; // "imic" |
| 1321 |
const ISPK: u32 = 0x6973_706B; // "ispk" |
| 1322 |
const EMIC: u32 = 0x656D_6963; // "emic" |
| 1323 |
const HDPN: u32 = 0x6864_706E; // "hdpn" |
| 1324 |
let pairs = [(IMIC, ISPK), (EMIC, HDPN)]; |
| 1325 |
|
| 1326 |
let mut input_group_ids = HashMap::<u32, String>::new(); |
| 1327 |
let input_devices = test_get_devices_in_scope(Scope::Input); |
| 1328 |
for device in input_devices.iter() { |
| 1329 |
match run_serially_forward_panics(|| get_device_source(*device, DeviceType::INPUT)) { |
| 1330 |
Ok(source) => match run_serially_forward_panics(|| { |
| 1331 |
get_device_group_id(*device, DeviceType::INPUT) |
| 1332 |
}) { |
| 1333 |
Ok(id) => assert!(input_group_ids |
| 1334 |
.insert(source, id.into_string().unwrap()) |
| 1335 |
.is_none()), |
| 1336 |
Err(e) => assert!(input_group_ids |
| 1337 |
.insert(source, format!("Error {}", e)) |
| 1338 |
.is_none()), |
| 1339 |
}, |
| 1340 |
_ => {} // do nothing when failing to get source. |
| 1341 |
} |
| 1342 |
} |
| 1343 |
|
| 1344 |
let mut output_group_ids = HashMap::<u32, String>::new(); |
| 1345 |
let output_devices = test_get_devices_in_scope(Scope::Output); |
| 1346 |
for device in output_devices.iter() { |
| 1347 |
match run_serially_forward_panics(|| get_device_source(*device, DeviceType::OUTPUT)) { |
| 1348 |
Ok(source) => match run_serially_forward_panics(|| { |
| 1349 |
get_device_group_id(*device, DeviceType::OUTPUT) |
| 1350 |
}) { |
| 1351 |
Ok(id) => assert!(output_group_ids |
| 1352 |
.insert(source, id.into_string().unwrap()) |
| 1353 |
.is_none()), |
| 1354 |
Err(e) => assert!(output_group_ids |
| 1355 |
.insert(source, format!("Error {}", e)) |
| 1356 |
.is_none()), |
| 1357 |
}, |
| 1358 |
_ => {} // do nothing when failing to get source. |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
for (input, output) in pairs.iter() { |
| 1363 |
let input_group_id = input_group_ids.get(input); |
| 1364 |
let output_group_id = output_group_ids.get(output); |
| 1365 |
|
| 1366 |
if input_group_id.is_some() && output_group_id.is_some() { |
| 1367 |
assert_eq!(input_group_id, output_group_id); |
| 1368 |
} |
| 1369 |
|
| 1370 |
input_group_ids.remove(input); |
| 1371 |
output_group_ids.remove(output); |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
#[test] |
| 1376 |
#[should_panic] |
| 1377 |
fn test_get_device_group_id_by_unknown_device() { |
| 1378 |
assert!(run_serially_forward_panics(|| get_device_group_id( |
| 1379 |
kAudioObjectUnknown, |
| 1380 |
DeviceType::INPUT |
| 1381 |
)) |
| 1382 |
.is_err()); |
| 1383 |
} |
| 1384 |
|
| 1385 |
// get_device_label |
| 1386 |
// ------------------------------------ |
| 1387 |
#[test] |
| 1388 |
fn test_get_device_label() { |
| 1389 |
if let Some(device) = test_get_default_device(Scope::Input) { |
| 1390 |
let name = |
| 1391 |
run_serially_forward_panics(|| get_device_label(device, DeviceType::INPUT)).unwrap(); |
| 1392 |
println!("input device label: {}", name.into_string()); |
| 1393 |
} else { |
| 1394 |
println!("No input device."); |
| 1395 |
} |
| 1396 |
|
| 1397 |
if let Some(device) = test_get_default_device(Scope::Output) { |
| 1398 |
let name = |
| 1399 |
run_serially_forward_panics(|| get_device_label(device, DeviceType::OUTPUT)).unwrap(); |
| 1400 |
println!("output device label: {}", name.into_string()); |
| 1401 |
} else { |
| 1402 |
println!("No output device."); |
| 1403 |
} |
| 1404 |
} |
| 1405 |
|
| 1406 |
#[test] |
| 1407 |
#[should_panic] |
| 1408 |
fn test_get_device_label_by_unknown_device() { |
| 1409 |
assert!(run_serially_forward_panics(|| get_device_label( |
| 1410 |
kAudioObjectUnknown, |
| 1411 |
DeviceType::INPUT |
| 1412 |
)) |
| 1413 |
.is_err()); |
| 1414 |
} |
| 1415 |
|
| 1416 |
// get_device_global_uid |
| 1417 |
// ------------------------------------ |
| 1418 |
#[test] |
| 1419 |
fn test_get_device_global_uid() { |
| 1420 |
// Input device. |
| 1421 |
if let Some(input) = test_get_default_device(Scope::Input) { |
| 1422 |
let uid = run_serially_forward_panics(|| get_device_global_uid(input)).unwrap(); |
| 1423 |
let uid = uid.into_string(); |
| 1424 |
assert!(!uid.is_empty()); |
| 1425 |
} |
| 1426 |
|
| 1427 |
// Output device. |
| 1428 |
if let Some(output) = test_get_default_device(Scope::Output) { |
| 1429 |
let uid = run_serially_forward_panics(|| get_device_global_uid(output)).unwrap(); |
| 1430 |
let uid = uid.into_string(); |
| 1431 |
assert!(!uid.is_empty()); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
|
| 1435 |
#[test] |
| 1436 |
#[should_panic] |
| 1437 |
fn test_get_device_global_uid_by_unknwon_device() { |
| 1438 |
// Unknown device. |
| 1439 |
assert!(run_serially_forward_panics(|| get_device_global_uid(kAudioObjectUnknown)).is_err()); |
| 1440 |
} |
| 1441 |
|
| 1442 |
// create_cubeb_device_info |
| 1443 |
// destroy_cubeb_device_info |
| 1444 |
// ------------------------------------ |
| 1445 |
#[test] |
| 1446 |
fn test_create_cubeb_device_info() { |
| 1447 |
use std::collections::VecDeque; |
| 1448 |
|
| 1449 |
test_create_device_from_hwdev_in_scope(Scope::Input); |
| 1450 |
test_create_device_from_hwdev_in_scope(Scope::Output); |
| 1451 |
|
| 1452 |
fn test_create_device_from_hwdev_in_scope(scope: Scope) { |
| 1453 |
if let Some(device) = test_get_default_device(scope.clone()) { |
| 1454 |
let is_input = test_device_in_scope(device, Scope::Input); |
| 1455 |
let is_output = test_device_in_scope(device, Scope::Output); |
| 1456 |
let mut results = test_create_device_infos_by_device(device); |
| 1457 |
assert_eq!(results.len(), 2); |
| 1458 |
// Input device type: |
| 1459 |
let input_result = results.pop_front().unwrap(); |
| 1460 |
if is_input { |
| 1461 |
let mut input_device_info = input_result.unwrap(); |
| 1462 |
check_device_info_by_device(&input_device_info, device, Scope::Input); |
| 1463 |
run_serially(|| destroy_cubeb_device_info(&mut input_device_info)); |
| 1464 |
} else { |
| 1465 |
assert_eq!(input_result.unwrap_err(), Error::error()); |
| 1466 |
} |
| 1467 |
// Output device type: |
| 1468 |
let output_result = results.pop_front().unwrap(); |
| 1469 |
if is_output { |
| 1470 |
let mut output_device_info = output_result.unwrap(); |
| 1471 |
check_device_info_by_device(&output_device_info, device, Scope::Output); |
| 1472 |
run_serially(|| destroy_cubeb_device_info(&mut output_device_info)); |
| 1473 |
} else { |
| 1474 |
assert_eq!(output_result.unwrap_err(), Error::error()); |
| 1475 |
} |
| 1476 |
} else { |
| 1477 |
println!("No device for {:?}.", scope); |
| 1478 |
} |
| 1479 |
} |
| 1480 |
|
| 1481 |
fn test_create_device_infos_by_device( |
| 1482 |
id: AudioObjectID, |
| 1483 |
) -> VecDeque<std::result::Result<ffi::cubeb_device_info, Error>> { |
| 1484 |
let dev_types = [DeviceType::INPUT, DeviceType::OUTPUT]; |
| 1485 |
let mut results = VecDeque::new(); |
| 1486 |
for dev_type in dev_types.iter() { |
| 1487 |
results.push_back(run_serially_forward_panics(|| { |
| 1488 |
create_cubeb_device_info(id, *dev_type) |
| 1489 |
})); |
| 1490 |
} |
| 1491 |
results |
| 1492 |
} |
| 1493 |
|
| 1494 |
fn check_device_info_by_device(info: &ffi::cubeb_device_info, id: AudioObjectID, scope: Scope) { |
| 1495 |
assert!(!info.devid.is_null()); |
| 1496 |
assert!(mem::size_of_val(&info.devid) >= mem::size_of::<AudioObjectID>()); |
| 1497 |
assert_eq!(info.devid as AudioObjectID, id); |
| 1498 |
assert!(!info.device_id.is_null()); |
| 1499 |
assert!(!info.friendly_name.is_null()); |
| 1500 |
assert!(!info.group_id.is_null()); |
| 1501 |
|
| 1502 |
// TODO: Hit a kAudioHardwareUnknownPropertyError for AirPods |
| 1503 |
// assert!(!info.vendor_name.is_null()); |
| 1504 |
|
| 1505 |
// FIXME: The device is defined to input-only or output-only, but some device is in-out! |
| 1506 |
assert_eq!(info.device_type, DeviceType::from(scope.clone()).bits()); |
| 1507 |
assert_eq!(info.state, ffi::CUBEB_DEVICE_STATE_ENABLED); |
| 1508 |
// TODO: The preference is set when the device is default input/output device if the device |
| 1509 |
// info is created from input/output scope. Should the preference be set if the |
| 1510 |
// device is a default input/output device if the device info is created from |
| 1511 |
// output/input scope ? The device may be a in-out device! |
| 1512 |
assert_eq!(info.preferred, get_cubeb_device_pref(id, scope)); |
| 1513 |
|
| 1514 |
assert_eq!(info.format, ffi::CUBEB_DEVICE_FMT_ALL); |
| 1515 |
assert_eq!(info.default_format, ffi::CUBEB_DEVICE_FMT_F32NE); |
| 1516 |
assert!(info.max_channels > 0); |
| 1517 |
assert!(info.min_rate <= info.max_rate); |
| 1518 |
assert!(info.min_rate <= info.default_rate); |
| 1519 |
assert!(info.default_rate <= info.max_rate); |
| 1520 |
|
| 1521 |
assert!(info.latency_lo > 0); |
| 1522 |
assert!(info.latency_hi > 0); |
| 1523 |
assert!(info.latency_lo <= info.latency_hi); |
| 1524 |
|
| 1525 |
fn get_cubeb_device_pref(id: AudioObjectID, scope: Scope) -> ffi::cubeb_device_pref { |
| 1526 |
let default_device = test_get_default_device(scope); |
| 1527 |
if default_device.is_some() && default_device.unwrap() == id { |
| 1528 |
ffi::CUBEB_DEVICE_PREF_ALL |
| 1529 |
} else { |
| 1530 |
ffi::CUBEB_DEVICE_PREF_NONE |
| 1531 |
} |
| 1532 |
} |
| 1533 |
} |
| 1534 |
} |
| 1535 |
|
| 1536 |
#[test] |
| 1537 |
#[should_panic] |
| 1538 |
fn test_create_device_info_by_unknown_device() { |
| 1539 |
assert!(create_cubeb_device_info(kAudioObjectUnknown, DeviceType::OUTPUT).is_err()); |
| 1540 |
} |
| 1541 |
|
| 1542 |
#[test] |
| 1543 |
fn test_create_device_info_with_unknown_type() { |
| 1544 |
test_create_device_info_with_unknown_type_by_scope(Scope::Input); |
| 1545 |
test_create_device_info_with_unknown_type_by_scope(Scope::Output); |
| 1546 |
|
| 1547 |
fn test_create_device_info_with_unknown_type_by_scope(scope: Scope) { |
| 1548 |
if let Some(device) = test_get_default_device(scope.clone()) { |
| 1549 |
assert!(run_serially_forward_panics(|| create_cubeb_device_info( |
| 1550 |
device, |
| 1551 |
DeviceType::UNKNOWN |
| 1552 |
)) |
| 1553 |
.is_err()); |
| 1554 |
} |
| 1555 |
} |
| 1556 |
} |
| 1557 |
|
| 1558 |
#[test] |
| 1559 |
#[should_panic] |
| 1560 |
fn test_device_destroy_empty_device() { |
| 1561 |
let mut device = ffi::cubeb_device_info::default(); |
| 1562 |
|
| 1563 |
assert!(device.device_id.is_null()); |
| 1564 |
assert!(device.group_id.is_null()); |
| 1565 |
assert!(device.friendly_name.is_null()); |
| 1566 |
assert!(device.vendor_name.is_null()); |
| 1567 |
|
| 1568 |
// `friendly_name` must be set. |
| 1569 |
destroy_cubeb_device_info(&mut device); |
| 1570 |
|
| 1571 |
assert!(device.device_id.is_null()); |
| 1572 |
assert!(device.group_id.is_null()); |
| 1573 |
assert!(device.friendly_name.is_null()); |
| 1574 |
assert!(device.vendor_name.is_null()); |
| 1575 |
} |
| 1576 |
|
| 1577 |
#[test] |
| 1578 |
fn test_create_device_from_hwdev_with_inout_type() { |
| 1579 |
test_create_device_from_hwdev_with_inout_type_by_scope(Scope::Input); |
| 1580 |
test_create_device_from_hwdev_with_inout_type_by_scope(Scope::Output); |
| 1581 |
|
| 1582 |
fn test_create_device_from_hwdev_with_inout_type_by_scope(scope: Scope) { |
| 1583 |
if let Some(device) = test_get_default_device(scope.clone()) { |
| 1584 |
// Get a kAudioHardwareUnknownPropertyError in get_channel_count actually. |
| 1585 |
assert!(run_serially_forward_panics(|| create_cubeb_device_info( |
| 1586 |
device, |
| 1587 |
DeviceType::INPUT | DeviceType::OUTPUT |
| 1588 |
)) |
| 1589 |
.is_err()); |
| 1590 |
} else { |
| 1591 |
println!("No device for {:?}.", scope); |
| 1592 |
} |
| 1593 |
} |
| 1594 |
} |
| 1595 |
|
| 1596 |
// get_devices_of_type |
| 1597 |
// ------------------------------------ |
| 1598 |
#[test] |
| 1599 |
fn test_get_devices_of_type() { |
| 1600 |
use std::collections::HashSet; |
| 1601 |
|
| 1602 |
let all_devices = run_serially_forward_panics(|| { |
| 1603 |
audiounit_get_devices_of_type(DeviceType::INPUT | DeviceType::OUTPUT) |
| 1604 |
}); |
| 1605 |
let input_devices = |
| 1606 |
run_serially_forward_panics(|| audiounit_get_devices_of_type(DeviceType::INPUT)); |
| 1607 |
let output_devices = |
| 1608 |
run_serially_forward_panics(|| audiounit_get_devices_of_type(DeviceType::OUTPUT)); |
| 1609 |
|
| 1610 |
let mut expected_all = test_get_all_devices(DeviceFilter::ExcludeCubebAggregateAndVPIO); |
| 1611 |
expected_all.sort(); |
| 1612 |
assert_eq!(all_devices, expected_all); |
| 1613 |
for device in all_devices.iter() { |
| 1614 |
if test_device_in_scope(*device, Scope::Input) { |
| 1615 |
assert!(input_devices.contains(device)); |
| 1616 |
} |
| 1617 |
if test_device_in_scope(*device, Scope::Output) { |
| 1618 |
assert!(output_devices.contains(device)); |
| 1619 |
} |
| 1620 |
} |
| 1621 |
|
| 1622 |
let input: HashSet<AudioObjectID> = input_devices.iter().cloned().collect(); |
| 1623 |
let output: HashSet<AudioObjectID> = output_devices.iter().cloned().collect(); |
| 1624 |
let union: HashSet<AudioObjectID> = input.union(&output).cloned().collect(); |
| 1625 |
let mut union_devices: Vec<AudioObjectID> = union.iter().cloned().collect(); |
| 1626 |
union_devices.sort(); |
| 1627 |
assert_eq!(all_devices, union_devices); |
| 1628 |
} |
| 1629 |
|
| 1630 |
#[test] |
| 1631 |
#[should_panic] |
| 1632 |
fn test_get_devices_of_type_unknown() { |
| 1633 |
run_serially_forward_panics(|| { |
| 1634 |
let no_devs = audiounit_get_devices_of_type(DeviceType::UNKNOWN); |
| 1635 |
assert!(no_devs.is_empty()); |
| 1636 |
}); |
| 1637 |
} |
| 1638 |
|
| 1639 |
// add_devices_changed_listener |
| 1640 |
// ------------------------------------ |
| 1641 |
#[test] |
| 1642 |
fn test_add_devices_changed_listener() { |
| 1643 |
use std::collections::HashMap; |
| 1644 |
|
| 1645 |
extern "C" fn inout_callback(_: *mut ffi::cubeb, _: *mut c_void) {} |
| 1646 |
extern "C" fn in_callback(_: *mut ffi::cubeb, _: *mut c_void) {} |
| 1647 |
extern "C" fn out_callback(_: *mut ffi::cubeb, _: *mut c_void) {} |
| 1648 |
|
| 1649 |
let mut map: HashMap<DeviceType, extern "C" fn(*mut ffi::cubeb, *mut c_void)> = HashMap::new(); |
| 1650 |
map.insert(DeviceType::INPUT, in_callback); |
| 1651 |
map.insert(DeviceType::OUTPUT, out_callback); |
| 1652 |
map.insert(DeviceType::INPUT | DeviceType::OUTPUT, inout_callback); |
| 1653 |
|
| 1654 |
test_get_raw_context(|context| { |
| 1655 |
for (devtype, callback) in map.iter() { |
| 1656 |
assert!(get_devices_changed_callback(context, Scope::Input).is_none()); |
| 1657 |
assert!(get_devices_changed_callback(context, Scope::Output).is_none()); |
| 1658 |
|
| 1659 |
// Register a callback within a specific scope. |
| 1660 |
assert!(run_serially(|| { |
| 1661 |
context.add_devices_changed_listener(*devtype, Some(*callback), ptr::null_mut()) |
| 1662 |
}) |
| 1663 |
.is_ok()); |
| 1664 |
|
| 1665 |
if devtype.contains(DeviceType::INPUT) { |
| 1666 |
let cb = get_devices_changed_callback(context, Scope::Input); |
| 1667 |
assert!(cb.is_some()); |
| 1668 |
assert_eq!(cb.unwrap(), *callback); |
| 1669 |
} else { |
| 1670 |
let cb = get_devices_changed_callback(context, Scope::Input); |
| 1671 |
assert!(cb.is_none()); |
| 1672 |
} |
| 1673 |
|
| 1674 |
if devtype.contains(DeviceType::OUTPUT) { |
| 1675 |
let cb = get_devices_changed_callback(context, Scope::Output); |
| 1676 |
assert!(cb.is_some()); |
| 1677 |
assert_eq!(cb.unwrap(), *callback); |
| 1678 |
} else { |
| 1679 |
let cb = get_devices_changed_callback(context, Scope::Output); |
| 1680 |
assert!(cb.is_none()); |
| 1681 |
} |
| 1682 |
|
| 1683 |
// Unregister the callbacks within all scopes. |
| 1684 |
assert!(run_serially(|| { |
| 1685 |
context.remove_devices_changed_listener(DeviceType::INPUT | DeviceType::OUTPUT) |
| 1686 |
}) |
| 1687 |
.is_ok()); |
| 1688 |
|
| 1689 |
assert!(get_devices_changed_callback(context, Scope::Input).is_none()); |
| 1690 |
assert!(get_devices_changed_callback(context, Scope::Output).is_none()); |
| 1691 |
} |
| 1692 |
}); |
| 1693 |
} |
| 1694 |
|
| 1695 |
#[test] |
| 1696 |
#[should_panic] |
| 1697 |
fn test_add_devices_changed_listener_in_unknown_scope() { |
| 1698 |
extern "C" fn callback(_: *mut ffi::cubeb, _: *mut c_void) {} |
| 1699 |
|
| 1700 |
test_get_raw_context(|context| { |
| 1701 |
let _ = context.add_devices_changed_listener( |
| 1702 |
DeviceType::UNKNOWN, |
| 1703 |
Some(callback), |
| 1704 |
ptr::null_mut(), |
| 1705 |
); |
| 1706 |
}); |
| 1707 |
} |
| 1708 |
|
| 1709 |
#[test] |
| 1710 |
#[should_panic] |
| 1711 |
fn test_add_devices_changed_listener_with_none_callback() { |
| 1712 |
test_get_raw_context(|context| { |
| 1713 |
for devtype in &[DeviceType::INPUT, DeviceType::OUTPUT] { |
| 1714 |
assert!(context |
| 1715 |
.add_devices_changed_listener(*devtype, None, ptr::null_mut()) |
| 1716 |
.is_ok()); |
| 1717 |
} |
| 1718 |
}); |
| 1719 |
} |
| 1720 |
|
| 1721 |
// remove_devices_changed_listener |
| 1722 |
// ------------------------------------ |
| 1723 |
#[test] |
| 1724 |
fn test_remove_devices_changed_listener() { |
| 1725 |
use std::collections::HashMap; |
| 1726 |
|
| 1727 |
extern "C" fn in_callback(_: *mut ffi::cubeb, _: *mut c_void) {} |
| 1728 |
extern "C" fn out_callback(_: *mut ffi::cubeb, _: *mut c_void) {} |
| 1729 |
|
| 1730 |
let mut map: HashMap<DeviceType, extern "C" fn(*mut ffi::cubeb, *mut c_void)> = HashMap::new(); |
| 1731 |
map.insert(DeviceType::INPUT, in_callback); |
| 1732 |
map.insert(DeviceType::OUTPUT, out_callback); |
| 1733 |
|
| 1734 |
test_get_raw_context(|context| { |
| 1735 |
for (devtype, _callback) in map.iter() { |
| 1736 |
assert!(get_devices_changed_callback(context, Scope::Input).is_none()); |
| 1737 |
assert!(get_devices_changed_callback(context, Scope::Output).is_none()); |
| 1738 |
|
| 1739 |
// Register callbacks within all scopes. |
| 1740 |
for (scope, listener) in map.iter() { |
| 1741 |
assert!(run_serially(|| context.add_devices_changed_listener( |
| 1742 |
*scope, |
| 1743 |
Some(*listener), |
| 1744 |
ptr::null_mut() |
| 1745 |
)) |
| 1746 |
.is_ok()); |
| 1747 |
} |
| 1748 |
|
| 1749 |
let input_callback = get_devices_changed_callback(context, Scope::Input); |
| 1750 |
assert!(input_callback.is_some()); |
| 1751 |
assert_eq!( |
| 1752 |
input_callback.unwrap(), |
| 1753 |
*(map.get(&DeviceType::INPUT).unwrap()) |
| 1754 |
); |
| 1755 |
let output_callback = get_devices_changed_callback(context, Scope::Output); |
| 1756 |
assert!(output_callback.is_some()); |
| 1757 |
assert_eq!( |
| 1758 |
output_callback.unwrap(), |
| 1759 |
*(map.get(&DeviceType::OUTPUT).unwrap()) |
| 1760 |
); |
| 1761 |
|
| 1762 |
// Unregister the callbacks within one specific scopes. |
| 1763 |
assert!(run_serially(|| context.remove_devices_changed_listener(*devtype)).is_ok()); |
| 1764 |
|
| 1765 |
if devtype.contains(DeviceType::INPUT) { |
| 1766 |
let cb = get_devices_changed_callback(context, Scope::Input); |
| 1767 |
assert!(cb.is_none()); |
| 1768 |
} else { |
| 1769 |
let cb = get_devices_changed_callback(context, Scope::Input); |
| 1770 |
assert!(cb.is_some()); |
| 1771 |
assert_eq!(cb.unwrap(), *(map.get(&DeviceType::INPUT).unwrap())); |
| 1772 |
} |
| 1773 |
|
| 1774 |
if devtype.contains(DeviceType::OUTPUT) { |
| 1775 |
let cb = get_devices_changed_callback(context, Scope::Output); |
| 1776 |
assert!(cb.is_none()); |
| 1777 |
} else { |
| 1778 |
let cb = get_devices_changed_callback(context, Scope::Output); |
| 1779 |
assert!(cb.is_some()); |
| 1780 |
assert_eq!(cb.unwrap(), *(map.get(&DeviceType::OUTPUT).unwrap())); |
| 1781 |
} |
| 1782 |
|
| 1783 |
// Unregister the callbacks within all scopes. |
| 1784 |
assert!(run_serially( |
| 1785 |
|| context.remove_devices_changed_listener(DeviceType::INPUT | DeviceType::OUTPUT) |
| 1786 |
) |
| 1787 |
.is_ok()); |
| 1788 |
} |
| 1789 |
}); |
| 1790 |
} |
| 1791 |
|
| 1792 |
#[test] |
| 1793 |
fn test_remove_devices_changed_listener_without_adding_listeners() { |
| 1794 |
test_get_raw_context(|context| { |
| 1795 |
for devtype in &[ |
| 1796 |
DeviceType::INPUT, |
| 1797 |
DeviceType::OUTPUT, |
| 1798 |
DeviceType::INPUT | DeviceType::OUTPUT, |
| 1799 |
] { |
| 1800 |
assert!(run_serially(|| context.remove_devices_changed_listener(*devtype)).is_ok()); |
| 1801 |
} |
| 1802 |
}); |
| 1803 |
} |
| 1804 |
|
| 1805 |
#[test] |
| 1806 |
fn test_remove_devices_changed_listener_within_all_scopes() { |
| 1807 |
use std::collections::HashMap; |
| 1808 |
|
| 1809 |
extern "C" fn inout_callback(_: *mut ffi::cubeb, _: *mut c_void) {} |
| 1810 |
extern "C" fn in_callback(_: *mut ffi::cubeb, _: *mut c_void) {} |
| 1811 |
extern "C" fn out_callback(_: *mut ffi::cubeb, _: *mut c_void) {} |
| 1812 |
|
| 1813 |
let mut map: HashMap<DeviceType, extern "C" fn(*mut ffi::cubeb, *mut c_void)> = HashMap::new(); |
| 1814 |
map.insert(DeviceType::INPUT, in_callback); |
| 1815 |
map.insert(DeviceType::OUTPUT, out_callback); |
| 1816 |
map.insert(DeviceType::INPUT | DeviceType::OUTPUT, inout_callback); |
| 1817 |
|
| 1818 |
test_get_raw_context(|context| { |
| 1819 |
for (devtype, callback) in map.iter() { |
| 1820 |
assert!(get_devices_changed_callback(context, Scope::Input).is_none()); |
| 1821 |
assert!(get_devices_changed_callback(context, Scope::Output).is_none()); |
| 1822 |
|
| 1823 |
assert!(run_serially(|| context.add_devices_changed_listener( |
| 1824 |
*devtype, |
| 1825 |
Some(*callback), |
| 1826 |
ptr::null_mut() |
| 1827 |
)) |
| 1828 |
.is_ok()); |
| 1829 |
|
| 1830 |
if devtype.contains(DeviceType::INPUT) { |
| 1831 |
let cb = get_devices_changed_callback(context, Scope::Input); |
| 1832 |
assert!(cb.is_some()); |
| 1833 |
assert_eq!(cb.unwrap(), *callback); |
| 1834 |
} |
| 1835 |
|
| 1836 |
if devtype.contains(DeviceType::OUTPUT) { |
| 1837 |
let cb = get_devices_changed_callback(context, Scope::Output); |
| 1838 |
assert!(cb.is_some()); |
| 1839 |
assert_eq!(cb.unwrap(), *callback); |
| 1840 |
} |
| 1841 |
|
| 1842 |
assert!(run_serially( |
| 1843 |
|| context.remove_devices_changed_listener(DeviceType::INPUT | DeviceType::OUTPUT) |
| 1844 |
) |
| 1845 |
.is_ok()); |
| 1846 |
|
| 1847 |
assert!(get_devices_changed_callback(context, Scope::Input).is_none()); |
| 1848 |
assert!(get_devices_changed_callback(context, Scope::Output).is_none()); |
| 1849 |
} |
| 1850 |
}); |
| 1851 |
} |
| 1852 |
|
| 1853 |
fn get_devices_changed_callback( |
| 1854 |
context: &AudioUnitContext, |
| 1855 |
scope: Scope, |
| 1856 |
) -> ffi::cubeb_device_collection_changed_callback { |
| 1857 |
let devices_guard = context.devices.lock().unwrap(); |
| 1858 |
match scope { |
| 1859 |
Scope::Input => devices_guard.input.changed_callback, |
| 1860 |
Scope::Output => devices_guard.output.changed_callback, |
| 1861 |
} |
| 1862 |
} |
| 1863 |
|
| 1864 |
// SharedVoiceProcessingUnitManager |
| 1865 |
// ------------------------------------ |
| 1866 |
#[test] |
| 1867 |
fn test_shared_voice_processing_unit() { |
| 1868 |
let queue = Queue::new_with_target( |
| 1869 |
"test_shared_voice_processing_unit", |
| 1870 |
get_serial_queue_singleton(), |
| 1871 |
); |
| 1872 |
let mut shared = SharedVoiceProcessingUnitManager::new(queue.clone()); |
| 1873 |
let r1 = queue.run_sync(|| shared.take()).unwrap(); |
| 1874 |
assert!(r1.is_err()); |
| 1875 |
let r2 = queue.run_sync(|| shared.take_or_create()).unwrap(); |
| 1876 |
assert!(r2.is_ok()); |
| 1877 |
{ |
| 1878 |
let _handle = r2.unwrap(); |
| 1879 |
let r3 = queue.run_sync(|| shared.take()).unwrap(); |
| 1880 |
assert!(r3.is_err()); |
| 1881 |
} |
| 1882 |
let r4 = queue.run_sync(|| shared.take()).unwrap(); |
| 1883 |
assert!(r4.is_ok()); |
| 1884 |
} |
| 1885 |
|
| 1886 |
#[test] |
| 1887 |
#[should_panic] |
| 1888 |
fn test_shared_voice_processing_unit_bad_release_order() { |
| 1889 |
let queue = Queue::new_with_target( |
| 1890 |
"test_shared_voice_processing_unit_bad_release_order", |
| 1891 |
get_serial_queue_singleton(), |
| 1892 |
); |
| 1893 |
let mut shared = SharedVoiceProcessingUnitManager::new(queue.clone()); |
| 1894 |
let r1 = queue.run_sync(|| shared.take()).unwrap(); |
| 1895 |
assert!(r1.is_ok()); |
| 1896 |
drop(shared); |
| 1897 |
run_serially_forward_panics(|| drop(r1)); |
| 1898 |
} |
| 1899 |
|
| 1900 |
#[test] |
| 1901 |
fn test_shared_voice_processing_multiple_units() { |
| 1902 |
let queue = Queue::new_with_target( |
| 1903 |
"test_shared_voice_processing_multiple_units", |
| 1904 |
get_serial_queue_singleton(), |
| 1905 |
); |
| 1906 |
let mut shared = SharedVoiceProcessingUnitManager::new(queue.clone()); |
| 1907 |
let r1 = queue.run_sync(|| shared.take_or_create()).unwrap(); |
| 1908 |
assert!(r1.is_ok()); |
| 1909 |
let r2 = queue.run_sync(|| shared.take_or_create()).unwrap(); |
| 1910 |
assert!(r2.is_ok()); |
| 1911 |
{ |
| 1912 |
let _handle1 = r1.unwrap(); |
| 1913 |
let _handle2 = r2.unwrap(); |
| 1914 |
let r3 = queue.run_sync(|| shared.take()).unwrap(); |
| 1915 |
assert!(r3.is_err()); |
| 1916 |
} |
| 1917 |
let r1 = queue.run_sync(|| shared.take()).unwrap(); |
| 1918 |
assert!(r1.is_ok()); |
| 1919 |
let r2 = queue.run_sync(|| shared.take()).unwrap(); |
| 1920 |
assert!(r2.is_ok()); |
| 1921 |
let r3 = queue.run_sync(|| shared.take()).unwrap(); |
| 1922 |
assert!(r3.is_err()); |
| 1923 |
} |
| 1924 |
|
| 1925 |
#[test] |
| 1926 |
fn test_shared_voice_processing_release_on_idle() { |
| 1927 |
let queue = Queue::new_with_target( |
| 1928 |
"test_shared_voice_processing_release_on_idle", |
| 1929 |
get_serial_queue_singleton(), |
| 1930 |
); |
| 1931 |
let mut shared = SharedVoiceProcessingUnitManager::with_idle_timeout( |
| 1932 |
queue.clone(), |
| 1933 |
Duration::from_millis(0), |
| 1934 |
); |
| 1935 |
let r = queue.run_sync(|| shared.take_or_create()).unwrap(); |
| 1936 |
assert!(r.is_ok()); |
| 1937 |
{ |
| 1938 |
let _handle = r.unwrap(); |
| 1939 |
} |
| 1940 |
queue.run_sync(|| {}); |
| 1941 |
let r = queue.run_sync(|| shared.take()).unwrap(); |
| 1942 |
assert!(r.is_err()); |
| 1943 |
} |
| 1944 |
|
| 1945 |
#[test] |
| 1946 |
fn test_shared_voice_processing_no_release_on_outstanding() { |
| 1947 |
let queue = Queue::new_with_target( |
| 1948 |
"test_shared_voice_processing_no_release_on_outstanding", |
| 1949 |
get_serial_queue_singleton(), |
| 1950 |
); |
| 1951 |
let mut shared = SharedVoiceProcessingUnitManager::with_idle_timeout( |
| 1952 |
queue.clone(), |
| 1953 |
Duration::from_millis(0), |
| 1954 |
); |
| 1955 |
let r1 = queue.run_sync(|| shared.take_or_create()).unwrap(); |
| 1956 |
assert!(r1.is_ok()); |
| 1957 |
let r2 = queue.run_sync(|| shared.take_or_create()).unwrap(); |
| 1958 |
assert!(r2.is_ok()); |
| 1959 |
{ |
| 1960 |
let _handle1 = r1.unwrap(); |
| 1961 |
} |
| 1962 |
queue.run_sync(|| {}); |
| 1963 |
let r1 = queue.run_sync(|| shared.take()).unwrap(); |
| 1964 |
assert!(r1.is_ok()); |
| 1965 |
} |
| 1966 |
|
| 1967 |
#[test] |
| 1968 |
fn test_shared_voice_processing_release_on_idle_cancel_on_take() { |
| 1969 |
let queue = Queue::new_with_target( |
| 1970 |
"test_shared_voice_processing_release_on_idle_cancel_on_take", |
| 1971 |
get_serial_queue_singleton(), |
| 1972 |
); |
| 1973 |
let mut shared = SharedVoiceProcessingUnitManager::with_idle_timeout( |
| 1974 |
queue.clone(), |
| 1975 |
Duration::from_millis(0), |
| 1976 |
); |
| 1977 |
let r1 = queue.run_sync(|| shared.take_or_create()).unwrap(); |
| 1978 |
assert!(r1.is_ok()); |
| 1979 |
let r2 = queue.run_sync(|| shared.take_or_create()).unwrap(); |
| 1980 |
assert!(r2.is_ok()); |
| 1981 |
let r1 = queue |
| 1982 |
.run_sync(|| { |
| 1983 |
{ |
| 1984 |
let _handle1 = r1.unwrap(); |
| 1985 |
let _handle2 = r2.unwrap(); |
| 1986 |
} |
| 1987 |
shared.take() |
| 1988 |
}) |
| 1989 |
.unwrap(); |
| 1990 |
assert!(r1.is_ok()); |
| 1991 |
queue.run_sync(|| {}); |
| 1992 |
let r2 = queue.run_sync(|| shared.take()).unwrap(); |
| 1993 |
assert!(r2.is_ok()); |
| 1994 |
} |