add udpReadBufferSize parameter (#5129)

this allows to set a global UDP read buffer, applied to every UDP socket.
This commit is contained in:
Alessandro Ros 2025-10-29 11:28:22 +01:00 committed by GitHub
parent 3ae5262510
commit adc4a6ceb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 78 additions and 91 deletions

View file

@ -1,47 +1,20 @@
# Decrease packet loss
MediaMTX is meant for routing live streams, and makes use of a series of protocol which try to minimize latency at cost of losing packets in transmit. In particular, most protocols are built on UDP, which is an "unrealiable transport", specifically picked because it allows to drop late packets in case of network congestions, preserving the real-time aspect of streams.
MediaMTX is meant for routing live streams, and makes use of a series of protocol which try to preserve the real-time aspect of streams and minimize latency at cost of losing packets in transmit. In particular, most protocols are built on UDP, which is an "unrealiable transport", specifically picked because it allows to drop late packets in case of network congestions.
These packet losses are usually detected and printed in MediaMTX logs.
If you need to improve the stream reliability and decrease packet losses, the first thing to do is to check whether the network between the MediaMTX instance and the intended publishers and readers has sufficient bandwidth for transmitting the media stream. Most of the times, packet losses are caused by a network which is not fit for this scope. This limitation can be overcome by either recompressing the stream with a lower bitrate, or by changing network.
If you need to improve the stream reliability and decrease packet losses, the first thing to do is to check whether the network between the MediaMTX instance and the intended publishers and readers has sufficient bandwidth for transmitting the media stream. Most of the times, packet losses are caused by a network which is not fit for this scope. This limitation can be overcome by either recompressing the stream with a lower bitrate, or by upgrading the network physical infrastructure (routers, cables, Wi-Fi, firewalls, topology, etc).
There are however some parameters that can be tuned to improve the situation, at cost of increasing RAM consumption:
- When publishing a stream with RTSP, MPEG-TS or RTP, packets might get discarded by the server because the UDP read buffer size is too small. Try increasing the UDP read buffer size:
- When publishing a stream with a UDP-based protocol (currently RTSP, MPEG-TS, RTP, SRT, WebRTC), packets might get discarded by the server because the read buffer size of UDP sockets is too small. It can be increased with this parameter:
```yml
rtspUDPReadBufferSize: 1000000
udpReadBufferSize: 1000000
```
If the source of the stream is a camera:
```yml
paths:
test:
source: rtsp://..
rtspUDPReadBufferSize: 1000000
```
There are similar options for the MPEG-TS and RTP protocol:
```yml
paths:
test:
source: udp+mpegts://..
mpegtsUDPReadBufferSize: 1000000
```
or:
```yml
paths:
test:
source: udp+rtp://..
rtpUDPReadBufferSize: 1000000
```
All these options require the `net.core.rmem_max` system parameter to be equal or greater than `rtspUDPReadBufferSize`:
The `udpReadBufferSize` parameter requires the `net.core.rmem_max` system parameter to be equal or greater than it. It can be set with this command:
```sh
sudo sysctl net.core.rmem_max=100000000