react-native-webrtc / react-native-callkeep

iOS CallKit framework and Android ConnectionService for React Native
ISC License
923 stars 445 forks source link

Microphone issues on locked screen or background for Android device with version 14. #802

Closed CClaudiu26 closed 2 months ago

CClaudiu26 commented 3 months ago

Bug report

Description

Is anyone having issue with microphone when the screen is locked or the app is in background? I didn't have any problem with android 13, but after the update to 14 I had this issue.

Versions

- React Native:  0.73.6
- Android: 14
- Phone model:  Google pixel 6

Logs

Paste here
LukakHR commented 2 months ago

Same issue on Xiaomi here as well as Motorola Moto G84

nero2009 commented 2 months ago

This is a valid issue. Microphone doesn't work when the phone is locked or the app is in the background. It only works when the app is back in the foreground.

This is related to https://developer.android.com/develop/background-work/services/fg-service-types#microphone from sdk 34.

The main issue seems to be that we are starting the VoiceConnectionService foreground service too early here https://github.com/react-native-webrtc/react-native-callkeep/blob/master/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java#L209

I think we should start this foreground service when the call is answered, somewhere here https://github.com/react-native-webrtc/react-native-callkeep/blob/master/android/src/main/java/io/wazo/callkeep/RNCallKeepModule.java#L464

What happens right now is that this foregroundservice https://github.com/react-native-webrtc/react-native-callkeep/blob/master/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java#L209 fails silently if you dont have microphone permission already.

The only way around this atm is to request for the microphone permission upfront before you receive a call. This can be bad for UX.

Please if you have some time you can share how this call https://github.com/react-native-webrtc/react-native-callkeep/blob/master/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java#L209 can be delayed/recalled onanswer of the call. @manuquentin

I can then make a PR for this

LukakHR commented 2 months ago

@nero2009 for some of my users the issue seems to be happening even if they have allowed microphone permissions before the call has been initialized so this might not be the only issue

wkwiatek commented 2 months ago

This may be related to the additional permissions required for Android 14 to add foreground service type: https://developer.android.com/about/versions/14/changes/fgs-types-required.

In short, add following permissions to the AndroidManifest.xml:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" />

I've made doc change in this PR: https://github.com/react-native-webrtc/react-native-callkeep/pull/806.

manuquentin commented 2 months ago

I've merged #806. Thanks @wkwiatek for your work on investigating it.

nero2009 commented 2 months ago

Not sure if this fixes this issue.

Last time I checked https://github.com/react-native-webrtc/react-native-callkeep/blob/master/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java#L348 still fails cos we need the RECORD_AUDIO runtime permission before we can start that foreground service.

Ideally we should start that foreground service when the call is answer not when the call is received. I could be wrong tho.

nero2009 commented 1 month ago

You can always start you own foreground service using this https://rn-foreground.vercel.app/ or implement yours and expose via native modules