blob: 52b970fa744baa6a151ce158b0290a808a58312f [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();
simon09fe4822022-11-30 23:36:25 -0500173 const { conversation } = useConversationContext();
simone35acc22022-12-02 16:51:12 -0500174 const [elapsedTime, setElapsedTime] = useState(callStartTime ? (Date.now() - callStartTime) / 1000 : 0);
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -0500175 const memberName = useMemo(() => conversation.getFirstMember().contact.registeredName, [conversation]);
simon8b496b02022-11-29 01:46:46 -0500176
177 useEffect(() => {
178 if (callStartTime) {
179 const interval = setInterval(() => {
Misha Krieger-Raynauldd0cc3e32022-11-29 19:59:31 -0500180 setElapsedTime((Date.now() - callStartTime) / 1000);
simon8b496b02022-11-29 01:46:46 -0500181 }, 1000);
182 return () => clearInterval(interval);
183 }
184 }, [callStartTime]);
185
Gabriel Rochone382a302022-11-23 12:37:04 -0500186 const elapsedTimerString = formatElapsedSeconds(elapsedTime);
187
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400188 return (
189 <Stack direction="row" justifyContent="space-between" alignItems="center">
190 <Typography color="white" component="p">
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500191 {memberName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400192 </Typography>
193 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500194 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400195 </Typography>
196 </Stack>
197 );
198};
199
200const CallInterfacePrimaryButtons = () => {
201 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500202 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
203 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400204 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500205 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400206 <CallingVideoCameraButton />
207 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400208 </Card>
209 );
210};
211
simon33c06182022-11-02 17:39:31 -0400212const SECONDARY_BUTTONS = [
213 CallingVolumeButton,
214 CallingGroupButton,
215 CallingChatButton,
216 CallingScreenShareButton,
217 CallingRecordButton,
218 CallingExtensionButton,
219 CallingFullScreenButton,
220];
221
simon571240f2022-11-29 23:59:27 -0500222const CallInterfaceSecondaryButtons = ({
223 gridItemRef,
224 showVolumeButton,
225}: Props & { showVolumeButton: boolean; gridItemRef: RefObject<HTMLElement> }) => {
simon33c06182022-11-02 17:39:31 -0400226 const stackRef = useRef<HTMLElement>(null);
227
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500228 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400229 const [hiddenStackCount, setHiddenStackCount] = useState(0);
230 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
231
simon571240f2022-11-29 23:59:27 -0500232 // Audio out options are only available on Chrome and other browsers that support `setSinkId`.
233 // This removes the `CallingVolumeButton` if `setSinkId` is not defined.
234 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
235 const secondaryButtons = useMemo(() => {
236 return showVolumeButton ? SECONDARY_BUTTONS : SECONDARY_BUTTONS.slice(1);
237 }, [showVolumeButton]);
238
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500239 const calculateStackCount = useCallback(() => {
simon571240f2022-11-29 23:59:27 -0500240 if (stackRef?.current && gridItemRef?.current) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500241 const buttonWidth = stackRef.current.children[0].clientWidth;
simon571240f2022-11-29 23:59:27 -0500242 const availableSpace = gridItemRef.current.clientWidth;
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500243 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
simon571240f2022-11-29 23:59:27 -0500244 if (availableButtons < secondaryButtons.length) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500245 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400246 }
simon571240f2022-11-29 23:59:27 -0500247 setHiddenStackCount(secondaryButtons.length - availableButtons);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500248 }
simon571240f2022-11-29 23:59:27 -0500249 }, [gridItemRef, secondaryButtons]);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500250
251 useLayoutEffect(() => {
252 // Run once, at the beginning, for initial measurements
253 if (!initialMeasurementDone) {
254 calculateStackCount();
255 setInitialMeasurementDone(true);
256 }
257
258 const onResize = () => {
259 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400260 };
261 window.addEventListener('resize', onResize);
262 return () => {
263 window.removeEventListener('resize', onResize);
264 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500265 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400266
267 const { displayedButtons, hiddenButtons } = useMemo(() => {
268 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
269 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
simon571240f2022-11-29 23:59:27 -0500270 for (let i = 0; i < secondaryButtons.length; i++) {
271 const button = secondaryButtons[i];
272 if (i < secondaryButtons.length - hiddenStackCount) {
simon33c06182022-11-02 17:39:31 -0400273 displayedButtons.push(button);
274 } else {
275 hiddenButtons.push(button);
276 }
simon571240f2022-11-29 23:59:27 -0500277 }
simon33c06182022-11-02 17:39:31 -0400278
279 return {
280 displayedButtons,
281 hiddenButtons,
282 };
simon571240f2022-11-29 23:59:27 -0500283 }, [hiddenStackCount, secondaryButtons]);
simon33c06182022-11-02 17:39:31 -0400284
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400285 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500286 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
287 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
288 {initialMeasurementDone &&
289 displayedButtons.map((SecondaryButton, i) => (
290 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500291 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500292 </Fragment>
293 ))}
294 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500295 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500296 )}
297 </Stack>
298
299 {!!hiddenButtons.length && hiddenMenuVisible && (
300 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
301 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400302 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500303 direction="column"
304 justifyContent="flex-end"
305 alignItems="flex-end"
306 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400307 >
308 {hiddenButtons.map((SecondaryButton, i) => (
309 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500310 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400311 </Fragment>
312 ))}
313 </Stack>
314 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500315 </Box>
316 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400317 </Card>
318 );
319};