12

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!

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
Rakuyo
  • 390
  • 3
  • 15
  • Did you solve the problem? – WorieN Jul 22 '19 at 13:59
  • I have one weird issue. For the first time when the device is killed and locked, there is no audio issue, Everything works perfectly. But If I end the call from CallKit screen and again receive an incoming call, there is no audio in the call. Both parties can't hear anything, though didActivateAudioSession was called. I am stuck here. Any help will be very helpful. – Tanay Mondal May 14 '20 at 15:22

3 Answers3

10

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
Hugo H
  • 6,029
  • 5
  • 37
  • 57
Gregios
  • 196
  • 1
  • 6
  • Sorry, but didDeactivate called only when user hangup. How can I give control on audio to webrtc? – WorieN Jul 18 '19 at 13:19
  • How can I use RTCAudioSession when making webrtc connection? Can you give code sample? – WorieN Jul 19 '19 at 13:46
  • If you don't know how to set a CallKit call over WebRTC to speaker immediately, this is the solution. Also when you notice the AVAudioSession for CallKit and RTCAudioSession do not have the same memory address, despite the fact that they seem to use the shared shared instance initially. This reply might also help people find this post when they're using the same search keywords as I did before I finally found this ;) – Lucas van Dongen Jan 28 '20 at 18:40
  • CallKit call is getting disconnected when user presses the Lock button. Any idea? – YSR fan Feb 27 '20 at 12:43
  • I have one weird issue. For the first time when the device is killed and locked, there is no audio issue, Everything works perfectly. But If I end the call from CallKit screen and again receive an incoming call, there is no audio in the call. Both parties can't hear anything, though didActivateAudioSession was called. I am stuck here. Any help will be very helpful. – Tanay Mondal May 14 '20 at 15:21
5

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

Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75
3

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!

Rakuyo
  • 390
  • 3
  • 15
  • because sometimes the audio start before it were not activated, so delay 1 second help. but it's not good solution buddy – famfamfam Feb 17 '21 at 14:36