forked from twilio/video-quickstart-ios
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathViewController.swift
135 lines (109 loc) · 4.43 KB
/
ViewController.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
//
// ViewController.swift
// ScreenCapturerExample
//
// Copyright © 2016-2017 Twilio, Inc. All rights reserved.
//
import TwilioVideo
import UIKit
import WebKit
import AVFoundation
class ViewController : UIViewController {
var localVideoTrack: TVILocalVideoTrack?
var remoteView: TVIVideoView?
var screenCapturer: TVIVideoCapturer?
var webView: WKWebView?
var webNavigation: WKNavigation?
// Set this value to 'true' to use ExampleScreenCapturer instead of TVIScreenCapturer.
let useExampleCapturer = false
override func viewDidLoad() {
super.viewDidLoad()
// Setup a WKWebView, and request Twilio's website
webView = WKWebView.init(frame: view.frame)
webView?.navigationDelegate = self
webView?.translatesAutoresizingMaskIntoConstraints = false
webView?.allowsBackForwardNavigationGestures = true
self.view.addSubview(webView!)
let requestURL: URL = URL(string: "https://twilio.com")!
let request = URLRequest.init(url: requestURL)
webNavigation = webView?.load(request)
setupLocalMedia()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override var prefersStatusBarHidden: Bool {
return true
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
webView?.frame = self.view.bounds
// Layout the remote video using frame based techniques. It's also possible to do this using autolayout
if ((remoteView?.hasVideoData)!) {
let dimensions = remoteView?.videoDimensions
let remoteRect = remoteViewSize()
let aspect = CGSize(width: CGFloat((dimensions?.width)!), height: CGFloat((dimensions?.height)!))
let padding : CGFloat = 10.0
let boundedRect = AVMakeRect(aspectRatio: aspect, insideRect: remoteRect).integral
remoteView?.frame = CGRect(x: self.view.bounds.width - boundedRect.width - padding,
y: self.view.bounds.height - boundedRect.height - padding,
width: boundedRect.width,
height: boundedRect.height)
} else {
remoteView?.frame = CGRect.zero
}
}
func setupLocalMedia() {
// Setup screen capturer
let capturer: TVIVideoCapturer
if (useExampleCapturer) {
capturer = ExampleScreenCapturer.init(aView: self.webView!)
} else {
capturer = TVIScreenCapturer.init(view: self.webView!)
}
localVideoTrack = TVILocalVideoTrack.init(capturer: capturer)
if (localVideoTrack == nil) {
presentError(message: "Failed to add screen capturer track!")
return;
}
screenCapturer = capturer;
// Setup rendering
remoteView = TVIVideoView.init(frame: CGRect.zero, delegate: self)
localVideoTrack?.addRenderer(remoteView!)
remoteView!.isHidden = true
self.view.addSubview(self.remoteView!)
self.view.setNeedsLayout()
}
func presentError( message: String) {
print(message)
}
func remoteViewSize() -> CGRect {
let traits = self.traitCollection
let width = traits.horizontalSizeClass == UIUserInterfaceSizeClass.regular ? 188 : 160;
let height = traits.horizontalSizeClass == UIUserInterfaceSizeClass.regular ? 188 : 120;
return CGRect(x: 0, y: 0, width: width, height: height)
}
}
// MARK: WKNavigationDelegate
extension ViewController : WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("WebView:", webView, "finished navigation:", navigation)
self.navigationItem.title = webView.title
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
let message = String(format: "WebView:", webView, "did fail navigation:", navigation, error as CVarArg)
presentError(message: message)
}
}
// MARK: TVIVideoViewDelegate
extension ViewController : TVIVideoViewDelegate {
func videoViewDidReceiveData(_ view: TVIVideoView) {
if (view == remoteView) {
remoteView?.isHidden = false
self.view.setNeedsLayout()
}
}
func videoView(_ view: TVIVideoView, videoDimensionsDidChange dimensions: CMVideoDimensions) {
self.view.setNeedsLayout()
}
}