summaryrefslogtreecommitdiffstats
path: root/third_party/rust/cubeb-coreaudio/src/backend/tests/device_property.rs
blob: a974aee64bb7a8f310b7b0dc05bafc365f05385c (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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use super::utils::{test_get_default_device, Scope};
use super::*;

// get_device_uid
// ------------------------------------
#[test]
fn test_get_device_uid() {
    // Input device.
    if let Some(input) = test_get_default_device(Scope::Input) {
        let uid = run_serially(|| get_device_uid(input, DeviceType::INPUT)).unwrap();
        let uid = uid.into_string();
        assert!(!uid.is_empty());
    }

    // Output device.
    if let Some(output) = test_get_default_device(Scope::Output) {
        let uid = run_serially(|| get_device_uid(output, DeviceType::OUTPUT)).unwrap();
        let uid = uid.into_string();
        assert!(!uid.is_empty());
    }
}

#[test]
#[should_panic]
fn test_get_device_uid_by_unknwon_device() {
    // Unknown device.
    assert!(
        run_serially_forward_panics(|| get_device_uid(kAudioObjectUnknown, DeviceType::INPUT))
            .is_err()
    );
}

// get_device_model_uid
// ------------------------------------
// Some devices (e.g., AirPods) fail to get model uid.
#[test]
fn test_get_device_model_uid() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        match run_serially(|| get_device_model_uid(device, DeviceType::INPUT)) {
            Ok(uid) => println!("input model uid: {}", uid.into_string()),
            Err(e) => println!("No input model uid. Error: {}", e),
        }
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        match run_serially(|| get_device_model_uid(device, DeviceType::OUTPUT)) {
            Ok(uid) => println!("output model uid: {}", uid.into_string()),
            Err(e) => println!("No output model uid. Error: {}", e),
        }
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_device_model_uid_by_unknown_device() {
    assert!(run_serially_forward_panics(|| get_device_model_uid(
        kAudioObjectUnknown,
        DeviceType::INPUT
    ))
    .is_err());
}

// get_device_transport_type
// ------------------------------------
#[test]
fn test_get_device_transport_type() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        match run_serially(|| get_device_transport_type(device, DeviceType::INPUT)) {
            Ok(trans_type) => println!(
                "input transport type: {:X}, {:?}",
                trans_type,
                convert_uint32_into_string(trans_type)
            ),
            Err(e) => println!("No input transport type. Error: {}", e),
        }
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        match run_serially(|| get_device_transport_type(device, DeviceType::OUTPUT)) {
            Ok(trans_type) => println!(
                "output transport type: {:X}, {:?}",
                trans_type,
                convert_uint32_into_string(trans_type)
            ),
            Err(e) => println!("No output transport type. Error: {}", e),
        }
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_device_transport_type_by_unknown_device() {
    assert!(run_serially_forward_panics(|| get_device_transport_type(
        kAudioObjectUnknown,
        DeviceType::INPUT
    ))
    .is_err());
}

// get_device_source
// ------------------------------------
// Some USB headsets (e.g., Plantronic .Audio 628) fails to get data source.
#[test]
fn test_get_device_source() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        match run_serially(|| get_device_source(device, DeviceType::INPUT)) {
            Ok(source) => println!(
                "input source: {:X}, {:?}",
                source,
                convert_uint32_into_string(source)
            ),
            Err(e) => println!("No input data source. Error: {}", e),
        }
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        match run_serially(|| get_device_source(device, DeviceType::OUTPUT)) {
            Ok(source) => println!(
                "output source: {:X}, {:?}",
                source,
                convert_uint32_into_string(source)
            ),
            Err(e) => println!("No output data source. Error: {}", e),
        }
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_device_source_by_unknown_device() {
    assert!(run_serially_forward_panics(|| get_device_source(
        kAudioObjectUnknown,
        DeviceType::INPUT
    ))
    .is_err());
}

// get_device_source_name
// ------------------------------------
#[test]
fn test_get_device_source_name() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        match run_serially(|| get_device_source_name(device, DeviceType::INPUT)) {
            Ok(name) => println!("input: {}", name.into_string()),
            Err(e) => println!("No input data source name. Error: {}", e),
        }
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        match run_serially(|| get_device_source_name(device, DeviceType::OUTPUT)) {
            Ok(name) => println!("output: {}", name.into_string()),
            Err(e) => println!("No output data source name. Error: {}", e),
        }
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_device_source_name_by_unknown_device() {
    assert!(run_serially_forward_panics(|| get_device_source_name(
        kAudioObjectUnknown,
        DeviceType::INPUT
    ))
    .is_err());
}

// get_device_name
// ------------------------------------
#[test]
fn test_get_device_name() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        let name = run_serially(|| get_device_name(device, DeviceType::INPUT)).unwrap();
        println!("input device name: {}", name.into_string());
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        let name = run_serially(|| get_device_name(device, DeviceType::OUTPUT).unwrap());
        println!("output device name: {}", name.into_string());
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_device_name_by_unknown_device() {
    assert!(run_serially_forward_panics(|| get_device_name(
        kAudioObjectUnknown,
        DeviceType::INPUT
    ))
    .is_err());
}

// get_device_manufacturer
// ------------------------------------
#[test]
fn test_get_device_manufacturer() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        // Some devices like AirPods cannot get the vendor info so we print the error directly.
        // TODO: Replace `map` and `unwrap_or_else` by `map_or_else`
        let name = run_serially(|| get_device_manufacturer(device, DeviceType::INPUT))
            .map(|name| name.into_string())
            .unwrap_or_else(|e| format!("Error: {}", e));
        println!("input device vendor: {}", name);
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        // Some devices like AirPods cannot get the vendor info so we print the error directly.
        // TODO: Replace `map` and `unwrap_or_else` by `map_or_else`
        let name =
            run_serially_forward_panics(|| get_device_manufacturer(device, DeviceType::OUTPUT))
                .map(|name| name.into_string())
                .unwrap_or_else(|e| format!("Error: {}", e));
        println!("output device vendor: {}", name);
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_device_manufacturer_by_unknown_device() {
    assert!(run_serially_forward_panics(|| get_device_manufacturer(
        kAudioObjectUnknown,
        DeviceType::INPUT
    ))
    .is_err());
}

// get_device_buffer_frame_size_range
// ------------------------------------
#[test]
fn test_get_device_buffer_frame_size_range() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        let range =
            run_serially(|| get_device_buffer_frame_size_range(device, DeviceType::INPUT)).unwrap();
        println!(
            "range of input buffer frame size: {}-{}",
            range.mMinimum, range.mMaximum
        );
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        let range = run_serially(|| get_device_buffer_frame_size_range(device, DeviceType::OUTPUT))
            .unwrap();
        println!(
            "range of output buffer frame size: {}-{}",
            range.mMinimum, range.mMaximum
        );
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_device_buffer_frame_size_range_by_unknown_device() {
    assert!(
        run_serially_forward_panics(|| get_device_buffer_frame_size_range(
            kAudioObjectUnknown,
            DeviceType::INPUT
        ))
        .is_err()
    );
}

// get_device_latency
// ------------------------------------
#[test]
fn test_get_device_latency() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        let latency = run_serially(|| get_device_latency(device, DeviceType::INPUT)).unwrap();
        println!("latency of input device: {}", latency);
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        let latency = run_serially(|| get_device_latency(device, DeviceType::OUTPUT)).unwrap();
        println!("latency of output device: {}", latency);
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_device_latency_by_unknown_device() {
    assert!(run_serially_forward_panics(|| get_device_latency(
        kAudioObjectUnknown,
        DeviceType::INPUT
    ))
    .is_err());
}

// get_device_streams
// ------------------------------------
#[test]
fn test_get_device_streams() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        let streams = run_serially(|| get_device_streams(device, DeviceType::INPUT)).unwrap();
        println!("streams on the input device: {:?}", streams);
        assert!(!streams.is_empty());
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        let streams = run_serially(|| get_device_streams(device, DeviceType::OUTPUT)).unwrap();
        println!("streams on the output device: {:?}", streams);
        assert!(!streams.is_empty());
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_device_streams_by_unknown_device() {
    assert!(run_serially_forward_panics(|| get_device_streams(
        kAudioObjectUnknown,
        DeviceType::INPUT
    ))
    .is_err());
}

// get_device_sample_rate
// ------------------------------------
#[test]
fn test_get_device_sample_rate() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        let rate = run_serially(|| get_device_sample_rate(device, DeviceType::INPUT)).unwrap();
        println!("input sample rate: {}", rate);
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        let rate = run_serially(|| get_device_sample_rate(device, DeviceType::OUTPUT).unwrap());
        println!("output sample rate: {}", rate);
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_device_sample_rate_by_unknown_device() {
    assert!(run_serially_forward_panics(|| get_device_sample_rate(
        kAudioObjectUnknown,
        DeviceType::INPUT
    ))
    .is_err());
}

// get_ranges_of_device_sample_rate
// ------------------------------------
#[test]
fn test_get_ranges_of_device_sample_rate() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        let ranges =
            run_serially(|| get_ranges_of_device_sample_rate(device, DeviceType::INPUT)).unwrap();
        println!("ranges of input sample rate: {:?}", ranges);
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        let ranges =
            run_serially(|| get_ranges_of_device_sample_rate(device, DeviceType::OUTPUT)).unwrap();
        println!("ranges of output sample rate: {:?}", ranges);
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_ranges_of_device_sample_rate_by_unknown_device() {
    assert!(
        run_serially_forward_panics(|| get_ranges_of_device_sample_rate(
            kAudioObjectUnknown,
            DeviceType::INPUT
        ))
        .is_err()
    );
}

// get_stream_latency
// ------------------------------------
#[test]
fn test_get_stream_latency() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        let streams = run_serially(|| get_device_streams(device, DeviceType::INPUT)).unwrap();
        for stream in streams {
            let latency = run_serially(|| get_stream_latency(stream)).unwrap();
            println!("latency of the input stream {} is {}", stream, latency);
        }
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        let streams = run_serially(|| get_device_streams(device, DeviceType::OUTPUT)).unwrap();
        for stream in streams {
            let latency = run_serially(|| get_stream_latency(stream)).unwrap();
            println!("latency of the output stream {} is {}", stream, latency);
        }
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_stream_latency_by_unknown_device() {
    assert!(get_stream_latency(kAudioObjectUnknown).is_err());
}

// get_stream_virtual_format
// ------------------------------------
#[test]
fn test_get_stream_virtual_format() {
    if let Some(device) = test_get_default_device(Scope::Input) {
        let streams = run_serially(|| get_device_streams(device, DeviceType::INPUT)).unwrap();
        let formats = streams
            .iter()
            .map(|s| run_serially(|| get_stream_virtual_format(*s)))
            .collect::<Vec<std::result::Result<AudioStreamBasicDescription, OSStatus>>>();
        println!("input stream formats: {:?}", formats);
        assert!(!formats.is_empty());
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        let streams = run_serially(|| get_device_streams(device, DeviceType::OUTPUT)).unwrap();
        let formats = streams
            .iter()
            .map(|s| run_serially(|| get_stream_virtual_format(*s)))
            .collect::<Vec<std::result::Result<AudioStreamBasicDescription, OSStatus>>>();
        println!("output stream formats: {:?}", formats);
        assert!(!formats.is_empty());
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_stream_virtual_format_by_unknown_stream() {
    assert!(
        run_serially_forward_panics(|| get_stream_virtual_format(kAudioObjectUnknown)).is_err()
    );
}

// get_stream_terminal_type
// ------------------------------------

#[test]
fn test_get_stream_terminal_type() {
    fn terminal_type_to_device_type(terminal_type: u32) -> Option<DeviceType> {
        #[allow(non_upper_case_globals)]
        match terminal_type {
            kAudioStreamTerminalTypeMicrophone
            | kAudioStreamTerminalTypeHeadsetMicrophone
            | kAudioStreamTerminalTypeReceiverMicrophone => Some(DeviceType::INPUT),
            kAudioStreamTerminalTypeSpeaker
            | kAudioStreamTerminalTypeHeadphones
            | kAudioStreamTerminalTypeLFESpeaker
            | kAudioStreamTerminalTypeReceiverSpeaker => Some(DeviceType::OUTPUT),
            t if t > INPUT_UNDEFINED && t < OUTPUT_UNDEFINED => Some(DeviceType::INPUT),
            t if t > OUTPUT_UNDEFINED && t < BIDIRECTIONAL_UNDEFINED => Some(DeviceType::OUTPUT),
            t => {
                println!("UNKNOWN TerminalType {:#06x}", t);
                None
            }
        }
    }
    if let Some(device) = test_get_default_device(Scope::Input) {
        let streams = run_serially(|| get_device_streams(device, DeviceType::INPUT)).unwrap();
        assert!(streams.iter().any(|&s| {
            terminal_type_to_device_type(run_serially(|| get_stream_terminal_type(s)).unwrap())
                == Some(DeviceType::INPUT)
        }));
    } else {
        println!("No input device.");
    }

    if let Some(device) = test_get_default_device(Scope::Output) {
        let streams = run_serially(|| get_device_streams(device, DeviceType::OUTPUT)).unwrap();
        assert!(streams.iter().any(|&s| {
            terminal_type_to_device_type(run_serially(|| get_stream_terminal_type(s)).unwrap())
                == Some(DeviceType::OUTPUT)
        }));
    } else {
        println!("No output device.");
    }
}

#[test]
#[should_panic]
fn test_get_stream_terminal_type_by_unknown_stream() {
    assert!(get_stream_terminal_type(kAudioObjectUnknown).is_err());
}