@@ -9,8 +9,6 @@ import Foundation
9
9
import AVFoundation
10
10
import CallKit
11
11
12
-
13
-
14
12
enum CallEvent : String {
15
13
case answerCall = " answerCall "
16
14
case endCall = " endCall "
@@ -107,12 +105,13 @@ class CallKitController : NSObject {
107
105
update. supportsHolding = false
108
106
update. supportsDTMF = false
109
107
108
+ configureAudioSession ( active: true )
110
109
if ( self . currentCallData [ " session_id " ] == nil || self . currentCallData [ " session_id " ] as! String != uuid) {
111
110
print ( " [CallKitController][reportIncomingCall] report new call: \( uuid) " )
112
111
provider. reportNewIncomingCall ( with: UUID ( uuidString: uuid) !, update: update) { error in
113
112
completion ? ( error)
114
113
if ( error == nil ) {
115
- self . configureAudioSession ( )
114
+ self . configureAudioSession ( active : true )
116
115
117
116
self . currentCallData [ " session_id " ] = uuid
118
117
self . currentCallData [ " call_type " ] = callType
@@ -173,15 +172,30 @@ class CallKitController : NSObject {
173
172
self . callsData. removeAll ( )
174
173
}
175
174
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] " )
177
185
let audioSession = AVAudioSession . sharedInstance ( )
178
186
179
187
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)
182
196
try audioSession. setPreferredSampleRate ( 44100.0 )
183
197
try audioSession. setPreferredIOBufferDuration ( 0.005 )
184
- try audioSession. setActive ( true )
198
+ try audioSession. setActive ( active )
185
199
} catch {
186
200
print ( error)
187
201
}
@@ -262,14 +276,28 @@ extension CallKitController: CXProviderDelegate {
262
276
263
277
func provider( _ provider: CXProvider , perform action: CXAnswerCallAction ) {
264
278
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
+
267
287
action. fulfill ( )
268
288
}
269
289
270
290
func provider( _ provider: CXProvider , didActivate audioSession: AVAudioSession ) {
271
291
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 )
273
301
}
274
302
275
303
func provider( _ provider: CXProvider , didDeactivate audioSession: AVAudioSession ) {
@@ -279,7 +307,7 @@ extension CallKitController: CXProviderDelegate {
279
307
func provider( _ provider: CXProvider , perform action: CXEndCallAction ) {
280
308
print ( " CallKitController: End Call " )
281
309
actionListener ? ( . endCall, action. callUUID, currentCallData)
282
- self . callStates [ action. callUUID. uuidString. lowercased ( ) ] = . rejected
310
+ callStates [ action. callUUID. uuidString. lowercased ( ) ] = . rejected
283
311
action. fulfill ( )
284
312
}
285
313
@@ -303,9 +331,7 @@ extension CallKitController: CXProviderDelegate {
303
331
func provider( _ provider: CXProvider , perform action: CXStartCallAction ) {
304
332
print ( " CallKitController: Start Call " )
305
333
actionListener ? ( . startCall, action. callUUID, currentCallData)
306
- self . callStates [ action. callUUID. uuidString. lowercased ( ) ] = . accepted
334
+ callStates [ action. callUUID. uuidString. lowercased ( ) ] = . accepted
307
335
action. fulfill ( )
308
336
}
309
337
}
310
-
311
-
0 commit comments