This repository has been archived by the owner on May 7, 2024. It is now read-only.
forked from twilio/twilio-video-app-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
115 lines (104 loc) · 4.38 KB
/
index.tsx
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
import React, { createContext, ReactNode, useCallback, useState } from 'react';
import { CreateLocalTrackOptions, ConnectOptions, LocalAudioTrack, LocalVideoTrack, Room } from 'twilio-video';
import { ErrorCallback } from '../../types';
import { SelectedParticipantProvider } from './useSelectedParticipant/useSelectedParticipant';
import AttachVisibilityHandler from './AttachVisibilityHandler/AttachVisibilityHandler';
import useBackgroundSettings, { BackgroundSettings } from './useBackgroundSettings/useBackgroundSettings';
import useHandleRoomDisconnection from './useHandleRoomDisconnection/useHandleRoomDisconnection';
import useHandleTrackPublicationFailed from './useHandleTrackPublicationFailed/useHandleTrackPublicationFailed';
import useLocalTracks from './useLocalTracks/useLocalTracks';
import useRestartAudioTrackOnDeviceChange from './useRestartAudioTrackOnDeviceChange/useRestartAudioTrackOnDeviceChange';
import useRoom from './useRoom/useRoom';
import useScreenShareToggle from './useScreenShareToggle/useScreenShareToggle';
/*
* The hooks used by the VideoProvider component are different than the hooks found in the 'hooks/' directory. The hooks
* in the 'hooks/' directory can be used anywhere in a video application, and they can be used any number of times.
* the hooks in the 'VideoProvider/' directory are intended to be used by the VideoProvider component only. Using these hooks
* elsewhere in the application may cause problems as these hooks should not be used more than once in an application.
*/
export interface IVideoContext {
room: Room | null;
localTracks: (LocalAudioTrack | LocalVideoTrack)[];
isConnecting: boolean;
connect: (token: string) => Promise<void>;
onError: ErrorCallback;
getLocalVideoTrack: (newOptions?: CreateLocalTrackOptions) => Promise<LocalVideoTrack>;
isAcquiringLocalTracks: boolean;
removeLocalVideoTrack: () => void;
isSharingScreen: boolean;
toggleScreenShare: () => void;
getAudioAndVideoTracks: () => Promise<void>;
isBackgroundSelectionOpen: boolean;
setIsBackgroundSelectionOpen: (value: boolean) => void;
backgroundSettings: BackgroundSettings;
setBackgroundSettings: (settings: BackgroundSettings) => void;
}
export const VideoContext = createContext<IVideoContext>(null!);
interface VideoProviderProps {
options?: ConnectOptions;
onError: ErrorCallback;
children: ReactNode;
}
export function VideoProvider({ options, children, onError = () => {} }: VideoProviderProps) {
const onErrorCallback: ErrorCallback = useCallback(
error => {
console.log(`ERROR: ${error.message}`, error);
onError(error);
},
[onError]
);
const {
localTracks,
getLocalVideoTrack,
isAcquiringLocalTracks,
removeLocalAudioTrack,
removeLocalVideoTrack,
getAudioAndVideoTracks,
} = useLocalTracks();
const { room, isConnecting, connect } = useRoom(localTracks, onErrorCallback, options);
const [isSharingScreen, toggleScreenShare] = useScreenShareToggle(room, onError);
// Register callback functions to be called on room disconnect.
useHandleRoomDisconnection(
room,
onError,
removeLocalAudioTrack,
removeLocalVideoTrack,
isSharingScreen,
toggleScreenShare
);
useHandleTrackPublicationFailed(room, onError);
useRestartAudioTrackOnDeviceChange(localTracks);
const [isBackgroundSelectionOpen, setIsBackgroundSelectionOpen] = useState(false);
const videoTrack = localTracks.find(track => !track.name.includes('screen') && track.kind === 'video') as
| LocalVideoTrack
| undefined;
const [backgroundSettings, setBackgroundSettings] = useBackgroundSettings(videoTrack, room);
return (
<VideoContext.Provider
value={{
room,
localTracks,
isConnecting,
onError: onErrorCallback,
getLocalVideoTrack,
connect,
isAcquiringLocalTracks,
removeLocalVideoTrack,
isSharingScreen,
toggleScreenShare,
getAudioAndVideoTracks,
isBackgroundSelectionOpen,
setIsBackgroundSelectionOpen,
backgroundSettings,
setBackgroundSettings,
}}
>
<SelectedParticipantProvider room={room}>{children}</SelectedParticipantProvider>
{/*
The AttachVisibilityHandler component is using the useLocalVideoToggle hook
which must be used within the VideoContext Provider.
*/}
<AttachVisibilityHandler />
</VideoContext.Provider>
);
}