blob: 289fcabdb201bcad9429f1f9f2d7430f4040d3ec [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';
idillon9e542ca2022-12-15 17:54:07 -050019import dayjs from 'dayjs';
20import { Duration } from 'dayjs/plugin/duration';
Gabriel Rochon8321a0d2022-11-06 23:18:36 -050021import {
22 ComponentType,
23 Fragment,
24 ReactNode,
simon492e8402022-11-29 16:48:37 -050025 RefObject,
Gabriel Rochon8321a0d2022-11-06 23:18:36 -050026 useCallback,
simonf929a362022-11-18 16:53:45 -050027 useEffect,
Gabriel Rochon8321a0d2022-11-06 23:18:36 -050028 useLayoutEffect,
29 useMemo,
30 useRef,
31 useState,
32} from 'react';
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';
simondaae9102022-12-02 16:51:31 -050049import VideoOverlay from '../components/VideoOverlay';
50import VideoStream from '../components/VideoStream';
idillon255682e2023-02-06 13:25:26 -050051import { useCallManagerContext } from '../contexts/CallManagerProvider';
simon09fe4822022-11-30 23:36:25 -050052import { useConversationContext } from '../contexts/ConversationProvider';
idillon27dab022023-02-02 17:55:47 -050053import { useUserMediaContext } from '../contexts/UserMediaProvider';
idillonc45a43d2023-02-10 18:12:10 -050054import { CallStatus, VideoStatus } from '../services/CallManager';
idillon9e542ca2022-12-15 17:54:07 -050055import { formatCallDuration } from '../utils/dates&times';
simon571240f2022-11-29 23:59:27 -050056import { VideoElementWithSinkId } from '../utils/utils';
simonf929a362022-11-18 16:53:45 -050057import { CallPending } from './CallPending';
simone35acc22022-12-02 16:51:12 -050058import CallPermissionDenied from './CallPermissionDenied';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040059
simon1170c322022-10-31 14:51:31 -040060export default () => {
idillon255682e2023-02-06 13:25:26 -050061 const { callStatus, isChatShown, isFullscreen } = useCallManagerContext();
simon2a5cf142022-11-25 15:47:35 -050062 const callInterfaceRef = useRef<HTMLDivElement>();
63
64 useEffect(() => {
65 if (!callInterfaceRef.current) {
66 return;
67 }
68
69 if (isFullscreen && document.fullscreenElement === null) {
70 callInterfaceRef.current.requestFullscreen();
simon09fe4822022-11-30 23:36:25 -050071 } else if (!isFullscreen && document.fullscreenElement !== null) {
simon2a5cf142022-11-25 15:47:35 -050072 document.exitFullscreen();
73 }
74 }, [isFullscreen]);
simonf929a362022-11-18 16:53:45 -050075
simone35acc22022-12-02 16:51:12 -050076 if (callStatus === CallStatus.PermissionsDenied) {
77 return <CallPermissionDenied />;
78 }
simon9f814a32022-11-22 21:40:53 -050079 if (callStatus !== CallStatus.InCall) {
simon9076a9a2022-11-29 17:13:01 -050080 return <CallPending />;
simonf929a362022-11-18 16:53:45 -050081 }
simon9f814a32022-11-22 21:40:53 -050082
simonf9d78f22022-11-25 15:47:15 -050083 return (
simon2a5cf142022-11-25 15:47:35 -050084 <Box ref={callInterfaceRef} flexGrow={1} display="flex">
simonf9d78f22022-11-25 15:47:15 -050085 <CallInterface />
86 {isChatShown && <CallChatDrawer />}
87 </Box>
88 );
simon1170c322022-10-31 14:51:31 -040089};
90
simon33c06182022-11-02 17:39:31 -040091interface Props {
92 children?: ReactNode;
93}
94
simon1170c322022-10-31 14:51:31 -040095const CallInterface = () => {
simon492e8402022-11-29 16:48:37 -050096 const {
idillon27dab022023-02-02 17:55:47 -050097 localStream,
98 screenShareLocalStream,
simon492e8402022-11-29 16:48:37 -050099 currentMediaDeviceIds: {
100 audiooutput: { id: audioOutDeviceId },
101 },
idillon27dab022023-02-02 17:55:47 -0500102 } = useUserMediaContext();
idillon255682e2023-02-06 13:25:26 -0500103 const { remoteStreams, videoStatus } = useCallManagerContext();
simon571240f2022-11-29 23:59:27 -0500104 const remoteVideoRef = useRef<VideoElementWithSinkId | null>(null);
simon492e8402022-11-29 16:48:37 -0500105 const gridItemRef = useRef<HTMLDivElement | null>(null);
simondaae9102022-12-02 16:51:31 -0500106 const [isLocalVideoZoomed, setIsLocalVideoZoomed] = useState(false);
simonf929a362022-11-18 16:53:45 -0500107
simondaae9102022-12-02 16:51:31 -0500108 const stream = useMemo(() => {
109 switch (videoStatus) {
110 case VideoStatus.Camera:
111 return localStream;
112 case VideoStatus.ScreenShare:
113 return screenShareLocalStream;
simonf929a362022-11-18 16:53:45 -0500114 }
simondaae9102022-12-02 16:51:31 -0500115 }, [videoStatus, localStream, screenShareLocalStream]);
simonce2c0c42022-11-02 17:39:31 -0400116
simon571240f2022-11-29 23:59:27 -0500117 const hasSetSinkId = remoteVideoRef.current?.setSinkId != null;
118
simondaae9102022-12-02 16:51:31 -0500119 // TODO: For now, `remoteStream` is the first remote stream in the array.
120 // There should only be one in the array, but we should make sure this is right.
121 const remoteStream = remoteStreams?.at(0);
122
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400123 return (
simonf9d78f22022-11-25 15:47:15 -0500124 <Box display="flex" flexGrow={1}>
simondaae9102022-12-02 16:51:31 -0500125 <VideoStream
simonce2c0c42022-11-02 17:39:31 -0400126 ref={remoteVideoRef}
simondaae9102022-12-02 16:51:31 -0500127 stream={remoteStream}
128 audioOutDeviceId={audioOutDeviceId}
simon9f814a32022-11-22 21:40:53 -0500129 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400130 />
simon9f814a32022-11-22 21:40:53 -0500131 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
132 {/* Guest video, takes the whole screen */}
simon8b496b02022-11-29 01:46:46 -0500133 <CallInterfaceInformation />
simon9f814a32022-11-22 21:40:53 -0500134 <Box flexGrow={1} marginY={2} position="relative">
simondaae9102022-12-02 16:51:31 -0500135 <VideoOverlay
136 stream={stream}
137 hidden={!stream}
138 muted
139 size={isLocalVideoZoomed ? 'large' : 'medium'}
140 onClick={() => setIsLocalVideoZoomed((v) => !v)}
141 />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400142 </Box>
simon33c06182022-11-02 17:39:31 -0400143 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400144 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400145 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
146 <div>
147 <CallInterfacePrimaryButtons />
148 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400149 </Grid>
simon33c06182022-11-02 17:39:31 -0400150 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
simon571240f2022-11-29 23:59:27 -0500151 <CallInterfaceSecondaryButtons showVolumeButton={hasSetSinkId} gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400152 </Grid>
153 </Grid>
simon9f814a32022-11-22 21:40:53 -0500154 </Box>
simonf9d78f22022-11-25 15:47:15 -0500155 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400156 );
simon1170c322022-10-31 14:51:31 -0400157};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400158
simon8b496b02022-11-29 01:46:46 -0500159const CallInterfaceInformation = () => {
idillon255682e2023-02-06 13:25:26 -0500160 const { callStartTime } = useCallManagerContext();
idillon07d31cc2022-12-06 22:40:14 -0500161 const { conversationDisplayName } = useConversationContext();
idillon9e542ca2022-12-15 17:54:07 -0500162 const [elapsedTime, setElapsedTime] = useState<Duration>(
163 dayjs.duration(callStartTime ? Date.now() - callStartTime : 0)
164 );
simon8b496b02022-11-29 01:46:46 -0500165
166 useEffect(() => {
167 if (callStartTime) {
168 const interval = setInterval(() => {
idillon9e542ca2022-12-15 17:54:07 -0500169 setElapsedTime(dayjs.duration(Date.now() - callStartTime));
simon8b496b02022-11-29 01:46:46 -0500170 }, 1000);
171 return () => clearInterval(interval);
172 }
173 }, [callStartTime]);
174
idillon9e542ca2022-12-15 17:54:07 -0500175 const elapsedTimerString = formatCallDuration(elapsedTime);
Gabriel Rochone382a302022-11-23 12:37:04 -0500176
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400177 return (
178 <Stack direction="row" justifyContent="space-between" alignItems="center">
179 <Typography color="white" component="p">
idillon07d31cc2022-12-06 22:40:14 -0500180 {conversationDisplayName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400181 </Typography>
182 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500183 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400184 </Typography>
185 </Stack>
186 );
187};
188
189const CallInterfacePrimaryButtons = () => {
190 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500191 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
192 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400193 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500194 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400195 <CallingVideoCameraButton />
196 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400197 </Card>
198 );
199};
200
simon33c06182022-11-02 17:39:31 -0400201const SECONDARY_BUTTONS = [
202 CallingVolumeButton,
203 CallingGroupButton,
204 CallingChatButton,
205 CallingScreenShareButton,
206 CallingRecordButton,
207 CallingExtensionButton,
208 CallingFullScreenButton,
209];
210
simon571240f2022-11-29 23:59:27 -0500211const CallInterfaceSecondaryButtons = ({
212 gridItemRef,
213 showVolumeButton,
214}: Props & { showVolumeButton: boolean; gridItemRef: RefObject<HTMLElement> }) => {
simon33c06182022-11-02 17:39:31 -0400215 const stackRef = useRef<HTMLElement>(null);
216
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500217 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400218 const [hiddenStackCount, setHiddenStackCount] = useState(0);
219 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
220
simon571240f2022-11-29 23:59:27 -0500221 // Audio out options are only available on Chrome and other browsers that support `setSinkId`.
222 // This removes the `CallingVolumeButton` if `setSinkId` is not defined.
223 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
224 const secondaryButtons = useMemo(() => {
225 return showVolumeButton ? SECONDARY_BUTTONS : SECONDARY_BUTTONS.slice(1);
226 }, [showVolumeButton]);
227
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500228 const calculateStackCount = useCallback(() => {
simon571240f2022-11-29 23:59:27 -0500229 if (stackRef?.current && gridItemRef?.current) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500230 const buttonWidth = stackRef.current.children[0].clientWidth;
simon571240f2022-11-29 23:59:27 -0500231 const availableSpace = gridItemRef.current.clientWidth;
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500232 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
simon571240f2022-11-29 23:59:27 -0500233 if (availableButtons < secondaryButtons.length) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500234 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400235 }
simon571240f2022-11-29 23:59:27 -0500236 setHiddenStackCount(secondaryButtons.length - availableButtons);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500237 }
simon571240f2022-11-29 23:59:27 -0500238 }, [gridItemRef, secondaryButtons]);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500239
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>[] = [];
simon571240f2022-11-29 23:59:27 -0500259 for (let i = 0; i < secondaryButtons.length; i++) {
260 const button = secondaryButtons[i];
261 if (i < secondaryButtons.length - hiddenStackCount) {
simon33c06182022-11-02 17:39:31 -0400262 displayedButtons.push(button);
263 } else {
264 hiddenButtons.push(button);
265 }
simon571240f2022-11-29 23:59:27 -0500266 }
simon33c06182022-11-02 17:39:31 -0400267
268 return {
269 displayedButtons,
270 hiddenButtons,
271 };
simon571240f2022-11-29 23:59:27 -0500272 }, [hiddenStackCount, secondaryButtons]);
simon33c06182022-11-02 17:39:31 -0400273
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400274 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500275 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
276 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
277 {initialMeasurementDone &&
278 displayedButtons.map((SecondaryButton, i) => (
279 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500280 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500281 </Fragment>
282 ))}
283 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500284 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500285 )}
286 </Stack>
287
288 {!!hiddenButtons.length && hiddenMenuVisible && (
289 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
290 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400291 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500292 direction="column"
293 justifyContent="flex-end"
294 alignItems="flex-end"
295 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400296 >
297 {hiddenButtons.map((SecondaryButton, i) => (
298 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500299 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400300 </Fragment>
301 ))}
302 </Stack>
303 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500304 </Box>
305 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400306 </Card>
307 );
308};