This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
MediaDeviceProvider.js
77 lines (68 loc) · 2.23 KB
/
MediaDeviceProvider.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
import React, { createContext, useContext, useMemo } from 'react';
import { useDaily, useDevices } from '@daily-co/daily-react-hooks';
import PropTypes from 'prop-types';
export const DEVICE_STATE_LOADING = 'loading';
export const DEVICE_STATE_PENDING = 'pending';
export const DEVICE_STATE_ERROR = 'error';
export const DEVICE_STATE_GRANTED = 'granted';
export const DEVICE_STATE_NOT_FOUND = 'not-found';
export const DEVICE_STATE_NOT_SUPPORTED = 'not-supported';
export const DEVICE_STATE_BLOCKED = 'blocked';
export const DEVICE_STATE_IN_USE = 'in-use';
export const DEVICE_STATE_OFF = 'off';
export const DEVICE_STATE_PLAYABLE = 'playable';
export const DEVICE_STATE_SENDABLE = 'sendable';
export const MediaDeviceContext = createContext();
export const MediaDeviceProvider = ({ children }) => {
const {
hasCamError,
cameras,
camState,
setCamera,
hasMicError,
microphones,
micState,
setMicrophone,
speakers,
setSpeaker,
refreshDevices,
} = useDevices();
const daily = useDaily();
const localParticipant = daily?.participants().local;
const isCamMuted = useMemo(() => {
const videoState = localParticipant?.tracks?.video?.state;
return videoState === DEVICE_STATE_OFF || videoState === DEVICE_STATE_BLOCKED || hasCamError;
}, [hasCamError, localParticipant?.tracks?.video?.state]);
const isMicMuted = useMemo(() => {
const audioState = localParticipant?.tracks?.audio?.state;
return audioState === DEVICE_STATE_OFF || audioState === DEVICE_STATE_BLOCKED || hasMicError;
}, [hasMicError, localParticipant?.tracks?.audio?.state]);
return (
<MediaDeviceContext.Provider
value={{
isCamMuted,
isMicMuted,
camError: hasCamError,
cams: cameras,
camState,
micError: hasMicError,
mics: microphones,
micState,
refreshDevices,
setCurrentCam: setCamera,
setCurrentMic: setMicrophone,
setCurrentSpeaker: setSpeaker,
speakers,
}}
>
{children}
</MediaDeviceContext.Provider>
);
};
MediaDeviceProvider.propTypes = {
children: PropTypes.node,
};
MediaDeviceProvider.defaultProps = {
children: null,
};
export const useMediaDevices = () => useContext(MediaDeviceContext);