roleoroleo / yi-hack_ha_integration

Home Assistant custom integration for Yi cameras: yi-hack-MStar, yi-hack-Allwinner, yi-hack-Allwinner-v2, yi-hack-v5 and sonoff-hack
GNU General Public License v3.0
221 stars 32 forks source link

Partial functioning #5

Closed jeroen85 closed 3 years ago

jeroen85 commented 3 years ago

Hi, I have installed the latest version of the integration (HA docker on Synology).

Screenshot 2021-04-25 at 10 35 54

The snapshot is showing correctly. But when opening the stream, it does not load and generates the following error in the logs:

Logger: aiohttp.server
Source: custom_components/yi_hack/camera.py:200
First occurred: 10:30:39 AM (2 occurrences)
Last logged: 10:32:55 AM

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
    resp = await self._request_handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_app.py", line 499, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 56, in security_filter_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 18, in request_context_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 74, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 135, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 131, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/camera/__init__.py", line 498, in get
    return await self.handle(request, camera)
  File "/usr/src/homeassistant/homeassistant/components/camera/__init__.py", line 533, in handle
    return await camera.handle_async_mjpeg_stream(request)
  File "/config/custom_components/yi_hack/camera.py", line 200, in handle_async_mjpeg_stream
    await stream.close()
  File "/usr/local/lib/python3.8/site-packages/haffmpeg/core.py", line 158, in close
    await self._loop.run_in_executor(None, _close)
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.8/site-packages/haffmpeg/core.py", line 153, in _close
    self._proc.stdin.write(b"q")
BrokenPipeError: [Errno 32] Broken pipe

I have auth setup on my camera

Firmware Version | 0.2.5
Base Version | 8.2.0.0A_201912270941
Model Suffix | y20ga
roleoroleo commented 3 years ago

Are you using authentication?

jeroen85 commented 3 years ago

Correct. Just checked: If I disable authentication, it is working.

roleoroleo commented 3 years ago

I will check the problem.

roleoroleo commented 3 years ago

Fixed: https://github.com/roleoroleo/yi-hack_ha_integration/commit/bb95afe02ac679fa59852a29630cf0bdccf13be5

vin-w commented 3 years ago

Big thanks to this fix! I want to say this is the first time I could see the live stream of my yi-hack in HASS!

p.s. I have enabled swap in my yi cam, but there is still some delay and constantly dropping frame (update per 5 sec) but it's pretty good to me already :)

Load Average | 2.70 1.36 0.54
Free/Total Memory | 32664/61164 KB
Free SD Space | 5%

/cgi-bin/get_configs.sh?conf=system
"RTSP":"yes",
"RTSP_STREAM":"high",
"RTSP_AUDIO":"alaw",
"ONVIF":"no",
"ONVIF_WSDD":"no",
"ONVIF_PROFILE":"high",
"ONVIF_WM_SNAPSHOT":"no",
roleoroleo commented 3 years ago

A couple of seconds delay is normal, dropped frames no. Is your wifi connection ok?

jeroen85 commented 3 years ago

Got a stream! But not working perfectly.

Screenshot 2021-04-27 at 11 01 42

I have also set it up in HASS via another route (see below config in configuration.yaml). I use the still image directly from the camera. The stream I got via Synology Surveillance Station, because a while ago I was not able to get it working via the rtsp url from the camera...

This stream gives a much better result at the moment.

camera:
  - platform: generic
    still_image_url: "http://192.168.2.8:8080/cgi-bin/snapshot.sh?res=high&watermark=yes"
    stream_source: "rtsp://syno:2878bde57b6f13b5998684546855d1c7@192.168.2.1:554/Sms=1.unicast"
    name: "Kamer Arthur"
    verify_ssl: false
    username: "xxxx"
    password: "xxxx"
    authentication: basic
roleoroleo commented 3 years ago

I have the same effect when I scroll the page during the stream. I think it's not a problem of the cam.

jeroen85 commented 3 years ago

It seems much better now after running for a while... Looks good!

vin-w commented 3 years ago

A couple of seconds delay is normal, dropped frames no. Is your wifi connection ok?

Wifi is good. Dropping frame: what I observed is for 1 min, there are 5-6 times dropping frame, each time is 3 sec missed. (I observed with the timestamp on the top LHS of the camera picture)

After some investigation, I think that the problem could be due to hass ffmpeg process. When you open the live streaming with yi-hack cam (y20ga), the ffmpeg process is over 100%. I am running Raspberry Pi 4 using docker (not hass.io)

Did you guys observe high CPU in hass too?

Yi-hack resources looks ok

WiFi Strength    100 %
Load Average    2.10 2.07 1.79
Free/Total Memory   22232/61164 KB
Free SD Space   4%

Cmd line called by hass, I dont understand why the argument is mjpeg

ffmpeg -i rtsp://xx:xx@my_ip:554/ch0_0.h264 -an -c:v mjpeg -rtsp_transport tcp -f mpjpeg -

ffmpeg version from hass docker

bash-5.0# ffmpeg
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3.0 (Alpine 9.3.0)
  configuration: --prefix=/usr --enable-avresample --enable-avfilter --enable-gnutls --enable-gpl --enable-libass --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libxvid --enable-libx264 --enable-libx265 --enable-libtheora --enable-libv4l2 --enable-libdav1d --enable-postproc --enable-pic --enable-pthreads --enable-shared --enable-libxcb --enable-libssh --disable-stripping --disable-static --disable-librtmp --enable-vaapi --enable-vdpau --enable-libopus --disable-debug
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

top cmd result, outside docker (cpu 111)

root     29881  111  2.2 168528 87892 ?        Rl   21:35   2:09 ffmpeg -i  rtsp://xx:xx@my_ip:554/ch0_0.h264 -an -c:v mjpeg -rtsp_transport tcp -f mpjpeg -
jeroen85 commented 3 years ago

CPU usage (docker within synology NAS) is about 20-25%

When RTSP stream is loaded via the generic option, the cpu usage stays very low.

camera:
  - platform: generic
    still_image_url: "http://192.168.2.8:8080/cgi-bin/snapshot.sh?res=high&watermark=yes"
    stream_source: "rtsp://syno:2878bde57b6f13b5998684546855d1c7@192.168.2.1:554/Sms=1.unicast"
    name: "Kamer Arthur"
    verify_ssl: false
    username: "xxxx"
    password: "xxxx"
    authentication: basic

But apparently this is loaded/shown in another way. The camera live view has some controls and full screen mode etc.

Edit: I see this is changed by the latest pull request from foxel. Works very well now! And CPU usage dropped significantly.