Give the stream sockets room for high-bitrate bursts

Watching an official client's 1440p60 H.264 stream (6-9 Mbit/s) showed a
very bad frame rate. The peer sockets used the system's default buffers
(208 KB on Linux, about 90 full-size packets once the kernel's per-packet
overhead is counted), so a keyframe of hundreds of packets that arrives
while the peer task is busy overflows them, and every packet the kernel
drops costs the viewer a retransmission or a keyframe.

Each peer socket now asks for 4 MB of receive and send buffer
(PeerConfig::udp_buffer; the kernel caps it at rmem_max/wmem_max). What
the system granted is logged, and once per process with a sysctl hint
when it is less than asked.

Measured on loopback, 9 Mbit/s of noise at 60 fps with a 400 KB keyframe
every second, unpaced (high_bitrate_bursts, --ignored): with the default
buffers the kernel dropped 118-209 datagrams per 5 s and 3 of 300 frames
arrived after a gap; asking for 212992 bytes (what a default rmem_max
allows, doubled by Linux) or for 4 MB, nothing was dropped and every
frame arrived whole.

Also checked: our answer to a real libwebrtc offer (headless Chromium
152, H.264 first, stored as a fixture) keeps nack, nack pli,
transport-cc and an RTX payload type for every H.264 variant, and a relay
that loses one media packet in 25 shows str0m's NACK and retransmissions
repairing every loss before its depacketizer gives up (30 frames). The
relay (tests/relay) also serves the fallback tests that follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
selikhovandClaude Opus 5.5 committed 2026-10-03 22:56:14 +03:00
1 parent 9b20da22d8
commit ba6142e12e
6 files changed
+625 -3

No files matched your search

+44 -1
View File
@@ -40,6 +40,9 @@ use crate::stun;
/// [`OfferOptions::start_bitrate`].
pub const DEFAULT_START_BITRATE: u64 = 1_000_000;
/// Socket buffers a peer asks for by default ([`PeerConfig::udp_buffer`]).
pub const DEFAULT_UDP_BUFFER: usize = 4 << 20;
/// Video codecs a peer can negotiate.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VideoCodec {
@@ -197,6 +200,13 @@ pub struct PeerConfig {
/// level 3.1 by default. A viewer gets frames of the profile its answer
/// chose ([`PeerEvent::VideoCodec`]). At most two are offered.
pub h264_profile_level_ids: Vec<u32>,
/// Receive and send buffer (bytes) each peer socket asks the system for
/// (`SO_RCVBUF`, `SO_SNDBUF`; Linux caps them at `net.core.rmem_max` and
/// `wmem_max`); 0 keeps the system's default. Linux's default, 208 KB,
/// holds about 90 full-size packets: a 1440p keyframe arriving while
/// the peer task is busy overflows it, and every lost packet costs the
/// viewer a retransmission or a keyframe.
pub udp_buffer: usize,
}
impl Default for PeerConfig {
@@ -215,6 +225,7 @@ impl Default for PeerConfig {
.iter()
.map(|p| h264::profile_level_id(*p, h264::MIN_OFFER_LEVEL))
.collect(),
udp_buffer: DEFAULT_UDP_BUFFER,
}
}
}
@@ -676,7 +687,7 @@ impl Net {
let (tx, incoming) = mpsc::channel(256);
let mut sockets = Vec::new();
for ip in hosts {
let socket = Arc::new(UdpSocket::bind(SocketAddr::new(ip, 0)).await?);
let socket = Arc::new(udp_socket(SocketAddr::new(ip, 0), config.udp_buffer)?);
let local = socket.local_addr()?;
match Candidate::host(local, "udp") {
Ok(c) => {
@@ -720,6 +731,38 @@ impl Net {
}
}
/// A UDP socket on `addr` asking for `buffer` bytes of receive and send
/// buffer (0: the system's default), see [`PeerConfig::udp_buffer`]. What
/// the system granted is logged; once per process with a hint when it is
/// less than asked.
fn udp_socket(addr: SocketAddr, buffer: usize) -> std::io::Result<UdpSocket> {
use socket2::{Domain, Protocol, Socket, Type};
static CAPPED: std::sync::Once = std::sync::Once::new();
let socket = Socket::new(Domain::for_address(addr), Type::DGRAM, Some(Protocol::UDP))?;
if buffer > 0 {
// Best effort: the system caps what it grants, and a smaller
// buffer only costs packets under load.
let _ = socket.set_recv_buffer_size(buffer);
let _ = socket.set_send_buffer_size(buffer);
}
socket.set_nonblocking(true)?;
socket.bind(&addr.into())?;
let (receive, send) =
(socket.recv_buffer_size().unwrap_or(0), socket.send_buffer_size().unwrap_or(0));
debug!(%addr, asked = buffer, receive, send, "UDP socket buffers");
if receive < buffer {
CAPPED.call_once(|| {
warn!(
asked = buffer,
granted = receive,
"the system grants less UDP receive buffer than asked; high-bitrate streams \
may lose packets in bursts (Linux: sysctl net.core.rmem_max)"
);
});
}
UdpSocket::from_std(socket.into())
}
struct PendingStun {
id: stun::TransactionId,
server: SocketAddr,