blob: b535b25a606246ee7313f2f49204b8d97d3e40fe [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,
simon492e8402022-11-29 16:48:37 -050023 RefObject,
Gabriel Rochon8321a0d2022-11-06 23:18:36 -050024 useCallback,
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';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040031
simon33c06182022-11-02 17:39:31 -040032import { ExpandableButtonProps } from '../components/Button';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040033import {
34 CallingChatButton,
35 CallingEndButton,
36 CallingExtensionButton,
simon33c06182022-11-02 17:39:31 -040037 CallingFullScreenButton,
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040038 CallingGroupButton,
39 CallingMicButton,
simon33c06182022-11-02 17:39:31 -040040 CallingMoreVerticalButton,
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040041 CallingRecordButton,
42 CallingScreenShareButton,
43 CallingVideoCameraButton,
44 CallingVolumeButton,
simon1170c322022-10-31 14:51:31 -040045} from '../components/CallButtons';
simonf9d78f22022-11-25 15:47:15 -050046import CallChatDrawer from '../components/CallChatDrawer';
simondaae9102022-12-02 16:51:31 -050047import VideoOverlay from '../components/VideoOverlay';
48import VideoStream from '../components/VideoStream';
simon5c677962022-12-02 16:51:54 -050049import { CallStatus, useCallContext, VideoStatus } from '../contexts/CallProvider';
simon09fe4822022-11-30 23:36:25 -050050import { useConversationContext } from '../contexts/ConversationProvider';
simon5c677962022-12-02 16:51:54 -050051import { useWebRtcContext } from '../contexts/WebRtcProvider';
simon571240f2022-11-29 23:59:27 -050052import { VideoElementWithSinkId } from '../utils/utils';
simonf929a362022-11-18 16:53:45 -050053import { CallPending } from './CallPending';
simone35acc22022-12-02 16:51:12 -050054import CallPermissionDenied from './CallPermissionDenied';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040055
simon1170c322022-10-31 14:51:31 -040056export default () => {
simon5c677962022-12-02 16:51:54 -050057 const { callStatus, isChatShown, isFullscreen } = useCallContext();
simon2a5cf142022-11-25 15:47:35 -050058 const callInterfaceRef = useRef<HTMLDivElement>();
59
60 useEffect(() => {
61 if (!callInterfaceRef.current) {
62 return;
63 }
64
65 if (isFullscreen && document.fullscreenElement === null) {
66 callInterfaceRef.current.requestFullscreen();
simon09fe4822022-11-30 23:36:25 -050067 } else if (!isFullscreen && document.fullscreenElement !== null) {
simon2a5cf142022-11-25 15:47:35 -050068 document.exitFullscreen();
69 }
70 }, [isFullscreen]);
simonf929a362022-11-18 16:53:45 -050071
simone35acc22022-12-02 16:51:12 -050072 if (callStatus === CallStatus.PermissionsDenied) {
73 return <CallPermissionDenied />;
74 }
simon9f814a32022-11-22 21:40:53 -050075 if (callStatus !== CallStatus.InCall) {
simon9076a9a2022-11-29 17:13:01 -050076 return <CallPending />;
simonf929a362022-11-18 16:53:45 -050077 }
simon9f814a32022-11-22 21:40:53 -050078
simonf9d78f22022-11-25 15:47:15 -050079 return (
simon2a5cf142022-11-25 15:47:35 -050080 <Box ref={callInterfaceRef} flexGrow={1} display="flex">
simonf9d78f22022-11-25 15:47:15 -050081 <CallInterface />
82 {isChatShown && <CallChatDrawer />}
83 </Box>
84 );
simon1170c322022-10-31 14:51:31 -040085};
86
simon33c06182022-11-02 17:39:31 -040087interface Props {
88 children?: ReactNode;
89}
90
simon1170c322022-10-31 14:51:31 -040091const CallInterface = () => {
simon5c677962022-12-02 16:51:54 -050092 const { localStream, screenShareLocalStream, remoteStreams } = useWebRtcContext();
simon492e8402022-11-29 16:48:37 -050093 const {
simon492e8402022-11-29 16:48:37 -050094 currentMediaDeviceIds: {
95 audiooutput: { id: audioOutDeviceId },
96 },
simondaae9102022-12-02 16:51:31 -050097 videoStatus,
simon5c677962022-12-02 16:51:54 -050098 } = useCallContext();
simon571240f2022-11-29 23:59:27 -050099 const remoteVideoRef = useRef<VideoElementWithSinkId | null>(null);
simon492e8402022-11-29 16:48:37 -0500100 const gridItemRef = useRef<HTMLDivElement | null>(null);
simondaae9102022-12-02 16:51:31 -0500101 const [isLocalVideoZoomed, setIsLocalVideoZoomed] = useState(false);
simonf929a362022-11-18 16:53:45 -0500102
simondaae9102022-12-02 16:51:31 -0500103 const stream = useMemo(() => {
104 switch (videoStatus) {
105 case VideoStatus.Camera:
106 return localStream;
107 case VideoStatus.ScreenShare:
108 return screenShareLocalStream;
simonf929a362022-11-18 16:53:45 -0500109 }
simondaae9102022-12-02 16:51:31 -0500110 }, [videoStatus, localStream, screenShareLocalStream]);
simonce2c0c42022-11-02 17:39:31 -0400111
simon571240f2022-11-29 23:59:27 -0500112 const hasSetSinkId = remoteVideoRef.current?.setSinkId != null;
113
simondaae9102022-12-02 16:51:31 -0500114 // TODO: For now, `remoteStream` is the first remote stream in the array.
115 // There should only be one in the array, but we should make sure this is right.
116 const remoteStream = remoteStreams?.at(0);
117
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400118 return (
simonf9d78f22022-11-25 15:47:15 -0500119 <Box display="flex" flexGrow={1}>
simondaae9102022-12-02 16:51:31 -0500120 <VideoStream
simonce2c0c42022-11-02 17:39:31 -0400121 ref={remoteVideoRef}
simondaae9102022-12-02 16:51:31 -0500122 stream={remoteStream}
123 audioOutDeviceId={audioOutDeviceId}
simon9f814a32022-11-22 21:40:53 -0500124 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400125 />
simon9f814a32022-11-22 21:40:53 -0500126 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
127 {/* Guest video, takes the whole screen */}
simon8b496b02022-11-29 01:46:46 -0500128 <CallInterfaceInformation />
simon9f814a32022-11-22 21:40:53 -0500129 <Box flexGrow={1} marginY={2} position="relative">
simondaae9102022-12-02 16:51:31 -0500130 <VideoOverlay
131 stream={stream}
132 hidden={!stream}
133 muted
134 size={isLocalVideoZoomed ? 'large' : 'medium'}
135 onClick={() => setIsLocalVideoZoomed((v) => !v)}
136 />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400137 </Box>
simon33c06182022-11-02 17:39:31 -0400138 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400139 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400140 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
141 <div>
142 <CallInterfacePrimaryButtons />
143 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400144 </Grid>
simon33c06182022-11-02 17:39:31 -0400145 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
simon571240f2022-11-29 23:59:27 -0500146 <CallInterfaceSecondaryButtons showVolumeButton={hasSetSinkId} gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400147 </Grid>
148 </Grid>
simon9f814a32022-11-22 21:40:53 -0500149 </Box>
simonf9d78f22022-11-25 15:47:15 -0500150 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400151 );
simon1170c322022-10-31 14:51:31 -0400152};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400153
Gabriel Rochone382a302022-11-23 12:37:04 -0500154const formatElapsedSeconds = (elapsedSeconds: number): string => {
155 const seconds = Math.floor(elapsedSeconds % 60);
156 elapsedSeconds = Math.floor(elapsedSeconds / 60);
157 const minutes = elapsedSeconds % 60;
158 elapsedSeconds = Math.floor(elapsedSeconds / 60);
159 const hours = elapsedSeconds % 24;
160
161 const times: string[] = [];
162 if (hours > 0) {
163 times.push(hours.toString().padStart(2, '0'));
164 }
165 times.push(minutes.toString().padStart(2, '0'));
166 times.push(seconds.toString().padStart(2, '0'));
167
168 return times.join(':');
169};
170
simon8b496b02022-11-29 01:46:46 -0500171const CallInterfaceInformation = () => {
simon5c677962022-12-02 16:51:54 -0500172 const { callStartTime } = useCallContext();
idillon07d31cc2022-12-06 22:40:14 -0500173 const { conversationDisplayName } = useConversationContext();
simone35acc22022-12-02 16:51:12 -0500174 const [elapsedTime, setElapsedTime] = useState(callStartTime ? (Date.now() - callStartTime) / 1000 : 0);
simon8b496b02022-11-29 01:46:46 -0500175
176 useEffect(() => {
177 if (callStartTime) {
178 const interval = setInterval(() => {
Misha Krieger-Raynauldd0cc3e32022-11-29 19:59:31 -0500179 setElapsedTime((Date.now() - callStartTime) / 1000);
simon8b496b02022-11-29 01:46:46 -0500180 }, 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">
idillon07d31cc2022-12-06 22:40:14 -0500190 {conversationDisplayName}
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
simon571240f2022-11-29 23:59:27 -0500221const CallInterfaceSecondaryButtons = ({
222 gridItemRef,
223 showVolumeButton,
224}: Props & { showVolumeButton: boolean; gridItemRef: RefObject<HTMLElement> }) => {
simon33c06182022-11-02 17:39:31 -0400225 const stackRef = useRef<HTMLElement>(null);
226
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500227 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400228 const [hiddenStackCount, setHiddenStackCount] = useState(0);
229 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
230
simon571240f2022-11-29 23:59:27 -0500231 // Audio out options are only available on Chrome and other browsers that support `setSinkId`.
232 // This removes the `CallingVolumeButton` if `setSinkId` is not defined.
233 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
234 const secondaryButtons = useMemo(() => {
235 return showVolumeButton ? SECONDARY_BUTTONS : SECONDARY_BUTTONS.slice(1);
236 }, [showVolumeButton]);
237
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500238 const calculateStackCount = useCallback(() => {
simon571240f2022-11-29 23:59:27 -0500239 if (stackRef?.current && gridItemRef?.current) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500240 const buttonWidth = stackRef.current.children[0].clientWidth;
simon571240f2022-11-29 23:59:27 -0500241 const availableSpace = gridItemRef.current.clientWidth;
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500242 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
simon571240f2022-11-29 23:59:27 -0500243 if (availableButtons < secondaryButtons.length) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500244 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400245 }
simon571240f2022-11-29 23:59:27 -0500246 setHiddenStackCount(secondaryButtons.length - availableButtons);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500247 }
simon571240f2022-11-29 23:59:27 -0500248 }, [gridItemRef, secondaryButtons]);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500249
250 useLayoutEffect(() => {
251 // Run once, at the beginning, for initial measurements
252 if (!initialMeasurementDone) {
253 calculateStackCount();
254 setInitialMeasurementDone(true);
255 }
256
257 const onResize = () => {
258 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400259 };
260 window.addEventListener('resize', onResize);
261 return () => {
262 window.removeEventListener('resize', onResize);
263 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500264 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400265
266 const { displayedButtons, hiddenButtons } = useMemo(() => {
267 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
268 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
simon571240f2022-11-29 23:59:27 -0500269 for (let i = 0; i < secondaryButtons.length; i++) {
270 const button = secondaryButtons[i];
271 if (i < secondaryButtons.length - hiddenStackCount) {
simon33c06182022-11-02 17:39:31 -0400272 displayedButtons.push(button);
273 } else {
274 hiddenButtons.push(button);
275 }
simon571240f2022-11-29 23:59:27 -0500276 }
simon33c06182022-11-02 17:39:31 -0400277
278 return {
279 displayedButtons,
280 hiddenButtons,
281 };
simon571240f2022-11-29 23:59:27 -0500282 }, [hiddenStackCount, secondaryButtons]);
simon33c06182022-11-02 17:39:31 -0400283
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400284 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500285 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
286 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
287 {initialMeasurementDone &&
288 displayedButtons.map((SecondaryButton, i) => (
289 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500290 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500291 </Fragment>
292 ))}
293 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500294 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500295 )}
296 </Stack>
297
298 {!!hiddenButtons.length && hiddenMenuVisible && (
299 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
300 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400301 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500302 direction="column"
303 justifyContent="flex-end"
304 alignItems="flex-end"
305 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400306 >
307 {hiddenButtons.map((SecondaryButton, i) => (
308 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500309 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400310 </Fragment>
311 ))}
312 </Stack>
313 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500314 </Box>
315 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400316 </Card>
317 );
318};