mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
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:
1 parent
0683368584
commit
7dc7c7a756
20 files changed
+1526
-67
No files matched your search
@@ -5,6 +5,10 @@
|
||||
|
||||
#[path = "device_buffers.rs"]
|
||||
mod buffers;
|
||||
#[path = "capture_worker.rs"]
|
||||
mod capture_worker;
|
||||
#[path = "processing.rs"]
|
||||
mod processing;
|
||||
|
||||
use std::io;
|
||||
use std::sync::Arc;
|
||||
@@ -25,6 +29,7 @@ use buffers::Buffers;
|
||||
use buffers::CaptureBoundary;
|
||||
use buffers::Frame;
|
||||
use buffers::FramePacker;
|
||||
use buffers::MAX_CALLBACK_FRAMES;
|
||||
use buffers::Playback;
|
||||
use buffers::QUEUE_CAPACITY;
|
||||
|
||||
@@ -33,7 +38,7 @@ const MAX_CAPTURE_AGE: Duration = Duration::from_secs(/*secs*/ 1);
|
||||
pub(super) struct Devices {
|
||||
_input: cpal::Stream,
|
||||
_output: cpal::Stream,
|
||||
buffers: Arc<Buffers>,
|
||||
worker: capture_worker::CaptureWorker,
|
||||
}
|
||||
|
||||
macro_rules! stream {
|
||||
@@ -79,7 +84,22 @@ impl Devices {
|
||||
}
|
||||
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 buffers = Arc::new(Buffers::new(
|
||||
input_config.sample_rate(),
|
||||
output_config.sample_rate(),
|
||||
));
|
||||
let processor =
|
||||
processing::Processor::new(input_config.sample_rate(), output_config.sample_rate())
|
||||
.map_err(io::Error::other)?;
|
||||
let (cpal::BufferSize::Fixed(input_frames), cpal::BufferSize::Fixed(output_frames)) = (
|
||||
input_stream_config.buffer_size,
|
||||
output_stream_config.buffer_size,
|
||||
) else {
|
||||
return Err(io::Error::other("audio callback size unavailable"));
|
||||
};
|
||||
processor
|
||||
.validate_callback_timing(input_frames, output_frames)
|
||||
.map_err(io::Error::other)?;
|
||||
let input = stream!(
|
||||
input_config.sample_format(),
|
||||
build_input,
|
||||
@@ -105,37 +125,26 @@ impl Devices {
|
||||
Ok(Self {
|
||||
_input: input,
|
||||
_output: output,
|
||||
buffers,
|
||||
worker: capture_worker::CaptureWorker {
|
||||
buffers,
|
||||
processor,
|
||||
pending: Default::default(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn set_controls(
|
||||
&self,
|
||||
&mut 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)
|
||||
self.worker.set_controls(controls)
|
||||
}
|
||||
|
||||
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(())
|
||||
pub(super) async fn service(
|
||||
&mut self,
|
||||
audio: &mut crate::audio_track::AudioTrack,
|
||||
) -> io::Result<usize> {
|
||||
self.worker.service(audio, Instant::now).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,13 +155,8 @@ fn bounded_stream_config(
|
||||
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);
|
||||
let max = max.min(MAX_CALLBACK_FRAMES as u32);
|
||||
if min > max {
|
||||
return Err(io::Error::other("unsupported audio callback size range"));
|
||||
}
|
||||
@@ -225,6 +229,7 @@ where
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
.unwrap_or_else(Instant::now);
|
||||
capture.discard_capture_gap(start, rate);
|
||||
for (index, chunk) in data.chunks(BLOCK * channels).enumerate() {
|
||||
let mut frame = Frame {
|
||||
samples: [0.0; BLOCK],
|
||||
|
||||
Reference in new issue
Block a user