-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCallViewController.swift
187 lines (163 loc) · 8.15 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
/*
* Copyright (c) 2011-2020, Zingaya, Inc. All rights reserved.
*/
import UIKit
import VoxImplantSDK
import ReplayKit
final class CallViewController: UIViewController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { .all }
@IBOutlet private weak var conferenceView: ConferenceView!
@IBOutlet private weak var videoButton: CallOptionButton!
@IBOutlet private weak var muteButton: CallOptionButton!
@IBOutlet private weak var sharingButton: CallOptionButton!
@IBOutlet private weak var hangupButton: CallOptionButton!
private var screenSharingButtonSubview: UIImageView?
private var shouldDismissAfterAppearing = false
var myDisplayName: String! // DI
var callManager: CallManager! // DI
var storyAssembler: StoryAssembler! // DI
override func viewDidLoad() {
super.viewDidLoad()
callManager.callObserver = { [weak self] call in
self?.updateContent(with: call)
}
videoButton.state = .initial(model: CallOptionButtonModels.camera)
videoButton.touchUpHandler = { [weak self] button in
Log.d("Changing sendVideo")
let previousState = button.state
button.state = .unavailable
self?.callManager.toggleSendVideo { error in
if let error = error {
Log.e("setSendVideo error \(error.localizedDescription)")
AlertHelper.showError(message: error.localizedDescription, on: self)
}
button.state = previousState
}
}
muteButton.state = .initial(model: CallOptionButtonModels.mute)
muteButton.state = .normal
muteButton.touchUpHandler = { [weak self] button in
Log.d("Changing mute status")
let previousState = button.state
button.state = .unavailable
self?.callManager.toggleMuteStatus { error in
if let error = error {
Log.e("sendAudio error \(error.localizedDescription)")
AlertHelper.showError(message: error.localizedDescription, on: self)
}
button.state = previousState
if button.state == .normal {
button.state = .selected
button.updateButtonLabel(text: "Unmute")
} else {
button.state = .normal
button.updateButtonLabel(text: "Mute")
}
}
}
if #available(iOS 12.0, *) {
sharingButton.state = .initial(model: CallOptionButtonModels.screen)
} else {
sharingButton.state = .initial(model: CallOptionButtonModels.screenOld)
}
sharingButton.touchUpHandler = { _ in
if #available(iOS 12.0, *) {
// nothing, besause used handler of RPSystemBroadcastPickerView, which created in self.init() method
} else {
AlertHelper.showAlert(title: "On iOS 11 enabling screensharing", message: "Open the Control Panel (swipe up from bottom) -> hold down on the Record Button until options appeared -> select ScreenSharingUploadAppex -> start broadcast. If can't find Record Button in Control Panel go to the Settings -> Control Center -> Customize Controls and add Screen Recording to the Control Panel.", defaultAction: true)
}
}
if #available(iOS 12.0, *) {
let broadcastPicker = RPSystemBroadcastPickerView(frame: CGRect(x: 0, y: 0, width: 46, height: 46))
broadcastPicker.preferredExtension = "com.voximplant.demos.ScreenSharing.ScreenSharingUploadAppex"
if let button = broadcastPicker.subviews.first as? UIButton {
button.imageView?.tintColor = UIColor.white
screenSharingButtonSubview = button.imageView
}
sharingButton.addSubview(broadcastPicker)
}
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()
} catch (let error) {
Log.e(error.localizedDescription)
}
self?.dismiss(animated: true)
}
callManager.endpointAddedHandler = conferenceView.addParticipant(withID:displayName:)
callManager.endpointUpdatedHandler = conferenceView.updateParticipant(withID:displayName:)
callManager.endpointRemovedHandler = conferenceView.removeParticipant(withId:)
callManager.localVideoStreamAddedHandler = conferenceView.prepareVideoRendererForStream(participantID:completion:)
callManager.remoteVideoStreamAddedHandler = conferenceView.prepareVideoRendererForStream(participantID:completion:)
callManager.remoteVideoStreamAddedHandler = conferenceView.prepareVideoRendererForStream(participantID:completion:)
callManager.remoteVideoStreamRemovedHandler = conferenceView.removeVideoStream(participantID:)
do {
try callManager.startOutgoingCall()
conferenceView.addParticipant(withID: myId, displayName: "\(myDisplayName ?? "") (you)")
} catch (let error) {
Log.e("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)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if shouldDismissAfterAppearing {
dismiss(animated: true)
}
}
deinit {
callManager.endpointAddedHandler = nil
callManager.endpointUpdatedHandler = nil
callManager.endpointRemovedHandler = nil
callManager.localVideoStreamAddedHandler = nil
callManager.remoteVideoStreamAddedHandler = nil
callManager.remoteVideoStreamAddedHandler = nil
callManager.remoteVideoStreamRemovedHandler = nil
}
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
}
if #available(iOS 12.0, *) {
screenSharingButtonSubview?.tintColor = call.sharingScreen ? #colorLiteral(red: 0.9607843137, green: 0.2941176471, blue: 0.368627451, alpha: 1) : .white
} else {
sharingButton.state = call.state == .connected
? call.sharingScreen ? .selected : .normal
: .unavailable
}
videoButton.state = call.state == .connected
? call.sendingVideo ? .normal : .selected
: .unavailable
}
private enum CallOptionButtonModels {
static let screenOld = CallOptionButtonModel(image: UIImage(named: "screenSharing"), text: "Screen")
static let screen = CallOptionButtonModel(image: nil, text: "Screen")
static let camera = CallOptionButtonModel(image: UIImage(named: "videoOn"), imageSelected: UIImage(named: "videoOff"), text: "Camera")
static let hangup = CallOptionButtonModel(image: UIImage(named: "hangup"), imageTint: #colorLiteral(red: 1, green: 0.02352941176, blue: 0.2549019608, alpha: 1), text: "Hangup")
static let mute = CallOptionButtonModel(image: UIImage(named: "micOn"),
imageSelected: UIImage(named: "micOff"),
text: "Mute")
}
}