blob: 571ac535a436bca7cab876abb35b537fd346ab83 [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 = () => {
Gabriel Rochone382a302022-11-23 12:37:04 -050093 const { isVideoOn, localStream, remoteStream, callStartTime } = 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
Gabriel Rochone382a302022-11-23 12:37:04 -050098 const [elapsedTime, setElapsedTime] = useState<number>();
99
simonf929a362022-11-18 16:53:45 -0500100 useEffect(() => {
101 if (localStream && localVideoRef.current) {
102 localVideoRef.current.srcObject = localStream;
103 }
104 }, [localStream]);
105
106 useEffect(() => {
107 if (remoteStream && remoteVideoRef.current) {
108 remoteVideoRef.current.srcObject = remoteStream;
109 }
110 }, [remoteStream]);
simonce2c0c42022-11-02 17:39:31 -0400111
Gabriel Rochone382a302022-11-23 12:37:04 -0500112 useEffect(() => {
113 if (callStartTime) {
114 const interval = setInterval(() => {
115 setElapsedTime((new Date().getTime() - callStartTime.getTime()) / 1000);
116 }, 1000);
117 return () => clearInterval(interval);
118 }
119 }, [callStartTime]);
120
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400121 return (
simonf9d78f22022-11-25 15:47:15 -0500122 <Box display="flex" flexGrow={1}>
simonce2c0c42022-11-02 17:39:31 -0400123 <video
124 ref={remoteVideoRef}
125 autoPlay
simon9f814a32022-11-22 21:40:53 -0500126 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400127 />
simon9f814a32022-11-22 21:40:53 -0500128 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
129 {/* Guest video, takes the whole screen */}
Gabriel Rochone382a302022-11-23 12:37:04 -0500130 <CallInterfaceInformation elapsedTime={elapsedTime} />
simon9f814a32022-11-22 21:40:53 -0500131 <Box flexGrow={1} marginY={2} position="relative">
simonf929a362022-11-18 16:53:45 -0500132 <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}>
133 <video
134 ref={localVideoRef}
135 autoPlay
136 style={{
137 position: 'absolute',
138 right: 0,
simonf929a362022-11-18 16:53:45 -0500139 borderRadius: '12px',
simonf929a362022-11-18 16:53:45 -0500140 maxHeight: '50%',
simon9f814a32022-11-22 21:40:53 -0500141 maxWidth: '50%',
simonf929a362022-11-18 16:53:45 -0500142 visibility: isVideoOn ? 'visible' : 'hidden',
143 }}
144 />
145 </Draggable>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400146 </Box>
simon33c06182022-11-02 17:39:31 -0400147 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400148 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400149 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
150 <div>
151 <CallInterfacePrimaryButtons />
152 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400153 </Grid>
simon33c06182022-11-02 17:39:31 -0400154 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
155 <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400156 </Grid>
157 </Grid>
simon9f814a32022-11-22 21:40:53 -0500158 </Box>
simonf9d78f22022-11-25 15:47:15 -0500159 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400160 );
simon1170c322022-10-31 14:51:31 -0400161};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400162
Gabriel Rochone382a302022-11-23 12:37:04 -0500163const formatElapsedSeconds = (elapsedSeconds: number): string => {
164 const seconds = Math.floor(elapsedSeconds % 60);
165 elapsedSeconds = Math.floor(elapsedSeconds / 60);
166 const minutes = elapsedSeconds % 60;
167 elapsedSeconds = Math.floor(elapsedSeconds / 60);
168 const hours = elapsedSeconds % 24;
169
170 const times: string[] = [];
171 if (hours > 0) {
172 times.push(hours.toString().padStart(2, '0'));
173 }
174 times.push(minutes.toString().padStart(2, '0'));
175 times.push(seconds.toString().padStart(2, '0'));
176
177 return times.join(':');
178};
179
180interface CallInterfaceInformationProps {
181 elapsedTime?: number;
182}
183
184const CallInterfaceInformation = ({ elapsedTime = 0 }: CallInterfaceInformationProps) => {
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500185 const { conversation } = useContext(ConversationContext);
186 const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]);
Gabriel Rochone382a302022-11-23 12:37:04 -0500187 const elapsedTimerString = formatElapsedSeconds(elapsedTime);
188
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400189 return (
190 <Stack direction="row" justifyContent="space-between" alignItems="center">
191 <Typography color="white" component="p">
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500192 {memberName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400193 </Typography>
194 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500195 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400196 </Typography>
197 </Stack>
198 );
199};
200
201const CallInterfacePrimaryButtons = () => {
202 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500203 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
204 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400205 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500206 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400207 <CallingVideoCameraButton />
208 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400209 </Card>
210 );
211};
212
simon33c06182022-11-02 17:39:31 -0400213const SECONDARY_BUTTONS = [
214 CallingVolumeButton,
215 CallingGroupButton,
216 CallingChatButton,
217 CallingScreenShareButton,
218 CallingRecordButton,
219 CallingExtensionButton,
220 CallingFullScreenButton,
221];
222
223const CallInterfaceSecondaryButtons = (props: Props & { gridItemRef: React.RefObject<HTMLElement> }) => {
224 const stackRef = useRef<HTMLElement>(null);
225
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500226 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400227 const [hiddenStackCount, setHiddenStackCount] = useState(0);
228 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
229
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500230 const calculateStackCount = useCallback(() => {
231 if (stackRef?.current && props.gridItemRef?.current) {
232 const buttonWidth = stackRef.current.children[0].clientWidth;
233 const availableSpace = props.gridItemRef.current.clientWidth;
234 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
235 if (availableButtons < SECONDARY_BUTTONS.length) {
236 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400237 }
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500238 setHiddenStackCount(SECONDARY_BUTTONS.length - availableButtons);
239 }
240 }, [props.gridItemRef]);
241
242 useLayoutEffect(() => {
243 // Run once, at the beginning, for initial measurements
244 if (!initialMeasurementDone) {
245 calculateStackCount();
246 setInitialMeasurementDone(true);
247 }
248
249 const onResize = () => {
250 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400251 };
252 window.addEventListener('resize', onResize);
253 return () => {
254 window.removeEventListener('resize', onResize);
255 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500256 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400257
258 const { displayedButtons, hiddenButtons } = useMemo(() => {
259 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
260 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
261 SECONDARY_BUTTONS.forEach((button, i) => {
262 if (i < SECONDARY_BUTTONS.length - hiddenStackCount) {
263 displayedButtons.push(button);
264 } else {
265 hiddenButtons.push(button);
266 }
267 });
268
269 return {
270 displayedButtons,
271 hiddenButtons,
272 };
273 }, [hiddenStackCount]);
274
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400275 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500276 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
277 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
278 {initialMeasurementDone &&
279 displayedButtons.map((SecondaryButton, i) => (
280 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500281 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500282 </Fragment>
283 ))}
284 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500285 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500286 )}
287 </Stack>
288
289 {!!hiddenButtons.length && hiddenMenuVisible && (
290 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
291 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400292 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500293 direction="column"
294 justifyContent="flex-end"
295 alignItems="flex-end"
296 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400297 >
298 {hiddenButtons.map((SecondaryButton, i) => (
299 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500300 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400301 </Fragment>
302 ))}
303 </Stack>
304 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500305 </Box>
306 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400307 </Card>
308 );
309};