seydx / homebridge-camera-ui

Homebridge plugin for RTSP Cameras with HSV, motion detection support, Image Rekognition, Web UI to manage/watch streams and WebApp support
MIT License
638 stars 92 forks source link

videoanalysis keeps restarting #335

Closed longzheng closed 2 years ago

longzheng commented 2 years ago

Describe the bug My logs keeps showing the videoanalysis process starting and closing/restarting over and over for my camera

[14/01/2022, 11:41:11 am] [CameraUI] Front 2: Videoanalysis scheduled for restart at 3AM: 919 minutes
[14/01/2022, 11:41:12 am] [CameraUI] Front 2: Restart videoanalysis session..
[14/01/2022, 11:41:22 am] [CameraUI] Front 2: Videoanalysis scheduled for restart at 3AM: 919 minutes
[14/01/2022, 11:41:22 am] [CameraUI] Front 2: Restart videoanalysis session..
[14/01/2022, 11:41:32 am] [CameraUI] Front 2: Videoanalysis scheduled for restart at 3AM: 918 minutes
[14/01/2022, 11:41:32 am] [CameraUI] Front 2: Restart videoanalysis session..

To Reproduce Enable "video analysis" for camera

Expected behavior "Video analysis" to work

Logs I enabled debug: true to get some more useful logs

14/01/2022, 11:53:39 am] [CameraUI] Front 2: Pinging 192.168.1.4:554 - successful
[14/01/2022, 11:53:39 am] [CameraUI] Front 2: Start videoanalysis...
[14/01/2022, 11:53:39 am] [CameraUI] Front 2: Videoanalysis command: C:\Users\Long\AppData\Roaming\npm\node_modules\homebridge-camera-ui\node_modules\ffmpeg-for-homebridge\ffmpeg.exe -hide_banner -loglevel error -hwaccel auto -rtsp_transport tcp -i rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=1 -an -vcodec pam -pix_fmt rgba -f image2pipe -vf fps=2,scale=640:360 pipe:1
[14/01/2022, 11:53:39 am] [CameraUI] Front 2: Videoanalysis: Currently active zones: [{"name":"region0","difference":9,"percent":75,"polygon":[{"x":0,"y":0},{"x":6,"y":353},{"x":627,"y":356},{"x":634,"y":4},{"x":0,"y":0}]}]
[14/01/2022, 11:53:39 am] [CameraUI] Front 2: Videoanalysis scheduled for restart at 3AM: 906 minutes
[14/01/2022, 11:53:45 am] [CameraUI] Front 2: FFmpeg videoanalysis process exited (expected)
[14/01/2022, 11:53:45 am] [CameraUI] Front 2: Videoanalysis process closed
[14/01/2022, 11:53:45 am] [CameraUI] Front 2: Restart videoanalysis session..
[14/01/2022, 11:53:55 am] [CameraUI] Front 2: Incoming ping request for: rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=0 - Timeout: 1s
[14/01/2022, 11:53:55 am] [CameraUI] Front 2: Pinging 192.168.1.4:554
[14/01/2022, 11:53:55 am] [CameraUI] Front 2: Pinging 192.168.1.4:554 - successful
[14/01/2022, 11:53:55 am] [CameraUI] Front 2: Start videoanalysis...
[14/01/2022, 11:53:55 am] [CameraUI] Front 2: Videoanalysis command: C:\Users\Long\AppData\Roaming\npm\node_modules\homebridge-camera-ui\node_modules\ffmpeg-for-homebridge\ffmpeg.exe -hide_banner -loglevel error -hwaccel auto -rtsp_transport tcp -i rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=1 -an -vcodec pam -pix_fmt rgba -f image2pipe -vf fps=2,scale=640:360 pipe:1
[14/01/2022, 11:53:55 am] [CameraUI] Front 2: Videoanalysis: Currently active zones: [{"name":"region0","difference":9,"percent":75,"polygon":[{"x":0,"y":0},{"x":6,"y":353},{"x":627,"y":356},{"x":634,"y":4},{"x":0,"y":0}]}]
[14/01/2022, 11:53:55 am] [CameraUI] Front 2: Videoanalysis scheduled for restart at 3AM: 906 minutes
[14/01/2022, 11:54:01 am] [CameraUI] Front 2: FFmpeg videoanalysis process exited (expected)
[14/01/2022, 11:54:01 am] [CameraUI] Front 2: Videoanalysis process closed
[14/01/2022, 11:54:01 am] [CameraUI] Front 2: Restart videoanalysis session..

