blob: 3c98784093bafcbf50f3c5f064e6a28f8bf67407 [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,
25 useContext,
simonf929a362022-11-18 16:53:45 -050026 useEffect,
Gabriel Rochon8321a0d2022-11-06 23:18:36 -050027 useLayoutEffect,
28 useMemo,
29 useRef,
30 useState,
31} from 'react';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040032
simon33c06182022-11-02 17:39:31 -040033import { ExpandableButtonProps } from '../components/Button';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040034import {
35 CallingChatButton,
36 CallingEndButton,
37 CallingExtensionButton,
simon33c06182022-11-02 17:39:31 -040038 CallingFullScreenButton,
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040039 CallingGroupButton,
40 CallingMicButton,
simon33c06182022-11-02 17:39:31 -040041 CallingMoreVerticalButton,
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040042 CallingRecordButton,
43 CallingScreenShareButton,
44 CallingVideoCameraButton,
45 CallingVolumeButton,
simon1170c322022-10-31 14:51:31 -040046} from '../components/CallButtons';
simonf9d78f22022-11-25 15:47:15 -050047import CallChatDrawer from '../components/CallChatDrawer';
simondaae9102022-12-02 16:51:31 -050048import VideoOverlay from '../components/VideoOverlay';
49import VideoStream from '../components/VideoStream';
simon1e2bf342022-12-02 12:19:40 -050050import { CallContext, CallStatus, VideoStatus } from '../contexts/CallProvider';
simon09fe4822022-11-30 23:36:25 -050051import { useConversationContext } from '../contexts/ConversationProvider';
simon9076a9a2022-11-29 17:13:01 -050052import { WebRtcContext } from '../contexts/WebRtcProvider';
simon571240f2022-11-29 23:59:27 -050053import { VideoElementWithSinkId } from '../utils/utils';
simonf929a362022-11-18 16:53:45 -050054import { CallPending } from './CallPending';
simone35acc22022-12-02 16:51:12 -050055import CallPermissionDenied from './CallPermissionDenied';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040056
simon1170c322022-10-31 14:51:31 -040057export default () => {
simon9076a9a2022-11-29 17:13:01 -050058 const { callStatus, isChatShown, isFullscreen } = useContext(CallContext);
simon2a5cf142022-11-25 15:47:35 -050059 const callInterfaceRef = useRef<HTMLDivElement>();
60
61 useEffect(() => {
62 if (!callInterfaceRef.current) {
63 return;
64 }
65
66 if (isFullscreen && document.fullscreenElement === null) {
67 callInterfaceRef.current.requestFullscreen();
simon09fe4822022-11-30 23:36:25 -050068 } else if (!isFullscreen && document.fullscreenElement !== null) {
simon2a5cf142022-11-25 15:47:35 -050069 document.exitFullscreen();
70 }
71 }, [isFullscreen]);
simonf929a362022-11-18 16:53:45 -050072
simone35acc22022-12-02 16:51:12 -050073 if (callStatus === CallStatus.PermissionsDenied) {
74 return <CallPermissionDenied />;
75 }
simon9f814a32022-11-22 21:40:53 -050076 if (callStatus !== CallStatus.InCall) {
simon9076a9a2022-11-29 17:13:01 -050077 return <CallPending />;
simonf929a362022-11-18 16:53:45 -050078 }
simon9f814a32022-11-22 21:40:53 -050079
simonf9d78f22022-11-25 15:47:15 -050080 return (
simon2a5cf142022-11-25 15:47:35 -050081 <Box ref={callInterfaceRef} flexGrow={1} display="flex">
simonf9d78f22022-11-25 15:47:15 -050082 <CallInterface />
83 {isChatShown && <CallChatDrawer />}
84 </Box>
85 );
simon1170c322022-10-31 14:51:31 -040086};
87
simon33c06182022-11-02 17:39:31 -040088interface Props {
89 children?: ReactNode;
90}
91
simon1170c322022-10-31 14:51:31 -040092const CallInterface = () => {
simondaae9102022-12-02 16:51:31 -050093 const { localStream, screenShareLocalStream, remoteStreams } = useContext(WebRtcContext);
simon492e8402022-11-29 16:48:37 -050094 const {
simon492e8402022-11-29 16:48:37 -050095 currentMediaDeviceIds: {
96 audiooutput: { id: audioOutDeviceId },
97 },
simondaae9102022-12-02 16:51:31 -050098 videoStatus,
simon492e8402022-11-29 16:48:37 -050099 } = useContext(CallContext);
simon571240f2022-11-29 23:59:27 -0500100 const remoteVideoRef = useRef<VideoElementWithSinkId | null>(null);
simon492e8402022-11-29 16:48:37 -0500101 const gridItemRef = useRef<HTMLDivElement | null>(null);
simondaae9102022-12-02 16:51:31 -0500102 const [isLocalVideoZoomed, setIsLocalVideoZoomed] = useState(false);
simonf929a362022-11-18 16:53:45 -0500103
simondaae9102022-12-02 16:51:31 -0500104 const stream = useMemo(() => {
105 switch (videoStatus) {
106 case VideoStatus.Camera:
107 return localStream;
108 case VideoStatus.ScreenShare:
109 return screenShareLocalStream;
simonf929a362022-11-18 16:53:45 -0500110 }
simondaae9102022-12-02 16:51:31 -0500111 }, [videoStatus, localStream, screenShareLocalStream]);
simonce2c0c42022-11-02 17:39:31 -0400112
simon571240f2022-11-29 23:59:27 -0500113 const hasSetSinkId = remoteVideoRef.current?.setSinkId != null;
114
simondaae9102022-12-02 16:51:31 -0500115 // TODO: For now, `remoteStream` is the first remote stream in the array.
116 // There should only be one in the array, but we should make sure this is right.
117 const remoteStream = remoteStreams?.at(0);
118
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400119 return (
simonf9d78f22022-11-25 15:47:15 -0500120 <Box display="flex" flexGrow={1}>
simondaae9102022-12-02 16:51:31 -0500121 <VideoStream
simonce2c0c42022-11-02 17:39:31 -0400122 ref={remoteVideoRef}
simondaae9102022-12-02 16:51:31 -0500123 stream={remoteStream}
124 audioOutDeviceId={audioOutDeviceId}
simon9f814a32022-11-22 21:40:53 -0500125 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400126 />
simon9f814a32022-11-22 21:40:53 -0500127 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
128 {/* Guest video, takes the whole screen */}
simon8b496b02022-11-29 01:46:46 -0500129 <CallInterfaceInformation />
simon9f814a32022-11-22 21:40:53 -0500130 <Box flexGrow={1} marginY={2} position="relative">
simondaae9102022-12-02 16:51:31 -0500131 <VideoOverlay
132 stream={stream}
133 hidden={!stream}
134 muted
135 size={isLocalVideoZoomed ? 'large' : 'medium'}
136 onClick={() => setIsLocalVideoZoomed((v) => !v)}
137 />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400138 </Box>
simon33c06182022-11-02 17:39:31 -0400139 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400140 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400141 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
142 <div>
143 <CallInterfacePrimaryButtons />
144 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400145 </Grid>
simon33c06182022-11-02 17:39:31 -0400146 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
simon571240f2022-11-29 23:59:27 -0500147 <CallInterfaceSecondaryButtons showVolumeButton={hasSetSinkId} gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400148 </Grid>
149 </Grid>
simon9f814a32022-11-22 21:40:53 -0500150 </Box>
simonf9d78f22022-11-25 15:47:15 -0500151 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400152 );
simon1170c322022-10-31 14:51:31 -0400153};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400154
Gabriel Rochone382a302022-11-23 12:37:04 -0500155const formatElapsedSeconds = (elapsedSeconds: number): string => {
156 const seconds = Math.floor(elapsedSeconds % 60);
157 elapsedSeconds = Math.floor(elapsedSeconds / 60);
158 const minutes = elapsedSeconds % 60;
159 elapsedSeconds = Math.floor(elapsedSeconds / 60);
160 const hours = elapsedSeconds % 24;
161
162 const times: string[] = [];
163 if (hours > 0) {
164 times.push(hours.toString().padStart(2, '0'));
165 }
166 times.push(minutes.toString().padStart(2, '0'));
167 times.push(seconds.toString().padStart(2, '0'));
168
169 return times.join(':');
170};
171
simon8b496b02022-11-29 01:46:46 -0500172const CallInterfaceInformation = () => {
173 const { callStartTime } = useContext(CallContext);
simon09fe4822022-11-30 23:36:25 -0500174 const { conversation } = useConversationContext();
simone35acc22022-12-02 16:51:12 -0500175 const [elapsedTime, setElapsedTime] = useState(callStartTime ? (Date.now() - callStartTime) / 1000 : 0);
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -0500176 const memberName = useMemo(() => conversation.getFirstMember().contact.registeredName, [conversation]);
simon8b496b02022-11-29 01:46:46 -0500177
178 useEffect(() => {
179 if (callStartTime) {
180 const interval = setInterval(() => {
Misha Krieger-Raynauldd0cc3e32022-11-29 19:59:31 -0500181 setElapsedTime((Date.now() - callStartTime) / 1000);
simon8b496b02022-11-29 01:46:46 -0500182 }, 1000);
183 return () => clearInterval(interval);
184 }
185 }, [callStartTime]);
186
Gabriel Rochone382a302022-11-23 12:37:04 -0500187 const elapsedTimerString = formatElapsedSeconds(elapsedTime);
188
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400189 return (
190 <Stack direction="row" justifyContent="space-between" alignItems="center">
191 <Typography color="white" component="p">
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500192 {memberName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400193 </Typography>
194 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500195 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400196 </Typography>
197 </Stack>
198 );
199};
200
201const CallInterfacePrimaryButtons = () => {
202 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500203 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
204 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400205 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500206 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400207 <CallingVideoCameraButton />
208 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400209 </Card>
210 );
211};
212
simon33c06182022-11-02 17:39:31 -0400213const SECONDARY_BUTTONS = [
214 CallingVolumeButton,
215 CallingGroupButton,
216 CallingChatButton,
217 CallingScreenShareButton,
218 CallingRecordButton,
219 CallingExtensionButton,
220 CallingFullScreenButton,
221];
222
simon571240f2022-11-29 23:59:27 -0500223const CallInterfaceSecondaryButtons = ({
224 gridItemRef,
225 showVolumeButton,
226}: Props & { showVolumeButton: boolean; gridItemRef: RefObject<HTMLElement> }) => {
simon33c06182022-11-02 17:39:31 -0400227 const stackRef = useRef<HTMLElement>(null);
228
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500229 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400230 const [hiddenStackCount, setHiddenStackCount] = useState(0);
231 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
232
simon571240f2022-11-29 23:59:27 -0500233 // Audio out options are only available on Chrome and other browsers that support `setSinkId`.
234 // This removes the `CallingVolumeButton` if `setSinkId` is not defined.
235 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
236 const secondaryButtons = useMemo(() => {
237 return showVolumeButton ? SECONDARY_BUTTONS : SECONDARY_BUTTONS.slice(1);
238 }, [showVolumeButton]);
239
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500240 const calculateStackCount = useCallback(() => {
simon571240f2022-11-29 23:59:27 -0500241 if (stackRef?.current && gridItemRef?.current) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500242 const buttonWidth = stackRef.current.children[0].clientWidth;
simon571240f2022-11-29 23:59:27 -0500243 const availableSpace = gridItemRef.current.clientWidth;
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500244 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
simon571240f2022-11-29 23:59:27 -0500245 if (availableButtons < secondaryButtons.length) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500246 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400247 }
simon571240f2022-11-29 23:59:27 -0500248 setHiddenStackCount(secondaryButtons.length - availableButtons);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500249 }
simon571240f2022-11-29 23:59:27 -0500250 }, [gridItemRef, secondaryButtons]);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500251
252 useLayoutEffect(() => {
253 // Run once, at the beginning, for initial measurements
254 if (!initialMeasurementDone) {
255 calculateStackCount();
256 setInitialMeasurementDone(true);
257 }
258
259 const onResize = () => {
260 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400261 };
262 window.addEventListener('resize', onResize);
263 return () => {
264 window.removeEventListener('resize', onResize);
265 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500266 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400267
268 const { displayedButtons, hiddenButtons } = useMemo(() => {
269 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
270 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
simon571240f2022-11-29 23:59:27 -0500271 for (let i = 0; i < secondaryButtons.length; i++) {
272 const button = secondaryButtons[i];
273 if (i < secondaryButtons.length - hiddenStackCount) {
simon33c06182022-11-02 17:39:31 -0400274 displayedButtons.push(button);
275 } else {
276 hiddenButtons.push(button);
277 }
simon571240f2022-11-29 23:59:27 -0500278 }
simon33c06182022-11-02 17:39:31 -0400279
280 return {
281 displayedButtons,
282 hiddenButtons,
283 };
simon571240f2022-11-29 23:59:27 -0500284 }, [hiddenStackCount, secondaryButtons]);
simon33c06182022-11-02 17:39:31 -0400285
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400286 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500287 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
288 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
289 {initialMeasurementDone &&
290 displayedButtons.map((SecondaryButton, i) => (
291 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500292 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500293 </Fragment>
294 ))}
295 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500296 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500297 )}
298 </Stack>
299
300 {!!hiddenButtons.length && hiddenMenuVisible && (
301 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
302 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400303 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500304 direction="column"
305 justifyContent="flex-end"
306 alignItems="flex-end"
307 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400308 >
309 {hiddenButtons.map((SecondaryButton, i) => (
310 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500311 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400312 </Fragment>
313 ))}
314 </Stack>
315 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500316 </Box>
317 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400318 </Card>
319 );
320};