blob: 3d5b36652d765dd71b135a83d9b7f9925ac28cfc [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';
simon1e2bf342022-12-02 12:19:40 -050049import { CallContext, CallStatus, VideoStatus } from '../contexts/CallProvider';
simon09fe4822022-11-30 23:36:25 -050050import { useConversationContext } 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();
simon09fe4822022-11-30 23:36:25 -050066 } else if (!isFullscreen && document.fullscreenElement !== null) {
simon2a5cf142022-11-25 15:47:35 -050067 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 = () => {
simon1e2bf342022-12-02 12:19:40 -050088 const { remoteStreams } = useContext(WebRtcContext);
simon492e8402022-11-29 16:48:37 -050089 const {
simon492e8402022-11-29 16:48:37 -050090 currentMediaDeviceIds: {
91 audiooutput: { id: audioOutDeviceId },
92 },
simon492e8402022-11-29 16:48:37 -050093 } = useContext(CallContext);
simon571240f2022-11-29 23:59:27 -050094 const remoteVideoRef = useRef<VideoElementWithSinkId | null>(null);
simon492e8402022-11-29 16:48:37 -050095 const gridItemRef = useRef<HTMLDivElement | null>(null);
simonf929a362022-11-18 16:53:45 -050096
97 useEffect(() => {
simon9076a9a2022-11-29 17:13:01 -050098 // TODO: For now, `remoteStream` is the first remote stream in the array.
99 // There should only be one in the array, but we should make sure this is right.
100 const remoteStream = remoteStreams?.at(0);
simonf929a362022-11-18 16:53:45 -0500101 if (remoteStream && remoteVideoRef.current) {
102 remoteVideoRef.current.srcObject = remoteStream;
103 }
simon492e8402022-11-29 16:48:37 -0500104 }, [remoteStreams, remoteVideoRef]);
105
106 useEffect(() => {
107 if (!audioOutDeviceId) {
108 return;
109 }
110
111 if (remoteVideoRef.current?.setSinkId) {
112 // This only work on chrome and other browsers that support `setSinkId`
113 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
114 remoteVideoRef.current.setSinkId(audioOutDeviceId);
115 }
116 }, [audioOutDeviceId, remoteVideoRef]);
simonce2c0c42022-11-02 17:39:31 -0400117
simon571240f2022-11-29 23:59:27 -0500118 const hasSetSinkId = remoteVideoRef.current?.setSinkId != null;
119
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400120 return (
simonf9d78f22022-11-25 15:47:15 -0500121 <Box display="flex" flexGrow={1}>
simonce2c0c42022-11-02 17:39:31 -0400122 <video
123 ref={remoteVideoRef}
124 autoPlay
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">
simon1e2bf342022-12-02 12:19:40 -0500131 <Box
132 sx={{
133 position: 'absolute',
134 width: '100%',
135 height: '100%',
136 }}
137 >
138 <LocalVideo />
139 </Box>
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
simon1e2bf342022-12-02 12:19:40 -0500157const LocalVideo = () => {
158 const { localStream, screenShareLocalStream } = useContext(WebRtcContext);
159 const { videoStatus } = useContext(CallContext);
160 const videoRef = useRef<VideoElementWithSinkId | null>(null);
161
162 const stream = useMemo(() => {
163 switch (videoStatus) {
164 case VideoStatus.Camera:
165 return localStream;
166 case VideoStatus.ScreenShare:
167 return screenShareLocalStream;
168 }
169 }, [videoStatus, localStream, screenShareLocalStream]);
170
171 useEffect(() => {
172 if (stream && videoRef.current) {
173 videoRef.current.srcObject = stream;
174 }
175 }, [stream, videoRef]);
176
177 return (
178 <Draggable bounds="parent" nodeRef={videoRef ?? undefined}>
179 <video
180 ref={videoRef}
181 autoPlay
182 muted
183 style={{
184 position: 'absolute',
185 borderRadius: '12px',
186 maxHeight: '50%',
187 maxWidth: '50%',
188 right: 0,
189 visibility: stream ? 'visible' : 'hidden',
190 }}
191 />
192 </Draggable>
193 );
194};
195
Gabriel Rochone382a302022-11-23 12:37:04 -0500196const formatElapsedSeconds = (elapsedSeconds: number): string => {
197 const seconds = Math.floor(elapsedSeconds % 60);
198 elapsedSeconds = Math.floor(elapsedSeconds / 60);
199 const minutes = elapsedSeconds % 60;
200 elapsedSeconds = Math.floor(elapsedSeconds / 60);
201 const hours = elapsedSeconds % 24;
202
203 const times: string[] = [];
204 if (hours > 0) {
205 times.push(hours.toString().padStart(2, '0'));
206 }
207 times.push(minutes.toString().padStart(2, '0'));
208 times.push(seconds.toString().padStart(2, '0'));
209
210 return times.join(':');
211};
212
simon8b496b02022-11-29 01:46:46 -0500213const CallInterfaceInformation = () => {
214 const { callStartTime } = useContext(CallContext);
simon09fe4822022-11-30 23:36:25 -0500215 const { conversation } = useConversationContext();
Misha Krieger-Raynauldd0cc3e32022-11-29 19:59:31 -0500216 const [elapsedTime, setElapsedTime] = useState(0);
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -0500217 const memberName = useMemo(() => conversation.getFirstMember().contact.registeredName, [conversation]);
simon8b496b02022-11-29 01:46:46 -0500218
219 useEffect(() => {
220 if (callStartTime) {
221 const interval = setInterval(() => {
Misha Krieger-Raynauldd0cc3e32022-11-29 19:59:31 -0500222 setElapsedTime((Date.now() - callStartTime) / 1000);
simon8b496b02022-11-29 01:46:46 -0500223 }, 1000);
224 return () => clearInterval(interval);
225 }
226 }, [callStartTime]);
227
Gabriel Rochone382a302022-11-23 12:37:04 -0500228 const elapsedTimerString = formatElapsedSeconds(elapsedTime);
229
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400230 return (
231 <Stack direction="row" justifyContent="space-between" alignItems="center">
232 <Typography color="white" component="p">
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500233 {memberName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400234 </Typography>
235 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500236 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400237 </Typography>
238 </Stack>
239 );
240};
241
242const CallInterfacePrimaryButtons = () => {
243 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500244 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
245 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400246 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500247 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400248 <CallingVideoCameraButton />
249 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400250 </Card>
251 );
252};
253
simon33c06182022-11-02 17:39:31 -0400254const SECONDARY_BUTTONS = [
255 CallingVolumeButton,
256 CallingGroupButton,
257 CallingChatButton,
258 CallingScreenShareButton,
259 CallingRecordButton,
260 CallingExtensionButton,
261 CallingFullScreenButton,
262];
263
simon571240f2022-11-29 23:59:27 -0500264const CallInterfaceSecondaryButtons = ({
265 gridItemRef,
266 showVolumeButton,
267}: Props & { showVolumeButton: boolean; gridItemRef: RefObject<HTMLElement> }) => {
simon33c06182022-11-02 17:39:31 -0400268 const stackRef = useRef<HTMLElement>(null);
269
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500270 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400271 const [hiddenStackCount, setHiddenStackCount] = useState(0);
272 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
273
simon571240f2022-11-29 23:59:27 -0500274 // Audio out options are only available on Chrome and other browsers that support `setSinkId`.
275 // This removes the `CallingVolumeButton` if `setSinkId` is not defined.
276 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
277 const secondaryButtons = useMemo(() => {
278 return showVolumeButton ? SECONDARY_BUTTONS : SECONDARY_BUTTONS.slice(1);
279 }, [showVolumeButton]);
280
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500281 const calculateStackCount = useCallback(() => {
simon571240f2022-11-29 23:59:27 -0500282 if (stackRef?.current && gridItemRef?.current) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500283 const buttonWidth = stackRef.current.children[0].clientWidth;
simon571240f2022-11-29 23:59:27 -0500284 const availableSpace = gridItemRef.current.clientWidth;
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500285 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
simon571240f2022-11-29 23:59:27 -0500286 if (availableButtons < secondaryButtons.length) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500287 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400288 }
simon571240f2022-11-29 23:59:27 -0500289 setHiddenStackCount(secondaryButtons.length - availableButtons);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500290 }
simon571240f2022-11-29 23:59:27 -0500291 }, [gridItemRef, secondaryButtons]);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500292
293 useLayoutEffect(() => {
294 // Run once, at the beginning, for initial measurements
295 if (!initialMeasurementDone) {
296 calculateStackCount();
297 setInitialMeasurementDone(true);
298 }
299
300 const onResize = () => {
301 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400302 };
303 window.addEventListener('resize', onResize);
304 return () => {
305 window.removeEventListener('resize', onResize);
306 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500307 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400308
309 const { displayedButtons, hiddenButtons } = useMemo(() => {
310 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
311 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
simon571240f2022-11-29 23:59:27 -0500312 for (let i = 0; i < secondaryButtons.length; i++) {
313 const button = secondaryButtons[i];
314 if (i < secondaryButtons.length - hiddenStackCount) {
simon33c06182022-11-02 17:39:31 -0400315 displayedButtons.push(button);
316 } else {
317 hiddenButtons.push(button);
318 }
simon571240f2022-11-29 23:59:27 -0500319 }
simon33c06182022-11-02 17:39:31 -0400320
321 return {
322 displayedButtons,
323 hiddenButtons,
324 };
simon571240f2022-11-29 23:59:27 -0500325 }, [hiddenStackCount, secondaryButtons]);
simon33c06182022-11-02 17:39:31 -0400326
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400327 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500328 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
329 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
330 {initialMeasurementDone &&
331 displayedButtons.map((SecondaryButton, i) => (
332 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500333 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500334 </Fragment>
335 ))}
336 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500337 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500338 )}
339 </Stack>
340
341 {!!hiddenButtons.length && hiddenMenuVisible && (
342 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
343 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400344 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500345 direction="column"
346 justifyContent="flex-end"
347 alignItems="flex-end"
348 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400349 >
350 {hiddenButtons.map((SecondaryButton, i) => (
351 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500352 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400353 </Fragment>
354 ))}
355 </Stack>
356 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500357 </Box>
358 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400359 </Card>
360 );
361};