From 09ff115ef7726c35f17fce9eefe264d42a007ccd Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:56:34 +0700 Subject: [PATCH] fix(usb): keep about 96 ms of transfers in flight, so a late worker does not starve the DAC Direct USB kept four isochronous transfers of about 4 ms in flight: 16 ms of audio in the kernel. The worker that refills them is an ordinary thread at nice -16, not a real-time one, so when the phone is busy (opening or switching to another app) it can be kept off the processor longer than that, and the DAC runs dry: a stutter, on some DACs followed by a mute while they lock again. The 1.5 s queue behind the worker does not help, since nothing moves it into the kernel while the worker waits. Up to 24 transfers now go out while the queue has music to fill them whole. Past the first four a transfer is only submitted with music, never padded, so an underrun or the end of a song behaves as before. Pause and seek still cancel everything in flight at once. Feedback from the DAC takes effect up to 96 ms later, which is slow drift and well inside a DAC's own buffer. Not run on a DAC: the cause is read from the code, not measured. --- .../crates/mobile/src/usb_audio/README.md | 9 +++++--- .../mobile/src/usb_audio/descriptors.rs | 22 +++++++++++++++++++ .../crates/mobile/src/usb_audio/transport.rs | 8 ++++++- 3 files changed, 35 insertions(+), 4 deletions(-) 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() {