feat(p2p): adaptive quality on direct calls (#23)
P2P calls now adapt codec quality based on observed network conditions, matching what relay calls already had. Three-layer implementation: - QualityReport::from_path_stats(): construct reports from local quinn stats (loss%, RTT, jitter) without needing relay-generated reports - CallEncoder.pending_quality_report: one-shot attachment to next source packet (consumed on encode, not repeated) - Engine send tasks: generate quality report every 50 frames (~1s) from quinn_path_stats() and attach via set_pending_quality_report() - Engine recv tasks: self-observe from own QUIC path stats every 50 packets, feed to AdaptiveQualityController for P2P adaptation (works even if peer isn't sending quality reports yet) Both relay and P2P calls now have adaptive quality. On relay calls, both peer-sent reports AND local observations feed the controller. Hysteresis (3 consecutive bad reports to downgrade) prevents thrashing. 372 tests passing (+4 new: from_path_stats encoding, clamping, zero values, encoder quality report attachment). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -234,6 +234,8 @@ pub struct CallEncoder {
|
||||
mini_frames_enabled: bool,
|
||||
/// Frames encoded since the last full header was emitted.
|
||||
frames_since_full: u32,
|
||||
/// Pending quality report to attach to the next source packet.
|
||||
pending_quality_report: Option<QualityReport>,
|
||||
}
|
||||
|
||||
impl CallEncoder {
|
||||
@@ -264,6 +266,7 @@ impl CallEncoder {
|
||||
mini_context: MiniFrameContext::default(),
|
||||
mini_frames_enabled: config.mini_frames_enabled,
|
||||
frames_since_full: 0,
|
||||
pending_quality_report: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -367,7 +370,7 @@ impl CallEncoder {
|
||||
version: 0,
|
||||
is_repair: false,
|
||||
codec_id: self.profile.codec,
|
||||
has_quality_report: false,
|
||||
has_quality_report: self.pending_quality_report.is_some(),
|
||||
fec_ratio_encoded,
|
||||
seq: self.seq,
|
||||
timestamp: self.timestamp_ms,
|
||||
@@ -377,7 +380,7 @@ impl CallEncoder {
|
||||
csrc_count: 0,
|
||||
},
|
||||
payload: Bytes::from(encoded.clone()),
|
||||
quality_report: None,
|
||||
quality_report: self.pending_quality_report.take(),
|
||||
};
|
||||
|
||||
self.seq = self.seq.wrapping_add(1);
|
||||
@@ -454,6 +457,13 @@ impl CallEncoder {
|
||||
self.audio_enc.set_expected_loss(tuning.expected_loss_pct);
|
||||
}
|
||||
|
||||
/// Queue a quality report for attachment to the next source packet.
|
||||
/// Used by the send task to embed locally-observed path quality so
|
||||
/// the peer can drive adaptive quality switching.
|
||||
pub fn set_pending_quality_report(&mut self, report: QualityReport) {
|
||||
self.pending_quality_report = Some(report);
|
||||
}
|
||||
|
||||
/// Enable or disable acoustic echo cancellation.
|
||||
pub fn set_aec_enabled(&mut self, enabled: bool) {
|
||||
self.aec.set_enabled(enabled);
|
||||
@@ -1578,4 +1588,28 @@ mod tests {
|
||||
let packets = enc.encode_frame(&pcm).unwrap();
|
||||
assert!(!packets.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoder_attaches_quality_report() {
|
||||
let mut enc = CallEncoder::new(&CallConfig {
|
||||
profile: QualityProfile::GOOD,
|
||||
suppression_enabled: false,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
// Set a quality report
|
||||
enc.set_pending_quality_report(QualityReport::from_path_stats(5.0, 80, 10));
|
||||
|
||||
// Encode a frame — should have quality_report attached
|
||||
let pcm = voice_frame_20ms(0);
|
||||
let packets = enc.encode_frame(&pcm).unwrap();
|
||||
assert!(!packets.is_empty());
|
||||
assert!(packets[0].header.has_quality_report, "first packet should have quality report");
|
||||
assert!(packets[0].quality_report.is_some());
|
||||
|
||||
// Next frame should NOT have quality_report (it was consumed)
|
||||
let packets2 = enc.encode_frame(&voice_frame_20ms(960)).unwrap();
|
||||
assert!(!packets2[0].header.has_quality_report, "second packet should not have quality report");
|
||||
assert!(packets2[0].quality_report.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,6 +180,19 @@ impl QualityReport {
|
||||
self.rtt_4ms as u16 * 4
|
||||
}
|
||||
|
||||
/// Construct a QualityReport from locally-observed path statistics.
|
||||
///
|
||||
/// Used by the send task to embed quality data in outgoing packets so
|
||||
/// the peer's recv task (or relay) can drive adaptive quality switching.
|
||||
pub fn from_path_stats(loss_pct: f32, rtt_ms: u32, jitter_ms: u32) -> Self {
|
||||
Self {
|
||||
loss_pct: (loss_pct / 100.0 * 255.0).clamp(0.0, 255.0) as u8,
|
||||
rtt_4ms: (rtt_ms / 4).min(255) as u8,
|
||||
jitter_ms: jitter_ms.min(255) as u8,
|
||||
bitrate_cap_kbps: 200,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_to(&self, buf: &mut impl BufMut) {
|
||||
buf.put_u8(self.loss_pct);
|
||||
buf.put_u8(self.rtt_4ms);
|
||||
@@ -966,6 +979,32 @@ pub enum HangupReason {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn quality_report_from_path_stats_basic() {
|
||||
let qr = QualityReport::from_path_stats(10.0, 100, 20);
|
||||
// 10.0 / 100.0 * 255.0 = 25.5 → truncated to 25
|
||||
assert_eq!(qr.loss_pct, 25);
|
||||
assert_eq!(qr.rtt_4ms, 25); // 100 / 4 = 25
|
||||
assert_eq!(qr.jitter_ms, 20);
|
||||
assert_eq!(qr.bitrate_cap_kbps, 200);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quality_report_from_path_stats_zero() {
|
||||
let qr = QualityReport::from_path_stats(0.0, 0, 0);
|
||||
assert_eq!(qr.loss_pct, 0);
|
||||
assert_eq!(qr.rtt_4ms, 0);
|
||||
assert_eq!(qr.jitter_ms, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quality_report_from_path_stats_clamps_high() {
|
||||
let qr = QualityReport::from_path_stats(100.0, 2000, 300);
|
||||
assert_eq!(qr.loss_pct, 255);
|
||||
assert_eq!(qr.rtt_4ms, 255); // 2000/4=500, clamped to 255
|
||||
assert_eq!(qr.jitter_ms, 255);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_roundtrip() {
|
||||
let header = MediaHeader {
|
||||
|
||||
Reference in New Issue
Block a user