blob: 54c9b10b267f1197fd4fd89e1b7b03ab73185d2a [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';
simon33c06182022-11-02 17:39:31 -040032import Draggable from 'react-draggable';
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';
simonf929a362022-11-18 16:53:45 -050049import { CallContext, CallStatus } from '../contexts/CallProvider';
Gabriel Rochon15a5fb22022-11-27 19:25:14 -050050import { ConversationContext } from '../contexts/ConversationProvider';
simon9076a9a2022-11-29 17:13:01 -050051import { WebRtcContext } from '../contexts/WebRtcProvider';
simon571240f2022-11-29 23:59:27 -050052import { VideoElementWithSinkId } from '../utils/utils';
simonf929a362022-11-18 16:53:45 -050053import { CallPending } from './CallPending';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040054
simon1170c322022-10-31 14:51:31 -040055export default () => {
simon9076a9a2022-11-29 17:13:01 -050056 const { callStatus, isChatShown, isFullscreen } = useContext(CallContext);
simon2a5cf142022-11-25 15:47:35 -050057 const callInterfaceRef = useRef<HTMLDivElement>();
58
59 useEffect(() => {
60 if (!callInterfaceRef.current) {
61 return;
62 }
63
64 if (isFullscreen && document.fullscreenElement === null) {
65 callInterfaceRef.current.requestFullscreen();
66 } else if (!isFullscreen && document.fullscreenEnabled !== null) {
67 document.exitFullscreen();
68 }
69 }, [isFullscreen]);
simonf929a362022-11-18 16:53:45 -050070
simon9f814a32022-11-22 21:40:53 -050071 if (callStatus !== CallStatus.InCall) {
simon9076a9a2022-11-29 17:13:01 -050072 return <CallPending />;
simonf929a362022-11-18 16:53:45 -050073 }
simon9f814a32022-11-22 21:40:53 -050074
simonf9d78f22022-11-25 15:47:15 -050075 return (
simon2a5cf142022-11-25 15:47:35 -050076 <Box ref={callInterfaceRef} flexGrow={1} display="flex">
simonf9d78f22022-11-25 15:47:15 -050077 <CallInterface />
78 {isChatShown && <CallChatDrawer />}
79 </Box>
80 );
simon1170c322022-10-31 14:51:31 -040081};
82
simon33c06182022-11-02 17:39:31 -040083interface Props {
84 children?: ReactNode;
85}
86
simon1170c322022-10-31 14:51:31 -040087const CallInterface = () => {
simon9076a9a2022-11-29 17:13:01 -050088 const { localStream, remoteStreams } = useContext(WebRtcContext);
simon492e8402022-11-29 16:48:37 -050089 const {
90 isVideoOn,
91 currentMediaDeviceIds: {
92 audiooutput: { id: audioOutDeviceId },
93 },
simon492e8402022-11-29 16:48:37 -050094 } = useContext(CallContext);
simon571240f2022-11-29 23:59:27 -050095 const localVideoRef = useRef<VideoElementWithSinkId | null>(null);
96 const remoteVideoRef = useRef<VideoElementWithSinkId | null>(null);
simon492e8402022-11-29 16:48:37 -050097 const gridItemRef = useRef<HTMLDivElement | null>(null);
simonf929a362022-11-18 16:53:45 -050098
99 useEffect(() => {
100 if (localStream && localVideoRef.current) {
101 localVideoRef.current.srcObject = localStream;
102 }
simon492e8402022-11-29 16:48:37 -0500103 }, [localStream, localVideoRef]);
simonf929a362022-11-18 16:53:45 -0500104
105 useEffect(() => {
simon9076a9a2022-11-29 17:13:01 -0500106 // TODO: For now, `remoteStream` is the first remote stream in the array.
107 // There should only be one in the array, but we should make sure this is right.
108 const remoteStream = remoteStreams?.at(0);
simonf929a362022-11-18 16:53:45 -0500109 if (remoteStream && remoteVideoRef.current) {
110 remoteVideoRef.current.srcObject = remoteStream;
111 }
simon492e8402022-11-29 16:48:37 -0500112 }, [remoteStreams, remoteVideoRef]);
113
114 useEffect(() => {
115 if (!audioOutDeviceId) {
116 return;
117 }
118
119 if (remoteVideoRef.current?.setSinkId) {
120 // This only work on chrome and other browsers that support `setSinkId`
121 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
122 remoteVideoRef.current.setSinkId(audioOutDeviceId);
123 }
124 }, [audioOutDeviceId, remoteVideoRef]);
simonce2c0c42022-11-02 17:39:31 -0400125
simon571240f2022-11-29 23:59:27 -0500126 const hasSetSinkId = remoteVideoRef.current?.setSinkId != null;
127
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400128 return (
simonf9d78f22022-11-25 15:47:15 -0500129 <Box display="flex" flexGrow={1}>
simonce2c0c42022-11-02 17:39:31 -0400130 <video
131 ref={remoteVideoRef}
132 autoPlay
simon9f814a32022-11-22 21:40:53 -0500133 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400134 />
simon9f814a32022-11-22 21:40:53 -0500135 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
136 {/* Guest video, takes the whole screen */}
simon8b496b02022-11-29 01:46:46 -0500137 <CallInterfaceInformation />
simon9f814a32022-11-22 21:40:53 -0500138 <Box flexGrow={1} marginY={2} position="relative">
simonf929a362022-11-18 16:53:45 -0500139 <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}>
140 <video
141 ref={localVideoRef}
142 autoPlay
Issam E. Maghni89615932022-11-29 19:05:28 +0000143 muted
simonf929a362022-11-18 16:53:45 -0500144 style={{
145 position: 'absolute',
146 right: 0,
simonf929a362022-11-18 16:53:45 -0500147 borderRadius: '12px',
simonf929a362022-11-18 16:53:45 -0500148 maxHeight: '50%',
simon9f814a32022-11-22 21:40:53 -0500149 maxWidth: '50%',
simonf929a362022-11-18 16:53:45 -0500150 visibility: isVideoOn ? 'visible' : 'hidden',
151 }}
152 />
153 </Draggable>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400154 </Box>
simon33c06182022-11-02 17:39:31 -0400155 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400156 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400157 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
158 <div>
159 <CallInterfacePrimaryButtons />
160 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400161 </Grid>
simon33c06182022-11-02 17:39:31 -0400162 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
simon571240f2022-11-29 23:59:27 -0500163 <CallInterfaceSecondaryButtons showVolumeButton={hasSetSinkId} gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400164 </Grid>
165 </Grid>
simon9f814a32022-11-22 21:40:53 -0500166 </Box>
simonf9d78f22022-11-25 15:47:15 -0500167 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400168 );
simon1170c322022-10-31 14:51:31 -0400169};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400170
Gabriel Rochone382a302022-11-23 12:37:04 -0500171const formatElapsedSeconds = (elapsedSeconds: number): string => {
172 const seconds = Math.floor(elapsedSeconds % 60);
173 elapsedSeconds = Math.floor(elapsedSeconds / 60);
174 const minutes = elapsedSeconds % 60;
175 elapsedSeconds = Math.floor(elapsedSeconds / 60);
176 const hours = elapsedSeconds % 24;
177
178 const times: string[] = [];
179 if (hours > 0) {
180 times.push(hours.toString().padStart(2, '0'));
181 }
182 times.push(minutes.toString().padStart(2, '0'));
183 times.push(seconds.toString().padStart(2, '0'));
184
185 return times.join(':');
186};
187
simon8b496b02022-11-29 01:46:46 -0500188const CallInterfaceInformation = () => {
189 const { callStartTime } = useContext(CallContext);
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500190 const { conversation } = useContext(ConversationContext);
Misha Krieger-Raynauldd0cc3e32022-11-29 19:59:31 -0500191 const [elapsedTime, setElapsedTime] = useState(0);
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500192 const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]);
simon8b496b02022-11-29 01:46:46 -0500193
194 useEffect(() => {
195 if (callStartTime) {
196 const interval = setInterval(() => {
Misha Krieger-Raynauldd0cc3e32022-11-29 19:59:31 -0500197 setElapsedTime((Date.now() - callStartTime) / 1000);
simon8b496b02022-11-29 01:46:46 -0500198 }, 1000);
199 return () => clearInterval(interval);
200 }
201 }, [callStartTime]);
202
Gabriel Rochone382a302022-11-23 12:37:04 -0500203 const elapsedTimerString = formatElapsedSeconds(elapsedTime);
204
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400205 return (
206 <Stack direction="row" justifyContent="space-between" alignItems="center">
207 <Typography color="white" component="p">
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500208 {memberName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400209 </Typography>
210 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500211 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400212 </Typography>
213 </Stack>
214 );
215};
216
217const CallInterfacePrimaryButtons = () => {
218 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500219 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
220 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400221 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500222 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400223 <CallingVideoCameraButton />
224 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400225 </Card>
226 );
227};
228
simon33c06182022-11-02 17:39:31 -0400229const SECONDARY_BUTTONS = [
230 CallingVolumeButton,
231 CallingGroupButton,
232 CallingChatButton,
233 CallingScreenShareButton,
234 CallingRecordButton,
235 CallingExtensionButton,
236 CallingFullScreenButton,
237];
238
simon571240f2022-11-29 23:59:27 -0500239const CallInterfaceSecondaryButtons = ({
240 gridItemRef,
241 showVolumeButton,
242}: Props & { showVolumeButton: boolean; gridItemRef: RefObject<HTMLElement> }) => {
simon33c06182022-11-02 17:39:31 -0400243 const stackRef = useRef<HTMLElement>(null);
244
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500245 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400246 const [hiddenStackCount, setHiddenStackCount] = useState(0);
247 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
248
simon571240f2022-11-29 23:59:27 -0500249 // Audio out options are only available on Chrome and other browsers that support `setSinkId`.
250 // This removes the `CallingVolumeButton` if `setSinkId` is not defined.
251 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
252 const secondaryButtons = useMemo(() => {
253 return showVolumeButton ? SECONDARY_BUTTONS : SECONDARY_BUTTONS.slice(1);
254 }, [showVolumeButton]);
255
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500256 const calculateStackCount = useCallback(() => {
simon571240f2022-11-29 23:59:27 -0500257 if (stackRef?.current && gridItemRef?.current) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500258 const buttonWidth = stackRef.current.children[0].clientWidth;
simon571240f2022-11-29 23:59:27 -0500259 const availableSpace = gridItemRef.current.clientWidth;
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500260 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
simon571240f2022-11-29 23:59:27 -0500261 if (availableButtons < secondaryButtons.length) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500262 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400263 }
simon571240f2022-11-29 23:59:27 -0500264 setHiddenStackCount(secondaryButtons.length - availableButtons);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500265 }
simon571240f2022-11-29 23:59:27 -0500266 }, [gridItemRef, secondaryButtons]);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500267
268 useLayoutEffect(() => {
269 // Run once, at the beginning, for initial measurements
270 if (!initialMeasurementDone) {
271 calculateStackCount();
272 setInitialMeasurementDone(true);
273 }
274
275 const onResize = () => {
276 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400277 };
278 window.addEventListener('resize', onResize);
279 return () => {
280 window.removeEventListener('resize', onResize);
281 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500282 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400283
284 const { displayedButtons, hiddenButtons } = useMemo(() => {
285 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
286 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
simon571240f2022-11-29 23:59:27 -0500287 for (let i = 0; i < secondaryButtons.length; i++) {
288 const button = secondaryButtons[i];
289 if (i < secondaryButtons.length - hiddenStackCount) {
simon33c06182022-11-02 17:39:31 -0400290 displayedButtons.push(button);
291 } else {
292 hiddenButtons.push(button);
293 }
simon571240f2022-11-29 23:59:27 -0500294 }
simon33c06182022-11-02 17:39:31 -0400295
296 return {
297 displayedButtons,
298 hiddenButtons,
299 };
simon571240f2022-11-29 23:59:27 -0500300 }, [hiddenStackCount, secondaryButtons]);
simon33c06182022-11-02 17:39:31 -0400301
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400302 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500303 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
304 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
305 {initialMeasurementDone &&
306 displayedButtons.map((SecondaryButton, i) => (
307 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500308 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500309 </Fragment>
310 ))}
311 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500312 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500313 )}
314 </Stack>
315
316 {!!hiddenButtons.length && hiddenMenuVisible && (
317 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
318 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400319 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500320 direction="column"
321 justifyContent="flex-end"
322 alignItems="flex-end"
323 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400324 >
325 {hiddenButtons.map((SecondaryButton, i) => (
326 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500327 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400328 </Fragment>
329 ))}
330 </Stack>
331 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500332 </Box>
333 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400334 </Card>
335 );
336};