In Android, RTCView goes black after phone goes to sleep

Hi all,

Been working with React Native WebRTC for almost a year now, and I have a neat little app. However, when I leave the app running and put the phone down (when not streaming), the phone will go to sleep, and the RTCView view will go black. This would be fine, if only I knew how to recycle the RTC view to get back my connection.

It seems like the peer connection is fine, it’s just the camera that goes away!

Running on a recent pixel version, everything else is stellar. Thanks for reading and thanks for working on this great software.

I’ll bet this is related:

From plutoless:

on android you will need to call disableVideo when going to background mode, while you will need to call enableVideo when coming back to foreground mode. If you want to keep camera working you will need to use foreground service, otherwise you will need to follow above guidance

Will need to figure out how to call disableVideo and enableVideo when coming back. How hard can that be?

Just giving an update - no luck so far.

You must have foreground service to running camera while background.
Do you try to send event to JS to pause camera(using SCREEN_TURN_OFF boardcast event on native), only active it’s while resume app

I think I fixed it! When getting the MediaStream to set up the peer connection context, you can walk through the tracks. I attached a handler to the AppState to handle any changes to app state- if the react-native AppState changes to ‘background’ or ‘inactive’, then disable the tracks with a simple track.enabled = false, and if the AppState goes to ‘active’, then enable the tracks by setting that bit to true.

This appears to have come about because I manage the PeerConnection in a context manager, so I have a peer connection set up even when not broadcasting. Now I get the benefit of a stable peer connection, and also an app that can be backgrounded and still work on android!