diff --git a/rust_backend/crates/mobile/src/usb_audio/README.md b/rust_backend/crates/mobile/src/usb_audio/README.md index ae6ec531b..fe8d46429 100644 --- a/rust_backend/crates/mobile/src/usb_audio/README.md +++ b/rust_backend/crates/mobile/src/usb_audio/README.md @@ -9,9 +9,12 @@ opt-in under Settings > Playback > Hi-res and bit-perfect audio. - Android asks for USB permission; the engine, not the Activity, owns the connection. Permission denial and absent hardware leave PCM on normal output. -- One Rust worker owns libusb, its claimed interfaces, four isochronous output - transfers, and a separate explicit-feedback transfer. The bounded queue holds - approximately 200 ms of audio (minimum 256 KiB). There is no resampling or gain. +- One Rust worker owns libusb, its claimed interfaces, the isochronous output + transfers, and a separate explicit-feedback transfer. Up to 24 transfers of + about 4 ms are in flight while the queue has music for them, so the DAC + keeps playing when the worker is late; at least four always are, padded with + silence in an underrun. The bounded queue holds 1.5 s of audio (minimum + 256 KiB). There is no gain; a rate the DAC refuses is resampled. - UAC1 fixed rates are verified from descriptors; variable UAC1/UAC2 rates are read back from the endpoint/clock. Implicit feedback, ambiguous clocks, and multiple configurations fail closed. Endpoint capacities bound packet sizes. diff --git a/rust_backend/crates/mobile/src/usb_audio/descriptors.rs b/rust_backend/crates/mobile/src/usb_audio/descriptors.rs index a0564b2a0..786229c85 100644 --- a/rust_backend/crates/mobile/src/usb_audio/descriptors.rs +++ b/rust_backend/crates/mobile/src/usb_audio/descriptors.rs @@ -252,6 +252,19 @@ pub fn queue_bytes(rate: u32, frame: usize) -> usize { (rate as usize * 3 / 2 * frame).max(256 * 1024) / frame.max(1) * frame.max(1) } +/// Transfers of about 4 ms that are always in flight, padded with silence +/// when the queue runs dry. +pub const TRANSFERS_FLOOR: usize = 4; +/// Transfers in flight while the queue has music for them, about 96 ms: the +/// time the worker may be kept off the processor before the DAC starves. +pub const TRANSFERS: usize = 24; + +/// Whether a free transfer goes out now: up to the floor always, past it +/// only when `queued` bytes fill its `capacity`, so no silence is added. +pub fn feeds_transfer(in_flight: usize, queued: usize, capacity: usize) -> bool { + in_flight < TRANSFERS_FLOOR || queued >= capacity +} + /// The rates a UAC2 clock source lists in its RANGE reply: a 2-byte count, /// then MIN, MAX and RES of 4 bytes each. A malformed or empty reply gives /// none, which [`clock_offers`] takes as a clock that did not say. @@ -678,6 +691,15 @@ mod tests { assert_eq!(queue_bytes(8000, 6) % 6, 0); } #[test] + fn transfers_past_the_floor_carry_music_only() { + // An empty queue still keeps the floor in flight, as silence. + assert!(feeds_transfer(0, 0, 4096)); + assert!(feeds_transfer(TRANSFERS_FLOOR - 1, 0, 4096)); + assert!(!feeds_transfer(TRANSFERS_FLOOR, 4095, 4096)); + assert!(feeds_transfer(TRANSFERS_FLOOR, 4096, 4096)); + assert!(feeds_transfer(TRANSFERS - 1, 8192, 4096)); + } + #[test] fn a_clock_is_only_set_to_a_rate_it_lists() { // A DAC whose DSP stops at 48 kHz lists two discrete rates. let listed = [(44100, 44100, 0), (48000, 48000, 0)]; diff --git a/rust_backend/crates/mobile/src/usb_audio/transport.rs b/rust_backend/crates/mobile/src/usb_audio/transport.rs index 8631108fa..3573f4a01 100644 --- a/rust_backend/crates/mobile/src/usb_audio/transport.rs +++ b/rust_backend/crates/mobile/src/usb_audio/transport.rs @@ -386,7 +386,7 @@ impl Session { continue; } let packets = (4000 / s.interval).clamp(1, 32) as usize; - for _ in 0..4 { + for _ in 0..descriptors::TRANSFERS { s.slots.push(Slot::new(packets, s.endpoint.max_packet)?); } if let Some(ep) = &s.feedback { @@ -657,6 +657,7 @@ impl Session { return Err("USB feedback stopped or invalid".into()); } } + let mut in_flight = self.slots.iter().filter(|slot| slot.pending).count(); for slot in &mut self.slots { if slot.pending { continue; @@ -667,7 +668,12 @@ impl Session { } s.frames += slot.music_frames; slot.music_frames = 0; + slot.submitted = false; } + if !descriptors::feeds_transfer(in_flight, s.bytes.len(), slot.bytes.len()) { + continue; + } + in_flight += 1; let mut packet_lengths = [0usize; 32]; let lengths = &mut packet_lengths[..packets]; for length in lengths.iter_mut() {