keshavdv / unifi-cam-proxy

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

ffmpeg stderr pipe fills up, bricking the video stream #148

Closed ascott18 closed 2 years ago

ascott18 commented 2 years ago

Caused by the change in 68237c59b.

The stderr buffer will fill up after a while, causing either 1) The ffmpeg process to crash with a Broken Pipe error (which it recovers from), or 2) The ffmpeg process just stops. Unifi keeps sending ChangeVideoSettings messages to restart the stream, but unifi-cam-proxy thinks ffmpeg is still running and so does not spawn a new process.

Some ideas: 1) Add -loglevel error to the baseline ffmpeg args. 2) Dont pipe stderr anywhere - just let it pass through. 3) Continuously read from stderr, manually buffering the last X lines of output.

Broken pipe error (trimmed the output, but it was right at 2^16 characters, which is the pipe max capacity):

[AVBSFContext @ 0x7fffbf19f1c0] Warning: low_delay_hrd_flag does not match inferred value: 1, but should be 0.
frame= 3843 fps= 15 q=-1.0 size=    1794kB time=00:04:16.20 bitrate=  57.4kbits/s speed=1.01x
frame= 3850 fps= 15 q=-1.0 size=    1796kB time=00:04:16.71 bitrate=  57.3kbits/s speed=1.01x
frame= 3859 fps= 15 q=-1.0 size=    1797kB time=00:04:17.16 bitrate=  57.3kbits/s speed=1.01x
frame= 3866 fps= 15 q=-1.0 size=    1799kB time=00:04:17.74 bitrate=  57.2kbits/s speed=1.01x
[AVBSFContext @ 0x7fffbf19f1c0] Warning: low_delay_hrd_flag does not match inferred value: 1, but should be 0.
frame= 3874 fps= 15 q=-1.0 size=    1808kB time=00:04:18.25 bitrate=  57.4kbits/s speed=1.01x
frame= 3881 fps= 15 q=-1.0 size=    1810kB time=00:04:18.75 bitrate=  57.3kbits/s speed=1.01x
frame= 3889 fps= 15 q=-1.0 size=    1811kB time=00:04:19.26 bitrate=  57.2kbits/s speed=1.01x
frame= 3897 fps= 15 q=-1.0 size=    1813kB time=00:04:19.79 bitrate=  57.2kbits/s speed=1.01x
[AVBSFContext @ 0x7fffbf19f1c0] Warning: low_delay_hrd_flag does not match inferred value: 1, but should be 0.
frame= 3902 fps= 14 q=-1.0 size=    1822kB time=00:04:20.04 bitrate=  57.4kbits/s speed=0.92x
[AVBSFContext @ 0x7fffbf19f1c0] Warning: low_delay_hrd_flag does not match inferred value: 1, but should be 0.
Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/mnt/c/src/unifi-cam-proxy/unifi/clock_sync.py", line 167, in <module>
    main()
  File "/mnt/c/src/unifi-cam-proxy/unifi/clock_sync.py", line 157, in main
    write(read_bytes(source, payload_size))
  File "/mnt/c/src/unifi-cam-proxy/unifi/clock_sync.py", line 87, in write
    sys.stdout.buffer.write(data)
BrokenPipeError: [Errno 32] Broken pipe
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe
    Last message repeated 4 times
av_interleaved_write_frame(): Broken pipe
[flv @ 0x7fffbf034f00] Failed to update header with correct duration.
[flv @ 0x7fffbf034f00] Failed to update header with correct filesize.
Error writing trailer of pipe:: Broken pipe
frame= 4070 fps= 14 q=-1.0 Lsize=    1898kB time=00:04:31.23 bitrate=  57.3kbits/s speed=0.959x
video:1266kB audio:517kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 6.445845%
Conversion failed!
tsspmq commented 2 years ago

I took your pull and manually added it to mine due to having the exact issue you brought up. So far, so good.