Files
codex/codex-rs/voice-host/src/capture_worker.rs
T
Benjamin Carlsson a9896da3fe Connect voice-host RTP audio to speaker playback (#43248)
## Why

The voice host drained incoming RTP packets without playing them. Route received audio through decoding and speaker output while preserving speaker suppression boundaries.

## What changed

- Add a GStreamer pipeline with RTP jitter buffering, Opus decoding, conversion, and resampling into the native speaker sink.
- Rebuild playout when the speaker is re-enabled, reject packets received before the new pipeline started, and discard queued audio and echo references on speaker transitions while preserving capture history.
- Limit each receive pass to 64 packets so incoming media cannot indefinitely delay audio controls.
- Prevent retired sink failures from failing a new speaker generation, and retain the last coherent playback delay estimate during callback contention.

## Testing

Add tests for real Opus playback and stale packet rejection, combined microphone mute and speaker suppression, retired sink failures, delay contention, and echo-reference reset without losing capture history.

GitOrigin-RevId: c91c94a2ba2347ac98af4e7fd1dc331ae766cc45
2026-09-06 16:40:33 +00:00

83 lines
3.1 KiB
Rust

//! Pumps bounded device queues through capture processing and the outgoing audio track.
//! Devices retain real stream ownership; all queued and partial capture obey mute generations.
//! Retain at most one encoded batch and return between packets so controls can take priority.
use std::collections::VecDeque;
use std::io;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Instant;
use super::MAX_CAPTURE_AGE;
use super::buffers::Buffers;
use super::processing;
pub(super) struct CaptureWorker {
pub(super) buffers: Arc<Buffers>,
pub(super) processor: processing::Processor,
pub(super) pending: VecDeque<crate::audio_track::EncodedAudio>,
}
impl CaptureWorker {
pub(super) fn set_controls(
&mut self,
controls: codex_realtime_webrtc::AudioControls,
) -> io::Result<()> {
// Retire the old sink before rebuilding microphone processing.
self.buffers
.set_speaker_disabled(controls.speaker_suppressed)?;
let previous = self.buffers.microphone.load(Ordering::Acquire);
Buffers::set_disabled(&self.buffers.microphone, controls.microphone_muted)?;
if previous != self.buffers.microphone.load(Ordering::Acquire) {
self.pending.clear();
self.processor.reset().map_err(io::Error::other)?;
}
Ok(())
}
pub(super) async fn service(
&mut self,
audio: &mut crate::audio_track::AudioTrack,
now: impl Fn() -> Instant,
) -> io::Result<usize> {
// Some backends start callbacks during stream construction, before open returns.
self.buffers.serviced.store(true, Ordering::Release);
let generation = self.buffers.microphone.load(Ordering::Acquire);
for _ in 0..self.buffers.rendered.capacity() {
let Some(frame) = self.buffers.rendered.pop() else {
break;
};
if frame.at.elapsed() > MAX_CAPTURE_AGE {
return Err(io::Error::other("audio rendering fell behind"));
}
self.processor.render(&frame).map_err(io::Error::other)?;
}
if self.pending.is_empty() {
for _ in 0..self.buffers.capture.capacity() {
let Some(frame) = self.buffers.capture.pop() else {
break;
};
if generation.is_multiple_of(2) && frame.generation == generation {
let frames = self.processor.capture(&frame, &now);
self.pending.extend(frames.map_err(io::Error::other)?);
}
}
}
if self.buffers.failed.load(Ordering::Acquire) {
return Err(io::Error::other("audio device failed"));
}
let Some(frame) = self.pending.pop_front() else {
return Ok(0);
};
if now().saturating_duration_since(frame.at) > processing::MAX_PROCESSING_DELAY {
return Err(io::Error::other("voice processing fell behind"));
}
audio.send(frame).await.map_err(io::Error::other)?;
Ok(1)
}
}
#[cfg(test)]
#[path = "capture_worker_tests.rs"]
mod tests;