Files
VoelinClient/crates/voelin-audio/tests/drift.rs
T
faumarayandClaude Sonnet 5.5 df7be67d4c Move voice packet types into voelin-audio
voelin-audio no longer depends on tsclientlib or tsproto-packets. Its
boundary is now plain types in packet.rs: TalkerId, VoiceCodec, VoicePacket
(talker, seq, codec, whisper, payload) and EncodedVoice (codec, payload; an
empty payload ends the transmission). The jitter buffer moves from
tsclientlib::audio into jitter.rs and reads VoicePacket fields; the encoder
returns EncodedVoice and the mixer is keyed by TalkerId.

In voelin-core, voice.rs is the only place that wraps EncodedVoice into a
TeamSpeak audio packet and reads one into a VoicePacket (codecs other than
Opus are dropped there, where the jitter buffer rejected them before);
audio.rs and session.rs work on the new types. voelinctl converts at its
own edge. Behaviour is unchanged: the one-hour drift simulation gives the
same latencies as before.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 13:06:46 +03:00

128 lines
4.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! A one-hour call with a sender whose clock runs ±200 ppm off ours: the
//! jitter buffer must keep the latency bounded instead of drifting by the
//! 720 ms the clocks disagree over an hour.
//!
//! The sender transmits continuously (the worst case: talk spurts would reset
//! the queue) over a network with 20–60 ms of jitter, reordering and 0.5 %
//! loss. Every 30 s it sends a loud marker frame instead of the quiet one;
//! the latency is the time from generating a marker to it leaving the mixer.
use voelin_audio::pcm::{FRAME_SAMPLES, SAMPLE_RATE, energy, sine, white_noise};
use voelin_audio::{Mixer, TalkerId, VoiceCodec, VoiceEncoder, VoicePacket};
/// The receiver pulls 10 ms at a time, like a playback loop.
const PULL: usize = SAMPLE_RATE as usize / 100;
const MARKER_EVERY: u64 = 1500;
struct Rng(u64);
impl Rng {
fn next(&mut self) -> f64 {
self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
(self.0 >> 11) as f64 / (1u64 << 53) as f64
}
}
/// Returns the latency of each marker in ms, in order.
fn simulate(ppm: f64, hours: f64) -> Vec<f64> {
let mut encoder = VoiceEncoder::new(VoiceCodec::Voice).unwrap();
// A quiet frame (-65 dBFS noise) and a loud marker, encoded once.
let quiet = white_noise(FRAME_SAMPLES, 0.001, 1);
for _ in 0..5 {
encoder.encode_to_bytes(&quiet).unwrap();
}
let quiet = encoder.encode_to_bytes(&quiet).unwrap().to_vec();
let marker = encoder.encode_to_bytes(&sine(1000.0, 0.02, 0.5)).unwrap().to_vec();
let mut rng = Rng(ppm.to_bits());
let mut mixer = Mixer::new();
let talker = TalkerId(5);
let total = (hours * 3600.0 * SAMPLE_RATE as f64) as u64;
// Sender frame n is made at n * 20 ms of the sender's clock.
let frame_time = |n: u64| n as f64 * FRAME_SAMPLES as f64 / (1.0 + ppm * 1e-6);
// (arrival time, packet id), kept sorted by arrival.
let mut in_flight: Vec<(f64, u64)> = Vec::new();
let mut next_frame = 0u64;
let mut markers_sent = Vec::new();
let mut latencies = Vec::new();
let mut out = vec![0.0f32; PULL * 2];
let mut now = 0u64;
while now < total {
// Everything the sender produced up to now goes on the wire.
while frame_time(next_frame) <= now as f64 {
let is_marker = next_frame % MARKER_EVERY == MARKER_EVERY / 2;
if is_marker {
markers_sent.push(frame_time(next_frame));
}
// Markers are never lost, so each one can be matched.
if is_marker || rng.next() >= 0.005 {
let jitter = (0.02 + 0.04 * rng.next()) * SAMPLE_RATE as f64;
in_flight.push((frame_time(next_frame) + jitter, next_frame));
}
next_frame += 1;
}
in_flight.sort_by(|a, b| a.0.total_cmp(&b.0));
let arrived = in_flight.partition_point(|&(t, _)| t <= now as f64);
for (_, n) in in_flight.drain(..arrived) {
let data = if n % MARKER_EVERY == MARKER_EVERY / 2 { &marker } else { &quiet };
let packet = VoicePacket {
talker,
seq: n as u16,
codec: VoiceCodec::Voice,
whisper: false,
payload: data.clone(),
};
// Late packets are dropped by the jitter buffer, as on a real network.
let _ = mixer.handle_packet(packet);
}
out.fill(0.0);
mixer.fill_buffer(&mut out);
let left: Vec<f32> = out.iter().step_by(2).copied().collect();
if energy(&left) > 1e-3 {
// The latest marker made before now; they are 30 s apart.
let sent = markers_sent.iter().rev().find(|&&t| t <= now as f64).copied();
if let Some(sent) = sent {
let latency = (now as f64 - sent) * 1000.0 / SAMPLE_RATE as f64;
// One detection per marker (it spans two 10 ms pulls).
if latencies.len() < markers_sent.len() && latency < 5000.0 {
let index = markers_sent.iter().position(|&t| t == sent).unwrap();
if index == latencies.len() {
latencies.push(latency);
}
}
}
}
now += PULL as u64;
}
assert_eq!(latencies.len(), markers_sent.len(), "every marker must come out (ppm {ppm})");
latencies
}
fn check(ppm: f64) {
let latencies = simulate(ppm, 1.0);
assert_eq!(latencies.len(), 120);
let max = latencies.iter().copied().fold(0.0, f64::max);
let min = latencies.iter().copied().fold(f64::MAX, f64::min);
let first: f64 = latencies[..20].iter().sum::<f64>() / 20.0;
let last: f64 = latencies[latencies.len() - 20..].iter().sum::<f64>() / 20.0;
println!(
"{ppm:+} ppm: latency min {min:.0} ms, max {max:.0} ms, first 10 min avg {first:.0} ms, last 10 min avg {last:.0} ms"
);
// The network alone adds 20-60 ms; unchecked drift would add 720 ms.
assert!(min >= 20.0, "latency {min} ms is below the network delay");
assert!(max < 250.0, "latency grew to {max} ms");
assert!((last - first).abs() < 60.0, "latency drifted from {first} ms to {last} ms");
}
#[test]
fn fast_sender_one_hour() {
check(200.0);
}
#[test]
fn slow_sender_one_hour() {
check(-200.0);
}