@@ -82,11 +82,12 @@ class CallKitController : NSObject {
82
82
if ( icon != nil ) {
83
83
let iconImage = UIImage ( named: icon!)
84
84
let iconData = iconImage? . pngData ( )
85
+
85
86
providerConfiguration. iconTemplateImageData = iconData
86
87
}
87
88
}
88
89
89
- func reportIncomingCall(
90
+ @ objc func reportIncomingCall(
90
91
uuid: String ,
91
92
callType: Int ,
92
93
callInitiatorId: Int ,
@@ -95,7 +96,8 @@ class CallKitController : NSObject {
95
96
userInfo: String ? ,
96
97
completion: ( ( Error ? ) -> Void ) ?
97
98
) {
98
- print ( " [CallKitController][reportIncomingCall] call data: \( uuid) , \( callType) , \( callInitiatorId) , \( callInitiatorName) , \( opponents) , \( userInfo ?? " " ) , " )
99
+ print ( " [CallKitController][reportIncomingCall] call data: \( uuid) , \( callType) , \( callInitiatorId) , \( callInitiatorName) , \( opponents) , \( userInfo ?? " nil " ) " )
100
+
99
101
let update = CXCallUpdate ( )
100
102
update. localizedCallerName = callInitiatorName
101
103
update. remoteHandle = CXHandle ( type: . generic, value: uuid)
@@ -105,11 +107,12 @@ class CallKitController : NSObject {
105
107
update. supportsHolding = false
106
108
update. supportsDTMF = false
107
109
108
- configureAudioSession ( active: true )
109
110
if ( self . currentCallData [ " session_id " ] == nil || self . currentCallData [ " session_id " ] as! String != uuid) {
110
111
print ( " [CallKitController][reportIncomingCall] report new call: \( uuid) " )
112
+
111
113
provider. reportNewIncomingCall ( with: UUID ( uuidString: uuid) !, update: update) { error in
112
114
completion ? ( error)
115
+
113
116
if ( error == nil ) {
114
117
self . configureAudioSession ( active: true )
115
118
@@ -126,12 +129,16 @@ class CallKitController : NSObject {
126
129
}
127
130
} else if ( self . currentCallData [ " session_id " ] as! String == uuid) {
128
131
print ( " [CallKitController][reportIncomingCall] update existing call: \( uuid) " )
132
+
129
133
provider. reportCall ( with: UUID ( uuidString: uuid) !, updated: update)
134
+
135
+ completion ? ( nil )
130
136
}
131
137
}
132
138
133
139
func reportOutgoingCall( uuid : UUID , finishedConnecting: Bool ) {
134
- print ( " CallKitController: report outgoing call: \( uuid) connected: \( finishedConnecting) " )
140
+ print ( " [CallKitController][reportOutgoingCall] uuid: \( uuid. uuidString. lowercased ( ) ) connected: \( finishedConnecting) " )
141
+
135
142
if !finishedConnecting {
136
143
self . provider. reportOutgoingCall ( with: uuid, startedConnectingAt: nil )
137
144
} else {
@@ -140,7 +147,8 @@ class CallKitController : NSObject {
140
147
}
141
148
142
149
func reportCallEnded( uuid : UUID , reason: CallEndedReason ) {
143
- print ( " CallKitController: report call ended: \( uuid) " )
150
+ print ( " [CallKitController][reportCallEnded] uuid: \( uuid. uuidString. lowercased ( ) ) " )
151
+
144
152
var cxReason : CXCallEndedReason
145
153
switch reason {
146
154
case . unanswered:
@@ -150,12 +158,14 @@ class CallKitController : NSObject {
150
158
default :
151
159
cxReason = CXCallEndedReason . failed
152
160
}
161
+
153
162
self . callStates [ uuid. uuidString. lowercased ( ) ] = . rejected
154
163
self . provider. reportCall ( with: uuid, endedAt: Date . init ( ) , reason: cxReason)
155
164
}
156
165
157
166
func getCallState( uuid: String ) -> CallState {
158
- print ( " CallKitController: getCallState: \( self . callStates [ uuid. lowercased ( ) ] ?? . unknown) " )
167
+ print ( " [CallKitController][getCallState] uuid: \( uuid) , state: \( self . callStates [ uuid. lowercased ( ) ] ?? . unknown) " )
168
+
159
169
return self . callStates [ uuid. lowercased ( ) ] ?? . unknown
160
170
}
161
171
@@ -173,15 +183,18 @@ class CallKitController : NSObject {
173
183
}
174
184
175
185
func sendAudioInterruptionNotification( ) {
186
+ print ( " [CallKitController][sendAudioInterruptionNotification] " )
176
187
var userInfo : [ AnyHashable : Any ] = [ : ]
177
188
let intrepEndeRaw = AVAudioSession . InterruptionType. ended. rawValue
178
189
userInfo [ AVAudioSessionInterruptionTypeKey] = intrepEndeRaw
179
190
userInfo [ AVAudioSessionInterruptionOptionKey] = AVAudioSession . InterruptionOptions. shouldResume. rawValue
191
+
180
192
NotificationCenter . default. post ( name: AVAudioSession . interruptionNotification, object: self , userInfo: userInfo)
181
193
}
182
194
183
195
func configureAudioSession( active: Bool ) {
184
- print ( " CallKitController: [configureAudioSession] " )
196
+ print ( " [CallKitController][configureAudioSession] active: \( active) " )
197
+
185
198
let audioSession = AVAudioSession . sharedInstance ( )
186
199
187
200
do {
@@ -190,7 +203,6 @@ class CallKitController : NSObject {
190
203
options: [
191
204
. allowBluetooth,
192
205
. allowBluetoothA2DP,
193
- . duckOthers,
194
206
] )
195
207
try audioSession. setMode ( AVAudioSession . Mode. videoChat)
196
208
try audioSession. setPreferredSampleRate ( 44100.0 )
@@ -206,26 +218,29 @@ class CallKitController : NSObject {
206
218
extension CallKitController {
207
219
208
220
func end( uuid: UUID ) {
209
- print ( " CallKitController: user requested end call " )
221
+ print ( " [CallKitController][end] uuid: \( uuid. uuidString. lowercased ( ) ) " )
222
+
210
223
let endCallAction = CXEndCallAction ( call: uuid)
211
224
let transaction = CXTransaction ( action: endCallAction)
212
225
213
226
self . callStates [ uuid. uuidString. lowercased ( ) ] = . rejected
227
+
214
228
requestTransaction ( transaction)
215
229
}
216
230
217
231
private func requestTransaction( _ transaction: CXTransaction ) {
218
232
callController. request ( transaction) { error in
219
233
if let error = error {
220
- print ( " CallKitController: Error requesting transaction : \( error. localizedDescription) " )
234
+ print ( " [ CallKitController][requestTransaction] Error: \( error. localizedDescription) " )
221
235
} else {
222
- print ( " CallKitController: Requested transaction successfully " )
236
+ print ( " [ CallKitController][requestTransaction] successfully" )
223
237
}
224
238
}
225
239
}
226
240
227
241
func setHeld( uuid: UUID , onHold: Bool ) {
228
- print ( " CallKitController: user requested hold call " )
242
+ print ( " [CallKitController][setHeld] uuid: \( uuid. uuidString. lowercased ( ) ) , onHold: \( onHold) " )
243
+
229
244
let setHeldCallAction = CXSetHeldCallAction ( call: uuid, onHold: onHold)
230
245
231
246
let transaction = CXTransaction ( )
@@ -235,15 +250,18 @@ extension CallKitController {
235
250
}
236
251
237
252
func setMute( uuid: UUID , muted: Bool ) {
238
- print ( " CallKitController: user requested mute call: muted - \( muted) " )
253
+ print ( " [CallKitController][setMute] uuid: \( uuid. uuidString. lowercased ( ) ) , muted: \( muted) " )
254
+
239
255
let muteCallAction = CXSetMutedCallAction ( call: uuid, muted: muted) ;
240
256
let transaction = CXTransaction ( )
241
257
transaction. addAction ( muteCallAction)
258
+
242
259
requestTransaction ( transaction)
243
260
}
244
261
245
262
func startCall( handle: String , videoEnabled: Bool , uuid: String ? = nil ) {
246
- print ( " CallKitController: user requested start call handle: \( handle) , videoEnabled: \( videoEnabled) uuid: \( uuid ?? " " ) " )
263
+ print ( " [CallKitController][startCall] handle: \( handle) , videoEnabled: \( videoEnabled) uuid: \( uuid ?? " nil " ) " )
264
+
247
265
let handle = CXHandle ( type: . generic, value: handle)
248
266
let callUUID = uuid == nil ? UUID ( ) : UUID ( uuidString: uuid!)
249
267
let startCallAction = CXStartCallAction ( call: callUUID!, handle: handle)
@@ -257,7 +275,8 @@ extension CallKitController {
257
275
}
258
276
259
277
func answerCall( uuid: String ) {
260
- print ( " CallKitController: user requested answer call, uuid: \( uuid) " )
278
+ print ( " [CallKitController][answerCall] uuid: \( uuid) " )
279
+
261
280
let callUUID = UUID ( uuidString: uuid)
262
281
let answerCallAction = CXAnswerCallAction ( call: callUUID!)
263
282
let transaction = CXTransaction ( action: answerCallAction)
@@ -275,50 +294,46 @@ extension CallKitController: CXProviderDelegate {
275
294
}
276
295
277
296
func provider( _ provider: CXProvider , perform action: CXAnswerCallAction ) {
278
- print ( " CallKitController: Answer Call \( action. callUUID. uuidString) " )
279
-
280
- DispatchQueue . main. asyncAfter ( deadline: . now( ) + . milliseconds( 1200 ) ) {
281
- self . configureAudioSession ( active: true )
282
- }
297
+ print ( " [CallKitController][CXAnswerCallAction] callUUID: \( action. callUUID. uuidString. lowercased ( ) ) " )
283
298
299
+ configureAudioSession ( active: true )
284
300
callStates [ action. callUUID. uuidString. lowercased ( ) ] = . accepted
285
301
actionListener ? ( . answerCall, action. callUUID, self . currentCallData)
286
302
287
303
action. fulfill ( )
288
304
}
289
305
290
306
func provider( _ provider: CXProvider , didActivate audioSession: AVAudioSession ) {
291
- print ( " CallKitController: Audio session activated " )
292
-
293
- if ( currentCallData [ " session_id " ] != nil
294
- && callStates [ currentCallData [ " session_id " ] as! String ] == . accepted) {
295
- sendAudioInterruptionNotification ( )
296
- return
297
- }
307
+ print ( " [CallKitController] Audio session activated " )
298
308
299
309
sendAudioInterruptionNotification ( )
300
310
configureAudioSession ( active: true )
301
311
}
302
312
303
313
func provider( _ provider: CXProvider , didDeactivate audioSession: AVAudioSession ) {
304
- print ( " CallKitController: Audio session deactivated " )
314
+ print ( " [ CallKitController] Audio session deactivated" )
305
315
}
306
316
307
317
func provider( _ provider: CXProvider , perform action: CXEndCallAction ) {
308
- print ( " CallKitController: End Call " )
318
+ print ( " [CallKitController][CXEndCallAction] " )
319
+
309
320
actionListener ? ( . endCall, action. callUUID, currentCallData)
310
321
callStates [ action. callUUID. uuidString. lowercased ( ) ] = . rejected
322
+
311
323
action. fulfill ( )
312
324
}
313
325
314
326
func provider( _ provider: CXProvider , perform action: CXSetHeldCallAction ) {
315
- print ( " CallKitController: Set Held " )
327
+ print ( " [CallKitController][CXSetHeldCallAction] callUUID: \( action. callUUID. uuidString. lowercased ( ) ) " )
328
+
316
329
actionListener ? ( . setHeld, action. callUUID, [ " isOnHold " : action. isOnHold] )
330
+
317
331
action. fulfill ( )
318
332
}
319
333
320
334
func provider( _ provider: CXProvider , perform action: CXSetMutedCallAction ) {
321
- print ( " CallKitController: Mute call " )
335
+ print ( " [CallKitController][CXSetMutedCallAction] callUUID: \( action. callUUID. uuidString. lowercased ( ) ) " )
336
+
322
337
if ( action. isMuted) {
323
338
actionListener ? ( . setMuted, action. callUUID, currentCallData)
324
339
} else {
@@ -329,9 +344,12 @@ extension CallKitController: CXProviderDelegate {
329
344
}
330
345
331
346
func provider( _ provider: CXProvider , perform action: CXStartCallAction ) {
332
- print ( " CallKitController: Start Call " )
347
+ print ( " [CallKitController][CXStartCallAction]: callUUID: \( action. callUUID. uuidString. lowercased ( ) ) " )
348
+
333
349
actionListener ? ( . startCall, action. callUUID, currentCallData)
334
350
callStates [ action. callUUID. uuidString. lowercased ( ) ] = . accepted
351
+ configureAudioSession ( active: true )
352
+
335
353
action. fulfill ( )
336
354
}
337
355
}
0 commit comments