forked from twilio/video-quickstart-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
604 lines (479 loc) · 24 KB
/
ViewController.swift
File metadata and controls
604 lines (479 loc) · 24 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//
// ViewController.swift
// AudioDeviceExample
//
// Copyright © 2018 Twilio Inc. All rights reserved.
//
import TwilioVideo
import UIKit
class ViewController: UIViewController {
// MARK: View Controller Members
// Configure access token manually for testing, if desired! Create one manually in the console
// at https://www.twilio.com/console/video/runtime/testing-tools
var accessToken = "TWILIO_ACCESS_TOKEN"
// Configure remote URL to fetch token from
let tokenUrl = "http://localhost:8000/token.php"
// Video SDK components
var room: TVIRoom?
var camera: TVICameraCapturer?
var localVideoTrack: TVILocalVideoTrack!
var localAudioTrack: TVILocalAudioTrack!
var audioDevice: TVIAudioDevice = ExampleCoreAudioDevice()
// MARK: UI Element Outlets and handles
@IBOutlet weak var audioDeviceButton: UIButton!
@IBOutlet weak var connectButton: UIButton!
@IBOutlet weak var disconnectButton: UIButton!
@IBOutlet weak var musicButton: UIButton!
@IBOutlet weak var messageLabel: UILabel!
@IBOutlet weak var remoteViewStack: UIStackView!
@IBOutlet weak var roomTextField: UITextField!
@IBOutlet weak var roomLine: UIView!
@IBOutlet weak var roomLabel: UILabel!
var messageTimer: Timer!
let kPreviewPadding = CGFloat(10)
let kTextBottomPadding = CGFloat(4)
let kMaxRemoteVideos = Int(2)
static let coreAudioDeviceText = "CoreAudio Device"
static let engineAudioDeviceText = "AVAudioEngine Device"
override func viewDidLoad() {
super.viewDidLoad()
title = "AudioDevice Example"
disconnectButton.isHidden = true
musicButton.isHidden = true
disconnectButton.setTitleColor(UIColor.init(white: 0.75, alpha: 1), for: .disabled)
connectButton.setTitleColor(UIColor.init(white: 0.75, alpha: 1), for: .disabled)
roomTextField.autocapitalizationType = .none
roomTextField.delegate = self
logMessage(messageText: ViewController.coreAudioDeviceText + " selected")
audioDeviceButton.setTitle("CoreAudio Device", for: .normal)
prepareLocalMedia()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func selectAudioDevice() {
var coreAudioDeviceButton : UIAlertAction!
var audioEngineDeviceButton : UIAlertAction?
var selectedButton : UIAlertAction!
let alertController = UIAlertController(title: "Select Audio Device", message: nil, preferredStyle: .actionSheet)
// ExampleCoreAudioDevice
coreAudioDeviceButton = UIAlertAction(title: ViewController.coreAudioDeviceText,
style: .default,
handler: { (action) -> Void in
self.coreAudioDeviceSelected()
})
alertController.addAction(coreAudioDeviceButton!)
// EngineAudioDevice
var audioEngineDeviceTitle = ""
if #available(iOS 11.0, *) {
audioEngineDeviceTitle = ViewController.engineAudioDeviceText
} else {
audioEngineDeviceTitle = "AVAudioEngine Device (iOS 11+ only)"
}
audioEngineDeviceButton = UIAlertAction(title: audioEngineDeviceTitle,
style: .default,
handler: { (action) -> Void in
self.avAudioEngineDeviceSelected()
})
// EXampleAVAudioEngineDevice is supported only on iOS 11+
if let deviceButton = audioEngineDeviceButton {
if #available(iOS 11.0, *) {
deviceButton.isEnabled = true
} else {
deviceButton.isEnabled = false
}
}
alertController.addAction(audioEngineDeviceButton!)
if (self.audioDevice is ExampleCoreAudioDevice) {
selectedButton = coreAudioDeviceButton
} else if #available(iOS 11.0, *) {
if (self.audioDevice is ExampleAVAudioEngineDevice) {
selectedButton = audioEngineDeviceButton
}
}
if UIDevice.current.userInterfaceIdiom == .pad {
alertController.popoverPresentationController?.sourceView = self.audioDeviceButton
alertController.popoverPresentationController?.sourceRect = self.audioDeviceButton.bounds
} else {
selectedButton?.setValue("true", forKey: "checked")
// Adding the cancel action
let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in })
alertController.addAction(cancelButton)
}
self.navigationController!.present(alertController, animated: true, completion: nil)
}
func coreAudioDeviceSelected() {
/*
* To set an audio device on Video SDK, it is necessary to destroyed the media engine first. By cleaning up the
* Room and Tracks the media engine gets destroyed.
*/
self.unprepareLocalMedia()
self.audioDevice = ExampleCoreAudioDevice()
self.audioDeviceButton.setTitle(ViewController.coreAudioDeviceText, for: .normal)
self.logMessage(messageText: ViewController.coreAudioDeviceText + " Selected")
self.prepareLocalMedia()
}
func avAudioEngineDeviceSelected() {
if #available(iOS 11.0, *) {
/*
* To set an audio device on Video SDK, it is necessary to destroyed the media engine first. By cleaning up the
* Room and Tracks the media engine gets destroyed.
*/
self.unprepareLocalMedia()
self.audioDevice = ExampleAVAudioEngineDevice()
self.audioDeviceButton.setTitle(ViewController.engineAudioDeviceText, for: .normal)
self.logMessage(messageText: ViewController.coreAudioDeviceText + " Selected")
self.prepareLocalMedia()
}
}
// MARK: IBActions
@IBAction func connect(sender: AnyObject) {
// Configure access token either from server or manually.
// If the default wasn't changed, try fetching from server.
if (accessToken == "TWILIO_ACCESS_TOKEN") {
do {
accessToken = try TokenUtils.fetchToken(url: tokenUrl)
} catch {
let message = "Failed to fetch access token"
logMessage(messageText: message)
return
}
}
// Preparing the connect options with the access token that we fetched (or hardcoded).
let connectOptions = TVIConnectOptions.init(token: accessToken) { (builder) in
if let videoTrack = self.localVideoTrack {
builder.videoTracks = [videoTrack]
}
// We will share a local audio track only if ExampleAVAudioEngineDevice is selected.
if let audioTrack = self.localAudioTrack {
builder.audioTracks = [audioTrack]
}
// Use the preferred codecs
if let preferredAudioCodec = Settings.shared.audioCodec {
builder.preferredAudioCodecs = [preferredAudioCodec.rawValue]
}
if let preferredVideoCodec = Settings.shared.videoCodec {
builder.preferredVideoCodecs = [preferredVideoCodec.rawValue]
}
// Use the preferred encoding parameters
if let encodingParameters = Settings.shared.getEncodingParameters() {
builder.encodingParameters = encodingParameters
}
// The name of the Room where the Client will attempt to connect to. Please note that if you pass an empty
// Room `name`, the Client will create one for you. You can get the name or sid from any connected Room.
builder.roomName = self.roomTextField.text
}
// Connect to the Room using the options we provided.
room = TwilioVideo.connect(with: connectOptions, delegate: self)
logMessage(messageText: "Connecting to \(roomTextField.text ?? "a Room")")
self.showRoomUI(inRoom: true)
self.dismissKeyboard()
}
@IBAction func disconnect(sender: UIButton) {
if let room = self.room {
logMessage(messageText: "Disconnecting from \(room.name)")
room.disconnect()
sender.isEnabled = false
}
}
@IBAction func playMusic(sender: UIButton) {
if #available(iOS 11.0, *) {
if let audioDevice = self.audioDevice as? ExampleAVAudioEngineDevice {
audioDevice.playMusic()
}
}
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// Layout the preview view.
if let previewView = self.camera?.previewView {
let dimensions = previewView.videoDimensions
var previewBounds = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 160, height: 160))
previewBounds = AVMakeRect(aspectRatio: CGSize.init(width: CGFloat(dimensions.width), height: CGFloat(dimensions.height)),
insideRect: previewBounds)
previewBounds = previewBounds.integral
previewView.bounds = previewBounds
previewView.center = CGPoint.init(x: view.bounds.width - previewBounds.width / 2 - kPreviewPadding,
y: view.bounds.height - previewBounds.height / 2 - kPreviewPadding)
}
}
override func prefersHomeIndicatorAutoHidden() -> Bool {
return self.room != nil
}
override var prefersStatusBarHidden: Bool {
return self.room != nil
}
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
if (newCollection.horizontalSizeClass == .regular ||
(newCollection.horizontalSizeClass == .compact && newCollection.verticalSizeClass == .compact)) {
remoteViewStack.axis = .horizontal
} else {
remoteViewStack.axis = .vertical
}
}
// Update our UI based upon if we are in a Room or not
func showRoomUI(inRoom: Bool) {
self.connectButton.isHidden = inRoom
self.connectButton.isEnabled = !inRoom
self.roomTextField.isHidden = inRoom
self.roomLine.isHidden = inRoom
self.roomLabel.isHidden = inRoom
self.disconnectButton.isHidden = !inRoom
self.disconnectButton.isEnabled = inRoom
UIApplication.shared.isIdleTimerDisabled = inRoom
self.audioDeviceButton.isHidden = inRoom
self.audioDeviceButton.isEnabled = !inRoom
if #available(iOS 11.0, *) {
if ((self.audioDevice as? ExampleAVAudioEngineDevice) != nil) {
self.musicButton.isHidden = !inRoom
self.musicButton.isEnabled = inRoom
}
self.setNeedsUpdateOfHomeIndicatorAutoHidden()
}
self.setNeedsStatusBarAppearanceUpdate()
self.navigationController?.setNavigationBarHidden(inRoom, animated: true)
}
func dismissKeyboard() {
if (self.roomTextField.isFirstResponder) {
self.roomTextField.resignFirstResponder()
}
}
func logMessage(messageText: String) {
messageLabel.text = messageText
if (messageLabel.alpha < 1.0) {
self.messageLabel.isHidden = false
UIView.animate(withDuration: 0.4, animations: {
self.messageLabel.alpha = 1.0
})
}
// Hide the message with a delay.
self.messageTimer?.invalidate()
self.messageTimer = Timer.init(timeInterval: TimeInterval(6),
target: self,
selector: #selector(hideMessageLabel),
userInfo: nil,
repeats: false)
RunLoop.main.add(self.messageTimer, forMode: .commonModes)
}
@objc func hideMessageLabel() {
if (self.messageLabel.isHidden == false) {
UIView.animate(withDuration: 0.6, animations: {
self.messageLabel.alpha = 0
}, completion: { (complete) in
if (complete) {
self.messageLabel.isHidden = true
}
})
}
}
func prepareLocalMedia() {
/*
* The important thing to remember when using a custom TVIAudioDevice is that the device must be set
* before performing any other actions with the SDK (such as creating Tracks, or connecting to a Room).
*/
TwilioVideo.audioDevice = self.audioDevice
if #available(iOS 11.0, *) {
// Only the ExampleAVAudioEngineDevice supports local audio capturing.
if (TwilioVideo.audioDevice is ExampleAVAudioEngineDevice) {
localAudioTrack = TVILocalAudioTrack()
}
}
/*
* ExampleCoreAudioDevice is a playback only device. Because of this, any attempts to create a
* TVILocalAudioTrack will result in an exception being thrown. In this example we will only share video
* (where available) and not audio.
*/
if (TVICameraCapturer.isSourceAvailable(TVICameraCaptureSource.frontCamera)) {
// We will render the camera using TVICameraPreviewView.
camera = TVICameraCapturer(source: .frontCamera, delegate: self, enablePreview: true)
localVideoTrack = TVILocalVideoTrack.init(capturer: camera!)
if (localVideoTrack == nil) {
logMessage(messageText: "Failed to create video track!")
} else {
logMessage(messageText: "Video track created.")
if let preview = camera?.previewView {
view.addSubview(preview);
}
}
} else {
logMessage(messageText: "Front camera is not available, using audio only.")
}
}
func unprepareLocalMedia() {
self.room = nil
self.localAudioTrack = nil
self.localVideoTrack = nil
self.camera?.previewView .removeFromSuperview()
self.camera = nil;
}
func setupRemoteVideoView(publication: TVIRemoteVideoTrackPublication) {
// Create a `TVIVideoView` programmatically, and add to our `UIStackView`
if let remoteView = TVIVideoView.init(frame: CGRect.zero, delegate:nil) {
// We will bet that a hash collision between two unique SIDs is very rare.
remoteView.tag = publication.trackSid.hashValue
// `TVIVideoView` supports scaleToFill, scaleAspectFill and scaleAspectFit.
// scaleAspectFit is the default mode when you create `TVIVideoView` programmatically.
remoteView.contentMode = .scaleAspectFit;
// Double tap to change the content mode.
let recognizerDoubleTap = UITapGestureRecognizer(target: self, action: #selector(ViewController.changeRemoteVideoAspect))
recognizerDoubleTap.numberOfTapsRequired = 2
remoteView.addGestureRecognizer(recognizerDoubleTap)
// Start rendering, and add to our stack.
publication.remoteTrack?.addRenderer(remoteView)
self.remoteViewStack.addArrangedSubview(remoteView)
}
}
func removeRemoteVideoView(publication: TVIRemoteVideoTrackPublication) {
let viewTag = publication.trackSid.hashValue
if let remoteView = self.remoteViewStack.viewWithTag(viewTag) {
// Stop rendering, we don't want to receive any more frames.
publication.remoteTrack?.removeRenderer(remoteView as! TVIVideoRenderer)
// Automatically removes us from the UIStackView's arranged subviews.
remoteView.removeFromSuperview()
}
}
func changeRemoteVideoAspect(gestureRecognizer: UIGestureRecognizer) {
guard let remoteView = gestureRecognizer.view else {
print("Couldn't find a view attached to the tap recognizer. \(gestureRecognizer)")
return;
}
if (remoteView.contentMode == .scaleAspectFit) {
remoteView.contentMode = .scaleAspectFill
} else {
remoteView.contentMode = .scaleAspectFit
}
}
}
// MARK: UITextFieldDelegate
extension ViewController : UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.connect(sender: textField)
return true
}
}
// MARK: TVIRoomDelegate
extension ViewController : TVIRoomDelegate {
func didConnect(to room: TVIRoom) {
// Listen to events from existing `TVIRemoteParticipant`s
for remoteParticipant in room.remoteParticipants {
remoteParticipant.delegate = self
}
let connectMessage = "Connected to room \(room.name) as \(room.localParticipant?.identity ?? "")."
logMessage(messageText: connectMessage)
}
func room(_ room: TVIRoom, didDisconnectWithError error: Error?) {
if let disconnectError = error {
logMessage(messageText: "Disconnected from \(room.name).\ncode = \((disconnectError as NSError).code) error = \(disconnectError.localizedDescription)")
} else {
logMessage(messageText: "Disconnected from \(room.name)")
}
self.room = nil
self.showRoomUI(inRoom: false)
}
func room(_ room: TVIRoom, didFailToConnectWithError error: Error) {
logMessage(messageText: "Failed to connect to Room:\n\(error.localizedDescription)")
self.room = nil
self.showRoomUI(inRoom: false)
}
func room(_ room: TVIRoom, participantDidConnect participant: TVIRemoteParticipant) {
participant.delegate = self
logMessage(messageText: "Participant \(participant.identity) connected with \(participant.remoteAudioTracks.count) audio and \(participant.remoteVideoTracks.count) video tracks")
}
func room(_ room: TVIRoom, participantDidDisconnect participant: TVIRemoteParticipant) {
logMessage(messageText: "Room \(room.name), Participant \(participant.identity) disconnected")
}
}
// MARK: TVIRemoteParticipantDelegate
extension ViewController : TVIRemoteParticipantDelegate {
func remoteParticipant(_ participant: TVIRemoteParticipant,
publishedVideoTrack publication: TVIRemoteVideoTrackPublication) {
// Remote Participant has offered to share the video Track.
logMessage(messageText: "Participant \(participant.identity) published \(publication.trackName) video track")
}
func remoteParticipant(_ participant: TVIRemoteParticipant,
unpublishedVideoTrack publication: TVIRemoteVideoTrackPublication) {
// Remote Participant has stopped sharing the video Track.
logMessage(messageText: "Participant \(participant.identity) unpublished \(publication.trackName) video track")
}
func remoteParticipant(_ participant: TVIRemoteParticipant,
publishedAudioTrack publication: TVIRemoteAudioTrackPublication) {
// Remote Participant has offered to share the audio Track.
logMessage(messageText: "Participant \(participant.identity) published \(publication.trackName) audio track")
}
func remoteParticipant(_ participant: TVIRemoteParticipant,
unpublishedAudioTrack publication: TVIRemoteAudioTrackPublication) {
// Remote Participant has stopped sharing the audio Track.
logMessage(messageText: "Participant \(participant.identity) unpublished \(publication.trackName) audio track")
}
func subscribed(to videoTrack: TVIRemoteVideoTrack,
publication: TVIRemoteVideoTrackPublication,
for participant: TVIRemoteParticipant) {
// We are subscribed to the remote Participant's video Track. We will start receiving the
// remote Participant's video frames now.
logMessage(messageText: "Subscribed to \(publication.trackName) video track for Participant \(participant.identity)")
// Start remote rendering, and add a touch handler.
if (self.remoteViewStack.arrangedSubviews.count < kMaxRemoteVideos) {
setupRemoteVideoView(publication: publication)
}
}
func unsubscribed(from videoTrack: TVIRemoteVideoTrack,
publication: TVIRemoteVideoTrackPublication,
for participant: TVIRemoteParticipant) {
// We are unsubscribed from the remote Participant's video Track. We will no longer receive the
// remote Participant's video.
logMessage(messageText: "Unsubscribed from \(publication.trackName) video track for Participant \(participant.identity)")
// Stop remote rendering.
removeRemoteVideoView(publication: publication)
}
func subscribed(to audioTrack: TVIRemoteAudioTrack,
publication: TVIRemoteAudioTrackPublication,
for participant: TVIRemoteParticipant) {
// We are subscribed to the remote Participant's audio Track. We will start receiving the
// remote Participant's audio now.
logMessage(messageText: "Subscribed to \(publication.trackName) audio track for Participant \(participant.identity)")
}
func unsubscribed(from audioTrack: TVIRemoteAudioTrack,
publication: TVIRemoteAudioTrackPublication,
for participant: TVIRemoteParticipant) {
// We are unsubscribed from the remote Participant's audio Track. We will no longer receive the
// remote Participant's audio.
logMessage(messageText: "Unsubscribed from \(publication.trackName) audio track for Participant \(participant.identity)")
}
func remoteParticipant(_ participant: TVIRemoteParticipant,
enabledVideoTrack publication: TVIRemoteVideoTrackPublication) {
logMessage(messageText: "Participant \(participant.identity) enabled \(publication.trackName) video track")
}
func remoteParticipant(_ participant: TVIRemoteParticipant,
disabledVideoTrack publication: TVIRemoteVideoTrackPublication) {
logMessage(messageText: "Participant \(participant.identity) disabled \(publication.trackName) video track")
}
func remoteParticipant(_ participant: TVIRemoteParticipant,
enabledAudioTrack publication: TVIRemoteAudioTrackPublication) {
logMessage(messageText: "Participant \(participant.identity) enabled \(publication.trackName) audio track")
}
func remoteParticipant(_ participant: TVIRemoteParticipant,
disabledAudioTrack publication: TVIRemoteAudioTrackPublication) {
// We will continue to record silence and/or recognize audio while a Track is disabled.
logMessage(messageText: "Participant \(participant.identity) disabled \(publication.trackName) audio track")
}
func failedToSubscribe(toAudioTrack publication: TVIRemoteAudioTrackPublication,
error: Error,
for participant: TVIRemoteParticipant) {
logMessage(messageText: "FailedToSubscribe \(publication.trackName) audio track, error = \(String(describing: error))")
}
func failedToSubscribe(toVideoTrack publication: TVIRemoteVideoTrackPublication,
error: Error,
for participant: TVIRemoteParticipant) {
logMessage(messageText: "FailedToSubscribe \(publication.trackName) video track, error = \(String(describing: error))")
}
}
extension ViewController : TVICameraCapturerDelegate {
func cameraCapturer(_ capturer: TVICameraCapturer, didStartWith source: TVICameraCaptureSource) {
// Layout the camera preview with dimensions appropriate for our orientation.
self.view.setNeedsLayout()
}
func cameraCapturer(_ capturer: TVICameraCapturer, didFailWithError error: Error) {
logMessage(messageText: "Capture failed with error.\ncode = \((error as NSError).code) error = \(error.localizedDescription)")
capturer.previewView.removeFromSuperview()
}
}