Debian Codegraph @

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

raw
1 use super::utils::{test_get_default_device, Scope};
2 use super::*;
3
4 // get_device_uid
5 // ------------------------------------
6 #[test]
7 fn test_get_device_uid() {
8 // Input device.
9 if let Some(input) = test_get_default_device(Scope::Input) {
10 let uid = run_serially(|| get_device_uid(input, DeviceType::INPUT)).unwrap();
11 let uid = uid.into_string();
12 assert!(!uid.is_empty());
13 }
14
15 // Output device.
16 if let Some(output) = test_get_default_device(Scope::Output) {
17 let uid = run_serially(|| get_device_uid(output, DeviceType::OUTPUT)).unwrap();
18 let uid = uid.into_string();
19 assert!(!uid.is_empty());
20 }
21 }
22
23 #[test]
24 #[should_panic]
25 fn test_get_device_uid_by_unknwon_device() {
26 // Unknown device.
27 assert!(
28 run_serially_forward_panics(|| get_device_uid(kAudioObjectUnknown, DeviceType::INPUT))
29 .is_err()
30 );
31 }
32
33 // get_device_model_uid
34 // ------------------------------------
35 // Some devices (e.g., AirPods) fail to get model uid.
36 #[test]
37 fn test_get_device_model_uid() {
38 if let Some(device) = test_get_default_device(Scope::Input) {
39 match run_serially(|| get_device_model_uid(device, DeviceType::INPUT)) {
40 Ok(uid) => println!("input model uid: {}", uid.into_string()),
41 Err(e) => println!("No input model uid. Error: {}", e),
42 }
43 } else {
44 println!("No input device.");
45 }
46
47 if let Some(device) = test_get_default_device(Scope::Output) {
48 match run_serially(|| get_device_model_uid(device, DeviceType::OUTPUT)) {
49 Ok(uid) => println!("output model uid: {}", uid.into_string()),
50 Err(e) => println!("No output model uid. Error: {}", e),
51 }
52 } else {
53 println!("No output device.");
54 }
55 }
56
57 #[test]
58 #[should_panic]
59 fn test_get_device_model_uid_by_unknown_device() {
60 assert!(run_serially_forward_panics(|| get_device_model_uid(
61 kAudioObjectUnknown,
62 DeviceType::INPUT
63 ))
64 .is_err());
65 }
66
67 // get_device_transport_type
68 // ------------------------------------
69 #[test]
70 fn test_get_device_transport_type() {
71 if let Some(device) = test_get_default_device(Scope::Input) {
72 match run_serially(|| get_device_transport_type(device, DeviceType::INPUT)) {
73 Ok(trans_type) => println!(
74 "input transport type: {:X}, {:?}",
75 trans_type,
76 convert_uint32_into_string(trans_type)
77 ),
78 Err(e) => println!("No input transport type. Error: {}", e),
79 }
80 } else {
81 println!("No input device.");
82 }
83
84 if let Some(device) = test_get_default_device(Scope::Output) {
85 match run_serially(|| get_device_transport_type(device, DeviceType::OUTPUT)) {
86 Ok(trans_type) => println!(
87 "output transport type: {:X}, {:?}",
88 trans_type,
89 convert_uint32_into_string(trans_type)
90 ),
91 Err(e) => println!("No output transport type. Error: {}", e),
92 }
93 } else {
94 println!("No output device.");
95 }
96 }
97
98 #[test]
99 #[should_panic]
100 fn test_get_device_transport_type_by_unknown_device() {
101 assert!(run_serially_forward_panics(|| get_device_transport_type(
102 kAudioObjectUnknown,
103 DeviceType::INPUT
104 ))
105 .is_err());
106 }
107
108 // get_device_source
109 // ------------------------------------
110 // Some USB headsets (e.g., Plantronic .Audio 628) fails to get data source.
111 #[test]
112 fn test_get_device_source() {
113 if let Some(device) = test_get_default_device(Scope::Input) {
114 match run_serially(|| get_device_source(device, DeviceType::INPUT)) {
115 Ok(source) => println!(
116 "input source: {:X}, {:?}",
117 source,
118 convert_uint32_into_string(source)
119 ),
120 Err(e) => println!("No input data source. Error: {}", e),
121 }
122 } else {
123 println!("No input device.");
124 }
125
126 if let Some(device) = test_get_default_device(Scope::Output) {
127 match run_serially(|| get_device_source(device, DeviceType::OUTPUT)) {
128 Ok(source) => println!(
129 "output source: {:X}, {:?}",
130 source,
131 convert_uint32_into_string(source)
132 ),
133 Err(e) => println!("No output data source. Error: {}", e),
134 }
135 } else {
136 println!("No output device.");
137 }
138 }
139
140 #[test]
141 #[should_panic]
142 fn test_get_device_source_by_unknown_device() {
143 assert!(run_serially_forward_panics(|| get_device_source(
144 kAudioObjectUnknown,
145 DeviceType::INPUT
146 ))
147 .is_err());
148 }
149
150 // get_device_source_name
151 // ------------------------------------
152 #[test]
153 fn test_get_device_source_name() {
154 if let Some(device) = test_get_default_device(Scope::Input) {
155 match run_serially(|| get_device_source_name(device, DeviceType::INPUT)) {
156 Ok(name) => println!("input: {}", name.into_string()),
157 Err(e) => println!("No input data source name. Error: {}", e),
158 }
159 } else {
160 println!("No input device.");
161 }
162
163 if let Some(device) = test_get_default_device(Scope::Output) {
164 match run_serially(|| get_device_source_name(device, DeviceType::OUTPUT)) {
165 Ok(name) => println!("output: {}", name.into_string()),
166 Err(e) => println!("No output data source name. Error: {}", e),
167 }
168 } else {
169 println!("No output device.");
170 }
171 }
172
173 #[test]
174 #[should_panic]
175 fn test_get_device_source_name_by_unknown_device() {
176 assert!(run_serially_forward_panics(|| get_device_source_name(
177 kAudioObjectUnknown,
178 DeviceType::INPUT
179 ))
180 .is_err());
181 }
182
183 // get_device_name
184 // ------------------------------------
185 #[test]
186 fn test_get_device_name() {
187 if let Some(device) = test_get_default_device(Scope::Input) {
188 let name = run_serially(|| get_device_name(device, DeviceType::INPUT)).unwrap();
189 println!("input device name: {}", name.into_string());
190 } else {
191 println!("No input device.");
192 }
193
194 if let Some(device) = test_get_default_device(Scope::Output) {
195 let name = run_serially(|| get_device_name(device, DeviceType::OUTPUT).unwrap());
196 println!("output device name: {}", name.into_string());
197 } else {
198 println!("No output device.");
199 }
200 }
201
202 #[test]
203 #[should_panic]
204 fn test_get_device_name_by_unknown_device() {
205 assert!(run_serially_forward_panics(|| get_device_name(
206 kAudioObjectUnknown,
207 DeviceType::INPUT
208 ))
209 .is_err());
210 }
211
212 // get_device_manufacturer
213 // ------------------------------------
214 #[test]
215 fn test_get_device_manufacturer() {
216 if let Some(device) = test_get_default_device(Scope::Input) {
217 // Some devices like AirPods cannot get the vendor info so we print the error directly.
218 // TODO: Replace `map` and `unwrap_or_else` by `map_or_else`
219 let name = run_serially(|| get_device_manufacturer(device, DeviceType::INPUT))
220 .map(|name| name.into_string())
221 .unwrap_or_else(|e| format!("Error: {}", e));
222 println!("input device vendor: {}", name);
223 } else {
224 println!("No input device.");
225 }
226
227 if let Some(device) = test_get_default_device(Scope::Output) {
228 // Some devices like AirPods cannot get the vendor info so we print the error directly.
229 // TODO: Replace `map` and `unwrap_or_else` by `map_or_else`
230 let name =
231 run_serially_forward_panics(|| get_device_manufacturer(device, DeviceType::OUTPUT))
232 .map(|name| name.into_string())
233 .unwrap_or_else(|e| format!("Error: {}", e));
234 println!("output device vendor: {}", name);
235 } else {
236 println!("No output device.");
237 }
238 }
239
240 #[test]
241 #[should_panic]
242 fn test_get_device_manufacturer_by_unknown_device() {
243 assert!(run_serially_forward_panics(|| get_device_manufacturer(
244 kAudioObjectUnknown,
245 DeviceType::INPUT
246 ))
247 .is_err());
248 }
249
250 // get_device_buffer_frame_size_range
251 // ------------------------------------
252 #[test]
253 fn test_get_device_buffer_frame_size_range() {
254 if let Some(device) = test_get_default_device(Scope::Input) {
255 let range =
256 run_serially(|| get_device_buffer_frame_size_range(device, DeviceType::INPUT)).unwrap();
257 println!(
258 "range of input buffer frame size: {}-{}",
259 range.mMinimum, range.mMaximum
260 );
261 } else {
262 println!("No input device.");
263 }
264
265 if let Some(device) = test_get_default_device(Scope::Output) {
266 let range = run_serially(|| get_device_buffer_frame_size_range(device, DeviceType::OUTPUT))
267 .unwrap();
268 println!(
269 "range of output buffer frame size: {}-{}",
270 range.mMinimum, range.mMaximum
271 );
272 } else {
273 println!("No output device.");
274 }
275 }
276
277 #[test]
278 #[should_panic]
279 fn test_get_device_buffer_frame_size_range_by_unknown_device() {
280 assert!(
281 run_serially_forward_panics(|| get_device_buffer_frame_size_range(
282 kAudioObjectUnknown,
283 DeviceType::INPUT
284 ))
285 .is_err()
286 );
287 }
288
289 // get_device_latency
290 // ------------------------------------
291 #[test]
292 fn test_get_device_latency() {
293 if let Some(device) = test_get_default_device(Scope::Input) {
294 let latency = run_serially(|| get_device_latency(device, DeviceType::INPUT)).unwrap();
295 println!("latency of input device: {}", latency);
296 } else {
297 println!("No input device.");
298 }
299
300 if let Some(device) = test_get_default_device(Scope::Output) {
301 let latency = run_serially(|| get_device_latency(device, DeviceType::OUTPUT)).unwrap();
302 println!("latency of output device: {}", latency);
303 } else {
304 println!("No output device.");
305 }
306 }
307
308 #[test]
309 #[should_panic]
310 fn test_get_device_latency_by_unknown_device() {
311 assert!(run_serially_forward_panics(|| get_device_latency(
312 kAudioObjectUnknown,
313 DeviceType::INPUT
314 ))
315 .is_err());
316 }
317
318 // get_device_streams
319 // ------------------------------------
320 #[test]
321 fn test_get_device_streams() {
322 if let Some(device) = test_get_default_device(Scope::Input) {
323 let streams = run_serially(|| get_device_streams(device, DeviceType::INPUT)).unwrap();
324 println!("streams on the input device: {:?}", streams);
325 assert!(!streams.is_empty());
326 } else {
327 println!("No input device.");
328 }
329
330 if let Some(device) = test_get_default_device(Scope::Output) {
331 let streams = run_serially(|| get_device_streams(device, DeviceType::OUTPUT)).unwrap();
332 println!("streams on the output device: {:?}", streams);
333 assert!(!streams.is_empty());
334 } else {
335 println!("No output device.");
336 }
337 }
338
339 #[test]
340 #[should_panic]
341 fn test_get_device_streams_by_unknown_device() {
342 assert!(run_serially_forward_panics(|| get_device_streams(
343 kAudioObjectUnknown,
344 DeviceType::INPUT
345 ))
346 .is_err());
347 }
348
349 // get_device_sample_rate
350 // ------------------------------------
351 #[test]
352 fn test_get_device_sample_rate() {
353 if let Some(device) = test_get_default_device(Scope::Input) {
354 let rate = run_serially(|| get_device_sample_rate(device, DeviceType::INPUT)).unwrap();
355 println!("input sample rate: {}", rate);
356 } else {
357 println!("No input device.");
358 }
359
360 if let Some(device) = test_get_default_device(Scope::Output) {
361 let rate = run_serially(|| get_device_sample_rate(device, DeviceType::OUTPUT).unwrap());
362 println!("output sample rate: {}", rate);
363 } else {
364 println!("No output device.");
365 }
366 }
367
368 #[test]
369 #[should_panic]
370 fn test_get_device_sample_rate_by_unknown_device() {
371 assert!(run_serially_forward_panics(|| get_device_sample_rate(
372 kAudioObjectUnknown,
373 DeviceType::INPUT
374 ))
375 .is_err());
376 }
377
378 // get_ranges_of_device_sample_rate
379 // ------------------------------------
380 #[test]
381 fn test_get_ranges_of_device_sample_rate() {
382 if let Some(device) = test_get_default_device(Scope::Input) {
383 let ranges =
384 run_serially(|| get_ranges_of_device_sample_rate(device, DeviceType::INPUT)).unwrap();
385 println!("ranges of input sample rate: {:?}", ranges);
386 } else {
387 println!("No input device.");
388 }
389
390 if let Some(device) = test_get_default_device(Scope::Output) {
391 let ranges =
392 run_serially(|| get_ranges_of_device_sample_rate(device, DeviceType::OUTPUT)).unwrap();
393 println!("ranges of output sample rate: {:?}", ranges);
394 } else {
395 println!("No output device.");
396 }
397 }
398
399 #[test]
400 #[should_panic]
401 fn test_get_ranges_of_device_sample_rate_by_unknown_device() {
402 assert!(
403 run_serially_forward_panics(|| get_ranges_of_device_sample_rate(
404 kAudioObjectUnknown,
405 DeviceType::INPUT
406 ))
407 .is_err()
408 );
409 }
410
411 // get_stream_latency
412 // ------------------------------------
413 #[test]
414 fn test_get_stream_latency() {
415 if let Some(device) = test_get_default_device(Scope::Input) {
416 let devstreams = run_serially(|| get_device_streams(device, DeviceType::INPUT)).unwrap();
417 for ds in devstreams {
418 let latency = run_serially(|| get_stream_latency(ds.stream)).unwrap();
419 println!("latency of the input stream {} is {}", ds.stream, latency);
420 }
421 } else {
422 println!("No input device.");
423 }
424
425 if let Some(device) = test_get_default_device(Scope::Output) {
426 let devstreams = run_serially(|| get_device_streams(device, DeviceType::OUTPUT)).unwrap();
427 for ds in devstreams {
428 let latency = run_serially(|| get_stream_latency(ds.stream)).unwrap();
429 println!("latency of the output stream {} is {}", ds.stream, latency);
430 }
431 } else {
432 println!("No output device.");
433 }
434 }
435
436 #[test]
437 #[should_panic]
438 fn test_get_stream_latency_by_unknown_device() {
439 assert!(get_stream_latency(kAudioObjectUnknown).is_err());
440 }
441
442 // get_stream_virtual_format
443 // ------------------------------------
444 #[test]
445 fn test_get_stream_virtual_format() {
446 if let Some(device) = test_get_default_device(Scope::Input) {
447 let devstreams = run_serially(|| get_device_streams(device, DeviceType::INPUT)).unwrap();
448 let formats = devstreams
449 .iter()
450 .map(|ds| run_serially(|| get_stream_virtual_format(ds.stream)))
451 .collect::<Vec<std::result::Result<AudioStreamBasicDescription, OSStatus>>>();
452 println!("input stream formats: {:?}", formats);
453 assert!(!formats.is_empty());
454 } else {
455 println!("No input device.");
456 }
457
458 if let Some(device) = test_get_default_device(Scope::Output) {
459 let devstreams = run_serially(|| get_device_streams(device, DeviceType::OUTPUT)).unwrap();
460 let formats = devstreams
461 .iter()
462 .map(|ds| run_serially(|| get_stream_virtual_format(ds.stream)))
463 .collect::<Vec<std::result::Result<AudioStreamBasicDescription, OSStatus>>>();
464 println!("output stream formats: {:?}", formats);
465 assert!(!formats.is_empty());
466 } else {
467 println!("No output device.");
468 }
469 }
470
471 #[test]
472 #[should_panic]
473 fn test_get_stream_virtual_format_by_unknown_stream() {
474 assert!(
475 run_serially_forward_panics(|| get_stream_virtual_format(kAudioObjectUnknown)).is_err()
476 );
477 }
478
479 // get_devices
480 // ------------------------------------
481
482 #[test]
483 fn test_get_devices() {
484 if let Some(device) = test_get_default_device(Scope::Input) {
485 let devices = run_serially(|| get_devices());
486 assert!(devices.contains(&device));
487 } else {
488 println!("No input device.");
489 }
490
491 if let Some(device) = test_get_default_device(Scope::Output) {
492 let devices = run_serially(|| get_devices());
493 assert!(devices.contains(&device));
494 } else {
495 println!("No output device.");
496 }
497 }