-
Notifications
You must be signed in to change notification settings - Fork 29
/
SharedObjectTest.swift
340 lines (263 loc) · 14.1 KB
/
SharedObjectTest.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
//
// SharedObjectTest.swift
// R5ProTestbed
//
// Created by David Heimann on 1/26/17.
// 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(SharedObjectTest)
class SharedObjectTest: BaseTest, UITextViewDelegate {
var sObject : R5SharedObject? = nil
var chatView: UITextView? = nil
var chatInput: UITextView? = nil
var sendBtn: UIButton? = nil
var messageBuffer: NSMutableArray = []
var timer: Timer? = nil
var thisUser: String = "subscriber"
var SOConnected : Bool = false
func getTextColorGestureRecognizer () -> UIGestureRecognizer {
return UITapGestureRecognizer(target: self, action: #selector(setTextColor))
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
AVAudioSession.sharedInstance().requestRecordPermission { (gotPerm: Bool) -> Void in
};
setupDefaultR5VideoViewController()
let tapOut = UITapGestureRecognizer(target: self, action: #selector(textViewDidEndEditing))
view.addGestureRecognizer(tapOut)
let screenSize = UIScreen.main.bounds.size
//make textviews
chatView = UITextView(frame: CGRect(x: 0, y: screenSize.height * 0.5, width: screenSize.width * 0.6, height: (screenSize.height * 0.5) - 60))
chatView?.backgroundColor = UIColor.white.withAlphaComponent(0.5)
chatView?.isEditable = false
view.addSubview(chatView!)
addMessage(message: "Waiting for Stream Connection")
let redButton = UIButton(frame: CGRect(x: 2, y: screenSize.height - 58, width: 32, height: 32))
redButton.backgroundColor = UIColor.red;
redButton.setTitleColor(UIColor.red, for: UIControl.State.normal);
redButton.setTitle("#ff0000", for: UIControl.State.normal)
view.addSubview(redButton)
redButton.addGestureRecognizer(getTextColorGestureRecognizer())
let greenButton = UIButton(frame: CGRect(x: 36, y: screenSize.height - 58, width: 32, height: 32))
greenButton.backgroundColor = UIColor.green;
greenButton.setTitleColor(UIColor.green, for: UIControl.State.normal);
greenButton.setTitle("#00ff00", for: UIControl.State.normal)
view.addSubview(greenButton)
greenButton.addGestureRecognizer(getTextColorGestureRecognizer())
let blueButton = UIButton(frame: CGRect(x: 70, y: screenSize.height - 58, width: 32, height: 32))
blueButton.backgroundColor = UIColor.blue;
blueButton.setTitleColor(UIColor.blue, for: UIControl.State.normal);
blueButton.setTitle("#0000ff", for: UIControl.State.normal)
view.addSubview(blueButton)
blueButton.addGestureRecognizer(getTextColorGestureRecognizer())
let blackButton = UIButton(frame: CGRect(x: 104, y: screenSize.height - 58, width: 32, height: 32))
blackButton.backgroundColor = UIColor.black;
blackButton.setTitleColor(UIColor.black, for: UIControl.State.normal);
blackButton.setTitle("#000000", for: UIControl.State.normal)
view.addSubview(blackButton)
blackButton.addGestureRecognizer(getTextColorGestureRecognizer())
chatInput = UITextView(frame: CGRect(x: 0, y: screenSize.height - 24, width: (screenSize.width * 0.6) - 50, height: 24) )
chatInput?.backgroundColor = UIColor.lightGray
chatInput?.isEditable = true
chatInput?.delegate = self
view.addSubview(chatInput!)
sendBtn = UIButton(frame: CGRect(x: (screenSize.width * 0.6) - 50, y: screenSize.height - 24, width: 50, height: 24))
sendBtn?.backgroundColor = UIColor.darkGray
sendBtn?.setTitle("Send", for: UIControl.State.normal)
view.addSubview(sendBtn!)
let tap = UITapGestureRecognizer(target: self, action: #selector(sendMessage))
sendBtn?.addGestureRecognizer(tap)
callForStreamList();
}
@objc func callForStreamList(){
let domain = Testbed.getParameter(param: "host") as! String
let app = Testbed.getParameter(param: "context") as! String
let url = "http://" + domain + ":5080/" + app + "/streams.jsp"
let request = URLRequest.init(url: URL.init(string: url)!)
NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.init(), completionHandler: { (response: URLResponse?, data: Data?, error: Error?) -> Void in
self.streamListReturn(response: response, data: data, error: error)
})
}
func delayCallForList() {
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(callForStreamList), userInfo: nil, repeats: false)
}
//add to destroy - close the web call and timer if it's running
func streamListReturn( response: URLResponse?, data: Data?, error: Error? ){
addMessage(message: "Retrieved List")
// Set up the configuration
let config = getConfig()
// Set up the connection and stream
let connection = R5Connection(config: config)
//parse the response
if (error != nil) {
NSLog("Error, %@", error!.localizedDescription);
delayCallForList();
} else {
do{
let list = try JSONSerialization.jsonObject(with: data!) as! Array<Dictionary<String, String>>;
var shouldPublish: Bool = true;
for dict:Dictionary<String, String> in list {
if(dict["name"] == (Testbed.getParameter(param: "stream1") as! String)){
shouldPublish = false;
break;
}
}
DispatchQueue.main.async {
if(shouldPublish){
self.setupPublisher(connection: connection!)
self.currentView!.attach(self.publishStream!)
self.publishStream!.publish(Testbed.getParameter(param: "stream1") as! String, type: R5RecordTypeLive)
self.addMessage(message: "Begining Publish")
}
else{
self.subscribeStream = R5Stream(connection: connection)
self.subscribeStream!.delegate = self
self.subscribeStream?.client = self;
self.currentView?.attach(self.subscribeStream)
self.subscribeStream!.play(Testbed.getParameter(param: "stream1") as! String)
self.addMessage(message: "Begin Subscribe")
}
}
}catch let error as NSError {
print(error)
delayCallForList();
}
}
}
override func onR5StreamStatus( _ stream: R5Stream!, withStatus statusCode: Int32, withMessage msg: String!) {
super.onR5StreamStatus(stream, withStatus: statusCode, withMessage: msg)
if(Int(statusCode) == Int(r5_status_start_streaming.rawValue)){
self.timer = Timer.scheduledTimer(timeInterval: 0.25, target: self, selector: #selector(SOConnect), userInfo: nil, repeats: false)
}
else if(Int(statusCode) == Int(r5_status_disconnected.rawValue)
|| Int(statusCode) == Int(r5_status_connection_close.rawValue)){
SOConnected = false
}
}
@objc func SOConnect(){
let stream = (publishStream != nil) ? publishStream : subscribeStream;
NSLog("%@", "Sending shared object connection request")
sObject = R5SharedObject(name:"sharedChatTest", connection: stream?.connection);
sObject?.client = self;
}
func randomString(length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).map{ _ in letters.randomElement()! })
}
//callback for remote object connection - remote object now available
@objc func onSharedObjectConnect( objectValue: NSDictionary){
SOConnected = true;
let data : NSMutableDictionary = (sObject?.data)!
if let c = data["color"] {
setChatViewToHex(hexString: c as! String)
}
}
@objc func sendMessage(){
textViewDidEndEditing(chatInput!)
if( (chatInput?.text.isEmpty)! ){
return
}
let messageOut : [AnyHashable:Any] = [ "user":thisUser, "message":(chatInput?.text)! ]
//Calls for the relevant method with the sent parameters on all clients listening to the shared object
//Note - This includes the client that sends the call
sObject?.send("messageTransmit", withParams: messageOut)
chatInput?.text = ""
}
@objc func setChatViewToHex (hexString: String) {
let hex = hexString.replacingOccurrences(of: "#", with: "")
var rgb: UInt32 = 0
guard Scanner(string: hex).scanHexInt32(&rgb) else { return }
let r = CGFloat((rgb & 0xFF0000) >> 16) / 255.0
let g = CGFloat((rgb & 0x00FF00) >> 8) / 255.0
let b = CGFloat(rgb & 0x0000FF) / 255.0
let color = UIColor(red:r, green:g, blue:b, alpha: CGFloat(1.0))
chatView?.textColor = color;
}
@objc func setTextColor (recognizer: UITapGestureRecognizer) {
let button : UIButton = recognizer.view as! UIButton
let hex = button.title(for: UIControl.State.normal)
sObject?.setProperty("color", withValue: hex as NSObject?)
setChatViewToHex(hexString: hex!)
// let messageOut : [AnyHashable:Any] = [ "user":thisUser, "message":"Color changed to: " + hex! ]
// sObject?.send("messageTransmit", withParams: messageOut)
}
//Called whenever a property of the shared object is changed
@objc func onUpdateProperty( propertyInfo: [AnyHashable: Any] ) {
// propertyInfo.keys[0] can be used to find which property has updated.
if let c = propertyInfo["color"] {
setChatViewToHex(hexString: c as! String)
}
}
@objc func messageTransmit( messageIn: [AnyHashable: Any] ){
let user: String = messageIn["user"] as! String
let message : String = messageIn["message"] as! String
let display: String = "User \"" + user + "\": " + message
addMessage(message: display)
}
override func closeTest() {
if( self.timer != nil ){
self.timer?.invalidate();
}
if(SOConnected){
sObject?.close()
}
super.closeTest()
}
func addMessage( message: String, update: Bool = true ){
DispatchQueue.main.async {
let dateFormat = DateFormatter()
dateFormat.dateFormat = "hh:mm:ss.SSS"
let dateString = dateFormat.string(from: NSDate() as Date)
self.messageBuffer.add(dateString + " " + message)
if( update ){
self.chatUpdate();
}
}
}
func chatUpdate() {
while messageBuffer.count > 20 {
messageBuffer.removeObject(at: 0)
}
var textOut: String = ""
for line in messageBuffer {
textOut += (line as! String) + "\n"
}
chatView?.text = textOut
}
func textViewDidBeginEditing(_ textView: UITextView) {
let screenSize = UIScreen.main.bounds.size
chatInput?.frame = CGRect(x:0, y:screenSize.height * 0.5, width: (screenSize.width * 0.6) - 50, height: 24)
sendBtn?.frame = CGRect(x: (screenSize.width * 0.6) - 50, y: screenSize.height * 0.5, width: 50, height: 24)
}
@objc func textViewDidEndEditing(_ textView: UITextView) {
view.endEditing(true)
let screenSize = UIScreen.main.bounds.size
chatInput?.frame = CGRect(x:0, y: screenSize.height - 24, width: (screenSize.width * 0.6) - 50, height: 24)
sendBtn?.frame = CGRect(x: (screenSize.width * 0.6) - 50, y: screenSize.height - 24, width: 50, height: 24)
chatInput?.resignFirstResponder()
}
}