mediamtx/docs/2-usage/26-decrease-packet-loss.md
2025-10-29 10:51:02 +01:00

2.4 KiB

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.

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.

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:

    rtspUDPReadBufferSize: 1000000
    

    If the source of the stream is a camera:

    paths:
      test:
        source: rtsp://..
        rtspUDPReadBufferSize: 1000000
    

    There are similar options for the MPEG-TS and RTP protocol:

    paths:
      test:
        source: udp+mpegts://..
        mpegtsUDPReadBufferSize: 1000000
    

    or:

    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:

    sudo sysctl net.core.rmem_max=100000000
    
  • When reading a stream, packets might get discarded because the write queue is too small. This can be noticed in logs through the "reader is too slow" message. Try increasing the write queue:

    writeQueueSize: 1024
    
  • When publishing or reading a stream with RTSP, it's possible to switch from the UDP transport protocol to the TCP transport protocol, which is less performant but has a packet retransmission mechanism:

    rtspTransports: [tcp]
    

    In case the source is a camera:

    paths:
      test:
        source: rtsp://..
        rtspTransport: tcp