restreamer icon indicating copy to clipboard operation
restreamer copied to clipboard

Latency problems live stream is lagging 8-9 seconds

Open divx118 opened this issue 5 years ago • 14 comments

I use encoding h264 see #128 , but I have a latency problem. I believe it is the clappr player used and not the encoding time. Someone has a solution for this or some more inside? Also why is the "-re" flag set in the live.json config https://github.com/datarhei/restreamer/blob/master/conf/live.json#L90 --> see https://trac.ffmpeg.org/wiki/StreamingGuide#The-reflag For live streaming it shouldn't be used according to the wiki.

Thanks in advance

divx118 avatar Dec 29 '19 09:12 divx118

The encoder could well introduce some latency. Other sources might be the nginx-rtmp module and the player. The nginx-rtmp is configured in a way that it creates chunks of 2 seconds (https://github.com/datarhei/restreamer/blob/master/conf/nginx.conf#L27). The player might buffer 3-4 chunks which would explain the delay.

OT: the "-re" flag is most likely not introducing a delay. But I will check if it can be removed for good.

ioppermann avatar Dec 30 '19 09:12 ioppermann

@ioppermann Thanks for the pointers. I am now down to 1-2 seconds delay which is acceptable to me. The biggest improvement I had with setting the hls_playlist_length to 1s. (https://github.com/datarhei/restreamer/blob/master/conf/nginx.conf#L26) This probably helps with the player not buffering, which was IMO the biggest problem. I still need to read some documentation about it and experiment some more. However this looks very promising lets see how stable the stream will be on the long term.

After a bit of testing and reading below works for me fine, about 1 second delay. Sometimes the stream stops for a second, but that is not an issue for me.

            hls_playlist_length 1s;
            hls_fragment 500ms;

I am very happy the latency is hugely improved from 8-9 to 1-2 seconds. Now some fine tuning and make everything a bit more robust on my local webpage for serving the streams. :)

divx118 avatar Dec 30 '19 14:12 divx118

@divx118 Thanks a lot for your findings! Did you find some other "fine tuning" to make the stream robust and with very low latency?

andersla avatar Feb 21 '20 06:02 andersla

@andersla I didn't have any time yet to continue on this, hopefully next weekend. So if you find something I am all ears.

divx118 avatar Feb 23 '20 15:02 divx118

hls_playlist_length 1s;
hls_fragment 500ms;

I did this and got it down to 4 to 5 seconds. Going to try to modify some more settings, any advice is appreciated.

Wisbell avatar Sep 14 '20 20:09 Wisbell

@Wisbell You also have to consider the key frame interval that the camera produces (also known as GOP size). The nginx-rtmp module usually splits the stream at key frames and using hls_fragment as a guideline, but it is not guaranteed.

You can try to use the hls_max_fragment [x]ms directive which will force a fragment split even if there's no key frame. This has the consequence that a fragment will most likely not start with a key frame. A player will have to wait for the the first keyframe in order to start decoding the stream. This will again add the latency.

If you have the possibility to configure the key frame (or GOP size) in the camera, then you can tweak the latency there. You can set the interval to e.g. 2 seconds which is usually a good tradeoff between compression and latency.

ioppermann avatar Sep 15 '20 06:09 ioppermann

Any workaround for this?

How much would hls_max_fragment help?

alex9099 avatar Oct 29 '20 17:10 alex9099

I didn't try out the hls_max_fragment option yet. However, I'm not sure how much it will help because players usually wait until they have a complete key frame until they show something. This can be up to the duration of one GOP.

ioppermann avatar Nov 05 '20 11:11 ioppermann

Is there a variable that I can pass to Docker to set these or do I have to build my own image to tweak it?

Baenwort avatar Dec 08 '21 14:12 Baenwort

You can mount in a modified nginx.conf. Copy the existing one from the repo, apply the tweaks and mount it in by adding -v /path/to/tweaked/nginx.conf:/restreamer/conf/nginx.conf.

ioppermann avatar Dec 08 '21 15:12 ioppermann

Am i correct that I add that string (after fixing location) to the end of my Docker run command?

Baenwort avatar Dec 08 '21 18:12 Baenwort

It should be part of the docker command where all the other options go. At the end of the docker command is the name of the image. You can add it right before the image name.

E.g.

docker run -d --restart always -e "RS_USERNAME=admin" -e "RS_PASSWORD=datarhei" -p 8080:8080 -v /mnt/restreamer/db:/restreamer/db datarhei/restreamer:latest

becomes

docker run -d --restart always -e "RS_USERNAME=admin" -e "RS_PASSWORD=datarhei" -p 8080:8080 -v /mnt/restreamer/db:/restreamer/db -v /path/to/tweaked/nginx.conf:/restreamer/conf/nginx.conf datarhei/restreamer:latest

ioppermann avatar Dec 09 '21 17:12 ioppermann

Does anyone know how to make this modification in the latest version of the code ? Stuck with an 8-9 second delay and would love to improve it

danagain avatar Jun 06 '22 06:06 danagain

I'm also stuck on this, I can only get the WebUI to be about 9 seconds delay.

devedse avatar Sep 11 '22 22:09 devedse

@svenerbeck , I saw this issue was closed. What is the solution to reduce the delay?

devedse avatar Oct 17 '22 16:10 devedse

@svenerbeck I too would like to know?

divx118 avatar Dec 05 '22 14:12 divx118

i updated the restreamer to version 2 . but now i have up to 30 seconds of latency for some live cameras . can anyone recommand any improvment to do .

arcangeltech avatar Jan 21 '23 13:01 arcangeltech

I moved to Open streaming platform with a beta branch that uses OvenMediaEngine. This gives me sub second latency.

devedse avatar Jan 21 '23 13:01 devedse

Hello @devedse do you have a link to the beta branch? Thanks

apvlv avatar Feb 14 '23 13:02 apvlv

@apvlv https://github.com/deamos/openstreamingplatform/tree/feature/webrtc-test

Or if you want support for RaspberryPI checkout my Fork.

devedse avatar Feb 14 '23 14:02 devedse