11

I am trying to do WebRTC, all is working fine but there seems to be an issue, that is, if the screen remains off for more than a minute the audio stops recording, meaning the audio from device stops until I switch the screen on once again.

What I have tried?

1) I have tried setting webSettings.setMediaPlaybackRequiresUserGesture(false); it does no good to the problem.

2) I have also tried adding a wakelock in the activity in which I am doing WebRTC but it also didn't work.

Here are the permissions declared in Manifest:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

Here in activity, I am granting permission for the microphone in WebChromeClient:

@Override
public void onPermissionRequest(final PermissionRequest request) {
    request.grant(request.getResources());
}

What I want?

I want to be able to continue call without disrupting the user to turn screen back on again. Please point me in right direction.

Thanks!

Update: I tried loading the WebRTC url in Chrome and the same thing is happening, that is, audio stops recording from my device.

Update 2: Adding log when audio stops coming from the device.

2019-08-06 17:18:47.266 4332-22405/? V/APM_AudioPolicyManager: getAudioPolicyConfig: audioParam;outDevice
2019-08-06 17:18:47.266 4332-22405/? V/APM_AudioPolicyManager: getNewOutputDevice() selected device 2
2019-08-06 17:18:47.266 4332-22405/? V/APM_AudioPolicyManager: ### curdevice : 2
2019-08-06 17:18:47.307 4332-22405/? V/APM_AudioPolicyManager: AudioPolicyManager:setRecordSilenced(uid:99066, silenced:1)
2019-08-06 17:18:47.308 4332-22405/? V/APM_AudioPolicyManager: AudioPolicyManager:setRecordSilenced(uid:11556, silenced:1)

Update 3: Tried initializing WebView in a Foreground Service still same result.

Update 4: Tried a demo call on https://appr.tc/ using Chrome(76.0.3809.132). Observed the same result.

Update 5: Tried a demo call using Firefox and it worked FLAWLESSLY which lets me thinking that is it a Chromium bug?

Update 6: Filled a bug report

Mustansir
  • 2,445
  • 1
  • 21
  • 32
  • (1) Open “Settings” (2) Tap on “Battery” (3)“Close apps after screen lock” (4) Scroll down to “xxxxx” - switch to “Don't close” – Jon Goodwin Aug 26 '19 at 23:29
  • @JonGoodwin Thanks for your comment, but I don't see as a cross platform solution, as this option is missing from my device(Samsung Galaxy S9) – Mustansir Aug 27 '19 at 15:57
  • Over the years Google has discouraged the domination of one apps dominion on resources over another ( and only allowed with kernel based calls for those providers who pay for it...facebook et. al.). You **CANNOT** do what you want now, or in the future, without severe restrictions, or paying google (and be rich !). Many ways to fail, one way to win, pay. – Jon Goodwin Aug 30 '19 at 20:25
  • @JonGoodwin tried WebRTC using Firefox, it worked as expected!! – Mustansir Aug 31 '19 at 06:09
  • I don't know about WebRTC but have you tried wake lock and wifi lock? – Sumit Shukla Aug 31 '19 at 06:28
  • @SumitShukla I already tried using a `WakeLock`, but I don't see a point in using `WifiLock` cause I am already using mobile data in first place. – Mustansir Aug 31 '19 at 06:34

5 Answers5

2

Check your Chrome/Android version due to this issue with WebRTC on Android: Issue 513633: WebRTC call in Chrome on Android will be cut off 1 min after screen off

WebRTC is supported by default in Chrome so... it should work.

BTW, if you dont't need WebRtc or want try to implement in a background service...

Interest readings:

1 - recording-when-screen-off

As the post says, keep in mind:

To call:

startForeground();

Use START_STICKY:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
       return START_STICKY;
}

2 - how to implement a recorder

As the post says, keep in mind permissions:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Background services with Apache Cordova

With cordova and a webview you need to use a plugin to run code in background as a service too. Take a look at this link: cordova plugin

