keshavdv / unifi-cam-proxy

Enable non-Ubiquiti cameras to work with Unifi NVR
MIT License
1.6k stars 226 forks source link

BrokenPipeError using RTSP feed #188

Closed daltskin closed 2 years ago

daltskin commented 2 years ago

First thing - great app @keshavdv, this makes the Unifi Protect actually useful :)

Describe the bug When using with a Samsung (Techwin) DVR and Hikvision THC-D220 & THC-T220 cameras the RTSP output within the Unifi Protect web or mobile app are not updating - using the Android app, I'm just seeing the original image and 3 dots animation.

From the docker-compose example I'm have lots of "BrokenPipeError" errors eg:

unifi-cam-proxy0_1  | 2022-02-21 19:36:07 5f95c2e9bc1f RTSPCam[1] WARNING Previous ffmpeg process for video3 died.
unifi-cam-proxy0_1  | 2022-02-21 19:36:07 5f95c2e9bc1f RTSPCam[1] INFO Spawning ffmpeg for video3 (7KBzctvieeBbHTZO): ffmpeg -nostdin -loglevel error -y -avoid_negative_ts make_zero -fflags +genpts+discardcorrupt -stimeout 15000000 -rtsp_transport tcp -i "rtsp://{user}{password}@{ip}:4524/0" -c:v copy -ar 32000 -ac 1 -codec:a aac -b:a 32k -metadata streamname=7KBzctvieeBbHTZO -f flv - | /usr/local/bin/python -m unifi.clock_sync | nc {ip} 7550
unifi-cam-proxy0_1  | 2022-02-21 19:36:08 5f95c2e9bc1f RTSPCam[1] INFO Processing [NetworkStatus] message
unifi-cam-proxy0_1  | 2022-02-21 19:36:08 5f95c2e9bc1f RTSPCam[1] INFO Processing [GetSystemStats] message
unifi-cam-proxy0_1  | Traceback (most recent call last):
unifi-cam-proxy0_1  |   File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
unifi-cam-proxy0_1  |     return _run_code(code, main_globals, None,
unifi-cam-proxy0_1  |   File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
unifi-cam-proxy0_1  |     exec(code, run_globals)
unifi-cam-proxy0_1  |   File "/app/unifi/clock_sync.py", line 167, in <module>
unifi-cam-proxy0_1  |     main()
unifi-cam-proxy0_1  |   File "/app/unifi/clock_sync.py", line 157, in main
unifi-cam-proxy0_1  |     write(read_bytes(source, payload_size))
unifi-cam-proxy0_1  |   File "/app/unifi/clock_sync.py", line 87, in write
unifi-cam-proxy0_1  |     sys.stdout.buffer.write(data)
unifi-cam-proxy0_1  | BrokenPipeError: [Errno 32] Broken pipe
unifi-cam-proxy0_1  | Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
unifi-cam-proxy0_1  | BrokenPipeError: [Errno 32] Broken pipe
unifi-cam-proxy0_1  | av_interleaved_write_frame(): Broken pipe
unifi-cam-proxy0_1  | Error writing trailer of pipe:: Broken pipe
unifi-cam-proxy0_1  | Traceback (most recent call last):
unifi-cam-proxy0_1  |   File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
unifi-cam-proxy0_1  |     return _run_code(code, main_globals, None,
unifi-cam-proxy0_1  |   File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
unifi-cam-proxy0_1  |     exec(code, run_globals)
unifi-cam-proxy0_1  |   File "/app/unifi/clock_sync.py", line 167, in <module>
unifi-cam-proxy0_1  |     main()
unifi-cam-proxy0_1  |   File "/app/unifi/clock_sync.py", line 161, in main
unifi-cam-proxy0_1  |     write(read_bytes(source, payload_size))
unifi-cam-proxy0_1  |   File "/app/unifi/clock_sync.py", line 87, in write
unifi-cam-proxy0_1  |     sys.stdout.buffer.write(data)
unifi-cam-proxy0_1  | BrokenPipeError: [Errno 32] Broken pipe
unifi-cam-proxy0_1  | Traceback (most recent call last):
unifi-cam-proxy0_1  |   File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
unifi-cam-proxy0_1  |     return _run_code(code, main_globals, None,
unifi-cam-proxy0_1  |   File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
unifi-cam-proxy0_1  |     exec(code, run_globals)
unifi-cam-proxy0_1  |   File "/app/unifi/clock_sync.py", line 167, in <module>
unifi-cam-proxy0_1  |     main()
unifi-cam-proxy0_1  |   File "/app/unifi/clock_sync.py", line 160, in main
unifi-cam-proxy0_1  |     write(header)
unifi-cam-proxy0_1  |   File "/app/unifi/clock_sync.py", line 87, in write
unifi-cam-proxy0_1  |     sys.stdout.buffer.write(data)
unifi-cam-proxy0_1  | BrokenPipeError: [Errno 32] Broken pipe
unifi-cam-proxy0_1  | Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
unifi-cam-proxy0_1  | BrokenPipeError: [Errno 32] Broken pipe
unifi-cam-proxy0_1  | Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
unifi-cam-proxy0_1  | BrokenPipeError: [Errno 32] Broken pipe
unifi-cam-proxy0_1  | av_interleaved_write_frame(): Broken pipe
unifi-cam-proxy0_1  | Error writing trailer of pipe:: Broken pipe
unifi-cam-proxy0_1  | av_interleaved_write_frame(): Broken pipe
unifi-cam-proxy0_1  | Error writing trailer of pipe:: Broken pipe
unifi-cam-proxy0_1  | 2022-02-21 19:36:18 5f95c2e9bc1f RTSPCam[1] INFO Processing [NetworkStatus] message
unifi-cam-proxy0_1  | 2022-02-21 19:36:18 5f95c2e9bc1f RTSPCam[1] INFO Processing [GetSystemStats] message

ffprobe output from RTSP

 --arch=armv6t2 --disable-thumb --enable-shared --disable-doc --disable-programs
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Input #0, rtsp, from 'rtsp://{user}:{password}@{ip}:4524/6':
  Metadata:
    comment         : samsung
    title           : Media Presentation
  Duration: N/A, start: 0.160044, bitrate: N/A
    Stream #0:0: Video: h264 (Baseline), yuv420p(progressive), 704x288, 6.25 fps, 6.25 tbr, 90k tbn, 180k tbc

VLC streams fine with no issue as does ffmpeg when outputting to a local mp4 file.

Things I've tried

Have tried using various --ffmpeg settings (and dropping audio) eg:

--ffmpeg-args '-c:v copy -bsf:v "h264_metadata=tick_rate=60000/1001:fixed_frame_rate_flag=1" -an' --ffmpeg-args '-c:v copy -bsf:v "h264_metadata=tick_rate=30000/1001:fixed_frame_rate_flag=1" -an' --ffmpeg-args '-c:v copy -bsf:v "h264_metadata=tick_rate=30" -an' --ffmpeg-args '-c:v copy -an'

Also tried different rtsp protocols (udp/http etc.) but to no avail. Any tips on what I can try next?

Thanks again!

keshavdv commented 2 years ago

Can you give --ffmpeg-args='-c:v copy -ar 32000 -ac 1 -codec:a aac -b:a 32k -bsf:v "h264_metadata=tick_rate=12.5"' a go? The 12.5 is 2x the FPS of your stream based on your ffprobe output.

daltskin commented 2 years ago

Thanks, I tried that and it didn't work :( I think this could be similar to #194 where in my case, the DVR fps output (6.5 fps) is just too low. The Samsung SRD-870D I was using doesn't have much/any ability to fine tune the video rtsp output. The only way I could get it working was to encode rather than copy the stream using the ffmpeg setting -cv h264. But, this was no use to me as it sent the CPU sky high on my PC whilst debugging, so RPi would have no chance feeding 8 cameras!

So, as a test I bought a Annke DVR and moved over the existing cameras to it. This unit allows me to specify the frame rate per stream and doesn't require encoding, so using the default ffmpeg settings is so far working well for the live stream :)

daltskin commented 2 years ago

Re-titled in case this helps others. @keshavdv feel free to close, or if you want me to test anything against the old unit lmk.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.