react-native-webrtc / react-native-incall-manager

Handling media-routes/sensors/events during a audio/video chat on React Native
ISC License
544 stars 191 forks source link

[Android] Media does not play with 'unable to play during call' notification after call ends #184

Open minosjj opened 2 years ago

minosjj commented 2 years ago

I am implementing a video call with WebRTC, and I am using CallKeep and IncallManager together.

RN: 0.66.3 WebRTC: 1.94.1 RNCallkeep: 4.3.1 IncallManager: 3.2.2

Initially, there was no problem when the call function was implemented using webrtc and incallmanager. After adding Callkeep to receive calls, I'm having a problem with Android.

When media is played with android default app after the call is ended, the message “unable to play during call” appears and the media cannot be played. In other media apps (such as YouTube), the video plays but no sound. My app is running in the background. I ended the callkeep or incallmanager well after the call ended, but I don't understand why this happens.

GettingCall

//1. Answer incoming Call
RNCallKeep.answerIncomingCall(this.currentCallId);

...
WebRTC Signaling
...

//2. When a media stream is received, start IncallManager
handleStreamEvent(event: EventOnAddStream) {
    this.setRemoteStream(event.stream);

    // start IncallManager video, auto mode
    InCallManager.start({ media: 'video' });

    this.callStateChangeCallback('connected');
 }

EndCall

 closeVideoCall(): void {

    //stop IncallManager
     InCallManager.stop();

    if (this.conn) {
      this.conn.close();
      this.conn = null;
    }

    //end call
    RNCallKeep.endCall(this.currentCallId);
  }
pawan5209 commented 2 years ago

@minosjj I am also facing this issue.In my case the ringback tone is also not ended when i disconnect the call. Did you find any solution?

minosjj commented 2 years ago

@minosjj I am also facing this issue.In my case the ringback tone is also not ended when i disconnect the call. Did you find any solution?

I solved the problem by closing CallKeep first and starting IncallManager when receiving a call.

My guess is that when you start a call with CallKeep (RNCallKeep.start() or RNCallKeep.answerIncomingCall()), ConnectionService gets audioFocus, , but after that when IncallManager runs (IncallManager.start()) it doesn't get audioFocus. After that, even if RNCallKeep and IncallManager are finished(RNCallKeep.endCall() and InCallManager.stop()), it seems that CallKeep still has audioFocus. Because of that, when playing audio, Android seems to recognize the current state as incall state.

This is not a good solution, but I haven't had the same problem so far. It seems like a good way to create and use NativeModule with Android's InCallService.