blob: 5dbda362bdf19cdecfb8674fa0736728b466e74a [file] [log] [blame]
Gabriel Rochone3ec0d22022-10-08 14:27:03 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
simonf929a362022-11-18 16:53:45 -050018import { Box, Card, Grid, Stack, Typography } from '@mui/material';
Gabriel Rochon8321a0d2022-11-06 23:18:36 -050019import {
20 ComponentType,
21 Fragment,
22 ReactNode,
23 useCallback,
24 useContext,
simonf929a362022-11-18 16:53:45 -050025 useEffect,
Gabriel Rochon8321a0d2022-11-06 23:18:36 -050026 useLayoutEffect,
27 useMemo,
28 useRef,
29 useState,
30} from 'react';
simon33c06182022-11-02 17:39:31 -040031import Draggable from 'react-draggable';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040032
simon33c06182022-11-02 17:39:31 -040033import { ExpandableButtonProps } from '../components/Button';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040034import {
35 CallingChatButton,
36 CallingEndButton,
37 CallingExtensionButton,
simon33c06182022-11-02 17:39:31 -040038 CallingFullScreenButton,
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040039 CallingGroupButton,
40 CallingMicButton,
simon33c06182022-11-02 17:39:31 -040041 CallingMoreVerticalButton,
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040042 CallingRecordButton,
43 CallingScreenShareButton,
44 CallingVideoCameraButton,
45 CallingVolumeButton,
simon1170c322022-10-31 14:51:31 -040046} from '../components/CallButtons';
simonf9d78f22022-11-25 15:47:15 -050047import CallChatDrawer from '../components/CallChatDrawer';
simonf929a362022-11-18 16:53:45 -050048import { CallContext, CallStatus } from '../contexts/CallProvider';
49import { CallPending } from './CallPending';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040050
simon1170c322022-10-31 14:51:31 -040051export default () => {
simon2a5cf142022-11-25 15:47:35 -050052 const { callRole, callStatus, isChatShown, isFullscreen } = useContext(CallContext);
53 const callInterfaceRef = useRef<HTMLDivElement>();
54
55 useEffect(() => {
56 if (!callInterfaceRef.current) {
57 return;
58 }
59
60 if (isFullscreen && document.fullscreenElement === null) {
61 callInterfaceRef.current.requestFullscreen();
62 } else if (!isFullscreen && document.fullscreenEnabled !== null) {
63 document.exitFullscreen();
64 }
65 }, [isFullscreen]);
simonf929a362022-11-18 16:53:45 -050066
simon9f814a32022-11-22 21:40:53 -050067 if (callStatus !== CallStatus.InCall) {
68 return (
69 <CallPending
70 pending={callRole}
simonff1cb352022-11-24 15:15:26 -050071 caller={callStatus === CallStatus.Connecting ? 'connecting' : 'calling'}
simon9f814a32022-11-22 21:40:53 -050072 medium="audio"
73 />
74 );
simonf929a362022-11-18 16:53:45 -050075 }
simon9f814a32022-11-22 21:40:53 -050076
simonf9d78f22022-11-25 15:47:15 -050077 return (
simon2a5cf142022-11-25 15:47:35 -050078 <Box ref={callInterfaceRef} flexGrow={1} display="flex">
simonf9d78f22022-11-25 15:47:15 -050079 <CallInterface />
80 {isChatShown && <CallChatDrawer />}
81 </Box>
82 );
simon1170c322022-10-31 14:51:31 -040083};
84
simon33c06182022-11-02 17:39:31 -040085interface Props {
86 children?: ReactNode;
87}
88
simon1170c322022-10-31 14:51:31 -040089const CallInterface = () => {
simonf929a362022-11-18 16:53:45 -050090 const { isVideoOn, localStream, remoteStream } = useContext(CallContext);
simon33c06182022-11-02 17:39:31 -040091 const gridItemRef = useRef(null);
simonf929a362022-11-18 16:53:45 -050092 const remoteVideoRef = useRef<HTMLVideoElement | null>(null);
93 const localVideoRef = useRef<HTMLVideoElement | null>(null);
94
95 useEffect(() => {
96 if (localStream && localVideoRef.current) {
97 localVideoRef.current.srcObject = localStream;
98 }
99 }, [localStream]);
100
101 useEffect(() => {
102 if (remoteStream && remoteVideoRef.current) {
103 remoteVideoRef.current.srcObject = remoteStream;
104 }
105 }, [remoteStream]);
simonce2c0c42022-11-02 17:39:31 -0400106
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400107 return (
simonf9d78f22022-11-25 15:47:15 -0500108 <Box display="flex" flexGrow={1}>
simonce2c0c42022-11-02 17:39:31 -0400109 <video
110 ref={remoteVideoRef}
111 autoPlay
simon9f814a32022-11-22 21:40:53 -0500112 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400113 />
simon9f814a32022-11-22 21:40:53 -0500114 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
115 {/* Guest video, takes the whole screen */}
116 <CallInterfaceInformation />
117 <Box flexGrow={1} marginY={2} position="relative">
simonf929a362022-11-18 16:53:45 -0500118 <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}>
119 <video
120 ref={localVideoRef}
121 autoPlay
122 style={{
123 position: 'absolute',
124 right: 0,
simonf929a362022-11-18 16:53:45 -0500125 borderRadius: '12px',
simonf929a362022-11-18 16:53:45 -0500126 maxHeight: '50%',
simon9f814a32022-11-22 21:40:53 -0500127 maxWidth: '50%',
simonf929a362022-11-18 16:53:45 -0500128 visibility: isVideoOn ? 'visible' : 'hidden',
129 }}
130 />
131 </Draggable>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400132 </Box>
simon33c06182022-11-02 17:39:31 -0400133 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400134 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400135 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
136 <div>
137 <CallInterfacePrimaryButtons />
138 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400139 </Grid>
simon33c06182022-11-02 17:39:31 -0400140 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
141 <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400142 </Grid>
143 </Grid>
simon9f814a32022-11-22 21:40:53 -0500144 </Box>
simonf9d78f22022-11-25 15:47:15 -0500145 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400146 );
simon1170c322022-10-31 14:51:31 -0400147};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400148
149const CallInterfaceInformation = () => {
150 return (
151 <Stack direction="row" justifyContent="space-between" alignItems="center">
152 <Typography color="white" component="p">
153 Alain Thérieur
154 </Typography>
155 <Typography color="white" component="p">
156 01:23
157 </Typography>
158 </Stack>
159 );
160};
161
162const CallInterfacePrimaryButtons = () => {
163 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500164 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
165 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400166 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500167 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400168 <CallingVideoCameraButton />
169 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400170 </Card>
171 );
172};
173
simon33c06182022-11-02 17:39:31 -0400174const SECONDARY_BUTTONS = [
175 CallingVolumeButton,
176 CallingGroupButton,
177 CallingChatButton,
178 CallingScreenShareButton,
179 CallingRecordButton,
180 CallingExtensionButton,
181 CallingFullScreenButton,
182];
183
184const CallInterfaceSecondaryButtons = (props: Props & { gridItemRef: React.RefObject<HTMLElement> }) => {
185 const stackRef = useRef<HTMLElement>(null);
186
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500187 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400188 const [hiddenStackCount, setHiddenStackCount] = useState(0);
189 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
190
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500191 const calculateStackCount = useCallback(() => {
192 if (stackRef?.current && props.gridItemRef?.current) {
193 const buttonWidth = stackRef.current.children[0].clientWidth;
194 const availableSpace = props.gridItemRef.current.clientWidth;
195 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
196 if (availableButtons < SECONDARY_BUTTONS.length) {
197 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400198 }
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500199 setHiddenStackCount(SECONDARY_BUTTONS.length - availableButtons);
200 }
201 }, [props.gridItemRef]);
202
203 useLayoutEffect(() => {
204 // Run once, at the beginning, for initial measurements
205 if (!initialMeasurementDone) {
206 calculateStackCount();
207 setInitialMeasurementDone(true);
208 }
209
210 const onResize = () => {
211 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400212 };
213 window.addEventListener('resize', onResize);
214 return () => {
215 window.removeEventListener('resize', onResize);
216 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500217 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400218
219 const { displayedButtons, hiddenButtons } = useMemo(() => {
220 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
221 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
222 SECONDARY_BUTTONS.forEach((button, i) => {
223 if (i < SECONDARY_BUTTONS.length - hiddenStackCount) {
224 displayedButtons.push(button);
225 } else {
226 hiddenButtons.push(button);
227 }
228 });
229
230 return {
231 displayedButtons,
232 hiddenButtons,
233 };
234 }, [hiddenStackCount]);
235
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400236 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500237 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
238 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
239 {initialMeasurementDone &&
240 displayedButtons.map((SecondaryButton, i) => (
241 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500242 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500243 </Fragment>
244 ))}
245 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500246 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500247 )}
248 </Stack>
249
250 {!!hiddenButtons.length && hiddenMenuVisible && (
251 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
252 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400253 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500254 direction="column"
255 justifyContent="flex-end"
256 alignItems="flex-end"
257 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400258 >
259 {hiddenButtons.map((SecondaryButton, i) => (
260 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500261 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400262 </Fragment>
263 ))}
264 </Stack>
265 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500266 </Box>
267 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400268 </Card>
269 );
270};