blob: c5c092209646a1260d89b03728ab63d0d433c045 [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,
23 useCallback,
24 useContext,
simonf929a362022-11-18 16:53:45 -050025 useEffect,
Gabriel Rochon8321a0d2022-11-06 23:18:36 -050026 useLayoutEffect,
27 useMemo,
28 useRef,
29 useState,
30} from 'react';
simon33c06182022-11-02 17:39:31 -040031import Draggable from 'react-draggable';
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';
simonf929a362022-11-18 16:53:45 -050047import { CallContext, CallStatus } from '../contexts/CallProvider';
48import { CallPending } from './CallPending';
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040049
simon1170c322022-10-31 14:51:31 -040050export default () => {
simonf929a362022-11-18 16:53:45 -050051 const { callRole, callStatus } = useContext(CallContext);
52
simon9f814a32022-11-22 21:40:53 -050053 if (callStatus !== CallStatus.InCall) {
54 return (
55 <CallPending
56 pending={callRole}
57 caller={callStatus === CallStatus.Ringing ? 'calling' : 'connecting'}
58 medium="audio"
59 />
60 );
simonf929a362022-11-18 16:53:45 -050061 }
simon9f814a32022-11-22 21:40:53 -050062
63 return <CallInterface />;
simon1170c322022-10-31 14:51:31 -040064};
65
simon33c06182022-11-02 17:39:31 -040066interface Props {
67 children?: ReactNode;
68}
69
simon1170c322022-10-31 14:51:31 -040070const CallInterface = () => {
simonf929a362022-11-18 16:53:45 -050071 const { isVideoOn, localStream, remoteStream } = useContext(CallContext);
simon33c06182022-11-02 17:39:31 -040072 const gridItemRef = useRef(null);
simonf929a362022-11-18 16:53:45 -050073 const remoteVideoRef = useRef<HTMLVideoElement | null>(null);
74 const localVideoRef = useRef<HTMLVideoElement | null>(null);
75
76 useEffect(() => {
77 if (localStream && localVideoRef.current) {
78 localVideoRef.current.srcObject = localStream;
79 }
80 }, [localStream]);
81
82 useEffect(() => {
83 if (remoteStream && remoteVideoRef.current) {
84 remoteVideoRef.current.srcObject = remoteStream;
85 }
86 }, [remoteStream]);
simonce2c0c42022-11-02 17:39:31 -040087
Gabriel Rochone3ec0d22022-10-08 14:27:03 -040088 return (
89 <>
simonce2c0c42022-11-02 17:39:31 -040090 <video
91 ref={remoteVideoRef}
92 autoPlay
simon9f814a32022-11-22 21:40:53 -050093 style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }}
simonce2c0c42022-11-02 17:39:31 -040094 />
simon9f814a32022-11-22 21:40:53 -050095 <Box flexGrow={1} margin={2} display="flex" flexDirection="column">
96 {/* Guest video, takes the whole screen */}
97 <CallInterfaceInformation />
98 <Box flexGrow={1} marginY={2} position="relative">
simonf929a362022-11-18 16:53:45 -050099 <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}>
100 <video
101 ref={localVideoRef}
102 autoPlay
103 style={{
104 position: 'absolute',
105 right: 0,
simonf929a362022-11-18 16:53:45 -0500106 borderRadius: '12px',
simonf929a362022-11-18 16:53:45 -0500107 maxHeight: '50%',
simon9f814a32022-11-22 21:40:53 -0500108 maxWidth: '50%',
simonf929a362022-11-18 16:53:45 -0500109 visibility: isVideoOn ? 'visible' : 'hidden',
110 }}
111 />
112 </Draggable>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400113 </Box>
simon33c06182022-11-02 17:39:31 -0400114 <Grid container>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400115 <Grid item xs />
simon33c06182022-11-02 17:39:31 -0400116 <Grid item sx={{ display: 'flex', justifyContent: 'center' }}>
117 <div>
118 <CallInterfacePrimaryButtons />
119 </div>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400120 </Grid>
simon33c06182022-11-02 17:39:31 -0400121 <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}>
122 <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} />
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400123 </Grid>
124 </Grid>
simon9f814a32022-11-22 21:40:53 -0500125 </Box>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400126 </>
127 );
simon1170c322022-10-31 14:51:31 -0400128};
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400129
130const CallInterfaceInformation = () => {
131 return (
132 <Stack direction="row" justifyContent="space-between" alignItems="center">
133 <Typography color="white" component="p">
134 Alain Thérieur
135 </Typography>
136 <Typography color="white" component="p">
137 01:23
138 </Typography>
139 </Stack>
140 );
141};
142
143const CallInterfacePrimaryButtons = () => {
144 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500145 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}>
146 <Stack direction="row" justifyContent="center" alignItems="center">
simon33c06182022-11-02 17:39:31 -0400147 <CallingMicButton />
simon9a8fe202022-11-15 18:25:49 -0500148 <CallingEndButton />
simon33c06182022-11-02 17:39:31 -0400149 <CallingVideoCameraButton />
150 </Stack>
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400151 </Card>
152 );
153};
154
simon33c06182022-11-02 17:39:31 -0400155const SECONDARY_BUTTONS = [
156 CallingVolumeButton,
157 CallingGroupButton,
158 CallingChatButton,
159 CallingScreenShareButton,
160 CallingRecordButton,
161 CallingExtensionButton,
162 CallingFullScreenButton,
163];
164
165const CallInterfaceSecondaryButtons = (props: Props & { gridItemRef: React.RefObject<HTMLElement> }) => {
166 const stackRef = useRef<HTMLElement>(null);
167
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500168 const [initialMeasurementDone, setInitialMeasurementDone] = useState(false);
simon33c06182022-11-02 17:39:31 -0400169 const [hiddenStackCount, setHiddenStackCount] = useState(0);
170 const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false);
171
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500172 const calculateStackCount = useCallback(() => {
173 if (stackRef?.current && props.gridItemRef?.current) {
174 const buttonWidth = stackRef.current.children[0].clientWidth;
175 const availableSpace = props.gridItemRef.current.clientWidth;
176 let availableButtons = Math.floor((availableSpace - 1) / buttonWidth);
177 if (availableButtons < SECONDARY_BUTTONS.length) {
178 availableButtons -= 1; // Leave room for CallingMoreVerticalButton
simon33c06182022-11-02 17:39:31 -0400179 }
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500180 setHiddenStackCount(SECONDARY_BUTTONS.length - availableButtons);
181 }
182 }, [props.gridItemRef]);
183
184 useLayoutEffect(() => {
185 // Run once, at the beginning, for initial measurements
186 if (!initialMeasurementDone) {
187 calculateStackCount();
188 setInitialMeasurementDone(true);
189 }
190
191 const onResize = () => {
192 calculateStackCount();
simon33c06182022-11-02 17:39:31 -0400193 };
194 window.addEventListener('resize', onResize);
195 return () => {
196 window.removeEventListener('resize', onResize);
197 };
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500198 }, [calculateStackCount, initialMeasurementDone]);
simon33c06182022-11-02 17:39:31 -0400199
200 const { displayedButtons, hiddenButtons } = useMemo(() => {
201 const displayedButtons: ComponentType<ExpandableButtonProps>[] = [];
202 const hiddenButtons: ComponentType<ExpandableButtonProps>[] = [];
203 SECONDARY_BUTTONS.forEach((button, i) => {
204 if (i < SECONDARY_BUTTONS.length - hiddenStackCount) {
205 displayedButtons.push(button);
206 } else {
207 hiddenButtons.push(button);
208 }
209 });
210
211 return {
212 displayedButtons,
213 hiddenButtons,
214 };
215 }, [hiddenStackCount]);
216
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400217 return (
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500218 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}>
219 <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}>
220 {initialMeasurementDone &&
221 displayedButtons.map((SecondaryButton, i) => (
222 <Fragment key={i}>
simon9a8fe202022-11-15 18:25:49 -0500223 <SecondaryButton />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500224 </Fragment>
225 ))}
226 {(!!hiddenButtons.length || !initialMeasurementDone) && (
simon9a8fe202022-11-15 18:25:49 -0500227 <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} />
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500228 )}
229 </Stack>
230
231 {!!hiddenButtons.length && hiddenMenuVisible && (
232 <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}>
233 <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}>
simon33c06182022-11-02 17:39:31 -0400234 <Stack
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500235 direction="column"
236 justifyContent="flex-end"
237 alignItems="flex-end"
238 sx={{ bottom: 0, right: 0, height: '100%' }}
simon33c06182022-11-02 17:39:31 -0400239 >
240 {hiddenButtons.map((SecondaryButton, i) => (
241 <Fragment key={i}>
simon4e7445c2022-11-16 21:18:46 -0500242 <SecondaryButton isVertical />
simon33c06182022-11-02 17:39:31 -0400243 </Fragment>
244 ))}
245 </Stack>
246 </Card>
Gabriel Rochon8321a0d2022-11-06 23:18:36 -0500247 </Box>
248 )}
Gabriel Rochone3ec0d22022-10-08 14:27:03 -0400249 </Card>
250 );
251};