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 V3,
68 Unknown,
69}
70
71/// Bus speed the device is connected at.
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub enum LinkSpeed {
74 Low,
75 Full,
76 High,
77 Super,
78 SuperPlus,
79 Unknown,
80}
81
82/// Synchronisation type of an isochronous data endpoint.
83#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84pub enum SyncKind {
85 /// The DAC runs its own clock; an explicit feedback endpoint, when
86 /// present, tells the host the true rate.
87 Asynchronous,
88 /// The DAC follows the host's packet timing.
89 Adaptive,
90 Synchronous,
91 None,
92}
93
94/// What keeps the host's packets and the DAC's clock in step on an endpoint:
95/// the transport's pacing, named for diagnostics. It only labels what the
96/// transport does for the endpoint; it never selects a mode.
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98#[non_exhaustive]
99pub enum Pacing {
100 /// An asynchronous endpoint whose explicit feedback endpoint (`endpoint`,
101 /// the address) is polled; packet sizes follow the rate the DAC reports.
102 ExplicitFeedback { endpoint: u8 },
103 /// Packet frame counts follow a compatible capture audio endpoint.
104 ImplicitFeedback {
105 interface_number: u8,
106 alt_setting: u8,
107 endpoint: u8,
108 },
109 /// An adaptive endpoint: the DAC follows the host's packet timing.
110 Adaptive,
111 /// A synchronous endpoint: the DAC and the host share the bus clock.
112 Synchronous,
113 /// No rate information comes back from the DAC: the host sends at the
114 /// nominal rate. This is an asynchronous endpoint without a feedback
115 /// endpoint, streamed only because
116 /// [`DacConfig::allow_unsynchronized_async`](crate::DacConfig::allow_unsynchronized_async)
117 /// is on (never bit-perfect), or an endpoint that declares no
118 /// synchronisation at all.
119 Unsynchronized,
120}
121
122impl Pacing {
123 /// Stable identifier: `explicit_feedback`, `implicit_feedback`, `adaptive`, `synchronous`,
124 /// `unsynchronized`.
125 pub fn code(self) -> &'static str {
126 match self {
127 Self::ExplicitFeedback { .. } => "explicit_feedback",
128 Self::ImplicitFeedback { .. } => "implicit_feedback",
129 Self::Adaptive => "adaptive",
130 Self::Synchronous => "synchronous",
131 Self::Unsynchronized => "unsynchronized",
132 }
133 }
134
135 /// The feedback endpoint address, for [`Pacing::ExplicitFeedback`].
136 pub fn feedback_endpoint(self) -> Option<u8> {
137 match self {
138 Self::ExplicitFeedback { endpoint } => Some(endpoint),
139 _ => None,
140 }
141 }
142}
143
144/// The usage bits of an isochronous endpoint's `bmAttributes`.
145#[derive(Debug, Clone, Copy, PartialEq, Eq)]
146pub enum EndpointUsage {
147 /// A data endpoint.
148 Data,
149 /// A feedback endpoint.
150 Feedback,
151 /// An implicit feedback data endpoint (the feedback is carried by an
152 /// associated IN data endpoint).
153 FeedbackData,
154 Reserved,
155}
156
157/// What an alternate setting carries.
158#[derive(Debug, Clone, Copy, PartialEq, Eq)]
159pub enum AltFormat {
160 /// Linear PCM (also the carrier for DoP).
161 Pcm,
162 /// Type I unsigned 8-bit PCM.
163 PcmU8,
164 /// Type I IEEE-754 binary32 PCM.
165 PcmFloat32,
166 /// DoP only, using a source-backed carrier even without advertised PCM.
167 Dop,
168 /// Native DSD (format tag DSD).
169 Dsd,
170 /// Other formats, including unsupported Type II/III compressed transports.
171 Other,
172}
173
174/// A continuous or discrete range of sampling frequencies.
175#[derive(Debug, Clone, Copy, PartialEq, Eq)]
176pub struct RateRange {
177 pub min_hz: u32,
178 pub max_hz: u32,
179 /// Step between supported values; 0 for a single discrete rate.
180 pub step_hz: u32,
181}
182
183/// One playback configuration on an isochronous OUT alternate.
184/// A UAC2 or UAC3 OUT alternate can appear with several implicit-feedback IN
185/// alternates. Each entry's rates apply to the input named by its pacing.
186#[derive(Debug, Clone, PartialEq)]
187#[non_exhaustive]
188pub struct AltSetting {
189 pub interface_number: u8,
190 pub alt_setting: u8,
191 pub endpoint_address: u8,
192 pub format: AltFormat,
193 pub channels: u16,
194 /// Bytes per sample on the wire (the UAC `bSubslotSize`).
195 pub subslot_bytes: u8,
196 /// Significant bits per sample (the UAC `bBitResolution`).
197 pub valid_bits: u8,
198 pub rate_ranges: Vec<RateRange>,
199 /// A representative list of the supported rates (the common audio rates
200 /// inside `rate_ranges`, or the discrete rates themselves).
201 pub sample_rates: Vec<u32>,
202 pub sync_type: SyncKind,
203 /// Address of the explicit feedback endpoint tied to this alternate
204 /// setting, if it has one.
205 pub feedback_endpoint: Option<u8>,
206 pub max_packet_bytes: usize,
207 pub service_interval_us: u32,
208 pub uac_version: UacVersion,
209 /// The endpoint's `bInterval`.
210 pub endpoint_interval: u8,
211 /// The endpoint's usage bits.
212 pub usage: EndpointUsage,
213 /// The endpoint's `bRefresh`.
214 pub refresh: u8,
215 /// The endpoint's `bSynchAddress`: the feedback endpoint it names, or 0.
216 /// [`feedback_endpoint`](Self::feedback_endpoint) can be set while this is 0
217 /// (a feedback endpoint found by its usage bits).
218 pub synch_address: u8,
219 /// How a stream on this alternate setting would be paced, as
220 /// [`StreamFormat::pacing`](crate::StreamFormat::pacing) will report it.
221 /// `None` when the library would not stream it with the current
222 /// configuration: an asynchronous endpoint without a feedback endpoint
223 /// while [`DacConfig::allow_unsynchronized_async`](crate::DacConfig::allow_unsynchronized_async)
224 /// is off, or one that declares a feedback endpoint that cannot be resolved.
225 pub pacing: Option<Pacing>,
226}
227
228/// Summary of what the DAC can do.
229#[derive(Debug, Clone, PartialEq)]
230#[non_exhaustive]
231pub struct Capabilities {
232 /// All claimed playback OUT alternates in the cached descriptor snapshot parsed.
233 /// False means an absent shape cannot be inferred from the snapshot.
234 pub exclusion_snapshot_complete: bool,
235 /// `Other` alternates whose descriptor proves they can never carry PCM:
236 /// native-only DSD aliases and UAC2 alternates whose bmFormats has none of
237 /// the PCM, PCM8 and IEEE_FLOAT bits (such as a RAW_DATA-only alternate).
238 pub native_only_aliases: Vec<(u8, u8, u8)>,
239 /// UAC1 output alternates with a complete standard Type-I rate descriptor.
240 pub complete_uac1_rate_alternates: Vec<(u8, u8, u8)>,
241 pub uac_version: UacVersion,
242 pub link_speed: LinkSpeed,
243 pub alt_settings: Vec<AltSetting>,
244 /// Union of the PCM rates over all PCM alternate settings.
245 pub pcm_sample_rates: Vec<u32>,
246 /// Union of the PCM valid-bit depths.
247 pub pcm_bit_depths: Vec<u8>,
248 pub channel_counts: Vec<u16>,
249 pub supports_pcm: bool,
250 /// A PCM alternate setting with at least 24 valid bits that runs at
251 /// 176.4 kHz or faster exists, which is what DoP64 needs.
252 pub supports_dop: bool,
253 /// Highest DoP DSD rate (bits per second per channel, e.g. 2822400 for
254 /// DSD64) the PCM carrier can take, if DoP is supported.
255 pub dop_max_dsd_bit_rate: Option<u32>,
256 pub supports_native_dsd: bool,
257 /// DSD rates (bits per second per channel) the native DSD alternate
258 /// settings accept, derived from their wire rates and subslot sizes.
259 pub native_dsd_bit_rates: Vec<u32>,
260 /// At least one asynchronous alternate setting has an explicit feedback
261 /// endpoint.
262 pub has_explicit_feedback: bool,
263 /// The DAC's hardware volume control, if it has a writable one.
264 pub volume: Option<VolumeRange>,
265 /// The DAC has a usable hardware volume control (`volume.is_some()`). A
266 /// DAC that has none is refused by `open_stream` unless
267 /// `DacConfig::require_hardware_volume` is off.
268 pub has_hardware_volume: bool,
269 pub has_mute: bool,
270}
271
272#[cfg(test)]
273mod tests {
274 use super::*;
275
276 #[test]
277 fn device_info_builder_sets_the_optional_fields() {
278 let info = DeviceInfo::new(1, 2, "Dac")
279 .with_claimed_interfaces(vec![0u8, 1, 2])
280 .with_device_name("/dev/bus/usb/001/002")
281 .with_manufacturer_name("Acme");
282 assert_eq!(info.claimed_interfaces, vec![0, 1, 2]);
283 assert_eq!(info.device_name.as_deref(), Some("/dev/bus/usb/001/002"));
284 assert_eq!(info.manufacturer_name.as_deref(), Some("Acme"));
285 }
286}
287
288/// Authoritative shipped API boundaries used by the generated SDK status page.
289/// Update together with implementation when enabling another transport tier.
290#[derive(Debug, Clone, Copy)]
291pub struct ShippedSupport {
292 pub uac_versions: &'static [UacVersion],
293 pub playback: bool,
294 pub pcm_capture: bool,
295 pub duplex: bool,
296 /// None means no process-wide cap; each physical attachment has one owner.
297 pub max_open_devices: Option<usize>,
298 pub vendor_protocols: bool,
299 pub type_i_formats: &'static [&'static str],
300}
301
302pub const SHIPPED_SUPPORT: ShippedSupport = ShippedSupport {
303 uac_versions: &[UacVersion::V1, UacVersion::V2, UacVersion::V3],
304 playback: true,
305 pcm_capture: true,
306 duplex: true,
307 max_open_devices: None,
308 vendor_protocols: false,
309 type_i_formats: &["signed_pcm", "unsigned_pcm8", "float32"],
310};