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.
This commit is contained in:
zarzet committed 2026-11-19 17:56:34 +07:00
1 parent d8e6292811
commit 09ff115ef7
3 files changed
+35 -4

No files matched your search

@@ -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 - Android asks for USB permission; the engine, not the Activity, owns the
connection. Permission denial and absent hardware leave PCM on normal output. connection. Permission denial and absent hardware leave PCM on normal output.
- One Rust worker owns libusb, its claimed interfaces, four isochronous output - One Rust worker owns libusb, its claimed interfaces, the isochronous output
transfers, and a separate explicit-feedback transfer. The bounded queue holds transfers, and a separate explicit-feedback transfer. Up to 24 transfers of
approximately 200 ms of audio (minimum 256 KiB). There is no resampling or gain. 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 - UAC1 fixed rates are verified from descriptors; variable UAC1/UAC2 rates are
read back from the endpoint/clock. Implicit feedback, ambiguous clocks, and read back from the endpoint/clock. Implicit feedback, ambiguous clocks, and
multiple configurations fail closed. Endpoint capacities bound packet sizes. multiple configurations fail closed. Endpoint capacities bound packet sizes.
@@ -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) (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, /// 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 /// 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. /// 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); assert_eq!(queue_bytes(8000, 6) % 6, 0);
} }
#[test] #[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() { fn a_clock_is_only_set_to_a_rate_it_lists() {
// A DAC whose DSP stops at 48 kHz lists two discrete rates. // A DAC whose DSP stops at 48 kHz lists two discrete rates.
let listed = [(44100, 44100, 0), (48000, 48000, 0)]; let listed = [(44100, 44100, 0), (48000, 48000, 0)];
@@ -386,7 +386,7 @@ impl Session {
continue; continue;
} }
let packets = (4000 / s.interval).clamp(1, 32) as usize; 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)?); s.slots.push(Slot::new(packets, s.endpoint.max_packet)?);
} }
if let Some(ep) = &s.feedback { if let Some(ep) = &s.feedback {
@@ -657,6 +657,7 @@ impl Session {
return Err("USB feedback stopped or invalid".into()); 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 { for slot in &mut self.slots {
if slot.pending { if slot.pending {
continue; continue;
@@ -667,7 +668,12 @@ impl Session {
} }
s.frames += slot.music_frames; s.frames += slot.music_frames;
slot.music_frames = 0; 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 mut packet_lengths = [0usize; 32];
let lengths = &mut packet_lengths[..packets]; let lengths = &mut packet_lengths[..packets];
for length in lengths.iter_mut() { for length in lengths.iter_mut() {