blob: 11a1df5b4da27d2ff8e54ba651cdd6acda0e8fd9 [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';
MichelleSS55164202022-11-25 18:36:14 -050032import { useLocation } from 'react-router-dom';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040033
simon33c06182022-11-02 17:39:31 -040034import { ExpandableButtonProps } from '../components/Button';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040035import {
36 CallingChatButton,
37 CallingEndButton,
38 CallingExtensionButton,
simon33c06182022-11-02 17:39:31 -040039 CallingFullScreenButton,
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040040 CallingGroupButton,
41 CallingMicButton,
simon33c06182022-11-02 17:39:31 -040042 CallingMoreVerticalButton,
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040043 CallingRecordButton,
44 CallingScreenShareButton,
45 CallingVideoCameraButton,
46 CallingVolumeButton,
simon1170c322022-10-31 14:51:31 -040047} from '../components/CallButtons';
simonf9d78f22022-11-25 15:47:15 -050048import CallChatDrawer from '../components/CallChatDrawer';
simonf929a362022-11-18 16:53:45 -050049import { CallContext, CallStatus } from '../contexts/CallProvider';
50import { CallPending } from './CallPending';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040051
simon1170c322022-10-31 14:51:31 -040052export default () => {
simon2a5cf142022-11-25 15:47:35 -050053 const { callRole, callStatus, isChatShown, isFullscreen } = useContext(CallContext);
54 const callInterfaceRef = useRef<HTMLDivElement>();
MichelleSS55164202022-11-25 18:36:14 -050055 const { state } = useLocation();
simon2a5cf142022-11-25 15:47:35 -050056
57 useEffect(() => {
58 if (!callInterfaceRef.current) {
59 return;
60 }
61
62 if (isFullscreen && document.fullscreenElement === null) {
63 callInterfaceRef.current.requestFullscreen();
64 } else if (!isFullscreen && document.fullscreenEnabled !== null) {
65 document.exitFullscreen();
66 }
67 }, [isFullscreen]);
simonf929a362022-11-18 16:53:45 -050068
simon9f814a32022-11-22 21:40:53 -050069 if (callStatus !== CallStatus.InCall) {
70 return (
71 <CallPending
72 pending={callRole}
simonff1cb352022-11-24 15:15:26 -050073 caller={callStatus === CallStatus.Connecting ? 'connecting' : 'calling'}
MichelleSS55164202022-11-25 18:36:14 -050074 medium={state?.isVideoOn ? 'video' : 'audio'}
simon9f814a32022-11-22 21:40:53 -050075 />
76 );
simonf929a362022-11-18 16:53:45 -050077 }
simon9f814a32022-11-22 21:40:53 -050078
simonf9d78f22022-11-25 15:47:15 -050079 return (
simon2a5cf142022-11-25 15:47:35 -050080 <Box ref={callInterfaceRef} flexGrow={1} display="flex">
simonf9d78f22022-11-25 15:47:15 -050081 <CallInterface />
82 {isChatShown && <CallChatDrawer />}
83 </Box>
84 );
simon1170c322022-10-31 14:51:31 -040085};
86
simon33c06182022-11-02 17:39:31 -040087interface Props {
88 children?: ReactNode;
89}
90
simon1170c322022-10-31 14:51:31 -040091const CallInterface = () => {
Gabriel Rochone382a302022-11-23 12:37:04 -050092 const { isVideoOn, localStream, remoteStream, callStartTime } = useContext(CallContext);
simon33c06182022-11-02 17:39:31 -040093 const gridItemRef = useRef(null);
simonf929a362022-11-18 16:53:45 -050094 const remoteVideoRef = useRef<HTMLVideoElement | null>(null);
95 const localVideoRef = useRef<HTMLVideoElement | null>(null);
96
Gabriel Rochone382a302022-11-23 12:37:04 -050097 const [elapsedTime, setElapsedTime] = useState<number>();
98
simonf929a362022-11-18 16:53:45 -050099 useEffect(() => {
100 if (localStream && localVideoRef.current) {
101 localVideoRef.current.srcObject = localStream;
102 }
103 }, [localStream]);
104
105 useEffect(() => {
106 if (remoteStream && remoteVideoRef.current) {
107 remoteVideoRef.current.srcObject = remoteStream;
108 }
109 }, [remoteStream]);
simonce2c0c42022-11-02 17:39:31 -0400110
Gabriel Rochone382a302022-11-23 12:37:04 -0500111 useEffect(() => {
112 if (callStartTime) {
113 const interval = setInterval(() => {
114 setElapsedTime((new Date().getTime() - callStartTime.getTime()) / 1000);
115 }, 1000);
116 return () => clearInterval(interval);
117 }
118 }, [callStartTime]);
119
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400120 return (
simonf9d78f22022-11-25 15:47:15 -0500121 <Box display="flex" flexGrow={1}>
simonce2c0c42022-11-02 17:39:31 -0400122 <video
123 ref={remoteVideoRef}
124 autoPlay
simon9f814a32022-11-22 21:40:53 -0500125 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400126 />
simon9f814a32022-11-22 21:40:53 -0500127 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
128 {/* Guest video, takes the whole screen */}
Gabriel Rochone382a302022-11-23 12:37:04 -0500129 <CallInterfaceInformation elapsedTime={elapsedTime} />
simon9f814a32022-11-22 21:40:53 -0500130 <Box flexGrow={1} marginY={2} position="relative">
simonf929a362022-11-18 16:53:45 -0500131 <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}>
132 <video
133 ref={localVideoRef}
134 autoPlay
135 style={{
136 position: 'absolute',
137 right: 0,
simonf929a362022-11-18 16:53:45 -0500138 borderRadius: '12px',
simonf929a362022-11-18 16:53:45 -0500139 maxHeight: '50%',
simon9f814a32022-11-22 21:40:53 -0500140 maxWidth: '50%',
simonf929a362022-11-18 16:53:45 -0500141 visibility: isVideoOn ? 'visible' : 'hidden',
142 }}
143 />
144 </Draggable>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400145 </Box>
simon33c06182022-11-02 17:39:31 -0400146 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400147 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400148 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
149 <div>
150 <CallInterfacePrimaryButtons />
151 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400152 </Grid>
simon33c06182022-11-02 17:39:31 -0400153 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
154 <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400155 </Grid>
156 </Grid>
simon9f814a32022-11-22 21:40:53 -0500157 </Box>
simonf9d78f22022-11-25 15:47:15 -0500158 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400159 );
simon1170c322022-10-31 14:51:31 -0400160};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400161
Gabriel Rochone382a302022-11-23 12:37:04 -0500162const formatElapsedSeconds = (elapsedSeconds: number): string => {
163 const seconds = Math.floor(elapsedSeconds % 60);
164 elapsedSeconds = Math.floor(elapsedSeconds / 60);
165 const minutes = elapsedSeconds % 60;
166 elapsedSeconds = Math.floor(elapsedSeconds / 60);
167 const hours = elapsedSeconds % 24;
168
169 const times: string[] = [];
170 if (hours > 0) {
171 times.push(hours.toString().padStart(2, '0'));
172 }
173 times.push(minutes.toString().padStart(2, '0'));
174 times.push(seconds.toString().padStart(2, '0'));
175
176 return times.join(':');
177};
178
179interface CallInterfaceInformationProps {
180 elapsedTime?: number;
181}
182
183const CallInterfaceInformation = ({ elapsedTime = 0 }: CallInterfaceInformationProps) => {
184 const elapsedTimerString = formatElapsedSeconds(elapsedTime);
185
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400186 return (
187 <Stack direction="row" justifyContent="space-between" alignItems="center">
188 <Typography color="white" component="p">
189 Alain Thérieur
190 </Typography>
191 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500192 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400193 </Typography>
194 </Stack>
195 );
196};
197
198const CallInterfacePrimaryButtons = () => {
199 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500200 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
201 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400202 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500203 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400204 <CallingVideoCameraButton />
205 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400206 </Card>
207 );
208};
209
simon33c06182022-11-02 17:39:31 -0400210const SECONDARY_BUTTONS = [
211 CallingVolumeButton,
212 CallingGroupButton,
213 CallingChatButton,
214 CallingScreenShareButton,
215 CallingRecordButton,
216 CallingExtensionButton,
217 CallingFullScreenButton,
218];
219
220const CallInterfaceSecondaryButtons = (props: Props & { gridItemRef: React.RefObject<HTMLElement> }) => {
221 const stackRef = useRef<HTMLElement>(null);
222
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500223 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400224 const [hiddenStackCount, setHiddenStackCount] = useState(0);
225 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
226
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500227 const calculateStackCount = useCallback(() => {
228 if (stackRef?.current && props.gridItemRef?.current) {
229 const buttonWidth = stackRef.current.children[0].clientWidth;
230 const availableSpace = props.gridItemRef.current.clientWidth;
231 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
232 if (availableButtons < SECONDARY_BUTTONS.length) {
233 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400234 }
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500235 setHiddenStackCount(SECONDARY_BUTTONS.length - availableButtons);
236 }
237 }, [props.gridItemRef]);
238
239 useLayoutEffect(() => {
240 // Run once, at the beginning, for initial measurements
241 if (!initialMeasurementDone) {
242 calculateStackCount();
243 setInitialMeasurementDone(true);
244 }
245
246 const onResize = () => {
247 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400248 };
249 window.addEventListener('resize', onResize);
250 return () => {
251 window.removeEventListener('resize', onResize);
252 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500253 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400254
255 const { displayedButtons, hiddenButtons } = useMemo(() => {
256 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
257 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
258 SECONDARY_BUTTONS.forEach((button, i) => {
259 if (i < SECONDARY_BUTTONS.length - hiddenStackCount) {
260 displayedButtons.push(button);
261 } else {
262 hiddenButtons.push(button);
263 }
264 });
265
266 return {
267 displayedButtons,
268 hiddenButtons,
269 };
270 }, [hiddenStackCount]);
271
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400272 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500273 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
274 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
275 {initialMeasurementDone &&
276 displayedButtons.map((SecondaryButton, i) => (
277 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500278 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500279 </Fragment>
280 ))}
281 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500282 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500283 )}
284 </Stack>
285
286 {!!hiddenButtons.length && hiddenMenuVisible && (
287 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
288 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400289 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500290 direction="column"
291 justifyContent="flex-end"
292 alignItems="flex-end"
293 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400294 >
295 {hiddenButtons.map((SecondaryButton, i) => (
296 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500297 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400298 </Fragment>
299 ))}
300 </Stack>
301 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500302 </Box>
303 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400304 </Card>
305 );
306};