beele / homebridge-unifi-protect-camera-motion

Camera & Motion sensor support for Unifi Protect cameras in Homekit via Homebridge
Apache License 2.0
103 stars 12 forks source link

Streaming Taking Long Time to Start #69

Closed shnhrrsn closed 4 years ago

shnhrrsn commented 4 years ago

Describe the bug

I'm opening this separate from #62 because it doesn't actually seem to be an issue with ffmpeg.

Basically, after I open the request in Home.app (on either iOS or macOS), it seems to take 30+ seconds before the plugin even acknowledges there was a request to open.

I've previously used a different UniFi plugin without this issue (migrated when upgrading to Protect) and my Ring doesn't have the problem either -- it seems specific to this plugin.

This may or may not be a dupe of #58, but sounds like a similar issue.

Specifications What hardware and OS is your Homebridge running on?: Tested with homebridge running on macOS and Debian What Unifi hardware are you using: UDM-Pro, 3x G3, 1x G3 Dome, 1x G4 Doorbell

beele commented 4 years ago

I use the same logic as the base homebridge camera ffmpeg plugin. My video streams load anywhere between 5-15 seconds, which I would deem acceptable. Please provide a detailed log with the debug flag enabled so the ffmpeg debug output is visible.

JJJ commented 4 years ago

It takes about 2 seconds on my Raspberry Pi 4 4GB, running the Homebridge OS image.

shnhrrsn commented 4 years ago

@beele there’s really nothing useful in the logs which is why I didn't include them. If I clear logs before opening the stream in Home.app, the logs will be empty for 20-30s after I tap the camera in Home.app before finally kicking off the ffmpeg request. At that point, assuming the app hasn’t given up, everything works fine.

JJJ commented 4 years ago

Back when I was seeing slow ffmpeg times on my old Raspberry Pi 3 with Nest cameras, it was related to the order with which I had compiled and set everything up. Namely that I ended up with multiple versions of it installed in different directories because I had followed different tutorials and instructions, one of which was not configured to take advantage of hardware acceleration on the Raspberry Pi, using the wrong protocol, was missing libfdk-aac, and more.

Can you confirm that this plugin is finding and using the correct copy of ffmpeg, and can you copy its configuration here. You can get it via the command line simply by typing:

ffmpeg

Mine looks like this:

ffmpeg version 4.2.git-47773f7 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 6.3.0 (Raspbian 6.3.0-18+rpi1+deb9u1) 20170516
  configuration:
    --pkgconfigdir=/build/workspace/lib/pkgconfig
    --prefix=/build/workspace
    --pkg-config-flags=--static
    --extra-cflags=-I/build/workspace/include
    --extra-ldflags=-L/build/workspace/lib
    --extra-libs='-lpthread -lm'
    --enable-static
    --disable-debug
    --disable-shared
    --disable-ffplay
    --disable-doc
    --enable-openssl
    --enable-gpl
    --enable-version3
    --enable-nonfree
    --enable-pthreads
    --enable-libvpx
    --enable-libmp3lame
    --enable-libopus
    --enable-libtheora
    --enable-libvorbis
    --enable-libx264
    --enable-runtime-cpudetect
    --enable-libfdk-aac
    --enable-avfilter
    --enable-libopencore_amrwb
    --enable-libopencore_amrnb
    --enable-filters
    --enable-decoder=h264
    --enable-network
    --enable-protocol=tcp
    --enable-demuxer=rtsp
    --enable-omx-rpi
    --enable-mmal
  libavutil      56. 40.100 / 56. 40.100
  libavcodec     58. 68.102 / 58. 68.102
  libavformat    58. 38.100 / 58. 38.100
  libavdevice    58.  9.103 / 58.  9.103
  libavfilter     7. 75.100 /  7. 75.100
  libswscale      5.  6.100 /  5.  6.100
  libswresample   3.  6.100 /  3.  6.100
  libpostproc    55.  6.100 / 55.  6.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
janthony6 commented 4 years ago

I'm having trouble streaming on my Pi4 on my UDMP. My ffmpeg shows this:

ffmpeg version N-94452-g9af8ce754b Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8 (Raspbian 8.3.0-6+rpi1)
  configuration: --prefix=/usr/local --arch=armel --target-os=linux --enable-omx-rpi --enable-nonfree --enable-gpl --enable-libfdk-aac --enable-mmal --enable-libx264 --enable-decoder=h264 --enable-network --enable-protocol=tcp --enable-demuxer=rtsp
  libavutil      56. 33.100 / 56. 33.100
  libavcodec     58. 55.100 / 58. 55.100
  libavformat    58. 30.100 / 58. 30.100
  libavdevice    58.  9.100 / 58.  9.100
  libavfilter     7. 58.100 /  7. 58.100
  libswscale      5.  6.100 /  5.  6.100
  libswresample   3.  6.100 /  3.  6.100
  libpostproc    55.  6.100 / 55.  6.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
janthony6 commented 4 years ago

Recompiled with:

ffmpeg version N-98757-ge8a88a16f7 Copyright (c) 2000-2020 the FFmpeg developers built with gcc 8 (Raspbian 8.3.0-6+rpi1) configuration: --extra-cflags=-I/usr/local/include --extra-ldflags=-L/usr/local/lib --extra-libs='-lpthread -lm -latomic' --arch=armel --enable-gmp --enable-gpl --enable-libaom --enable-libass --enable-libdav1d --enable-libdrm --enable-libfdk-aac --enable-libfreetype --enable-libkvazaar --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libssh --enable-libvorbis --enable-libvpx --enable-libzimg --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-mmal --enable-nonfree --enable-omx --enable-omx-rpi --enable-version3 --target-os=linux --enable-pthreads --enable-openssl --enable-hardcoded-tables libavutil 56. 58.100 / 56. 58.100 libavcodec 58.100.100 / 58.100.100 libavformat 58. 51.100 / 58. 51.100 libavdevice 58. 11.101 / 58. 11.101 libavfilter 7. 87.100 / 7. 87.100 libswscale 5. 8.100 / 5. 8.100 libswresample 3. 8.100 / 3. 8.100 libpostproc 55. 8.100 / 55. 8.100 Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

beele commented 4 years ago

If you start homebridge, the plugin should print out the path of the ffmpeg binary that is being used. Can you confirm this is using your compiled version. Also the latest beta: homebridge-unifi-protect-camera-motion@0.4.3-beta.3 has some changes in the way video & snapshots are handled. sudo npm i -g homebridge-unifi-protect-camera-motion@0.4.3-beta.3 --unsafe-perm=true

The video config section for the plugin should be adjusted to this:

"videoConfig": {
    "audio": true,
    "maxFPS": 30,
    "maxBitrate": 3000
}
janthony6 commented 4 years ago

The new beta fixed all the ffmpeg issues. It now streams and works correctly. Sound works perfect too.

shnhrrsn commented 4 years ago

@beele closing this out -- not your fault, I figured out the issue though for others who may experience this:

HomeKit seems to load everything sequentially (unsure if this is a HAP/Homebridge issue or HomeKit itself). If you open a section in Home.app like favorites with 6 cameras on it and immediately open a stream, the plugin doesn't get the request for the stream until after all 6 snapshots have loaded.

In my case, one of my cameras is slow, which added to the delay.

Interestingly, this seems to be hub-specific (which may mean it's a HAP/Homebridge limitation?). My ring plugin wasn't suffering this because I have it in a separate homebridge instance.