devices/
devices.rs

1// Copyright © 2011 Mozilla Foundation
2// Copyright © 2015 Haakon Sporsheim <haakon.sporsheim@telenordigital.com>
3//
4// This program is made available under an ISC-style license.  See the
5// accompanying file LICENSE for details.
6
7//! libcubeb enumerate device test/example.
8//! Prints out a list of input/output devices connected to the system.
9extern crate cubeb;
10
11mod common;
12
13use cubeb::{DeviceFormat, DeviceType};
14
15fn print_device_info(info: &cubeb::DeviceInfo) {
16    let devtype = if info.device_type().contains(DeviceType::INPUT) {
17        "input"
18    } else if info.device_type().contains(DeviceType::OUTPUT) {
19        "output"
20    } else {
21        "unknown?"
22    };
23
24    let devstate = match info.state() {
25        cubeb::DeviceState::Disabled => "disabled",
26        cubeb::DeviceState::Unplugged => "unplugged",
27        cubeb::DeviceState::Enabled => "enabled",
28    };
29
30    let devdeffmt = match info.default_format() {
31        DeviceFormat::S16LE => "S16LE",
32        DeviceFormat::S16BE => "S16BE",
33        DeviceFormat::F32LE => "F32LE",
34        DeviceFormat::F32BE => "F32BE",
35        _ => "unknown?",
36    };
37
38    let mut devfmts = "".to_string();
39    if info.format().contains(DeviceFormat::S16LE) {
40        devfmts = format!("{} S16LE", devfmts);
41    }
42    if info.format().contains(DeviceFormat::S16BE) {
43        devfmts = format!("{} S16BE", devfmts);
44    }
45    if info.format().contains(DeviceFormat::F32LE) {
46        devfmts = format!("{} F32LE", devfmts);
47    }
48    if info.format().contains(DeviceFormat::F32BE) {
49        devfmts = format!("{} F32BE", devfmts);
50    }
51
52    if let Some(device_id) = info.device_id() {
53        let preferred = if info.preferred().is_empty() {
54            ""
55        } else {
56            " (PREFERRED)"
57        };
58        println!("dev: \"{}\"{}", device_id, preferred);
59    }
60    if let Some(friendly_name) = info.friendly_name() {
61        println!("\tName:    \"{}\"", friendly_name);
62    }
63    if let Some(group_id) = info.group_id() {
64        println!("\tGroup:   \"{}\"", group_id);
65    }
66    if let Some(vendor_name) = info.vendor_name() {
67        println!("\tVendor:  \"{}\"", vendor_name);
68    }
69    println!("\tType:    {}", devtype);
70    println!("\tState:   {}", devstate);
71    println!("\tCh:      {}", info.max_channels());
72    println!(
73        "\tFormat:  {} (0x{:x}) (default: {})",
74        &devfmts[1..],
75        info.format(),
76        devdeffmt
77    );
78    println!(
79        "\tRate:    {} - {} (default: {})",
80        info.min_rate(),
81        info.max_rate(),
82        info.default_rate()
83    );
84    println!(
85        "\tLatency: lo {} frames, hi {} frames",
86        info.latency_lo(),
87        info.latency_hi()
88    );
89}
90
91fn main() {
92    let ctx = common::init("Cubeb audio test").expect("Failed to create cubeb context");
93
94    println!("Enumerating input devices for backend {}", ctx.backend_id());
95
96    let devices = match ctx.enumerate_devices(DeviceType::INPUT) {
97        Ok(devices) => devices,
98        Err(cubeb::Error::NotSupported) => {
99            println!("Device enumeration not support for this backend.");
100            return;
101        }
102        Err(e) => {
103            println!("Error enumerating devices: {}", e);
104            return;
105        }
106    };
107
108    println!("Found {} input devices", devices.len());
109    for d in devices.iter() {
110        print_device_info(d);
111    }
112
113    println!(
114        "Enumerating output devices for backend {}",
115        ctx.backend_id()
116    );
117
118    let devices = match ctx.enumerate_devices(DeviceType::OUTPUT) {
119        Ok(devices) => devices,
120        Err(e) => {
121            println!("Error enumerating devices: {}", e);
122            return;
123        }
124    };
125
126    println!("Found {} output devices", devices.len());
127    for d in devices.iter() {
128        print_device_info(d);
129    }
130}