forked from callstack/react-native-opentok
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RCTOpenTokSubscriberView.m
150 lines (123 loc) · 3.41 KB
/
RCTOpenTokSubscriberView.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
/**
* Copyright (c) 2015-present, Callstack Sp z o.o.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
@import UIKit;
#import "RCTOpenTokSubscriberView.h"
#import "React/RCTEventDispatcher.h"
#import "React/RCTUtils.h"
#import <OpenTok/OpenTok.h>
@interface RCTOpenTokSubscriberView () <OTSessionDelegate, OTSubscriberDelegate>
@end
@implementation RCTOpenTokSubscriberView {
OTSession *_session;
OTSubscriber *_subscriber;
}
/**
* Mounts component after all props were passed
*/
- (void)didMoveToWindow {
[super didMoveToSuperview];
[self mount];
}
/**
* Creates a new session with a given apiKey, sessionID and token
*
* Calls `onSubscribeError` in case an error happens during initial creation.
*/
- (void)mount {
[self cleanupSubscriber];
_session = [[OTSession alloc] initWithApiKey:_apiKey sessionId:_sessionId delegate:self];
OTError *error = nil;
[_session connectWithToken:_token error:&error];
if (error) {
_onSubscribeError(RCTJSErrorFromNSError(error));
}
}
/**
* Creates an instance of `OTSubscriber` and subscribes to stream in current
* session
*/
- (void)doSubscribe:(OTStream*)stream {
_subscriber = [[OTSubscriber alloc] initWithStream:stream delegate:self];
OTError *error = nil;
[_session subscribe:_subscriber error:&error];
if (error)
{
_onSubscribeError(RCTJSErrorFromNSError(error));
return;
}
[self attachSubscriberView];
}
/**
* Attaches subscriber preview
*/
- (void)attachSubscriberView {
[_subscriber.view setFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
[self addSubview:_subscriber.view];
}
/**
* Cleans subscriber
*/
- (void)cleanupSubscriber {
[_subscriber.view removeFromSuperview];
_subscriber = nil;
}
#pragma mark - OTSession delegate callbacks
/**
* Called when session is created
*/
- (void)sessionDidConnect:(OTSession*)session {}
/**
* Called when session is destroyed
*/
- (void)sessionDidDisconnect:(OTSession*)session {}
/**
* When stream is created we start subscribtion
*
* @todo we only support subscribing to a single stream, multiple streams
* are out of scope for our use-cases. Contributions welcome.
*/
- (void)session:(OTSession*)session streamCreated:(OTStream*)stream {
if (_subscriber == nil) {
[self doSubscribe:stream];
}
}
/**
* Called when stream is destroyed
*/
- (void)session:(OTSession*)session streamDestroyed:(OTStream*)stream {
_onSubscribeStop(@{});
}
/**
* Called when session error occurs
*/
- (void)session:(OTSession*)session didFailWithError:(OTError*)error {
_onSubscribeError(RCTJSErrorFromNSError(error));
}
#pragma mark - OTSubscriber delegate callbacks
- (void)subscriber:(OTSubscriberKit*)subscriber didFailWithError:(OTError*)error {
_onSubscribeError(RCTJSErrorFromNSError(error));
[self cleanupSubscriber];
}
- (void)subscriberDidConnectToStream:(OTSubscriberKit*)subscriber {
_onSubscribeStart(@{});
}
- (void)subscriberDidDisconnectFromStream:(OTSubscriberKit*)subscriber {
_onSubscribeStop(@{});
[self cleanupSubscriber];
}
- (void)subscriberDidReconnectToStream:(OTSubscriberKit*)subscriber {
_onSubscribeStart(@{});
}
/**
* Remove session when this component is unmounted
*/
- (void)dealloc {
[self cleanupSubscriber];
[_session disconnect:nil];
}
@end