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'; |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 49 | import { ConversationContext } from '../contexts/ConversationProvider'; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 50 | import { WebRtcContext } from '../contexts/WebRtcProvider'; |
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 | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 54 | const { callStatus, isChatShown, isFullscreen } = useContext(CallContext); |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 55 | const callInterfaceRef = useRef<HTMLDivElement>(); |
| 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) { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 70 | return <CallPending />; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 71 | } |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 72 | |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 73 | return ( |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 74 | <Box ref={callInterfaceRef} flexGrow={1} display="flex"> |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 75 | <CallInterface /> |
| 76 | {isChatShown && <CallChatDrawer />} |
| 77 | </Box> |
| 78 | ); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 79 | }; |
| 80 | |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 81 | interface Props { |
| 82 | children?: ReactNode; |
| 83 | } |
| 84 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 85 | const CallInterface = () => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 86 | const { localStream, remoteStreams } = useContext(WebRtcContext); |
| 87 | const { isVideoOn } = useContext(CallContext); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 88 | const gridItemRef = useRef(null); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 89 | const remoteVideoRef = useRef<HTMLVideoElement | null>(null); |
| 90 | const localVideoRef = useRef<HTMLVideoElement | null>(null); |
| 91 | |
| 92 | useEffect(() => { |
| 93 | if (localStream && localVideoRef.current) { |
| 94 | localVideoRef.current.srcObject = localStream; |
| 95 | } |
| 96 | }, [localStream]); |
| 97 | |
| 98 | useEffect(() => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 99 | // TODO: For now, `remoteStream` is the first remote stream in the array. |
| 100 | // There should only be one in the array, but we should make sure this is right. |
| 101 | const remoteStream = remoteStreams?.at(0); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 102 | if (remoteStream && remoteVideoRef.current) { |
| 103 | remoteVideoRef.current.srcObject = remoteStream; |
| 104 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 105 | }, [remoteStreams]); |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 106 | |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 107 | return ( |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 108 | <Box display="flex" flexGrow={1}> |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 109 | <video |
| 110 | ref={remoteVideoRef} |
| 111 | autoPlay |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 112 | style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }} |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 113 | /> |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 114 | <Box flexGrow={1} margin={2} display="flex" flexDirection="column"> |
| 115 | {/* Guest video, takes the whole screen */} |
simon | 8b496b0 | 2022-11-29 01:46:46 -0500 | [diff] [blame] | 116 | <CallInterfaceInformation /> |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 117 | <Box flexGrow={1} marginY={2} position="relative"> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 118 | <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}> |
| 119 | <video |
| 120 | ref={localVideoRef} |
| 121 | autoPlay |
Issam E. Maghni | 8961593 | 2022-11-29 19:05:28 +0000 | [diff] [blame] | 122 | muted |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 123 | style={{ |
| 124 | position: 'absolute', |
| 125 | right: 0, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 126 | borderRadius: '12px', |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 127 | maxHeight: '50%', |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 128 | maxWidth: '50%', |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 129 | visibility: isVideoOn ? 'visible' : 'hidden', |
| 130 | }} |
| 131 | /> |
| 132 | </Draggable> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 133 | </Box> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 134 | <Grid container> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 135 | <Grid item xs /> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 136 | <Grid item sx={{ display: 'flex', justifyContent: 'center' }}> |
| 137 | <div> |
| 138 | <CallInterfacePrimaryButtons /> |
| 139 | </div> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 140 | </Grid> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 141 | <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}> |
| 142 | <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} /> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 143 | </Grid> |
| 144 | </Grid> |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 145 | </Box> |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 146 | </Box> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 147 | ); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 148 | }; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 149 | |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 150 | const formatElapsedSeconds = (elapsedSeconds: number): string => { |
| 151 | const seconds = Math.floor(elapsedSeconds % 60); |
| 152 | elapsedSeconds = Math.floor(elapsedSeconds / 60); |
| 153 | const minutes = elapsedSeconds % 60; |
| 154 | elapsedSeconds = Math.floor(elapsedSeconds / 60); |
| 155 | const hours = elapsedSeconds % 24; |
| 156 | |
| 157 | const times: string[] = []; |
| 158 | if (hours > 0) { |
| 159 | times.push(hours.toString().padStart(2, '0')); |
| 160 | } |
| 161 | times.push(minutes.toString().padStart(2, '0')); |
| 162 | times.push(seconds.toString().padStart(2, '0')); |
| 163 | |
| 164 | return times.join(':'); |
| 165 | }; |
| 166 | |
simon | 8b496b0 | 2022-11-29 01:46:46 -0500 | [diff] [blame] | 167 | const CallInterfaceInformation = () => { |
| 168 | const { callStartTime } = useContext(CallContext); |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 169 | const { conversation } = useContext(ConversationContext); |
simon | 8b496b0 | 2022-11-29 01:46:46 -0500 | [diff] [blame] | 170 | const [elapsedTime, setElapsedTime] = useState<number>(0); |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 171 | const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]); |
simon | 8b496b0 | 2022-11-29 01:46:46 -0500 | [diff] [blame] | 172 | |
| 173 | useEffect(() => { |
| 174 | if (callStartTime) { |
| 175 | const interval = setInterval(() => { |
| 176 | setElapsedTime((new Date().getTime() - callStartTime.getTime()) / 1000); |
| 177 | }, 1000); |
| 178 | return () => clearInterval(interval); |
| 179 | } |
| 180 | }, [callStartTime]); |
| 181 | |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 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"> |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 187 | {memberName} |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 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 | }; |