| 1 |
use super::utils::{ |
| 2 |
noop_data_callback, test_audiounit_get_buffer_frame_size, test_get_default_audiounit, |
| 3 |
test_get_default_device, test_ops_context_operation, PropertyScope, Scope, |
| 4 |
}; |
| 5 |
use super::*; |
| 6 |
use std::thread; |
| 7 |
|
| 8 |
// Ignore the test by default to avoid overwritting the buffer frame size of the device that is |
| 9 |
// currently used by other streams in other tests. |
| 10 |
#[ignore] |
| 11 |
#[test] |
| 12 |
fn test_parallel_ops_init_streams_in_parallel_input() { |
| 13 |
const THREADS: u32 = 50; |
| 14 |
create_streams_by_ops_in_parallel_with_different_latency( |
| 15 |
THREADS, |
| 16 |
StreamType::Input, |
| 17 |
|streams| { |
| 18 |
// All the latency frames should be the same value as the first stream's one, since the |
| 19 |
// latency frames cannot be changed if another stream is operating in parallel. |
| 20 |
let mut latency_frames = vec![]; |
| 21 |
let mut in_buffer_frame_sizes = vec![]; |
| 22 |
|
| 23 |
for stream in streams { |
| 24 |
latency_frames.push(stream.latency_frames); |
| 25 |
|
| 26 |
assert!(!stream.core_stream_data.input_unit.is_null()); |
| 27 |
let in_buffer_frame_size = test_audiounit_get_buffer_frame_size( |
| 28 |
stream.core_stream_data.input_unit, |
| 29 |
Scope::Input, |
| 30 |
PropertyScope::Output, |
| 31 |
) |
| 32 |
.unwrap(); |
| 33 |
in_buffer_frame_sizes.push(in_buffer_frame_size); |
| 34 |
|
| 35 |
assert!(stream.core_stream_data.output_unit.is_null()); |
| 36 |
} |
| 37 |
|
| 38 |
// Make sure all the latency frames are same as the first stream's one. |
| 39 |
for i in 0..latency_frames.len() - 1 { |
| 40 |
assert_eq!(latency_frames[i], latency_frames[i + 1]); |
| 41 |
} |
| 42 |
|
| 43 |
// Make sure all the buffer frame sizes on output scope of the input audiounit are same |
| 44 |
// as the defined latency of the first initial stream. |
| 45 |
for i in 0..in_buffer_frame_sizes.len() - 1 { |
| 46 |
assert_eq!(in_buffer_frame_sizes[i], in_buffer_frame_sizes[i + 1]); |
| 47 |
} |
| 48 |
}, |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
// Ignore the test by default to avoid overwritting the buffer frame size of the device that is |
| 53 |
// currently used by other streams in other tests. |
| 54 |
#[ignore] |
| 55 |
#[test] |
| 56 |
fn test_parallel_ops_init_streams_in_parallel_output() { |
| 57 |
const THREADS: u32 = 50; |
| 58 |
create_streams_by_ops_in_parallel_with_different_latency( |
| 59 |
THREADS, |
| 60 |
StreamType::Output, |
| 61 |
|streams| { |
| 62 |
// All the latency frames should be the same value as the first stream's one, since the |
| 63 |
// latency frames cannot be changed if another stream is operating in parallel. |
| 64 |
let mut latency_frames = vec![]; |
| 65 |
let mut out_buffer_frame_sizes = vec![]; |
| 66 |
|
| 67 |
for stream in streams { |
| 68 |
latency_frames.push(stream.latency_frames); |
| 69 |
|
| 70 |
assert!(stream.core_stream_data.input_unit.is_null()); |
| 71 |
|
| 72 |
assert!(!stream.core_stream_data.output_unit.is_null()); |
| 73 |
let out_buffer_frame_size = test_audiounit_get_buffer_frame_size( |
| 74 |
stream.core_stream_data.output_unit, |
| 75 |
Scope::Output, |
| 76 |
PropertyScope::Input, |
| 77 |
) |
| 78 |
.unwrap(); |
| 79 |
out_buffer_frame_sizes.push(out_buffer_frame_size); |
| 80 |
} |
| 81 |
|
| 82 |
// Make sure all the latency frames are same as the first stream's one. |
| 83 |
for i in 0..latency_frames.len() - 1 { |
| 84 |
assert_eq!(latency_frames[i], latency_frames[i + 1]); |
| 85 |
} |
| 86 |
|
| 87 |
// Make sure all the buffer frame sizes on input scope of the output audiounit are same |
| 88 |
// as the defined latency of the first initial stream. |
| 89 |
for i in 0..out_buffer_frame_sizes.len() - 1 { |
| 90 |
assert_eq!(out_buffer_frame_sizes[i], out_buffer_frame_sizes[i + 1]); |
| 91 |
} |
| 92 |
}, |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
// Ignore the test by default to avoid overwritting the buffer frame size of the device that is |
| 97 |
// currently used by other streams in other tests. |
| 98 |
#[ignore] |
| 99 |
#[test] |
| 100 |
fn test_parallel_ops_init_streams_in_parallel_duplex() { |
| 101 |
const THREADS: u32 = 50; |
| 102 |
create_streams_by_ops_in_parallel_with_different_latency( |
| 103 |
THREADS, |
| 104 |
StreamType::Duplex, |
| 105 |
|streams| { |
| 106 |
// All the latency frames should be the same value as the first stream's one, since the |
| 107 |
// latency frames cannot be changed if another stream is operating in parallel. |
| 108 |
let mut latency_frames = vec![]; |
| 109 |
let mut in_buffer_frame_sizes = vec![]; |
| 110 |
let mut out_buffer_frame_sizes = vec![]; |
| 111 |
|
| 112 |
for stream in streams { |
| 113 |
latency_frames.push(stream.latency_frames); |
| 114 |
|
| 115 |
assert!(!stream.core_stream_data.input_unit.is_null()); |
| 116 |
let in_buffer_frame_size = test_audiounit_get_buffer_frame_size( |
| 117 |
stream.core_stream_data.input_unit, |
| 118 |
Scope::Input, |
| 119 |
PropertyScope::Output, |
| 120 |
) |
| 121 |
.unwrap(); |
| 122 |
in_buffer_frame_sizes.push(in_buffer_frame_size); |
| 123 |
|
| 124 |
assert!(!stream.core_stream_data.output_unit.is_null()); |
| 125 |
let out_buffer_frame_size = test_audiounit_get_buffer_frame_size( |
| 126 |
stream.core_stream_data.output_unit, |
| 127 |
Scope::Output, |
| 128 |
PropertyScope::Input, |
| 129 |
) |
| 130 |
.unwrap(); |
| 131 |
out_buffer_frame_sizes.push(out_buffer_frame_size); |
| 132 |
} |
| 133 |
|
| 134 |
// Make sure all the latency frames are same as the first stream's one. |
| 135 |
for i in 0..latency_frames.len() - 1 { |
| 136 |
assert_eq!(latency_frames[i], latency_frames[i + 1]); |
| 137 |
} |
| 138 |
|
| 139 |
// Make sure all the buffer frame sizes on output scope of the input audiounit are same |
| 140 |
// as the defined latency of the first initial stream. |
| 141 |
for i in 0..in_buffer_frame_sizes.len() - 1 { |
| 142 |
assert_eq!(in_buffer_frame_sizes[i], in_buffer_frame_sizes[i + 1]); |
| 143 |
} |
| 144 |
|
| 145 |
// Make sure all the buffer frame sizes on input scope of the output audiounit are same |
| 146 |
// as the defined latency of the first initial stream. |
| 147 |
for i in 0..out_buffer_frame_sizes.len() - 1 { |
| 148 |
assert_eq!(out_buffer_frame_sizes[i], out_buffer_frame_sizes[i + 1]); |
| 149 |
} |
| 150 |
}, |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
fn create_streams_by_ops_in_parallel_with_different_latency<F>( |
| 155 |
amount: u32, |
| 156 |
stm_type: StreamType, |
| 157 |
callback: F, |
| 158 |
) where |
| 159 |
F: FnOnce(Vec<&AudioUnitStream>), |
| 160 |
{ |
| 161 |
let default_input = test_get_default_device(Scope::Input); |
| 162 |
let default_output = test_get_default_device(Scope::Output); |
| 163 |
|
| 164 |
let has_input = stm_type == StreamType::Input || stm_type == StreamType::Duplex; |
| 165 |
let has_output = stm_type == StreamType::Output || stm_type == StreamType::Duplex; |
| 166 |
|
| 167 |
if has_input && default_input.is_none() { |
| 168 |
println!("No input device to perform the test."); |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
if has_output && default_output.is_none() { |
| 173 |
println!("No output device to perform the test."); |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
test_ops_context_operation("context: init and destroy", |context_ptr| { |
| 178 |
let context_ptr_value = context_ptr as usize; |
| 179 |
|
| 180 |
let mut join_handles = vec![]; |
| 181 |
for i in 0..amount { |
| 182 |
// Make sure the parameters meet the requirements of AudioUnitContext::stream_init |
| 183 |
// (in the comments). |
| 184 |
let mut input_params = ffi::cubeb_stream_params::default(); |
| 185 |
input_params.format = ffi::CUBEB_SAMPLE_FLOAT32NE; |
| 186 |
input_params.rate = 48_000; |
| 187 |
input_params.channels = 1; |
| 188 |
input_params.layout = ffi::CUBEB_LAYOUT_UNDEFINED; |
| 189 |
input_params.prefs = ffi::CUBEB_STREAM_PREF_NONE; |
| 190 |
|
| 191 |
let mut output_params = ffi::cubeb_stream_params::default(); |
| 192 |
output_params.format = ffi::CUBEB_SAMPLE_FLOAT32NE; |
| 193 |
output_params.rate = 44100; |
| 194 |
output_params.channels = 2; |
| 195 |
output_params.layout = ffi::CUBEB_LAYOUT_UNDEFINED; |
| 196 |
output_params.prefs = ffi::CUBEB_STREAM_PREF_NONE; |
| 197 |
|
| 198 |
// Latency cannot be changed if another stream is operating in parallel. All the latecy |
| 199 |
// should be set to the same latency value of the first stream that is operating in the |
| 200 |
// context. |
| 201 |
let latency_frames = SAFE_MIN_LATENCY_FRAMES + i; |
| 202 |
assert!(latency_frames < SAFE_MAX_LATENCY_FRAMES); |
| 203 |
|
| 204 |
// Create many streams within the same context. The order of the stream creation |
| 205 |
// is random (The order of execution of the spawned threads is random.).assert! |
| 206 |
// It's super dangerous to pass `context_ptr_value` across threads and convert it back |
| 207 |
// to a pointer. However, it's the cheapest way to make sure the inside mutex works. |
| 208 |
let thread_name = format!("stream {} @ context {:?}", i, context_ptr); |
| 209 |
join_handles.push( |
| 210 |
thread::Builder::new() |
| 211 |
.name(thread_name) |
| 212 |
.spawn(move || { |
| 213 |
let context_ptr = context_ptr_value as *mut ffi::cubeb; |
| 214 |
let mut stream: *mut ffi::cubeb_stream = ptr::null_mut(); |
| 215 |
let stream_name = CString::new(format!("stream {}", i)).unwrap(); |
| 216 |
assert_eq!( |
| 217 |
unsafe { |
| 218 |
OPS.stream_init.unwrap()( |
| 219 |
context_ptr, |
| 220 |
&mut stream, |
| 221 |
stream_name.as_ptr(), |
| 222 |
ptr::null_mut(), // Use default input device. |
| 223 |
if has_input { |
| 224 |
&mut input_params |
| 225 |
} else { |
| 226 |
ptr::null_mut() |
| 227 |
}, |
| 228 |
ptr::null_mut(), // Use default output device. |
| 229 |
if has_output { |
| 230 |
&mut output_params |
| 231 |
} else { |
| 232 |
ptr::null_mut() |
| 233 |
}, |
| 234 |
latency_frames, |
| 235 |
Some(noop_data_callback), |
| 236 |
None, // No state callback. |
| 237 |
ptr::null_mut(), // No user data pointer. |
| 238 |
) |
| 239 |
}, |
| 240 |
ffi::CUBEB_OK |
| 241 |
); |
| 242 |
assert!(!stream.is_null()); |
| 243 |
stream as usize |
| 244 |
}) |
| 245 |
.unwrap(), |
| 246 |
); |
| 247 |
} |
| 248 |
|
| 249 |
let mut streams = vec![]; |
| 250 |
// Wait for finishing the tasks on the different threads. |
| 251 |
for handle in join_handles { |
| 252 |
let stream_ptr_value = handle.join().unwrap(); |
| 253 |
let stream = unsafe { Box::from_raw(stream_ptr_value as *mut AudioUnitStream) }; |
| 254 |
streams.push(stream); |
| 255 |
} |
| 256 |
|
| 257 |
let stream_refs: Vec<&AudioUnitStream> = streams.iter().map(|stm| stm.as_ref()).collect(); |
| 258 |
callback(stream_refs); |
| 259 |
}); |
| 260 |
} |
| 261 |
|
| 262 |
// Ignore the test by default to avoid overwritting the buffer frame size of the device that is |
| 263 |
// currently used by other streams in other tests. |
| 264 |
#[ignore] |
| 265 |
#[test] |
| 266 |
fn test_parallel_init_streams_in_parallel_input() { |
| 267 |
const THREADS: u32 = 10; |
| 268 |
create_streams_in_parallel_with_different_latency(THREADS, StreamType::Input, |streams| { |
| 269 |
// All the latency frames should be the same value as the first stream's one, since the |
| 270 |
// latency frames cannot be changed if another stream is operating in parallel. |
| 271 |
let mut latency_frames = vec![]; |
| 272 |
let mut in_buffer_frame_sizes = vec![]; |
| 273 |
|
| 274 |
for stream in streams { |
| 275 |
latency_frames.push(stream.latency_frames); |
| 276 |
|
| 277 |
assert!(!stream.core_stream_data.input_unit.is_null()); |
| 278 |
let in_buffer_frame_size = test_audiounit_get_buffer_frame_size( |
| 279 |
stream.core_stream_data.input_unit, |
| 280 |
Scope::Input, |
| 281 |
PropertyScope::Output, |
| 282 |
) |
| 283 |
.unwrap(); |
| 284 |
in_buffer_frame_sizes.push(in_buffer_frame_size); |
| 285 |
|
| 286 |
assert!(stream.core_stream_data.output_unit.is_null()); |
| 287 |
} |
| 288 |
|
| 289 |
// Make sure all the latency frames are same as the first stream's one. |
| 290 |
for i in 0..latency_frames.len() - 1 { |
| 291 |
assert_eq!(latency_frames[i], latency_frames[i + 1]); |
| 292 |
} |
| 293 |
|
| 294 |
// Make sure all the buffer frame sizes on output scope of the input audiounit are same |
| 295 |
// as the defined latency of the first initial stream. |
| 296 |
for i in 0..in_buffer_frame_sizes.len() - 1 { |
| 297 |
assert_eq!(in_buffer_frame_sizes[i], in_buffer_frame_sizes[i + 1]); |
| 298 |
} |
| 299 |
}); |
| 300 |
} |
| 301 |
|
| 302 |
// Ignore the test by default to avoid overwritting the buffer frame size of the device that is |
| 303 |
// currently used by other streams in other tests. |
| 304 |
#[ignore] |
| 305 |
#[test] |
| 306 |
fn test_parallel_init_streams_in_parallel_output() { |
| 307 |
const THREADS: u32 = 10; |
| 308 |
create_streams_in_parallel_with_different_latency(THREADS, StreamType::Output, |streams| { |
| 309 |
// All the latency frames should be the same value as the first stream's one, since the |
| 310 |
// latency frames cannot be changed if another stream is operating in parallel. |
| 311 |
let mut latency_frames = vec![]; |
| 312 |
let mut out_buffer_frame_sizes = vec![]; |
| 313 |
|
| 314 |
for stream in streams { |
| 315 |
latency_frames.push(stream.latency_frames); |
| 316 |
|
| 317 |
assert!(stream.core_stream_data.input_unit.is_null()); |
| 318 |
|
| 319 |
assert!(!stream.core_stream_data.output_unit.is_null()); |
| 320 |
let out_buffer_frame_size = test_audiounit_get_buffer_frame_size( |
| 321 |
stream.core_stream_data.output_unit, |
| 322 |
Scope::Output, |
| 323 |
PropertyScope::Input, |
| 324 |
) |
| 325 |
.unwrap(); |
| 326 |
out_buffer_frame_sizes.push(out_buffer_frame_size); |
| 327 |
} |
| 328 |
|
| 329 |
// Make sure all the latency frames are same as the first stream's one. |
| 330 |
for i in 0..latency_frames.len() - 1 { |
| 331 |
assert_eq!(latency_frames[i], latency_frames[i + 1]); |
| 332 |
} |
| 333 |
|
| 334 |
// Make sure all the buffer frame sizes on input scope of the output audiounit are same |
| 335 |
// as the defined latency of the first initial stream. |
| 336 |
for i in 0..out_buffer_frame_sizes.len() - 1 { |
| 337 |
assert_eq!(out_buffer_frame_sizes[i], out_buffer_frame_sizes[i + 1]); |
| 338 |
} |
| 339 |
}); |
| 340 |
} |
| 341 |
|
| 342 |
// Ignore the test by default to avoid overwritting the buffer frame size of the device that is |
| 343 |
// currently used by other streams in other tests. |
| 344 |
#[ignore] |
| 345 |
#[test] |
| 346 |
fn test_parallel_init_streams_in_parallel_duplex() { |
| 347 |
const THREADS: u32 = 10; |
| 348 |
create_streams_in_parallel_with_different_latency(THREADS, StreamType::Duplex, |streams| { |
| 349 |
// All the latency frames should be the same value as the first stream's one, since the |
| 350 |
// latency frames cannot be changed if another stream is operating in parallel. |
| 351 |
let mut latency_frames = vec![]; |
| 352 |
let mut in_buffer_frame_sizes = vec![]; |
| 353 |
let mut out_buffer_frame_sizes = vec![]; |
| 354 |
|
| 355 |
for stream in streams { |
| 356 |
latency_frames.push(stream.latency_frames); |
| 357 |
|
| 358 |
assert!(!stream.core_stream_data.input_unit.is_null()); |
| 359 |
let in_buffer_frame_size = test_audiounit_get_buffer_frame_size( |
| 360 |
stream.core_stream_data.input_unit, |
| 361 |
Scope::Input, |
| 362 |
PropertyScope::Output, |
| 363 |
) |
| 364 |
.unwrap(); |
| 365 |
in_buffer_frame_sizes.push(in_buffer_frame_size); |
| 366 |
|
| 367 |
assert!(!stream.core_stream_data.output_unit.is_null()); |
| 368 |
let out_buffer_frame_size = test_audiounit_get_buffer_frame_size( |
| 369 |
stream.core_stream_data.output_unit, |
| 370 |
Scope::Output, |
| 371 |
PropertyScope::Input, |
| 372 |
) |
| 373 |
.unwrap(); |
| 374 |
out_buffer_frame_sizes.push(out_buffer_frame_size); |
| 375 |
} |
| 376 |
|
| 377 |
// Make sure all the latency frames are same as the first stream's one. |
| 378 |
for i in 0..latency_frames.len() - 1 { |
| 379 |
assert_eq!(latency_frames[i], latency_frames[i + 1]); |
| 380 |
} |
| 381 |
|
| 382 |
// Make sure all the buffer frame sizes on output scope of the input audiounit are same |
| 383 |
// as the defined latency of the first initial stream. |
| 384 |
for i in 0..in_buffer_frame_sizes.len() - 1 { |
| 385 |
assert_eq!(in_buffer_frame_sizes[i], in_buffer_frame_sizes[i + 1]); |
| 386 |
} |
| 387 |
|
| 388 |
// Make sure all the buffer frame sizes on input scope of the output audiounit are same |
| 389 |
// as the defined latency of the first initial stream. |
| 390 |
for i in 0..out_buffer_frame_sizes.len() - 1 { |
| 391 |
assert_eq!(out_buffer_frame_sizes[i], out_buffer_frame_sizes[i + 1]); |
| 392 |
} |
| 393 |
}); |
| 394 |
} |
| 395 |
|
| 396 |
fn create_streams_in_parallel_with_different_latency<F>( |
| 397 |
amount: u32, |
| 398 |
stm_type: StreamType, |
| 399 |
callback: F, |
| 400 |
) where |
| 401 |
F: FnOnce(Vec<&AudioUnitStream>), |
| 402 |
{ |
| 403 |
let default_input = test_get_default_device(Scope::Input); |
| 404 |
let default_output = test_get_default_device(Scope::Output); |
| 405 |
|
| 406 |
let has_input = stm_type == StreamType::Input || stm_type == StreamType::Duplex; |
| 407 |
let has_output = stm_type == StreamType::Output || stm_type == StreamType::Duplex; |
| 408 |
|
| 409 |
if has_input && default_input.is_none() { |
| 410 |
println!("No input device to perform the test."); |
| 411 |
return; |
| 412 |
} |
| 413 |
|
| 414 |
if has_output && default_output.is_none() { |
| 415 |
println!("No output device to perform the test."); |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
let mut context = AudioUnitContext::new(); |
| 420 |
|
| 421 |
let context_ptr_value = &mut context as *mut AudioUnitContext as usize; |
| 422 |
|
| 423 |
let mut join_handles = vec![]; |
| 424 |
for i in 0..amount { |
| 425 |
// Make sure the parameters meet the requirements of AudioUnitContext::stream_init |
| 426 |
// (in the comments). |
| 427 |
let mut input_params = ffi::cubeb_stream_params::default(); |
| 428 |
input_params.format = ffi::CUBEB_SAMPLE_FLOAT32NE; |
| 429 |
input_params.rate = 48_000; |
| 430 |
input_params.channels = 1; |
| 431 |
input_params.layout = ffi::CUBEB_LAYOUT_UNDEFINED; |
| 432 |
input_params.prefs = ffi::CUBEB_STREAM_PREF_NONE; |
| 433 |
|
| 434 |
let mut output_params = ffi::cubeb_stream_params::default(); |
| 435 |
output_params.format = ffi::CUBEB_SAMPLE_FLOAT32NE; |
| 436 |
output_params.rate = 44100; |
| 437 |
output_params.channels = 2; |
| 438 |
output_params.layout = ffi::CUBEB_LAYOUT_UNDEFINED; |
| 439 |
output_params.prefs = ffi::CUBEB_STREAM_PREF_NONE; |
| 440 |
|
| 441 |
// Latency cannot be changed if another stream is operating in parallel. All the latecy |
| 442 |
// should be set to the same latency value of the first stream that is operating in the |
| 443 |
// context. |
| 444 |
let latency_frames = SAFE_MIN_LATENCY_FRAMES + i; |
| 445 |
assert!(latency_frames < SAFE_MAX_LATENCY_FRAMES); |
| 446 |
|
| 447 |
// Create many streams within the same context. The order of the stream creation |
| 448 |
// is random. (The order of execution of the spawned threads is random.) |
| 449 |
// It's super dangerous to pass `context_ptr_value` across threads and convert it back |
| 450 |
// to a reference. However, it's the cheapest way to make sure the inside mutex works. |
| 451 |
let thread_name = format!("stream {} @ context {:?}", i, context_ptr_value); |
| 452 |
join_handles.push( |
| 453 |
thread::Builder::new() |
| 454 |
.name(thread_name) |
| 455 |
.spawn(move || { |
| 456 |
let context = unsafe { &mut *(context_ptr_value as *mut AudioUnitContext) }; |
| 457 |
let input_params = unsafe { StreamParamsRef::from_ptr(&mut input_params) }; |
| 458 |
let output_params = unsafe { StreamParamsRef::from_ptr(&mut output_params) }; |
| 459 |
let stream = context |
| 460 |
.stream_init( |
| 461 |
None, |
| 462 |
ptr::null_mut(), // Use default input device. |
| 463 |
if has_input { Some(input_params) } else { None }, |
| 464 |
ptr::null_mut(), // Use default output device. |
| 465 |
if has_output { |
| 466 |
Some(output_params) |
| 467 |
} else { |
| 468 |
None |
| 469 |
}, |
| 470 |
latency_frames, |
| 471 |
Some(noop_data_callback), |
| 472 |
None, // No state callback. |
| 473 |
ptr::null_mut(), // No user data pointer. |
| 474 |
) |
| 475 |
.unwrap(); |
| 476 |
assert!(!stream.as_ptr().is_null()); |
| 477 |
let stream_ptr_value = stream.as_ptr() as usize; |
| 478 |
// Prevent the stream from being destroyed by leaking this stream. |
| 479 |
mem::forget(stream); |
| 480 |
stream_ptr_value |
| 481 |
}) |
| 482 |
.unwrap(), |
| 483 |
); |
| 484 |
} |
| 485 |
|
| 486 |
let mut streams = vec![]; |
| 487 |
// Wait for finishing the tasks on the different threads. |
| 488 |
for handle in join_handles { |
| 489 |
let stream_ptr_value = handle.join().unwrap(); |
| 490 |
// Retake the leaked stream. |
| 491 |
let stream = unsafe { Box::from_raw(stream_ptr_value as *mut AudioUnitStream) }; |
| 492 |
streams.push(stream); |
| 493 |
} |
| 494 |
|
| 495 |
let stream_refs: Vec<&AudioUnitStream> = streams.iter().map(|stm| stm.as_ref()).collect(); |
| 496 |
callback(stream_refs); |
| 497 |
} |
| 498 |
|
| 499 |
#[derive(Debug, PartialEq)] |
| 500 |
enum StreamType { |
| 501 |
Input, |
| 502 |
Output, |
| 503 |
Duplex, |
| 504 |
} |
| 505 |
|
| 506 |
// This is used to interfere other active streams. |
| 507 |
// From this testing, it's ok to set the buffer frame size of a device that is currently used by |
| 508 |
// other tests. It works on OSX 10.13, not sure if it works on other versions. |
| 509 |
// However, other tests may check the buffer frame size they set at the same time, |
| 510 |
// so we ignore this by default incase those checks fail. |
| 511 |
#[ignore] |
| 512 |
#[test] |
| 513 |
fn test_set_buffer_frame_size_in_parallel() { |
| 514 |
test_set_buffer_frame_size_in_parallel_in_scope(Scope::Input); |
| 515 |
test_set_buffer_frame_size_in_parallel_in_scope(Scope::Output); |
| 516 |
} |
| 517 |
|
| 518 |
fn test_set_buffer_frame_size_in_parallel_in_scope(scope: Scope) { |
| 519 |
const THREADS: u32 = 100; |
| 520 |
|
| 521 |
let unit = test_get_default_audiounit(scope.clone()); |
| 522 |
if unit.is_none() { |
| 523 |
println!("No unit for {:?}", scope); |
| 524 |
return; |
| 525 |
} |
| 526 |
|
| 527 |
let (unit_scope, unit_element, prop_scope) = match scope { |
| 528 |
Scope::Input => (kAudioUnitScope_Output, AU_IN_BUS, PropertyScope::Output), |
| 529 |
Scope::Output => (kAudioUnitScope_Input, AU_OUT_BUS, PropertyScope::Input), |
| 530 |
}; |
| 531 |
|
| 532 |
let mut units = vec![]; |
| 533 |
let mut join_handles = vec![]; |
| 534 |
for i in 0..THREADS { |
| 535 |
let latency_frames = SAFE_MIN_LATENCY_FRAMES + i; |
| 536 |
assert!(latency_frames < SAFE_MAX_LATENCY_FRAMES); |
| 537 |
units.push(test_get_default_audiounit(scope.clone()).unwrap()); |
| 538 |
let unit_value = units.last().unwrap().get_inner() as usize; |
| 539 |
join_handles.push(thread::spawn(move || { |
| 540 |
let status = run_serially(|| { |
| 541 |
audio_unit_set_property( |
| 542 |
unit_value as AudioUnit, |
| 543 |
kAudioDevicePropertyBufferFrameSize, |
| 544 |
unit_scope, |
| 545 |
unit_element, |
| 546 |
&latency_frames, |
| 547 |
mem::size_of::<u32>(), |
| 548 |
) |
| 549 |
}); |
| 550 |
(latency_frames, status) |
| 551 |
})); |
| 552 |
} |
| 553 |
|
| 554 |
let mut latencies = vec![]; |
| 555 |
let mut statuses = vec![]; |
| 556 |
for handle in join_handles { |
| 557 |
let (latency, status) = handle.join().unwrap(); |
| 558 |
latencies.push(latency); |
| 559 |
statuses.push(status); |
| 560 |
} |
| 561 |
|
| 562 |
let mut buffer_frames_list = vec![]; |
| 563 |
for unit in units.iter() { |
| 564 |
buffer_frames_list.push(unit.get_buffer_frame_size(scope.clone(), prop_scope.clone())); |
| 565 |
} |
| 566 |
|
| 567 |
for status in statuses { |
| 568 |
assert_eq!(status, NO_ERR); |
| 569 |
} |
| 570 |
|
| 571 |
for i in 0..buffer_frames_list.len() - 1 { |
| 572 |
assert_eq!(buffer_frames_list[i], buffer_frames_list[i + 1]); |
| 573 |
} |
| 574 |
} |