usbdac_core/capabilities.rs
1//! What a DAC offers: the data types returned by
2//! [`Dac::capabilities`](crate::Dac::capabilities).
3
4use crate::volume::VolumeRange;
5
6/// Identity of the USB device and the interfaces the host has claimed.
7///
8/// On Android the host gets the file descriptor from
9/// `UsbDeviceConnection.getFileDescriptor()` after claiming the audio
10/// interfaces with `claimInterface`; `claimed_interfaces` lists those interface
11/// numbers. The library never claims or releases interfaces itself: it checks
12/// that the host claimed all AudioControl and playback AudioStreaming interfaces
13/// of the active configuration, plus only preflight-approved feedback sources.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct DeviceInfo {
16 pub vendor_id: u16,
17 pub product_id: u16,
18 /// Product string (`UsbDevice.getProductName()`); used for quirk matching.
19 pub product_name: String,
20 pub manufacturer_name: Option<String>,
21 /// Platform device name, for example `/dev/bus/usb/001/004`. Only used to
22 /// key the remembered original volume.
23 pub device_name: Option<String>,
24 /// Interface numbers the host has claimed.
25 pub claimed_interfaces: Vec<u8>,
26 /// Active configuration identified by feedback claim preflight.
27 pub configuration_id: Option<u8>,
28 /// Plausible feedback sources the host could not claim.
29 pub unavailable_feedback_interfaces: Vec<u8>,
30}
31
32impl DeviceInfo {
33 pub fn new(vendor_id: u16, product_id: u16, product_name: impl Into<String>) -> Self {
34 Self {
35 vendor_id,
36 product_id,
37 product_name: product_name.into(),
38 manufacturer_name: None,
39 device_name: None,
40 claimed_interfaces: Vec::new(),
41 configuration_id: None,
42 unavailable_feedback_interfaces: Vec::new(),
43 }
44 }
45
46 pub fn with_claimed_interfaces(mut self, interfaces: impl Into<Vec<u8>>) -> Self {
47 self.claimed_interfaces = interfaces.into();
48 self
49 }
50
51 pub fn with_device_name(mut self, name: impl Into<String>) -> Self {
52 self.device_name = Some(name.into());
53 self
54 }
55
56 pub fn with_manufacturer_name(mut self, name: impl Into<String>) -> Self {
57 self.manufacturer_name = Some(name.into());
58 self
59 }
60}
61
62/// USB Audio Class version of the device's interfaces.
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub enum UacVersion {
65 V1,
66 V2,
67 Unknown,
68}
69
70/// Bus speed the device is connected at.
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72pub enum LinkSpeed {
73 Low,
74 Full,
75 High,
76 Super,
77 SuperPlus,
78 Unknown,
79}
80
81/// Synchronisation type of an isochronous data endpoint.
82#[derive(Debug, Clone, Copy, PartialEq, Eq)]
83pub enum SyncKind {
84 /// The DAC runs its own clock; an explicit feedback endpoint, when
85 /// present, tells the host the true rate.
86 Asynchronous,
87 /// The DAC follows the host's packet timing.
88 Adaptive,
89 Synchronous,
90 None,
91}
92
93/// What keeps the host's packets and the DAC's clock in step on an endpoint:
94/// the transport's pacing, named for diagnostics. It only labels what the
95/// transport does for the endpoint; it never selects a mode.
96#[derive(Debug, Clone, Copy, PartialEq, Eq)]
97#[non_exhaustive]
98pub enum Pacing {
99 /// An asynchronous endpoint whose explicit feedback endpoint (`endpoint`,
100 /// the address) is polled; packet sizes follow the rate the DAC reports.
101 ExplicitFeedback { endpoint: u8 },
102 /// Packet frame counts follow a compatible capture audio endpoint.
103 ImplicitFeedback {
104 interface_number: u8,
105 alt_setting: u8,
106 endpoint: u8,
107 },
108 /// An adaptive endpoint: the DAC follows the host's packet timing.
109 Adaptive,
110 /// A synchronous endpoint: the DAC and the host share the bus clock.
111 Synchronous,
112 /// No rate information comes back from the DAC: the host sends at the
113 /// nominal rate. This is an asynchronous endpoint without a feedback
114 /// endpoint, streamed only because
115 /// [`DacConfig::allow_unsynchronized_async`](crate::DacConfig::allow_unsynchronized_async)
116 /// is on (never bit-perfect), or an endpoint that declares no
117 /// synchronisation at all.
118 Unsynchronized,
119}
120
121impl Pacing {
122 /// Stable identifier: `explicit_feedback`, `implicit_feedback`, `adaptive`, `synchronous`,
123 /// `unsynchronized`.
124 pub fn code(self) -> &'static str {
125 match self {
126 Self::ExplicitFeedback { .. } => "explicit_feedback",
127 Self::ImplicitFeedback { .. } => "implicit_feedback",
128 Self::Adaptive => "adaptive",
129 Self::Synchronous => "synchronous",
130 Self::Unsynchronized => "unsynchronized",
131 }
132 }
133
134 /// The feedback endpoint address, for [`Pacing::ExplicitFeedback`].
135 pub fn feedback_endpoint(self) -> Option<u8> {
136 match self {
137 Self::ExplicitFeedback { endpoint } => Some(endpoint),
138 _ => None,
139 }
140 }
141}
142
143/// The usage bits of an isochronous endpoint's `bmAttributes`.
144#[derive(Debug, Clone, Copy, PartialEq, Eq)]
145pub enum EndpointUsage {
146 /// A data endpoint.
147 Data,
148 /// A feedback endpoint.
149 Feedback,
150 /// An implicit feedback data endpoint (the feedback is carried by an
151 /// associated IN data endpoint).
152 FeedbackData,
153 Reserved,
154}
155
156/// What an alternate setting carries.
157#[derive(Debug, Clone, Copy, PartialEq, Eq)]
158pub enum AltFormat {
159 /// Linear PCM (also the carrier for DoP).
160 Pcm,
161 /// Native DSD (format tag DSD).
162 Dsd,
163 /// Anything else (float, PCM8, ...); never selected.
164 Other,
165}
166
167/// A continuous or discrete range of sampling frequencies.
168#[derive(Debug, Clone, Copy, PartialEq, Eq)]
169pub struct RateRange {
170 pub min_hz: u32,
171 pub max_hz: u32,
172 /// Step between supported values; 0 for a single discrete rate.
173 pub step_hz: u32,
174}
175
176/// One isochronous OUT alternate setting of an AudioStreaming interface.
177#[derive(Debug, Clone, PartialEq)]
178#[non_exhaustive]
179pub struct AltSetting {
180 pub interface_number: u8,
181 pub alt_setting: u8,
182 pub endpoint_address: u8,
183 pub format: AltFormat,
184 pub channels: u16,
185 /// Bytes per sample on the wire (the UAC `bSubslotSize`).
186 pub subslot_bytes: u8,
187 /// Significant bits per sample (the UAC `bBitResolution`).
188 pub valid_bits: u8,
189 pub rate_ranges: Vec<RateRange>,
190 /// A representative list of the supported rates (the common audio rates
191 /// inside `rate_ranges`, or the discrete rates themselves).
192 pub sample_rates: Vec<u32>,
193 pub sync_type: SyncKind,
194 /// Address of the explicit feedback endpoint tied to this alternate
195 /// setting, if it has one.
196 pub feedback_endpoint: Option<u8>,
197 pub max_packet_bytes: usize,
198 pub service_interval_us: u32,
199 pub uac_version: UacVersion,
200 /// The endpoint's `bInterval`.
201 pub endpoint_interval: u8,
202 /// The endpoint's usage bits.
203 pub usage: EndpointUsage,
204 /// The endpoint's `bRefresh`.
205 pub refresh: u8,
206 /// The endpoint's `bSynchAddress`: the feedback endpoint it names, or 0.
207 /// [`feedback_endpoint`](Self::feedback_endpoint) can be set while this is 0
208 /// (a feedback endpoint found by its usage bits).
209 pub synch_address: u8,
210 /// How a stream on this alternate setting would be paced, as
211 /// [`StreamFormat::pacing`](crate::StreamFormat::pacing) will report it.
212 /// `None` when the library would not stream it with the current
213 /// configuration: an asynchronous endpoint without a feedback endpoint
214 /// while [`DacConfig::allow_unsynchronized_async`](crate::DacConfig::allow_unsynchronized_async)
215 /// is off, or one that declares a feedback endpoint that cannot be resolved.
216 pub pacing: Option<Pacing>,
217}
218
219/// Summary of what the DAC can do.
220#[derive(Debug, Clone, PartialEq)]
221#[non_exhaustive]
222pub struct Capabilities {
223 pub uac_version: UacVersion,
224 pub link_speed: LinkSpeed,
225 pub alt_settings: Vec<AltSetting>,
226 /// Union of the PCM rates over all PCM alternate settings.
227 pub pcm_sample_rates: Vec<u32>,
228 /// Union of the PCM valid-bit depths.
229 pub pcm_bit_depths: Vec<u8>,
230 pub channel_counts: Vec<u16>,
231 pub supports_pcm: bool,
232 /// A PCM alternate setting with at least 24 valid bits that runs at
233 /// 176.4 kHz or faster exists, which is what DoP64 needs.
234 pub supports_dop: bool,
235 /// Highest DoP DSD rate (bits per second per channel, e.g. 2822400 for
236 /// DSD64) the PCM carrier can take, if DoP is supported.
237 pub dop_max_dsd_bit_rate: Option<u32>,
238 pub supports_native_dsd: bool,
239 /// DSD rates (bits per second per channel) the native DSD alternate
240 /// settings accept, derived from their wire rates and subslot sizes.
241 pub native_dsd_bit_rates: Vec<u32>,
242 /// At least one asynchronous alternate setting has an explicit feedback
243 /// endpoint.
244 pub has_explicit_feedback: bool,
245 /// The DAC's hardware volume control, if it has a writable one.
246 pub volume: Option<VolumeRange>,
247 /// The DAC has a usable hardware volume control (`volume.is_some()`). A
248 /// DAC that has none is refused by `open_stream` unless
249 /// `DacConfig::require_hardware_volume` is off.
250 pub has_hardware_volume: bool,
251 pub has_mute: bool,
252}
253
254#[cfg(test)]
255mod tests {
256 use super::*;
257
258 #[test]
259 fn device_info_builder_sets_the_optional_fields() {
260 let info = DeviceInfo::new(1, 2, "Dac")
261 .with_claimed_interfaces(vec![0u8, 1, 2])
262 .with_device_name("/dev/bus/usb/001/002")
263 .with_manufacturer_name("Acme");
264 assert_eq!(info.claimed_interfaces, vec![0, 1, 2]);
265 assert_eq!(info.device_name.as_deref(), Some("/dev/bus/usb/001/002"));
266 assert_eq!(info.manufacturer_name.as_deref(), Some("Acme"));
267 }
268}