summaryrefslogtreecommitdiffstats
path: root/third_party/rust/cubeb-coreaudio/src/backend/tests/aggregate_device.rs
blob: 6c5e4940591d7c0715f108cb6bb0190114996dee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use super::utils::{
    test_get_all_devices, test_get_all_onwed_devices, test_get_default_device,
    test_get_drift_compensations, test_get_master_device, DeviceFilter, Scope,
};
use super::*;
use std::iter::zip;
use std::panic;

// AggregateDevice::set_sub_devices
// ------------------------------------
#[test]
#[should_panic]
fn test_aggregate_set_sub_devices_for_an_unknown_aggregate_device() {
    // If aggregate device id is kAudioObjectUnknown, we are unable to set device list.
    let default_input = test_get_default_device(Scope::Input);
    let default_output = test_get_default_device(Scope::Output);
    if default_input.is_none() || default_output.is_none() {
        panic!("No input or output device.");
    }

    let default_input = default_input.unwrap();
    let default_output = default_output.unwrap();
    assert!(
        run_serially_forward_panics(|| AggregateDevice::set_sub_devices(
            kAudioObjectUnknown,
            default_input,
            default_output
        ))
        .is_err()
    );
}

#[test]
#[should_panic]
fn test_aggregate_set_sub_devices_for_unknown_devices() {
    run_serially_forward_panics(|| {
        // If aggregate device id is kAudioObjectUnknown, we are unable to set device list.
        assert!(AggregateDevice::set_sub_devices(
            kAudioObjectUnknown,
            kAudioObjectUnknown,
            kAudioObjectUnknown
        )
        .is_err());
    });
}

// AggregateDevice::get_sub_devices
// ------------------------------------
// You can check this by creating an aggregate device in `Audio MIDI Setup`
// application and print out the sub devices of them!
#[test]
fn test_aggregate_get_sub_devices() {
    let devices = test_get_all_devices(DeviceFilter::ExcludeCubebAggregateAndVPIO);
    for device in devices {
        // `AggregateDevice::get_sub_devices(device)` will return a single-element vector
        // containing `device` itself if it's not an aggregate device. This test assumes devices
        // is not an empty aggregate device (Test will panic when calling get_sub_devices with
        // an empty aggregate device).
        println!(
            "get_sub_devices({}={})",
            device,
            run_serially_forward_panics(|| get_device_uid(device))
        );
        let sub_devices =
            run_serially_forward_panics(|| AggregateDevice::get_sub_devices(device).unwrap());
        // TODO: If the device is a blank aggregate device, then the assertion fails!
        assert!(!sub_devices.is_empty());
    }
}

#[test]
#[should_panic]
fn test_aggregate_get_sub_devices_for_a_unknown_device() {
    run_serially_forward_panics(|| {
        let devices = AggregateDevice::get_sub_devices(kAudioObjectUnknown).unwrap();
        assert!(devices.is_empty());
    });
}

// AggregateDevice::set_master_device
// ------------------------------------
#[test]
#[should_panic]
fn test_aggregate_set_master_device_for_an_unknown_aggregate_device() {
    run_serially_forward_panics(|| {
        assert!(
            AggregateDevice::set_master_device(kAudioObjectUnknown, kAudioObjectUnknown).is_err()
        );
    });
}

// AggregateDevice::activate_clock_drift_compensation
// ------------------------------------
#[test]
#[should_panic]
fn test_aggregate_activate_clock_drift_compensation_for_an_unknown_aggregate_device() {
    run_serially_forward_panics(|| {
        assert!(AggregateDevice::activate_clock_drift_compensation(kAudioObjectUnknown).is_err());
    });
}

// AggregateDevice::destroy_device
// ------------------------------------
#[test]
#[should_panic]
fn test_aggregate_destroy_device_for_unknown_plugin_and_aggregate_devices() {
    run_serially_forward_panics(|| {
        assert!(AggregateDevice::destroy_device(kAudioObjectUnknown, kAudioObjectUnknown).is_err())
    });
}

#[test]
#[should_panic]
fn test_aggregate_destroy_aggregate_device_for_a_unknown_aggregate_device() {
    run_serially_forward_panics(|| {
        let plugin = AggregateDevice::get_system_plugin_id().unwrap();
        assert!(AggregateDevice::destroy_device(plugin, kAudioObjectUnknown).is_err());
    });
}

