forked from stringeecom/stringee-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNStringeeRoom.m
More file actions
executable file
·510 lines (405 loc) · 16.5 KB
/
RNStringeeRoom.m
File metadata and controls
executable file
·510 lines (405 loc) · 16.5 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#import "RNStringeeRoom.h"
#import "RNStringeeInstanceManager.h"
#import <React/RCTLog.h>
static NSString *didRoomConnect = @"didRoomConnect";
static NSString *didRoomDisConnect = @"didRoomDisConnect";
static NSString *didRoomError = @"didRoomError";
static NSString *didStreamAdd = @"didStreamAdd";
static NSString *didStreamRemove = @"didStreamRemove";
@implementation RNStringeeRoom {
NSMutableArray<NSString *> *jsEvents;
StringeeRoomStream *localStream;
NSMutableDictionary *remoteStreams;
RCTResponseSenderBlock publishCallback;
RCTResponseSenderBlock unPublishCallback;
RCTResponseSenderBlock subscribeCallback;
RCTResponseSenderBlock unSubscribeCallback;
BOOL hasChangedSpeakerPhone;
BOOL isSpeaker;
}
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();
- (instancetype)init {
self = [super init];
if (self) {
[RNStringeeInstanceManager instance].rnRoom = self;
jsEvents = [[NSMutableArray alloc] init];
_rooms = [[NSMutableDictionary alloc] init];
remoteStreams = [[NSMutableDictionary alloc] init];
isSpeaker = YES;
}
return self;
}
- (NSArray<NSString *> *)supportedEvents {
return @[didRoomConnect,
didRoomDisConnect,
didRoomError,
didStreamAdd,
didStreamRemove
];
}
+ (BOOL)requiresMainQueueSetup {
return YES;
}
RCT_EXPORT_METHOD(setNativeEvent:(NSString *)event) {
[jsEvents addObject:event];
}
RCT_EXPORT_METHOD(removeNativeEvent:(NSString *)event) {
int index = -1;
index = (int)[jsEvents indexOfObject:event];
if (index >= 0) {
[jsEvents removeObjectAtIndex:index];
}
}
RCT_EXPORT_METHOD(makeRoom:(RCTResponseSenderBlock)callback) {
StringeeRoom *room = [[StringeeRoom alloc] initWithStringeeClient:[RNStringeeInstanceManager instance].rnClient.client];
room.delegate = self;
RNStringeeRoom *weakself = self;
[room makeRoomWithCompletionHandler:^(BOOL status, int code, NSString *message) {
RNStringeeRoom *strongself = weakself;
id roomId = (room.roomId != 0) ? [NSNumber numberWithLongLong:room.roomId] : [NSNull null];
if (status) {
[strongself.rooms setObject:room forKey:[self keyForLongValue:room.roomId]];
}
int returnCode;
if (code == 2) {
// not connected
returnCode = -1;
} else if (code == 3) {
// generic
returnCode = -2;
} else {
returnCode = code;
}
callback(@[@(status), @(returnCode), message, roomId]);
}];
}
RCT_EXPORT_METHOD(joinRoom:(nonnull NSNumber *)roomId callback:(RCTResponseSenderBlock)callback) {
if ([roomId longLongValue] == 0) {
callback(@[@(NO), @(-3), @"RoomId is invalid."]);
return;
}
StringeeRoom *room = [[StringeeRoom alloc] initWithStringeeClient:[RNStringeeInstanceManager instance].rnClient.client];
room.delegate = self;
RNStringeeRoom *weakself = self;
long long lRoomId = [roomId longLongValue];
[room joinRoomWithRoomId:lRoomId completionHandler:^(BOOL status, int code, NSString *message) {
RNStringeeRoom *strongself = weakself;
if (status) {
[strongself.rooms setObject:room forKey:[self keyForLongValue:room.roomId]];
}
int returnCode;
if (code == 2) {
// not connected
returnCode = -1;
} else if (code == 3) {
// generic
returnCode = -2;
} else {
returnCode = code;
}
callback(@[@(status), @(returnCode), message]);
}];
}
RCT_EXPORT_METHOD(publishLocalStream:(nonnull NSNumber *)roomId config:(NSString *)config callback:(RCTResponseSenderBlock)callback) {
if ([roomId longLongValue] == 0) {
callback(@[@(NO), @(-1), @"RoomId is invalid."]);
return;
}
StringeeRoom *targetRoom = [_rooms objectForKey:[self keyForLongValue:[roomId longLongValue]]];
// Kiem tra current room
if (!targetRoom) {
callback(@[@(NO), @(-2), @"Room is not found."]);
return;
}
// Check config format
NSError *jsonError;
NSData *objectData = [config dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
NSString *videoResolution;
if (jsonError) {
RCTLogInfo(@"The config format is invalid. Using default config");
} else {
videoResolution = data[@"videoResolution"];
}
// Kkoi tao local stream neu chua co
if (!localStream) {
StringeeRoomStreamConfig *config = [[StringeeRoomStreamConfig alloc] init];
if ([videoResolution isEqualToString:@"HD"]) {
config.streamVideoResolution = VideoResolution_HD;
}
localStream = [[StringeeRoomStream alloc] initLocalStreamWithConfig:config];
}
publishCallback = [callback copy];
[targetRoom publish:localStream];
}
RCT_EXPORT_METHOD(unPublishLocalStream:(nonnull NSNumber *)roomId streamId:(NSString *)streamId callback:(RCTResponseSenderBlock)callback) {
if ([roomId longLongValue] == 0) {
callback(@[@(NO), @(-1), @"RoomId is invalid."]);
return;
}
StringeeRoom *targetRoom = [_rooms objectForKey:[self keyForLongValue:[roomId longLongValue]]];
// Kiem tra current room
if (!targetRoom) {
callback(@[@(NO), @(-2), @"Room is not found."]);
return;
}
if (!streamId.length) {
callback(@[@(NO), @(-3), @"StreamId is invalid."]);
return;
}
if (!localStream) {
callback(@[@(YES), @(-4), @"Stream is not found."]);
}
unPublishCallback = [callback copy];
[targetRoom unPublish:localStream];
}
RCT_EXPORT_METHOD(unSubscribe:(nonnull NSNumber *)roomId streamId:(NSString *)streamId callback:(RCTResponseSenderBlock)callback) {
if ([roomId longLongValue] == 0) {
callback(@[@(NO), @(-1), @"RoomId is invalid."]);
return;
}
StringeeRoom *targetRoom = [_rooms objectForKey:[self keyForLongValue:[roomId longLongValue]]];
// Kiem tra current room
if (!targetRoom) {
callback(@[@(NO), @(-2), @"Room is not found."]);
return;
}
if (!streamId.length) {
callback(@[@(NO), @(-3), @"StreamId is invalid."]);
return;
}
StringeeRoomStream *targetStream = [remoteStreams objectForKey:streamId];
if (!targetStream) {
callback(@[@(NO), @(-4), @"Stream is not found."]);
return;
}
unSubscribeCallback = [callback copy];
[targetRoom unSubscribe:targetStream];
}
RCT_EXPORT_METHOD(subscribe:(nonnull NSNumber *)roomId streamId:(NSString *)streamId callback:(RCTResponseSenderBlock)callback) {
if ([roomId longLongValue] == 0) {
callback(@[@(NO), @(-1), @"RoomId is invalid."]);
return;
}
StringeeRoom *targetRoom = [_rooms objectForKey:[self keyForLongValue:[roomId longLongValue]]];
// Kiem tra current room
if (!targetRoom) {
callback(@[@(NO), @(-2), @"Room is not found."]);
return;
}
if (!streamId.length) {
callback(@[@(NO), @(-3), @"StreamId is invalid."]);
return;
}
StringeeRoomStream *targetStream = [remoteStreams objectForKey:streamId];
if (!targetStream) {
callback(@[@(NO), @(-4), @"Stream is not found."]);
return;
}
subscribeCallback = [callback copy];
[targetRoom subscribe:targetStream];
}
RCT_EXPORT_METHOD(destroy:(nonnull NSNumber *)roomId callback:(RCTResponseSenderBlock)callback) {
if ([roomId longLongValue] == 0) {
callback(@[@(NO), @(-2), @"RoomId is invalid."]);
return;
}
StringeeRoom *targetRoom = [_rooms objectForKey:[self keyForLongValue:[roomId longLongValue]]];
// Kiem tra current room
if (!targetRoom) {
callback(@[@(NO), @(-3), @"Room is not found."]);
return;
}
// Remove render view
if (localStream.localVideoView.superview) {
[localStream.localVideoView removeFromSuperview];
}
// Terminate room and release objects
[targetRoom destroy];
[_rooms removeObjectForKey:roomId];
targetRoom = nil;
localStream = nil;
[remoteStreams removeAllObjects];
hasChangedSpeakerPhone = NO;
isSpeaker = YES;
callback(@[@(YES), @(0), @"Success"]);
}
RCT_EXPORT_METHOD(switchCamera) {
[localStream switchCamera];
}
RCT_EXPORT_METHOD(mute:(BOOL)mute) {
[localStream mute:mute];
}
RCT_EXPORT_METHOD(turnOnCamera:(BOOL)isOn callback:(RCTResponseSenderBlock)callback) {
if (!localStream) {
callback(@[@(NO), @(-1), @"Local stream is not found."]);
}
[localStream turnOnCamera:isOn];
callback(@[@(YES), @(0), @"Success."]);
}
RCT_EXPORT_METHOD(getStats:(nonnull NSNumber *)roomId streamId:(NSString *)streamId useVideoTrack:(BOOL)useVideoTrack callback:(RCTResponseSenderBlock)callback) {
if ([roomId longLongValue] == 0) {
callback(@[@(NO), @(-1), @"RoomId is invalid."]);
return;
}
StringeeRoom *targetRoom = [_rooms objectForKey:[self keyForLongValue:[roomId longLongValue]]];
// Kiem tra current room
if (!targetRoom) {
callback(@[@(NO), @(-2), @"Room is not found."]);
return;
}
if (!streamId.length) {
callback(@[@(NO), @(-3), @"StreamId is invalid."]);
return;
}
StringeeRoomStream *targetStream;
if ([streamId isEqualToString:localStream.streamId]) {
targetStream = localStream;
} else {
targetStream = [remoteStreams objectForKey:streamId];
}
if (targetStream) {
[targetRoom statsReportForStream:targetStream useVideoTrack:NO withCompletionHandler:^(NSDictionary<NSString *,NSString *> *stats) {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:stats
options:NSJSONWritingPrettyPrinted
error:nil];
NSString *jsonString = [[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@" " withString:@""];
callback(@[@(YES), @(0), @"Success", jsonString]);
}];
} else {
callback(@[@(NO), @(-4), @"Stream is not found."]);
}
}
RCT_EXPORT_METHOD(setSpeakerphoneOn:(BOOL)isOn) {
isSpeaker = isOn;
[[StringeeAudioManager instance] setLoudspeaker:isOn];
}
// TODO: - Private functions
- (void)addRenderToView:(UIView *)view streamId:(NSString *)streamId isLocal:(BOOL)isLocal {
RCTLogInfo(@"addRenderToView");
if (isLocal) {
if (localStream) {
localStream.localVideoView.frame = CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height);
[view addSubview:localStream.localVideoView];
}
} else {
RCTLogInfo(@"addRenderToView - remote");
StringeeRoomStream *remoteStream = [remoteStreams objectForKey:streamId];
if (remoteStream) {
RCTLogInfo(@"addRenderToView - remote - sucess");
[remoteStream.remoteVideoView setFrame:CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height)];
remoteStream.remoteVideoView.delegate = view;
[view addSubview:remoteStream.remoteVideoView];
}
}
}
- (NSString *)keyForLongValue:(long long)value {
return [NSString stringWithFormat:@"%lld", value];
}
// TODO: - Stringee Room Delegate
- (void)didRoomConnect:(StringeeRoom *)stringeeRoom streams:(NSArray<StringeeRoomStream *> *)streams {
RCTLogInfo(@"Đã kết nối tới room");
if ([jsEvents containsObject:didRoomConnect]) {
NSMutableArray *streamInfos = [[NSMutableArray alloc] init];
for (StringeeRoomStream *stream in streams) {
[remoteStreams setObject:stream forKey:stream.streamId];
NSDictionary *streamInfo = @{@"userId" : stream.userId, @"streamId" : stream.streamId};
[streamInfos addObject:streamInfo];
}
[self sendEventWithName:didRoomConnect body:@{ @"roomId" : @(stringeeRoom.roomId), @"streams" : streamInfos }];
}
}
- (void)didRoomError:(StringeeRoom *)stringeeRoom code:(int)code message:(NSString *)message {
RCTLogInfo(@"Kết nối tới room lỗi");
// [self clearAndEnd];
if ([jsEvents containsObject:didRoomError]) {
[self sendEventWithName:didRoomError body:@{ @"roomId" : @(stringeeRoom.roomId), @"code" : @(code), @"message" : message }];
}
}
- (void)didRoomDisConnect:(StringeeRoom *)stringeeRoom {
RCTLogInfo(@"Đã ngắt kết nối tới room");
if ([jsEvents containsObject:didRoomDisConnect]) {
[self sendEventWithName:didRoomDisConnect body:@{ @"roomId" : @(stringeeRoom.roomId) }];
}
}
// Publish and unpublish
- (void)didStreamPublish:(StringeeRoom *)stringeeRoom stream:(StringeeRoomStream *)stream {
RCTLogInfo(@"Publish local stream thành công - streamId: %@", stream.streamId);
if (publishCallback) {
publishCallback(@[@(YES), @(0), @"Publish local stream successfully.", stream.streamId]);
publishCallback = nil;
}
}
- (void)didStreamPublishError:(StringeeRoom *)stringeeRoom stream:(StringeeRoomStream *)stream error:(NSString *)error {
RCTLogInfo(@"%@", error);
if (publishCallback) {
publishCallback(@[@(NO), @(-3), @"Generic error."]);
publishCallback = nil;
}
}
- (void)didStreamUnPublish:(StringeeRoom *)stringeeRoom stream:(StringeeRoomStream *)stream {
RCTLogInfo(@"unPublish local stream thành công - streamId: %@", stream.streamId);
if (unPublishCallback) {
unPublishCallback(@[@(YES), @"Unpublish local stream successfully."]);
unPublishCallback = nil;
}
}
- (void)didStreamUnPublishError:(StringeeRoom *)stringeeRoom stream:(StringeeRoomStream *)stream error:(NSString *)error {
RCTLogInfo(@"%@", error);
if (unPublishCallback) {
unPublishCallback(@[@(NO), @(-4), @"Generic error."]);
unPublishCallback = nil;
}
}
- (void)didStreamAdd:(StringeeRoom *)stringeeRoom stream:(StringeeRoomStream *)stream {
if ([jsEvents containsObject:didStreamAdd]) {
NSDictionary *info = @{@"userId" : stream.userId, @"streamId" : stream.streamId};
[remoteStreams setObject:stream forKey:stream.streamId];
[self sendEventWithName:didStreamAdd body:@{ @"roomId" : @(stringeeRoom.roomId), @"stream" : info }];
}
}
- (void)didStreamSubscribe:(StringeeRoom *)stringeeRoom stream:(StringeeRoomStream *)stream {
RCTLogInfo(@"Đã subscribe stream - streamId: %@", stream.streamId);
if (!hasChangedSpeakerPhone) {
// Set lai speaker
hasChangedSpeakerPhone = !hasChangedSpeakerPhone;
[[StringeeAudioManager instance] setLoudspeaker:isSpeaker];
}
if (subscribeCallback) {
subscribeCallback(@[@(YES), @(0), @"Subscribe stream successfully."]);
subscribeCallback = nil;
}
}
- (void)didStreamSubscribeError:(StringeeRoom *)stringeeRoom stream:(StringeeRoomStream *)stream error:(NSString *)error {
RCTLogInfo(@"%@", error);
if (subscribeCallback) {
subscribeCallback(@[@(NO), @(-5), @"Generic error."]);
subscribeCallback = nil;
}
}
- (void)didStreamUnSubscribe:(StringeeRoom *)stringeeRoom stream:(StringeeRoomStream *)stream {
RCTLogInfo(@"Đã unsubscribe stream - streamId: %@", stream.streamId);
if (unSubscribeCallback) {
unSubscribeCallback(@[@(YES), @(0), @"Unsubscribe stream successfully."]);
unSubscribeCallback = nil;
}
}
- (void)didStreamUnSubscribeError:(StringeeRoom *)stringeeRoom stream:(StringeeRoomStream *)stream error:(NSString *)error {
RCTLogInfo(@"%@", error);
if (unSubscribeCallback) {
unSubscribeCallback(@[@(NO), @(-5), @"Generic error."]);
unSubscribeCallback = nil;
}
}
- (void)didStreamRemove:(StringeeRoom *)stringeeRoom stream:(StringeeRoomStream *)stream {
RCTLogInfo(@"Đã xóa stream - streamId: %@", stream.streamId);
if ([jsEvents containsObject:didStreamRemove]) {
NSDictionary *info = @{@"userId" : stream.userId, @"streamId" : stream.streamId};
[remoteStreams removeObjectForKey:stream.streamId];
[self sendEventWithName:didStreamRemove body:@{ @"roomId" : @(stringeeRoom.roomId), @"stream" : info }];
}
}
@end