blob: f66a1806b43d115d6e43b1b11b6fc7cbc3fe2505 [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';
simon5c677962022-12-02 16:51:54 -050051import { CallStatus, useCallContext, VideoStatus } from '../contexts/CallProvider';
simon09fe4822022-11-30 23:36:25 -050052import { useConversationContext } from '../contexts/ConversationProvider';
idillon27dab022023-02-02 17:55:47 -050053import { useUserMediaContext } from '../contexts/UserMediaProvider';
idillon9e542ca2022-12-15 17:54:07 -050054import { formatCallDuration } from '../utils/dates&times';
simon571240f2022-11-29 23:59:27 -050055import { VideoElementWithSinkId } from '../utils/utils';
simonf929a362022-11-18 16:53:45 -050056import { CallPending } from './CallPending';
simone35acc22022-12-02 16:51:12 -050057import CallPermissionDenied from './CallPermissionDenied';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040058
simon1170c322022-10-31 14:51:31 -040059export default () => {
simon5c677962022-12-02 16:51:54 -050060 const { callStatus, isChatShown, isFullscreen } = useCallContext();
simon2a5cf142022-11-25 15:47:35 -050061 const callInterfaceRef = useRef<HTMLDivElement>();
62
63 useEffect(() => {
64 if (!callInterfaceRef.current) {
65 return;
66 }
67
68 if (isFullscreen && document.fullscreenElement === null) {
69 callInterfaceRef.current.requestFullscreen();
simon09fe4822022-11-30 23:36:25 -050070 } else if (!isFullscreen && document.fullscreenElement !== null) {
simon2a5cf142022-11-25 15:47:35 -050071 document.exitFullscreen();
72 }
73 }, [isFullscreen]);
simonf929a362022-11-18 16:53:45 -050074
simone35acc22022-12-02 16:51:12 -050075 if (callStatus === CallStatus.PermissionsDenied) {
76 return <CallPermissionDenied />;
77 }
simon9f814a32022-11-22 21:40:53 -050078 if (callStatus !== CallStatus.InCall) {
simon9076a9a2022-11-29 17:13:01 -050079 return <CallPending />;
simonf929a362022-11-18 16:53:45 -050080 }
simon9f814a32022-11-22 21:40:53 -050081
simonf9d78f22022-11-25 15:47:15 -050082 return (
simon2a5cf142022-11-25 15:47:35 -050083 <Box ref={callInterfaceRef} flexGrow={1} display="flex">
simonf9d78f22022-11-25 15:47:15 -050084 <CallInterface />
85 {isChatShown && <CallChatDrawer />}
86 </Box>
87 );
simon1170c322022-10-31 14:51:31 -040088};
89
simon33c06182022-11-02 17:39:31 -040090interface Props {
91 children?: ReactNode;
92}
93
simon1170c322022-10-31 14:51:31 -040094const CallInterface = () => {
simon492e8402022-11-29 16:48:37 -050095 const {
idillon27dab022023-02-02 17:55:47 -050096 localStream,
97 screenShareLocalStream,
simon492e8402022-11-29 16:48:37 -050098 currentMediaDeviceIds: {
99 audiooutput: { id: audioOutDeviceId },
100 },
idillon27dab022023-02-02 17:55:47 -0500101 } = useUserMediaContext();
102 const { remoteStreams, videoStatus } = useCallContext();
simon571240f2022-11-29 23:59:27 -0500103 const remoteVideoRef = useRef<VideoElementWithSinkId | null>(null);
simon492e8402022-11-29 16:48:37 -0500104 const gridItemRef = useRef<HTMLDivElement | null>(null);
simondaae9102022-12-02 16:51:31 -0500105 const [isLocalVideoZoomed, setIsLocalVideoZoomed] = useState(false);
simonf929a362022-11-18 16:53:45 -0500106
simondaae9102022-12-02 16:51:31 -0500107 const stream = useMemo(() => {
108 switch (videoStatus) {
109 case VideoStatus.Camera:
110 return localStream;
111 case VideoStatus.ScreenShare:
112 return screenShareLocalStream;
simonf929a362022-11-18 16:53:45 -0500113 }
simondaae9102022-12-02 16:51:31 -0500114 }, [videoStatus, localStream, screenShareLocalStream]);
simonce2c0c42022-11-02 17:39:31 -0400115
simon571240f2022-11-29 23:59:27 -0500116 const hasSetSinkId = remoteVideoRef.current?.setSinkId != null;
117
simondaae9102022-12-02 16:51:31 -0500118 // TODO: For now, `remoteStream` is the first remote stream in the array.
119 // There should only be one in the array, but we should make sure this is right.
120 const remoteStream = remoteStreams?.at(0);
121
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400122 return (
simonf9d78f22022-11-25 15:47:15 -0500123 <Box display="flex" flexGrow={1}>
simondaae9102022-12-02 16:51:31 -0500124 <VideoStream
simonce2c0c42022-11-02 17:39:31 -0400125 ref={remoteVideoRef}
simondaae9102022-12-02 16:51:31 -0500126 stream={remoteStream}
127 audioOutDeviceId={audioOutDeviceId}
simon9f814a32022-11-22 21:40:53 -0500128 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400129 />
simon9f814a32022-11-22 21:40:53 -0500130 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
131 {/* Guest video, takes the whole screen */}
simon8b496b02022-11-29 01:46:46 -0500132 <CallInterfaceInformation />
simon9f814a32022-11-22 21:40:53 -0500133 <Box flexGrow={1} marginY={2} position="relative">
simondaae9102022-12-02 16:51:31 -0500134 <VideoOverlay
135 stream={stream}
136 hidden={!stream}
137 muted
138 size={isLocalVideoZoomed ? 'large' : 'medium'}
139 onClick={() => setIsLocalVideoZoomed((v) => !v)}
140 />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400141 </Box>
simon33c06182022-11-02 17:39:31 -0400142 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400143 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400144 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
145 <div>
146 <CallInterfacePrimaryButtons />
147 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400148 </Grid>
simon33c06182022-11-02 17:39:31 -0400149 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
simon571240f2022-11-29 23:59:27 -0500150 <CallInterfaceSecondaryButtons showVolumeButton={hasSetSinkId} gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400151 </Grid>
152 </Grid>
simon9f814a32022-11-22 21:40:53 -0500153 </Box>
simonf9d78f22022-11-25 15:47:15 -0500154 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400155 );
simon1170c322022-10-31 14:51:31 -0400156};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400157
simon8b496b02022-11-29 01:46:46 -0500158const CallInterfaceInformation = () => {
simon5c677962022-12-02 16:51:54 -0500159 const { callStartTime } = useCallContext();
idillon07d31cc2022-12-06 22:40:14 -0500160 const { conversationDisplayName } = useConversationContext();
idillon9e542ca2022-12-15 17:54:07 -0500161 const [elapsedTime, setElapsedTime] = useState<Duration>(
162 dayjs.duration(callStartTime ? Date.now() - callStartTime : 0)
163 );
simon8b496b02022-11-29 01:46:46 -0500164
165 useEffect(() => {
166 if (callStartTime) {
167 const interval = setInterval(() => {
idillon9e542ca2022-12-15 17:54:07 -0500168 setElapsedTime(dayjs.duration(Date.now() - callStartTime));
simon8b496b02022-11-29 01:46:46 -0500169 }, 1000);
170 return () => clearInterval(interval);
171 }
172 }, [callStartTime]);
173
idillon9e542ca2022-12-15 17:54:07 -0500174 const elapsedTimerString = formatCallDuration(elapsedTime);
Gabriel Rochone382a302022-11-23 12:37:04 -0500175
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400176 return (
177 <Stack direction="row" justifyContent="space-between" alignItems="center">
178 <Typography color="white" component="p">
idillon07d31cc2022-12-06 22:40:14 -0500179 {conversationDisplayName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400180 </Typography>
181 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500182 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400183 </Typography>
184 </Stack>
185 );
186};
187
188const CallInterfacePrimaryButtons = () => {
189 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500190 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
191 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400192 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500193 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400194 <CallingVideoCameraButton />
195 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400196 </Card>
197 );
198};
199
simon33c06182022-11-02 17:39:31 -0400200const SECONDARY_BUTTONS = [
201 CallingVolumeButton,
202 CallingGroupButton,
203 CallingChatButton,
204 CallingScreenShareButton,
205 CallingRecordButton,
206 CallingExtensionButton,
207 CallingFullScreenButton,
208];
209
simon571240f2022-11-29 23:59:27 -0500210const CallInterfaceSecondaryButtons = ({
211 gridItemRef,
212 showVolumeButton,
213}: Props & { showVolumeButton: boolean; gridItemRef: RefObject<HTMLElement> }) => {
simon33c06182022-11-02 17:39:31 -0400214 const stackRef = useRef<HTMLElement>(null);
215
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500216 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400217 const [hiddenStackCount, setHiddenStackCount] = useState(0);
218 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
219
simon571240f2022-11-29 23:59:27 -0500220 // Audio out options are only available on Chrome and other browsers that support `setSinkId`.
221 // This removes the `CallingVolumeButton` if `setSinkId` is not defined.
222 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
223 const secondaryButtons = useMemo(() => {
224 return showVolumeButton ? SECONDARY_BUTTONS : SECONDARY_BUTTONS.slice(1);
225 }, [showVolumeButton]);
226
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500227 const calculateStackCount = useCallback(() => {
simon571240f2022-11-29 23:59:27 -0500228 if (stackRef?.current && gridItemRef?.current) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500229 const buttonWidth = stackRef.current.children[0].clientWidth;
simon571240f2022-11-29 23:59:27 -0500230 const availableSpace = gridItemRef.current.clientWidth;
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500231 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
simon571240f2022-11-29 23:59:27 -0500232 if (availableButtons < secondaryButtons.length) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500233 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400234 }
simon571240f2022-11-29 23:59:27 -0500235 setHiddenStackCount(secondaryButtons.length - availableButtons);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500236 }
simon571240f2022-11-29 23:59:27 -0500237 }, [gridItemRef, secondaryButtons]);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500238
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>[] = [];
simon571240f2022-11-29 23:59:27 -0500258 for (let i = 0; i < secondaryButtons.length; i++) {
259 const button = secondaryButtons[i];
260 if (i < secondaryButtons.length - hiddenStackCount) {
simon33c06182022-11-02 17:39:31 -0400261 displayedButtons.push(button);
262 } else {
263 hiddenButtons.push(button);
264 }
simon571240f2022-11-29 23:59:27 -0500265 }
simon33c06182022-11-02 17:39:31 -0400266
267 return {
268 displayedButtons,
269 hiddenButtons,
270 };
simon571240f2022-11-29 23:59:27 -0500271 }, [hiddenStackCount, secondaryButtons]);
simon33c06182022-11-02 17:39:31 -0400272
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400273 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500274 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
275 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
276 {initialMeasurementDone &&
277 displayedButtons.map((SecondaryButton, i) => (
278 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500279 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500280 </Fragment>
281 ))}
282 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500283 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500284 )}
285 </Stack>
286
287 {!!hiddenButtons.length && hiddenMenuVisible && (
288 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
289 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400290 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500291 direction="column"
292 justifyContent="flex-end"
293 alignItems="flex-end"
294 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400295 >
296 {hiddenButtons.map((SecondaryButton, i) => (
297 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500298 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400299 </Fragment>
300 ))}
301 </Stack>
302 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500303 </Box>
304 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400305 </Card>
306 );
307};