-
Notifications
You must be signed in to change notification settings - Fork 74
/
JoinMultipleChannel.tsx
376 lines (356 loc) · 9.82 KB
/
JoinMultipleChannel.tsx
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
import {
ClientRoleType,
RemoteVideoState,
RemoteVideoStateReason,
RtcConnection,
RtcStats,
VideoCanvas,
} from 'agora-electron-sdk';
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
import {
AgoraButton,
AgoraCard,
AgoraList,
AgoraText,
AgoraTextInput,
RtcSurfaceView,
} from '../../../components/ui';
import * as log from '../../../utils/log';
import { BaseComponent } from '../components/BaseComponent';
import useInitRtcEngine from '../hooks/useInitRtcEngine';
export default function JoinMultipleChannel() {
const [enableVideo] = useState<boolean>(true);
const {
channelId,
setChannelId,
token,
uid,
setUid,
joinChannelSuccess,
remoteUsers,
setRemoteUsers,
startPreview,
engine,
} =
/**
* Step 1: initRtcEngine
*/
useInitRtcEngine(enableVideo, false);
const [channelId2, setChannelId2] = useState<string>('');
const [token2] = useState<string>('');
const [uid2, setUid2] = useState<number>(0);
const [joinChannelSuccess2, setJoinChannelSuccess2] =
useState<boolean>(false);
const [remoteUsers2, setRemoteUsers2] = useState<number[]>([]);
/**
* Step 2-1: joinChannel
*/
const joinChannel = () => {
if (!channelId) {
log.error('channelId is invalid');
return;
}
if (uid <= 0) {
log.error('uid is invalid');
return;
}
// start joining channel
// 1. Users can only see each other after they join the
// same channel successfully using the same app id.
// 2. If app certificate is turned on at dashboard, token is needed
// when joining channel. The channel name and uid used to calculate
// the token has to match the ones used for channel join
engine.current.joinChannelEx(
token,
{
channelId,
localUid: uid,
},
{
// Make myself as the broadcaster to send stream to remote
clientRoleType: ClientRoleType.ClientRoleBroadcaster,
publishMicrophoneTrack: false,
publishCameraTrack: false,
}
);
};
/**
* Step 2-2: joinChannel2
*/
const joinChannel2 = () => {
if (!channelId2) {
log.error('channelId2 is invalid');
return;
}
if (uid2 < 0) {
log.error('uid2 is invalid');
return;
}
// start joining channel
// 1. Users can only see each other after they join the
// same channel successfully using the same app id.
// 2. If app certificate is turned on at dashboard, token is needed
// when joining channel. The channel name and uid used to calculate
// the token has to match the ones used for channel join
engine.current.joinChannelEx(
token2,
{
channelId: channelId2,
localUid: uid2,
},
{
// Make myself as the broadcaster to send stream to remote
clientRoleType: ClientRoleType.ClientRoleBroadcaster,
publishMicrophoneTrack: false,
publishCameraTrack: false,
}
);
};
/**
* Step 3-1: publishStreamToChannel
*/
const publishStreamToChannel = () => {
engine.current.updateChannelMediaOptionsEx(
{ publishMicrophoneTrack: false, publishCameraTrack: false },
{
channelId: channelId2,
localUid: uid2,
}
);
engine.current.updateChannelMediaOptionsEx(
{ publishMicrophoneTrack: true, publishCameraTrack: true },
{
channelId,
localUid: uid,
}
);
};
/**
* Step 3-2: publishStreamToChannel2
*/
const publishStreamToChannel2 = () => {
engine.current.updateChannelMediaOptionsEx(
{ publishMicrophoneTrack: false, publishCameraTrack: false },
{
channelId,
localUid: uid,
}
);
engine.current.updateChannelMediaOptionsEx(
{ publishMicrophoneTrack: true, publishCameraTrack: true },
{
channelId: channelId2,
localUid: uid2,
}
);
};
/**
* Step 4-1: leaveChannel
*/
const leaveChannel = () => {
engine.current.leaveChannelEx({
channelId,
localUid: uid,
});
};
/**
* Step 4-2: leaveChannel2
*/
const leaveChannel2 = () => {
engine.current.leaveChannelEx({
channelId: channelId2,
localUid: uid2,
});
};
const onJoinChannelSuccess = useCallback(
(connection: RtcConnection, elapsed: number) => {
if (connection.channelId === channelId2 && connection.localUid === uid2) {
setJoinChannelSuccess2(true);
}
},
[channelId2, uid2]
);
const onLeaveChannel = useCallback(
(connection: RtcConnection, stats: RtcStats) => {
if (connection.channelId === channelId2 && connection.localUid === uid2) {
setJoinChannelSuccess2(false);
setRemoteUsers2([]);
}
// Keep preview after leave channel
engine.current.startPreview();
},
[channelId2, engine, uid2]
);
const onRemoteVideoStateChanged = useCallback(
(
connection: RtcConnection,
remoteUid: number,
state: RemoteVideoState,
reason: RemoteVideoStateReason,
elapsed: number
) => {
log.info(
'onRemoteVideoStateChanged',
'connection',
connection,
'remoteUid',
remoteUid,
'state',
state,
'reason',
reason,
'elapsed',
elapsed
);
if (state === RemoteVideoState.RemoteVideoStateStarting) {
if (connection.channelId === channelId && connection.localUid === uid) {
setRemoteUsers((prev) => {
return [...prev, remoteUid];
});
} else if (
connection.channelId === channelId2 &&
connection.localUid === uid2
) {
setRemoteUsers2((prev) => {
return [...prev, remoteUid];
});
}
} else if (state === RemoteVideoState.RemoteVideoStateStopped) {
if (connection.channelId === channelId && connection.localUid === uid) {
setRemoteUsers((prev) => {
return prev.filter((value) => value !== remoteUid);
});
} else if (
connection.channelId === channelId2 &&
connection.localUid === uid2
) {
setRemoteUsers2((prev) => {
return prev.filter((value) => value !== remoteUid);
});
}
}
},
[channelId, channelId2, setRemoteUsers, uid, uid2]
);
useEffect(() => {
engine.current.addListener('onJoinChannelSuccess', onJoinChannelSuccess);
engine.current.addListener('onLeaveChannel', onLeaveChannel);
engine.current.addListener(
'onRemoteVideoStateChanged',
onRemoteVideoStateChanged
);
const engineCopy = engine.current;
return () => {
engineCopy.removeListener('onJoinChannelSuccess', onJoinChannelSuccess);
engineCopy.removeListener('onLeaveChannel', onLeaveChannel);
engineCopy.removeListener(
'onRemoteVideoStateChanged',
onRemoteVideoStateChanged
);
};
}, [engine, onJoinChannelSuccess, onLeaveChannel, onRemoteVideoStateChanged]);
return (
<BaseComponent
name={'JoinMultipleChannel'}
renderChannel={renderChannel}
renderUsers={renderUsers}
renderAction={renderAction}
/>
);
function renderChannel(): ReactElement | undefined {
return (
<>
<AgoraTextInput
onChangeText={(text) => {
setChannelId(text);
}}
placeholder={`channelId`}
value={channelId}
/>
<AgoraTextInput
onChangeText={(text) => {
if (isNaN(+text)) return;
setUid(+text);
}}
numberKeyboard={true}
placeholder={`uid (must > 0)`}
value={uid > 0 ? uid.toString() : ''}
/>
<AgoraButton
title={`${joinChannelSuccess ? 'leave' : 'join'} Channel`}
onPress={() => {
joinChannelSuccess ? leaveChannel() : joinChannel();
}}
/>
<AgoraTextInput
onChangeText={(text) => {
setChannelId2(text);
}}
placeholder={`channelId2`}
value={channelId2}
/>
<AgoraTextInput
onChangeText={(text) => {
if (isNaN(+text)) return;
setUid2(+text);
}}
numberKeyboard={true}
placeholder={`uid2 (must > 0)`}
value={uid2 > 0 ? uid2.toString() : ''}
/>
<AgoraButton
title={`${joinChannelSuccess2 ? 'leave' : 'join'} Channel2`}
onPress={() => {
joinChannelSuccess2 ? leaveChannel2() : joinChannel2();
}}
/>
</>
);
}
function renderUsers(): ReactElement | undefined {
return (
<>
{startPreview || joinChannelSuccess || joinChannelSuccess2 ? (
<AgoraList
data={[0, ...remoteUsers, ...remoteUsers2]}
renderItem={(item) => {
return renderVideo(
{ uid: item },
remoteUsers2.indexOf(item) === -1 ? channelId : channelId2,
remoteUsers2.indexOf(item) === -1 ? uid : uid2
);
}}
/>
) : undefined}
</>
);
}
function renderVideo(
user: VideoCanvas,
channelId?: string,
localUid?: number
): ReactElement | undefined {
return (
<AgoraCard title={`${channelId} - ${user.uid}`}>
<AgoraText>Click view to mirror</AgoraText>
<RtcSurfaceView canvas={user} connection={{ channelId, localUid }} />
</AgoraCard>
);
}
function renderAction(): ReactElement | undefined {
return (
<>
<AgoraButton
disabled={!joinChannelSuccess}
title={`publish Stream To Channel`}
onPress={publishStreamToChannel}
/>
<AgoraButton
disabled={!joinChannelSuccess2}
title={`publish Stream To Channel2`}
onPress={publishStreamToChannel2}
/>
</>
);
}
}