Environment

longzheng commented 2 years ago

I realized after testing the command in my Windows command prompt

C:\Users\Long>ffmpeg.exe -hide_banner -loglevel error -hwaccel auto -rtsp_transport tcp -i rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=1 -an -vcodec pam -pix_fmt rgba -f image2pipe -vf fps=2,scale=640:360 pipe:1
[rtsp @ 00000174873e0900] method DESCRIBE failed: 404 Not Found
rtsp://192.168.1.4:554/cam/realmonitor?channel=7: Server returned 404 Not Found
'subtype' is not recognized as an internal or external command,
operable program or batch file.

that it was actually failing because the RTSP URL was being broken by the & for my sub-stream.

However if I wrap my RTSP URL with quotes it would work

ffmpeg.exe -hide_banner -loglevel error -hwaccel auto -rtsp_transport tcp -i "rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=1" -an -vcodec pam -pix_fmt rgba -f image2pipe -vf fps=2,scale=640:360 pipe:1

I'm not sure if this is a Windows-specific problem but I wonder if I should wrap the URL in my config with quotes.

seydx commented 2 years ago

Interesting, so with quotes its working?

longzheng commented 2 years ago

With quotes it works in my command prompt (as in the process seems to execute and I'm getting some output from the ffmpeg process, looks like it's piping out the video stream).

However if I add quotes to the config, it throws a different error.

First I tried escaping the double quotes

                    "videoConfig": {
                        "source": "-i rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=0",
                        "subSource": "-i \"rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=1\"",
                        "rtspTransport": "tcp",
                        "audio": true,
                        "debug": true
                    },
[14/01/2022, 12:03:40 pm] [CameraUI] Front 2: Start videoanalysis...
[14/01/2022, 12:03:40 pm] [CameraUI] Front 2: Videoanalysis command: C:\Users\Long\AppData\Roaming\npm\node_modules\homebridge-camera-ui\node_modules\ffmpeg-for-homebridge\ffmpeg.exe -hide_banner -loglevel error -hwaccel auto -rtsp_transport tcp -i "rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=1" -an -vcodec pam -pix_fmt rgb24 -f image2pipe -vf fps=2,scale=640:360 pipe:1
[14/01/2022, 12:03:40 pm] [CameraUI] Front 2: Videoanalysis: Sensitivity: 25 - Active zones: [{"name":"region0","difference":9,"percent":75,"polygon":[{"x":0,"y":0},{"x":6,"y":353},{"x":627,"y":356},{"x":634,"y":4},{"x":0,"y":0}]}]
[14/01/2022, 12:03:40 pm] [CameraUI] Front 2: Videoanalysis scheduled for restart at 3AM: 896 minutes
[14/01/2022, 12:03:40 pm] [CameraUI] Front 2: FFmpeg videoanalysis process exited with error! (null) - "rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=1": Invalid argument
[14/01/2022, 12:03:40 pm] [CameraUI] Front 2: Videoanalysis process closed
[14/01/2022, 12:03:40 pm] [CameraUI] Front 2: Restart videoanalysis session..

Then I tried a single quote

                    "videoConfig": {
                        "source": "-i rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=0",
                        "subSource": "-i 'rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=1'",
                        "rtspTransport": "tcp",
                        "audio": true,
                        "debug": true
                    },