Another choice with Cordova is to do your own plugin like this:

custom plugin - background video recorder

Ariel Carrera
  • 5,113
  • 25
  • 36
  • Hello, thanks for your response. But I am trying to do the call using WebView. – Mustansir Aug 29 '19 at 07:34
  • Hi @Mustansir, I added a note to the post... use this cordova plugin to run as a service in background. – Ariel Carrera Aug 29 '19 at 11:15
  • Can you please explain it more? Thing is I need to show user controls which are present on that site. I cannot run anything in background. – Mustansir Aug 29 '19 at 11:45
  • No problem, your activity may be paused and resumed, not only destroyed. So, your recording is interrupted. – Ariel Carrera Aug 29 '19 at 12:18
  • Hi, I just tried running webview in a service to eliminate the element of activity. I was successfully able to make a call, but the same thing repeated. Voice stopped coming from device. – Mustansir Aug 29 '19 at 13:34
  • What version of chrome are you testing? Do you read about this Chrome issue with WebRTC? https://bugs.chromium.org/p/chromium/issues/detail?id=513633 – Ariel Carrera Aug 29 '19 at 14:37
  • Using Chrome 76.0.3809.132 I tried to do a demo call on https://appr.tc/ on Chrome and observed the same result. – Mustansir Aug 30 '19 at 06:50
2

Android will automatically destroy your activity on a few minutes after leaving foreground that will cause the audio recording to turn off.

I have working with webrtc on android, if you want to create call and video call with webrtc on Android, I suggest to use native webrtc and implement everything related to webrtc on foreground service. Foreground service will ensure your recorder and camera to keep running event when activity is destroyed.

For reference, here the google sample for implementing webrtc native https://webrtc.googlesource.com/src/+/master/examples/androidapp/src/org/appspot/apprtc

flx_h
  • 101
  • 3
  • Sorry but for this project I am limited to use only Webview. – Mustansir Aug 29 '19 at 11:08
  • I am facing this problem. https://stackoverflow.com/questions/62633876/use-microphone-in-webrtc-media-recorder-simultaneously-in-android Help me to resolve this? – Gopinathan B Jun 29 '20 at 11:18
2

You should work on keeping the screen on in that activity during the call and prevent if from dimming.

Use this:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

and after your call is done:

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Ali Ben Zarrouk
  • 1,891
  • 16
  • 24
2

Obviously, it's no usual task, because all your implementation, it's just a WebView. Which very hard to align with such long living task and lifecycle inside Android. For ex. every VoIP application we did, had services in background, to keep connection alive, with wake locks. This was only one way to ensure about stability of the call.

However, I think you could try to do the same. By managing your WebView work inside Service. For this purpose, you could consider moving some calling logic into another view, and starting new Service and creation new Window. This will ensure your Window will be alive, during all the lifecycle of the Service. Smth like.

public class ServiceWithWebView extends Service {

    @Override
    public void onCreate() {
        super.onCreate();

        final WindowManager windowManager = (WindowManager) 
                    getSystemService(WINDOW_SERVICE);
        params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, 
                    WindowManager.LayoutParams.WRAP_CONTENT, 
                    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);

        final LinearLayout view = new LinearLayout(this);
        view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout
              .LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

        WebView wv = new WebView(this);
        wv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout
                 .LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        view.addView(wv);
        wv.loadUrl("http://google.com");

        windowManager.addView(view, params);
    }
}
GensaGames
  • 5,538
  • 4
  • 24
  • 53
1

It is possible that the problem is in battery optimization. The device cleans up the background processes and finds there your audio recording screen working on the background. try to add the app to the list of Battery Best Performance list. Search how to do that on your device.

For my case even important background tasks as accessibility service is forced to stop under that battery optimization algorithm. To allow my service to work all the time, the user should add the app to the whitelist of battery best performance rule.

I hope it can help you.

RelaxedSoul
  • 644
  • 4
  • 10