-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathViewController.swift
375 lines (319 loc) · 14.8 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
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
//
// ViewController.swift
// SimpleWebRTC
//
// Created by n0 on 2019/01/05.
// Copyright © 2019年 n0. All rights reserved.
//
import UIKit
import Starscream
import WebRTC
import UIKit
class ViewController: UIViewController, WebSocketDelegate, WebRTCClientDelegate, CameraSessionDelegate {
enum messageType {
case greet
case introduce
func text() -> String {
switch self {
case .greet:
return "Hello!"
case .introduce:
return "I'm " + UIDevice.modelName
}
}
}
//MARK: - Properties
var webRTCClient: WebRTCClient!
var socket: WebSocket!
var tryToConnectWebSocket: Timer!
var cameraSession: CameraSession?
// You can create video source from CMSampleBuffer :)
var useCustomCapturer: Bool = false
var cameraFilter: CameraFilter?
// Constants
// MARK: Change this ip address in your case
let ipAddress: String = "192.168.1.189"
let wsStatusMessageBase = "WebSocket: "
let webRTCStatusMesasgeBase = "WebRTC: "
let likeStr: String = "Like"
// UI
var wsStatusLabel: UILabel!
var webRTCStatusLabel: UILabel!
var webRTCMessageLabel: UILabel!
var likeImage: UIImage!
var likeImageViewRect: CGRect!
//MARK: - ViewController Override Methods
override func viewDidLoad() {
super.viewDidLoad()
#if targetEnvironment(simulator)
// simulator does not have camera
self.useCustomCapturer = false
#endif
webRTCClient = WebRTCClient()
webRTCClient.delegate = self
webRTCClient.setup(videoTrack: true, audioTrack: true, dataChannel: true, customFrameCapturer: useCustomCapturer)
if useCustomCapturer {
print("--- use custom capturer ---")
self.cameraSession = CameraSession()
self.cameraSession?.delegate = self
self.cameraSession?.setupSession()
self.cameraFilter = CameraFilter()
}
socket = WebSocket(url: URL(string: "ws://" + ipAddress + ":8080/")!)
socket.delegate = self
tryToConnectWebSocket = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (timer) in
if self.webRTCClient.isConnected || self.socket.isConnected {
return
}
self.socket.connect()
})
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
self.setupUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - UI
private func setupUI(){
let remoteVideoViewContainter = UIView(frame: CGRect(x: 0, y: 0, width: ScreenSizeUtil.width(), height: ScreenSizeUtil.height()*0.7))
remoteVideoViewContainter.backgroundColor = .gray
self.view.addSubview(remoteVideoViewContainter)
let remoteVideoView = webRTCClient.remoteVideoView()
webRTCClient.setupRemoteViewFrame(frame: CGRect(x: 0, y: 0, width: ScreenSizeUtil.width()*0.7, height: ScreenSizeUtil.height()*0.7))
remoteVideoView.center = remoteVideoViewContainter.center
remoteVideoViewContainter.addSubview(remoteVideoView)
let localVideoView = webRTCClient.localVideoView()
webRTCClient.setupLocalViewFrame(frame: CGRect(x: 0, y: 0, width: ScreenSizeUtil.width()/3, height: ScreenSizeUtil.height()/3))
localVideoView.center.y = self.view.center.y
localVideoView.subviews.last?.isUserInteractionEnabled = true
self.view.addSubview(localVideoView)
let localVideoViewButton = UIButton(frame: CGRect(x: 0, y: 0, width: localVideoView.frame.width, height: localVideoView.frame.height))
localVideoViewButton.backgroundColor = UIColor.clear
localVideoViewButton.addTarget(self, action: #selector(self.localVideoViewTapped(_:)), for: .touchUpInside)
localVideoView.addSubview(localVideoViewButton)
let likeButton = UIButton(frame: CGRect(x: remoteVideoViewContainter.right - 50, y: remoteVideoViewContainter.bottom - 50, width: 40, height: 40))
likeButton.backgroundColor = UIColor.clear
likeButton.addTarget(self, action: #selector(self.likeButtonTapped(_:)), for: .touchUpInside)
self.view.addSubview(likeButton)
likeButton.setImage(UIImage(named: "like_border.png"), for: .normal)
likeImage = UIImage(named: "like_filled.png")
likeImageViewRect = CGRect(x: remoteVideoViewContainter.right - 70, y: likeButton.top - 70, width: 60, height: 60)
let messageButton = UIButton(frame: CGRect(x: likeButton.left - 220, y: remoteVideoViewContainter.bottom - 50, width: 210, height: 40))
messageButton.setBackgroundImage(UIColor.green.rectImage(width: messageButton.frame.width, height: messageButton.frame.height), for: .normal)
messageButton.addTarget(self, action: #selector(self.sendMessageButtonTapped(_:)), for: .touchUpInside)
messageButton.titleLabel?.adjustsFontSizeToFitWidth = true
messageButton.setTitle(messageType.greet.text(), for: .normal)
messageButton.layer.cornerRadius = 20
messageButton.layer.masksToBounds = true
self.view.addSubview(messageButton)
wsStatusLabel = UILabel(frame: CGRect(x: 0, y: remoteVideoViewContainter.bottom, width: ScreenSizeUtil.width(), height: 30))
wsStatusLabel.textAlignment = .center
self.view.addSubview(wsStatusLabel)
webRTCStatusLabel = UILabel(frame: CGRect(x: 0, y: wsStatusLabel.bottom, width: ScreenSizeUtil.width(), height: 30))
webRTCStatusLabel.textAlignment = .center
webRTCStatusLabel.text = webRTCStatusMesasgeBase + "initialized"
self.view.addSubview(webRTCStatusLabel)
webRTCMessageLabel = UILabel(frame: CGRect(x: 0, y: webRTCStatusLabel.bottom, width: ScreenSizeUtil.width(), height: 30))
webRTCMessageLabel.textAlignment = .center
webRTCMessageLabel.textColor = .black
self.view.addSubview(webRTCMessageLabel)
let buttonWidth = ScreenSizeUtil.width()*0.4
let buttonHeight: CGFloat = 60
let buttonRadius: CGFloat = 30
let callButton = UIButton(frame: CGRect(x: 0, y: 0, width: buttonWidth, height: buttonHeight))
callButton.setBackgroundImage(UIColor.blue.rectImage(width: callButton.frame.width, height: callButton.frame.height), for: .normal)
callButton.layer.cornerRadius = buttonRadius
callButton.layer.masksToBounds = true
callButton.center.x = ScreenSizeUtil.width()/4
callButton.center.y = webRTCStatusLabel.bottom + (ScreenSizeUtil.height() - webRTCStatusLabel.bottom)/2
callButton.setTitle("Call", for: .normal)
callButton.titleLabel?.font = UIFont.systemFont(ofSize: 23)
callButton.addTarget(self, action: #selector(self.callButtonTapped(_:)), for: .touchUpInside)
self.view.addSubview(callButton)
let hangupButton = UIButton(frame: CGRect(x: 0, y: 0, width: buttonWidth, height: buttonHeight))
hangupButton.setBackgroundImage(UIColor.red.rectImage(width: hangupButton.frame.width, height: hangupButton.frame.height), for: .normal)
hangupButton.layer.cornerRadius = buttonRadius
hangupButton.layer.masksToBounds = true
hangupButton.center.x = ScreenSizeUtil.width()/4 * 3
hangupButton.center.y = callButton.center.y
hangupButton.setTitle("hang up" , for: .normal)
hangupButton.titleLabel?.font = UIFont.systemFont(ofSize: 22)
hangupButton.addTarget(self, action: #selector(self.hangupButtonTapped(_:)), for: .touchUpInside)
self.view.addSubview(hangupButton)
}
// MARK: - UI Events
@objc func callButtonTapped(_ sender: UIButton){
if !webRTCClient.isConnected {
webRTCClient.connect(onSuccess: { (offerSDP: RTCSessionDescription) -> Void in
self.sendSDP(sessionDescription: offerSDP)
})
}
}
@objc func hangupButtonTapped(_ sender: UIButton){
if webRTCClient.isConnected {
webRTCClient.disconnect()
}
}
@objc func sendMessageButtonTapped(_ sender: UIButton){
webRTCClient.sendMessge(message: (sender.titleLabel?.text!)!)
if sender.titleLabel?.text == messageType.greet.text() {
sender.setTitle(messageType.introduce.text(), for: .normal)
}else if sender.titleLabel?.text == messageType.introduce.text() {
sender.setTitle(messageType.greet.text(), for: .normal)
}
}
@objc func likeButtonTapped(_ sender: UIButton){
let data = likeStr.data(using: String.Encoding.utf8)
webRTCClient.sendData(data: data!)
}
@objc func localVideoViewTapped(_ sender: UITapGestureRecognizer) {
// if let filter = self.cameraFilter {
// filter.changeFilter(filter.filterType.next())
// }
webRTCClient.switchCameraPosition()
}
private func startLikeAnimation(){
let likeImageView = UIImageView(frame: likeImageViewRect)
likeImageView.backgroundColor = UIColor.clear
likeImageView.contentMode = .scaleAspectFit
likeImageView.image = likeImage
likeImageView.alpha = 1.0
self.view.addSubview(likeImageView)
UIView.animate(withDuration: 0.5, animations: {
likeImageView.alpha = 0.0
}) { (reuslt) in
likeImageView.removeFromSuperview()
}
}
// MARK: - WebRTC Signaling
private func sendSDP(sessionDescription: RTCSessionDescription){
var type = ""
if sessionDescription.type == .offer {
type = "offer"
}else if sessionDescription.type == .answer {
type = "answer"
}
let sdp = SDP.init(sdp: sessionDescription.sdp)
let signalingMessage = SignalingMessage.init(type: type, sessionDescription: sdp, candidate: nil)
do {
let data = try JSONEncoder().encode(signalingMessage)
let message = String(data: data, encoding: String.Encoding.utf8)!
if self.socket.isConnected {
self.socket.write(string: message)
}
}catch{
print(error)
}
}
private func sendCandidate(iceCandidate: RTCIceCandidate){
let candidate = Candidate.init(sdp: iceCandidate.sdp, sdpMLineIndex: iceCandidate.sdpMLineIndex, sdpMid: iceCandidate.sdpMid!)
let signalingMessage = SignalingMessage.init(type: "candidate", sessionDescription: nil, candidate: candidate)
do {
let data = try JSONEncoder().encode(signalingMessage)
let message = String(data: data, encoding: String.Encoding.utf8)!
if self.socket.isConnected {
self.socket.write(string: message)
}
}catch{
print(error)
}
}
}
// MARK: - WebSocket Delegate
extension ViewController {
func websocketDidConnect(socket: WebSocketClient) {
print("-- websocket did connect --")
wsStatusLabel.text = wsStatusMessageBase + "connected"
wsStatusLabel.textColor = .green
}
func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
print("-- websocket did disconnect --")
wsStatusLabel.text = wsStatusMessageBase + "disconnected"
wsStatusLabel.textColor = .red
}
func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
do{
let signalingMessage = try JSONDecoder().decode(SignalingMessage.self, from: text.data(using: .utf8)!)
if signalingMessage.type == "offer" {
webRTCClient.receiveOffer(offerSDP: RTCSessionDescription(type: .offer, sdp: (signalingMessage.sessionDescription?.sdp)!), onCreateAnswer: {(answerSDP: RTCSessionDescription) -> Void in
self.sendSDP(sessionDescription: answerSDP)
})
}else if signalingMessage.type == "answer" {
webRTCClient.receiveAnswer(answerSDP: RTCSessionDescription(type: .answer, sdp: (signalingMessage.sessionDescription?.sdp)!))
}else if signalingMessage.type == "candidate" {
let candidate = signalingMessage.candidate!
webRTCClient.receiveCandidate(candidate: RTCIceCandidate(sdp: candidate.sdp, sdpMLineIndex: candidate.sdpMLineIndex, sdpMid: candidate.sdpMid))
}
}catch{
print(error)
}
}
func websocketDidReceiveData(socket: WebSocketClient, data: Data) { }
}
// MARK: - WebRTCClient Delegate
extension ViewController {
func didGenerateCandidate(iceCandidate: RTCIceCandidate) {
self.sendCandidate(iceCandidate: iceCandidate)
}
func didIceConnectionStateChanged(iceConnectionState: RTCIceConnectionState) {
var state = ""
switch iceConnectionState {
case .checking:
state = "checking..."
case .closed:
state = "closed"
case .completed:
state = "completed"
case .connected:
state = "connected"
case .count:
state = "count..."
case .disconnected:
state = "disconnected"
case .failed:
state = "failed"
case .new:
state = "new..."
}
self.webRTCStatusLabel.text = self.webRTCStatusMesasgeBase + state
}
func didConnectWebRTC() {
self.webRTCStatusLabel.textColor = .green
// MARK: Disconnect websocket
self.socket.disconnect()
}
func didDisconnectWebRTC() {
self.webRTCStatusLabel.textColor = .red
}
func didOpenDataChannel() {
print("did open data channel")
}
func didReceiveData(data: Data) {
if data == likeStr.data(using: String.Encoding.utf8) {
self.startLikeAnimation()
}
}
func didReceiveMessage(message: String) {
self.webRTCMessageLabel.text = message
}
}
// MARK: - CameraSessionDelegate
extension ViewController {
func didOutput(_ sampleBuffer: CMSampleBuffer) {
if self.useCustomCapturer {
if let cvpixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer){
if let buffer = self.cameraFilter?.apply(cvpixelBuffer){
self.webRTCClient.captureCurrentFrame(sampleBuffer: buffer)
}else{
print("no applied image")
}
}else{
print("no pixelbuffer")
}
// self.webRTCClient.captureCurrentFrame(sampleBuffer: buffer)
}
}
}