Skip to content

Commit 573e53f

Browse files
- improve Audio for iOS background calls;
1 parent 9dc6556 commit 573e53f

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

ios/Classes/CallKitController.swift

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import Foundation
99
import AVFoundation
1010
import CallKit
1111

12-
13-
1412
enum CallEvent : String {
1513
case answerCall = "answerCall"
1614
case endCall = "endCall"
@@ -107,12 +105,13 @@ class CallKitController : NSObject {
107105
update.supportsHolding = false
108106
update.supportsDTMF = false
109107

108+
configureAudioSession(active: true)
110109
if (self.currentCallData["session_id"] == nil || self.currentCallData["session_id"] as! String != uuid) {
111110
print("[CallKitController][reportIncomingCall] report new call: \(uuid)")
112111
provider.reportNewIncomingCall(with: UUID(uuidString: uuid)!, update: update) { error in
113112
completion?(error)
114113
if(error == nil){
115-
self.configureAudioSession()
114+
self.configureAudioSession(active: true)
116115

117116
self.currentCallData["session_id"] = uuid
118117
self.currentCallData["call_type"] = callType
@@ -173,15 +172,30 @@ class CallKitController : NSObject {
173172
self.callsData.removeAll()
174173
}
175174

176-
func configureAudioSession(){
175+
func sendAudioInterruptionNotification(){
176+
var userInfo : [AnyHashable : Any] = [:]
177+
let intrepEndeRaw = AVAudioSession.InterruptionType.ended.rawValue
178+
userInfo[AVAudioSessionInterruptionTypeKey] = intrepEndeRaw
179+
userInfo[AVAudioSessionInterruptionOptionKey] = AVAudioSession.InterruptionOptions.shouldResume.rawValue
180+
NotificationCenter.default.post(name: AVAudioSession.interruptionNotification, object: self, userInfo: userInfo)
181+
}
182+
183+
func configureAudioSession(active: Bool){
184+
print("CallKitController: [configureAudioSession]")
177185
let audioSession = AVAudioSession.sharedInstance()
178186

179187
do {
180-
try audioSession.setCategory(AVAudioSession.Category.playback, options: AVAudioSession.CategoryOptions.allowBluetooth)
181-
try audioSession.setMode(AVAudioSession.Mode.voiceChat)
188+
try audioSession.setCategory(
189+
AVAudioSession.Category.playAndRecord,
190+
options: [
191+
.allowBluetooth,
192+
.allowBluetoothA2DP,
193+
.duckOthers,
194+
])
195+
try audioSession.setMode(AVAudioSession.Mode.videoChat)
182196
try audioSession.setPreferredSampleRate(44100.0)
183197
try audioSession.setPreferredIOBufferDuration(0.005)
184-
try audioSession.setActive(true)
198+
try audioSession.setActive(active)
185199
} catch {
186200
print(error)
187201
}
@@ -262,14 +276,28 @@ extension CallKitController: CXProviderDelegate {
262276

263277
func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
264278
print("CallKitController: Answer Call \(action.callUUID.uuidString)")
265-
actionListener?(.answerCall, action.callUUID, currentCallData)
266-
self.callStates[action.callUUID.uuidString.lowercased()] = .accepted
279+
280+
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1200)) {
281+
self.configureAudioSession(active: true)
282+
}
283+
284+
callStates[action.callUUID.uuidString.lowercased()] = .accepted
285+
actionListener?(.answerCall, action.callUUID, self.currentCallData)
286+
267287
action.fulfill()
268288
}
269289

270290
func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
271291
print("CallKitController: Audio session activated")
272-
self.configureAudioSession()
292+
293+
if(currentCallData["session_id"] != nil
294+
&& callStates[currentCallData["session_id"] as! String] == .accepted){
295+
sendAudioInterruptionNotification()
296+
return
297+
}
298+
299+
sendAudioInterruptionNotification()
300+
configureAudioSession(active: true)
273301
}
274302

275303
func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) {
@@ -279,7 +307,7 @@ extension CallKitController: CXProviderDelegate {
279307
func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
280308
print("CallKitController: End Call")
281309
actionListener?(.endCall, action.callUUID, currentCallData)
282-
self.callStates[action.callUUID.uuidString.lowercased()] = .rejected
310+
callStates[action.callUUID.uuidString.lowercased()] = .rejected
283311
action.fulfill()
284312
}
285313

@@ -303,9 +331,7 @@ extension CallKitController: CXProviderDelegate {
303331
func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
304332
print("CallKitController: Start Call")
305333
actionListener?(.startCall, action.callUUID, currentCallData)
306-
self.callStates[action.callUUID.uuidString.lowercased()] = .accepted
334+
callStates[action.callUUID.uuidString.lowercased()] = .accepted
307335
action.fulfill()
308336
}
309337
}
310-
311-

0 commit comments

Comments
 (0)