-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathIndividualCall.swift
362 lines (302 loc) · 12 KB
/
IndividualCall.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// Copyright 2016 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import AVFoundation
import SignalRingRTC
public import SignalServiceKit
import SignalUI
import WebRTC
enum CallState: String {
case idle
case dialing
case answering
case remoteRinging
// The local ringing state is a bit more complex since we sometimes kick off
// a CallKit ring before RingRTC is ready to answer. We can only answer the call
// once both the user has answered and RingRTC is ready.
case localRinging_Anticipatory // RingRTC not ready. User has not answered
case localRinging_ReadyToAnswer // RingRTC ready. User has not answered
case accepting // RingRTC not ready. User has answered
case connected
case reconnecting
case localFailure // terminal
case localHangup // terminal
case remoteHangup // terminal
case remoteHangupNeedPermission // terminal
case remoteBusy // terminal
case answeredElsewhere // terminal
case declinedElsewhere // terminal
case busyElsewhere // terminal
}
enum CallDirection {
case outgoing, incoming
}
protocol IndividualCallObserver: AnyObject {
@MainActor func individualCallStateDidChange(_ call: IndividualCall, state: CallState)
@MainActor func individualCallLocalVideoMuteDidChange(_ call: IndividualCall, isVideoMuted: Bool)
@MainActor func individualCallLocalAudioMuteDidChange(_ call: IndividualCall, isAudioMuted: Bool)
@MainActor func individualCallHoldDidChange(_ call: IndividualCall, isOnHold: Bool)
@MainActor func individualCallRemoteAudioMuteDidChange(_ call: IndividualCall, isAudioMuted: Bool)
@MainActor func individualCallRemoteVideoMuteDidChange(_ call: IndividualCall, isVideoMuted: Bool)
@MainActor func individualCallRemoteSharingScreenDidChange(_ call: IndividualCall, isRemoteSharingScreen: Bool)
}
extension IndividualCallObserver {
func individualCallLocalVideoMuteDidChange(_ call: IndividualCall, isVideoMuted: Bool) {}
func individualCallLocalAudioMuteDidChange(_ call: IndividualCall, isAudioMuted: Bool) {}
func individualCallHoldDidChange(_ call: IndividualCall, isOnHold: Bool) {}
func individualCallRemoteAudioMuteDidChange(_ call: IndividualCall, isAudioMuted: Bool) {}
func individualCallRemoteVideoMuteDidChange(_ call: IndividualCall, isVideoMuted: Bool) {}
func individualCallRemoteSharingScreenDidChange(_ call: IndividualCall, isRemoteSharingScreen: Bool) {}
}
/**
* Data model for a WebRTC backed voice/video call.
*
* This class' state should only be accessed on the main queue.
*/
public class IndividualCall: CustomDebugStringConvertible {
private var databaseStorage: SDSDatabaseStorage { SSKEnvironment.shared.databaseStorageRef }
// Mark -
var backgroundTask: OWSBackgroundTask? {
didSet {
AssertIsOnMainThread()
Logger.info("")
}
}
private(set) var callId: UInt64?
let callEventInserter: CallEventInserter
func setOutgoingCallIdAndUpdateCallRecord(_ callId: UInt64) {
AssertIsOnMainThread()
owsPrecondition(self.direction == .outgoing)
Logger.info("")
self.callId = callId
self.databaseStorage.asyncWrite { tx in
self.callEventInserter.setOutgoingCallId(callId, tx: tx)
}
}
@MainActor
weak var remoteVideoTrack: RTCVideoTrack? {
didSet {
Logger.info("")
observers.elements.forEach {
$0.individualCallRemoteVideoMuteDidChange(self, isVideoMuted: !isRemoteVideoEnabled)
}
}
}
@MainActor
var isRemoteAudioMuted = false {
didSet {
Logger.info("\(isRemoteAudioMuted)")
observers.elements.forEach {
$0.individualCallRemoteAudioMuteDidChange(self, isAudioMuted: isRemoteAudioMuted)
}
}
}
@MainActor
var isRemoteVideoEnabled = false {
didSet {
Logger.info("\(isRemoteVideoEnabled)")
observers.elements.forEach {
$0.individualCallRemoteVideoMuteDidChange(self, isVideoMuted: !isRemoteVideoEnabled)
}
}
}
@MainActor
var isRemoteSharingScreen = false {
didSet {
Logger.info("\(isRemoteSharingScreen)")
observers.elements.forEach {
$0.individualCallRemoteSharingScreenDidChange(self, isRemoteSharingScreen: isRemoteSharingScreen)
}
}
}
var networkRoute: NetworkRoute = NetworkRoute(localAdapterType: .unknown)
// MARK: -
var remoteAddress: SignalServiceAddress { thread.contactAddress }
@MainActor
var isEnded: Bool {
switch state {
case .localFailure, .localHangup, .remoteHangup, .remoteHangupNeedPermission, .remoteBusy, .answeredElsewhere, .declinedElsewhere, .busyElsewhere:
return true
case .idle, .dialing, .answering, .remoteRinging, .localRinging_Anticipatory, .localRinging_ReadyToAnswer, .accepting, .connected, .reconnecting:
return false
}
}
let commonState: CommonCallState
let direction: CallDirection
let thread: TSContactThread
let sentAtTimestamp: UInt64
/// Used by IndividualCallService to make decisions about what actions to take.
/// Not guaranteed to be up-to-date with what is in the database, but
/// is up to date with CallKit callbacks on the main thread.
/// Can be accessed from the main thread.
private(set) var callType: RPRecentCallType?
// The current local device ID; must be valid for lifetime of the call.
let localDeviceId: DeviceId
@MainActor
lazy var hasLocalVideo = offerMediaType == .video {
didSet {
observers.elements.forEach {
$0.individualCallLocalVideoMuteDidChange(self, isVideoMuted: !hasLocalVideo)
}
}
}
/// This is part of an ugly hack, but CallService is currently responsible
/// for starting/stopping the video preview, but we don't want to do that
/// until the view is actually going to be displayed. So we pass state
/// through IndividualCall.
var isViewLoaded = false
@MainActor
var deferredAnswerCompletion: (() -> Void)? {
didSet {
owsAssertDebug(deferredAnswerCompletion == nil || state == .accepting)
}
}
@MainActor
var state: CallState {
didSet {
Logger.debug("state changed: \(oldValue) -> \(self.state) for call: \(self)")
let state = self.state
if case .connected = state {
commonState.setConnectedDateIfNeeded()
}
observers.elements.forEach {
$0.individualCallStateDidChange(self, state: state)
}
}
}
var error: CallError?
public let offerMediaType: TSRecentCallOfferType
// We start out muted if the record permission isn't granted. This should generally
// only happen for incoming calls, because we proactively ask about it before you
// can make an outgoing call.
@MainActor
public var isMuted = AVAudioSession.sharedInstance().recordPermission != .granted {
didSet {
Logger.debug("muted changed: \(oldValue) -> \(self.isMuted)")
observers.elements.forEach {
$0.individualCallLocalAudioMuteDidChange(self, isAudioMuted: isMuted)
}
}
}
@MainActor
public var isOnHold = false {
didSet {
Logger.debug("isOnHold changed: \(oldValue) -> \(self.isOnHold)")
observers.elements.forEach {
$0.individualCallHoldDidChange(self, isOnHold: isOnHold)
}
}
}
@MainActor
var hasTerminated: Bool {
switch state {
case .idle, .dialing, .answering, .remoteRinging, .localRinging_Anticipatory, .localRinging_ReadyToAnswer,
.accepting, .connected, .reconnecting:
return false
case .localFailure, .localHangup, .remoteHangup, .remoteHangupNeedPermission, .remoteBusy, .answeredElsewhere,
.declinedElsewhere, .busyElsewhere:
return true
}
}
private(set) lazy var videoCaptureController = VideoCaptureController()
// MARK: Initializers and Factory Methods
static func outgoingIndividualCall(
thread: TSContactThread,
offerMediaType: TSRecentCallOfferType,
localDeviceId: DeviceId
) -> IndividualCall {
return IndividualCall(
callId: nil,
direction: .outgoing,
offerMediaType: offerMediaType,
state: .dialing,
thread: thread,
sentAtTimestamp: MessageTimestampGenerator.sharedInstance.generateTimestamp(),
localDeviceId: localDeviceId
)
}
static func incomingIndividualCall(
callId: UInt64,
thread: TSContactThread,
sentAtTimestamp: UInt64,
offerMediaType: TSRecentCallOfferType,
localDeviceId: DeviceId
) -> IndividualCall {
return IndividualCall(
callId: callId,
direction: .incoming,
offerMediaType: offerMediaType,
state: .answering,
thread: thread,
sentAtTimestamp: sentAtTimestamp,
localDeviceId: localDeviceId
)
}
private init(
callId: UInt64?,
direction: CallDirection,
offerMediaType: TSRecentCallOfferType,
state: CallState,
thread: TSContactThread,
sentAtTimestamp: UInt64,
localDeviceId: DeviceId
) {
self.callId = callId
self.callEventInserter = CallEventInserter(
thread: thread,
callId: callId,
offerMediaType: offerMediaType,
sentAtTimestamp: sentAtTimestamp
)
self.commonState = CommonCallState(
audioActivity: AudioActivity(
audioDescription: "[SignalCall] with individual \(thread.contactAddress)",
behavior: .call
)
)
self.direction = direction
self.offerMediaType = offerMediaType
self.state = state
self.thread = thread
self.sentAtTimestamp = sentAtTimestamp
self.localDeviceId = localDeviceId
}
deinit {
Logger.debug("")
}
public var debugDescription: String {
return "IndividualCall: {\(remoteAddress), signalingId: \(callId as Optional)))}"
}
// MARK: - Observers
private var observers: WeakArray<any IndividualCallObserver> = []
@MainActor
func addObserverAndSyncState(_ observer: any IndividualCallObserver) {
AssertIsOnMainThread()
observers.append(observer)
// Synchronize observer with current call state
observer.individualCallStateDidChange(self, state: state)
}
func removeObserver(_ observer: any IndividualCallObserver) {
observers.removeAll(where: { $0 === observer })
}
// MARK: - Fetching and updating db objects
public func createOrUpdateCallInteractionAsync(
callType: RPRecentCallType
) {
// Set the call type immediately; additional CallKit callbacks might come in
// before we get the lock to write, and they may make decisions based on the
// last callType they tried to set.
// They _should not_ rely on the callType actually being set on the TSCall; TSCall fields
// should be read within write transactions if they will be used as inputs when determining
// what new state to write to TSCall.
// Write transactions should almost always be asyncWrite, which puts them in a queue and
// enforces FIFO ordering; sync writes skip the line and can cause older state to get
// written later. A sync write is used only for the initial call offer handling, as that
// is always the first write for any given call, anyway.
self.callType = callType
self.databaseStorage.asyncWrite { tx in
self.callEventInserter.createOrUpdate(callType: callType, tx: tx)
}
}
}