-
Notifications
You must be signed in to change notification settings - Fork 12
/
JoiningScreen.js
531 lines (467 loc) · 16.7 KB
/
JoiningScreen.js
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import React, { useEffect, useMemo, useRef, useState } from "react";
import { MeetingDetailsScreen } from "../MeetingDetailsScreen";
import { createMeeting, getToken, validateMeeting } from "../../api";
import { CheckCircleIcon } from "@heroicons/react/outline";
import SettingDialogueBox from "../SettingDialogueBox";
import ConfirmBox from "../ConfirmBox";
import { Constants } from "@videosdk.live/react-sdk";
import useIsMobile from "../../hooks/useIsMobile";
import { createPopper } from "@popperjs/core";
import WebcamOffIcon from "../../icons/WebcamOffIcon";
import WebcamOnIcon from "../../icons/Bottombar/WebcamOnIcon";
import MicOffIcon from "../../icons/MicOffIcon";
import MicOnIcon from "../../icons/Bottombar/MicOnIcon";
export function JoiningScreen({
participantName,
setParticipantName,
setMeetingId,
setToken,
setSelectedMic,
setSelectedWebcam,
onClickStartMeeting,
micEnabled,
webcamEnabled,
setWebcamOn,
setMicOn,
setMeetingMode,
meetingMode,
}) {
const [setting, setSetting] = useState("video");
const [{ webcams, mics }, setDevices] = useState({
devices: [],
webcams: [],
mics: [],
});
const [videoTrack, setVideoTrack] = useState(null);
const [dlgMuted, setDlgMuted] = useState(false);
const [dlgDevices, setDlgDevices] = useState(false);
const videoPlayerRef = useRef();
const popupVideoPlayerRef = useRef();
const popupAudioPlayerRef = useRef();
const videoTrackRef = useRef();
const audioTrackRef = useRef();
const audioAnalyserIntervalRef = useRef();
const [settingDialogueOpen, setSettingDialogueOpen] = useState(false);
const [audioTrack, setAudioTrack] = useState(null);
const handleClickOpen = () => {
setSettingDialogueOpen(true);
};
const handleClose = (value) => {
setSettingDialogueOpen(false);
};
const isMobile = useIsMobile();
const webcamOn = useMemo(() => !!videoTrack, [videoTrack]);
const micOn = useMemo(() => !!audioTrack, [audioTrack]);
const _handleTurnOffWebcam = () => {
const videoTrack = videoTrackRef.current;
if (videoTrack) {
videoTrack.stop();
setVideoTrack(null);
setWebcamOn(false);
}
};
const _handleTurnOnWebcam = () => {
const videoTrack = videoTrackRef.current;
if (!videoTrack) {
getDefaultMediaTracks({ mic: false, webcam: true });
setWebcamOn(true);
}
};
const _toggleWebcam = () => {
const videoTrack = videoTrackRef.current;
if (videoTrack) {
_handleTurnOffWebcam();
} else {
_handleTurnOnWebcam();
}
};
const _handleTurnOffMic = () => {
const audioTrack = audioTrackRef.current;
if (audioTrack) {
audioTrack.stop();
setAudioTrack(null);
setMicOn(false);
}
};
const _handleTurnOnMic = () => {
const audioTrack = audioTrackRef.current;
if (!audioTrack) {
getDefaultMediaTracks({ mic: true, webcam: false });
setMicOn(true);
}
};
const _handleToggleMic = () => {
const audioTrack = audioTrackRef.current;
if (audioTrack) {
_handleTurnOffMic();
} else {
_handleTurnOnMic();
}
};
const changeWebcam = async (deviceId) => {
const currentvideoTrack = videoTrackRef.current;
if (currentvideoTrack) {
currentvideoTrack.stop();
}
const stream = await navigator.mediaDevices.getUserMedia({
video: { deviceId },
});
const videoTracks = stream.getVideoTracks();
const videoTrack = videoTracks.length ? videoTracks[0] : null;
setVideoTrack(videoTrack);
};
const changeMic = async (deviceId) => {
const currentAudioTrack = audioTrackRef.current;
currentAudioTrack && currentAudioTrack.stop();
const stream = await navigator.mediaDevices.getUserMedia({
audio: { deviceId },
});
const audioTracks = stream.getAudioTracks();
const audioTrack = audioTracks.length ? audioTracks[0] : null;
clearInterval(audioAnalyserIntervalRef.current);
setAudioTrack(audioTrack);
};
const getDefaultMediaTracks = async ({ mic, webcam, firstTime }) => {
if (mic) {
try {
const audioConstraints = {
audio: true,
};
const stream = await navigator.mediaDevices.getUserMedia(
audioConstraints
);
const audioTracks = stream.getAudioTracks();
const audioTrack = audioTracks.length ? audioTracks[0] : null;
setAudioTrack(audioTrack);
if (firstTime) {
setSelectedMic({
id: audioTrack?.getSettings()?.deviceId,
});
}
} catch (error) {
console.log("Failed to access microphone", error);
}
}
if (webcam) {
const videoConstraints = {
video: {
width: 1280,
height: 720,
},
};
const stream = await navigator.mediaDevices.getUserMedia(
videoConstraints
);
const videoTracks = stream.getVideoTracks();
const videoTrack = videoTracks.length ? videoTracks[0] : null;
setVideoTrack(videoTrack);
if (firstTime) {
setSelectedWebcam({
id: videoTrack?.getSettings()?.deviceId,
});
}
}
};
async function startMuteListener() {
const currentAudioTrack = audioTrackRef.current;
if (currentAudioTrack) {
if (currentAudioTrack.muted) {
setDlgMuted(true);
}
currentAudioTrack.addEventListener("mute", (ev) => {
setDlgMuted(true);
});
}
}
const getDevices = async ({ micEnabled, webcamEnabled }) => {
try {
const devices = await navigator.mediaDevices.enumerateDevices();
const webcams = devices.filter((d) => d.kind === "videoinput");
const mics = devices.filter((d) => d.kind === "audioinput");
const hasMic = mics.length > 0;
const hasWebcam = webcams.length > 0;
setDevices({ webcams, mics, devices });
if (hasMic) {
startMuteListener();
}
getDefaultMediaTracks({
mic: hasMic && micEnabled,
webcam: hasWebcam && webcamEnabled,
firstTime: true,
});
} catch (err) {
console.log(err);
}
};
useEffect(() => {
try {
audioTrackRef.current = audioTrack;
startMuteListener();
return () => {
const currentAudioTrack = audioTrackRef.current;
currentAudioTrack && currentAudioTrack.stop();
audioTrackRef.current = null;
};
} catch (error) {
console.log("Error in audio track", error);
}
}, [audioTrack]);
useEffect(() => {
if (meetingMode === Constants.modes.VIEWER) {
_handleTurnOffMic();
_handleTurnOffWebcam();
}
}, [meetingMode]);
useEffect(() => {
videoTrackRef.current = videoTrack;
var isPlaying =
videoPlayerRef.current.currentTime > 0 &&
!videoPlayerRef.current.paused &&
!videoPlayerRef.current.ended &&
videoPlayerRef.current.readyState >
videoPlayerRef.current.HAVE_CURRENT_DATA;
if (videoTrack) {
const videoSrcObject = new MediaStream([videoTrack]);
if (videoPlayerRef.current) {
videoPlayerRef.current.srcObject = videoSrcObject;
if (videoPlayerRef.current.pause && !isPlaying) {
try {
videoPlayerRef.current.play();
} catch (err) {
console.log("error in playing video", err);
}
}
}
setTimeout(() => {
if (popupVideoPlayerRef.current) {
popupVideoPlayerRef.current.srcObject = videoSrcObject;
try {
popupVideoPlayerRef.current.play();
} catch (err) {
console.log("error in playing video", err);
}
}
}, 1000);
} else {
if (videoPlayerRef.current) {
videoPlayerRef.current.srcObject = null;
}
if (popupVideoPlayerRef.current) {
popupVideoPlayerRef.current.srcObject = null;
}
}
}, [videoTrack, setting, settingDialogueOpen]);
useEffect(() => {
getDevices({ micEnabled, webcamEnabled });
}, []);
const ButtonWithTooltip = ({ onClick, onState, OnIcon, OffIcon, mic }) => {
const [tooltipShow, setTooltipShow] = useState(false);
const btnRef = useRef();
const tooltipRef = useRef();
const openTooltip = () => {
createPopper(btnRef.current, tooltipRef.current, {
placement: "top",
});
setTooltipShow(true);
};
const closeTooltip = () => {
setTooltipShow(false);
};
return (
<>
<div>
<button
ref={btnRef}
onMouseEnter={openTooltip}
onMouseLeave={closeTooltip}
onClick={onClick}
className={`rounded-full min-w-auto w-11 h-11 flex items-center justify-center ${
onState ? "bg-white" : "bg-red-650 text-white"
}`}
disabled={meetingMode === Constants.modes.VIEWER}
>
{onState ? (
<OnIcon fillcolor={onState ? "#050A0E" : "#fff"} />
) : (
<OffIcon fillcolor={onState ? "#050A0E" : "#fff"} />
)}
</button>
</div>
<div
style={{ zIndex: 999 }}
className={`${
tooltipShow ? "" : "hidden"
} overflow-hidden flex flex-col items-center justify-center pb-1.5`}
ref={tooltipRef}
>
<div className={"rounded-md p-1.5 bg-black "}>
<p className="text-base text-white ">
{onState
? `Turn off ${mic ? "mic" : "webcam"}`
: `Turn on ${mic ? "mic" : "webcam"}`}
</p>
</div>
</div>
</>
);
};
return (
<div className="fixed inset-0">
<div className="overflow-y-auto flex flex-col flex-1 h-screen bg-gray-800">
<div className="flex flex-1 flex-col md:flex-row items-center justify-center md:m-[72px] m-16">
<div className="container grid md:grid-flow-col grid-flow-row ">
<div className="grid grid-cols-12">
<div className="md:col-span-7 2xl:col-span-6 col-span-12">
<div className="flex items-center justify-center p-1.5 sm:p-4 lg:p-6">
<div className="relative w-full md:pl-4 sm:pl-10 pl-5 md:pr-4 sm:pr-10 pr-5">
<div className="w-full relative" style={{ height: "45vh" }}>
<video
autoPlay
playsInline
muted
ref={videoPlayerRef}
controls={false}
style={{
backgroundColor: "#1c1c1c",
}}
className={
"rounded-[10px] h-full w-full object-cover flex items-center justify-center flip"
}
/>
{!isMobile ? (
<>
<div className="absolute top-0 bottom-0 left-0 right-0 flex items-center justify-center">
{!webcamOn ? (
<p className="text-xl xl:text-lg 2xl:text-xl text-white">
{meetingMode === Constants.modes.VIEWER
? "You are not permitted to use your microphone and camera."
: "The camera is off"}
</p>
) : null}
</div>
</>
) : null}
{settingDialogueOpen ? (
<SettingDialogueBox
open={settingDialogueOpen}
onClose={handleClose}
popupVideoPlayerRef={popupVideoPlayerRef}
popupAudioPlayerRef={popupAudioPlayerRef}
changeWebcam={changeWebcam}
changeMic={changeMic}
setting={setting}
setSetting={setSetting}
webcams={webcams}
mics={mics}
setSelectedMic={setSelectedMic}
setSelectedWebcam={setSelectedWebcam}
videoTrack={videoTrack}
audioTrack={audioTrack}
/>
) : null}
{meetingMode === Constants.modes.CONFERENCE && (
<div className="absolute xl:bottom-6 bottom-4 left-0 right-0">
<div className="container grid grid-flow-col space-x-4 items-center justify-center md:-m-2">
<ButtonWithTooltip
onClick={_handleToggleMic}
onState={micOn}
mic={true}
OnIcon={MicOnIcon}
OffIcon={MicOffIcon}
/>
<ButtonWithTooltip
onClick={_toggleWebcam}
onState={webcamOn}
mic={false}
OnIcon={WebcamOnIcon}
OffIcon={WebcamOffIcon}
/>
</div>
</div>
)}
</div>
{!isMobile &&
meetingMode === Constants.modes.CONFERENCE && (
<div
className="m-4 absolute md:left-12 lg:left-24 xl:left-44 md:right-12 lg:right-24 xl:right-44 rounded cursor-pointer bg-gray-700"
onClick={(e) => {
handleClickOpen();
}}
>
<div className="flex flex-row items-center justify-center m-1">
<button className="text-white">
<CheckCircleIcon className="h-5 w-5" />
</button>
<p className="text-base text-white ml-1">
Check your audio and video
</p>
</div>
</div>
)}
</div>
</div>
</div>
<div className="md:col-span-5 2xl:col-span-6 col-span-12 md:relative">
<div className="flex flex-1 flex-col items-center justify-center xl:m-16 lg:m-6 md:mt-9 lg:mt-14 xl:mt-20 mt-3 md:absolute md:left-0 md:right-0 md:top-0 md:bottom-0">
<MeetingDetailsScreen
participantName={participantName}
setParticipantName={setParticipantName}
videoTrack={videoTrack}
setVideoTrack={setVideoTrack}
setMeetingMode={setMeetingMode}
meetingMode={meetingMode}
onClickStartMeeting={onClickStartMeeting}
onClickJoin={async (id) => {
const token = await getToken();
const valid = await validateMeeting({
roomId: id,
token,
});
if (valid) {
setToken(token);
setMeetingId(id);
if (videoTrack) {
videoTrack.stop();
setVideoTrack(null);
}
onClickStartMeeting();
setParticipantName("");
} else alert("Invalid Meeting Id");
}}
_handleOnCreateMeeting={async () => {
const token = await getToken();
const _meetingId = await createMeeting({ token });
setToken(token);
setMeetingId(_meetingId);
setParticipantName("");
return _meetingId;
}}
/>
</div>
</div>
</div>
</div>
</div>
</div>
<ConfirmBox
open={dlgMuted}
successText="OKAY"
onSuccess={() => {
setDlgMuted(false);
}}
title="System mic is muted"
subTitle="You're default microphone is muted, please unmute it or increase audio
input volume from system settings."
/>
<ConfirmBox
open={dlgDevices}
successText="DISMISS"
onSuccess={() => {
setDlgDevices(false);
}}
title="Mic or webcam not available"
subTitle="Please connect a mic and webcam to speak and share your video in the meeting. You can also join without them."
/>
</div>
);
}