Send processed microphone audio over RTP in voice-host (#43090)

## Why

`voice-host` previously drained captured audio locally without sending it to the peer. Connect capture to the outgoing media track while preserving mute boundaries and limiting stale audio.

## What changed

- Resample capture and render references to 48 kHz with Rubato, apply Sonora echo cancellation, noise suppression, and gain control, and encode capture as 20 ms mono Opus packets.
- Attach an Opus track to the WebRTC peer and advance RTP timestamps across mute gaps without introducing sequence-number gaps.
- Prioritize queued controls and shutdown between sends. Clear pending audio and processing history on microphone mute transitions, reject delayed pre-unmute buffers, and reset partial history after capture gaps.
- Size bounded capture and render queues for device rates, validate callback timing, enforce a 500 ms processing deadline, and time out stalled sends after 100 ms.

Receive/decode and TUI integration remain unimplemented. The bundled Opus encoder build requires CMake and a C compiler.

## Testing

Add synthetic-audio tests for Opus decoding at 44.1 and 48 kHz input rates, echo reduction, capture gaps, stale audio, queue capacity, and control priority. A loopback WebRTC test verifies RTP delivery, decoded signal, mute isolation, and timestamp and sequence continuity across mute gaps.

GitOrigin-RevId: 38b8438577cfadb8124f8aeb426dffbab4845964
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-05 20:02:55 +00:00
1 parent 0683368584
commit 7dc7c7a756
20 files changed
+1526 -67

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
//! 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<()> {
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)?;
}
Buffers::set_disabled(&self.buffers.speaker, controls.speaker_suppressed)
}
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;