// AggregateDevice::create_blank_device_sync
// ------------------------------------
#[test]
fn test_aggregate_create_blank_device() {
    // TODO: Test this when there is no available devices.
    let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
    let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
    let devices = test_get_all_devices(DeviceFilter::IncludeAll);
    let device = devices.into_iter().find(|dev| dev == &device).unwrap();
    let uid = run_serially(|| get_device_global_uid(device).unwrap().into_string());
    assert!(uid.contains(PRIVATE_AGGREGATE_DEVICE_NAME));
    assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
}

// AggregateDevice::get_sub_devices
// ------------------------------------
#[test]
#[should_panic]
fn test_aggregate_get_sub_devices_for_blank_aggregate_devices() {
    run_serially_forward_panics(|| {
        // TODO: Test this when there is no available devices.
        let plugin = AggregateDevice::get_system_plugin_id().unwrap();
        let device = AggregateDevice::create_blank_device_sync(plugin).unwrap();
        // There is no sub device in a blank aggregate device!
        // AggregateDevice::get_sub_devices guarantees returning a non-empty devices vector, so
        // the following call will panic!
        let sub_devices = AggregateDevice::get_sub_devices(device).unwrap();
        assert!(sub_devices.is_empty());
        assert!(AggregateDevice::destroy_device(plugin, device).is_ok());
    });
}

// AggregateDevice::set_sub_devices_sync
// ------------------------------------
#[test]
fn test_aggregate_set_sub_devices() {
    let input_device = test_get_default_device(Scope::Input);
    let output_device = test_get_default_device(Scope::Output);
    if input_device.is_none() || output_device.is_none() || input_device == output_device {
        println!("No input or output device to create an aggregate device.");
        return;
    }

    let input_device = input_device.unwrap();
    let output_device = output_device.unwrap();

    let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
    let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
    assert!(run_serially(|| AggregateDevice::set_sub_devices_sync(
        device,
        input_device,
        output_device
    ))
    .is_ok());

    let sub_devices = run_serially(|| AggregateDevice::get_sub_devices(device)).unwrap();
    let input_sub_devices =
        run_serially(|| AggregateDevice::get_sub_devices(input_device)).unwrap();
    let output_sub_devices =
        run_serially(|| AggregateDevice::get_sub_devices(output_device)).unwrap();

    // TODO: There may be overlapping devices between input_sub_devices and output_sub_devices,
    //       but now AggregateDevice::set_sub_devices will add them directly.
    assert_eq!(
        sub_devices.len(),
        input_sub_devices.len() + output_sub_devices.len()
    );
    for dev in &input_sub_devices {
        assert!(sub_devices.contains(dev));
    }
    for dev in &output_sub_devices {
        assert!(sub_devices.contains(dev));
    }

    let onwed_devices = run_serially(|| test_get_all_onwed_devices(device));
    let onwed_device_uids = run_serially(|| get_device_uids(&onwed_devices));
    let input_sub_device_uids = run_serially(|| get_device_uids(&input_sub_devices));
    let output_sub_device_uids = run_serially(|| get_device_uids(&output_sub_devices));
    for uid in &input_sub_device_uids {
        assert!(onwed_device_uids.contains(uid));
    }
    for uid in &output_sub_device_uids {
        assert!(onwed_device_uids.contains(uid));
    }

    assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
}

#[test]
#[should_panic]
fn test_aggregate_set_sub_devices_for_unknown_input_devices() {
    let output_device = test_get_default_device(Scope::Output);
    if output_device.is_none() {
        panic!("Need a output device for the test!");
    }
    let output_device = output_device.unwrap();

    run_serially_forward_panics(|| {
        let plugin = AggregateDevice::get_system_plugin_id().unwrap();
        let device = AggregateDevice::create_blank_device_sync(plugin).unwrap();

        assert!(
            AggregateDevice::set_sub_devices(device, kAudioObjectUnknown, output_device).is_err()
        );

        assert!(AggregateDevice::destroy_device(plugin, device).is_ok());
    });
}

#[test]
#[should_panic]
fn test_aggregate_set_sub_devices_for_unknown_output_devices() {
    let input_device = test_get_default_device(Scope::Input);
    if input_device.is_none() {
        panic!("Need a input device for the test!");
    }
    let input_device = input_device.unwrap();

    run_serially_forward_panics(|| {
        let plugin = AggregateDevice::get_system_plugin_id().unwrap();
        let device = AggregateDevice::create_blank_device_sync(plugin).unwrap();

        assert!(
            AggregateDevice::set_sub_devices(device, input_device, kAudioObjectUnknown).is_err()
        );

        assert!(AggregateDevice::destroy_device(plugin, device).is_ok());
    });
}

