Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 18 | import { Box, Card, Grid, Stack, Typography } from '@mui/material'; |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 19 | import { |
| 20 | ComponentType, |
| 21 | Fragment, |
| 22 | ReactNode, |
| 23 | useCallback, |
| 24 | useContext, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 25 | useEffect, |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 26 | useLayoutEffect, |
| 27 | useMemo, |
| 28 | useRef, |
| 29 | useState, |
| 30 | } from 'react'; |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 31 | import Draggable from 'react-draggable'; |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 32 | import { useLocation } from 'react-router-dom'; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 33 | |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 34 | import { ExpandableButtonProps } from '../components/Button'; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 35 | import { |
| 36 | CallingChatButton, |
| 37 | CallingEndButton, |
| 38 | CallingExtensionButton, |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 39 | CallingFullScreenButton, |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 40 | CallingGroupButton, |
| 41 | CallingMicButton, |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 42 | CallingMoreVerticalButton, |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 43 | CallingRecordButton, |
| 44 | CallingScreenShareButton, |
| 45 | CallingVideoCameraButton, |
| 46 | CallingVolumeButton, |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 47 | } from '../components/CallButtons'; |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 48 | import CallChatDrawer from '../components/CallChatDrawer'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 49 | import { CallContext, CallStatus } from '../contexts/CallProvider'; |
| 50 | import { CallPending } from './CallPending'; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 51 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 52 | export default () => { |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 53 | const { callRole, callStatus, isChatShown, isFullscreen } = useContext(CallContext); |
| 54 | const callInterfaceRef = useRef<HTMLDivElement>(); |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 55 | const { state } = useLocation(); |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 56 | |
| 57 | useEffect(() => { |
| 58 | if (!callInterfaceRef.current) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if (isFullscreen && document.fullscreenElement === null) { |
| 63 | callInterfaceRef.current.requestFullscreen(); |
| 64 | } else if (!isFullscreen && document.fullscreenEnabled !== null) { |
| 65 | document.exitFullscreen(); |
| 66 | } |
| 67 | }, [isFullscreen]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 68 | |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 69 | if (callStatus !== CallStatus.InCall) { |
| 70 | return ( |
| 71 | <CallPending |
| 72 | pending={callRole} |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 73 | caller={callStatus === CallStatus.Connecting ? 'connecting' : 'calling'} |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 74 | medium={state?.isVideoOn ? 'video' : 'audio'} |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 75 | /> |
| 76 | ); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 77 | } |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 78 | |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 79 | return ( |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 80 | <Box ref={callInterfaceRef} flexGrow={1} display="flex"> |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 81 | <CallInterface /> |
| 82 | {isChatShown && <CallChatDrawer />} |
| 83 | </Box> |
| 84 | ); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 85 | }; |
| 86 | |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 87 | interface Props { |
| 88 | children?: ReactNode; |
| 89 | } |
| 90 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 91 | const CallInterface = () => { |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 92 | const { isVideoOn, localStream, remoteStream, callStartTime } = useContext(CallContext); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 93 | const gridItemRef = useRef(null); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 94 | const remoteVideoRef = useRef<HTMLVideoElement | null>(null); |
| 95 | const localVideoRef = useRef<HTMLVideoElement | null>(null); |
| 96 | |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 97 | const [elapsedTime, setElapsedTime] = useState<number>(); |
| 98 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 99 | useEffect(() => { |
| 100 | if (localStream && localVideoRef.current) { |
| 101 | localVideoRef.current.srcObject = localStream; |
| 102 | } |
| 103 | }, [localStream]); |
| 104 | |
| 105 | useEffect(() => { |
| 106 | if (remoteStream && remoteVideoRef.current) { |
| 107 | remoteVideoRef.current.srcObject = remoteStream; |
| 108 | } |
| 109 | }, [remoteStream]); |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 110 | |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 111 | useEffect(() => { |
| 112 | if (callStartTime) { |
| 113 | const interval = setInterval(() => { |
| 114 | setElapsedTime((new Date().getTime() - callStartTime.getTime()) / 1000); |
| 115 | }, 1000); |
| 116 | return () => clearInterval(interval); |
| 117 | } |
| 118 | }, [callStartTime]); |
| 119 | |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 120 | return ( |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 121 | <Box display="flex" flexGrow={1}> |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 122 | <video |
| 123 | ref={remoteVideoRef} |
| 124 | autoPlay |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 125 | style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }} |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 126 | /> |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 127 | <Box flexGrow={1} margin={2} display="flex" flexDirection="column"> |
| 128 | {/* Guest video, takes the whole screen */} |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 129 | <CallInterfaceInformation elapsedTime={elapsedTime} /> |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 130 | <Box flexGrow={1} marginY={2} position="relative"> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 131 | <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}> |
| 132 | <video |
| 133 | ref={localVideoRef} |
| 134 | autoPlay |
| 135 | style={{ |
| 136 | position: 'absolute', |
| 137 | right: 0, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 138 | borderRadius: '12px', |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 139 | maxHeight: '50%', |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 140 | maxWidth: '50%', |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 141 | visibility: isVideoOn ? 'visible' : 'hidden', |
| 142 | }} |
| 143 | /> |
| 144 | </Draggable> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 145 | </Box> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 146 | <Grid container> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 147 | <Grid item xs /> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 148 | <Grid item sx={{ display: 'flex', justifyContent: 'center' }}> |
| 149 | <div> |
| 150 | <CallInterfacePrimaryButtons /> |
| 151 | </div> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 152 | </Grid> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 153 | <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}> |
| 154 | <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} /> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 155 | </Grid> |
| 156 | </Grid> |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 157 | </Box> |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 158 | </Box> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 159 | ); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 160 | }; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 161 | |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 162 | const formatElapsedSeconds = (elapsedSeconds: number): string => { |
| 163 | const seconds = Math.floor(elapsedSeconds % 60); |
| 164 | elapsedSeconds = Math.floor(elapsedSeconds / 60); |
| 165 | const minutes = elapsedSeconds % 60; |
| 166 | elapsedSeconds = Math.floor(elapsedSeconds / 60); |
| 167 | const hours = elapsedSeconds % 24; |
| 168 | |
| 169 | const times: string[] = []; |
| 170 | if (hours > 0) { |
| 171 | times.push(hours.toString().padStart(2, '0')); |
| 172 | } |
| 173 | times.push(minutes.toString().padStart(2, '0')); |
| 174 | times.push(seconds.toString().padStart(2, '0')); |
| 175 | |
| 176 | return times.join(':'); |
| 177 | }; |
| 178 | |
| 179 | interface CallInterfaceInformationProps { |
| 180 | elapsedTime?: number; |
| 181 | } |
| 182 | |
| 183 | const CallInterfaceInformation = ({ elapsedTime = 0 }: CallInterfaceInformationProps) => { |
| 184 | const elapsedTimerString = formatElapsedSeconds(elapsedTime); |
| 185 | |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 186 | return ( |
| 187 | <Stack direction="row" justifyContent="space-between" alignItems="center"> |
| 188 | <Typography color="white" component="p"> |
| 189 | Alain Thérieur |
| 190 | </Typography> |
| 191 | <Typography color="white" component="p"> |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 192 | {elapsedTimerString} |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 193 | </Typography> |
| 194 | </Stack> |
| 195 | ); |
| 196 | }; |
| 197 | |
| 198 | const CallInterfacePrimaryButtons = () => { |
| 199 | return ( |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 200 | <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}> |
| 201 | <Stack direction="row" justifyContent="center" alignItems="center"> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 202 | <CallingMicButton /> |
simon | 9a8fe20 | 2022-11-15 18:25:49 -0500 | [diff] [blame] | 203 | <CallingEndButton /> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 204 | <CallingVideoCameraButton /> |
| 205 | </Stack> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 206 | </Card> |
| 207 | ); |
| 208 | }; |
| 209 | |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 210 | const SECONDARY_BUTTONS = [ |
| 211 | CallingVolumeButton, |
| 212 | CallingGroupButton, |
| 213 | CallingChatButton, |
| 214 | CallingScreenShareButton, |
| 215 | CallingRecordButton, |
| 216 | CallingExtensionButton, |
| 217 | CallingFullScreenButton, |
| 218 | ]; |
| 219 | |
| 220 | const CallInterfaceSecondaryButtons = (props: Props & { gridItemRef: React.RefObject<HTMLElement> }) => { |
| 221 | const stackRef = useRef<HTMLElement>(null); |
| 222 | |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 223 | const [initialMeasurementDone, setInitialMeasurementDone] = useState(false); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 224 | const [hiddenStackCount, setHiddenStackCount] = useState(0); |
| 225 | const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false); |
| 226 | |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 227 | const calculateStackCount = useCallback(() => { |
| 228 | if (stackRef?.current && props.gridItemRef?.current) { |
| 229 | const buttonWidth = stackRef.current.children[0].clientWidth; |
| 230 | const availableSpace = props.gridItemRef.current.clientWidth; |
| 231 | let availableButtons = Math.floor((availableSpace - 1) / buttonWidth); |
| 232 | if (availableButtons < SECONDARY_BUTTONS.length) { |
| 233 | availableButtons -= 1; // Leave room for CallingMoreVerticalButton |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 234 | } |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 235 | setHiddenStackCount(SECONDARY_BUTTONS.length - availableButtons); |
| 236 | } |
| 237 | }, [props.gridItemRef]); |
| 238 | |
| 239 | useLayoutEffect(() => { |
| 240 | // Run once, at the beginning, for initial measurements |
| 241 | if (!initialMeasurementDone) { |
| 242 | calculateStackCount(); |
| 243 | setInitialMeasurementDone(true); |
| 244 | } |
| 245 | |
| 246 | const onResize = () => { |
| 247 | calculateStackCount(); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 248 | }; |
| 249 | window.addEventListener('resize', onResize); |
| 250 | return () => { |
| 251 | window.removeEventListener('resize', onResize); |
| 252 | }; |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 253 | }, [calculateStackCount, initialMeasurementDone]); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 254 | |
| 255 | const { displayedButtons, hiddenButtons } = useMemo(() => { |
| 256 | const displayedButtons: ComponentType<ExpandableButtonProps>[] = []; |
| 257 | const hiddenButtons: ComponentType<ExpandableButtonProps>[] = []; |
| 258 | SECONDARY_BUTTONS.forEach((button, i) => { |
| 259 | if (i < SECONDARY_BUTTONS.length - hiddenStackCount) { |
| 260 | displayedButtons.push(button); |
| 261 | } else { |
| 262 | hiddenButtons.push(button); |
| 263 | } |
| 264 | }); |
| 265 | |
| 266 | return { |
| 267 | displayedButtons, |
| 268 | hiddenButtons, |
| 269 | }; |
| 270 | }, [hiddenStackCount]); |
| 271 | |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 272 | return ( |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 273 | <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}> |
| 274 | <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}> |
| 275 | {initialMeasurementDone && |
| 276 | displayedButtons.map((SecondaryButton, i) => ( |
| 277 | <Fragment key={i}> |
simon | 9a8fe20 | 2022-11-15 18:25:49 -0500 | [diff] [blame] | 278 | <SecondaryButton /> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 279 | </Fragment> |
| 280 | ))} |
| 281 | {(!!hiddenButtons.length || !initialMeasurementDone) && ( |
simon | 9a8fe20 | 2022-11-15 18:25:49 -0500 | [diff] [blame] | 282 | <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} /> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 283 | )} |
| 284 | </Stack> |
| 285 | |
| 286 | {!!hiddenButtons.length && hiddenMenuVisible && ( |
| 287 | <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}> |
| 288 | <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 289 | <Stack |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 290 | direction="column" |
| 291 | justifyContent="flex-end" |
| 292 | alignItems="flex-end" |
| 293 | sx={{ bottom: 0, right: 0, height: '100%' }} |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 294 | > |
| 295 | {hiddenButtons.map((SecondaryButton, i) => ( |
| 296 | <Fragment key={i}> |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 297 | <SecondaryButton isVertical /> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 298 | </Fragment> |
| 299 | ))} |
| 300 | </Stack> |
| 301 | </Card> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 302 | </Box> |
| 303 | )} |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 304 | </Card> |
| 305 | ); |
| 306 | }; |