home-assistant / android

:iphone: Home Assistant Companion for Android
https://companion.home-assistant.io/
Apache License 2.0
2.24k stars 626 forks source link

Camera traffic, when app in background. #979

Open nick2525 opened 3 years ago

nick2525 commented 3 years ago

Home Assistant Android version: 2.4.1-full

Android version: 10 Phone model: Xiaomi mi 9 Home Assistant version: 0.115.3 Last working Home Assistant release (if known): no Description of problem:

When app in background, video streams from cameras does not stop stream. Almost all apps stops video stream at onPause() Android lifecycle, but HA does not. Now the problem is even more serious, because from the activities of the sensors, the system almost never kills the application. I have a lot of cameras and I wasted 70 gigabytes in a month because of this bug. Plus the battery runs out quickly. Traceback (if applicable):

Screenshot of problem: Screenshot_2020-09-25-18-25-18-432_com miui securitycenter

Additional information: Solution: You should stop stream, when device goes onStop(), maybe communicate with HA server, and show video placeholder.

dshokouhi commented 3 years ago

You need to check that "automatically close connections" in your HA user profile is enabled. The frontend actually handles this part as we are just a webview so they look to see if you have gone idle.

You should also check that you are not going to the home screen when you have the more info panel open, another offender in this case is if you use the live view card on lovelace which plays video on the homescreen directly.

nick2525 commented 3 years ago

1) I checked "automatically close connections" - "Should we close the connection to the server after being hidden for 5 minutes?" - was on, but it seems 5 minutes too much for mobile. 2) I use card on lovelace which plays video on the homescreen directly - as it is nice feature. 3) Yes, HA is Webview, but HA is not website, here you can control server or frontend, and forward iOS /Android lifecycle to frontend, maybe even to backend, to lower the number of frames in the video, or use still image when APP is onStop() or onPause()

barrymossel commented 3 years ago

This started for me too after (one of the) latest update(s). Just spend 2,5 GB in 10 minutes. Never experienced this before.

JBassett commented 3 years ago

@nick2525 and @barrymossel can you please include your yaml camera config and frontend config so we can attempt to recreate. I have tried with my personal setup and I am unable to get the app to stream in the background.

nick2525 commented 3 years ago

camera:

camera 3:

camera 4:

camera 5:

camera 6:

nick2525 commented 3 years ago

{ "background": "radial-gradient(crimson, skyblue)", "cards": [ { "camera_view": "live", "entity": "camera.children_camera", "title": "\u041a\u043e\u0440\u0438\u0434\u043e\u0440", "type": "picture-entity" }, { "camera_view": "live", "entity": "camera.child_room_camera", "title": "\u0441\u043f\u0430\u043b\u044c\u043d\u044f", "type": "picture-entity" }, { "camera_view": "live", "entity": "camera.kitchen_camera", "title": "\u043a\u0443\u0445\u043d\u044f", "type": "picture-entity" }, { "camera_view": "live", "entity": "camera.slipping_camera", "title": "\u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0430", "type": "picture-entity" }, { "camera_view": "live", "entity": "camera.slipping_room_camera", "title": "\u0441\u043f\u0430\u043b\u044c\u043d\u044f", "type": "picture-entity" } ], "id": "cameras", "theme": "dark-mode", "title": "\u043a\u0430\u043c\u0435\u0440\u044b" }

barrymossel commented 3 years ago
camera:
  # - platform: foscam
    # ip: !secret ip_camera_kleren
    # name: Klerenkamer
    # username: !secret camera_kleren_username
    # password: !secret camera_kleren_password
  # - platform: ffmpeg
    # name: Lise
    # input: !secret url_camera_lise
  - platform: mjpeg
    name: Sophie
    mjpeg_url: http://192.168.1.43:8081/
  - platform: mjpeg
    name: Lise
    mjpeg_url: http://192.168.1.43:8082/
  - platform: mjpeg
    name: Hal
    mjpeg_url: http://192.168.1.43:8083/

