-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOpenTokPlugin.m
345 lines (273 loc) · 13.2 KB
/
OpenTokPlugin.m
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
//
// OpentokPlugin.m
// opentokTest
//
// Created by Song Zheng on 4/19/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "OpentokPlugin.h"
@implementation OpenTokPlugin{
OTSession* _session;
OTPublisher* _publisher;
OTSubscriber* _subscriber;
NSMutableDictionary *streamDictionary;
}
@synthesize callbackID;
@synthesize streamDisconnectedId;
@synthesize streamCreatedId;
@synthesize sessionDisconnectedId;
@synthesize exceptionId;
/*** TB Methods
****/
// Called by TB.addEventListener('exception', fun...)
-(void)exceptionHandler:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSLog(@"iOS Added exception Event Handler");
self.exceptionId = [arguments pop];
}
// Called by TB.initsession()
-(void)initSession:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSLog(@"iOS Initializing Session");
// Get Parameters
NSString* callback = [arguments pop];
NSString* sessionId = [arguments objectAtIndex:0];
NSString* production = [arguments objectAtIndex:1];
// Create Session
if ([production isEqualToString:@"true"]) {
_session = [[OTSession alloc] initWithSessionId:sessionId delegate:self environment:OTSessionEnvironmentProduction];
}else {
_session = [[OTSession alloc] initWithSessionId:sessionId delegate:self];
}
// Initialize Dictionary, contains DOM info for every stream
streamDictionary = [[NSMutableDictionary alloc] init];
// Return Result
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript: [pluginResult toSuccessCallbackString:callback]];
}
// Called by TB.initPublisher()
- (void)initPublisher:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options{
NSLog(@"iOS initPublisher");
BOOL bpubAudio = YES;
BOOL bpubVideo = YES;
// Get Parameters
NSString* callback = [arguments pop];
int top = [[arguments objectAtIndex:0] intValue];
int left = [[arguments objectAtIndex:1] intValue];
int width = [[arguments objectAtIndex:2] intValue];
int height = [[arguments objectAtIndex:3] intValue];
NSString* name = [arguments objectAtIndex:4];
if ([name isEqualToString:@"TBNameHolder"]) {
name = [[UIDevice currentDevice] name];
}
NSString* publishAudio = [arguments objectAtIndex:5];
if ([publishAudio isEqualToString:@"false"]) {
bpubAudio = NO;
}
NSString* publishVideo = [arguments objectAtIndex:6];
if ([publishVideo isEqualToString:@"false"]) {
bpubVideo = NO;
}
// Publish and set View
_publisher = [[OTPublisher alloc] initWithDelegate:self name:name];
[_publisher setPublishAudio:bpubAudio];
[_publisher setPublishVideo:bpubVideo];
[self.webView.superview addSubview:_publisher.view];
[_publisher.view setFrame:CGRectMake(left, top, width, height)];
// Return to Javascript
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript: [pluginResult toSuccessCallbackString:callback]];
}
// Called by addEventListener() for streamCreated
- (void)streamCreatedHandler:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSLog(@"iOS Adding Stream Created Event Listener");
self.streamCreatedId = [arguments pop];
}
/*** Session Methods
****/
// Called by session.connect(key, token)
- (void)connect:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options{
NSLog(@"iOS Connecting to Session");
// Get Parameters
self.callbackID = [arguments pop];
NSString* tbKey = [arguments objectAtIndex:0];
NSString* tbToken = [arguments objectAtIndex:1];
[_session connectWithApiKey:tbKey token:tbToken];
}
// Called by session.disconnect()
- (void)disconnect:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
[_session disconnect];
}
// Called by session.publish(top, left)
- (void)publish:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSLog(@"iOS session Publish with Publisher");
self.callbackID = [arguments pop];
[_session publish:_publisher];
// Return to Javascript
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
}
// Called by session.unpublish(...)
- (void)unpublish:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSLog(@"iOS Unpublishing publisher");
[_session unpublish:_publisher];
}
// Called by session.subscribe(streamId, top, left)
- (void)subscribe:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSLog(@"iOS subscribing to stream");
// Get Parameters
self.callbackID = [arguments pop];
NSString* sid = [arguments objectAtIndex:0];
int top = [[arguments objectAtIndex:1] intValue];
int left = [[arguments objectAtIndex:2] intValue];
int width = [[arguments objectAtIndex:3] intValue];
int height = [[arguments objectAtIndex:4] intValue];
OTSubscriber* sub = [streamDictionary objectForKey:sid];
[sub.view setFrame:CGRectMake(left, top, width, height)];
[self.webView.superview addSubview:sub.view];
// Return to JS event handler
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
}
// Called by session.subscribe(streamId, top, left)
- (void)unsubscribe:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSLog(@"iOS unSubscribing to stream");
}
/*** Subscriber Methods
****/
- (void)subscriberDidConnectToStream:(OTSubscriber*)sub{
NSLog(@"iOS Connected To Stream");
}
- (void)subscriber:(OTSubscriber*)subscrib didFailWithError:(NSError*)error{
CDVPluginResult* callbackResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: subscrib.stream.connection.connectionId];
[callbackResult setKeepCallbackAsBool:YES];
// [self writeJavascript: [callbackResult toSuccessCallbackString:self.streamDisconnectedId]];
}
// OTSession Connection Delegates
- (void)sessionDidConnect:(OTSession*)session{
NSLog(@"iOS Connected to Session");
NSMutableDictionary* sessionDict = [[NSMutableDictionary alloc] init];
// SessionConnectionStatus
NSString* connectionStatus = @"";
if (session.sessionConnectionStatus==OTSessionConnectionStatusConnected) {
connectionStatus = @"OTSessionConnectionStatusConnected";
}else if (session.sessionConnectionStatus==OTSessionConnectionStatusConnecting) {
connectionStatus = @"OTSessionConnectionStatusConnecting";
}else if (session.sessionConnectionStatus==OTSessionConnectionStatusDisconnected) {
connectionStatus = @"OTSessionConnectionStatusDisconnected";
}else{
connectionStatus = @"OTSessionConnectionStatusFailed";
}
[sessionDict setObject:connectionStatus forKey:@"sessionConnectionStatus"];
// SessionId
[sessionDict setObject:session.sessionId forKey:@"sessionId"];
// Session ConnectionCount
NSString* strInt = [NSString stringWithFormat:@"%d", session.connectionCount];
[sessionDict setObject:strInt forKey:@"connectionCount"];
// SessionStreams
NSMutableArray* streamsArray = [[NSMutableArray alloc] init];
for(id key in session.streams){
[streamsArray addObject: [session.streams objectForKey:key]];
}
[sessionDict setObject:streamsArray forKey:@"streams"];
// After session is successfully connected, the connection property is available
NSMutableDictionary* connection = [[NSMutableDictionary alloc] init];
[connection setObject:session.connection.connectionId forKey:@"connectionId"];
NSString* strDate= [NSString stringWithFormat:@"%.0f", [session.connection.creationTime timeIntervalSince1970]];
[connection setObject:strDate forKey:@"creationTime"];
[sessionDict setObject:connection forKey:@"connection"];
// Session Environment
if (session.environment==OTSessionEnvironmentProduction) {
[sessionDict setObject:@"production" forKey:@"environment"];
}else {
[sessionDict setObject:@"staging" forKey:@"environment"];
}
// After session dictionary is constructed, return the result!
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:sessionDict];
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
}
- (void)session:(OTSession*)mySession didReceiveStream:(OTStream*)stream{
NSLog(@"iOS Received Stream");
OTSubscriber* subscriber = [[OTSubscriber alloc] initWithStream:stream delegate:self];
[streamDictionary setObject:subscriber forKey:stream.streamId];
// Set up result, trigger JS event handler
NSString* result = [[NSString alloc] initWithFormat:@"%@ %@", stream.connection.connectionId, stream.streamId];
CDVPluginResult* callbackResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: result];
[callbackResult setKeepCallbackAsBool:YES];
[self writeJavascript: [callbackResult toSuccessCallbackString:self.streamCreatedId]];
}
- (void)session:(OTSession*)session didFailWithError:(NSError*)error {
NSLog(@"iOS Session didFailWithError");
NSMutableDictionary* err = [[NSMutableDictionary alloc] init];
[err setObject:error.localizedDescription forKey:@"message"];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: err];
[pluginResult setKeepCallbackAsBool:YES];
[self writeJavascript: [pluginResult toSuccessCallbackString:self.exceptionId]];
}
- (void)sessionDidDisconnect:(OTSession*)session{
NSString* alertMessage = [NSString stringWithFormat:@"Session disconnected: (%@)", session.sessionId];
NSLog(@"sessionDidDisconnect (%@)", alertMessage);
// Setting up event object
NSMutableDictionary* event = [[NSMutableDictionary alloc] init];
[event setObject:@"networkDisconnected" forKey:@"reason"];
[event setObject:@"sessionDisconnected" forKey:@"type"];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:event];
[pluginResult setKeepCallbackAsBool:YES];
[self writeJavascript: [pluginResult toSuccessCallbackString:self.sessionDisconnectedId]];
}
- (void)session:(OTSession*)session didDropStream:(OTStream*)stream{
NSLog(@"iOS Drop Stream");
CDVPluginResult* callbackResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: stream.streamId];
[callbackResult setKeepCallbackAsBool:YES];
[self writeJavascript: [callbackResult toSuccessCallbackString:self.streamDisconnectedId]];
}
// Called by addEventListener() for session/stream Disconnected
-(void)streamDisconnectedHandler:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
self.streamDisconnectedId = [arguments pop];
}
-(void)sessionDisconnectedHandler:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
self.sessionDisconnectedId = [arguments pop];
}
// Helper function to update Views
- (void)updateView:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSString* callback = [arguments pop];
NSString* sid = [arguments objectAtIndex:0];
int top = [[arguments objectAtIndex:1] intValue];
int left = [[arguments objectAtIndex:2] intValue];
int width = [[arguments objectAtIndex:3] intValue];
int height = [[arguments objectAtIndex:4] intValue];
if ([sid isEqualToString:@"TBPublisher"]) {
NSLog(@"The Width is: %d", width);
_publisher.view.frame = CGRectMake(left, top, width, height);
}
OTSubscriber* streamInfo = [streamDictionary objectForKey:sid];
if (streamInfo) {
// Reposition the video feeds!
// streamInfo.subscriber.view.frame = CGRectMake(left, top, width, height);
streamInfo.view.frame = CGRectMake(left, top, width, height);
}
CDVPluginResult* callbackResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[callbackResult setKeepCallbackAsBool:YES];
[self writeJavascript: [callbackResult toSuccessCallbackString:callback]];
}
/***** Errors
****/
- (void)publisher:(OTPublisher*)publisher didFailWithError:(NSError*) error {
NSLog(@"iOS Publisher didFailWithError");
NSMutableDictionary* err = [[NSMutableDictionary alloc] init];
[err setObject:error.localizedDescription forKey:@"message"];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: err];
[pluginResult setKeepCallbackAsBool:YES];
[self writeJavascript: [pluginResult toSuccessCallbackString:self.exceptionId]];
}
/**** End of Errors
****/
/***** Notes
NSString *stringObtainedFromJavascript = [arguments objectAtIndex:0];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: stringObtainedFromJavascript];
if(YES){
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
}else{
//Call the Failure Javascript function
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]];
}
******/
@end