1 // Copyright © 2017-2018 Mozilla Foundation
2 //
3 // This program is made available under an ISC-style license.  See the
4 // accompanying file LICENSE for details.
5 
6 use callbacks::cubeb_device_collection_changed_callback;
7 use context::cubeb;
8 use std::{fmt, mem};
9 use std::os::raw::{c_char, c_int, c_uint, c_void};
10 
11 cubeb_enum! {
12     pub enum cubeb_device_fmt {
13         CUBEB_DEVICE_FMT_S16LE          = 0x0010,
14         CUBEB_DEVICE_FMT_S16BE          = 0x0020,
15         CUBEB_DEVICE_FMT_F32LE          = 0x1000,
16         CUBEB_DEVICE_FMT_F32BE          = 0x2000,
17     }
18 }
19 
20 #[cfg(target_endian = "big")]
21 pub const CUBEB_DEVICE_FMT_S16NE: cubeb_device_fmt = CUBEB_DEVICE_FMT_S16BE;
22 #[cfg(target_endian = "big")]
23 pub const CUBEB_DEVICE_FMT_F32NE: cubeb_device_fmt = CUBEB_DEVICE_FMT_F32BE;
24 #[cfg(target_endian = "little")]
25 pub const CUBEB_DEVICE_FMT_S16NE: cubeb_device_fmt = CUBEB_DEVICE_FMT_S16LE;
26 #[cfg(target_endian = "little")]
27 pub const CUBEB_DEVICE_FMT_F32NE: cubeb_device_fmt = CUBEB_DEVICE_FMT_F32LE;
28 
29 pub const CUBEB_DEVICE_FMT_S16_MASK: cubeb_device_fmt =
30     CUBEB_DEVICE_FMT_S16LE | CUBEB_DEVICE_FMT_S16BE;
31 pub const CUBEB_DEVICE_FMT_F32_MASK: cubeb_device_fmt =
32     CUBEB_DEVICE_FMT_F32LE | CUBEB_DEVICE_FMT_F32BE;
33 pub const CUBEB_DEVICE_FMT_ALL: cubeb_device_fmt =
34     CUBEB_DEVICE_FMT_S16_MASK | CUBEB_DEVICE_FMT_F32_MASK;
35 
36 cubeb_enum! {
37     pub enum cubeb_device_pref  {
38         CUBEB_DEVICE_PREF_NONE          = 0x00,
39         CUBEB_DEVICE_PREF_MULTIMEDIA    = 0x01,
40         CUBEB_DEVICE_PREF_VOICE         = 0x02,
41         CUBEB_DEVICE_PREF_NOTIFICATION  = 0x04,
42         CUBEB_DEVICE_PREF_ALL           = 0x0F,
43     }
44 }
45 
46 cubeb_enum! {
47     pub enum cubeb_device_state {
48         CUBEB_DEVICE_STATE_DISABLED,
49         CUBEB_DEVICE_STATE_UNPLUGGED,
50         CUBEB_DEVICE_STATE_ENABLED,
51     }
52 }
53 cubeb_enum! {
54     pub enum cubeb_device_type {
55         CUBEB_DEVICE_TYPE_UNKNOWN,
56         CUBEB_DEVICE_TYPE_INPUT,
57         CUBEB_DEVICE_TYPE_OUTPUT,
58     }
59 }
60 
61 pub type cubeb_devid = *const c_void;
62 
63 #[repr(C)]
64 pub struct cubeb_device {
65     pub output_name: *mut c_char,
66     pub input_name: *mut c_char,
67 }
68 
69 // Explicit Debug impl to work around bug in ctest
70 impl Default for cubeb_device {
default() -> Self71     fn default() -> Self {
72         unsafe { mem::zeroed() }
73     }
74 }
75 
76 impl fmt::Debug for cubeb_device {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result77     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78         f.debug_struct("cubeb_device")
79             .field("output_name", &self.output_name)
80             .field("input_name", &self.input_name)
81             .finish()
82     }
83 }
84 
85 #[repr(C)]
86 pub struct cubeb_device_collection {
87     pub device: *mut cubeb_device_info,
88     pub count: usize,
89 }
90 
91 impl Default for cubeb_device_collection {
default() -> Self92     fn default() -> Self {
93         unsafe { mem::zeroed() }
94     }
95 }
96 
97 impl fmt::Debug for cubeb_device_collection {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result98     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99         f.debug_struct("cubeb_device_collection")
100             .field("device", &self.device)
101             .field("count", &self.count)
102             .finish()
103     }
104 }
105 
106 #[repr(C)]
107 pub struct cubeb_device_info {
108     pub devid: cubeb_devid,
109     pub device_id: *const c_char,
110     pub friendly_name: *const c_char,
111     pub group_id: *const c_char,
112     pub vendor_name: *const c_char,
113 
114     pub device_type: cubeb_device_type,
115     pub state: cubeb_device_state,
116     pub preferred: cubeb_device_pref,
117 
118     pub format: cubeb_device_fmt,
119     pub default_format: cubeb_device_fmt,
120     pub max_channels: c_uint,
121     pub default_rate: c_uint,
122     pub max_rate: c_uint,
123     pub min_rate: c_uint,
124 
125     pub latency_lo: c_uint,
126     pub latency_hi: c_uint,
127 }
128 
129 impl Default for cubeb_device_info {
default() -> Self130     fn default() -> Self {
131         unsafe { mem::zeroed() }
132     }
133 }
134 
135 impl fmt::Debug for cubeb_device_info {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result136     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
137         f.debug_struct("cubeb_device_info")
138             .field("devid", &self.devid)
139             .field("device_id", &self.device_id)
140             .field("friendly_name", &self.friendly_name)
141             .field("group_id", &self.group_id)
142             .field("vendor_name", &self.vendor_name)
143             .field("device_type", &self.device_type)
144             .field("state", &self.state)
145             .field("preferred", &self.preferred)
146             .field("format", &self.format)
147             .field("default_format", &self.default_format)
148             .field("max_channels", &self.max_channels)
149             .field("default_rate", &self.default_rate)
150             .field("max_rate", &self.max_rate)
151             .field("min_rate", &self.min_rate)
152             .field("latency_lo", &self.latency_lo)
153             .field("latency_hi", &self.latency_hi)
154             .finish()
155     }
156 }
157 
158 extern "C" {
cubeb_enumerate_devices( context: *mut cubeb, devtype: cubeb_device_type, collection: *mut cubeb_device_collection, ) -> c_int159     pub fn cubeb_enumerate_devices(
160         context: *mut cubeb,
161         devtype: cubeb_device_type,
162         collection: *mut cubeb_device_collection,
163     ) -> c_int;
cubeb_device_collection_destroy( context: *mut cubeb, collection: *mut cubeb_device_collection, ) -> c_int164     pub fn cubeb_device_collection_destroy(
165         context: *mut cubeb,
166         collection: *mut cubeb_device_collection,
167     ) -> c_int;
cubeb_register_device_collection_changed( context: *mut cubeb, devtype: cubeb_device_type, callback: cubeb_device_collection_changed_callback, user_ptr: *mut c_void, ) -> c_int168     pub fn cubeb_register_device_collection_changed(
169         context: *mut cubeb,
170         devtype: cubeb_device_type,
171         callback: cubeb_device_collection_changed_callback,
172         user_ptr: *mut c_void,
173     ) -> c_int;
174 }
175