-
Notifications
You must be signed in to change notification settings - Fork 6
/
ViewController.swift
105 lines (70 loc) · 2.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
//
// ViewController.swift
//
// Created by Matt Goodson on 26/06/17.
// Copyright © 2017 Matt Goodson. All rights reserved.
//
import UIKit
struct Platform {
static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0 // Use this line in Xcode 7 or newer
}
}
class ViewController: UIViewController {
var toId = "Sim";
var myId = "Device";
var withVideo = true;
var url = "ws://<INSERT-MAC-NAME>.local:9090"
var webrtc:RTClient?
var remoteView:RTCEAGLVideoView = RTCEAGLVideoView(frame: UIScreen.main.bounds);
let localView:UIView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 150.0, height: 150.0));
var previewLayer:AVCaptureVideoPreviewLayer?
var remoteVideoTrack:RTCVideoTrack?
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.remoteView);
//This example is set up to make a call between a device and a simulator
//It assumes that the simulator has no camera.
if Platform.isSimulator {
print("Running on Simulator")
self.withVideo = false;
self.toId = "Device";
self.myId = "Sim";
}
else {
print("Running on device");
}
self.webrtc = RTClient(withVideo: self.withVideo, url: url, id: myId, delegate: self);
self.webrtc?.connect();
}
internal func joinPeer() {
self.webrtc?.joinPeer(withId: self.toId);
}
internal func setLocalPreview() {
if (self.withVideo) {
self.previewLayer = self.webrtc?.getLocalPreview();
self.previewLayer?.frame = self.localView.bounds;
self.localView.layer.addSublayer(self.previewLayer!);
self.view.addSubview(self.localView);
}
}
}
extension ViewController:RTClientDelegate {
func addedRemoteVideoTrack(videoTrack:RTCVideoTrack) {
if (self.remoteVideoTrack != nil) {
self.remoteVideoTrack?.remove(self.remoteView);
self.remoteVideoTrack = nil;
self.remoteView.renderFrame(nil);
}
self.remoteVideoTrack = videoTrack;
self.remoteVideoTrack?.add(self.remoteView);
}
func didConnect() {
self.setLocalPreview();
self.joinPeer();
}
func didDisconnect() {
//Tidy up
print("Disconnected");
}
}