-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathVideoViewController.swift
417 lines (314 loc) · 14.7 KB
/
VideoViewController.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
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
//
// VideoViewController.swift
// AntMediaReferenceApplication
//
// Copyright © 2018 AntMedia. All rights reserved.
//
import UIKit
import WebRTC
import AVFoundation
import WebRTCiOSSDK
class VideoViewController: UIViewController {
@IBOutlet weak var pipVideoView: UIView!
@IBOutlet weak var fullVideoView: UIView!
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var modeLabel: UILabel!
// Auto Layout Constraints used for animations
@IBOutlet weak var containerLeftConstraint: NSLayoutConstraint?
var client: AntMediaClient?;
var clientUrl: String!
var clientStreamId: String!
var clientToken: String!
var clientMode: AntMediaClientMode!
var tapGesture: UITapGestureRecognizer!
var readerOutput: AVAssetReaderTrackOutput?;
var reader: AVAssetReader?;
var audioFileUrl: URL? = nil ;
override func viewDidLoad() {
super.viewDidLoad()
self.setGesture()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//NOTE for the one who wants to sends mp3. Just comment out below lines and return the method including return below
//let audioFileURL = Bundle.main.url(forResource: "michael_jackson_audio_mono", withExtension: "mp3")!
//self.initAntMediaClientForExternalAudio(fileUrl: audioFileURL);
//self.modeLabel.text = "Mode: Audio Sending"
//return;
self.client = AntMediaClient.init()
self.client!.delegate = self
self.client!.setDebug(true)
self.client!.setOptions(url: self.clientUrl, streamId: self.clientStreamId, token: self.clientToken, mode: self.clientMode, enableDataChannel: true, useExternalCameraSource:false)
//this should be enabled when an audio app or broadcast extension is used.
//Please check the sample in ScreenShare
self.client!.setExternalAudio(externalAudioEnabled: false);
//set default stunserver or turn server
//let iceServer:RTCIceServer = RTCIceServer.init(urlStrings: ["stun:stun.l.google.com:19302"], username: "", credential: "")
//Config.setDefaultStunServer(server: iceServer);
//self.client.setMaxVideoBps(videoBitratePerSecond: 500000)
/*
Enable the below line if you use multi peer node for embedded sdk
*/
//self.client.setMultiPeerMode(enable: true, mode: "play")
if self.client!.getCurrentMode() == AntMediaClientMode.join {
self.modeLabel.text = "Mode: P2P"
self.pipVideoView.isHidden = false
self.fullVideoView.isHidden = false
self.client!.setLocalView(container: pipVideoView)
self.client!.setRemoteView(remoteContainer: fullVideoView)
self.client!.start();
} else if self.client!.getCurrentMode() == AntMediaClientMode.publish {
// self.client.setVideoEnable(enable: true);
self.pipVideoView.isHidden = false
self.fullVideoView.isHidden = false
self.modeLabel.text = "Mode: Publish"
self.client!.setCameraPosition(position: .front)
self.client!.setTargetResolution(width: 640, height: 360)
self.client!.setLocalView(container: fullVideoView, mode: .scaleAspectFit)
//calling this method is not necessary. It just initializes the connection and opens the camera
self.client!.initPeerConnection(streamId: self.clientStreamId);
//Enable below method to have the mirror effect
//self.mirrorView(view: fullVideoView);
self.client!.publish(streamId: self.clientStreamId);
} else if self.client!.getCurrentMode() == AntMediaClientMode.play {
self.fullVideoView.isHidden = false
self.pipVideoView.isHidden = false
self.client!.setRemoteView(remoteContainer: fullVideoView, mode: .scaleAspectFit)
self.modeLabel.text = "Mode: Play"
self.client!.play(streamId:self.clientStreamId);
}
}
/*
* Mirror the view. fullVideoView or pipViewVideo can provided as parameter
*/
private func mirrorView(view:UIView) {
view.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
}
@IBAction func audioTapped(_ sender: UIButton!) {
sender.isSelected = !sender.isSelected
if (self.client?.getCurrentMode() == .play) {
AntMediaClient.printf("It's in play mode and calling to mute/unmute the incoming stream with enableAudioTrack:\(!sender.isSelected) ");
//mute/unmute the incoming stream audio
self.client?.enableAudioTrack(trackId: self.clientStreamId, enabled: !sender.isSelected);
}
else {
AntMediaClient.printf("It's in publis mode and calling to mute/unmute the local audio to send");
//mute/unmute the microphone for the publisher
let toggleAudio = !sender.isSelected
self.client?.setAudioTrack(enableTrack: toggleAudio);
if (toggleAudio) {
//it means audio is enabled
self.client?.removeAudioLevelExtractor();
}
else {
//it means audio is disabled
self.client?.registerAudioLevelExtractor();
}
}
}
@IBAction func videoTapped(_ video: UIButton!) {
video.isSelected = !video.isSelected
//self.client.toggleVideo()
self.client?.switchCamera()
}
@IBAction func closeTapped(_ sender: UIButton!) {
self.client?.stop()
self.dismiss(animated: true, completion: nil)
}
@IBAction func messageButtonTapped(_ sender: Any) {
//show alert window if data channel is enabled
let active = self.client?.isDataChannelActive()
if (active == true)
{
let alert = UIAlertController(title: "Send Message", message: "Send message with WebRTC Datachannel", preferredStyle: .alert)
alert.addTextField { (textField) in
textField.text = ""
}
alert.addAction(UIAlertAction(title: "Send", style: .default, handler: { [weak alert] (_) in
guard let textValue = alert?.textFields?.first?.text else {
return
}
if let data = textValue.data(using: .utf8) {
/*
Send data through data channel
*/
self.client?.sendData(data: data, binary: false)
/*
You can either use some simple JSON formatting in order to have better
let candidateJson = ["command": "message",
"content" : textValue,
] as [String : Any]
self.client.sendData(data: candidateJson.json.data(using: .utf8) ?? Data.init(capacity: 1), binary: false)
*/
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { [weak alert] (_) in
alert?.dismiss(animated: false, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
else {
AlertHelper.getInstance().show("Warning", message: "Data channel is not active. Please make sure data channel is enabled in both server side and mobile sdk ")
}
}
private func setGesture() {
self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(VideoViewController.toggleContainer))
self.tapGesture.numberOfTapsRequired = 1
self.fullVideoView.addGestureRecognizer(tapGesture)
self.view.addGestureRecognizer(tapGesture)
}
@objc private func toggleContainer() {
UIView.animate(withDuration: 0.3, animations: { () -> Void in
if (self.containerLeftConstraint!.constant <= -45.0) {
self.containerLeftConstraint!.constant = 15.0
self.containerView.alpha = 1.0
} else {
self.containerLeftConstraint!.constant = -45.0
self.containerView.alpha = 0.0
}
self.view.layoutIfNeeded()
})
}
override func viewWillDisappear(_ animated: Bool) {
self.client?.stop()
}
}
extension VideoViewController: AntMediaClientDelegate {
func clientHasError(_ message: String) {
AlertHelper.getInstance().show("Error!", message: message, cancelButtonText: "OK", cancelAction: { [weak self] in
self?.dismiss(animated: true, completion: nil)
})
}
func disconnected(streamId: String) {
print("Disconnected -> \(streamId)")
}
func remoteStreamRemoved(streamId: String) {
print("Remote stream removed -> \(streamId)")
if (self.client?.getCurrentMode() == .join) {
Run.afterDelay(1, block: {
self.fullVideoView.isHidden = true
})
} else {
AlertHelper.getInstance().show("Caution!", message: "Remote stream is no longer available", cancelButtonText: "OK", cancelAction: {
//self.dismiss(animated: true, completion: nil)
//self.fullVideoView.
})
}
}
func localStreamStarted(streamId: String) {
print("Local stream added")
self.fullVideoView.isHidden = false
var localVideoTrack:RTCVideoTrack? = self.client?.getLocalVideoTrack();
print("local video trackId:\(localVideoTrack?.trackId)");
var localAudioTrack:RTCAudioTrack? = self.client?.getLocalAudioTrack();
print("local audio trackId:\(localAudioTrack?.trackId)");
}
func playStarted(streamId: String)
{
print("play started");
}
func playFinished(streamId: String) {
print("play finished")
AlertHelper.getInstance().show("Caution!", message: "Remote stream is no longer available", cancelButtonText: "OK", cancelAction: {
self.dismiss(animated: true, completion: nil)
})
}
func publishStarted(streamId: String)
{
Run.onMainThread
{
Run.afterDelay(3, block: {
Run.onMainThread {
self.pipVideoView.bringSubviewToFront(self.fullVideoView)
}
})
}
if let audioFile = self.audioFileUrl {
sendMp3File(url: audioFile);
}
self.client?.registerStatsListener(for: streamId)
}
func publishFinished(streamId: String) {
}
func audioSessionDidStartPlayOrRecord(streamId: String) {
AntMediaClient.speakerOn()
}
func dataReceivedFromDataChannel(streamId: String, data: Data, binary: Bool) {
AntMediaClient.printf("Data is received from stream: \(streamId) is binary:\(binary) content: " + String(decoding: data, as: UTF8.self))
Run.onMainThread {
self.showToast(controller: self, message: String(decoding: data, as: UTF8.self), seconds: 1.0)
}
}
func streamInformation(streamInfo: [StreamInformation]) {
AntMediaClient.printf("Incoming stream infos")
for result in streamInfo {
AntMediaClient.printf("resolution width:\(result.streamWidth) heigh:\(result.streamHeight) video " + "bitrate:\(result.videoBitrate) audio bitrate:\(result.audioBitrate) codec:\(result.videoCodec)");
}
}
func showToast(controller: UIViewController, message : String, seconds: Double)
{
let alert = UIAlertController(title: "Received Message", message: message, preferredStyle: .alert)
alert.view.backgroundColor = UIColor.black
alert.view.alpha = 0.6
alert.view.layer.cornerRadius = 15
controller.present(alert, animated: true)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + seconds) {
alert.dismiss(animated: true)
}
}
func eventHappened(streamId: String, eventType: String) {
}
func initAntMediaClientForExternalAudio(fileUrl: URL) {
client = AntMediaClient.init();
client?.delegate = self
client?.setDebug(true)
client?.setUseExternalCameraSource(useExternalCameraSource: false)
client?.setWebSocketServerUrl(url: self.clientUrl);
client?.setVideoEnable(enable: false);
client?.setExternalVideoCapture(externalVideoCapture: false);
client?.setExternalAudio(externalAudioEnabled: true)
client?.publish(streamId: self.clientStreamId);
self.audioFileUrl = fileUrl;
}
func sendMp3File(url: URL) {
let asset = AVAsset(url: url)
let track = asset.tracks(withMediaType: .audio).first!
do {
reader = try AVAssetReader(asset: asset)
/*
mSampleRate: 44100.0, mFormatID: 1819304813,
mFormatFlags: 14, mBytesPerPacket: 4, mFramesPerPacket: 1, mBytesPerFrame: 4, mChannelsPerFrame: 2,
mBitsPerChannel: 16, mReserved: 0
*/
readerOutput = AVAssetReaderTrackOutput(track: track, outputSettings: [
AVFormatIDKey: kAudioFormatLinearPCM,
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2,
AVLinearPCMBitDepthKey: 16,
AVLinearPCMIsNonInterleaved: false,
AVLinearPCMIsFloatKey: false,
AVLinearPCMIsBigEndianKey: true
])
if let localReader = readerOutput {
reader?.add(localReader)
reader?.startReading()
if let buffer = localReader.copyNextSampleBuffer() {
sendCMSampleBuffer(sampleBuffer: buffer);
}
}
} catch {
print("Error occurred: \(error)")
}
}
func sendCMSampleBuffer(sampleBuffer:CMSampleBuffer)
{
client?.deliverExternalAudio(sampleBuffer: sampleBuffer)
if let buffer = readerOutput?.copyNextSampleBuffer()
{
DispatchQueue.main.asyncAfter(deadline: .now() + CMTimeGetSeconds(CMSampleBufferGetDuration(sampleBuffer)))
{
self.sendCMSampleBuffer(sampleBuffer: buffer);
}
}
}
}