-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathNativeMobileSDKBridge.m
263 lines (232 loc) · 8.65 KB
/
NativeMobileSDKBridge.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
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
//
#import "NativeMobileSDKBridge.h"
#import <AVFoundation/AVFoundation.h>
#import "RNVideoViewManager.h"
#import <AmazonChimeSDKMedia/AmazonChimeSDKMedia.h>
#import "MeetingObservers.h"
#import <React/RCTUIManager.h>
@implementation NativeMobileSDKBridge
static DefaultMeetingSession *meetingSession;
static ConsoleLogger *logger;
RCT_EXPORT_MODULE();
- (NSArray<NSString *> *)supportedEvents
{
return
@[
kEventOnMeetingStart,
kEventOnMeetingEnd,
kEventOnAttendeesJoin,
kEventOnAttendeesLeave,
kEventOnAttendeesMute,
kEventOnAttendeesUnmute,
kEventOnAddVideoTile,
kEventOnRemoveVideoTile,
kEventOnDataMessageReceive,
kEventOnError
];
}
# pragma mark: Native Function
RCT_EXPORT_METHOD(startMeeting:(NSDictionary *)meetingInfoDict attendeeInfo:(NSDictionary *)attendeeInfoDict)
{
if (meetingSession != nil)
{
[meetingSession.audioVideo stop];
meetingSession = nil;
}
logger = [[ConsoleLogger alloc] initWithName:@"NativeMobileSDKBridge" level:LogLevelDEFAULT];
[logger infoWithMsg: [[NSString alloc] initWithFormat:@"Running Amazon Chime SDK (%@)", Versioning.sdkVersion]];
// Parse meeting join data from payload
NSDictionary *mediaPlacementDict = [meetingInfoDict objectForKey:kMediaPlacement];
// Parse meeting info
NSString *meetingId = [meetingInfoDict objectForKey:kMeetingId];
NSString *externalMeetingId = [meetingInfoDict objectForKey:kExternalMeetingId];
NSString *meetingRegion = [meetingInfoDict objectForKey:kMediaRegion];
// Parse meeting join info
NSString *audioFallbackUrl = [mediaPlacementDict objectForKey:kAudioFallbackUrl];
NSString *audioHostUrl = [mediaPlacementDict objectForKey:kAudioHostUrl];
NSString *turnControlUrl = [mediaPlacementDict objectForKey:kTurnControlUrl];
NSString *signalingUrl = [mediaPlacementDict objectForKey:kSignalingUrl];
// Parse attendee info
NSString *attendeeId = [attendeeInfoDict objectForKey:kAttendeeId];
NSString *externalUserId = [attendeeInfoDict objectForKey:kExternalUserId];
NSString *joinToken = [attendeeInfoDict objectForKey:kJoinToken];
// Initialize meeting session through AmazonChimeSDK
MediaPlacement *mediaPlacement = [[MediaPlacement alloc] initWithAudioFallbackUrl:audioFallbackUrl
audioHostUrl:audioHostUrl
signalingUrl:signalingUrl
turnControlUrl:turnControlUrl];
Meeting *meeting = [[Meeting alloc] initWithExternalMeetingId:externalMeetingId
mediaPlacement:mediaPlacement
mediaRegion:meetingRegion
meetingId:meetingId];
CreateMeetingResponse *createMeetingResponse = [[CreateMeetingResponse alloc] initWithMeeting:meeting];
Attendee *attendee = [[Attendee alloc] initWithAttendeeId:attendeeId
externalUserId:externalUserId joinToken:joinToken];
CreateAttendeeResponse *createAttendeeResponse = [[CreateAttendeeResponse alloc] initWithAttendee:attendee];
MeetingSessionConfiguration *meetingSessionConfiguration = [[MeetingSessionConfiguration alloc] initWithCreateMeetingResponse:createMeetingResponse
createAttendeeResponse:createAttendeeResponse];
meetingSession = [[DefaultMeetingSession alloc] initWithConfiguration:meetingSessionConfiguration
logger:logger];
[self startAudioClient];
}
RCT_EXPORT_METHOD(stopMeeting)
{
[meetingSession.audioVideo stop];
meetingSession = nil;
[self sendEventWithName:kEventOnMeetingEnd body: nil];
}
RCT_EXPORT_METHOD(setMute:(BOOL)isMute)
{
BOOL success = true;
if (isMute)
{
success = [meetingSession.audioVideo realtimeLocalMute];
}
else
{
success = [meetingSession.audioVideo realtimeLocalUnmute];
}
if (!success)
{
[self sendEventWithName:kEventOnError body:@"Failed to set mute state"];
}
}
RCT_EXPORT_METHOD(setCameraOn:(BOOL)isOn)
{
if (isOn)
{
[self startVideo];
}
else
{
[meetingSession.audioVideo stopLocalVideo];
}
}
RCT_EXPORT_METHOD(bindVideoView:(NSNumber * _Nonnull)viewIdentifier tileId:(NSNumber * _Nonnull)tileId)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIView* view = [self.bridge.uiManager viewForReactTag:viewIdentifier];
if(view != nil) {
[meetingSession.audioVideo bindVideoViewWithVideoView:(DefaultVideoRenderView*)view tileId:[tileId integerValue]];
} else {
[logger errorWithMsg:@"Failed to bind video view"];
}
});
}
RCT_EXPORT_METHOD(unbindVideoView:(NSNumber * _Nonnull)tileId)
{
dispatch_async(dispatch_get_main_queue(), ^{
[meetingSession.audioVideo unbindVideoViewWithTileId:[tileId integerValue]];
});
}
#pragma mark: Media Related Function
-(void)startVideo
{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (status)
{
case AVAuthorizationStatusNotDetermined:
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
{
if (granted)
{ // Access has been granted ..retry starting video
[self startVideo];
} else { // Access denied
[self sendEventWithName:kEventOnError body:@"User denied camera permission"];
}
}];
break;
}
case AVAuthorizationStatusAuthorized:
{
NSError* error;
[meetingSession.audioVideo startLocalVideoAndReturnError:&error];
if(error != nil)
{
[self sendEventWithName:kEventOnError body:@"Fail to start local video"];
}
break;
}
case AVAuthorizationStatusDenied:
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
break;
}
default:
break;
}
}
-(void)startAudioClient
{
if (meetingSession == nil)
{
[logger errorWithMsg:@"meetingSession is not initialized"];
return;
}
MeetingObservers* observer = [[MeetingObservers alloc] initWithBridge:self logger:logger];
[meetingSession.audioVideo addRealtimeObserverWithObserver:observer];
[meetingSession.audioVideo addVideoTileObserverWithObserver:observer];
[meetingSession.audioVideo addAudioVideoObserverWithObserver:observer];
[meetingSession.audioVideo addRealtimeDataMessageObserverWithTopic:@"chat" observer:observer];
[self startAudioVideo];
}
-(void)startAudioVideo
{
NSError* error = nil;
BOOL started = [meetingSession.audioVideo startAndReturnError:&error];
if (started && error == nil)
{
[logger infoWithMsg:@"RN meeting session was started successfully"];
[meetingSession.audioVideo startRemoteVideo];
}
else
{
NSString *errorMsg = [NSString stringWithFormat:@"Failed to start meeting, error: %@", error.description];
[logger errorWithMsg:errorMsg];
// Handle missing permission error
if ([error.domain isEqual:@"AmazonChimeSDK.PermissionError"])
{
AVAudioSessionRecordPermission permissionStatus = [[AVAudioSession sharedInstance] recordPermission];
if (permissionStatus == AVAudioSessionRecordPermissionUndetermined)
{
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted)
{
if (granted)
{
[logger infoWithMsg:@"Audio permission granted"];
// Retry after permission is granted
[self startAudioVideo];
}
else
{
[logger infoWithMsg:@"Audio permission not granted"];
[self sendEventWithName:kEventOnMeetingEnd body:nil];
}
}];
}
else if (permissionStatus == AVAudioSessionRecordPermissionDenied)
{
[logger errorWithMsg:@"User did not grant permission, should redirect to Settings"];
[self sendEventWithName:kEventOnMeetingEnd body:nil];
}
}
else
{
// Uncaught error
[self sendEventWithName:kEventOnError body: errorMsg];
[self sendEventWithName:kEventOnMeetingEnd body:nil];
}
}
}
RCT_EXPORT_METHOD(sendDataMessage:(NSString* _Nonnull)topic data:(NSString* _Nonnull)data lifetimeMs:(int)lifetimeMs)
{
if (meetingSession == nil) {
return;
}
[meetingSession.audioVideo realtimeSendDataMessageWithTopic:topic data:data lifetimeMs:lifetimeMs error:nil];
}
@end