mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Add bounded GStreamer playback components to the voice host (#43244)
## What changed - Add a `GstAudioSink` subclass backed by a bounded mono `F32LE` playback writer, with partial writes, cancellation on speaker epoch changes, and delay accounting for queued samples and pending device output. - Recreate the CPAL output stream and discard buffered audio when speaker controls change the epoch, preventing stale audio from surviving suppression. - Add GStreamer dependencies, adjust Windows Bazel native-tool selection, and allow the helper handshake time to load startup-linked libraries. - Separate installed-client tests from protocol lifecycle tests and supply native startup libraries in test packages. ## Testing Add unit coverage for partial writes, sample accounting, suppression cancellation, stale writers, device delay, invalid samples, and stalled consumption. GitOrigin-RevId: c48c48d2fee00659cf6f8a4039a6745385cb77cd
This commit is contained in:
1 parent
ac192cd793
commit
9daf7d22ca
14 files changed
+1132
-342
No files matched your search
@@ -3,10 +3,14 @@
|
||||
//! Capture and actual rendered output carry device timing.
|
||||
//! References start with worker service; unmute rejects earlier device capture buffers.
|
||||
|
||||
#[path = "audio_sink.rs"]
|
||||
mod audio_sink;
|
||||
#[path = "device_buffers.rs"]
|
||||
mod buffers;
|
||||
#[path = "capture_worker.rs"]
|
||||
mod capture_worker;
|
||||
#[path = "playback.rs"]
|
||||
mod playback;
|
||||
#[path = "processing.rs"]
|
||||
mod processing;
|
||||
|
||||
@@ -33,12 +37,17 @@ use buffers::FramePacker;
|
||||
use buffers::MAX_CALLBACK_FRAMES;
|
||||
use buffers::Playback;
|
||||
use buffers::QUEUE_CAPACITY;
|
||||
use playback::PlaybackPort;
|
||||
|
||||
const MAX_CAPTURE_AGE: Duration = Duration::from_secs(/*secs*/ 1);
|
||||
|
||||
pub(super) struct Devices {
|
||||
_input: cpal::Stream,
|
||||
_output: cpal::Stream,
|
||||
output: Option<cpal::Stream>,
|
||||
output_device: cpal::Device,
|
||||
output_config: cpal::SupportedStreamConfig,
|
||||
output_stream_config: cpal::StreamConfig,
|
||||
playback: PlaybackPort,
|
||||
worker: capture_worker::CaptureWorker,
|
||||
}
|
||||
|
||||
@@ -93,6 +102,7 @@ impl Devices {
|
||||
input_config.sample_rate(),
|
||||
output_config.sample_rate(),
|
||||
));
|
||||
let playback = PlaybackPort::new(buffers.clone(), output_config.sample_rate());
|
||||
let processor =
|
||||
processing::Processor::new(input_config.sample_rate(), output_config.sample_rate())
|
||||
.map_err(io::Error::other)?;
|
||||
@@ -113,7 +123,7 @@ impl Devices {
|
||||
buffers.clone()
|
||||
)
|
||||
.map_err(|_| io::Error::other("failed to open microphone"))?;
|
||||
let output = stream!(
|
||||
let output_stream = stream!(
|
||||
output_config.sample_format(),
|
||||
build_output,
|
||||
&output,
|
||||
@@ -121,7 +131,7 @@ impl Devices {
|
||||
buffers.clone()
|
||||
)
|
||||
.map_err(|_| io::Error::other("failed to open speaker"))?;
|
||||
output
|
||||
output_stream
|
||||
.play()
|
||||
.map_err(|_| io::Error::other("failed to start speaker"))?;
|
||||
input
|
||||
@@ -129,7 +139,11 @@ impl Devices {
|
||||
.map_err(|_| io::Error::other("failed to start microphone"))?;
|
||||
Ok(Self {
|
||||
_input: input,
|
||||
_output: output,
|
||||
output: Some(output_stream),
|
||||
output_device: output,
|
||||
output_config,
|
||||
output_stream_config,
|
||||
playback,
|
||||
worker: capture_worker::CaptureWorker {
|
||||
buffers,
|
||||
processor,
|
||||
@@ -142,7 +156,37 @@ impl Devices {
|
||||
&mut self,
|
||||
controls: codex_realtime_webrtc::AudioControls,
|
||||
) -> io::Result<()> {
|
||||
self.worker.set_controls(controls)
|
||||
let buffers = self.worker.buffers.clone();
|
||||
let previous = buffers.speaker.load(Ordering::Acquire);
|
||||
self.worker.set_controls(controls)?;
|
||||
if previous != buffers.speaker.load(Ordering::Acquire) {
|
||||
// Invalidate writers first, then wait off the callback for any in-flight write.
|
||||
let _producer = self
|
||||
.playback
|
||||
.0
|
||||
.producer
|
||||
.lock()
|
||||
.map_err(|_| io::Error::other("speaker writer failed"))?;
|
||||
// Dropping the stream stops its callbacks and discards device-owned audio.
|
||||
drop(self.output.take());
|
||||
while buffers.playback.pop().is_some() {}
|
||||
while buffers.rendered.pop().is_some() {}
|
||||
buffers.queued.store(/*val*/ 0, Ordering::Release);
|
||||
buffers.last_dac_ns.store(/*val*/ 0, Ordering::Release);
|
||||
let output = stream!(
|
||||
self.output_config.sample_format(),
|
||||
build_output,
|
||||
&self.output_device,
|
||||
&self.output_stream_config,
|
||||
buffers
|
||||
)
|
||||
.map_err(|_| io::Error::other("failed to reset speaker"))?;
|
||||
output
|
||||
.play()
|
||||
.map_err(|_| io::Error::other("failed to restart speaker"))?;
|
||||
self.output = Some(output);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) async fn service(
|
||||
@@ -153,6 +197,14 @@ impl Devices {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Devices {
|
||||
fn drop(&mut self) {
|
||||
self.worker.buffers.failed.store(true, Ordering::Release);
|
||||
let _producer = self.playback.0.producer.lock().ok();
|
||||
drop(self.output.take());
|
||||
}
|
||||
}
|
||||
|
||||
fn bounded_stream_config(
|
||||
supported: &cpal::SupportedStreamConfig,
|
||||
) -> io::Result<cpal::StreamConfig> {
|
||||
@@ -322,6 +374,7 @@ fn render_output<T>(
|
||||
data.fill(T::from_sample(0.0));
|
||||
return;
|
||||
}
|
||||
let mut delivered_until = None;
|
||||
for (index, chunk) in data.chunks_mut(BLOCK * channels).enumerate() {
|
||||
let mut reference = Frame {
|
||||
samples: [0.0; BLOCK],
|
||||
@@ -329,8 +382,18 @@ fn render_output<T>(
|
||||
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));
|
||||
for (offset, (frame, sample)) in chunk
|
||||
.chunks_mut(channels)
|
||||
.zip(&mut reference.samples)
|
||||
.enumerate()
|
||||
{
|
||||
let next = output.playback.next(buffers);
|
||||
if next.is_some() {
|
||||
delivered_until = Some(
|
||||
start + Duration::from_secs_f64((index * BLOCK + offset + 1) as f64 / rate),
|
||||
);
|
||||
}
|
||||
let rendered = T::from_sample(next.unwrap_or(0.0));
|
||||
frame.fill(rendered);
|
||||
*sample = f32::from_sample(rendered);
|
||||
record_peak(&buffers.speaker_peak, *sample);
|
||||
@@ -339,6 +402,14 @@ fn render_output<T>(
|
||||
buffers.failed.store(true, Ordering::Release);
|
||||
}
|
||||
}
|
||||
if let Some(end) = delivered_until {
|
||||
buffers.last_dac_ns.store(
|
||||
end.saturating_duration_since(buffers.clock)
|
||||
.as_nanos()
|
||||
.min(u128::from(u64::MAX)) as u64,
|
||||
Ordering::Release,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user