-
Notifications
You must be signed in to change notification settings - Fork 64
/
ParticipantView.js
524 lines (497 loc) · 17.1 KB
/
ParticipantView.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
import { Popover, Transition } from "@headlessui/react";
import { XIcon } from "@heroicons/react/outline";
import { useParticipant } from "@videosdk.live/react-sdk";
import { Fragment, useEffect, useMemo, useRef, useState } from "react";
import ReactPlayer from "react-player";
import { useMediaQuery } from "react-responsive";
import useIsMobile from "../hooks/useIsMobile";
import useIsTab from "../hooks/useIsTab";
import useWindowSize from "../hooks/useWindowSize";
import MicOffSmallIcon from "../icons/MicOffSmallIcon";
import NetworkIcon from "../icons/NetworkIcon";
import SpeakerIcon from "../icons/SpeakerIcon";
import { getQualityScore, nameTructed } from "../utils/common";
import * as ReactDOM from "react-dom";
import { useMeetingAppContext } from "../MeetingAppContextDef";
export const CornerDisplayName = ({
participantId,
isPresenting,
displayName,
isLocal,
micOn,
mouseOver,
isActiveSpeaker,
}) => {
const isMobile = useIsMobile();
const isTab = useIsTab();
const isLGDesktop = useMediaQuery({ minWidth: 1024, maxWidth: 1439 });
const isXLDesktop = useMediaQuery({ minWidth: 1440 });
const { height: windowHeight } = useWindowSize();
const [statsBoxHeightRef, setStatsBoxHeightRef] = useState(null);
const [statsBoxWidthRef, setStatsBoxWidthRef] = useState(null);
const [coords, setCoords] = useState({}); // takes current button coordinates
const statsBoxHeight = useMemo(
() => statsBoxHeightRef?.offsetHeight,
[statsBoxHeightRef]
);
const statsBoxWidth = useMemo(
() => statsBoxWidthRef?.offsetWidth,
[statsBoxWidthRef]
);
const analyzerSize = isXLDesktop
? 32
: isLGDesktop
? 28
: isTab
? 24
: isMobile
? 20
: 18;
const show = useMemo(() => mouseOver, [mouseOver]);
const {
webcamStream,
micStream,
screenShareStream,
getVideoStats,
getAudioStats,
getShareStats,
getShareAudioStats
} = useParticipant(participantId);
const statsIntervalIdRef = useRef();
const [score, setScore] = useState({});
const [audioStats, setAudioStats] = useState({});
const [videoStats, setVideoStats] = useState({});
const updateStats = async () => {
let stats = [];
let audioStats = [];
let videoStats = [];
if (isPresenting) {
stats = await getShareStats();
} else if (webcamStream) {
stats = await getVideoStats();
} else if (micStream) {
stats = await getAudioStats();
}
if (webcamStream || micStream || isPresenting) {
videoStats = isPresenting ? await getShareStats() : await getVideoStats();
audioStats = isPresenting ? await getShareAudioStats() : await getAudioStats();
}
let score = stats
? stats.length > 0
? getQualityScore(stats[0])
: 100
: 100;
setScore(score);
setAudioStats(audioStats);
setVideoStats(videoStats);
};
const qualityStateArray = [
{ label: "", audio: "Audio", video: "Video" },
{
label: "Latency",
audio:
audioStats && audioStats[0]?.rtt ? `${audioStats[0]?.rtt} ms` : "-",
video:
videoStats && videoStats[0]?.rtt ? `${videoStats[0]?.rtt} ms` : "-",
},
{
label: "Jitter",
audio:
audioStats && audioStats[0]?.jitter
? `${parseFloat(audioStats[0]?.jitter).toFixed(2)} ms`
: "-",
video:
videoStats && videoStats[0]?.jitter
? `${parseFloat(videoStats[0]?.jitter).toFixed(2)} ms`
: "-",
},
{
label: "Packet Loss",
audio: audioStats
? audioStats[0]?.packetsLost
? `${parseFloat(
(audioStats[0]?.packetsLost * 100) / audioStats[0]?.totalPackets
).toFixed(2)}%`
: "-"
: "-",
video: videoStats
? videoStats[0]?.packetsLost
? `${parseFloat(
(videoStats[0]?.packetsLost * 100) / videoStats[0]?.totalPackets
).toFixed(2)}%`
: "-"
: "-",
},
{
label: "Bitrate",
audio:
audioStats && audioStats[0]?.bitrate
? `${parseFloat(audioStats[0]?.bitrate).toFixed(2)} kb/s`
: "-",
video:
videoStats && videoStats[0]?.bitrate
? `${parseFloat(videoStats[0]?.bitrate).toFixed(2)} kb/s`
: "-",
},
{
label: "Frame rate",
audio: "-",
video:
videoStats &&
(videoStats[0]?.size?.framerate === null ||
videoStats[0]?.size?.framerate === undefined)
? "-"
: `${videoStats ? videoStats[0]?.size?.framerate : "-"}`,
},
{
label: "Resolution",
audio: "-",
video: videoStats
? videoStats && videoStats[0]?.size?.width === null
? "-"
: `${videoStats[0]?.size?.width}x${videoStats[0]?.size?.height}`
: "-",
},
{
label: "Codec",
audio: audioStats && audioStats[0]?.codec ? audioStats[0]?.codec : "-",
video: videoStats && videoStats[0]?.codec ? videoStats[0]?.codec : "-",
},
{
label: "Cur. Layers",
audio: "-",
video:
videoStats && !isLocal
? videoStats && videoStats[0]?.currentSpatialLayer === null
? "-"
: `S:${videoStats[0]?.currentSpatialLayer || 0} T:${
videoStats[0]?.currentTemporalLayer || 0
}`
: "-",
},
{
label: "Pref. Layers",
audio: "-",
video:
videoStats && !isLocal
? videoStats && videoStats[0]?.preferredSpatialLayer === null
? "-"
: `S:${videoStats[0]?.preferredSpatialLayer || 0} T:${
videoStats[0]?.preferredTemporalLayer || 0
}`
: "-",
},
];
useEffect(() => {
if (webcamStream || micStream || screenShareStream) {
updateStats();
if (statsIntervalIdRef.current) {
clearInterval(statsIntervalIdRef.current);
}
statsIntervalIdRef.current = setInterval(updateStats, 500);
} else {
if (statsIntervalIdRef.current) {
clearInterval(statsIntervalIdRef.current);
statsIntervalIdRef.current = null;
}
}
return () => {
if (statsIntervalIdRef.current) clearInterval(statsIntervalIdRef.current);
};
}, [webcamStream, micStream, screenShareStream]);
return (
<>
<div
className="absolute bottom-2 left-2 rounded-md flex items-center justify-center p-2"
style={{
backgroundColor: "#00000066",
transition: "all 200ms",
transitionTimingFunction: "linear",
transform: `scale(${show ? 1 : 0})`,
}}
>
{!micOn && !isPresenting ? (
<MicOffSmallIcon fillcolor="white" />
) : micOn && isActiveSpeaker ? (
<SpeakerIcon />
) : null}
<p className="text-sm text-white ml-0.5">
{isPresenting
? isLocal
? `You are presenting`
: `${nameTructed(displayName, 15)} is presenting`
: isLocal
? "You"
: nameTructed(displayName, 26)}
</p>
</div>
{(webcamStream || micStream || screenShareStream) && (
<div>
<div
onClick={(e) => {
e.stopPropagation();
}}
className="absolute top-2 right-2 rounded-md p-2 cursor-pointer "
>
<Popover className="relative ">
{({ close }) => (
<>
<Popover.Button
className={`absolute right-0 top-0 rounded-md flex items-center justify-center p-1.5 cursor-pointer`}
style={{
backgroundColor:
score > 7
? "#3BA55D"
: score > 4
? "#faa713"
: "#FF5D5D",
}}
onClick={(e) => {
e.stopPropagation();
const rect = e.target.getBoundingClientRect();
setCoords({
left: Math.round(rect.x + rect.width / 2),
top: Math.round(rect.y + window.scrollY),
});
}}
>
<div>
<NetworkIcon
color1={"#ffffff"}
color2={"#ffffff"}
color3={"#ffffff"}
color4={"#ffffff"}
style={{
height: analyzerSize * 0.6,
width: analyzerSize * 0.6,
}}
/>
</div>
</Popover.Button>
<Transition
as={Fragment}
enter="transition ease-out duration-200"
enterFrom="opacity-0 translate-y-1"
enterTo="opacity-100 translate-y-0"
leave="transition ease-in duration-150"
leaveFrom="opacity-100 translate-y-0"
leaveTo="opacity-0 translate-y-1"
>
<Popover.Panel style={{ zIndex: 999 }} className="absolute">
{ReactDOM.createPortal(
<div
ref={setStatsBoxWidthRef}
style={{
top:
coords?.top + statsBoxHeight > windowHeight
? windowHeight - statsBoxHeight - 20
: coords?.top,
left:
coords?.left - statsBoxWidth < 0
? 12
: coords?.left - statsBoxWidth,
}}
className={`absolute`}
>
<div
ref={setStatsBoxHeightRef}
className="bg-gray-800 rounded-lg shadow-lg ring-1 ring-black ring-opacity-5 "
>
<div
className={`p-[9px] flex items-center justify-between rounded-t-lg`}
style={{
backgroundColor:
score > 7
? "#3BA55D"
: score > 4
? "#faa713"
: "#FF5D5D",
}}
>
<p className="text-sm text-white font-semibold">{`Quality Score : ${
score > 7
? "Good"
: score > 4
? "Average"
: "Poor"
}`}</p>
<button
className="cursor-pointer text-white hover:bg-[#ffffff33] rounded-full px-1 text-center"
onClick={(e) => {
e.stopPropagation();
close();
}}
>
<XIcon
className="text-white"
style={{ height: 16, width: 16 }}
/>
</button>
</div>
<div className="flex">
<div className="flex flex-col">
{qualityStateArray.map((item, index) => {
return (
<div
className="flex"
style={{
borderBottom:
index === qualityStateArray.length - 1
? ""
: `1px solid #ffffff33`,
}}
>
<div className="flex flex-1 items-center w-[120px]">
{index !== 0 && (
<p className="text-xs text-white my-[6px] ml-2">
{item.label}
</p>
)}
</div>
<div
className="flex flex-1 items-center justify-center"
style={{
borderLeft: `1px solid #ffffff33`,
}}
>
<p className="text-xs text-white my-[6px] w-[80px] text-center">
{item.audio}
</p>
</div>
<div
className="flex flex-1 items-center justify-center"
style={{
borderLeft: `1px solid #ffffff33`,
}}
>
<p className="text-xs text-white my-[6px] w-[80px] text-center">
{item.video}
</p>
</div>
</div>
);
})}
</div>
</div>
</div>
</div>,
document.body
)}
</Popover.Panel>
</Transition>
</>
)}
</Popover>
</div>
</div>
)}
</>
);
};
export function ParticipantView({ participantId }) {
const {
displayName,
webcamStream,
micStream,
webcamOn,
micOn,
isLocal,
mode,
isActiveSpeaker,
} = useParticipant(participantId);
const { selectedSpeaker } = useMeetingAppContext();
const micRef = useRef(null);
const [mouseOver, setMouseOver] = useState(false);
useEffect(() => {
const isFirefox =
navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
if (micRef.current) {
try{
if (!isFirefox){
micRef.current.setSinkId(selectedSpeaker.id);
}
}catch(err){
console.log("Setting speaker device failed", err);
}
}
}, [ selectedSpeaker]);
useEffect(() => {
if (micRef.current) {
if (micOn && micStream) {
const mediaStream = new MediaStream();
mediaStream.addTrack(micStream.track);
micRef.current.srcObject = mediaStream;
micRef.current
.play()
.catch((error) =>
console.error("micRef.current.play() failed", error)
);
}else {
micRef.current.srcObject = null;
}
}
}, [micStream, micOn, micRef])
const webcamMediaStream = useMemo(() => {
if (webcamOn && webcamStream) {
const mediaStream = new MediaStream();
mediaStream.addTrack(webcamStream.track);
return mediaStream;
}
}, [webcamStream, webcamOn]);
return mode === "CONFERENCE" ? (
<div
onMouseEnter={() => {
setMouseOver(true);
}}
onMouseLeave={() => {
setMouseOver(false);
}}
className={`h-full w-full bg-gray-750 relative overflow-hidden rounded-lg video-cover`}
>
<audio ref={micRef} autoPlay muted={isLocal} />
{webcamOn ? (
<ReactPlayer
//
playsinline // very very imp prop
playIcon={<></>}
//
pip={false}
light={false}
controls={false}
muted={true}
playing={true}
//
url={webcamMediaStream}
//
height={"100%"}
width={"100%"}
onError={(err) => {
console.log(err, "participant video error");
}}
/>
) : (
<div className="h-full w-full flex items-center justify-center">
<div
className={`z-10 flex items-center justify-center rounded-full bg-gray-800 2xl:h-[92px] h-[52px] 2xl:w-[92px] w-[52px]`}
>
<p className="text-2xl text-white">
{String(displayName).charAt(0).toUpperCase()}
</p>
</div>
</div>
)}
<CornerDisplayName
{...{
isLocal,
displayName,
micOn,
webcamOn,
isPresenting: false,
participantId,
mouseOver,
isActiveSpeaker,
}}
/>
</div>
) : null;
}