-
Notifications
You must be signed in to change notification settings - Fork 25
/
Call.js
84 lines (76 loc) · 2.93 KB
/
Call.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
import React, { useEffect, useRef, useState, useCallback } from 'react';
import {
useParticipantIds,
useVideoTrack,
useScreenShare,
useLocalParticipant,
useDailyEvent,
} from '@daily-co/daily-react-hooks';
import './Call.css';
import Tile from '../Tile/Tile';
import UserMediaError from '../UserMediaError/UserMediaError';
export default function Call() {
/* If a participant runs into a getUserMedia() error, we need to warn them. */
const [getUserMediaError, setGetUserMediaError] = useState(false);
/* We can use the useDailyEvent() hook to listen for daily-js events. Here's a full list
* of all events: https://docs.daily.co/reference/daily-js/events */
useDailyEvent(
'camera-error',
useCallback((ev) => {
setGetUserMediaError(true);
}, []),
);
/* This is for displaying our self-view. */
const localParticipant = useLocalParticipant();
const localParticipantVideoTrack = useVideoTrack(localParticipant?.session_id);
const localVideoElement = useRef(null);
useEffect(() => {
if (!localParticipantVideoTrack.persistentTrack) return;
localVideoElement?.current &&
(localVideoElement.current.srcObject =
localParticipantVideoTrack.persistentTrack &&
new MediaStream([localParticipantVideoTrack?.persistentTrack]));
}, [localParticipantVideoTrack.persistentTrack]);
/* This is for displaying remote participants: this includes other humans, but also screen shares. */
const { screens } = useScreenShare();
const remoteParticipantIds = useParticipantIds({ filter: 'remote' });
const renderCallScreen = () => {
return (
<div className={`${screens.length > 0 ? 'is-screenshare' : 'call'}`}>
{/*Your self view*/}
{localParticipant && (
<div
className={
remoteParticipantIds?.length > 0 || screens?.length > 0
? 'self-view'
: 'self-view alone'
}>
<video autoPlay muted playsInline ref={localVideoElement} />
<div className="username">
{localParticipant?.user_name || localParticipant?.user_id} (you)
</div>
</div>
)}
{/*Videos of remote participants and screen shares*/}
{remoteParticipantIds?.length > 0 || screens?.length > 0 ? (
<>
{remoteParticipantIds.map((id) => (
<Tile key={id} id={id} />
))}
{screens.map((screen) => (
<Tile key={screen.screenId} id={screen.session_id} isScreenShare />
))}
</>
) : (
// When there are no remote participants or screen shares
<div className="info-box">
<h1>Waiting for others</h1>
<p>Invite someone by sharing this link:</p>
<span className="room-url">{window.location.href}</span>
</div>
)}
</div>
);
};
return <>{getUserMediaError ? <UserMediaError /> : renderCallScreen()}</>;
}