Debian Codegraph @

third_party/rust/cubeb-coreaudio/src/backend/tests/aggregate_device.rs

raw
1 extern crate itertools;
2
3 use super::utils::{
4 test_get_all_devices, test_get_all_onwed_devices, test_get_default_device,
5 test_get_drift_compensations, test_get_master_device, DeviceFilter, Scope,
6 };
7 use super::*;
8 use std::collections::HashSet;
9 use std::iter::zip;
10 use std::panic;
11
12 // AggregateDevice::set_sub_devices
13 // ------------------------------------
14 #[test]
15 #[should_panic]
16 fn test_aggregate_set_sub_devices_for_an_unknown_aggregate_device() {
17 // If aggregate device id is kAudioObjectUnknown, we are unable to set device list.
18 let default_input = test_get_default_device(Scope::Input);
19 let default_output = test_get_default_device(Scope::Output);
20 if default_input.is_none() || default_output.is_none() {
21 panic!("No input or output device.");
22 }
23
24 let default_input = default_input.unwrap();
25 let default_output = default_output.unwrap();
26 assert!(
27 run_serially_forward_panics(|| AggregateDevice::set_sub_devices(
28 kAudioObjectUnknown,
29 default_input,
30 default_output
31 ))
32 .is_err()
33 );
34 }
35
36 #[test]
37 #[should_panic]
38 fn test_aggregate_set_sub_devices_for_unknown_devices() {
39 run_serially_forward_panics(|| {
40 // If aggregate device id is kAudioObjectUnknown, we are unable to set device list.
41 assert!(AggregateDevice::set_sub_devices(
42 kAudioObjectUnknown,
43 kAudioObjectUnknown,
44 kAudioObjectUnknown
45 )
46 .is_err());
47 });
48 }
49
50 // AggregateDevice::get_sub_devices
51 // ------------------------------------
52 #[test]
53 #[ignore]
54 fn test_aggregate_get_sub_devices() {
55 fn diff(lhs: Vec<u32>, rhs: Vec<u32>) -> Vec<u32> {
56 let left: HashSet<u32> = lhs.into_iter().collect();
57 let right: HashSet<u32> = rhs.into_iter().collect();
58 left.symmetric_difference(&right)
59 .map(|&i| i.clone())
60 .collect()
61 }
62
63 // Run in a large block so other test cases cannot add or remove devices while this runs.
64 let input_device = test_get_default_device(Scope::Input);
65 let output_device = test_get_default_device(Scope::Output);
66 if input_device.is_none() || output_device.is_none() || input_device == output_device {
67 println!("No input and output device to create an aggregate device.");
68 return;
69 }
70 let devices_base = test_get_all_devices(DeviceFilter::ExcludeVPIO);
71 run_serially_forward_panics(|| {
72 assert!(
73 devices_base
74 .clone()
75 .into_iter()
76 .map(AggregateDevice::get_sub_devices)
77 .any(|r| r.is_err()),
78 "There should be some device that is not an aggregate."
79 )
80 });
81
82 {
83 // Test get_sub_devices on an empty aggregate device.
84 let plugin_id = AggregateDevice::get_system_plugin_id().unwrap();
85 let aggr = run_serially_forward_panics(|| AggregateDevice::create_blank_device(plugin_id))
86 .unwrap();
87 let new = diff(
88 devices_base.clone(),
89 test_get_all_devices(DeviceFilter::ExcludeVPIO),
90 );
91 assert_eq!(new.len(), 1);
92 let new_subs = run_serially_forward_panics(|| AggregateDevice::get_sub_devices(new[0]));
93 assert!(new_subs.is_ok());
94 assert_eq!(new_subs.unwrap().len(), 0);
95 assert!(
96 run_serially_forward_panics(|| AggregateDevice::destroy_device(plugin_id, aggr))
97 .is_ok()
98 );
99 }
100
101 {
102 // Test get_sub_devices on an aggregate device with two sub devices.
103 let aggr = run_serially_forward_panics(|| {
104 let input_device = input_device.unwrap();
105 let output_device = output_device.unwrap();
106
107 AggregateDevice::new(input_device, output_device)
108 })
109 .unwrap();
110 let new = diff(
111 devices_base.clone(),
112 test_get_all_devices(DeviceFilter::ExcludeVPIO),
113 );
114 assert_eq!(new.len(), 1);
115 let new_subs = run_serially_forward_panics(|| AggregateDevice::get_sub_devices(new[0]));
116 assert!(new_subs.is_ok());
117 assert_eq!(new_subs.unwrap().len(), 2);
118 run_serially_forward_panics(|| drop(aggr));
119 }
120 }
121
122 #[test]
123 #[should_panic]
124 fn test_aggregate_get_sub_devices_for_a_unknown_device() {
125 run_serially_forward_panics(|| {
126 AggregateDevice::get_sub_devices(kAudioObjectUnknown);
127 });
128 }
129
130 // AggregateDevice::set_master_device
131 // ------------------------------------
132 #[test]
133 #[should_panic]
134 fn test_aggregate_set_master_device_for_an_unknown_aggregate_device() {
135 run_serially_forward_panics(|| {
136 assert!(
137 AggregateDevice::set_master_device(kAudioObjectUnknown, kAudioObjectUnknown).is_err()
138 );
139 });
140 }
141
142 // AggregateDevice::activate_clock_drift_compensation
143 // ------------------------------------
144 #[test]
145 #[should_panic]
146 fn test_aggregate_activate_clock_drift_compensation_for_an_unknown_aggregate_device() {
147 run_serially_forward_panics(|| {
148 assert!(AggregateDevice::activate_clock_drift_compensation(kAudioObjectUnknown).is_err());
149 });
150 }
151
152 // AggregateDevice::destroy_device
153 // ------------------------------------
154 #[test]
155 #[should_panic]
156 fn test_aggregate_destroy_device_for_unknown_plugin_and_aggregate_devices() {
157 run_serially_forward_panics(|| {
158 assert!(AggregateDevice::destroy_device(kAudioObjectUnknown, kAudioObjectUnknown).is_err())
159 });
160 }
161
162 #[test]
163 #[should_panic]
164 fn test_aggregate_destroy_aggregate_device_for_a_unknown_aggregate_device() {
165 run_serially_forward_panics(|| {
166 let plugin = AggregateDevice::get_system_plugin_id().unwrap();
167 assert!(AggregateDevice::destroy_device(plugin, kAudioObjectUnknown).is_err());
168 });
169 }
170
171 // AggregateDevice::create_blank_device_sync
172 // ------------------------------------
173 #[test]
174 fn test_aggregate_create_blank_device() {
175 // TODO: Test this when there is no available devices.
176 let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
177 let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
178 let devices = test_get_all_devices(DeviceFilter::IncludeAll);
179 let device = devices.into_iter().find(|dev| dev == &device).unwrap();
180 let uid = run_serially(|| get_device_global_uid(device).unwrap().into_string());
181 assert!(uid.contains(PRIVATE_AGGREGATE_DEVICE_NAME));
182 assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
183 }
184
185 // AggregateDevice::get_sub_devices
186 // ------------------------------------
187 #[test]
188 fn test_aggregate_get_sub_devices_for_blank_aggregate_devices() {
189 run_serially_forward_panics(|| {
190 // TODO: Test this when there is no available devices.
191 let plugin = AggregateDevice::get_system_plugin_id().unwrap();
192 let device = AggregateDevice::create_blank_device_sync(plugin).unwrap();
193 let sub_devices = AggregateDevice::get_sub_devices(device).unwrap();
194 assert!(sub_devices.is_empty());
195 assert!(AggregateDevice::destroy_device(plugin, device).is_ok());
196 });
197 }
198
199 // AggregateDevice::set_sub_devices_sync
200 // ------------------------------------
201 #[test]
202 fn test_aggregate_set_sub_devices() {
203 let input_device = test_get_default_device(Scope::Input);
204 let output_device = test_get_default_device(Scope::Output);
205 if input_device.is_none() || output_device.is_none() || input_device == output_device {
206 println!("No input or output device to create an aggregate device.");
207 return;
208 }
209
210 let input_device = input_device.unwrap();
211 let output_device = output_device.unwrap();
212
213 let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
214 let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
215 assert!(run_serially(|| AggregateDevice::set_sub_devices_sync(
216 device,
217 input_device,
218 output_device
219 ))
220 .is_ok());
221
222 let sub_devices = run_serially(|| AggregateDevice::get_sub_devices_or_self(device)).unwrap();
223 let input_sub_devices =
224 run_serially(|| AggregateDevice::get_sub_devices_or_self(input_device)).unwrap();
225 let output_sub_devices =
226 run_serially(|| AggregateDevice::get_sub_devices_or_self(output_device)).unwrap();
227
228 // TODO: There may be overlapping devices between input_sub_devices and output_sub_devices,
229 // but now AggregateDevice::set_sub_devices will add them directly.
230 assert_eq!(
231 sub_devices.len(),
232 input_sub_devices.len() + output_sub_devices.len()
233 );
234 for dev in &input_sub_devices {
235 assert!(sub_devices.contains(dev));
236 }
237 for dev in &output_sub_devices {
238 assert!(sub_devices.contains(dev));
239 }
240
241 let onwed_devices = run_serially(|| test_get_all_onwed_devices(device));
242 let onwed_device_uids = run_serially(|| get_device_uids(&onwed_devices));
243 let input_sub_device_uids = run_serially(|| get_device_uids(&input_sub_devices));
244 let output_sub_device_uids = run_serially(|| get_device_uids(&output_sub_devices));
245 for uid in &input_sub_device_uids {
246 assert!(onwed_device_uids.contains(uid));
247 }
248 for uid in &output_sub_device_uids {
249 assert!(onwed_device_uids.contains(uid));
250 }
251
252 assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
253 }
254
255 #[test]
256 #[should_panic]
257 fn test_aggregate_set_sub_devices_for_unknown_input_devices() {
258 let output_device = test_get_default_device(Scope::Output);
259 if output_device.is_none() {
260 panic!("Need a output device for the test!");
261 }
262 let output_device = output_device.unwrap();
263
264 run_serially_forward_panics(|| {
265 let plugin = AggregateDevice::get_system_plugin_id().unwrap();
266 let device = AggregateDevice::create_blank_device_sync(plugin).unwrap();
267
268 assert!(
269 AggregateDevice::set_sub_devices(device, kAudioObjectUnknown, output_device).is_err()
270 );
271
272 assert!(AggregateDevice::destroy_device(plugin, device).is_ok());
273 });
274 }
275
276 #[test]
277 #[should_panic]
278 fn test_aggregate_set_sub_devices_for_unknown_output_devices() {
279 let input_device = test_get_default_device(Scope::Input);
280 if input_device.is_none() {
281 panic!("Need a input device for the test!");
282 }
283 let input_device = input_device.unwrap();
284
285 run_serially_forward_panics(|| {
286 let plugin = AggregateDevice::get_system_plugin_id().unwrap();
287 let device = AggregateDevice::create_blank_device_sync(plugin).unwrap();
288
289 assert!(
290 AggregateDevice::set_sub_devices(device, input_device, kAudioObjectUnknown).is_err()
291 );
292
293 assert!(AggregateDevice::destroy_device(plugin, device).is_ok());
294 });
295 }
296
297 fn get_device_uids(devices: &Vec<AudioObjectID>) -> Vec<String> {
298 devices
299 .iter()
300 .map(|device| get_device_global_uid(*device).unwrap().into_string())
301 .collect()
302 }
303
304 // AggregateDevice::set_master_device
305 // ------------------------------------
306 #[test]
307 fn test_aggregate_set_master_device() {
308 let input_device = test_get_default_device(Scope::Input);
309 let output_device = test_get_default_device(Scope::Output);
310 if input_device.is_none() || output_device.is_none() || input_device == output_device {
311 println!("No input or output device to create an aggregate device.");
312 return;
313 }
314
315 let input_device = input_device.unwrap();
316 let output_device = output_device.unwrap();
317
318 let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
319 let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
320 assert!(run_serially(|| AggregateDevice::set_sub_devices_sync(
321 device,
322 input_device,
323 output_device
324 ))
325 .is_ok());
326 assert!(run_serially(|| AggregateDevice::set_master_device(device, output_device)).is_ok());
327
328 let output_sub_devices =
329 run_serially(|| AggregateDevice::get_sub_devices_or_self(output_device)).unwrap();
330 let first_output_sub_device_uid = run_serially(|| get_device_uid(output_sub_devices[0]));
331
332 // Check that the first sub device of the output device is set as master device.
333 let master_device_uid = run_serially(|| test_get_master_device(device));
334 assert_eq!(first_output_sub_device_uid, master_device_uid);
335
336 assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
337 }
338
339 #[test]
340 fn test_aggregate_set_master_device_for_a_blank_aggregate_device() {
341 let output_device = test_get_default_device(Scope::Output);
342 if output_device.is_none() {
343 println!("No output device to test.");
344 return;
345 }
346
347 let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
348 let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
349 assert!(
350 run_serially(|| AggregateDevice::set_master_device(device, output_device.unwrap())).is_ok()
351 );
352
353 // TODO: it's really weird the aggregate device actually own nothing
354 // but its master device can be set successfully!
355 // The sub devices of this blank aggregate device (by `AggregateDevice::get_sub_devices`)
356 // and the own devices (by `test_get_all_onwed_devices`) is empty since the size returned
357 // from `audio_object_get_property_data_size` is 0.
358 // The CFStringRef of the master device returned from `test_get_master_device` is actually
359 // non-null.
360
361 assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
362 }
363
364 fn get_device_uid(id: AudioObjectID) -> String {
365 get_device_global_uid(id).map_or(String::new(), |uid| uid.into_string())
366 }
367
368 // AggregateDevice::activate_clock_drift_compensation
369 // ------------------------------------
370 #[test]
371 fn test_aggregate_activate_clock_drift_compensation() {
372 let input_device = test_get_default_device(Scope::Input);
373 let output_device = test_get_default_device(Scope::Output);
374 if input_device.is_none() || output_device.is_none() || input_device == output_device {
375 println!("No input or output device to create an aggregate device.");
376 return;
377 }
378
379 let input_device = input_device.unwrap();
380 let output_device = output_device.unwrap();
381
382 let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
383 let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
384 assert!(run_serially(|| AggregateDevice::set_sub_devices_sync(
385 device,
386 input_device,
387 output_device
388 ))
389 .is_ok());
390 assert!(run_serially(|| AggregateDevice::set_master_device(device, output_device)).is_ok());
391 assert!(run_serially(|| AggregateDevice::activate_clock_drift_compensation(device)).is_ok());
392
393 // Check the compensations.
394 let devices = run_serially(|| test_get_all_onwed_devices(device));
395 let compensations = run_serially(|| get_drift_compensations(&devices));
396 let master_device_uid = run_serially(|| test_get_master_device(device));
397 assert!(!compensations.is_empty());
398 assert_eq!(devices.len(), compensations.len());
399
400 for (device, compensation) in zip(devices, compensations) {
401 let uid = run_serially(|| get_device_uid(device));
402 assert_eq!(
403 compensation,
404 if uid == master_device_uid {
405 0
406 } else {
407 DRIFT_COMPENSATION
408 }
409 );
410 }
411
412 assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
413 }
414
415 #[test]
416 fn test_aggregate_activate_clock_drift_compensation_for_an_aggregate_device_without_master_device()
417 {
418 let input_device = test_get_default_device(Scope::Input);
419 let output_device = test_get_default_device(Scope::Output);
420 if input_device.is_none() || output_device.is_none() || input_device == output_device {
421 println!("No input or output device to create an aggregate device.");
422 return;
423 }
424
425 let input_device = input_device.unwrap();
426 let output_device = output_device.unwrap();
427
428 let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
429 let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
430 assert!(run_serially(|| AggregateDevice::set_sub_devices_sync(
431 device,
432 input_device,
433 output_device
434 ))
435 .is_ok());
436
437 // The master device is by default the first sub device in the list.
438 // This happens to be the first sub device of the input device, see implementation of
439 // AggregateDevice::set_sub_devices.
440 let first_input_sub_device_uid = run_serially(|| {
441 get_device_uid(AggregateDevice::get_sub_devices_or_self(input_device).unwrap()[0])
442 });
443 let first_sub_device_uid = run_serially(|| {
444 get_device_uid(AggregateDevice::get_sub_devices_or_self(device).unwrap()[0])
445 });
446 assert_eq!(first_input_sub_device_uid, first_sub_device_uid);
447 let master_device_uid = run_serially(|| test_get_master_device(device));
448 assert_eq!(first_sub_device_uid, master_device_uid);
449
450 // Compensate the drift directly without setting master device.
451 assert!(run_serially(|| AggregateDevice::activate_clock_drift_compensation(device)).is_ok());
452
453 // Check the compensations.
454 let devices = run_serially(|| test_get_all_onwed_devices(device));
455 let compensations = run_serially(|| get_drift_compensations(&devices));
456 assert!(!compensations.is_empty());
457 assert_eq!(devices.len(), compensations.len());
458
459 for (i, compensation) in compensations.iter().enumerate() {
460 assert_eq!(*compensation, if i == 0 { 0 } else { DRIFT_COMPENSATION });
461 }
462
463 assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
464 }
465
466 #[test]
467 #[should_panic]
468 fn test_aggregate_activate_clock_drift_compensation_for_a_blank_aggregate_device() {
469 run_serially_forward_panics(|| {
470 let plugin = AggregateDevice::get_system_plugin_id().unwrap();
471 let device = AggregateDevice::create_blank_device_sync(plugin).unwrap();
472
473 let sub_devices = AggregateDevice::get_sub_devices_or_self(device).unwrap();
474 assert!(sub_devices.is_empty());
475 let onwed_devices = test_get_all_onwed_devices(device);
476 assert!(onwed_devices.is_empty());
477
478 // Get a panic since no sub devices to be set compensation.
479 assert!(AggregateDevice::activate_clock_drift_compensation(device).is_err());
480
481 assert!(AggregateDevice::destroy_device(plugin, device).is_ok());
482 });
483 }
484
485 fn get_drift_compensations(devices: &Vec<AudioObjectID>) -> Vec<u32> {
486 assert!(!devices.is_empty());
487 let mut compensations = Vec::new();
488 for device in devices {
489 let compensation = test_get_drift_compensations(*device).unwrap();
490 compensations.push(compensation);
491 }
492
493 compensations
494 }
495
496 // AggregateDevice::destroy_device
497 // ------------------------------------
498 #[test]
499 #[should_panic]
500 fn test_aggregate_destroy_aggregate_device_for_a_unknown_plugin_device() {
501 run_serially_forward_panics(|| {
502 let plugin = AggregateDevice::get_system_plugin_id().unwrap();
503 let device = AggregateDevice::create_blank_device_sync(plugin).unwrap();
504 assert!(AggregateDevice::destroy_device(kAudioObjectUnknown, device).is_err());
505 });
506 }
507
508 // AggregateDevice::new
509 // ------------------------------------
510 #[test]
511 fn test_aggregate_new() {
512 let input_device = test_get_default_device(Scope::Input);
513 let output_device = test_get_default_device(Scope::Output);
514 if input_device.is_none() || output_device.is_none() || input_device == output_device {
515 println!("No input or output device to create an aggregate device.");
516 return;
517 }
518
519 run_serially_forward_panics(|| {
520 let input_device = input_device.unwrap();
521 let output_device = output_device.unwrap();
522
523 let aggr = AggregateDevice::new(input_device, output_device).unwrap();
524
525 // Check main device
526 let output_sub_devices = AggregateDevice::get_sub_devices_or_self(output_device).unwrap();
527 let first_output_sub_device_uid = get_device_uid(output_sub_devices[0]);
528 let master_device_uid = test_get_master_device(aggr.get_device_id());
529 assert_eq!(first_output_sub_device_uid, master_device_uid);
530
531 // Check drift compensation
532 let devices = test_get_all_onwed_devices(aggr.get_device_id());
533 let compensations = get_drift_compensations(&devices);
534 assert!(!compensations.is_empty());
535 assert_eq!(devices.len(), compensations.len());
536
537 let device_uids = devices.iter().map(|&id| get_device_uid(id));
538 for (uid, compensation) in zip(device_uids, compensations) {
539 assert_eq!(
540 compensation,
541 if uid == master_device_uid {
542 0
543 } else {
544 DRIFT_COMPENSATION
545 },
546 "Unexpected drift value for device with uid {}",
547 uid
548 );
549 }
550 });
551 }