feat(p2p): adaptive quality on direct calls (#23)
Some checks failed
Mirror to GitHub / mirror (push) Failing after 27s
Build Release Binaries / build-amd64 (push) Failing after 3m37s

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:
Siavash Sameni
2026-04-13 16:14:06 +04:00
parent 81b5522942
commit 1e82811cc1
3 changed files with 157 additions and 2 deletions

View File

@@ -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 {