-
Notifications
You must be signed in to change notification settings - Fork 29
/
PublishStreamManagerTranscodeTest.swift
394 lines (323 loc) · 17.7 KB
/
PublishStreamManagerTranscodeTest.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
//
// PublishStreamManagerTranscodeTest.swift
// R5ProTestbed
//
// Created by Todd Anderson 07/25/2018.
// Copyright © 2015 Infrared5, Inc. All rights reserved.
//
// The accompanying code comprising examples for use solely in conjunction with Red5 Pro (the "Example Code")
// is licensed to you by Infrared5 Inc. in consideration of your agreement to the following
// license terms and conditions. Access, use, modification, or redistribution of the accompanying
// code constitutes your acceptance of the following license terms and conditions.
//
// Permission is hereby granted, free of charge, to you to use the Example Code and associated documentation
// files (collectively, the "Software") without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The Software shall be used solely in conjunction with Red5 Pro. Red5 Pro is licensed under a separate end
// user license agreement (the "EULA"), which must be executed with Infrared5, Inc.
// An example of the EULA can be found on our website at: https://account.red5pro.com/assets/LICENSE.txt.
//
// The above copyright notice and this license shall be included in all copies or portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL INFRARED5, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import UIKit
import R5Streaming
@objc(PublishStreamManagerTranscodeTest)
class PublishStreamManagerTranscodeTest: BaseTest, UITextFieldDelegate, PublishTranscoderFormDelegate {
var provisionList: Array<AnyObject>? = nil
func showInfo(title: String, message: String){
DispatchQueue.main.async(execute: {
// let appDelegate = UIApplication.shared.delegate as! AppDelegate
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { action in
// Trying to redirect user to details form...
// controller?.performSegue(withIdentifier: "showDetail", sender: test)
}))
self.present(alert, animated: true, completion: nil)
})
}
func setupPublisherWithVariant(connection: R5Connection, variant: [String : Any]) {
let fpsVariants = Array<Int32>(Testbed.localParameters!["fps"] as! Array)
self.publishStream = R5Stream(connection: connection)
self.publishStream!.delegate = self
if(Testbed.getParameter(param: "video_on") as! Bool){
let props : [String : Any] = variant["properties"] as! [String : Any]
// Attach the video from camera to stream
let videoDevice = AVCaptureDevice.devices(for: AVMediaType.video).last as? AVCaptureDevice
let camera = R5Camera(device: videoDevice, andBitRate: Int32(props["videoBR"] as! Int) / 1000)
// Not relying on available Frame Rate Ranges, instead use local properties.
// var fpsList = [1:60.0, 2:30.0, 3:15.0]
// let range = videoDevice?.activeFormat.videoSupportedFrameRateRanges;
// if variant["level"] as! Int == 1 {
// fpsList[0] = range?[0].maxFrameRate
// }
// High (0): 60, Medium (1): 30, Low (2): 15
camera?.fps = fpsVariants[(variant["level"] as! Int) - 1]
camera?.width = Int32(props["videoWidth"] as! Int)
camera?.height = Int32(props["videoHeight"] as! Int)
camera?.orientation = 90
self.publishStream!.attachVideo(camera)
}
if(Testbed.getParameter(param: "audio_on") as! Bool){
// Attach the audio from microphone to stream
let audioDevice = AVCaptureDevice.default(for: AVMediaType.audio)
let microphone = R5Microphone(device: audioDevice)
microphone?.bitrate = 32
NSLog("Got device %@", String(describing: audioDevice?.localizedName))
self.publishStream!.attachAudio(microphone)
}
}
func postProvisions(_ url: String, data: [String : Any], resolve: @escaping (_ streams: Array<AnyObject>?, _ error: Error?) -> Void) {
// JSONEncoder().encode(data)
let jsonData = try? JSONSerialization.data(withJSONObject: data)
var req = NSURLRequest( url: NSURL(string: url)! as URL ) as URLRequest
req.httpMethod = "POST"
req.setValue("\(jsonData!.count)", forHTTPHeaderField: "Content-Length")
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
req.httpBody = jsonData
NSURLConnection.sendAsynchronousRequest(
req,
queue: OperationQueue(),
completionHandler:{ (response: URLResponse?, data: Data?, error: Error?) -> Void in
if ((error) != nil) {
resolve(nil, error)
return
}
// Convert our response to a usable NSString
let dataAsString = NSString( data: data!, encoding: String.Encoding.utf8.rawValue)
// The string above is in JSON format, we specifically need the serverAddress value
var json: [String: AnyObject]
do {
json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions()) as! [String: AnyObject]
} catch {
print(error)
return
}
var streams : Array<AnyObject>? = nil
if let errorMessage = json["errorMessage"] as? String {
resolve(nil, AccessError.error(message: errorMessage))
return
}
else if let data = json["data"] as? [String:AnyObject]{
if let metaNode = data["meta"] as? [String:AnyObject] {
streams = metaNode["stream"] as? Array<AnyObject>
}
}
else if let metaRoot = json["meta"] as? [String:AnyObject] {
streams = metaRoot["stream"] as? Array<AnyObject>
}
if (streams != nil) {
resolve(streams, error)
} else {
resolve(nil, AccessError.error(message: "No streams found."))
}
})
}
func respondToProvisions(data: [String : Any], urls: Array<String>) -> (Array<AnyObject>?, Error?) -> Void {
var urls = urls
return {(streams: Array<AnyObject>?, error: Error?) -> Void in
if ((error) != nil) {
if (urls.endIndex == urls.startIndex) {
NSLog("%@", String(error!.localizedDescription))
self.showInfo(title: "Error", message: String(error!.localizedDescription) + "\n\n" + "You may be trying to access over HTTPS which requires a Fully-Qualified Domain Name for host.\n\nYou will need to edit your host and port settings accordingly.")
}
else {
self.postProvisions(urls.popLast()!, data: data, resolve: self.respondToProvisions(data: data, urls: urls))
}
return;
}
self.provisionList = streams
// UI updates must be asynchronous
DispatchQueue.main.async(execute: {
let screenSize = UIScreen.main.bounds.size
var index = CGFloat(1.0)
for stream in streams! {
let s = stream as! [String:AnyObject]
let sendBtn = UIButton(frame: CGRect(x: 20, y: (screenSize.height * 0.75) - (70*index), width: screenSize.width - 40, height: 60))
sendBtn.backgroundColor = UIColor.darkGray
sendBtn.setTitle(s["name"] as! String, for: UIControl.State.normal)
self.view.addSubview(sendBtn)
let tap = UITapGestureRecognizer(target: self, action: #selector(self.selectStream))
sendBtn.addGestureRecognizer(tap)
index = index + 1.0
}
})
}
}
func requestOrigin(_ url: String, resolve: @escaping (_ ip: String?, _ error: Error?) -> Void) {
NSURLConnection.sendAsynchronousRequest(
NSURLRequest( url: NSURL(string: url)! as URL ) as URLRequest,
queue: OperationQueue(),
completionHandler:{ (response: URLResponse?, data: Data?, error: Error?) -> Void in
if ((error) != nil) {
resolve(nil, error)
return
}
// Convert our response to a usable NSString
let dataAsString = NSString( data: data!, encoding: String.Encoding.utf8.rawValue)
// The string above is in JSON format, we specifically need the serverAddress value
var json: [String: AnyObject]
do {
json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions()) as! [String: AnyObject]
} catch {
print(error)
return
}
if let ip = json["serverAddress"] as? String {
NSLog("Retrieved %@ from %@, of which the usable IP is %@", dataAsString!, url, ip);
resolve(ip, error)
}
else if let errorMessage = json["errorMessage"] as? String {
resolve(nil, AccessError.error(message: errorMessage))
}
})
}
func respondToOrigin(urls: Array<String>, streamName: String) -> (String?, Error?) -> Void {
var urls = urls
return {(ip: String?, error: Error?) -> Void in
if ((error) != nil) {
if (urls.endIndex == urls.startIndex) {
NSLog("%@", String(error!.localizedDescription))
self.showInfo(title: "Error", message: String(error!.localizedDescription) + "\n\n" + "You may be trying to access over HTTPS which requires a Fully-Qualified Domain Name for host.\n\nYou will need to edit your host and port settings accordingly.")
}
else {
self.requestOrigin(urls.popLast()!, resolve: self.respondToOrigin(urls: urls, streamName: streamName))
}
return;
}
// Setup a configuration object for our connection
let config = R5Configuration()
config.host = ip
config.port = Int32(Testbed.getParameter(param: "port") as! Int)
config.contextName = Testbed.getParameter(param: "context") as! String
config.`protocol` = 1;
config.buffer_time = Testbed.getParameter(param: "buffer_time") as! Float
config.licenseKey = Testbed.getParameter(param: "license_key") as! String
var selectedVariant : [String: Any]? = nil
for stream in self.provisionList! {
let s = stream as! [String:Any]
if s["name"] as! String == streamName {
selectedVariant = s
break
}
}
// Create a new connection using the configuration above
let connection = R5Connection(config: config)
let type = self.getPublishRecordType ()
// UI updates must be asynchronous
DispatchQueue.main.async(execute: {
for uibutton in self.view.subviews {
if let btn = uibutton as? UIButton {
btn.removeFromSuperview()
}
}
self.setupDefaultR5VideoViewController()
// Create our new stream that will utilize that connection
self.setupPublisherWithVariant(connection: connection!, variant: selectedVariant!)
// show preview and debug info
self.currentView!.attach(self.publishStream!)
self.publishStream!.publish(streamName, type: type)
let label = UILabel(frame: CGRect(x: 0, y: self.view.frame.height-24, width: self.view.frame.width, height: 24))
label.textAlignment = NSTextAlignment.left
label.backgroundColor = UIColor.lightGray
label.text = "Connected to: " + ip!
self.view.addSubview(label)
})
}
}
func sendProvisions (name: String, data: [String : Any]) {
let port = (Testbed.getParameter(param: "server_port") as! String)
let portURI = (port == "80" || port == "443") ? "" : ":" + port
let version = (Testbed.getParameter(param: "sm_version") as! String)
let accessToken = (Testbed.getParameter(param: "sm_access_token") as! String)
let originURI = (Testbed.getParameter(param: "host") as! String) + portURI + "/streammanager/api/" + version +
"/admin/event/meta/" +
(Testbed.getParameter(param: "context") as! String) + "/" +
name + "?accessToken=" + accessToken
let httpString = "http://" + originURI
let httpsString = "https://" + originURI
var urls = [httpString, httpsString]
postProvisions(urls.popLast()!, data: data, resolve: respondToProvisions(data: data, urls: urls))
}
func getOrigin (name: String, topLevelStreamName: String) {
let port = (Testbed.getParameter(param: "server_port") as! String)
let portURI = (port == "80" || port == "443") ? "" : ":" + port
let version = (Testbed.getParameter(param: "sm_version") as! String)
let originURI = (Testbed.getParameter(param: "host") as! String) + portURI + "/streammanager/api/" + version +
"/event/" +
(Testbed.getParameter(param: "context") as! String) + "/" +
name + "?action=broadcast&transcode=true"
let httpString = "http://" + originURI
let httpsString = "https://" + originURI
var urls = [httpString, httpsString]
requestOrigin(urls.popLast()!, resolve: respondToOrigin(urls: urls, streamName: topLevelStreamName))
}
@objc func selectStream(sender: UITapGestureRecognizer) {
let streamName = (Testbed.getParameter(param: "stream1") as? String)
let button = sender.view as? UIButton
let name = button?.title(for: UIControl.State.normal) ?? streamName! + "_1"
self.getOrigin(name: streamName!, topLevelStreamName: name)
for uibutton in self.view.subviews {
if let btn = uibutton as? UIButton {
btn.isEnabled = false
}
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
AVAudioSession.sharedInstance().requestRecordPermission { (gotPerm: Bool) -> Void in };
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = (storyboard.instantiateViewController(withIdentifier: "PublishTranscoderForm") as? PublishTranscoderForm)!
vc.delegate = self
vc.view.backgroundColor = UIColor.white
self.addChild(vc)
self.view.addSubview(vc.view)
vc.view.frame = CGRect(x:0, y:0, width:self.view.frame.width, height: self.view.frame.height)
vc.view.layoutSubviews()
}
func onProvisionSubmit (_ controller: PublishTranscoderForm) {
controller.view.removeFromSuperview()
controller.removeFromParent()
let high = controller.getHighFormValues()
let medium = controller.getMediumFormValues()
let low = controller.getLowFormValues()
let name = (Testbed.getParameter(param: "stream1") as! String)
var level = 3
let provisions = [low, medium, high].map {
(values: (Int, Int, Int)) -> [String:Any] in
let variant: [String : Any] = [
"name": name + "_" + String(level),
"level": level,
"properties": [
"videoBR": values.0,
"videoWidth": values.1,
"videoHeight": values.2
]
]
level = level - 1
return variant
}
let transcoderPOST : [String : Any ] = [
"meta": [
"authentication": [
"username": "",
"password": ""
],
"stream": provisions,
"georules": [
"regions": ["US", "UK"],
"restricted": false
],
"qos": 3
]
]
self.sendProvisions(name: name, data: transcoderPOST)
}
}