blob: 4feecc20a793a45548bf23032de01862bc188895 [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';
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 () => {
simon9076a9a2022-11-29 17:13:01 -050057 const { callStatus, isChatShown, isFullscreen } = useContext(CallContext);
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 = () => {
simon1e2bf342022-12-02 12:19:40 -050092 const { remoteStreams } = useContext(WebRtcContext);
simon492e8402022-11-29 16:48:37 -050093 const {
simon492e8402022-11-29 16:48:37 -050094 currentMediaDeviceIds: {
95 audiooutput: { id: audioOutDeviceId },
96 },
simon492e8402022-11-29 16:48:37 -050097 } = useContext(CallContext);
simon571240f2022-11-29 23:59:27 -050098 const remoteVideoRef = useRef<VideoElementWithSinkId | null>(null);
simon492e8402022-11-29 16:48:37 -050099 const gridItemRef = useRef<HTMLDivElement | null>(null);
simonf929a362022-11-18 16:53:45 -0500100
101 useEffect(() => {
simon9076a9a2022-11-29 17:13:01 -0500102 // TODO: For now, `remoteStream` is the first remote stream in the array.
103 // There should only be one in the array, but we should make sure this is right.
104 const remoteStream = remoteStreams?.at(0);
simonf929a362022-11-18 16:53:45 -0500105 if (remoteStream && remoteVideoRef.current) {
106 remoteVideoRef.current.srcObject = remoteStream;
107 }
simon492e8402022-11-29 16:48:37 -0500108 }, [remoteStreams, remoteVideoRef]);
109
110 useEffect(() => {
111 if (!audioOutDeviceId) {
112 return;
113 }
114
115 if (remoteVideoRef.current?.setSinkId) {
116 // This only work on chrome and other browsers that support `setSinkId`
117 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
118 remoteVideoRef.current.setSinkId(audioOutDeviceId);
119 }
120 }, [audioOutDeviceId, remoteVideoRef]);
simonce2c0c42022-11-02 17:39:31 -0400121
simon571240f2022-11-29 23:59:27 -0500122 const hasSetSinkId = remoteVideoRef.current?.setSinkId != null;
123
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400124 return (
simonf9d78f22022-11-25 15:47:15 -0500125 <Box display="flex" flexGrow={1}>
simonce2c0c42022-11-02 17:39:31 -0400126 <video
127 ref={remoteVideoRef}
128 autoPlay
simon9f814a32022-11-22 21:40:53 -0500129 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400130 />
simon9f814a32022-11-22 21:40:53 -0500131 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
132 {/* Guest video, takes the whole screen */}
simon8b496b02022-11-29 01:46:46 -0500133 <CallInterfaceInformation />
simon9f814a32022-11-22 21:40:53 -0500134 <Box flexGrow={1} marginY={2} position="relative">
simon1e2bf342022-12-02 12:19:40 -0500135 <Box
136 sx={{
137 position: 'absolute',
138 width: '100%',
139 height: '100%',
140 }}
141 >
142 <LocalVideo />
143 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400144 </Box>
simon33c06182022-11-02 17:39:31 -0400145 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400146 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400147 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
148 <div>
149 <CallInterfacePrimaryButtons />
150 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400151 </Grid>
simon33c06182022-11-02 17:39:31 -0400152 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
simon571240f2022-11-29 23:59:27 -0500153 <CallInterfaceSecondaryButtons showVolumeButton={hasSetSinkId} gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400154 </Grid>
155 </Grid>
simon9f814a32022-11-22 21:40:53 -0500156 </Box>
simonf9d78f22022-11-25 15:47:15 -0500157 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400158 );
simon1170c322022-10-31 14:51:31 -0400159};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400160
simon1e2bf342022-12-02 12:19:40 -0500161const LocalVideo = () => {
162 const { localStream, screenShareLocalStream } = useContext(WebRtcContext);
163 const { videoStatus } = useContext(CallContext);
164 const videoRef = useRef<VideoElementWithSinkId | null>(null);
165
166 const stream = useMemo(() => {
167 switch (videoStatus) {
168 case VideoStatus.Camera:
169 return localStream;
170 case VideoStatus.ScreenShare:
171 return screenShareLocalStream;
172 }
173 }, [videoStatus, localStream, screenShareLocalStream]);
174
175 useEffect(() => {
176 if (stream && videoRef.current) {
177 videoRef.current.srcObject = stream;
178 }
179 }, [stream, videoRef]);
180
181 return (
182 <Draggable bounds="parent" nodeRef={videoRef ?? undefined}>
183 <video
184 ref={videoRef}
185 autoPlay
186 muted
187 style={{
188 position: 'absolute',
189 borderRadius: '12px',
190 maxHeight: '50%',
191 maxWidth: '50%',
192 right: 0,
193 visibility: stream ? 'visible' : 'hidden',
194 }}
195 />
196 </Draggable>
197 );
198};
199
Gabriel Rochone382a302022-11-23 12:37:04 -0500200const formatElapsedSeconds = (elapsedSeconds: number): string => {
201 const seconds = Math.floor(elapsedSeconds % 60);
202 elapsedSeconds = Math.floor(elapsedSeconds / 60);
203 const minutes = elapsedSeconds % 60;
204 elapsedSeconds = Math.floor(elapsedSeconds / 60);
205 const hours = elapsedSeconds % 24;
206
207 const times: string[] = [];
208 if (hours > 0) {
209 times.push(hours.toString().padStart(2, '0'));
210 }
211 times.push(minutes.toString().padStart(2, '0'));
212 times.push(seconds.toString().padStart(2, '0'));
213
214 return times.join(':');
215};
216
simon8b496b02022-11-29 01:46:46 -0500217const CallInterfaceInformation = () => {
218 const { callStartTime } = useContext(CallContext);
simon09fe4822022-11-30 23:36:25 -0500219 const { conversation } = useConversationContext();
simone35acc22022-12-02 16:51:12 -0500220 const [elapsedTime, setElapsedTime] = useState(callStartTime ? (Date.now() - callStartTime) / 1000 : 0);
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -0500221 const memberName = useMemo(() => conversation.getFirstMember().contact.registeredName, [conversation]);
simon8b496b02022-11-29 01:46:46 -0500222
223 useEffect(() => {
224 if (callStartTime) {
225 const interval = setInterval(() => {
Misha Krieger-Raynauldd0cc3e32022-11-29 19:59:31 -0500226 setElapsedTime((Date.now() - callStartTime) / 1000);
simon8b496b02022-11-29 01:46:46 -0500227 }, 1000);
228 return () => clearInterval(interval);
229 }
230 }, [callStartTime]);
231
Gabriel Rochone382a302022-11-23 12:37:04 -0500232 const elapsedTimerString = formatElapsedSeconds(elapsedTime);
233
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400234 return (
235 <Stack direction="row" justifyContent="space-between" alignItems="center">
236 <Typography color="white" component="p">
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500237 {memberName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400238 </Typography>
239 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500240 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400241 </Typography>
242 </Stack>
243 );
244};
245
246const CallInterfacePrimaryButtons = () => {
247 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500248 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
249 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400250 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500251 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400252 <CallingVideoCameraButton />
253 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400254 </Card>
255 );
256};
257
simon33c06182022-11-02 17:39:31 -0400258const SECONDARY_BUTTONS = [
259 CallingVolumeButton,
260 CallingGroupButton,
261 CallingChatButton,
262 CallingScreenShareButton,
263 CallingRecordButton,
264 CallingExtensionButton,
265 CallingFullScreenButton,
266];
267
simon571240f2022-11-29 23:59:27 -0500268const CallInterfaceSecondaryButtons = ({
269 gridItemRef,
270 showVolumeButton,
271}: Props & { showVolumeButton: boolean; gridItemRef: RefObject<HTMLElement> }) => {
simon33c06182022-11-02 17:39:31 -0400272 const stackRef = useRef<HTMLElement>(null);
273
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500274 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400275 const [hiddenStackCount, setHiddenStackCount] = useState(0);
276 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
277
simon571240f2022-11-29 23:59:27 -0500278 // Audio out options are only available on Chrome and other browsers that support `setSinkId`.
279 // This removes the `CallingVolumeButton` if `setSinkId` is not defined.
280 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
281 const secondaryButtons = useMemo(() => {
282 return showVolumeButton ? SECONDARY_BUTTONS : SECONDARY_BUTTONS.slice(1);
283 }, [showVolumeButton]);
284
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500285 const calculateStackCount = useCallback(() => {
simon571240f2022-11-29 23:59:27 -0500286 if (stackRef?.current && gridItemRef?.current) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500287 const buttonWidth = stackRef.current.children[0].clientWidth;
simon571240f2022-11-29 23:59:27 -0500288 const availableSpace = gridItemRef.current.clientWidth;
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500289 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
simon571240f2022-11-29 23:59:27 -0500290 if (availableButtons < secondaryButtons.length) {
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500291 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400292 }
simon571240f2022-11-29 23:59:27 -0500293 setHiddenStackCount(secondaryButtons.length - availableButtons);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500294 }
simon571240f2022-11-29 23:59:27 -0500295 }, [gridItemRef, secondaryButtons]);
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500296
297 useLayoutEffect(() => {
298 // Run once, at the beginning, for initial measurements
299 if (!initialMeasurementDone) {
300 calculateStackCount();
301 setInitialMeasurementDone(true);
302 }
303
304 const onResize = () => {
305 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400306 };
307 window.addEventListener('resize', onResize);
308 return () => {
309 window.removeEventListener('resize', onResize);
310 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500311 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400312
313 const { displayedButtons, hiddenButtons } = useMemo(() => {
314 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
315 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
simon571240f2022-11-29 23:59:27 -0500316 for (let i = 0; i < secondaryButtons.length; i++) {
317 const button = secondaryButtons[i];
318 if (i < secondaryButtons.length - hiddenStackCount) {
simon33c06182022-11-02 17:39:31 -0400319 displayedButtons.push(button);
320 } else {
321 hiddenButtons.push(button);
322 }
simon571240f2022-11-29 23:59:27 -0500323 }
simon33c06182022-11-02 17:39:31 -0400324
325 return {
326 displayedButtons,
327 hiddenButtons,
328 };
simon571240f2022-11-29 23:59:27 -0500329 }, [hiddenStackCount, secondaryButtons]);
simon33c06182022-11-02 17:39:31 -0400330
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400331 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500332 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
333 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
334 {initialMeasurementDone &&
335 displayedButtons.map((SecondaryButton, i) => (
336 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500337 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500338 </Fragment>
339 ))}
340 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500341 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500342 )}
343 </Stack>
344
345 {!!hiddenButtons.length && hiddenMenuVisible && (
346 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
347 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400348 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500349 direction="column"
350 justifyContent="flex-end"
351 alignItems="flex-end"
352 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400353 >
354 {hiddenButtons.map((SecondaryButton, i) => (
355 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500356 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400357 </Fragment>
358 ))}
359 </Stack>
360 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500361 </Box>
362 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400363 </Card>
364 );
365};