The problem you're facing is the high latency of HLS, which causes a longer delay for viewers to see new content when switching live content, such as changing cameras or restarting the stream. In simple terms: How to reduce the end-to-end latency of HLS.
Generally, HLS latency is around 30 seconds. This is because TS segments are typically set to 10 seconds per TS file, and the window (number of TS files) is set to 6, resulting in a total duration of 60 seconds for TS files in the M3U8 playlist. If the player starts requesting segments from the middle position, i.e., from the third-to-last segment, the latency will naturally be around 30 seconds.
You can reduce these two parameters, for example, by changing the TS file duration to 2 seconds, which will result in a latency of 6 to 8 seconds. Due to the HLS segment and window mechanism, it is difficult to optimize the latency to less than 8 seconds, as players may encounter network delays, segment loading failures, and retries, with different behaviors for different players. For more details, please refer to SRS HLS Latency. The config for SRS media server:
vhost __defaultVhost__ {
hls {
enabled on;
hls_fragment 2;
hls_window 10;
}
}
If you want to reduce the latency to within 3 seconds, you need to change the streaming protocol to HTTP-FLV. For more information, please refer to SRS FLV. Since iOS Safari does not yet support MSE, you still need to use HLS or WebRTC protocol. The config for SRS media server:
vhost __defaultVhost__ {
http_remux {
enabled on;
}
}
If you want to reduce the latency to within 1 second or achieve low latency on iOS Safari, you can use the WebRTC protocol. For more information, please refer to SRS WebRTC. The config for SRS media server:
rtc_server {
enabled on;
}
vhost __defaultVhost__ {
rtc {
enabled on;
rtmp_to_rtc on;
}
}
The root cause of this problem is not the Nginx distribution server, but the HLS segmentation mechanism and streaming protocol. It is recommended to switch to the SRS media server, which supports all these protocols and has the following workflow:
OBS/FFmpeg --RTMP--> SRS Media Server ---+--HTTP-FLV----> Browser
+--HLS/WebRTC--> iOS Safari
Note: WebRTC is generally used in the online conferencing field and is gradually being applied in low-latency broadcasting. The protocol used to push streams with WebRTC is called WHIP, similar to RTMP, and the protocol used to play streams with WebRTC is called WHEP, similar to HLS.