Monitor SRT Packet Loss and Latency with FFmpeg
Monitoring network performance is crucial for maintaining high-quality live streams. This article explains how to track real-time packet loss, latency, and other vital statistics of an active Secure Reliable Transport (SRT) connection using FFmpeg’s built-in logging and command-line options.
Enabling SRT Statistics in FFmpeg
FFmpeg leverages the official libsrt library to handle
SRT streams. To monitor the connection’s health, you can pass the
stats parameter directly inside the SRT target URL.
The stats parameter dictates how frequently FFmpeg
outputs connection metrics to the console. The value is defined by the
number of packets processed. For example, setting stats=100
will output performance statistics every 100 packets.
Example: Receiving an SRT Stream (Listener Mode)
To receive an SRT stream and output statistics to the console every 100 packets, use the following command:
ffmpeg -i "srt://0.0.0.0:9000?mode=listener&stats=100" -c copy output.mp4Example: Sending an SRT Stream (Caller Mode)
To send a stream to a remote server and monitor the transmission statistics, use:
ffmpeg -re -i input.mp4 -c:v libx264 -f mpegts "srt://192.168.1.50:9000?mode=caller&stats=100"Interpreting the Console Output
Once the connection is active, FFmpeg will continuously output
statistics block data to the standard error (stderr)
stream. Look for lines in the console output that look like this:
SRT stats: sid=0 bytesRecv=12450000 pkgRecv=9000 pkgRcvLoss=12 pkgRcvRetrans=8 rtt=15ms
To monitor packet loss and latency, focus on these key metrics:
rtt(Round Trip Time): Measured in milliseconds (ms), this represents the current network latency. If therttvalue rises close to your configured SRT latency envelope, you may experience stream buffering.pkgRcvLoss/pkgSndLoss(Packet Loss): The total number of lost packets detected.pkgRcvLossdisplays packets lost on the receiving end, whilepkgSndLossdisplays packets lost during transmission.pkgRcvRetrans/pkgSndRetrans(Retransmissions): The number of packets that were successfully recovered and retransmitted via SRT’s ARQ (Automatic Repeat reQuest) mechanism.
Best Practices for Monitoring
Adjust the Frequency: If the console output is too fast or too slow, adjust the
statsvalue. For high-bitrate streams,stats=1000is often sufficient, while lower-bitrate streams benefit fromstats=100.Redirect Logs for Analysis: If you want to analyze the data later or parse it using a script, redirect the standard error output to a log file:
ffmpeg -i "srt://0.0.0.0:9000?mode=listener&stats=100" -c copy output.mp4 2> srt_stats.log