The ones that were commented out are my old camera's which didn't have this problem. When I "updated" those the traffic started getting out of hand. Did some more investigation: I use MotionEye as when I load the rtsp feeds directly into HA I get a lag ranging from 4 to 60 sec. When I load the rtsp feed into MotionEye and let that process and stream it, there is virtually no lag (max 1 sec). But apparently the feeds use way too much bandwidth. When I load the stream url into a browser it uses lot's of data as well. So that's one of the issues for me. But now I turned the quality of the feeds to 25% and it still uses 100's of MB's per couple of minutes. Way more than the original feed passing through the Ewelink app (camera's are Sonoffs). But still, I believe the old camera's didn't do any of this anyway. Never noticed something weird on the data usage side of the HA app... So, could be two things I guess: very small feeds with low data usage, or no streaming in background.

And btw, if I force the app to close it doesn't stream and use data. If I just go back to Android home screen it does use bandwidth.

barrymossel commented 3 years ago

Lovelace:

          - type: picture-glance
            title: Sophie
            camera_image: camera.sophie
            camera_view: live
            aspect_ratio: 16:9
            entities:
              - binary_sensor.deursensor_sophie_contact      
              - light.sophie       
          - type: picture-glance
            title: Lise  
            camera_image: camera.lise
            camera_view: live
            aspect_ratio: 16:9
            entities: 
              - binary_sensor.deursensor_lise_contact
              - light.lise
bram2202 commented 3 years ago

I got the same issue (#1017) I also happen to have a Xiaomi mi 9(T pro)

HA config:

- platform: generic
  name: Hallway
  still_image_url: !secret camera_hallway_snap_url
  verify_ssl: false

- platform: generic
  name: Street
  still_image_url: !secret camera_street_snap_url
  verify_ssl: false

- platform: generic
  name: Livingroom
  still_image_url: !secret camera_livingroom_snap_url
  verify_ssl: false

Lovelace:

    cards:
      - aspect_ratio: 0%
        camera_image: camera.hallway
        camera_view: live
        entity: camera.hallway
        show_name: false
        show_state: false
        type: picture-entity
      - aspect_ratio: 0%
        camera_image: camera.livingroom
        camera_view: live
        entity: camera.livingroom
        show_name: false
        show_state: false
        type: picture-entity
      - aspect_ratio: 0%
        camera_image: camera.street
        camera_view: live
        entity: camera.street
        show_name: false
        show_state: false
        type: picture-entity
nick2525 commented 3 years ago

My wife has Xiaomi mi 9T, the bug is also reproduced.

ericlathrop commented 3 years ago

I just got burned by this, wasted almost 1GB of my data plan. Using the Zoneminder integration, and Picture Glance Cards.

zoneminder:
  - host: zoneminder.mydomain.com
    ssl: true
    username: x
    password: x

sensor:
  - platform: zoneminder
    include_archived: false

camera:
  - platform: zoneminder
bramkragten commented 3 years ago

Fix: https://github.com/home-assistant/frontend/pull/7362

nick2525 commented 3 years ago

Was it fixed in 0.117.0b1? Because the bug is still being reproduced(

dshokouhi commented 3 years ago

@nick2525 can you verify the frontend version you are on please?

nick2525 commented 3 years ago

Frontend version: 20201021.1 - latest

nick2525 commented 3 years ago

Screenshot_2020-10-22-20-13-52-062_com miui securitycenter

dshokouhi commented 3 years ago

@nick2525 thanks, and you waited 5 minutes for the connection to close right?

dshokouhi commented 3 years ago

BTW it looks like the linked issue was actually not the fix for this. That was the reasoning for the bug closing and being re-opened.

nick2525 commented 3 years ago

Some cameras disconnected after 5 minutes, but live view is not restored on those cameras. But if I pres on camera, stream works. But live view is not restored on app onResume()

dshokouhi commented 3 years ago

live view being restored is unrelated to this issue....this issue is purely about making sure the data flow stops when the app is in the background after 5 minutes.

nick2525 commented 3 years ago

Screenshot_2020-10-22-20-44-29-006_com miui securitycenter It seems, that some camera didn't stop after ten minutes, because It too much data for sensors, but it 8 times less traffic when after one minute in background

dshokouhi commented 3 years ago

what are the results if you disable all your picture entity cards? does it 0 out then after 5 minutes?

nick2525 commented 3 years ago

Screenshot_2020-10-22-21-06-34-703_com miui securitycenter

nick2525 commented 3 years ago

It almost no traffic when I disabled camera view live, and 0.00kb after 7 minutes with disabled camera view live

nick2525 commented 3 years ago

@dshokouhi @bramkragten @JBassett https://github.com/home-assistant/frontend/pull/7362 fixed all cameras, except one which has no stream, and only still_image_url, so 2 frame per second is about 240 KB/s, I disabled/enabled camera view live for each cameras, and only camera with only still_image_url and no stream produced traffic after 8 minutes

"platform: generic
name: child_room_camera
framerate: 2
still_image_url: http://.../webcapture.jpg?command=snap&channel=1"
nick2525 commented 3 years ago

It still reproduced, but on still_image_url only cameras, version | 0.117.2 frontend 20201021.4

bendikrb commented 3 years ago

I can confirm this issue, which exists on both mine and my partners phones - since 0.117 ish up until now (0.118.0b2). The weird thing is that we're seeing data being counted as "foreground", although the app certainly hasn't been open/in use for the duration it would take to transfer this much data.

Most notably today, when it ran away using 1.9GB from 00:00 till about 07:30; Screenshot_20201115-131712_Settings_200

nick2525 commented 3 years ago

@bendikrb Do you have still_image_url camera? Or you have steam?

bendikrb commented 3 years ago

Do you have still_image_url camera? Or you have steam?

One of them is defined using the mjpeg platform, with both still_image_url and mjpeg_url defined;

- platform: mjpeg
  name: front_door_lowres
  username: !secret vivotek_username
  password: !secret vivotek_password
  verify_ssl: false
  still_image_url: !secret vivotek_still_image_url
  mjpeg_url: !secret vivotek_mjpeg_lowres_url

The other one is set up using the onvif integration (using config_flow), and is proxied (this is the entity I'm using in lovelace);

- platform: proxy
  entity_id: camera.backyard_onvif_hires
  name: backyard_hires
  max_stream_height: 1280
  cache_images: true

OT: The reason for proxying is because if I don't, and display the entity directly in lovelace, the ffmpeg parameters i have defined in the onvif settings isn't applied (dewarping of the 180° fisheye lens).

dshokouhi commented 3 years ago

If you are still experiencing this issue after you update to at least BETA 522 please also test once in Chrome and let us know if the issue occurs there as well.

nick2525 commented 3 years ago

It seams it wasn't fixed on Xiaomi mi 9, I installed beta-522-d99daec-full , and ha consume 1.12 mb/s in Xiaomi security app. I don't have spit screen, and Xiaomi security app is resumed, it not transparent. I also tried to open Google translate App after HA, and when Xiaomi security app. Bug 🐛 is reproduced

dshokouhi commented 3 years ago

@nick2525 can you check in chrome and see if you can reproduce there please.

nick2525 commented 3 years ago

Screenshot_2020-11-17-01-55-20-018_com miui securitycenter Same problem on chrome, with only HA tab

nick2525 commented 3 years ago

Screenshot_2020-11-17-02-05-21-016_amirz rootless nexuslauncher traffic from HA when I am at launcher , also traffic when I am on second tab in chrome, and no app started Screenshot_2020-11-17-02-09-45-561_com android chrome

bramkragten commented 3 years ago

How are you exactly doing these tests? Can you make a step by step guide with timing? And what is the config of the problematic dashboard?

dshokouhi commented 3 years ago

@nick2525 also please ensure you are waiting 5 minutes for the connection to close as well, please be precise with your steps when you comment back.

nick2525 commented 3 years ago

https://drive.google.com/file/d/1fD4z951965KTrxWSIvsIXk4jyztIzPFM/view?usp=drivesdk I uploaded video. Now I didn't wait 5 minutes, as though that onStop fix should work immediately. I have already posted my cameras config at beginning of bug

dshokouhi commented 3 years ago

@nick2525 this is in onPause please wait 5 minutes as thats intended behavior. The feeds should stop after the connection closes in 5 minutes, not immediately.

nick2525 commented 3 years ago

Screenshot_2020-11-17-03-04-16-242_amirz rootless nexuslauncher begin Screenshot_2020-11-17-03-09-34-178_amirz rootless nexuslauncher after 5 minutes some cameras stopped stream except still image url cameras. Screenshot_2020-11-17-03-09-45-403_com android systemui nothing changed after last fix home-assistant/frontend#7362 I have WebView implementation Android System WebView 86.0.4240.185

dshokouhi commented 3 years ago

@nick2525 and just to confirm you see the exact same behavior in chrome with the 5 minute wait?

nick2525 commented 3 years ago

Yes, same at Chrome 1.1 mb/s during 5 minutes, 200 kb/s after 5 minutes (some cameras were disconnected after 5 minutes but not all)

bramkragten commented 3 years ago

Ok, do you know what camera was and what camera wasn't stopped?

nick2525 commented 3 years ago

@bramkragten Yes, I removed all cameras and when added again one by one, and such camera wasn't stopped

platform: generic name: slipping_room_camera framerate: 2 still_image_url: http://.../webcapture.jpg?command=snap&channel=1

Mjpeg cameras were stopped, like this platform: mjpeg name: children_camera username: password: mjpeg_url: http://.../live/0/mjpeg.jpg

TRusselo commented 3 years ago

this happened to my father’s phone today with the companion app. 9 GB used in one day. Pixel 4XL He never opens the app. its used for location tracking only. “auto close connection” has been checked, for months with no issue until today. I do have some live view cameras on a lovelace page, but no issue until today on 1 phone, not all.

nick2525 commented 3 years ago

@TRusselo Do your have still image url cameras? I will try also to report bug to Chrome developers

TRusselo commented 3 years ago

I have live view cameras on a lovelace page, page 7. The cameras are live feeds using the BlueIris HACS integration.

Not chrome. Android companion app... or is that built on chrome?

This once happened to my phone back on November, 1 random day where I did not open app that day. Then yesterday to my fathers phone. he never uses the app. Its basically just used for location tracking. Odd how it just happens out of nowhere randomly until our carrier sends us an SMS that we have gone over a limit, or the phone starts burning a hole in our leg.... reboot. gone. fixed. until next time....

barrymossel commented 3 years ago

@nick2525 this is in onPause please wait 5 minutes as thats intended behavior. The feeds should stop after the connection closes in 5 minutes, not immediately.

What's the reason it's intended to stream for 5 minutes after closing? Everything more than 10 seconds seems needless.

dshokouhi commented 3 years ago

What's the reason it's intended to stream for 5 minutes after closing? Everything more than 10 seconds seems needless.

The HA frontend has an auto close connection setting in the user profile that is set to 5 minutes. This is the same behavior for browsers too. The app just respects this. This bug also impacts Chrome as noted in other comments too.

barrymossel commented 3 years ago

I get that, but also for browsers it seems quite long. But could it help to have a different setting in frontend for mobile apps/browsers? Last month I had almost 20GB of background data (luckily through wifi due to corona), which could be dramatically lower if the connection closes after 10 seconds. I would think that people open a mobile view of HA to quickly check something or turn something on, then close the view and don't open the mobile view for quite some time (at least 5 mins).

dshokouhi commented 3 years ago

But could it help to have a different setting in frontend for mobile apps/browsers?

Thats unrelated to the android app but I am sure you can start a discussion about it in the frontend repo because that is where the update will come from.

Last month I had almost 20GB of background data (luckily through wifi due to corona), which could be dramatically lower if the connection closes after 10 seconds.

You can just remove cameras from the home page and keep them on a tab that isn't normally visible and it will greatly reduce the camera stream issue. The issue here is that the camera is the last thing being observed so its kept open. By ensuring the video doesnt even start on the tab it removes the issue. Also as noted in this thread the connection closing didn't solve the issue so we are not sure where it is coming from.