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