Search code examples
iosswiftaudiopjsipcallkit

CallKit Audio Initialisation Error in Background - Audio not working after first Call


I have added pjsip VoIP functionality to my iOS App recently. Using pjsip offers no problem while the App is active, I can both receive and make Calls without issues.

But I seem to have a Problem with the sound devices in the background. The first call I receive from the background works fine, I get sound and I can close it normally.

The problem is after the first call I get no sound what so ever when I accept a call from the Background. The same happens if I make or receive a call while the App is active and then go to the background and receive a call. When I close the app, the first Call from the background works normally again.

I have no idea what could cause that.

I have tried to manually close all calls via CallManager and I call a CXEndCallAction when the Server Side terminates the call, to get CallKit to close properly.

I'd be happy to post specific code passages, but currently, I have no clue where the problem could lie...


UPDATE:

I have figured out that the pjsip coreaudio initializes differently the second time.

Now the Question is, how do I reset CoreAudio in pjsip properly?

Successful Call Init:

11:03:01.615 tsx0x1030710a8  .....State changed from Trying to 
Proceeding, event=TX_MSG
11:03:01.615 dlg0x1030840a8  ......Transaction tsx0x1030710a8 state changed to Proceeding
2019-02-13 11:03:01.615745+0100 VarioCallCTI[11928:2820452] Incoming call from "Matthias" <sip:[email protected]>
11:03:01.618    pjsua_aud.c  ..Set sound device: capture=0, playback=0
11:03:01.618    pjsua_aud.c !Set sound device: capture=0, playback=0
11:03:01.619    pjsua_aud.c !...Opening sound device PCM@16000/1/20ms
11:03:01.619 coreaudio_dev.  ....Using VoiceProcessingIO audio unit
2019-02-13 11:03:01.808024+0100 VarioCallCTI[11928:2820452] [aurioc] 1029: failed: '!int' (enable 3, outf< 1 ch,  16000 Hz, Int16> inf< 1 ch,  16000 Hz, Int16>)
11:03:01.851    pjsua_aud.c  ...Opening sound device PCM@44100/1/20ms
11:03:01.851 coreaudio_dev.  ....Using VoiceProcessingIO audio unit
2019-02-13 11:03:01.859854+0100 VarioCallCTI[11928:2820452] [aurioc] 1029: failed: '!int' (enable 3, outf< 1 ch,  44100 Hz, Int16> inf< 1 ch,  44100 Hz, Int16>)
11:03:01.860    pjsua_aud.c  ...Opening sound device PCM@48000/1/20ms
11:03:01.860 coreaudio_dev.  ....Using VoiceProcessingIO audio unit
2019-02-13 11:03:01.867061+0100 VarioCallCTI[11928:2820452] [aurioc] 1029: failed: '!int' (enable 3, outf< 1 ch,  48000 Hz, Int16> inf< 1 ch,  48000 Hz, Int16>)
11:03:01.867    pjsua_aud.c  ...Opening sound device PCM@32000/1/20ms
11:03:01.867 coreaudio_dev.  ....Using VoiceProcessingIO audio unit
2019-02-13 11:03:01.874321+0100 VarioCallCTI[11928:2820452] [aurioc] 1029: failed: '!int' (enable 3, outf< 1 ch,  32000 Hz, Int16> inf< 1 ch,  32000 Hz, Int16>)
11:03:01.875    pjsua_aud.c  ...Opening sound device PCM@16000/1/20ms
11:03:01.875 coreaudio_dev.  ....Using VoiceProcessingIO audio unit
2019-02-13 11:03:01.881126+0100 VarioCallCTI[11928:2820452] [aurioc] 1029: failed: '!int' (enable 3, outf< 1 ch,  16000 Hz, Int16> inf< 1 ch,  16000 Hz, Int16>)
11:03:01.881    pjsua_aud.c  ...Opening sound device PCM@8000/1/20ms
11:03:01.881 coreaudio_dev.  ....Using VoiceProcessingIO audio unit
2019-02-13 11:03:01.888214+0100 VarioCallCTI[11928:2820452] [aurioc] 1029: failed: '!int' (enable 3, outf< 1 ch,   8000 Hz, Int16> inf< 1 ch,   8000 Hz, Int16>)
11:03:01.888    pjsua_aud.c  ...Unable to open sound device: Unknown error -560117684 [status=-560117684]
Error in Setting of Audio Devices - IncomingCall
TESTPRINT FOR NOTIFICATION INFO: eingehender anruf von 238
TESTPRINT FOR NOTIFICATION INFO: 238

Unsuccessful Call Init:

    11:03:49.840 tsx0x103064ea8  .....State changed from Trying to Proceeding, event=TX_MSG
11:03:49.840 dlg0x1030906a8  ......Transaction tsx0x103064ea8 state changed to Proceeding
2019-02-13 11:03:49.841013+0100 VarioCallCTI[11928:2820906] Incoming call from "Matthias" <sip:[email protected]>
11:03:49.842    pjsua_aud.c  ..Set sound device: capture=0, playback=0

11:03:49.842    pjsua_aud.c  ...Opening sound device PCM@16000/1/20ms
11:03:49.842 coreaudio_dev.  ....Using VoiceProcessingIO audio unit
11:03:49.847    pjsua_aud.c !Set sound device: capture=0, playback=0
2019-02-13 11:03:50.276837+0100 VarioCallCTI[11928:2820280] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Speaker (type: Speaker)
2019-02-13 11:03:50.277597+0100 VarioCallCTI[11928:2820280] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Receiver (type: Receiver)
2019-02-13 11:03:50.292488+0100 VarioCallCTI[11928:2820280] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Receiver (type: Receiver)
2019-02-13 11:03:50.293379+0100 VarioCallCTI[11928:2820280] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Receiver (type: Receiver)
11:03:50.383 coreaudio_dev. !....core audio stream started
Setting of Audio Devices Successfull - IncomingCall











TESTPRINT FOR NOTIFICATION INFO: eingehender anruf von 238
TESTPRINT FOR NOTIFICATION INFO: 238

Solution

  • I have figured out how to solve the Problem.

    Since the PJSIP Library Init caused the coreAudio to initialize before the priority bump required by iOS was fully finished, I had to open CallKit and register at the PJSIP Server inside the CallKit - ProviderDelegate

      func reportIncomingCall(uuid: UUID, handle: String, hasVideo: Bool = false, completion: ((NSError?) -> Void)?) {
    let update = CXCallUpdate()
    update.remoteHandle = CXHandle(type: .generic, value: handle)
    update.hasVideo = hasVideo
    //MARK: Final Working Solution
    print("Starting PJSIP after PUSH")
    pjsipfunctions.init().registersip(mainUser.sipData["User"], mainUser.sipData["Password"], mainUser.sipData["Server"])
    
    provider.reportNewIncomingCall(with: uuid, update: update) { error in
      if error == nil {
        let call = Call(uuid: uuid, handle: handle)
        self.callManager.add(call: call)
        userTerminatedCall = false
    
      }
    
      completion?(error as? NSError)
    }
    }
    }