moritzmhmk / homebridge-camera-rpi

raspberry pi camera plugin for homebridge
MIT License
159 stars 29 forks source link

Does not detect disconnects #46

Open paulz opened 5 years ago

paulz commented 5 years ago

Thank you for the great component!

When HomeKit connection is lost the video stream is still getting sent. This prevents me from reconnecting to the camera, as the camera device is already busy.

How to test:

  1. Connect the Raspberry Pi over ssh to monitor logs: journalctl --follow -u hap-camera-rpi.service -b
  2. Connect to the camera on iPhone and see LIVE orange sign in the corner indicating the live stream
  3. Observe that log is showing ffmpeg decoded frames like:
    Jan 16 19:55:20 cameraberry node[700]: ffmpeg frame=16489 fps= 30 q=-1.0 size=   20211kB time=00:09:09.27 bitrate= 301.4kbits/s speed=   1x
    Jan 16 19:55:21 cameraberry node[700]: ffmpeg frame=16505 fps= 30 q=-1.0 size=   20262kB time=00:09:09.80 bitrate= 301.9kbits/s speed=   1x
    Jan 16 19:55:21 cameraberry node[700]: ffmpeg frame=16520 fps= 30 q=-1.0 size=   20266kB time=00:09:10.30 bitrate= 301.7kbits/s speed=   1x
    Jan 16 19:55:22 cameraberry node[700]: ffmpeg frame=16535 fps= 30 q=-1.0 size=   20271kB time=00:09:10.80 bitrate= 301.5kbits/s speed=   1x
    Jan 16 19:55:22 cameraberry node[700]: ffmpeg frame=16550 fps= 30 q=-1.0 size=   20286kB time=00:09:11.30 bitrate= 301.4kbits/s speed=   1x
  4. Put iPhone into the airplane mode, naturally video stream stops.
  5. Observe that log is still showing frames output even there is no connection to the iPhone
  6. Turn airplane mode off on iPhone
  7. Open camera on iPhone HomeKit and notice "No Response" message - "This camera is not responding"

What I was expecting after the iPhone goes offline frames stop and when iPhone goes back online video stream resumes.

paulz commented 5 years ago

Also notice that log is showing that the device is busy when trying to watch the camera again: Jan 16 19:59:29 cameraberry node[700]: ffmpeg /dev/video0: Device or resource busy

paulz commented 5 years ago

To workaround hanging video stream issue I have to ssh to the raspberry pi and restart the service:

sudo systemctl stop hap-camera-rpi
sudo systemctl start hap-camera-rpi
paulz commented 5 years ago

To give more context I am using standalone mode on Raspberry Pi Zero W, following instructions on wiki.

I've tried both versions of ffmpeg:

  1. sudo apt install ffmpeg
  2. Rebuild ffmpeg using hardware acceleration using these instructions: https://github.com/legotheboss/YouTube-files/wiki/(RPi)-Compile-FFmpeg-with-the-OpenMAX-H.264-GPU-acceleration which gave me much better CPU utilization (from 60% down to 5%)
paulz commented 5 years ago

The same issue of hanging unused video stream can also be reproduced for me by walking with iPhone between access points, even on the same network. Similar to what is described in https://github.com/moritzmhmk/homebridge-camera-rpi/issues/28

paulz commented 5 years ago

As a workaround, I am restarting the standalone service every 5 minutes to stop hanging video stream: by adding RuntimeMaxSec=300 parameter.

cat /etc/systemd/system/hap-camera-rpi.service

[Unit]
Description=HAP Camera RPi

[Service]
ExecStart=/usr/local/bin/node /opt/homebridge-camera-rpi/standalone.js -c /etc/homebridge-camera-rpi.conf.json
WorkingDirectory=/opt/homebridge-camera-rpi
Restart=always
RestartSec=10
RuntimeMaxSec=300
User=pi

[Install]
WantedBy=multi-user.target
michaelbublat commented 5 years ago

Same issue here. With the homebridge (not standalone) version. This bug makes this (very nice plugin) almost unusable :/