-
Notifications
You must be signed in to change notification settings - Fork 1
/
Call.js
89 lines (80 loc) · 2.99 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
85
86
87
88
89
import React, { useState, useCallback, useMemo, useRef, useEffect, useContext } from 'react';
import {
useParticipantIds,
useScreenShare,
useLocalParticipant,
useDailyEvent,
DailyAudio,
} from '@daily-co/daily-react';
import './Call.css';
import Tile from '../Tile/Tile';
import UserMediaError from '../UserMediaError/UserMediaError';
import { LanguageContext } from '../../contexts/Language/LanguageContext';
export default function Call() {
/* If a participant runs into a getUserMedia() error, we need to warn them. */
const [getUserMediaError, setGetUserMediaError] = useState(false);
const [lang] = useContext(LanguageContext);
const audioRef = useRef(null);
/* 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(() => {
setGetUserMediaError(true);
}, []),
);
/* This is for displaying remote participants: this includes other humans, but also screen shares. */
const { screens } = useScreenShare();
const remoteParticipantIds = useParticipantIds({ filter: 'remote' });
/* This is for displaying our self-view. */
const localParticipant = useLocalParticipant();
const isAlone = useMemo(
() => remoteParticipantIds?.length < 1 || screens?.length < 1,
[remoteParticipantIds, screens],
);
useEffect(() => {
// console.log('audioref current: ', audioRef.current);
if (audioRef.current) {
const audioTags = audioRef.current.getAllAudio();
audioTags.forEach((t) => {
if (t.dataset.sessionId) {
if (lang.remote[t.dataset.sessionId]) {
// this is an audio tag for a remote participant
const langData = lang.remote[t.dataset.sessionId];
// if their spoken language isn't what I want to hear, turn them down
if (langData.spoken !== lang.local.audio) {
t.volume = 0.1;
} else {
t.volume = 1;
}
} else if (lang.translators[t.dataset.sessionId]) {
// This is the audio tag for a translator
const langData = lang.translators[t.dataset.sessionId];
if (langData.out === lang.local.audio) {
t.volume = 1;
} else {
t.volume = 0;
}
}
}
});
}
}, [remoteParticipantIds]);
const renderCallScreen = () => (
<>
<div className="call">
{/* Your self view */}
{localParticipant && <Tile id={localParticipant.session_id} isLocal isAlone={isAlone} />}
{/* Videos of remote participants and screen shares */}
{remoteParticipantIds.map((id) => (
<Tile key={id} id={id} />
))}
{screens.map((screen) => (
<Tile key={screen.screenId} id={screen.session_id} isScreenShare />
))}
</div>
<DailyAudio ref={audioRef} />
</>
);
return getUserMediaError ? <UserMediaError /> : renderCallScreen();
}