fn get_device_uids(devices: &Vec<AudioObjectID>) -> Vec<String> {
    devices
        .iter()
        .map(|device| get_device_global_uid(*device).unwrap().into_string())
        .collect()
}

// AggregateDevice::set_master_device
// ------------------------------------
#[test]
fn test_aggregate_set_master_device() {
    let input_device = test_get_default_device(Scope::Input);
    let output_device = test_get_default_device(Scope::Output);
    if input_device.is_none() || output_device.is_none() || input_device == output_device {
        println!("No input or output device to create an aggregate device.");
        return;
    }

    let input_device = input_device.unwrap();
    let output_device = output_device.unwrap();

    let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
    let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
    assert!(run_serially(|| AggregateDevice::set_sub_devices_sync(
        device,
        input_device,
        output_device
    ))
    .is_ok());
    assert!(run_serially(|| AggregateDevice::set_master_device(device, output_device)).is_ok());

    let output_sub_devices =
        run_serially(|| AggregateDevice::get_sub_devices(output_device)).unwrap();
    let first_output_sub_device_uid = run_serially(|| get_device_uid(output_sub_devices[0]));

    // Check that the first sub device of the output device is set as master device.
    let master_device_uid = run_serially(|| test_get_master_device(device));
    assert_eq!(first_output_sub_device_uid, master_device_uid);

    assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
}

#[test]
fn test_aggregate_set_master_device_for_a_blank_aggregate_device() {
    let output_device = test_get_default_device(Scope::Output);
    if output_device.is_none() {
        println!("No output device to test.");
        return;
    }

    let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
    let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
    assert!(
        run_serially(|| AggregateDevice::set_master_device(device, output_device.unwrap())).is_ok()
    );

    // TODO: it's really weird the aggregate device actually own nothing
    //       but its master device can be set successfully!
    // The sub devices of this blank aggregate device (by `AggregateDevice::get_sub_devices`)
    // and the own devices (by `test_get_all_onwed_devices`) is empty since the size returned
    // from `audio_object_get_property_data_size` is 0.
    // The CFStringRef of the master device returned from `test_get_master_device` is actually
    // non-null.

    assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
}

fn get_device_uid(id: AudioObjectID) -> String {
    get_device_global_uid(id).map_or(String::new(), |uid| uid.into_string())
}

// AggregateDevice::activate_clock_drift_compensation
// ------------------------------------
#[test]
fn test_aggregate_activate_clock_drift_compensation() {
    let input_device = test_get_default_device(Scope::Input);
    let output_device = test_get_default_device(Scope::Output);
    if input_device.is_none() || output_device.is_none() || input_device == output_device {
        println!("No input or output device to create an aggregate device.");
        return;
    }

    let input_device = input_device.unwrap();
    let output_device = output_device.unwrap();

    let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
    let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
    assert!(run_serially(|| AggregateDevice::set_sub_devices_sync(
        device,
        input_device,
        output_device
    ))
    .is_ok());
    assert!(run_serially(|| AggregateDevice::set_master_device(device, output_device)).is_ok());
    assert!(run_serially(|| AggregateDevice::activate_clock_drift_compensation(device)).is_ok());

    // Check the compensations.
    let devices = run_serially(|| test_get_all_onwed_devices(device));
    let compensations = run_serially(|| get_drift_compensations(&devices));
    let master_device_uid = run_serially(|| test_get_master_device(device));
    assert!(!compensations.is_empty());
    assert_eq!(devices.len(), compensations.len());

    for (device, compensation) in zip(devices, compensations) {
        let uid = get_device_uid(device);
        assert_eq!(
            compensation,
            if uid == master_device_uid {
                0
            } else {
                DRIFT_COMPENSATION
            }
        );
    }

    assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
}