[14/01/2022, 12:05:45 pm] [CameraUI] Front 2: Start videoanalysis...
[14/01/2022, 12:05:45 pm] [CameraUI] Front 2: Videoanalysis command: C:\Users\Long\AppData\Roaming\npm\node_modules\homebridge-camera-ui\node_modules\ffmpeg-for-homebridge\ffmpeg.exe -hide_banner -loglevel error -hwaccel auto -rtsp_transport tcp -i 'rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=1' -an -vcodec pam -pix_fmt rgb24 -f image2pipe -vf fps=2,scale=640:360 pipe:1
[14/01/2022, 12:05:45 pm] [CameraUI] Front 2: Videoanalysis: Sensitivity: 25 - Active zones: [{"name":"region0","difference":9,"percent":75,"polygon":[{"x":0,"y":0},{"x":6,"y":353},{"x":627,"y":356},{"x":634,"y":4},{"x":0,"y":0}]}]
[14/01/2022, 12:05:45 pm] [CameraUI] Front 2: Videoanalysis scheduled for restart at 3AM: 894 minutes
[14/01/2022, 12:05:45 pm] [CameraUI] Front 2: FFmpeg videoanalysis process exited with error! (null) - 'rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=1': Invalid argument
[14/01/2022, 12:05:45 pm] [CameraUI] Front 2: Videoanalysis process closed
[14/01/2022, 12:05:45 pm] [CameraUI] Front 2: Restart videoanalysis session..

I'm guessing there's some quirk with how node is running this command vs. how it is ran in command prompt.

longzheng commented 2 years ago

I wonder if my error in command prompt is actually a red herring since the "source": "-i rtsp://192.168.1.4:554/cam/realmonitor?channel=7&subtype=0" without any quotes definitely works properly for streaming.

I'm only trying to get video analysis working for HSV, which I understand to be a requirement, is that correct?

longzheng commented 2 years ago

After discussing with @seydx on Discord, we figured out my issue was due to ffmpeg exiting due to an error with not being able to create a Direct3D device

[14/01/2022, 12:32:20 pm] [CameraUI] Front 2: Start videoanalysis...
[14/01/2022, 12:32:20 pm] [CameraUI] Front 2: Videoanalysis command: C:\Users\Long\AppData\Roaming\npm\node_modules\homebridge-camera-ui\node_modules\ffmpeg-for-homebridge\ffmpeg.exe -hide_banner -loglevel error -hwaccel auto -analyzeduration 0 -probesize 2140380 -f mpegts -i tcp://127.0.0.1:39276 -an -vcodec pam -pix_fmt rgb24 -f image2pipe -vf fps=2,scale=640:360 pipe:1
[14/01/2022, 12:32:20 pm] [CameraUI] Front 2: Videoanalysis: Sensitivity: 25 - Active zones: [{"name":"region0","difference":9,"percent":75,"polygon":[{"x":0,"y":0},{"x":6,"y":353},{"x":627,"y":356},{"x":634,"y":4},{"x":0,"y":0}]}]
[14/01/2022, 12:32:20 pm] [CameraUI] Front 2: Videoanalysis scheduled for restart at 3AM: 868 minutes
[14/01/2022, 12:32:21 pm] [CameraUI] Front 2: Prebuffer request ended
[AVHWDeviceContext @ 00000184fdc341c0] Failed to create Direct3D device - Device creation failed: -1313558101.
[14/01/2022, 12:32:21 pm] [CameraUI] Front 2: FFmpeg videoanalysis process exited (expected)
[14/01/2022, 12:32:21 pm] [CameraUI] Front 2: Videoanalysis process closed
[14/01/2022, 12:32:21 pm] [CameraUI] Front 2: Restart videoanalysis session..

This issue is related to the fact that I'm running homebridge on a headless Windows machine which causes issue with ffmpeg using hardware acceleration.

Related https://trac.ffmpeg.org/ticket/7511 https://trac.ffmpeg.org/ticket/6827 https://github.com/jellyfin/jellyfin/issues/2626

I was able to fix it by changing the -hwaccel auto to -hwaccel d3d11va on https://github.com/seydx/camera.ui/blob/b9c4a6cea96b930a5178726162fa20855a8f24f5/src/controller/camera/services/videoanalysis.service.js#L194

seydx commented 2 years ago

v5.0.7 will remove the -hwaccel flag from the videoanalysis process, so every user can put this flag in the source/subSource (config.json)

eg

-hwaccel d3d11va -i rtsp://...

longzheng commented 2 years ago

After updating to v5.0.7-beta.1 I was able to actually successfully configure video analysis without any -hwaccel.