Configure FFmpeg SRT Stream Keep-Alive
This article explains how to configure FFmpeg to manage keep-alive packets and connection timeouts during Secure Reliable Transport (SRT) streaming. You will learn how SRT handles connection maintenance and how to use FFmpeg command-line parameters to ensure your stream remains stable and active.
Under the hood, the SRT protocol automatically handles keep-alive packets. It sends periodic control signals (every 10 milliseconds to 1 second depending on the connection state) to maintain the connection and prevent firewalls or NAT devices from closing idle ports. While you cannot change the frequency of the background keep-alive packets directly via FFmpeg, you must configure FFmpeg’s timeout settings to properly interpret these keep-alives and prevent premature disconnections.
To configure keep-alive and connection timeouts, you append query parameters directly to your SRT destination URI in the FFmpeg command.
The Timeout Parameter
The primary parameter for managing connection life is
timeout. This value is defined in
microseconds (1 second = 1,000,000 microseconds). If
FFmpeg does not receive any keep-alive or data packets within this
window, it will terminate the connection.
Here is an example command configuring a 5-second timeout:
ffmpeg -re -i input.mp4 -f mpegts "srt://192.168.1.100:9000?timeout=5000000"The Connect Timeout Parameter
To configure how long FFmpeg should wait to establish the initial
connection before giving up, use the connect_timeout
parameter (also in microseconds). This is crucial for handling initial
handshake keep-alives when establishing a link.
Here is an example configuring a 10-second initial connection timeout:
ffmpeg -re -i input.mp4 -f mpegts "srt://192.168.1.100:9000?connect_timeout=10000000"Combining Parameters
You can combine multiple parameters in the SRT connection string
using the ampersand (&) character to optimize your
stream’s stability. Make sure to wrap the entire URL in quotes to
prevent your command line shell from misinterpreting the ampersand:
ffmpeg -re -i input.mp4 -f mpegts "srt://192.168.1.100:9000?timeout=5000000&connect_timeout=10000000&pkt_size=1316"By correctly setting these parameters, FFmpeg will reliably listen for SRT keep-alive packets and maintain a resilient streaming connection even during brief periods of network instability.