AlexxIT / go2rtc

Ultimate camera streaming application with support RTSP, RTMP, HTTP-FLV, WebRTC, MSE, HLS, MP4, MJPEG, HomeKit, FFmpeg, etc.
https://github.com/AlexxIT/Blog
MIT License
3.76k stars 304 forks source link

Timeouts in HA when using multiple transcoded streams #1162

Open atx32 opened 4 weeks ago

atx32 commented 4 weeks ago

I'm using the go2rtc HA add-on with the RTSPtoWebRTC integration all running on a RPi4. 3 of 4 cameras need transcoding to have a WebRTC compliant audio stream, so settings are configured as:

streams:
  "camera.bedroomcam_profile001_substream": ffmpeg:{input}#video=copy#audio=opus
  "camera.livingroomcam_fluent": ffmpeg:{input}#video=copy#audio=opus
  "camera.frontdoorcam_fluent": ffmpeg:{input}#video=copy#audio=opus

When viewing my dashboard that has all the camera feeds, most times one of the three that are transcoded will fail to start. The error shown from the integration is "Failed to start WebRTC stream: Timeout talking to RTSPtoWebRTC server". The integration has a 10 sec timeout for the stream to start, and in the go2rtc logs I see that one ffmpeg instance will take more than 10 sec to start.

5/17/2024, 7:52:59 PM   debug   [exec] run url=exec:ffmpeg -hide_banner -v error -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://<user>:<pass>@192.168.4.182:554/Preview_01_sub -c:v copy -c:a libopus -application:a lowdelay -min_comp 0 -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp rtsp://127.0.0.1:8554/b8ece7ccd1b0375f3979b4f6cdc82caa cmd=ffmpeg -hide_banner -v error -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://<user>:<pass>@192.168.4.182:554/Preview_01_sub -c:v copy -c:a libopus -application:a lowdelay -min_comp 0 -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp rtsp://127.0.0.1:8554/b8ece7ccd1b0375f3979b4f6cdc82caa
5/17/2024, 7:52:59 PM   debug   [exec] run url=exec:ffmpeg -hide_banner -v error -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://<user>:<pass>@192.168.4.136:554/h264Preview_01_sub -c:v copy -c:a libopus -application:a lowdelay -min_comp 0 -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp rtsp://127.0.0.1:8554/8cd8f6b037a1dd1aa36a24d92da587ae cmd=ffmpeg -hide_banner -v error -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://<user>:<pass>@192.168.4.136:554/h264Preview_01_sub -c:v copy -c:a libopus -application:a lowdelay -min_comp 0 -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp rtsp://127.0.0.1:8554/8cd8f6b037a1dd1aa36a24d92da587ae
5/17/2024, 7:52:59 PM   debug   [exec] run url=exec:ffmpeg -hide_banner -v error -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://<user>:<pass>@192.168.4.161:554/Preview_01_sub -c:v copy -c:a libopus -application:a lowdelay -min_comp 0 -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp rtsp://127.0.0.1:8554/a8092000eb9fde31a940ebe9f48dae3c cmd=ffmpeg -hide_banner -v error -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://<user>:<pass>@192.168.4.161:554/Preview_01_sub -c:v copy -c:a libopus -application:a lowdelay -min_comp 0 -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp rtsp://127.0.0.1:8554/a8092000eb9fde31a940ebe9f48dae3c
5/17/2024, 7:53:01 PM   debug   [streams] start producer url=rtsp://<user>:<pass>@192.168.4.163/live
5/17/2024, 7:53:06 PM   debug   [exec] run launch=6.556431141s
5/17/2024, 7:53:06 PM   debug   [streams] start producer url=ffmpeg:rtsp://<user>:<pass>@192.168.4.182:554/Preview_01_sub#video=copy#audio=opus
5/17/2024, 7:53:07 PM   debug   [exec] run launch=8.09587382s
5/17/2024, 7:53:07 PM   debug   [streams] start producer url=ffmpeg:rtsp://<user>:<pass>@192.168.4.161:554/Preview_01_sub#video=copy#audio=opus
5/17/2024, 7:53:13 PM   debug   [exec] run launch=13.507170036s
5/17/2024, 7:53:13 PM   debug   [streams] start producer url=ffmpeg:rtsp://<user>:<pass>@192.168.4.136:554/h264Preview_01_sub#video=copy#audio=opus
5/17/2024, 7:54:09 PM   debug   [streams] stop producer url=ffmpeg:rtsp://<user>:<pass>@192.168.4.161:554/Preview_01_sub#video=copy#audio=opus
5/17/2024, 7:54:11 PM   debug   [streams] stop producer url=rtsp://<user>:<pass>@192.168.4.163/live
5/17/2024, 7:54:11 PM   debug   [streams] stop producer url=ffmpeg:rtsp://<user>:<pass>@192.168.4.182:554/Preview_01_sub#video=copy#audio=opus
5/17/2024, 7:54:11 PM   debug   [streams] stop producer url=ffmpeg:rtsp://<user>:<pass>@192.168.4.136:554/h264Preview_01_sub#video=copy#audio=opus

Occasionally all three will start in less than 10 sec, and then all streams work with no error. I never see high CPU usage and latency is low once the streams start so I don't think this is specifically due to the RPi4.

Since all three ffmpeg instances are called at the same time, why do they take progressively longer to start and is there any way to improve this?

AlexxIT commented 3 weeks ago

Have you tried with single camera?

I don't know why you have progressive launch time. Maybe it's just a coincidence. There may be a heavy load on the network.

Also Reonlik cameras has awful RTSP realisation.

FFmpeg start time depends on cameras keyframe interval. On good cameras it's 1 second. I don't know this value on Reolink.

Also you can try two source version:

streams:
  camera1:
    - rtsp:// # for reolink better to use http
    - ffmpeg:camera1#audio=opus

This version doesn't depend on keyframe interval and should starts almost instantly.

atx32 commented 3 weeks ago

Network isn't loaded so that should not be an issue. I will experiment with the items you suggest and report back.

atx32 commented 3 weeks ago

The keyframe interval is one part of the issue as the default interval for the Reolink low-res stream is 4 sec. I set it lower (2 of the 3 cameras support changing the keyframe interval) and everything loads in under 10 sec. I still see the progressive launch times; they are just shorter now. I will spend some more time trying various settings, but it may be a week or 2 before I get to it.

It would be good to note the keyframe interval dependency in the docs.