blob: c0ab3346b5bd1827ac1916b6ae37ad632d0757c5 [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 = () => {
Gabriel Rochone382a302022-11-23 12:37:04 -050090 const { isVideoOn, localStream, remoteStream, callStartTime } = 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
Gabriel Rochone382a302022-11-23 12:37:04 -050095 const [elapsedTime, setElapsedTime] = useState<number>();
96
simonf929a362022-11-18 16:53:45 -050097 useEffect(() => {
98 if (localStream && localVideoRef.current) {
99 localVideoRef.current.srcObject = localStream;
100 }
101 }, [localStream]);
102
103 useEffect(() => {
104 if (remoteStream && remoteVideoRef.current) {
105 remoteVideoRef.current.srcObject = remoteStream;
106 }
107 }, [remoteStream]);
simonce2c0c42022-11-02 17:39:31 -0400108
Gabriel Rochone382a302022-11-23 12:37:04 -0500109 useEffect(() => {
110 if (callStartTime) {
111 const interval = setInterval(() => {
112 setElapsedTime((new Date().getTime() - callStartTime.getTime()) / 1000);
113 }, 1000);
114 return () => clearInterval(interval);
115 }
116 }, [callStartTime]);
117
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400118 return (
simonf9d78f22022-11-25 15:47:15 -0500119 <Box display="flex" flexGrow={1}>
simonce2c0c42022-11-02 17:39:31 -0400120 <video
121 ref={remoteVideoRef}
122 autoPlay
simon9f814a32022-11-22 21:40:53 -0500123 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400124 />
simon9f814a32022-11-22 21:40:53 -0500125 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
126 {/* Guest video, takes the whole screen */}
Gabriel Rochone382a302022-11-23 12:37:04 -0500127 <CallInterfaceInformation elapsedTime={elapsedTime} />
simon9f814a32022-11-22 21:40:53 -0500128 <Box flexGrow={1} marginY={2} position="relative">
simonf929a362022-11-18 16:53:45 -0500129 <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}>
130 <video
131 ref={localVideoRef}
132 autoPlay
133 style={{
134 position: 'absolute',
135 right: 0,
simonf929a362022-11-18 16:53:45 -0500136 borderRadius: '12px',
simonf929a362022-11-18 16:53:45 -0500137 maxHeight: '50%',
simon9f814a32022-11-22 21:40:53 -0500138 maxWidth: '50%',
simonf929a362022-11-18 16:53:45 -0500139 visibility: isVideoOn ? 'visible' : 'hidden',
140 }}
141 />
142 </Draggable>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400143 </Box>
simon33c06182022-11-02 17:39:31 -0400144 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400145 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400146 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
147 <div>
148 <CallInterfacePrimaryButtons />
149 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400150 </Grid>
simon33c06182022-11-02 17:39:31 -0400151 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
152 <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400153 </Grid>
154 </Grid>
simon9f814a32022-11-22 21:40:53 -0500155 </Box>
simonf9d78f22022-11-25 15:47:15 -0500156 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400157 );
simon1170c322022-10-31 14:51:31 -0400158};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400159
Gabriel Rochone382a302022-11-23 12:37:04 -0500160const formatElapsedSeconds = (elapsedSeconds: number): string => {
161 const seconds = Math.floor(elapsedSeconds % 60);
162 elapsedSeconds = Math.floor(elapsedSeconds / 60);
163 const minutes = elapsedSeconds % 60;
164 elapsedSeconds = Math.floor(elapsedSeconds / 60);
165 const hours = elapsedSeconds % 24;
166
167 const times: string[] = [];
168 if (hours > 0) {
169 times.push(hours.toString().padStart(2, '0'));
170 }
171 times.push(minutes.toString().padStart(2, '0'));
172 times.push(seconds.toString().padStart(2, '0'));
173
174 return times.join(':');
175};
176
177interface CallInterfaceInformationProps {
178 elapsedTime?: number;
179}
180
181const CallInterfaceInformation = ({ elapsedTime = 0 }: CallInterfaceInformationProps) => {
182 const elapsedTimerString = formatElapsedSeconds(elapsedTime);
183
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400184 return (
185 <Stack direction="row" justifyContent="space-between" alignItems="center">
186 <Typography color="white" component="p">
187 Alain Thérieur
188 </Typography>
189 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500190 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400191 </Typography>
192 </Stack>
193 );
194};
195
196const CallInterfacePrimaryButtons = () => {
197 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500198 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
199 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400200 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500201 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400202 <CallingVideoCameraButton />
203 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400204 </Card>
205 );
206};
207
simon33c06182022-11-02 17:39:31 -0400208const SECONDARY_BUTTONS = [
209 CallingVolumeButton,
210 CallingGroupButton,
211 CallingChatButton,
212 CallingScreenShareButton,
213 CallingRecordButton,
214 CallingExtensionButton,
215 CallingFullScreenButton,
216];
217
218const CallInterfaceSecondaryButtons = (props: Props & { gridItemRef: React.RefObject<HTMLElement> }) => {
219 const stackRef = useRef<HTMLElement>(null);
220
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500221 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400222 const [hiddenStackCount, setHiddenStackCount] = useState(0);
223 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
224
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500225 const calculateStackCount = useCallback(() => {
226 if (stackRef?.current && props.gridItemRef?.current) {
227 const buttonWidth = stackRef.current.children[0].clientWidth;
228 const availableSpace = props.gridItemRef.current.clientWidth;
229 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
230 if (availableButtons < SECONDARY_BUTTONS.length) {
231 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400232 }
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500233 setHiddenStackCount(SECONDARY_BUTTONS.length - availableButtons);
234 }
235 }, [props.gridItemRef]);
236
237 useLayoutEffect(() => {
238 // Run once, at the beginning, for initial measurements
239 if (!initialMeasurementDone) {
240 calculateStackCount();
241 setInitialMeasurementDone(true);
242 }
243
244 const onResize = () => {
245 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400246 };
247 window.addEventListener('resize', onResize);
248 return () => {
249 window.removeEventListener('resize', onResize);
250 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500251 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400252
253 const { displayedButtons, hiddenButtons } = useMemo(() => {
254 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
255 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
256 SECONDARY_BUTTONS.forEach((button, i) => {
257 if (i < SECONDARY_BUTTONS.length - hiddenStackCount) {
258 displayedButtons.push(button);
259 } else {
260 hiddenButtons.push(button);
261 }
262 });
263
264 return {
265 displayedButtons,
266 hiddenButtons,
267 };
268 }, [hiddenStackCount]);
269
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400270 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500271 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
272 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
273 {initialMeasurementDone &&
274 displayedButtons.map((SecondaryButton, i) => (
275 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500276 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500277 </Fragment>
278 ))}
279 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500280 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500281 )}
282 </Stack>
283
284 {!!hiddenButtons.length && hiddenMenuVisible && (
285 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
286 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400287 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500288 direction="column"
289 justifyContent="flex-end"
290 alignItems="flex-end"
291 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400292 >
293 {hiddenButtons.map((SecondaryButton, i) => (
294 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500295 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400296 </Fragment>
297 ))}
298 </Stack>
299 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500300 </Box>
301 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400302 </Card>
303 );
304};