blob: b58b326d9efde00f7a5ed255eb89ea8be8d0088f [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';
simonf929a362022-11-18 16:53:45 -050052import { CallPending } from './CallPending';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040053
simon1170c322022-10-31 14:51:31 -040054export default () => {
simon9076a9a2022-11-29 17:13:01 -050055 const { callStatus, isChatShown, isFullscreen } = useContext(CallContext);
simon2a5cf142022-11-25 15:47:35 -050056 const callInterfaceRef = useRef<HTMLDivElement>();
57
58 useEffect(() => {
59 if (!callInterfaceRef.current) {
60 return;
61 }
62
63 if (isFullscreen && document.fullscreenElement === null) {
64 callInterfaceRef.current.requestFullscreen();
65 } else if (!isFullscreen && document.fullscreenEnabled !== null) {
66 document.exitFullscreen();
67 }
68 }, [isFullscreen]);
simonf929a362022-11-18 16:53:45 -050069
simon9f814a32022-11-22 21:40:53 -050070 if (callStatus !== CallStatus.InCall) {
simon9076a9a2022-11-29 17:13:01 -050071 return <CallPending />;
simonf929a362022-11-18 16:53:45 -050072 }
simon9f814a32022-11-22 21:40:53 -050073
simonf9d78f22022-11-25 15:47:15 -050074 return (
simon2a5cf142022-11-25 15:47:35 -050075 <Box ref={callInterfaceRef} flexGrow={1} display="flex">
simonf9d78f22022-11-25 15:47:15 -050076 <CallInterface />
77 {isChatShown && <CallChatDrawer />}
78 </Box>
79 );
simon1170c322022-10-31 14:51:31 -040080};
81
simon33c06182022-11-02 17:39:31 -040082interface Props {
83 children?: ReactNode;
84}
85
simon1170c322022-10-31 14:51:31 -040086const CallInterface = () => {
simon9076a9a2022-11-29 17:13:01 -050087 const { localStream, remoteStreams } = useContext(WebRtcContext);
simon492e8402022-11-29 16:48:37 -050088 const {
89 isVideoOn,
90 currentMediaDeviceIds: {
91 audiooutput: { id: audioOutDeviceId },
92 },
93 localVideoRef,
94 remoteVideoRef,
95 } = useContext(CallContext);
96 const gridItemRef = useRef<HTMLDivElement | null>(null);
simonf929a362022-11-18 16:53:45 -050097
98 useEffect(() => {
99 if (localStream && localVideoRef.current) {
100 localVideoRef.current.srcObject = localStream;
101 }
simon492e8402022-11-29 16:48:37 -0500102 }, [localStream, localVideoRef]);
simonf929a362022-11-18 16:53:45 -0500103
104 useEffect(() => {
simon9076a9a2022-11-29 17:13:01 -0500105 // TODO: For now, `remoteStream` is the first remote stream in the array.
106 // There should only be one in the array, but we should make sure this is right.
107 const remoteStream = remoteStreams?.at(0);
simonf929a362022-11-18 16:53:45 -0500108 if (remoteStream && remoteVideoRef.current) {
109 remoteVideoRef.current.srcObject = remoteStream;
110 }
simon492e8402022-11-29 16:48:37 -0500111 }, [remoteStreams, remoteVideoRef]);
112
113 useEffect(() => {
114 if (!audioOutDeviceId) {
115 return;
116 }
117
118 if (remoteVideoRef.current?.setSinkId) {
119 // This only work on chrome and other browsers that support `setSinkId`
120 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility
121 remoteVideoRef.current.setSinkId(audioOutDeviceId);
122 }
123 }, [audioOutDeviceId, remoteVideoRef]);
simonce2c0c42022-11-02 17:39:31 -0400124
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400125 return (
simonf9d78f22022-11-25 15:47:15 -0500126 <Box display="flex" flexGrow={1}>
simonce2c0c42022-11-02 17:39:31 -0400127 <video
128 ref={remoteVideoRef}
129 autoPlay
simon9f814a32022-11-22 21:40:53 -0500130 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -0400131 />
simon9f814a32022-11-22 21:40:53 -0500132 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
133 {/* Guest video, takes the whole screen */}
simon8b496b02022-11-29 01:46:46 -0500134 <CallInterfaceInformation />
simon9f814a32022-11-22 21:40:53 -0500135 <Box flexGrow={1} marginY={2} position="relative">
simonf929a362022-11-18 16:53:45 -0500136 <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}>
137 <video
138 ref={localVideoRef}
139 autoPlay
Issam E. Maghni89615932022-11-29 19:05:28 +0000140 muted
simonf929a362022-11-18 16:53:45 -0500141 style={{
142 position: 'absolute',
143 right: 0,
simonf929a362022-11-18 16:53:45 -0500144 borderRadius: '12px',
simonf929a362022-11-18 16:53:45 -0500145 maxHeight: '50%',
simon9f814a32022-11-22 21:40:53 -0500146 maxWidth: '50%',
simonf929a362022-11-18 16:53:45 -0500147 visibility: isVideoOn ? 'visible' : 'hidden',
148 }}
149 />
150 </Draggable>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400151 </Box>
simon33c06182022-11-02 17:39:31 -0400152 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400153 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400154 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
155 <div>
156 <CallInterfacePrimaryButtons />
157 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400158 </Grid>
simon33c06182022-11-02 17:39:31 -0400159 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
160 <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400161 </Grid>
162 </Grid>
simon9f814a32022-11-22 21:40:53 -0500163 </Box>
simonf9d78f22022-11-25 15:47:15 -0500164 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400165 );
simon1170c322022-10-31 14:51:31 -0400166};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400167
Gabriel Rochone382a302022-11-23 12:37:04 -0500168const formatElapsedSeconds = (elapsedSeconds: number): string => {
169 const seconds = Math.floor(elapsedSeconds % 60);
170 elapsedSeconds = Math.floor(elapsedSeconds / 60);
171 const minutes = elapsedSeconds % 60;
172 elapsedSeconds = Math.floor(elapsedSeconds / 60);
173 const hours = elapsedSeconds % 24;
174
175 const times: string[] = [];
176 if (hours > 0) {
177 times.push(hours.toString().padStart(2, '0'));
178 }
179 times.push(minutes.toString().padStart(2, '0'));
180 times.push(seconds.toString().padStart(2, '0'));
181
182 return times.join(':');
183};
184
simon8b496b02022-11-29 01:46:46 -0500185const CallInterfaceInformation = () => {
186 const { callStartTime } = useContext(CallContext);
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500187 const { conversation } = useContext(ConversationContext);
Misha Krieger-Raynauldd0cc3e32022-11-29 19:59:31 -0500188 const [elapsedTime, setElapsedTime] = useState(0);
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500189 const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]);
simon8b496b02022-11-29 01:46:46 -0500190
191 useEffect(() => {
192 if (callStartTime) {
193 const interval = setInterval(() => {
Misha Krieger-Raynauldd0cc3e32022-11-29 19:59:31 -0500194 setElapsedTime((Date.now() - callStartTime) / 1000);
simon8b496b02022-11-29 01:46:46 -0500195 }, 1000);
196 return () => clearInterval(interval);
197 }
198 }, [callStartTime]);
199
Gabriel Rochone382a302022-11-23 12:37:04 -0500200 const elapsedTimerString = formatElapsedSeconds(elapsedTime);
201
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400202 return (
203 <Stack direction="row" justifyContent="space-between" alignItems="center">
204 <Typography color="white" component="p">
Gabriel Rochon15a5fb22022-11-27 19:25:14 -0500205 {memberName}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400206 </Typography>
207 <Typography color="white" component="p">
Gabriel Rochone382a302022-11-23 12:37:04 -0500208 {elapsedTimerString}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400209 </Typography>
210 </Stack>
211 );
212};
213
214const CallInterfacePrimaryButtons = () => {
215 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500216 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
217 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400218 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500219 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400220 <CallingVideoCameraButton />
221 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400222 </Card>
223 );
224};
225
simon33c06182022-11-02 17:39:31 -0400226const SECONDARY_BUTTONS = [
227 CallingVolumeButton,
228 CallingGroupButton,
229 CallingChatButton,
230 CallingScreenShareButton,
231 CallingRecordButton,
232 CallingExtensionButton,
233 CallingFullScreenButton,
234];
235
simon492e8402022-11-29 16:48:37 -0500236const CallInterfaceSecondaryButtons = (props: Props & { gridItemRef: RefObject<HTMLElement> }) => {
simon33c06182022-11-02 17:39:31 -0400237 const stackRef = useRef<HTMLElement>(null);
238
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500239 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400240 const [hiddenStackCount, setHiddenStackCount] = useState(0);
241 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
242
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500243 const calculateStackCount = useCallback(() => {
244 if (stackRef?.current && props.gridItemRef?.current) {
245 const buttonWidth = stackRef.current.children[0].clientWidth;
246 const availableSpace = props.gridItemRef.current.clientWidth;
247 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
248 if (availableButtons < SECONDARY_BUTTONS.length) {
249 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400250 }
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500251 setHiddenStackCount(SECONDARY_BUTTONS.length - availableButtons);
252 }
253 }, [props.gridItemRef]);
254
255 useLayoutEffect(() => {
256 // Run once, at the beginning, for initial measurements
257 if (!initialMeasurementDone) {
258 calculateStackCount();
259 setInitialMeasurementDone(true);
260 }
261
262 const onResize = () => {
263 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400264 };
265 window.addEventListener('resize', onResize);
266 return () => {
267 window.removeEventListener('resize', onResize);
268 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500269 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400270
271 const { displayedButtons, hiddenButtons } = useMemo(() => {
272 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
273 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
274 SECONDARY_BUTTONS.forEach((button, i) => {
275 if (i < SECONDARY_BUTTONS.length - hiddenStackCount) {
276 displayedButtons.push(button);
277 } else {
278 hiddenButtons.push(button);
279 }
280 });
281
282 return {
283 displayedButtons,
284 hiddenButtons,
285 };
286 }, [hiddenStackCount]);
287
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400288 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500289 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
290 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
291 {initialMeasurementDone &&
292 displayedButtons.map((SecondaryButton, i) => (
293 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500294 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500295 </Fragment>
296 ))}
297 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500298 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500299 )}
300 </Stack>
301
302 {!!hiddenButtons.length && hiddenMenuVisible && (
303 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
304 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400305 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500306 direction="column"
307 justifyContent="flex-end"
308 alignItems="flex-end"
309 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400310 >
311 {hiddenButtons.map((SecondaryButton, i) => (
312 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500313 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400314 </Fragment>
315 ))}
316 </Stack>
317 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500318 </Box>
319 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400320 </Card>
321 );
322};