Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CallKit:No sound when I use WebRTC

Our project uses WebRTC for VOIP calls and it works fine before accessing the CallKit framework. But when I tried to access the CallKit framework, there was a situation where neither side could hear each other's speech. When I removed CallKit, everything returned to normal.

CallKit's answer button is the same function as the original answer button in the project.

And what amazed me was that it was not necessary to hear no sound. Sometimes everything is normal, but sometimes there will be problems. Well, the probability of a problem is greater.

I found the following flowchart, I suspect the problem lies in the order of function calls. But I do not know how WebRTC corresponds to the functions in the diagram.
enter image description here

In addition, I am curious whether socket instability will cause the CallKit framework to work abnormally

Please forgive me English is not good, but this problem has been haunted me for several days, I do not know where exactly a problem, is not where the conflict with the CallKit framework?

Hope you can help me, thank you very much!

like image 908
Rakuyo Avatar asked Jan 03 '18 01:01

Rakuyo


3 Answers

Few steps need to be done to connect webrtc and callkit in proper way: First of all, you have to use the RTCAudioTrack and add the RTCAudioSession for handling the audio. Old legacy RTCAudioSession added directly into RTCPeerConnection works but it's not prefered a way to do that. Second thing is to use manualAudio. When app is booted you should change useManualAudio flag on RTCAudioSession:

RTCAudioSession.sharedSession().useManualAudio = true

which gives you possibility to postpone the audio until CallKit informs that audio session was activated, so inside the ProviderDelegate you should implement following method:

(void)provider:(CXProvider *)provider didActivateAudioSession:(AVAudioSession *)audioSession
RTCAudioSession.sharedSession().didActivecated(audioSession)
RTCAudioSession.sharedSession().isAudioEnabled = true

and for second audio delegate method don't forget to add:

(void)provider:(CXProvider *)provider didDeactivateAudioSession:(AVAudioSession *)audioSession
RTCAudioSession.sharedSession().didDeactivecated(audioSession)
RTCAudioSession.sharedSession().isAudioEnabled = false
like image 30
Gregios Avatar answered Nov 18 '22 01:11

Gregios


Apple suggests us to wait till the Connection gets established and then fulfill the performAnswerAction. Below is the source

Apple Suggestion for Call Kit Documentation

If the recipient of a call answers before the app establishes a connection to your server, don't fulfill the CXAnswerCallAction object sent to the provider:performAnswerCallAction: method of your delegate immediately. Instead, wait until you establish a connection and then fulfill the object. While it waits for your app to fulfill the request, the incoming call interface lets the user know that the call is connecting, but not yet ready.

So we need to wait for a second or two before we fulfill the action in performAnswerCallAction

like image 63
Pradeep Reddy Kypa Avatar answered Nov 18 '22 00:11

Pradeep Reddy Kypa


In the end, I solved the problem, but I still do not understand why it can be solved.Below is my solution:

First of all, I delay the call of "fulfill" by 1 second (note that this time can not be less than 1 second)

- (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAction *)action {

if (self.delegate && [self.delegate respondsToSelector:@selector(callKitManager:refreshCurrentCallStatus:)]) {
    [self.delegate callKitManager:self refreshCurrentCallStatus:EUCCallKitStatusAnswerAccept];
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [action fulfill];
});}

Second, I also delayed my network request call by one second (here longer than the previous one)

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  [self.peerConnection offerForConstraints:[self offerConstraintsRestartIce:NO] completionHandler:^(RTCSessionDescription * _Nullable sdp, NSError * _Nullable error) {

  [self peerConnection:self.peerConnection didCreateSessionDescription:sdp error:error];
        }];

});

In this way, my problem is solved.

If you know why this can solve this problem, please comment on me, thank you!

like image 3
Rakuyo Avatar answered Nov 18 '22 02:11

Rakuyo