Files
codex/codex-rs/voice-host/src/playback.rs
T
Benjamin Carlsson 2ef0f66901 Increase Linux voice callback periods at 48 kHz (#63169)
## Why

At 48 kHz, the two-period ALSA capture ring can overrun on PipeWire
graphs with 4,096- or 4,800-frame cycles, dropping microphone samples.

## What changed

Raise the Linux capture and playback target from 50 ms to 80 ms at
48 kHz, within device and queue limits. Give playback room for two
periods plus block-boundary headroom, and allow up to 160 ms for a
blocked write.

## Testing

Extend the PipeWire cycle simulation to check for capture overruns
across cycle-size changes and verify that playback can render a
two-period burst from 20 ms packets.

GitOrigin-RevId: ee5e70497bee6f8a00dd3f2f2ab5827bafc36b65
2026-11-22 18:09:39 +00:00

124 lines
4.1 KiB
Rust

//! Bounded mono F32LE writes for the native sink, never called from a device callback.
//! Each writer belongs to one speaker epoch. Reset cancels it before locking the producer.
use super::buffers::BLOCK;
use super::buffers::Buffers;
use super::buffers::Frame;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::PoisonError;
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::time::Instant;
// Leave room for the block crossing the two-period boundary. Splitting
// writes as callbacks free individual samples can exhaust queue slots.
pub(super) const LINUX_48K_LIMIT: u32 = 2 * super::LINUX_48K_PERIOD + BLOCK as u32 - 1;
pub(super) struct PlaybackState {
pub(super) producer: Mutex<()>,
buffers: Arc<Buffers>,
rate: u32,
}
pub(super) struct PlaybackPort(pub(super) Arc<PlaybackState>);
pub(crate) struct PlaybackWriter {
state: Arc<PlaybackState>,
epoch: u64,
}
impl PlaybackPort {
pub(super) fn new(buffers: Arc<Buffers>, rate: u32) -> Self {
Self(Arc::new(PlaybackState {
producer: Mutex::new(()),
buffers,
rate,
}))
}
pub(super) fn writer(&self) -> PlaybackWriter {
PlaybackWriter {
state: self.0.clone(),
epoch: self.0.buffers.speaker.load(Ordering::Acquire),
}
}
}
impl PlaybackWriter {
pub(crate) fn fail_if_current(&self) {
let buffers = &self.state.buffers;
let _transition = buffers
.speaker_failure_gate
.lock()
.unwrap_or_else(PoisonError::into_inner);
if buffers.speaker.load(Ordering::Acquire) == self.epoch {
buffers.failed.store(true, Ordering::Release);
}
}
pub(crate) fn rate(&self) -> u32 {
self.state.rate
}
pub(crate) fn write(&self, bytes: &[u8]) -> Result<usize, &'static str> {
if bytes.is_empty() || !bytes.len().is_multiple_of(4) {
return Err("invalid speaker buffer");
}
let mut frame = Frame {
samples: [0.0; BLOCK],
len: (bytes.len() / 4).min(BLOCK),
at: Instant::now(),
generation: self.epoch,
};
for (sample, bytes) in frame.samples.iter_mut().zip(bytes.as_chunks().0) {
*sample = f32::from_le_bytes(*bytes);
if !sample.is_finite() {
return Err("invalid speaker sample");
}
}
let _producer = self
.state
.producer
.lock()
.map_err(|_| "speaker writer failed")?;
let buffers = &self.state.buffers;
let windows_per_second = if cfg!(target_os = "linux") { 10 } else { 25 };
let (budget, wait_ms) = if cfg!(target_os = "linux") && self.state.rate == 48_000 {
// Allow two periods when sub-period cycles delay the next callback.
(LINUX_48K_LIMIT, 2 * u64::from(super::LINUX_48K_PERIOD) / 48)
} else {
(self.state.rate / windows_per_second, 100)
};
let deadline = Instant::now() + Duration::from_millis(wait_ms);
let limit = budget.min((BLOCK * buffers.playback.capacity()) as u32);
loop {
if self.epoch % 2 == 1
|| buffers.speaker.load(Ordering::Acquire) != self.epoch
|| buffers.failed.load(Ordering::Acquire)
{
return Err("speaker writer cancelled");
}
if buffers.queued.load(Ordering::Acquire) + frame.len as u32 <= limit
&& !buffers.playback.is_full()
{
let bytes = frame.len * 4;
buffers
.push_playback(frame)
.map_err(|_| "speaker queue failed")?;
return Ok(bytes);
}
if Instant::now() >= deadline {
// The sink must consume this chunk to catch up with the device clock.
// Dropping it keeps the bounded queue and current epoch intact.
return Ok(frame.len * 4);
}
std::thread::park_timeout(Duration::from_millis(/*millis*/ 1));
}
}
}
#[cfg(test)]
#[path = "playback_tests.rs"]
mod tests;