How to Set SRT Stream Buffer Size in FFmpeg

This guide explains how to configure the network buffer size for Secure Reliable Transport (SRT) streams using FFmpeg. You will learn about the key parameters required to adjust socket buffers and transmission latency, along with practical command-line examples to optimize your live streaming performance and prevent packet loss.

Key SRT Buffer Parameters in FFmpeg

When streaming over SRT, buffering is controlled by two main factors: the system network socket buffers and the SRT protocol latency buffer. You configure these parameters directly inside the SRT destination or source URL in FFmpeg using query parameters.

  • rcvbuf (Receive Buffer): Sets the socket receive buffer size in bytes. This is crucial for the receiving end to prevent packet loss during high-bitrate spikes.
  • sndbuf (Send Buffer): Sets the socket send buffer size in bytes. This is used by the sending end to queue packets before they are transmitted over the network.
  • latency (SRT Latency): Set in microseconds (1 second = 1,000,000 microseconds). This is the most critical buffer in SRT as it determines how long the receiver will wait for retransmitted packets before skipping them. It acts as the playout buffer.

Configuration Examples

To apply these settings, append them as query parameters to your SRT URI.

1. Configuring the Receiver (Listener/Caller)

If you are receiving an SRT stream, you should increase the rcvbuf and set an appropriate latency to handle network jitter.

ffmpeg -i "srt://0.0.0.0:9000?mode=listener&rcvbuf=12058624&latency=2000000" -c copy output.mp4

In this example:

  • rcvbuf=12058624 allocates approximately 11.5 MB for the socket receive buffer.
  • latency=2000000 sets a 2-second (2,000,000 microseconds) buffer window for packet recovery.

2. Configuring the Sender (Caller/Listener)

If you are sending an SRT stream, you should configure the sndbuf to ensure the system has enough memory to hold packets during temporary network dropouts.

ffmpeg -re -i input.mp4 -c:v libx264 -preset ultrafast -f mpegts "srt://192.168.1.50:9000?mode=caller&sndbuf=12058624&latency=2000000"

In this example:

  • sndbuf=12058624 allocates approximately 11.5 MB for the send buffer.
  • latency=2000000 configures the sender to retain packets in its sending buffer for up to 2 seconds to accommodate retransmission requests (NACK) from the receiver.

For optimal stability, use the following guidelines to calculate your buffer sizes:

  • Latency: Set the latency to at least 3 to 4 times the Round Trip Time (RTT) of your network connection. For long-distance internet streams, a latency of 1000000 to 2000000 (1 to 2 seconds) is recommended.
  • Socket Buffers (rcvbuf / sndbuf): The default system socket size is often too small for high-bitrate streams. A buffer size between 8388608 (8 MB) and 16777216 (16 MB) is generally sufficient for streaming 1080p and 4K video feeds.