fix(webrtc): PLI rate limiting + bitrate cap + VBV units (closes #23)
Root causes (3, discovered via Oracle review + x264 source verification):
A. PLI storm: WebRtcInner.force_keyframe_to_encode is a single bool with
no rate limit. Client PLI storms (5 PLIs in 845ms observed) produced
5 back-to-back IDRs (~1.5MB burst), swamping the network.
B. Bitrate runaway: BWE feedback had no upper bound. Observed bitrate
escalating from 5 Mbps to 9.9 Mbps in 12 seconds, all applied to
encoder. Combined with A, made each IDR grow 4-5x.
C. VBV effectively disabled (NEW finding not in original issue):
x264's vbv-maxrate and vbv-bufsize x264opts expect kbit/s and kbit
(confirmed via x264 source ratecontrol.c:658-661 which multiplies by
1000 at use site). The code passed bps, making VBV interpret 5.5 Mbps
as 5.5 Gbps (clipped to 2 Gbps). Buffer was 173MB instead of 172KB,
so VBV never constrained anything. This is why IDRs could balloon to
336KB after runtime bitrate increases.
Fixes (3, all in this commit per Oracle review):
Fix 0 (avhw.rs): divide bitrate by 1000 when formatting x264opts string.
Updated existing vbv_x264opts_format and vbv_bufsize_is_quarter_of_maxrate
tests to assert correct kbit/s values. Old tests passed but asserted
wrong values - classic 'tests covered the wrong implementation'.
Fix 1 (webrtc.rs): split keyframe trigger into two paths.
- set_need_keyframe() (internal: connect, resolution change) remains
unthrottled but updates last_forced_keyframe_at timestamp.
- request_keyframe_from_viewer() (external PLI) rate-limited to
Duration::from_secs(1), checked against last_forced_keyframe_at.
- Key insight from Oracle: track ALL keyframe production time, not
just PLI time, to prevent 'connect -> immediate PLI -> duplicate IDR'.
Fix 2 (args.rs + state_portal.rs + avhw.rs):
- New --max-bitrate CLI flag, default 8 Mbps.
- Primary clamp in webrtc_thread_loop (policy layer): clamp BWE via
variable shadowing so all downstream code (bitrate_tx, resolution
adaptation) uses clamped value.
- Defensive guardrail in encode_cpu_frame UpdateBitrate handler:
50 Mbps hard ceiling in case future callers bypass policy layer.
- Per Oracle: flat 8 Mbps default, no auto-scaling
(max(8M, 2*initial) would have failed the observed case).
Verification (82.1s session, 34.7 fps avg vs previous 18.2):
PLI storm absorption:
- 15 PLIs received from viewer
- 10 PLIs throttled (67%)
- 6 IDRs produced total (1 connect + 5 honored)
- 3 distinct PLI storms (625ms, 640ms, 858ms duration) all absorbed
VBV constraint working:
- First IDR: 65KB (was 67KB)
- Largest IDR: 169KB (was 336KB)
- All IDRs under VBV buffer bound of 172KB
- No more runaway IDR growth
Bitrate cap working:
- 5 bitrate updates applied (was 8)
- Peak bitrate: 7.4 Mbps (was 9.9 Mbps)
- 52952 BWE readings clamped (>8 Mbps filtered out)
Overall quality:
- Frame rate: 34.7 fps (was 18.2, 1.9x improvement, exceeds 30 fps target)
- Average bitrate: 4451 kb/s (was 5616, lower and more stable)
Out of scope (deferred to future work):
- Runtime VBV reconfiguration on bitrate change (Fix 3): encoder
recreation is expensive, observe whether VBV mismatch becomes a
quality issue after cap is in place
- Asymmetric BWE filter (Fix 4): rise=15%/fall=5% thresholds; nice
tuning but cap is sufficient for now
- Compositor stalls (#15): still causes some stutter at session end
but no longer compounds into latency
Tests:
- cargo test: 91 passed + 3 passed + 0 failed
- vbv_x264opts_format and vbv_bufsize_is_quarter_of_maxrate updated
and passing with new kbit/s assertions
- SAFETY comments preserved verbatim
- cargo build --release: clean, no new warnings
This commit is contained in:
+55
-7
@@ -1,7 +1,7 @@
|
||||
// WebRTC 传输模块 — 使用 str0m (Sans-IO) 将 H.264 编码帧推送到浏览器
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{SocketAddr, TcpListener, UdpSocket};
|
||||
use std::time::Instant;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use str0m::bwe::{Bitrate, BweKind};
|
||||
@@ -11,6 +11,12 @@ use str0m::media::{Frequency, MediaKind, MediaTime, Mid, Pt};
|
||||
use str0m::net::{Protocol, Receive};
|
||||
use str0m::{Candidate, Event, IceConnectionState, Input, Output, Rtc, RtcConfig};
|
||||
|
||||
/// Minimum interval between honored keyframe productions, regardless of source
|
||||
/// (PLI from viewer, connect event, resolution change). Prevents PLI storms
|
||||
/// from causing back-to-back IDRs that swamp the network with multi-hundred-KB
|
||||
/// bursts. See issue #23.
|
||||
const FORCED_KEYFRAME_MIN_INTERVAL: Duration = Duration::from_secs(1);
|
||||
|
||||
// ── 嵌入式 HTML 测试页面 ──────────────────────────────────────────────────
|
||||
|
||||
const HTML_PAGE: &str = r#"<!DOCTYPE html>
|
||||
@@ -201,6 +207,7 @@ struct WebRtcInner {
|
||||
connected: bool,
|
||||
need_keyframe: bool,
|
||||
force_keyframe_to_encode: bool,
|
||||
last_forced_keyframe_at: Option<Instant>,
|
||||
current_bwe_estimate: Option<Bitrate>,
|
||||
rtp_clock: u32,
|
||||
buf: Vec<u8>,
|
||||
@@ -351,10 +358,23 @@ impl WebRtcState {
|
||||
.and_then(|inner| inner.current_bwe_estimate.map(|b| b.as_u64()))
|
||||
}
|
||||
|
||||
/// Internal keyframe request (connect, resolution change). Always honored,
|
||||
/// but updates last_forced_keyframe_at so a subsequent viewer PLI in the next
|
||||
/// second is throttled.
|
||||
pub fn set_need_keyframe(&mut self) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.need_keyframe = true;
|
||||
inner.force_keyframe_to_encode = true;
|
||||
inner.set_need_keyframe();
|
||||
}
|
||||
}
|
||||
|
||||
/// External keyframe request from viewer (PLI/FIR via str0m
|
||||
/// `Event::KeyframeRequest`). Rate-limited to FORCED_KEYFRAME_MIN_INTERVAL
|
||||
/// to prevent PLI storms from swamping the network with IDR bursts.
|
||||
/// See issue #23.
|
||||
#[allow(dead_code)]
|
||||
pub fn request_keyframe_from_viewer(&mut self) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.request_keyframe_from_viewer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -442,6 +462,7 @@ impl WebRtcInner {
|
||||
connected: false,
|
||||
need_keyframe: false,
|
||||
force_keyframe_to_encode: false,
|
||||
last_forced_keyframe_at: None,
|
||||
current_bwe_estimate: None,
|
||||
rtp_clock: 0,
|
||||
buf: vec![0u8; 65535],
|
||||
@@ -494,6 +515,35 @@ impl WebRtcInner {
|
||||
}
|
||||
}
|
||||
|
||||
/// Unthrottled keyframe trigger. Always sets the keyframe flags and refreshes
|
||||
/// `last_forced_keyframe_at` so a follow-up viewer PLI within the next
|
||||
/// `FORCED_KEYFRAME_MIN_INTERVAL` is dropped.
|
||||
fn set_need_keyframe(&mut self) {
|
||||
self.need_keyframe = true;
|
||||
self.force_keyframe_to_encode = true;
|
||||
self.last_forced_keyframe_at = Some(Instant::now());
|
||||
}
|
||||
|
||||
/// Throttled keyframe trigger used for viewer-originated PLI/FIR requests.
|
||||
/// Honored only if enough time has elapsed since the last forced keyframe.
|
||||
fn request_keyframe_from_viewer(&mut self) {
|
||||
let now = Instant::now();
|
||||
let should_honor = self
|
||||
.last_forced_keyframe_at
|
||||
.map_or(true, |last| now.duration_since(last) >= FORCED_KEYFRAME_MIN_INTERVAL);
|
||||
if should_honor {
|
||||
self.last_forced_keyframe_at = Some(now);
|
||||
self.need_keyframe = true;
|
||||
self.force_keyframe_to_encode = true;
|
||||
} else {
|
||||
tracing::warn!(
|
||||
"PLI throttled (last forced keyframe {:?} ago, min interval {:?})",
|
||||
self.last_forced_keyframe_at.map(|t| now.duration_since(t)),
|
||||
FORCED_KEYFRAME_MIN_INTERVAL
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_rtc(&mut self) -> Result<bool> {
|
||||
loop {
|
||||
match self.rtc.poll_output() {
|
||||
@@ -516,8 +566,7 @@ impl WebRtcInner {
|
||||
Event::Connected => {
|
||||
tracing::info!("WebRTC connected!");
|
||||
self.connected = true;
|
||||
self.need_keyframe = true;
|
||||
self.force_keyframe_to_encode = true;
|
||||
self.set_need_keyframe();
|
||||
self.discover_video_params();
|
||||
}
|
||||
Event::IceConnectionStateChange(IceConnectionState::Disconnected) => {
|
||||
@@ -539,8 +588,7 @@ impl WebRtcInner {
|
||||
}
|
||||
Event::KeyframeRequest(_) => {
|
||||
tracing::info!("received keyframe request from viewer");
|
||||
self.need_keyframe = true;
|
||||
self.force_keyframe_to_encode = true;
|
||||
self.request_keyframe_from_viewer();
|
||||
}
|
||||
Event::EgressBitrateEstimate(est) => {
|
||||
let bitrate = match est {
|
||||
|
||||
Reference in New Issue
Block a user