#[test]
fn test_aggregate_activate_clock_drift_compensation_for_an_aggregate_device_without_master_device()
{
    let input_device = test_get_default_device(Scope::Input);
    let output_device = test_get_default_device(Scope::Output);
    if input_device.is_none() || output_device.is_none() || input_device == output_device {
        println!("No input or output device to create an aggregate device.");
        return;
    }

    let input_device = input_device.unwrap();
    let output_device = output_device.unwrap();

    let plugin = run_serially(|| AggregateDevice::get_system_plugin_id()).unwrap();
    let device = run_serially(|| AggregateDevice::create_blank_device_sync(plugin)).unwrap();
    assert!(run_serially(|| AggregateDevice::set_sub_devices_sync(
        device,
        input_device,
        output_device
    ))
    .is_ok());

    // The master device is by default the first sub device in the list.
    // This happens to be the first sub device of the input device, see implementation of
    // AggregateDevice::set_sub_devices.
    let first_input_sub_device_uid =
        run_serially(|| get_device_uid(AggregateDevice::get_sub_devices(input_device).unwrap()[0]));
    let first_sub_device_uid =
        run_serially(|| get_device_uid(AggregateDevice::get_sub_devices(device).unwrap()[0]));
    assert_eq!(first_input_sub_device_uid, first_sub_device_uid);
    let master_device_uid = run_serially(|| test_get_master_device(device));
    assert_eq!(first_sub_device_uid, master_device_uid);

    // Compensate the drift directly without setting master device.
    assert!(run_serially(|| AggregateDevice::activate_clock_drift_compensation(device)).is_ok());

    // Check the compensations.
    let devices = run_serially(|| test_get_all_onwed_devices(device));
    let compensations = run_serially(|| get_drift_compensations(&devices));
    assert!(!compensations.is_empty());
    assert_eq!(devices.len(), compensations.len());

    for (i, compensation) in compensations.iter().enumerate() {
        assert_eq!(*compensation, if i == 0 { 0 } else { DRIFT_COMPENSATION });
    }

    assert!(run_serially(|| AggregateDevice::destroy_device(plugin, device)).is_ok());
}

#[test]
#[should_panic]
fn test_aggregate_activate_clock_drift_compensation_for_a_blank_aggregate_device() {
    run_serially_forward_panics(|| {
        let plugin = AggregateDevice::get_system_plugin_id().unwrap();
        let device = AggregateDevice::create_blank_device_sync(plugin).unwrap();

        let sub_devices = AggregateDevice::get_sub_devices(device).unwrap();
        assert!(sub_devices.is_empty());
        let onwed_devices = test_get_all_onwed_devices(device);
        assert!(onwed_devices.is_empty());

        // Get a panic since no sub devices to be set compensation.
        assert!(AggregateDevice::activate_clock_drift_compensation(device).is_err());

        assert!(AggregateDevice::destroy_device(plugin, device).is_ok());
    });
}

fn get_drift_compensations(devices: &Vec<AudioObjectID>) -> Vec<u32> {
    assert!(!devices.is_empty());
    let mut compensations = Vec::new();
    for device in devices {
        let compensation = test_get_drift_compensations(*device).unwrap();
        compensations.push(compensation);
    }

    compensations
}

// AggregateDevice::destroy_device
// ------------------------------------
#[test]
#[should_panic]
fn test_aggregate_destroy_aggregate_device_for_a_unknown_plugin_device() {
    run_serially_forward_panics(|| {
        let plugin = AggregateDevice::get_system_plugin_id().unwrap();
        let device = AggregateDevice::create_blank_device_sync(plugin).unwrap();
        assert!(AggregateDevice::destroy_device(kAudioObjectUnknown, device).is_err());
    });
}

// AggregateDevice::new
// ------------------------------------
#[test]
fn test_aggregate_new() {
    let input_device = test_get_default_device(Scope::Input);
    let output_device = test_get_default_device(Scope::Output);
    if input_device.is_none() || output_device.is_none() || input_device == output_device {
        println!("No input or output device to create an aggregate device.");
        return;
    }

    run_serially_forward_panics(|| {
        let input_device = input_device.unwrap();
        let output_device = output_device.unwrap();

        let aggr = AggregateDevice::new(input_device, output_device).unwrap();

        // Check main device
        let output_sub_devices = AggregateDevice::get_sub_devices(output_device).unwrap();
        let first_output_sub_device_uid = get_device_uid(output_sub_devices[0]);
        let master_device_uid = test_get_master_device(aggr.get_device_id());
        assert_eq!(first_output_sub_device_uid, master_device_uid);

        // Check drift compensation
        let devices = test_get_all_onwed_devices(aggr.get_device_id());
        let compensations = get_drift_compensations(&devices);
        assert!(!compensations.is_empty());
        assert_eq!(devices.len(), compensations.len());

        let device_uids = devices.iter().map(|&id| get_device_uid(id));
        for (uid, compensation) in zip(device_uids, compensations) {
            assert_eq!(
                compensation,
                if uid == master_device_uid {
                    0
                } else {
                    DRIFT_COMPENSATION
                },
                "Unexpected drift value for device with uid {}",
                uid
            );
        }
    });
}