Add opt-in local audio devices to the voice helper (#43079)

## What changed

- Add `openDevices` and `setAudioControls` to the helper protocol. Open the default microphone and speaker after runtime initialization and transport negotiation, initially muted and suppressed.
- Use CPAL on macOS, GNU Linux, and MSVC Windows, with native audio dependencies confined to `codex-voice-host`. Add ALSA development dependencies to Linux CI.
- Pack callback samples into preallocated, bounded queues and service devices every 5 ms. Reject incompatible callback configurations, invalidate queued audio on control transitions, and reject capture backlog after unmute using device timestamps.
- Handle recoverable stream underruns and overruns without terminating the helper; terminate on device failure, active audio queue overflow, or stale queued audio.

Device opening only establishes local device readiness. Capture and rendered audio references are drained locally; encoding, decoding, and peer audio are not connected by this change.

## Testing

Add unit tests for mute boundaries, stale playback invalidation, sample bounds, partial-block packing and timestamps, callback size and timing limits, startup silence, queue overflow, and recoverable stream errors.

GitOrigin-RevId: 108aea3d411ef6c12908fe5fb67aaf9cddf4475f
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-05 19:11:52 +00:00
1 parent 51c97f3a67
commit 19b62211d9
14 files changed
+1318 -9

No files matched your search

+327
View File
@@ -0,0 +1,327 @@
//! Owns local device streams on the helper worker. Callbacks allocate no buffers and take no locks.
//! Small callbacks share full queue slots; processing lag still fails the session closed.
//! Capture and actual rendered output carry device timing.
//! References start with worker service; unmute rejects earlier device capture buffers.
#[path = "device_buffers.rs"]
mod buffers;
use std::io;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::time::Instant;
use cpal::FromSample;
use cpal::Sample;
use cpal::SampleFormat;
use cpal::SizedSample;
use cpal::traits::DeviceTrait;
use cpal::traits::HostTrait;
use cpal::traits::StreamTrait;
use buffers::BLOCK;
use buffers::Buffers;
use buffers::CaptureBoundary;
use buffers::Frame;
use buffers::FramePacker;
use buffers::Playback;
use buffers::QUEUE_CAPACITY;
const MAX_CAPTURE_AGE: Duration = Duration::from_secs(/*secs*/ 1);
pub(super) struct Devices {
_input: cpal::Stream,
_output: cpal::Stream,
buffers: Arc<Buffers>,
}
macro_rules! stream {
($format:expr, $build:ident, $($arg:expr),+) => {
match $format {
SampleFormat::I8 => $build::<i8>($($arg),+),
SampleFormat::I16 => $build::<i16>($($arg),+),
SampleFormat::I32 => $build::<i32>($($arg),+),
SampleFormat::I64 => $build::<i64>($($arg),+),
SampleFormat::U8 => $build::<u8>($($arg),+),
SampleFormat::U16 => $build::<u16>($($arg),+),
SampleFormat::U32 => $build::<u32>($($arg),+),
SampleFormat::U64 => $build::<u64>($($arg),+),
SampleFormat::F32 => $build::<f32>($($arg),+),
SampleFormat::F64 => $build::<f64>($($arg),+),
_ => Err(cpal::Error::from(cpal::ErrorKind::UnsupportedConfig)),
}
};
}
impl Devices {
pub(super) fn open() -> io::Result<Self> {
let host = cpal::default_host();
let input = host
.default_input_device()
.ok_or_else(|| io::Error::other("microphone unavailable"))?;
let output = host
.default_output_device()
.ok_or_else(|| io::Error::other("speaker unavailable"))?;
let input_config = input
.default_input_config()
.map_err(|_| io::Error::other("microphone configuration unavailable"))?;
let output_config = output
.default_output_config()
.map_err(|_| io::Error::other("speaker configuration unavailable"))?;
for config in [&input_config, &output_config] {
if config.channels() == 0
|| config.channels() > 32
|| !(8_000..=384_000).contains(&config.sample_rate())
{
return Err(io::Error::other("unsupported audio device configuration"));
}
}
let input_stream_config = bounded_stream_config(&input_config)?;
let output_stream_config = bounded_stream_config(&output_config)?;
let buffers = Arc::new(Buffers::new());
let input = stream!(
input_config.sample_format(),
build_input,
&input,
&input_stream_config,
buffers.clone()
)
.map_err(|_| io::Error::other("failed to open microphone"))?;
let output = stream!(
output_config.sample_format(),
build_output,
&output,
&output_stream_config,
buffers.clone()
)
.map_err(|_| io::Error::other("failed to open speaker"))?;
output
.play()
.map_err(|_| io::Error::other("failed to start speaker"))?;
input
.play()
.map_err(|_| io::Error::other("failed to start microphone"))?;
Ok(Self {
_input: input,
_output: output,
buffers,
})
}
pub(super) fn set_controls(
&self,
controls: codex_realtime_webrtc::AudioControls,
) -> io::Result<()> {
Buffers::set_disabled(&self.buffers.microphone, controls.microphone_muted)?;
Buffers::set_disabled(&self.buffers.speaker, controls.speaker_suppressed)
}
pub(super) fn service(&self) -> io::Result<()> {
// Some backends start callbacks during stream construction, before open returns.
self.buffers.serviced.store(true, Ordering::Release);
// Device-only stage: consume locally until the media worker is attached.
// Reading timestamps here also rejects a stalled callback before readiness is reused.
for queue in [&self.buffers.capture, &self.buffers.rendered] {
for _ in 0..queue.capacity() {
let Some(frame) = queue.pop() else {
break;
};
if frame.at.elapsed() > MAX_CAPTURE_AGE {
return Err(io::Error::other("audio device processing fell behind"));
}
}
}
if self.buffers.failed.load(Ordering::Acquire) {
return Err(io::Error::other("audio device failed"));
}
Ok(())
}
}
fn bounded_stream_config(
supported: &cpal::SupportedStreamConfig,
) -> io::Result<cpal::StreamConfig> {
let cpal::SupportedBufferSize::Range { min, max } = *supported.buffer_size() else {
return Err(io::Error::other("audio callback size range unavailable"));
};
let mut config = supported.config();
// Leave room for a partial block and smaller callbacks arriving before service.
let service_frames = (f64::from(config.sample_rate)
* super::DEVICE_SERVICE_INTERVAL.as_secs_f64())
.ceil() as usize;
let callback_capacity = (BLOCK * QUEUE_CAPACITY).saturating_sub(service_frames + BLOCK);
let min = min.max(1);
let max = max.min(callback_capacity as u32);
if min > max {
return Err(io::Error::other("unsupported audio callback size range"));
}
// Aim for 10 ms without consuming the queue's service headroom.
// Do not fall back to the backend's potentially much larger default buffer.
let frames = (config.sample_rate / 100).clamp(min, max);
let callback_duration =
Duration::from_secs_f64(f64::from(frames) / f64::from(config.sample_rate));
// Backends may deliver smaller callbacks than requested. Packing makes queue
// coverage depend on samples, while an incomplete block adds bounded latency.
let packing_duration = Duration::from_secs_f64(BLOCK as f64 / f64::from(config.sample_rate));
let queue_duration = packing_duration * QUEUE_CAPACITY as u32;
if callback_duration + packing_duration + super::DEVICE_SERVICE_INTERVAL >= MAX_CAPTURE_AGE
|| queue_duration <= super::DEVICE_SERVICE_INTERVAL
{
return Err(io::Error::other("unsupported audio callback timing"));
}
config.buffer_size = cpal::BufferSize::Fixed(frames);
Ok(config)
}
// CPAL reports recoverable underruns/overruns through the same callback as device loss.
fn handle_stream_error(buffers: &Buffers, error: cpal::Error) {
if error.kind() != cpal::ErrorKind::Xrun {
buffers.failed.store(true, Ordering::Release);
}
}
fn build_input<T>(
device: &cpal::Device,
config: &cpal::StreamConfig,
buffers: Arc<Buffers>,
) -> Result<cpal::Stream, cpal::Error>
where
T: SizedSample,
f32: FromSample<T>,
{
let channels = usize::from(config.channels);
let rate = f64::from(config.sample_rate);
let failure = buffers.clone();
let mut origin = None;
let mut boundary = CaptureBoundary::default();
let mut capture = FramePacker::default();
device.build_input_stream(
*config,
move |data: &[T], info: &cpal::InputCallbackInfo| {
let generation = buffers.microphone.load(Ordering::Acquire);
if generation % 2 == 1 {
capture.reset();
return;
}
let timestamp = info.timestamp();
let origin = origin.get_or_insert(timestamp.capture);
let (Some(callback), Some(captured)) = (
timestamp.callback.checked_duration_since(*origin),
timestamp.capture.checked_duration_since(*origin),
) else {
capture.reset();
return;
};
if !boundary.accepts(generation, callback, captured) {
capture.reset();
return;
}
let start = Instant::now()
.checked_sub(
timestamp
.callback
.checked_duration_since(timestamp.capture)
.unwrap_or_default(),
)
.unwrap_or_else(Instant::now);
for (index, chunk) in data.chunks(BLOCK * channels).enumerate() {
let mut frame = Frame {
samples: [0.0; BLOCK],
len: chunk.len() / channels,
at: start + Duration::from_secs_f64((index * BLOCK) as f64 / rate),
generation,
};
for (output, input) in frame.samples.iter_mut().zip(chunk.chunks_exact(channels)) {
*output = input
.iter()
.map(|sample| f32::from_sample(*sample))
.sum::<f32>()
/ channels as f32;
if !output.is_finite() {
buffers.failed.store(true, Ordering::Release);
return;
}
}
if !capture.push(frame, rate, &buffers.capture) {
buffers.failed.store(true, Ordering::Release);
return;
}
}
},
move |error| handle_stream_error(&failure, error),
/*timeout*/ None,
)
}
fn build_output<T>(
device: &cpal::Device,
config: &cpal::StreamConfig,
buffers: Arc<Buffers>,
) -> Result<cpal::Stream, cpal::Error>
where
T: SizedSample + FromSample<f32>,
f32: FromSample<T>,
{
let channels = usize::from(config.channels);
let rate = f64::from(config.sample_rate);
let failure = buffers.clone();
let mut output = OutputState::default();
device.build_output_stream(
*config,
move |data: &mut [T], info: &cpal::OutputCallbackInfo| {
let timestamp = info.timestamp();
let start = Instant::now()
+ timestamp
.playback
.checked_duration_since(timestamp.callback)
.unwrap_or_default();
render_output(data, channels, rate, start, &buffers, &mut output);
},
move |error| handle_stream_error(&failure, error),
/*timeout*/ None,
)
}
#[derive(Default)]
struct OutputState {
playback: Playback,
reference: FramePacker,
}
fn render_output<T>(
data: &mut [T],
channels: usize,
rate: f64,
start: Instant,
buffers: &Buffers,
output: &mut OutputState,
) where
T: SizedSample + FromSample<f32>,
f32: FromSample<T>,
{
if !buffers.serviced.load(Ordering::Acquire) {
data.fill(T::from_sample(0.0));
return;
}
for (index, chunk) in data.chunks_mut(BLOCK * channels).enumerate() {
let mut reference = Frame {
samples: [0.0; BLOCK],
len: chunk.len() / channels,
at: start + Duration::from_secs_f64((index * BLOCK) as f64 / rate),
generation: buffers.speaker.load(Ordering::Acquire),
};
for (frame, sample) in chunk.chunks_mut(channels).zip(&mut reference.samples) {
let rendered = T::from_sample(output.playback.next(buffers));
frame.fill(rendered);
*sample = f32::from_sample(rendered);
}
if !output.reference.push(reference, rate, &buffers.rendered) {
buffers.failed.store(true, Ordering::Release);
}
}
}
#[cfg(test)]
#[path = "devices_tests.rs"]
mod tests;