feat: protocol improvements — live trunking, mini-frames, noise suppression, adaptive jitter

T6 wiring: Trunking in relay hot path
- TrunkedForwarder wraps transport with TrunkBatcher
- run_participant uses 5ms flush timer when trunking enabled
- send_trunk/recv_trunk on QuinnTransport
- --trunking flag on relay config
- 2 new tests: forwarder batches, auto-flush on full

T7 wiring: Mini-frames in encoder/decoder
- MediaPacket::encode_compact/decode_compact with MiniFrameContext
- CallEncoder sends mini-headers for consecutive frames (full every 50th)
- CallDecoder auto-detects full vs mini on receive
- mini_frames_enabled in CallConfig (default true)
- 3 new tests: encode/decode sequence, periodic full, disabled mode

Noise suppression (nnnoiseless/RNNoise)
- NoiseSupressor in wzp-codec: pure Rust ML-based noise removal
- Processes 960-sample frames as two 480-sample halves
- Integrated in CallEncoder before silence detection
- noise_suppression in CallConfig (default true)
- 4 new tests: creation, processing, SNR improvement, passthrough

T1-S4: Adaptive playout delay
- AdaptivePlayoutDelay: EMA-based jitter tracking (NetEq-inspired)
- Computes target_delay from observed inter-arrival jitter
- JitterBuffer::new_adaptive() uses adaptive delay
- adaptive_jitter in CallConfig (default true)
- 5 new tests: stable, jitter increase, recovery, clamping, estimate

272 tests passing across all crates.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Siavash Sameni
2026-03-28 14:24:53 +04:00
parent 34cd1017c1
commit 0dc381e948
11 changed files with 1547 additions and 11 deletions

View File

