keshavdv / unifi-cam-proxy

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

Fail to stream (Broken pipe) #190

Closed AliMickey closed 2 years ago

AliMickey commented 2 years ago

Describe the bug I don't remember if I submitted this bug before, but I am back to trying to get this to work. I face this error when I run the container.

unifi                  | 2022-02-24 08:13:41 cef1a4fd2d96 RTSPCam[1] INFO Spawning ffmpeg for video3 (MpnGSRZ3ylKDUDGb): ffmpeg -nostdin -loglevel error -y -avoid_negative_ts make_zero -fflags +genpts+discardcorrupt -stimeout 15000000 -rtsp_transport tcp -i "rtsp://admin:Commodore123@10.0.0.7:554/chID=1&streamType=sub1" -c:v copy -ar 32000 -ac 1 -codec:a aac -b:a 32k -metadata streamname=MpnGSRZ3ylKDUDGb -f flv - | /usr/local/bin/python -m unifi.clock_sync | nc 10.0.0.1 7550
unifi                  | Traceback (most recent call last):
unifi                  |   File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
unifi                  |     return _run_code(code, main_globals, None,
unifi                  |   File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
unifi                  |     exec(code, run_globals)
unifi                  |   File "/app/unifi/clock_sync.py", line 167, in <module>
unifi                  |     main()
unifi                  |   File "/app/unifi/clock_sync.py", line 153, in main
unifi                  |     write(create_script_tag("onClockSync", data, timestamp))
unifi                  |   File "/app/unifi/clock_sync.py", line 87, in write
unifi                  |     sys.stdout.buffer.write(data)
unifi                  | BrokenPipeError: [Errno 32] Broken pipe
unifi                  | Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
unifi                  | BrokenPipeError: [Errno 32] Broken pipe
unifi                  | av_interleaved_write_frame(): Broken pipe
unifi                  | Error writing trailer of pipe:: Broken pipe
unifi                  | Traceback (most recent call last):
unifi                  |   File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
unifi                  |     return _run_code(code, main_globals, None,
unifi                  |   File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
unifi                  |     exec(code, run_globals)
unifi                  |   File "/app/unifi/clock_sync.py", line 167, in <module>
unifi                  |     main()
unifi                  |   File "/app/unifi/clock_sync.py", line 157, in main
unifi                  |     write(read_bytes(source, payload_size))
unifi                  |   File "/app/unifi/clock_sync.py", line 87, in write
unifi                  |     sys.stdout.buffer.write(data)
unifi                  | BrokenPipeError: [Errno 32] Broken pipe
unifi                  | Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
unifi                  | BrokenPipeError: [Errno 32] Broken pipe
unifi                  | av_interleaved_write_frame(): Broken pipe
unifi                  | Error writing trailer of pipe:: Broken pipe

To Reproduce Steps to reproduce the behavior: Set up and run container using no custom ffmpeg args.

Camera (please complete the following information):

Additional context Docker compose:

unifi:
    container_name: unifi
    image: keshavdv/unifi-cam-proxy:latest
    restart: unless-stopped
    networks:
      - home
    volumes:
      - ./unifi/client.pem:/client.pem
    command: unifi-cam-proxy --host 10.0.0.1 --cert /client.pem --token l6... rtsp -s rtsp://user:password@10.0.0.7:554/chID=1&streamType=sub1

Thanks!

nicoleise commented 2 years ago

I have the same issue, same error messages. I've been working with it for days. My usecase is "bare-metal" (Proxmox LXC) and I discovered that I can stream the bunny test to Unifi and stream the camera to disc (on the host of the unifi cam proxy), so either end of the proxy works individually, but not when combined. I'll supplement this thread rather than make a new one, I hope that's OK.

Asked on the Discord to no avail, and I'm not smart enough to figure out if this is a bug or me doing it wrong.

Setup According to bare-metal install in the docs (in LXC on Proxmox, plenty of ressources according to summary logs) Unifi Protect on Cloud Key G2+ @ 10.0.0.10 Dahua IPC-HDW4830something @ 10.10.30.11, main stream 4K/4 FPS, substream HD/1 FPS, both H264 No firewall rules between the subnets (yet). All stream sources mentioned in this post work on VLC in both 10.10.10.X and other networks.

Streaming Big Buck Bunny to Unifi Protect unifi-cam-proxy --host 10.0.0.10 --cert /var/certs/client.pem --token xxxxxxxxerVy5bDHbE7FYHSWxxxxxxx rtsp -s 'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_175k.mov' This command works as expected, Unifi controller reports the camera as online, shows snapshots when viewing the camera in device manager, shows live video and recorded playback. The video is choppy, but aside from that, things work.

Streaming from IP camera to disk of proxy host ffmpeg -nostdin -loglevel error -y -avoid_negative_ts make_zero -fflags +genpts+discardcorrupt -stimeout 15000000 -rtsp_transport tcp -i "rtsp://user:password@10.10.30.11:554/cam/realmonitor?channel=1&subtype=2" -c:v copy -ar 32000 -ac 1 -codec:a aac -b:a 32k -metadata streamname=iiBbC5Vkdy7Zadnd -f flv test.flv No errors shown and when I CTRL+C the file test.flv exists with a reasonable size.

Commands attempted to stream from IP camera to Unifi Protect Note that all of these output the broken pipe error shown in @AliMickey post above.

Exact repeat of the Big Buck Bunny test command changing only the source, generic rtsp: unifi-cam-proxy --host 10.0.0.10 --cert /var/certs/client.pem --token xxxxxxxxxxVy5bDHbE7FYHSWxxxxxxx rtsp -s 'rtsp://user:password@10.10.30.11/live'

This is the ffmpeg command run by the big buck bunny command. When changing only the source, I get the broken pipe error. ffmpeg -nostdin -loglevel error -y -avoid_negative_ts make_zero -fflags +genpts+discardcorrupt -stimeout 15000000 -rtsp_transport tcp -i "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_175k.mov" -c:v copy -ar 32000 -ac 1 -codec:a aac -b:a 32k -metadata streamname=iiBbC5Vkdy7Zadnd -f flv - | /usr/bin/python3 -m unifi.clock_sync | nc 10.0.0.10 7550

Trying a different stream source on the camera (still generic rstp), that also works in VLC (/live does too), main stream: unifi-cam-proxy --host 10.0.0.10 --cert /var/certs/client.pem --token xxxxxxxxxVy5bDHbE7FYHSWxxxxxxxx rtsp -s 'rtsp://user:password@10.10.30.11:554/cam/realmonitor?channel=1&subtype=0'

As above, sub stream: unifi-cam-proxy --host 10.0.0.10 --cert /var/certs/client.pem --token xxxxxxxxxVy5bDHbE7FYHSWxxxxxxxx rtsp -s 'rtsp://user:password@10.10.30.11:554/cam/realmonitor?channel=1&subtype=2'

Dahua-specific commands, main 4K stream: unifi-cam-proxy -H 10.0.0.10 -i 10.10.30.11 -c /var/certs/client.pem -t xxxxxxxxxxVy5bDHbE7FYHSWxxxxxxxxx dahua -u user -p password --sub-stream 0

Dahua-specific commands, sub HD stream: unifi-cam-proxy -H 10.0.0.10 -i 10.10.30.11 -c /var/certs/client.pem -t xxxxxxxxxxVy5bDHbE7FYHSWxxxxxxxxx dahua -u user -p password --sub-stream 2

All of these result in broken pipe. I would think it's a camera issue, except it's happy to stream to a file on disk and does report meta data correct.

Things I've tested

ffmpeg -nostdin -loglevel debug -y -avoid_negative_ts make_zero -fflags +genpts+discardcorrupt -stimeout 15000000 -rtsp_transport tcp -i "rtsp://user:password@10.10.30.11:554/cam/realmonitor?channel=1&subtype=0" -c:v copy -ar 32000 -ac 1 -codec:a aac -b:a 32k -metadata streamname=JwehwodFGzvzGtsW -f flv - | /usr/bin/python3 -m unifi.clock_sync | nc 10.0.0.10 7550

Will output: Stream #0:0, 0, 1/1000: Video: h264 (High), 1 reference frame ([7][0][0][0] / 0x0007), yuvj420p(pc, bt709, progressive, left), 3840x2160 (0x0) [SAR 1:1 DAR 16:9], 0/1, q=2-31, 4 fps, 14.99 tbr, 1k tbn, 90k tbc

EDIT (to add further detail): I tested with a different camera model on my network, Dahua IPC-HDBW4231E-ASE. This is just an HD camera, and I hadn't setup a user for the rtsp yet. Initially I got 401 (Forbidden), and after setting up the user, same broken pipe error.

I also attemped to use Onvif, this also does not change the broken pipe error.

AliMickey commented 2 years ago

I forgot to mention originally, the streams work fine in VLC as well as home-assistant.

nicoleise commented 2 years ago

@AliMickey (and others reading) - I've got it working finally. It seems the critical thing for me was the FPS. While it's not documented anywhere, it seems this must be 15 FPS or more. See #194 for more detail. I hope that helps you as well.

AliMickey commented 2 years ago

Seems my NVR only supports outputting 10FPS.

Thanks for the clarification.