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