@@ -2,6 +2,97 @@ use std::collections::BTreeMap;
use crate::packet::MediaPacket;
// ---------------------------------------------------------------------------
// Adaptive playout delay (NetEq-inspired)
// ---------------------------------------------------------------------------
/// Adaptive playout delay estimator based on observed inter-arrival jitter.
///
/// Inspired by WebRTC NetEq and IAX2 adaptive jitter buffering. Tracks an
/// exponential moving average (EMA) of inter-packet arrival jitter and
/// converts it to a target buffer depth in packets.
pub struct AdaptivePlayoutDelay {
/// Current target delay in packets (equivalent to target_depth).
target_delay: usize,
/// Minimum allowed delay.
min_delay: usize,
/// Maximum allowed delay.
max_delay: usize,
/// Exponential moving average of inter-packet arrival jitter (ms).
jitter_ema: f64,
/// EMA smoothing factor (0.0-1.0, lower = smoother).
alpha: f64,
/// Last packet arrival timestamp (for computing inter-arrival jitter).
last_arrival_ms: Option<u64>,
/// Last packet expected timestamp.
last_expected_ms: Option<u64>,
}
/// Frame duration in milliseconds (20ms Opus/Codec2 frames).
const FRAME_DURATION_MS: f64 = 20.0;
/// Safety margin added to jitter-derived target (in packets).
const SAFETY_MARGIN_PACKETS: f64 = 2.0;
/// Default EMA smoothing factor.
const DEFAULT_ALPHA: f64 = 0.05;
impl AdaptivePlayoutDelay {
/// Create a new adaptive playout delay estimator.
///
/// - `min_delay`: minimum target delay in packets
/// - `max_delay`: maximum target delay in packets
pub fn new(min_delay: usize, max_delay: usize) -> Self {
Self {
target_delay: min_delay,
min_delay,
max_delay,
jitter_ema: 0.0,
alpha: DEFAULT_ALPHA,
last_arrival_ms: None,
last_expected_ms: None,
}
}
/// Update with a new packet arrival. Returns the new target delay.
///
/// - `arrival_ms`: when the packet actually arrived (wall clock)
/// - `expected_ms`: when it should have arrived (based on sequence * 20ms)
pub fn update(&mut self, arrival_ms: u64, expected_ms: u64) -> usize {
if let (Some(last_arrival), Some(last_expected)) =
(self.last_arrival_ms, self.last_expected_ms)
{
let actual_delta = arrival_ms as f64 - last_arrival as f64;
let expected_delta = expected_ms as f64 - last_expected as f64;
let jitter = (actual_delta - expected_delta).abs();
// Update EMA
self.jitter_ema = self.alpha * jitter + (1.0 - self.alpha) * self.jitter_ema;
// Convert jitter estimate to target delay in packets
let raw_target = (self.jitter_ema / FRAME_DURATION_MS).ceil() + SAFETY_MARGIN_PACKETS;
self.target_delay =
(raw_target as usize).clamp(self.min_delay, self.max_delay);
}
self.last_arrival_ms = Some(arrival_ms);
self.last_expected_ms = Some(expected_ms);
self.target_delay
}
/// Get current target delay in packets.
pub fn target_delay(&self) -> usize {
self.target_delay
}
/// Get current jitter estimate in ms.
pub fn jitter_estimate_ms(&self) -> f64 {
self.jitter_ema
}
}
// ---------------------------------------------------------------------------
// Jitter buffer
// ---------------------------------------------------------------------------
/// Adaptive jitter buffer that reorders packets by sequence number.
///
/// Designed for the lossy relay link with up to 5 seconds of buffering depth.
@@ -21,6 +112,8 @@ pub struct JitterBuffer {
initialized: bool,
/// Statistics.
stats: JitterStats,
/// Optional adaptive playout delay estimator.
adaptive: Option<AdaptivePlayoutDelay>,
}
/// Jitter buffer statistics.
@@ -68,6 +161,27 @@ impl JitterBuffer {
min_depth,
initialized: false,
stats: JitterStats::default(),
adaptive: None,
}
}
/// Create a jitter buffer with adaptive playout delay.
///
/// The target depth will be automatically adjusted based on observed
/// inter-arrival jitter (NetEq-inspired algorithm).
///
/// - `min_delay`: minimum target delay in packets
/// - `max_delay`: maximum target delay in packets (also used as max_depth)
pub fn new_adaptive(min_delay: usize, max_delay: usize) -> Self {
Self {
buffer: BTreeMap::new(),
next_playout_seq: 0,
max_depth: max_delay,
target_depth: min_delay,
min_depth: min_delay,
initialized: false,
stats: JitterStats::default(),
adaptive: Some(AdaptivePlayoutDelay::new(min_delay, max_delay)),
}
}
@@ -107,6 +221,28 @@ impl JitterBuffer {
self.next_playout_seq = seq;
}
// Update adaptive playout delay if enabled.
// Use the packet's timestamp as expected_ms and compute a simple wall-clock
// proxy from the header timestamp (arrival_ms is approximated as timestamp
// + observed jitter, but since we don't have real wall-clock here we use
// the receive order with the header timestamp as the expected baseline).
if let Some(ref mut adaptive) = self.adaptive {
// expected_ms derived from sequence-implied timing: seq * frame_duration
let expected_ms = packet.header.timestamp as u64;
// For arrival_ms, use the actual receive timestamp. In the absence of
// a wall-clock parameter, we use std::time for a monotonic approximation.
// However, to keep the API simple, we compute arrival from the packet
// stats: the Nth received packet "arrives" at N * frame_duration as a
// baseline, and real network jitter shows in the deviation.
// NOTE: In production, the caller should pass real wall-clock time.
// For now, we use the header timestamp as-is (callers with adaptive
// mode should feed arrival time via push_with_arrival).
let arrival_ms = expected_ms; // no-op for basic push; use push_with_arrival
adaptive.update(arrival_ms, expected_ms);
self.target_depth = adaptive.target_delay();
self.min_depth = self.min_depth.min(self.target_depth);
}
self.buffer.insert(seq, packet);
// Evict oldest if over max depth
@@ -193,6 +329,68 @@ impl JitterBuffer {
};
}
/// Push a received packet with an explicit wall-clock arrival time.
///
/// This is the preferred entry point when adaptive playout delay is enabled,
/// since the estimator needs real arrival timestamps.
pub fn push_with_arrival(&mut self, packet: MediaPacket, arrival_ms: u64) {
let expected_ms = packet.header.timestamp as u64;
let seq = packet.header.seq;
self.stats.packets_received += 1;
if !self.initialized {
self.next_playout_seq = seq;
self.initialized = true;
}
// Check for duplicates
if self.buffer.contains_key(&seq) {
self.stats.packets_duplicate += 1;
return;
}
// Check if packet is too old (already played out)
if self.stats.packets_played > 0 && seq_before(seq, self.next_playout_seq) {
self.stats.packets_late += 1;
return;
}
// If we haven't started playout yet, adjust next_playout_seq to earliest known
if self.stats.packets_played == 0 && seq_before(seq, self.next_playout_seq) {
self.next_playout_seq = seq;
}
// Update adaptive playout delay if enabled.
if let Some(ref mut adaptive) = self.adaptive {
adaptive.update(arrival_ms, expected_ms);
self.target_depth = adaptive.target_delay();
}
self.buffer.insert(seq, packet);
// Evict oldest if over max depth
while self.buffer.len() > self.max_depth {
if let Some((&oldest_seq, _)) = self.buffer.first_key_value() {
self.buffer.remove(&oldest_seq);
self.stats.overruns += 1;
if seq_before(self.next_playout_seq, oldest_seq.wrapping_add(1)) {
self.next_playout_seq = oldest_seq.wrapping_add(1);
self.stats.packets_lost += 1;
}
}
}
self.stats.current_depth = self.buffer.len();
if self.stats.current_depth > self.stats.max_depth_seen {
self.stats.max_depth_seen = self.stats.current_depth;
}
}
/// Get a reference to the adaptive playout delay estimator, if enabled.
pub fn adaptive_delay(&self) -> Option<&AdaptivePlayoutDelay> {
self.adaptive.as_ref()
}
/// Adjust target depth based on observed jitter.
pub fn set_target_depth(&mut self, depth: usize) {
self.target_depth = depth.min(self.max_depth);
@@ -334,4 +532,192 @@ mod tests {
other => panic!("expected packet 0, got {:?}", other),
}
}
// ---------------------------------------------------------------
// AdaptivePlayoutDelay tests
// ---------------------------------------------------------------
#[test]
fn adaptive_delay_stable() {
// Feed packets with consistent 20ms spacing — target should stay at minimum.
let mut apd = AdaptivePlayoutDelay::new(3, 50);
for i in 0u64..200 {
let arrival_ms = i * 20;
let expected_ms = i * 20;
apd.update(arrival_ms, expected_ms);
}
// With zero jitter, target should be min_delay (ceil(0/20) + 2 = 2,
// clamped to min_delay=3).
assert_eq!(apd.target_delay(), 3);
assert!(
apd.jitter_estimate_ms() < 1.0,
"jitter estimate should be near zero, got {}",
apd.jitter_estimate_ms()
);
}
#[test]
fn adaptive_delay_increases_on_jitter() {
// Feed packets with variable spacing (±10ms jitter).
let mut apd = AdaptivePlayoutDelay::new(3, 50);
// Alternate: arrive 10ms early / 10ms late
for i in 0u64..200 {
let expected_ms = i * 20;
let jitter_offset: i64 = if i % 2 == 0 { 10 } else { -10 };
let arrival_ms = (expected_ms as i64 + jitter_offset).max(0) as u64;
apd.update(arrival_ms, expected_ms);
}
// Inter-arrival jitter should be ~20ms (swing of 10 to -10 = delta 20).
// target = ceil(~20/20) + 2 = 3, but EMA converges near 20 so target >= 3.
assert!(
apd.target_delay() >= 3,
"target should increase with jitter, got {}",
apd.target_delay()
);
assert!(
apd.jitter_estimate_ms() > 5.0,
"jitter estimate should be significant, got {}",
apd.jitter_estimate_ms()
);
}
#[test]
fn adaptive_delay_decreases_on_recovery() {
let mut apd = AdaptivePlayoutDelay::new(3, 50);
// Phase 1: high jitter (±30ms)
for i in 0u64..200 {
let expected_ms = i * 20;
let offset: i64 = if i % 2 == 0 { 30 } else { -30 };
let arrival_ms = (expected_ms as i64 + offset).max(0) as u64;
apd.update(arrival_ms, expected_ms);
}
let high_target = apd.target_delay();
let high_jitter = apd.jitter_estimate_ms();
// Phase 2: stable (no jitter) — target should decrease via EMA decay
for i in 200u64..600 {
let t = i * 20;
apd.update(t, t);
}
let low_target = apd.target_delay();
let low_jitter = apd.jitter_estimate_ms();
assert!(
low_target <= high_target,
"target should decrease after recovery: {} -> {}",
high_target,
low_target
);
assert!(
low_jitter < high_jitter,
"jitter estimate should decrease: {} -> {}",
high_jitter,
low_jitter
);
}
#[test]
fn adaptive_delay_clamped() {
let mut apd = AdaptivePlayoutDelay::new(3, 10);
// Extreme jitter: packets arrive with huge variance
for i in 0u64..500 {
let expected_ms = i * 20;
let offset: i64 = if i % 2 == 0 { 500 } else { -500 };
let arrival_ms = (expected_ms as i64 + offset).max(0) as u64;
apd.update(arrival_ms, expected_ms);
}
assert!(
apd.target_delay() <= 10,
"target should not exceed max_delay=10, got {}",
apd.target_delay()
);
assert!(
apd.target_delay() >= 3,
"target should not go below min_delay=3, got {}",
apd.target_delay()
);
}
#[test]
fn adaptive_jitter_estimate() {
let mut apd = AdaptivePlayoutDelay::new(3, 50);
// Initial jitter estimate should be zero
assert_eq!(apd.jitter_estimate_ms(), 0.0);
// After one packet, still zero (no delta yet)
apd.update(0, 0);
assert_eq!(apd.jitter_estimate_ms(), 0.0);
// Second packet with 5ms jitter
apd.update(25, 20); // arrived 5ms late
assert!(
apd.jitter_estimate_ms() > 0.0,
"jitter estimate should be positive after jittery packet"
);
assert!(
apd.jitter_estimate_ms() <= 5.0,
"first jitter sample of 5ms with alpha=0.05 should not exceed 5ms, got {}",
apd.jitter_estimate_ms()
);
// Feed many packets with ~15ms jitter — EMA should converge
for i in 2u64..500 {
let expected_ms = i * 20;
let arrival_ms = expected_ms + 15; // consistently 15ms late
apd.update(arrival_ms, expected_ms);
}
// Steady-state: inter-arrival jitter = |35 - 20| = 0 actually,
// because if every packet is 15ms late, delta_actual = 35-35 = 20,
// same as expected. So jitter should converge toward 0.
// Let's use variable jitter instead for a better test.
let mut apd2 = AdaptivePlayoutDelay::new(3, 50);
for i in 0u64..500 {
let expected_ms = i * 20;
// Alternate 0ms and 15ms late
let extra = if i % 2 == 0 { 0 } else { 15 };
let arrival_ms = expected_ms + extra;
apd2.update(arrival_ms, expected_ms);
}
let est = apd2.jitter_estimate_ms();
assert!(
est > 5.0 && est < 20.0,
"jitter estimate should converge near 15ms with alternating 0/15ms offsets, got {}",
est
);
}
// ---------------------------------------------------------------
// JitterBuffer with adaptive mode tests
// ---------------------------------------------------------------
#[test]
fn jitter_buffer_adaptive_constructor() {
let jb = JitterBuffer::new_adaptive(5, 250);
assert!(jb.adaptive_delay().is_some());
assert_eq!(jb.adaptive_delay().unwrap().target_delay(), 5);
}
#[test]
fn jitter_buffer_adaptive_push_with_arrival() {
let mut jb = JitterBuffer::new_adaptive(3, 50);
// Push packets with consistent timing
for i in 0u16..20 {
let pkt = make_packet(i);
let arrival_ms = i as u64 * 20;
jb.push_with_arrival(pkt, arrival_ms);
}
// With zero jitter, target should stay at min
let ad = jb.adaptive_delay().unwrap();
assert_eq!(ad.target_delay(), 3);
}
}