blob: 4daa1cd2fc590d9a1df3be205b0404cd18d22371 [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';
Gabriel Rochon15a5fb22022-11-27 19:25:14 -050050import { ConversationContext } from '../contexts/ConversationProvider';
simonf929a362022-11-18 16:53:45 -050051import { CallPending } from './CallPending';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040052
simon1170c322022-10-31 14:51:31 -040053export default () => {
simon2a5cf142022-11-25 15:47:35 -050054 const { callRole, callStatus, isChatShown, isFullscreen } = useContext(CallContext);
55 const callInterfaceRef = useRef<HTMLDivElement>();
MichelleSS55164202022-11-25 18:36:14 -050056 const { state } = useLocation();
simon2a5cf142022-11-25 15:47:35 -050057
58 useEffect(() => {
59 if (!callInterfaceRef.current) {
60 return;
61 }
62
63 if (isFullscreen && document.fullscreenElement === null) {
64 callInterfaceRef.current.requestFullscreen();
65 } else if (!isFullscreen && document.fullscreenEnabled !== null) {
66 document.exitFullscreen();
67 }
68 }, [isFullscreen]);
simonf929a362022-11-18 16:53:45 -050069
simon9f814a32022-11-22 21:40:53 -050070 if (callStatus !== CallStatus.InCall) {
71 return (
72 <CallPending
73 pending={callRole}
simonff1cb352022-11-24 15:15:26 -050074 caller={callStatus === CallStatus.Connecting ? 'connecting' : 'calling'}
MichelleSS55164202022-11-25 18:36:14 -050075 medium={state?.isVideoOn ? 'video' : 'audio'}
simon9f814a32022-11-22 21:40:53 -050076 />
77 );
simonf929a362022-11-18 16:53:45 -050078 }
simon9f814a32022-11-22 21:40:53 -050079
simonf9d78f22022-11-25 15:47:15 -050080 return (
simon2a5cf142022-11-25 15:47:35 -050081 <Box ref={callInterfaceRef} flexGrow={1} display="flex">
simonf9d78f22022-11-25 15:47:15 -050082 <CallInterface />
83 {isChatShown && <CallChatDrawer />}
84 </Box>
85 );
simon1170c322022-10-31 14:51:31 -040086};
87
simon33c06182022-11-02 17:39:31 -040088interface Props {
89 children?: ReactNode;
90}
91
simon1170c322022-10-31 14:51:31 -040092const CallInterface = () => {
simon8b496b02022-11-29 01:46:46 -050093 const { isVideoOn, localStream, remoteStream } = useContext(CallContext);
simon33c06182022-11-02 17:39:31 -040094 const gridItemRef = useRef(null);
simonf929a362022-11-18 16:53:45 -050095 const remoteVideoRef = useRef<HTMLVideoElement | null>(null);
96 const localVideoRef = useRef<HTMLVideoElement | null>(null);
97
98 useEffect(() => {
99 if (localStream && localVideoRef.current) {
100 localVideoRef.current.srcObject = localStream;
101 }
102 }, [localStream]);
103
104 useEffect(() => {
105 if (remoteStream && remoteVideoRef.current) {
106 remoteVideoRef.current.srcObject = remoteStream;
107 }
108 }, [remoteStream]);
simonce2c0c42022-11-02 17:39:31 -0400109
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400110 return (
simonf9d78f22022-11-25 15:47:15 -0500111 <Box display="flex" flexGrow={1}>
simonce2c0c42022-11-02 17:39:31 -0400112 <video
113 ref={remoteVideoRef}
114 autoPlay
simon9f814a32022-11-22 21:40:53 -0500115 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400116 />
simon9f814a32022-11-22 21:40:53 -0500117 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
118 {/* Guest video, takes the whole screen */}
simon8b496b02022-11-29 01:46:46 -0500119 <CallInterfaceInformation />
simon9f814a32022-11-22 21:40:53 -0500120 <Box flexGrow={1} marginY={2} position="relative">
simonf929a362022-11-18 16:53:45 -0500121 <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}>
122 <video
123 ref={localVideoRef}
124 autoPlay
Issam E. Maghni89615932022-11-29 19:05:28 +0000125 muted
simonf929a362022-11-18 16:53:45 -0500126 style={{
127 position: 'absolute',
128 right: 0,
simonf929a362022-11-18 16:53:45 -0500129 borderRadius: '12px',
simonf929a362022-11-18 16:53:45 -0500130 maxHeight: '50%',
simon9f814a32022-11-22 21:40:53 -0500131 maxWidth: '50%',
simonf929a362022-11-18 16:53:45 -0500132 visibility: isVideoOn ? 'visible' : 'hidden',
133 }}
134 />
135 </Draggable>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400136 </Box>
simon33c06182022-11-02 17:39:31 -0400137 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400138 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400139 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
140 <div>
141 <CallInterfacePrimaryButtons />
142 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400143 </Grid>
simon33c06182022-11-02 17:39:31 -0400144 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
145 <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400146 </Grid>
147 </Grid>
simon9f814a32022-11-22 21:40:53 -0500148 </Box>
simonf9d78f22022-11-25 15:47:15 -0500149 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400150 );
simon1170c322022-10-31 14:51:31 -0400151};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400152
Gabriel Rochone382a302022-11-23 12:37:04 -0500153const formatElapsedSeconds = (elapsedSeconds: number): string => {
154 const seconds = Math.floor(elapsedSeconds % 60);
155 elapsedSeconds = Math.floor(elapsedSeconds / 60);
156 const minutes = elapsedSeconds % 60;
157 elapsedSeconds = Math.floor(elapsedSeconds / 60);
158 const hours = elapsedSeconds % 24;
159
160 const times: string[] = [];
161 if (hours > 0) {
162 times.push(hours.toString().padStart(2, '0'));
163 }
164 times.push(minutes.toString().padStart(2, '0'));
165 times.push(seconds.toString().padStart(2, '0'));
166
167 return times.join(':');
168};
169
simon8b496b02022-11-29 01:46:46 -0500170const CallInterfaceInformation = () => {
171 const { callStartTime } = useContext(CallContext);
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500172 const { conversation } = useContext(ConversationContext);
simon8b496b02022-11-29 01:46:46 -0500173 const [elapsedTime, setElapsedTime] = useState<number>(0);
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500174 const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]);
simon8b496b02022-11-29 01:46:46 -0500175
176 useEffect(() => {
177 if (callStartTime) {
178 const interval = setInterval(() => {
179 setElapsedTime((new Date().getTime() - callStartTime.getTime()) / 1000);
180 }, 1000);
181 return () => clearInterval(interval);
182 }
183 }, [callStartTime]);
184
Gabriel Rochone382a302022-11-23 12:37:04 -0500185 const elapsedTimerString = formatElapsedSeconds(elapsedTime);
186
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400187 return (
188 <Stack direction="row" justifyContent="space-between" alignItems="center">
189 <Typography color="white" component="p">
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500190 {memberName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400191 </Typography>
192 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500193 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400194 </Typography>
195 </Stack>
196 );
197};
198
199const CallInterfacePrimaryButtons = () => {
200 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500201 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
202 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400203 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500204 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400205 <CallingVideoCameraButton />
206 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400207 </Card>
208 );
209};
210
simon33c06182022-11-02 17:39:31 -0400211const SECONDARY_BUTTONS = [
212 CallingVolumeButton,
213 CallingGroupButton,
214 CallingChatButton,
215 CallingScreenShareButton,
216 CallingRecordButton,
217 CallingExtensionButton,
218 CallingFullScreenButton,
219];
220
221const CallInterfaceSecondaryButtons = (props: Props & { gridItemRef: React.RefObject<HTMLElement> }) => {
222 const stackRef = useRef<HTMLElement>(null);
223
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500224 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400225 const [hiddenStackCount, setHiddenStackCount] = useState(0);
226 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
227
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500228 const calculateStackCount = useCallback(() => {
229 if (stackRef?.current && props.gridItemRef?.current) {
230 const buttonWidth = stackRef.current.children[0].clientWidth;
231 const availableSpace = props.gridItemRef.current.clientWidth;
232 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
233 if (availableButtons < SECONDARY_BUTTONS.length) {
234 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400235 }
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500236 setHiddenStackCount(SECONDARY_BUTTONS.length - availableButtons);
237 }
238 }, [props.gridItemRef]);
239
240 useLayoutEffect(() => {
241 // Run once, at the beginning, for initial measurements
242 if (!initialMeasurementDone) {
243 calculateStackCount();
244 setInitialMeasurementDone(true);
245 }
246
247 const onResize = () => {
248 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400249 };
250 window.addEventListener('resize', onResize);
251 return () => {
252 window.removeEventListener('resize', onResize);
253 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500254 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400255
256 const { displayedButtons, hiddenButtons } = useMemo(() => {
257 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
258 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
259 SECONDARY_BUTTONS.forEach((button, i) => {
260 if (i < SECONDARY_BUTTONS.length - hiddenStackCount) {
261 displayedButtons.push(button);
262 } else {
263 hiddenButtons.push(button);
264 }
265 });
266
267 return {
268 displayedButtons,
269 hiddenButtons,
270 };
271 }, [hiddenStackCount]);
272
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400273 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500274 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
275 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
276 {initialMeasurementDone &&
277 displayedButtons.map((SecondaryButton, i) => (
278 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500279 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500280 </Fragment>
281 ))}
282 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500283 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500284 )}
285 </Stack>
286
287 {!!hiddenButtons.length && hiddenMenuVisible && (
288 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
289 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400290 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500291 direction="column"
292 justifyContent="flex-end"
293 alignItems="flex-end"
294 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400295 >
296 {hiddenButtons.map((SecondaryButton, i) => (
297 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500298 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400299 </Fragment>
300 ))}
301 </Stack>
302 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500303 </Box>
304 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400305 </Card>
306 );
307};