-
Notifications
You must be signed in to change notification settings - Fork 6
/
CallViewController.swift
251 lines (225 loc) · 10.5 KB
/
CallViewController.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
/*
* Copyright (c) 2011-2020, Zingaya, Inc. All rights reserved.
*/
import UIKit
import VoxImplantSDK
final class CallViewController:
UIViewController,
AudioDeviceAlertSelecting,
VIVideoRendererViewDelegate
{
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { .all }
@IBOutlet private weak var muteButton: CallOptionButton!
@IBOutlet private weak var chooseAudioButton: CallOptionButton!
@IBOutlet private weak var holdButton: CallOptionButton!
@IBOutlet private weak var videoButton: CallOptionButton!
@IBOutlet private weak var hangupButton: CallOptionButton!
@IBOutlet private weak var localVideoStreamView: CallVideoView!
@IBOutlet private weak var magneticView: EdgeMagneticView!
@IBOutlet private weak var remoteVideoStreamView: CallVideoView!
@IBOutlet private weak var endpointDisplayNameLabel: UILabel!
@IBOutlet private weak var callStateLabel: LabelWithTimer!
@IBOutlet private weak var labelsStackView: UIStackView!
private var shouldDismissAfterAppearing = false
var callManager: CallManager! // DI
var storyAssembler: StoryAssembler! // DI
override func viewDidLoad() {
super.viewDidLoad()
muteButton.state = .initial(model: CallOptionButtonModels.mute)
muteButton.touchUpHandler = { [weak self] button in
Log.d("Changing mute state")
let previousState = button.state
button.state = .unavailable
do {
try self?.callManager.toggleMute()
// if success - UI will be updated from callObserver
} catch CallError.hasNoActiveCall {
button.state = previousState
Log.d("Tried to toggle mute state while active call is nil")
AlertHelper.showError(message: "Could'nt change mute state, active call not found", on: self)
} catch {
button.state = previousState
Log.d("An error during toggle mute state \(error.localizedDescription)")
AlertHelper.showError(message: "Could'nt change mute state: \(error.localizedDescription)", on: self)
}
}
chooseAudioButton.state = .initial(model: CallOptionButtonModels.chooseAudio)
chooseAudioButton.touchUpHandler = { [weak self] button in
Log.d("Showing audio devices actionSheet")
self?.showAudioDevicesActionSheet(sourceView: button)
}
holdButton.state = .initial(model: CallOptionButtonModels.hold)
holdButton.touchUpHandler = { [weak self] button in
Log.d("Changing hold state")
let previousState = button.state
button.state = .unavailable
self?.callManager.toggleHold { error in
if let _ = error as? CallError {
button.state = previousState
Log.d("Tried to toggle hold state while active call is nil")
AlertHelper.showError(message: "Could'nt change hold state, active call not found", on: self)
} else if let error = error {
button.state = previousState
Log.d("An error during toggle hold state \(error.localizedDescription)")
AlertHelper.showError(message: "Could'nt change hold state: \(error.localizedDescription)", on: self)
}
// if success - UI will be updated from callObserver
}
}
videoButton.state = .initial(model: CallOptionButtonModels.video)
videoButton.touchUpHandler = { [weak self] button in
Log.d("Changing sendVideo state")
let previousState = button.state
button.state = .unavailable
self?.callManager.toggleSendVideo { error in
if let _ = error as? CallError {
button.state = previousState
Log.d("Tried to toggle sendVideo state while active call is nil")
AlertHelper.showError(message: "Could'nt change sendVideo state, active call not found", on: self)
} else if let error = error {
button.state = previousState
Log.d("An error during toggle sendVideo state \(error.localizedDescription)")
AlertHelper.showError(message: "Could'nt change sendVideo state: \(error.localizedDescription)", on: self)
}
}
// if success - UI will be updated from callObserver
}
hangupButton.state = .initial(model: CallOptionButtonModels.hangup)
hangupButton.touchUpHandler = { [weak self] button in
Log.d("Call hangup called")
button.state = .unavailable
do {
try self?.callManager.endCall()
// if success - UI will be updated from callObserver
} catch {
self?.dismiss(animated: true)
Log.e(error.localizedDescription)
}
}
localVideoStreamView.showImage = false
callManager.callObserver = { [weak self] call in
self?.updateContent(with: call)
}
callManager.videoStreamAddedHandler = { [weak self] local, completion in
guard let self = self else { return }
if local {
self.localVideoStreamView.streamEnabled = true
completion(VIVideoRendererView(containerView: self.localVideoStreamView.streamView))
} else {
self.remoteVideoStreamView.streamEnabled = true
let renderer = VIVideoRendererView(containerView: self.remoteVideoStreamView.streamView)
renderer.delegate = self
completion(renderer)
}
}
callManager.videoStreamRemovedHandler = { [weak self] local in
(local ? self?.localVideoStreamView : self?.remoteVideoStreamView)?.streamEnabled = false
}
guard let callDirection = callManager.managedCallWrapper?.direction else {
shouldDismissAfterAppearing = true
return
}
do {
try callDirection == .outgoing
? callManager.startOutgoingCall()
: callManager.makeIncomingCallActive()
} catch (let error) {
Log.e(" \(callDirection) call start failed with error \(error.localizedDescription)")
shouldDismissAfterAppearing = true
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let call = callManager.managedCallWrapper {
updateContent(with: call)
}
labelsStackView.layoutMargins = UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 4)
labelsStackView.isLayoutMarginsRelativeArrangement = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if shouldDismissAfterAppearing {
dismiss(animated: true)
}
}
@IBAction private func localVideoStreamTapped(_ sender: UITapGestureRecognizer) {
callManager.switchCamera()
}
@IBAction private func localVideoStreamDragged(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .changed:
let translation = sender.translation(in: self.view)
localVideoStreamView.center = CGPoint(x: localVideoStreamView.center.x + translation.x,
y: localVideoStreamView.center.y + translation.y)
sender.setTranslation(CGPoint.zero, in: self.view)
case .ended:
magneticView.rearrangeInnerView()
default:
break
}
}
// MARK: - Private -
private func updateContent(with call: CallManager.CallWrapper) {
if case .ended (let reason) = call.state {
if case .disconnected = reason {
dismiss(animated: true)
}
if case .failed (let message) = reason {
navigationController?.setViewControllers(
[storyAssembler.callFailed(
callee: call.callee,
displayName: call.displayName ?? call.callee,
reason: message)
],
animated: true
)
}
return
}
endpointDisplayNameLabel.text = call.displayName ?? call.callee
muteButton.state = call.isMuted ? .selected : .normal
videoButton.state = !call.isOnHold && call.state == .connected
? call.sendingVideo ? .normal : .selected
: .unavailable
holdButton.state = call.state == .connected
? call.isOnHold ? .selected : .normal
: .unavailable
localVideoStreamView.streamEnabled = !call.isOnHold && call.sendingVideo
switch call.state {
case .connecting:
if call.direction == .outgoing {
callStateLabel.text = "Connecting..."
}
case .ringing:
callStateLabel.text = "Ringing..."
case .connected:
if call.isOnHold {
callStateLabel.stopTimer()
callStateLabel.text = "Call on hold"
} else {
callStateLabel.runTimer(with: call.duration)
}
case .reconnecting:
callStateLabel.stopTimer()
callStateLabel.text = "Reconnecting..."
default: break
}
}
private enum CallOptionButtonModels {
static let mute = CallOptionButtonModel(image: UIImage(named: "micOn"), imageSelected: UIImage(named: "micOff"), text: "Mic")
static let chooseAudio = CallOptionButtonModel(image: UIImage(named: "audioDevice"), text: "Audio")
static let hold = CallOptionButtonModel(image: UIImage(named: "pause"), text: "Hold")
static let video = CallOptionButtonModel(image: UIImage(named: "videoOn"), imageSelected: UIImage(named: "videoOff"), text: "Cam")
static let hangup = CallOptionButtonModel(image: UIImage(named: "hangup"), imageTint: #colorLiteral(red: 1, green: 0.02352941176, blue: 0.2549019608, alpha: 1), text: "Hangup")
}
}
// MARK: VIVideoRendererViewDelegate
extension CallViewController {
func videoView(_ videoView: VIVideoRendererView, didChangeVideoSize size: CGSize) {
if size.height > size.width {
videoView.resizeMode = .fill
} else {
videoView.resizeMode = .fit
}
}
}