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