Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Keep voice RTP timestamps aligned to 20 ms packets (#48824)

Benjamin Carlsson84cc4b3fb524
diff --git a/codex-rs/voice-host/src/audio_track.rs b/codex-rs/voice-host/src/audio_track.rs
index 1525465e7d..05a6f01aec 100644
--- a/codex-rs/voice-host/src/audio_track.rs
+++ b/codex-rs/voice-host/src/audio_track.rs
@@ -1,5 +1,6 @@
//! A single Opus RTP track. Muted capture sends generated silence to keep the peer alive;
-//! elapsed time still advances its clock without inventing packet loss.
+//! elapsed time advances in whole packets so capture jitter and mute transitions preserve
+//! the receiver's 20 ms source grid without inventing packet loss.
use std::sync::Arc;
use std::time::Duration;
@@ -115,10 +116,18 @@ impl AudioTrack {
pub(crate) async fn send(&mut self, frame: EncodedAudio) -> Result<(), &'static str> {
tokio::time::timeout(SEND_TIMEOUT, async {
- let mut gap = self
+ let duration = Duration::from_millis(/*millis*/ 20);
+ let elapsed = self
.end
.map(|end| frame.at.saturating_duration_since(end))
.unwrap_or_default();
+ // RTP counts audio samples, not callback wall-clock jitter. Skipping
+ // fractional packets changes the source grid and causes receivers
+ // that assemble fixed-duration frames to reject subsequent audio.
+ let skipped = Duration::from_millis(
+ (elapsed.as_millis() / duration.as_millis() * duration.as_millis()) as u64,
+ );
+ let mut gap = skipped;
while !gap.is_zero() {
let duration = gap.min(Duration::from_secs(/*secs*/ 3600));
self.track
@@ -126,7 +135,9 @@ impl AudioTrack {
self.ssrc,
/*payload_type*/ OPUS_PAYLOAD_TYPE,
&Sample {
- duration,
+ // write_sample truncates floating-point seconds to RTP ticks.
+ // Bias empty gaps by 1 ns to keep whole packets exact.
+ duration: duration + Duration::from_nanos(/*nanos*/ 1),
..Default::default()
},
&[],
@@ -135,7 +146,6 @@ impl AudioTrack {
.map_err(|_| "failed to advance voice clock")?;
gap -= duration;
}
- let duration = Duration::from_millis(/*millis*/ 20);
self.track
.write_sample(
self.ssrc,
@@ -149,7 +159,7 @@ impl AudioTrack {
)
.await
.map_err(|_| "failed to send voice audio")?;
- self.end = Some(self.end.unwrap_or(frame.at).max(frame.at) + duration);
+ self.end = Some(self.end.unwrap_or(frame.at) + skipped + duration);
Ok(())
})
.await
diff --git a/codex-rs/voice-host/src/audio_track_tests.rs b/codex-rs/voice-host/src/audio_track_tests.rs
index 1be43deac2..affed2f4d7 100644
--- a/codex-rs/voice-host/src/audio_track_tests.rs
+++ b/codex-rs/voice-host/src/audio_track_tests.rs
@@ -10,8 +10,8 @@ async fn timestamp_jitter_does_not_accumulate_but_mute_advances_the_clock() {
let at = start + Duration::from_millis(tick * 20 + tick % 2);
track.send(EncodedAudio { data: vec![], at }).await.unwrap();
}
- assert_eq!(track.end, Some(start + Duration::from_millis(10_001)));
- let at = start + Duration::from_secs(30);
+ assert_eq!(track.end, Some(start + Duration::from_millis(10_000)));
+ let at = start + Duration::from_millis(30_007);
track.send(EncodedAudio { data: vec![], at }).await.unwrap();
- assert_eq!(track.end, Some(at + Duration::from_millis(20)));
+ assert_eq!(track.end, Some(start + Duration::from_millis(30_020)));
}
diff --git a/codex-rs/voice-host/src/capture_worker_tests.rs b/codex-rs/voice-host/src/capture_worker_tests.rs
index 17e739b0bf..0d85153769 100644
--- a/codex-rs/voice-host/src/capture_worker_tests.rs
+++ b/codex-rs/voice-host/src/capture_worker_tests.rs
@@ -267,13 +267,20 @@ async fn capture_reaches_remote_rtp_and_mute_discards_queued_and_partial_audio()
worker.set_controls(muted()).unwrap();
worker.set_controls(unmuted()).unwrap();
let old_generation = worker.buffers.microphone.load(Ordering::Acquire);
- now = Instant::now().max(start + Duration::from_millis(/*millis*/ 100));
+ now = start + Duration::from_millis(before.len() as u64 * 20 + 580);
enqueue(&worker, /*samples*/ 720, old_generation, now);
assert_eq!(worker.service(&mut local.audio, || now).await.unwrap(), 0);
enqueue(&worker, /*samples*/ 2_880, old_generation, now);
worker.set_controls(muted()).unwrap();
assert_eq!(worker.service(&mut local.audio, || now).await.unwrap(), 0);
let silence = received.recv().await.unwrap();
+ assert_eq!(
+ silence
+ .header
+ .timestamp
+ .wrapping_sub(before[0].header.timestamp),
+ before.len() as u32 * 960 + 27_840
+ );
let mut decoder = opus::Decoder::new(/*sample_rate*/ 48_000, opus::Channels::Mono).unwrap();
let mut decoded = [1.0; 960];
assert_eq!(
@@ -316,10 +323,12 @@ async fn capture_reaches_remote_rtp_and_mute_discards_queued_and_partial_audio()
.header
.timestamp
.wrapping_sub(before[0].header.timestamp);
+ assert_eq!(clock_gap % 960, 0);
let expected_gap = (resumed.duration_since(start).as_secs_f64() * 48_000.0) as u32;
- // Allow 1 ms for sample rounding without imposing a wall-clock throughput requirement.
+ // Real gaps advance by whole packets, keeping wall time within one
+ // packet without imposing a wall-clock throughput requirement.
assert!(
- clock_gap.abs_diff(expected_gap) <= 48,
+ clock_gap.abs_diff(expected_gap) <= 960,
"received RTP clock gap: {clock_gap}, expected {expected_gap}"
);
assert_eq!(