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