跪拜 Guibai
← All articles
Frontend

WebRTC Latency Is Not One Number: Six Observation Points From ICE to Jitter Buffer

By niceyayale ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A one-number view of WebRTC latency sends a debugging team after the wrong hop, while a 1.4 second A/V gap actually lives in the jitter buffer. The six-threshold protocol and 30-round sampling routine put the cost in terms a developer can fix: buffer size, relay selection, bitrate, CPU.

Summary

WebRTC latency is the sum of four stages with different measurement hooks: capture plus encode (30-80 ms), network transport (20-150 ms), jitter buffering (20-200 ms), and decode plus render (10-50 ms). A real incident from a 500-person live session placed encoding, bandwidth, and servers inside normal range while the audience saw a stable 1.4 second A/V gap. The cause was a receiver-side jitter buffer: 0.3% packet loss but 68 ms inter-arrival jitter pushed the buffer past 240 ms, and after two-hop relay the lag locked in.

The debugging protocol calls for one getStats sample per second for 30 consecutive rounds. Three fields deserve careful reading: jitter is RTP inter-arrival variance per RFC 3550, not latency; the actual buffering cost comes from jitterBufferDelay / jitterBufferEmittedCount; and packetsLost is cumulative, so a retransmit does not lower it. Thresholds map symptoms to causes: RTT above 150 ms implicates a TURN relay or physical distance; loss above 2% separates Wi-Fi airloss from uplink congestion using transport bytesSent growth; jitter above 30 ms for five seconds flags cross-ISP or multi-hop paths; a jitterBufferDelay average above 200 ms means the receiver is exchanging smoothness for latency; freezeCount increases point at decode CPU; and totalEncodeTime above the frame interval means the sender needs lower bitrate, not more bandwidth.

Reproduction on the link uses Linux tc netem to inject 20 ms normal-distribution jitter and 1% loss on loopback, Wireshark RTP analysis for sequence gaps and arrival times, and chrome://webrtc-internals to record before/after series. To make comparisons meaningful, collection keeps only the six named fields and stamps region, network type, and version; dumping the whole getStats object destroys comparability because field sets change between versions. The resolution order is then straightforward: confirm each stage's upper bound, adjust the corresponding control, and turn the six observations into a pre-release dashboard.

Takeaways
— End-to-end WebRTC latency splits into four segments: capture + encode (30-80 ms), network transport (20-150 ms), jitter buffering (20-200 ms), decode + render (10-50 ms).
— Sample RTCPeerConnection.getStats() once per second for 30 rounds; a single sample is only transient noise and cannot separate sporadic from sustained degradation.
— r.jitter is RTP-layer inter-arrival variance in seconds per RFC 3550, not user-perceived latency.
— Compute average jitter buffer delay as jitterBufferDelay / jitterBufferEmittedCount; this ratio tracks the actual time packets spent buffered.
— packetsLost is cumulative and does not decrease after retransmission; read it together with packetsReceived.
— Rule-of-thumb thresholds: currentRoundTripTime > 150 ms; packet loss > 2%; jitter > 30 ms for over 5 seconds; jitterBufferDelay average > 200 ms; freezeCount/pauseCount increasing; totalEncodeTime above the frame interval.
— On Linux, inject jitter and loss with tc netem: `sudo tc qdisc add dev lo root netem delay 20ms 20ms distribution normal loss 1%`; clean up with `sudo tc qdisc del dev lo root`.
— Wireshark RTP analysis and chrome://webrtc-internals expose sequence gaps and per-round stats for before/after comparison.
— Collection cadence: daily inspection 1 second * 30 rounds with 24-hour retention; user report reproduction 250 ms * 120 rounds; version comparison 10 minutes before and after release.
— Keep exactly six fields and tag with region, network type, and version; full getStats dumps become incomparable as field sets change.
Conclusions

The 0.3% packet loss with 68 ms jitter case shows network health metrics can look clean while the receive buffer is the bottleneck; teams that only watch loss or RTT miss it.

Naming jitter as a delay metric is a trap; the RTP jitter field is variance, and the ratio with emitted count is the number that matches observed buffering.

Collecting fewer fields deliberately, instead of storing full getStats state, sacrifices forensic detail for longitudinal comparability across versions.

Concepts & terms
Jitter buffer
A receive-side buffer that smooths variation in RTP packet arrival times; it delays playback by the amount needed to keep the stream continuous, so larger buffers mean more latency.
Jitter (RTP)
A metric defined in RFC 3550 as the statistical variance of inter-arrival times for RTP packets, reported in seconds. It measures arrival irregularity, not end-to-end delay.
jitterBufferDelay / jitterBufferEmittedCount
The quotient of total jitter buffer delay and the number of emitted packets gives the average time each packet sat in the buffer; this is closer to user-perceived delay than raw jitter.
TURN relay
A TURN server relays media when a direct ICE connection fails. It adds one network hop, an extra RTT, and its own bandwidth ceiling, but can be more stable than a poor direct path.
tc netem
Linux traffic-control network emulator used to add delay, jitter, and loss to an interface, useful for reproducing mobile-network conditions locally.
From the discussion
Featured comments
组合搭配函数

The picture lags the sound by 1.4 seconds. After checking everything, it turned out the jitter buffer was blown up. That root-cause hunt is ruthless enough. With the six thresholds laid out, sampling 30 rounds is enough to locate the issue. The approach is practical.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