blob: e5ecbde2f8258073d101ced37bfcf879c178d8a2 [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
125 style={{
126 position: 'absolute',
127 right: 0,
simonf929a362022-11-18 16:53:45 -0500128 borderRadius: '12px',
simonf929a362022-11-18 16:53:45 -0500129 maxHeight: '50%',
simon9f814a32022-11-22 21:40:53 -0500130 maxWidth: '50%',
simonf929a362022-11-18 16:53:45 -0500131 visibility: isVideoOn ? 'visible' : 'hidden',
132 }}
133 />
134 </Draggable>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400135 </Box>
simon33c06182022-11-02 17:39:31 -0400136 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400137 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400138 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
139 <div>
140 <CallInterfacePrimaryButtons />
141 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400142 </Grid>
simon33c06182022-11-02 17:39:31 -0400143 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
144 <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400145 </Grid>
146 </Grid>
simon9f814a32022-11-22 21:40:53 -0500147 </Box>
simonf9d78f22022-11-25 15:47:15 -0500148 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400149 );
simon1170c322022-10-31 14:51:31 -0400150};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400151
Gabriel Rochone382a302022-11-23 12:37:04 -0500152const formatElapsedSeconds = (elapsedSeconds: number): string => {
153 const seconds = Math.floor(elapsedSeconds % 60);
154 elapsedSeconds = Math.floor(elapsedSeconds / 60);
155 const minutes = elapsedSeconds % 60;
156 elapsedSeconds = Math.floor(elapsedSeconds / 60);
157 const hours = elapsedSeconds % 24;
158
159 const times: string[] = [];
160 if (hours > 0) {
161 times.push(hours.toString().padStart(2, '0'));
162 }
163 times.push(minutes.toString().padStart(2, '0'));
164 times.push(seconds.toString().padStart(2, '0'));
165
166 return times.join(':');
167};
168
simon8b496b02022-11-29 01:46:46 -0500169const CallInterfaceInformation = () => {
170 const { callStartTime } = useContext(CallContext);
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500171 const { conversation } = useContext(ConversationContext);
simon8b496b02022-11-29 01:46:46 -0500172 const [elapsedTime, setElapsedTime] = useState<number>(0);
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500173 const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]);
simon8b496b02022-11-29 01:46:46 -0500174
175 useEffect(() => {
176 if (callStartTime) {
177 const interval = setInterval(() => {
178 setElapsedTime((new Date().getTime() - callStartTime.getTime()) / 1000);
179 }, 1000);
180 return () => clearInterval(interval);
181 }
182 }, [callStartTime]);
183
Gabriel Rochone382a302022-11-23 12:37:04 -0500184 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">
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500189 {memberName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400190 </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};