blob: beef80358e2891c5b0e41355948dc974f7c85e24 [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';
Gabriel Rochon15a5fb22022-11-27 19:25:14 -050049import { ConversationContext } from '../contexts/ConversationProvider';
simon9076a9a2022-11-29 17:13:01 -050050import { WebRtcContext } from '../contexts/WebRtcProvider';
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 () => {
simon9076a9a2022-11-29 17:13:01 -050054 const { callStatus, isChatShown, isFullscreen } = useContext(CallContext);
simon2a5cf142022-11-25 15:47:35 -050055 const callInterfaceRef = useRef<HTMLDivElement>();
56
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) {
simon9076a9a2022-11-29 17:13:01 -050070 return <CallPending />;
simonf929a362022-11-18 16:53:45 -050071 }
simon9f814a32022-11-22 21:40:53 -050072
simonf9d78f22022-11-25 15:47:15 -050073 return (
simon2a5cf142022-11-25 15:47:35 -050074 <Box ref={callInterfaceRef} flexGrow={1} display="flex">
simonf9d78f22022-11-25 15:47:15 -050075 <CallInterface />
76 {isChatShown && <CallChatDrawer />}
77 </Box>
78 );
simon1170c322022-10-31 14:51:31 -040079};
80
simon33c06182022-11-02 17:39:31 -040081interface Props {
82 children?: ReactNode;
83}
84
simon1170c322022-10-31 14:51:31 -040085const CallInterface = () => {
simon9076a9a2022-11-29 17:13:01 -050086 const { localStream, remoteStreams } = useContext(WebRtcContext);
87 const { isVideoOn } = useContext(CallContext);
simon33c06182022-11-02 17:39:31 -040088 const gridItemRef = useRef(null);
simonf929a362022-11-18 16:53:45 -050089 const remoteVideoRef = useRef<HTMLVideoElement | null>(null);
90 const localVideoRef = useRef<HTMLVideoElement | null>(null);
91
92 useEffect(() => {
93 if (localStream && localVideoRef.current) {
94 localVideoRef.current.srcObject = localStream;
95 }
96 }, [localStream]);
97
98 useEffect(() => {
simon9076a9a2022-11-29 17:13:01 -050099 // TODO: For now, `remoteStream` is the first remote stream in the array.
100 // There should only be one in the array, but we should make sure this is right.
101 const remoteStream = remoteStreams?.at(0);
simonf929a362022-11-18 16:53:45 -0500102 if (remoteStream && remoteVideoRef.current) {
103 remoteVideoRef.current.srcObject = remoteStream;
104 }
simon9076a9a2022-11-29 17:13:01 -0500105 }, [remoteStreams]);
simonce2c0c42022-11-02 17:39:31 -0400106
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400107 return (
simonf9d78f22022-11-25 15:47:15 -0500108 <Box display="flex" flexGrow={1}>
simonce2c0c42022-11-02 17:39:31 -0400109 <video
110 ref={remoteVideoRef}
111 autoPlay
simon9f814a32022-11-22 21:40:53 -0500112 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400113 />
simon9f814a32022-11-22 21:40:53 -0500114 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
115 {/* Guest video, takes the whole screen */}
simon8b496b02022-11-29 01:46:46 -0500116 <CallInterfaceInformation />
simon9f814a32022-11-22 21:40:53 -0500117 <Box flexGrow={1} marginY={2} position="relative">
simonf929a362022-11-18 16:53:45 -0500118 <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}>
119 <video
120 ref={localVideoRef}
121 autoPlay
Issam E. Maghni89615932022-11-29 19:05:28 +0000122 muted
simonf929a362022-11-18 16:53:45 -0500123 style={{
124 position: 'absolute',
125 right: 0,
simonf929a362022-11-18 16:53:45 -0500126 borderRadius: '12px',
simonf929a362022-11-18 16:53:45 -0500127 maxHeight: '50%',
simon9f814a32022-11-22 21:40:53 -0500128 maxWidth: '50%',
simonf929a362022-11-18 16:53:45 -0500129 visibility: isVideoOn ? 'visible' : 'hidden',
130 }}
131 />
132 </Draggable>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400133 </Box>
simon33c06182022-11-02 17:39:31 -0400134 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400135 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400136 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
137 <div>
138 <CallInterfacePrimaryButtons />
139 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400140 </Grid>
simon33c06182022-11-02 17:39:31 -0400141 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
142 <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400143 </Grid>
144 </Grid>
simon9f814a32022-11-22 21:40:53 -0500145 </Box>
simonf9d78f22022-11-25 15:47:15 -0500146 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400147 );
simon1170c322022-10-31 14:51:31 -0400148};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400149
Gabriel Rochone382a302022-11-23 12:37:04 -0500150const formatElapsedSeconds = (elapsedSeconds: number): string => {
151 const seconds = Math.floor(elapsedSeconds % 60);
152 elapsedSeconds = Math.floor(elapsedSeconds / 60);
153 const minutes = elapsedSeconds % 60;
154 elapsedSeconds = Math.floor(elapsedSeconds / 60);
155 const hours = elapsedSeconds % 24;
156
157 const times: string[] = [];
158 if (hours > 0) {
159 times.push(hours.toString().padStart(2, '0'));
160 }
161 times.push(minutes.toString().padStart(2, '0'));
162 times.push(seconds.toString().padStart(2, '0'));
163
164 return times.join(':');
165};
166
simon8b496b02022-11-29 01:46:46 -0500167const CallInterfaceInformation = () => {
168 const { callStartTime } = useContext(CallContext);
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500169 const { conversation } = useContext(ConversationContext);
simon8b496b02022-11-29 01:46:46 -0500170 const [elapsedTime, setElapsedTime] = useState<number>(0);
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500171 const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]);
simon8b496b02022-11-29 01:46:46 -0500172
173 useEffect(() => {
174 if (callStartTime) {
175 const interval = setInterval(() => {
176 setElapsedTime((new Date().getTime() - callStartTime.getTime()) / 1000);
177 }, 1000);
178 return () => clearInterval(interval);
179 }
180 }, [callStartTime]);
181
Gabriel Rochone382a302022-11-23 12:37:04 -0500182 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">
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500187 {memberName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400188 </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};