后台 CallKit 音频初始化错误 - 第一次通话后音频不工作

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

我最近在我的 iOS 应用程序中添加了 pjsip VoIP 功能。在应用程序处于活动状态时使用 pjsip 没有问题,我可以毫无问题地接听和拨打电话。

但我似乎对后台的声音设备有问题。我从后台收到的第一个电话工作正常,我听到声音并且可以正常关闭它。

问题是在第一次通话后,当我从后台接听电话时,我没有任何声音。如果我在应用程序处于活动状态时拨打或接听电话,然后转到后台接听电话,也会发生同样的情况。 当我关闭应用程序时,来自后台的第一个调用再次正常工作。

我不知道是什么原因造成的。

我已尝试通过 CallManager 手动关闭所有调用,并在服务器端终止调用时调用 CXEndCallAction,以使 CallKit 正确关闭。

我很乐意 post 特定的代码段落,但目前,我不知道问题出在哪里...


更新:

我发现 pjsip coreaudio 第二次初始化不同。

现在的问题是,如何正确重置 pjsip 中的 CoreAudio?

调用初始化成功:

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:238@80.80.245.33>
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

调用初始化失败:

    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:238@80.80.245.33>
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

我已经找到解决问题的方法了。

由于 PJSIP 库初始化导致 coreAudio 在 iOS 所需的优先级提升完全完成之前初始化,我不得不打开 CallKit 并在 CallKit - ProviderDelegate[=11= 内的 PJSIP 服务器上注册]

  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)
}
}
}