blob: 2425b52508dd3daca7e9aaf633ecfd8dfaa248bc [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';
simon5c677962022-12-02 16:51:54 -050053import { useWebRtcContext } from '../contexts/WebRtcProvider';
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 = () => {
simon5c677962022-12-02 16:51:54 -050095 const { localStream, screenShareLocalStream, remoteStreams } = useWebRtcContext();
simon492e8402022-11-29 16:48:37 -050096 const {
simon492e8402022-11-29 16:48:37 -050097 currentMediaDeviceIds: {
98 audiooutput: { id: audioOutDeviceId },
99 },
simondaae9102022-12-02 16:51:31 -0500100 videoStatus,
simon5c677962022-12-02 16:51:54 -0500101 } = useCallContext();
simon571240f2022-11-29 23:59:27 -0500102 const remoteVideoRef = useRef<VideoElementWithSinkId | null>(null);
simon492e8402022-11-29 16:48:37 -0500103 const gridItemRef = useRef<HTMLDivElement | null>(null);
simondaae9102022-12-02 16:51:31 -0500104 const [isLocalVideoZoomed, setIsLocalVideoZoomed] = useState(false);
simonf929a362022-11-18 16:53:45 -0500105
simondaae9102022-12-02 16:51:31 -0500106 const stream = useMemo(() => {
107 switch (videoStatus) {
108 case VideoStatus.Camera:
109 return localStream;
110 case VideoStatus.ScreenShare:
111 return screenShareLocalStream;
simonf929a362022-11-18 16:53:45 -0500112 }
simondaae9102022-12-02 16:51:31 -0500113 }, [videoStatus, localStream, screenShareLocalStream]);
simonce2c0c42022-11-02 17:39:31 -0400114
simon571240f2022-11-29 23:59:27 -0500115 const hasSetSinkId = remoteVideoRef.current?.setSinkId != null;
116
simondaae9102022-12-02 16:51:31 -0500117 // TODO: For now, `remoteStream` is the first remote stream in the array.
118 // There should only be one in the array, but we should make sure this is right.
119 const remoteStream = remoteStreams?.at(0);
120
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400121 return (
simonf9d78f22022-11-25 15:47:15 -0500122 <Box display="flex" flexGrow={1}>
simondaae9102022-12-02 16:51:31 -0500123 <VideoStream
simonce2c0c42022-11-02 17:39:31 -0400124 ref={remoteVideoRef}
simondaae9102022-12-02 16:51:31 -0500125 stream={remoteStream}
126 audioOutDeviceId={audioOutDeviceId}
simon9f814a32022-11-22 21:40:53 -0500127 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400128 />
simon9f814a32022-11-22 21:40:53 -0500129 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
130 {/* Guest video, takes the whole screen */}
simon8b496b02022-11-29 01:46:46 -0500131 <CallInterfaceInformation />
simon9f814a32022-11-22 21:40:53 -0500132 <Box flexGrow={1} marginY={2} position="relative">
simondaae9102022-12-02 16:51:31 -0500133 <VideoOverlay
134 stream={stream}
135 hidden={!stream}
136 muted
137 size={isLocalVideoZoomed ? 'large' : 'medium'}
138 onClick={() => setIsLocalVideoZoomed((v) => !v)}
139 />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400140 </Box>
simon33c06182022-11-02 17:39:31 -0400141 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400142 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400143 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
144 <div>
145 <CallInterfacePrimaryButtons />
146 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400147 </Grid>
simon33c06182022-11-02 17:39:31 -0400148 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
simon571240f2022-11-29 23:59:27 -0500149 <CallInterfaceSecondaryButtons showVolumeButton={hasSetSinkId} gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400150 </Grid>
151 </Grid>
simon9f814a32022-11-22 21:40:53 -0500152 </Box>
simonf9d78f22022-11-25 15:47:15 -0500153 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400154 );
simon1170c322022-10-31 14:51:31 -0400155};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400156
simon8b496b02022-11-29 01:46:46 -0500157const CallInterfaceInformation = () => {
simon5c677962022-12-02 16:51:54 -0500158 const { callStartTime } = useCallContext();
idillon07d31cc2022-12-06 22:40:14 -0500159 const { conversationDisplayName } = useConversationContext();
idillon9e542ca2022-12-15 17:54:07 -0500160 const [elapsedTime, setElapsedTime] = useState<Duration>(
161 dayjs.duration(callStartTime ? Date.now() - callStartTime : 0)
162 );
simon8b496b02022-11-29 01:46:46 -0500163
164 useEffect(() => {
165 if (callStartTime) {
166 const interval = setInterval(() => {
idillon9e542ca2022-12-15 17:54:07 -0500167 setElapsedTime(dayjs.duration(Date.now() - callStartTime));
simon8b496b02022-11-29 01:46:46 -0500168 }, 1000);
169 return () => clearInterval(interval);
170 }
171 }, [callStartTime]);
172
idillon9e542ca2022-12-15 17:54:07 -0500173 const elapsedTimerString = formatCallDuration(elapsedTime);
Gabriel Rochone382a302022-11-23 12:37:04 -0500174
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400175 return (
176 <Stack direction="row" justifyContent="space-between" alignItems="center">
177 <Typography color="white" component="p">
idillon07d31cc2022-12-06 22:40:14 -0500178 {conversationDisplayName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400179 </Typography>
180 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500181 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400182 </Typography>
183 </Stack>
184 );
185};
186
187const CallInterfacePrimaryButtons = () => {
188 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500189 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
190 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400191 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500192 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400193 <CallingVideoCameraButton />
194 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400195 </Card>
196 );
197};
198
simon33c06182022-11-02 17:39:31 -0400199const SECONDARY_BUTTONS = [
200 CallingVolumeButton,
201 CallingGroupButton,
202 CallingChatButton,
203 CallingScreenShareButton,
204 CallingRecordButton,
205 CallingExtensionButton,
206 CallingFullScreenButton,
207];
208
simon571240f2022-11-29 23:59:27 -0500209const CallInterfaceSecondaryButtons = ({
210 gridItemRef,
211 showVolumeButton,
212}: Props & { showVolumeButton: boolean; gridItemRef: RefObject<HTMLElement> }) => {
simon33c06182022-11-02 17:39:31 -0400213 const stackRef = useRef<HTMLElement>(null);
214
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500215 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400216 const [hiddenStackCount, setHiddenStackCount] = useState(0);
217 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
218
simon571240f2022-11-29 23:59:27 -0500219 // Audio out options are only available on Chrome and other browsers that support `setSinkId`.
220 // This removes the `CallingVolumeButton` if `setSinkId` is not defined.
221 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
222 const secondaryButtons = useMemo(() => {
223 return showVolumeButton ? SECONDARY_BUTTONS : SECONDARY_BUTTONS.slice(1);
224 }, [showVolumeButton]);
225
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500226 const calculateStackCount = useCallback(() => {
simon571240f2022-11-29 23:59:27 -0500227 if (stackRef?.current && gridItemRef?.current) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500228 const buttonWidth = stackRef.current.children[0].clientWidth;
simon571240f2022-11-29 23:59:27 -0500229 const availableSpace = gridItemRef.current.clientWidth;
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500230 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
simon571240f2022-11-29 23:59:27 -0500231 if (availableButtons < secondaryButtons.length) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500232 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400233 }
simon571240f2022-11-29 23:59:27 -0500234 setHiddenStackCount(secondaryButtons.length - availableButtons);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500235 }
simon571240f2022-11-29 23:59:27 -0500236 }, [gridItemRef, secondaryButtons]);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500237
238 useLayoutEffect(() => {
239 // Run once, at the beginning, for initial measurements
240 if (!initialMeasurementDone) {
241 calculateStackCount();
242 setInitialMeasurementDone(true);
243 }
244
245 const onResize = () => {
246 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400247 };
248 window.addEventListener('resize', onResize);
249 return () => {
250 window.removeEventListener('resize', onResize);
251 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500252 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400253
254 const { displayedButtons, hiddenButtons } = useMemo(() => {
255 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
256 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
simon571240f2022-11-29 23:59:27 -0500257 for (let i = 0; i < secondaryButtons.length; i++) {
258 const button = secondaryButtons[i];
259 if (i < secondaryButtons.length - hiddenStackCount) {
simon33c06182022-11-02 17:39:31 -0400260 displayedButtons.push(button);
261 } else {
262 hiddenButtons.push(button);
263 }
simon571240f2022-11-29 23:59:27 -0500264 }
simon33c06182022-11-02 17:39:31 -0400265
266 return {
267 displayedButtons,
268 hiddenButtons,
269 };
simon571240f2022-11-29 23:59:27 -0500270 }, [hiddenStackCount, secondaryButtons]);
simon33c06182022-11-02 17:39:31 -0400271
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400272 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500273 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
274 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
275 {initialMeasurementDone &&
276 displayedButtons.map((SecondaryButton, i) => (
277 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500278 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500279 </Fragment>
280 ))}
281 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500282 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500283 )}
284 </Stack>
285
286 {!!hiddenButtons.length && hiddenMenuVisible && (
287 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
288 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400289 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500290 direction="column"
291 justifyContent="flex-end"
292 alignItems="flex-end"
293 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400294 >
295 {hiddenButtons.map((SecondaryButton, i) => (
296 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500297 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400298 </Fragment>
299 ))}
300 </Stack>
301 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500302 </Box>
303 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400304 </Card>
305 );
306};