-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSparkMediaView.swift
More file actions
291 lines (256 loc) · 11.5 KB
/
Copy pathSparkMediaView.swift
File metadata and controls
291 lines (256 loc) · 11.5 KB
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
//
// SparkMediaView.swift
// SparkMediaView
//
// The MIT License (MIT)
//
// Created by Jonathan Field on 09/10/2016.
// Copyright © 2016 Cisco. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import UIKit
import SparkSDK
/**
Provides notifications when significant events take place during a video call
- function callDidComplete: Triggered when the call ends regardless of the reason for call ending.
- function callFailedWithError: Triggered when there was an error in setting up the call or there was an error with authentication
*/
public protocol SparkMediaViewDelegate: class {
func callDidComplete()
func callFailed(withError: String)
}
public class SparkMediaView: UIViewController, CallObserver {
//Video Components
@IBOutlet weak var remoteMediaView: MediaRenderView!
@IBOutlet weak var localMediaView: MediaRenderView!
//User Interface
@IBOutlet weak var muteButton: UIButton!
@IBOutlet weak var hangupButton: UIButton!
@IBOutlet weak var rotateCameraButton: UIButton!
@IBOutlet weak var callTimerLabel: UILabel!
//Enums
public enum AuthenticationStrategy {
case sparkId, appId
}
//Call Variables
var authenticationType: AuthenticationStrategy
let apiKey: String!
var currentCall: Call!
var callTimer: Timer!
var currentCallDuration: Int = 0
//Delegate
weak var delegate:SparkMediaViewDelegate?
//Initalizers
public init(authType: AuthenticationStrategy, apiKey: String, delegate: SparkMediaViewDelegate?) {
self.authenticationType = authType
self.apiKey = apiKey
self.delegate = delegate
super.init(nibName: nil, bundle: nil)
}
required public init?(coder aDecoder: NSCoder) {
self.apiKey = String()
self.authenticationType = .appId
super.init(coder: aDecoder)
}
override public func viewDidLoad() {
super.viewDidLoad()
let tapGestureRecogniser = UITapGestureRecognizer.init(target: self, action: #selector(tap))
self.remoteMediaView.addGestureRecognizer(tapGestureRecogniser)
let panGestureRecogniser = UIPanGestureRecognizer.init(target: self, action: #selector(repositionSelfView))
self.localMediaView.addGestureRecognizer(panGestureRecogniser)
}
override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//Convenience Functions
public func voiceCall(recipient: String!) {
self.startSparkCall(authType: self.authenticationType, recipient: recipient, mediaAccessType: .audio)
}
public func videoCall(recipient: String!) {
self.startSparkCall(authType: self.authenticationType ,recipient: recipient, mediaAccessType: .audioVideo)
}
/**
Start a Call using the Spark Media SDK
- parameter recipient: The Spark URI or SIP URI of the remote particpiant to be dialled
- parameter mediaAccessType: The type of Media that will be sent to the remote party (Audio or Audio Video)
*/
func startSparkCall(authType: AuthenticationStrategy, recipient: String, mediaAccessType: Phone.MediaAccessType) {
self.authenticateWithSpark(apiKey: self.apiKey, authType: authType) { (attempted) in
if attempted {
self.registerDeviceWithSpark(mediaAccessType: mediaAccessType, successfulRegistration: { (success) in
self.registerForSparkCallStateNotifications()
self.startSparkCallTo(recipient: recipient, mediaAccessType: mediaAccessType)
}) { (error) in
self.delegate?.callFailed(withError: "Unable to register with Spark, check your access token.")
}
}
}
}
/**
Initial Authentication to Spark
- parameter apiKey: Spark Access Token from www.developer.ciscospark.com
*/
func authenticateWithSpark(apiKey: String, authType: AuthenticationStrategy, authenticationAttempted: @escaping (_ attempted: Bool ) -> Void){
if authType == .sparkId {
Spark.initWith(accessToken: apiKey)
authenticationAttempted(true)
}
else if authType == .appId {
// TODO
// let jwtAuthStrategy = JWTAuthStrategy()
// let spark = Spark(authenticationStrategy: jwtAuthStrategy)
// if !jwtAuthStrategy.authorized {
// jwtAuthStrategy.authorizedWith(jwt: apiKey)
// if spark.authenticationStrategy.authorized {
// print("Not auth")
// }
//}
authenticationAttempted(true)
}
Spark.phone.disableVideoCodecActivation()
}
/**
Register this instance of the Media SDK with the Spark Cloud for Outgoing Calls
- parameter mediaAccessType: The type of Media that will be sent to the remote party (Audio or Audio Video)
- parameter successfulRegistration: Triggered if the instance of the Media SDK was registered with the Spark Cloud successfully
- parameter unSuccessfulRegistration: Triggered if the instance of the Media SDK was unable to register with the Spark Cloud
*/
func registerDeviceWithSpark(mediaAccessType: Phone.MediaAccessType, successfulRegistration: @escaping (_ successfulRegistration: String ) -> Void, unSuccessfulRegistration: @escaping (_ error: String) -> Void){
Spark.phone.requestMediaAccess(Phone.MediaAccessType.audioVideo) { granted in
if !granted {
print("not granted")
}
}
Spark.phone.register() { success in
if success {
print("success")
successfulRegistration("success")
} else {
print("failure")
unSuccessfulRegistration("failed")
}
}
}
func registerForSparkCallStateNotifications() {
CallNotificationCenter.sharedInstance.add(observer: self)
}
/**
Trigger the Spark call to start
- parameter recipient: The Spark URI or SIP URI of the remote particpiant to be dialled
- parameter mediaAccessType: The type of Media that will be sent to the remote party (Audio or Audio Video)
*/
func startSparkCallTo(recipient: String, mediaAccessType: Phone.MediaAccessType){
self.startCallTimer()
Spark.phone.defaultFacingMode = Call.FacingMode.User
let call = Spark.phone.dial(recipient, option: mediaAccessType == .audio ? MediaOption.audioOnly : MediaOption.audioVideo(local: self.localMediaView, remote: self.remoteMediaView)) { success in
if success {
if mediaAccessType == .audio {
self.callTimerLabel.isHidden = false
self.rotateCameraButton.isEnabled = false
self.toggleButtonVisibilityState()
}
}
else {
print("Failed to dial call.")
self.delegate?.callFailed(withError: "Failed to Dial, check your recipient has a valid address and that you have internet connectivity.")
}
}
self.currentCall = call
}
// Call Notifications
public func callDidBeginRinging(_ call: Call) {
// Your custom logic
}
public func callDidDisconnect(_ call: Call, disconnectionType: DisconnectionType) {
self.dismiss(animated: true, completion: {
self.delegate?.callDidComplete()
})
}
// Button Actions
@IBAction func rotateCameraPressed(_ sender: UIButton) {
self.currentCall.toggleFacingMode()
if self.currentCall.facingMode.hashValue == 0 {
self.rotateCameraButton.setImage(UIImage(named: "rotate"), for: UIControlState())
}
else if self.currentCall.facingMode.hashValue == 1{
self.rotateCameraButton.setImage(UIImage(named: "rotateActive"), for: UIControlState())
}
}
@IBAction func hangupPressed(_ sender: UIButton) {
self.view.backgroundColor = UIColor.black
self.callTimerLabel.isHidden = true
self.currentCall.hangup() { success in
if !success {
print("Failed to hangup call.")
} else {
self.currentCall = nil
self.dismiss(animated: true, completion: {
})
}
}
}
@IBAction func mutePressed(_ sender: UIButton) {
self.currentCall.toggleSendingAudio()
if self.currentCall.sendingAudio {
self.muteButton.setImage(UIImage(named: "mute"), for: UIControlState())
}
else{
self.muteButton.setImage(UIImage(named: "muteActive"), for: UIControlState())
}
}
// Gesture Recognisers
func tap(_ gestureRecognizer: UITapGestureRecognizer) {
self.toggleButtonVisibilityState()
}
func repositionSelfView(_ gestureRecognizer: UIPanGestureRecognizer){
self.updateLocalMediaView(sender: gestureRecognizer)
}
// UI Helpers
func toggleButtonVisibilityState() {
if self.muteButton.isHidden || self.hangupButton.isHidden || self.rotateCameraButton.isHidden {
self.muteButton.isHidden = false
self.hangupButton.isHidden = false
self.rotateCameraButton.isHidden = false
self.callTimerLabel.isHidden = false
}
else {
self.muteButton.isHidden = true
self.hangupButton.isHidden = true
self.rotateCameraButton.isHidden = true
self.callTimerLabel.isHidden = true
}
}
func updateLocalMediaView(sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: self.view)
sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
sender.setTranslation(CGPoint.init(x: 0, y: 0), in: self.view)
}
func startCallTimer() {
self.callTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (timer) in
self.currentCallDuration += 1
self.callTimerLabel.text = SparkMediaHelper.timeStringFromSeconds(currrentCallDuration: self.currentCallDuration)
})
}
fileprivate func showPhoneRegisterFailAlert() {
let alert = UIAlertController(title: "Alert", message: "Phone register fail", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
}