How NACK and TWCC Keep WebRTC Streams Smooth and Low‑Latency
This article explains the QoS mechanisms NACK and TWCC used in WebRTC, detailing their packet‑loss detection, retransmission strategies, congestion‑control algorithms, and how they improve real‑time audio/video communication while minimizing bandwidth waste.
NACK Generation Strategy
Negative Acknowledgment (NACK) packets are RTCP feedback messages (type 205, feedback type 1) that inform the sender about lost RTP packets, unlike TCP ACK which confirms receipt. When the receiver detects missing packets, it adds them to a NACK list and, after a reorder threshold or jitter timeout based on RTT, requests retransmission.
The SFU maintains a NACK array storing fields such as type , params , seqNum , bornAt , tries , and lastNACKedAt . Each entry records the RTP sequence number, creation time, and transmission attempts, allowing the SFU to manage retransmissions efficiently.
<code>type nack struct {
params *nackParams
seqNum uint16
bornAt time.Time
tries uint8
lastNACKedAt time.Time
}
</code>The SFU checks incoming RTP packets for sequence gaps, creates corresponding NACK entries, and removes them once the missing packets are received. If a NACK entry persists beyond 20 ms, the SFU sends a NACK request, limiting retries to ten attempts. An exponential back‑off algorithm, based on RTT, schedules retransmissions to reduce network load and improve delivery success.
TWCC Strategy
Transport‑Wide Congestion Control (TWCC) extends RTP with a transport‑wide CC header and RTCP feedback messages to estimate bandwidth usage from packet loss and delay, enabling fair sharing of network capacity among concurrent streams. The sender assigns a unique sequence number and send time to each RTP packet; the receiver records arrival times and computes recv delta values.
These deltas are sent back to the sender in TWCC feedback packets. The sender groups packets, calculates send‑time intervals ( send_delta ), and derives both instantaneous and cumulative delay variations. Linear regression on the delay series yields a slope that indicates congestion; if the slope exceeds a dynamic threshold, the sender reduces its transmission rate, otherwise it may increase the rate.
The algorithm also applies exponential smoothing to cumulative delay changes and uses the smoothed value in a regression model (X‑axis: arrival time of the last packet in a group, Y‑axis: smoothed cumulative delay). The resulting slope is compared against a threshold to trigger congestion signals, which adjust the bandwidth estimate via an AIMD (Additive Increase Multiplicative Decrease) controller that combines loss‑based and delay‑based estimates.
Summary
NACK and TWCC are key QoS strategies: NACK efficiently retransmits only lost packets without adding redundant data, conserving bandwidth, while TWCC provides precise bandwidth estimation and rapid congestion response, ensuring stable, low‑latency real‑time communication. Both mechanisms are employed in the company’s custom SFU implementation.
360 Zhihui Cloud Developer
360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.