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 | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 18 | import { Box, Button, 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, |
| 25 | useLayoutEffect, |
| 26 | useMemo, |
| 27 | useRef, |
| 28 | useState, |
| 29 | } from 'react'; |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 30 | import Draggable from 'react-draggable'; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 31 | |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 32 | import { ExpandableButtonProps } from '../components/Button'; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 33 | import { |
| 34 | CallingChatButton, |
| 35 | CallingEndButton, |
| 36 | CallingExtensionButton, |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 37 | CallingFullScreenButton, |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 38 | CallingGroupButton, |
| 39 | CallingMicButton, |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 40 | CallingMoreVerticalButton, |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 41 | CallingRecordButton, |
| 42 | CallingScreenShareButton, |
| 43 | CallingVideoCameraButton, |
| 44 | CallingVolumeButton, |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 45 | } from '../components/CallButtons'; |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 46 | import WebRTCProvider, { WebRTCContext } from '../contexts/WebRTCProvider'; |
simon | 3f5f3e7 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 47 | import { CallRouteParams } from '../router'; |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 48 | import { useUrlParams } from '../utils/hooks'; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 49 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 50 | export default () => { |
| 51 | const { |
| 52 | queryParams: { video }, |
| 53 | } = useUrlParams<CallRouteParams>(); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 54 | return ( |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 55 | //TODO: set contactID |
| 56 | <WebRTCProvider isVideoOn={video === 'true'} contactId={'contactIdToBeAdded'}> |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 57 | <CallInterface /> |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 58 | </WebRTCProvider> |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 59 | ); |
| 60 | }; |
| 61 | |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 62 | interface Props { |
| 63 | children?: ReactNode; |
| 64 | } |
| 65 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 66 | const CallInterface = () => { |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 67 | const { localVideoRef, remoteVideoRef, isVideoOn } = useContext(WebRTCContext); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 68 | const gridItemRef = useRef(null); |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 69 | |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 70 | return ( |
| 71 | <> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 72 | {/* Guest video, takes the whole screen */} |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 73 | <video |
| 74 | ref={remoteVideoRef} |
| 75 | autoPlay |
| 76 | style={{ backgroundColor: 'black', width: '100%', height: '100%', position: 'absolute' }} |
| 77 | /> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 78 | <Stack |
| 79 | position="absolute" |
| 80 | direction="column" |
| 81 | spacing={1} |
| 82 | margin={2} |
| 83 | sx={{ left: 0, right: 0, top: 0, bottom: 0 }} |
| 84 | > |
| 85 | {/* Top panel with guest information */} |
| 86 | <Box> |
| 87 | <CallInterfaceInformation /> |
| 88 | </Box> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 89 | {/* Local video, with empty space to be moved around and stickied to walls */} |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 90 | <Box height="100%"> |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 91 | {isVideoOn && ( |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 92 | <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 93 | <video |
| 94 | ref={localVideoRef} |
| 95 | autoPlay |
| 96 | style={{ |
| 97 | position: 'absolute', |
| 98 | right: 0, |
| 99 | zIndex: 2, |
| 100 | borderRadius: '12px', |
| 101 | minWidth: '25%', |
| 102 | minHeight: '25%', |
| 103 | maxWidth: '50%', |
| 104 | maxHeight: '50%', |
| 105 | }} |
| 106 | /> |
| 107 | </Draggable> |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 108 | )} |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 109 | </Box> |
| 110 | {/* Bottom panel with calling buttons */} |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 111 | <Grid container> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 112 | <Grid item xs /> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 113 | <Grid item sx={{ display: 'flex', justifyContent: 'center' }}> |
| 114 | <div> |
| 115 | <CallInterfacePrimaryButtons /> |
| 116 | </div> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 117 | </Grid> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 118 | <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}> |
| 119 | <CallInterfaceSecondaryButtons gridItemRef={gridItemRef} /> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 120 | </Grid> |
| 121 | </Grid> |
| 122 | </Stack> |
| 123 | </> |
| 124 | ); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 125 | }; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 126 | |
| 127 | const CallInterfaceInformation = () => { |
| 128 | return ( |
| 129 | <Stack direction="row" justifyContent="space-between" alignItems="center"> |
| 130 | <Typography color="white" component="p"> |
| 131 | Alain Thérieur |
| 132 | </Typography> |
| 133 | <Typography color="white" component="p"> |
| 134 | 01:23 |
| 135 | </Typography> |
| 136 | </Stack> |
| 137 | ); |
| 138 | }; |
| 139 | |
| 140 | const CallInterfacePrimaryButtons = () => { |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 141 | const { sendWebRTCOffer } = useContext(WebRTCContext); |
| 142 | |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 143 | return ( |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 144 | <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}> |
| 145 | <Stack direction="row" justifyContent="center" alignItems="center"> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 146 | <Button |
| 147 | variant="contained" |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 148 | size="small" |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 149 | onClick={() => { |
| 150 | sendWebRTCOffer(); |
| 151 | }} |
| 152 | > |
| 153 | {/* TODO: Remove this button and make calling automatic (https://git.jami.net/savoirfairelinux/jami-web/-/issues/91)*/} |
| 154 | Call |
| 155 | </Button> |
| 156 | <CallingMicButton /> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 157 | <CallingEndButton hidden={false} /> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 158 | <CallingVideoCameraButton /> |
| 159 | </Stack> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 160 | </Card> |
| 161 | ); |
| 162 | }; |
| 163 | |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 164 | const SECONDARY_BUTTONS = [ |
| 165 | CallingVolumeButton, |
| 166 | CallingGroupButton, |
| 167 | CallingChatButton, |
| 168 | CallingScreenShareButton, |
| 169 | CallingRecordButton, |
| 170 | CallingExtensionButton, |
| 171 | CallingFullScreenButton, |
| 172 | ]; |
| 173 | |
| 174 | const CallInterfaceSecondaryButtons = (props: Props & { gridItemRef: React.RefObject<HTMLElement> }) => { |
| 175 | const stackRef = useRef<HTMLElement>(null); |
| 176 | |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 177 | const [initialMeasurementDone, setInitialMeasurementDone] = useState(false); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 178 | const [hiddenStackCount, setHiddenStackCount] = useState(0); |
| 179 | const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false); |
| 180 | |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 181 | const calculateStackCount = useCallback(() => { |
| 182 | if (stackRef?.current && props.gridItemRef?.current) { |
| 183 | const buttonWidth = stackRef.current.children[0].clientWidth; |
| 184 | const availableSpace = props.gridItemRef.current.clientWidth; |
| 185 | let availableButtons = Math.floor((availableSpace - 1) / buttonWidth); |
| 186 | if (availableButtons < SECONDARY_BUTTONS.length) { |
| 187 | availableButtons -= 1; // Leave room for CallingMoreVerticalButton |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 188 | } |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 189 | setHiddenStackCount(SECONDARY_BUTTONS.length - availableButtons); |
| 190 | } |
| 191 | }, [props.gridItemRef]); |
| 192 | |
| 193 | useLayoutEffect(() => { |
| 194 | // Run once, at the beginning, for initial measurements |
| 195 | if (!initialMeasurementDone) { |
| 196 | calculateStackCount(); |
| 197 | setInitialMeasurementDone(true); |
| 198 | } |
| 199 | |
| 200 | const onResize = () => { |
| 201 | calculateStackCount(); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 202 | }; |
| 203 | window.addEventListener('resize', onResize); |
| 204 | return () => { |
| 205 | window.removeEventListener('resize', onResize); |
| 206 | }; |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 207 | }, [calculateStackCount, initialMeasurementDone]); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 208 | |
| 209 | const { displayedButtons, hiddenButtons } = useMemo(() => { |
| 210 | const displayedButtons: ComponentType<ExpandableButtonProps>[] = []; |
| 211 | const hiddenButtons: ComponentType<ExpandableButtonProps>[] = []; |
| 212 | SECONDARY_BUTTONS.forEach((button, i) => { |
| 213 | if (i < SECONDARY_BUTTONS.length - hiddenStackCount) { |
| 214 | displayedButtons.push(button); |
| 215 | } else { |
| 216 | hiddenButtons.push(button); |
| 217 | } |
| 218 | }); |
| 219 | |
| 220 | return { |
| 221 | displayedButtons, |
| 222 | hiddenButtons, |
| 223 | }; |
| 224 | }, [hiddenStackCount]); |
| 225 | |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 226 | return ( |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 227 | <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}> |
| 228 | <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}> |
| 229 | {initialMeasurementDone && |
| 230 | displayedButtons.map((SecondaryButton, i) => ( |
| 231 | <Fragment key={i}> |
| 232 | <SecondaryButton hidden={false} /> |
| 233 | </Fragment> |
| 234 | ))} |
| 235 | {(!!hiddenButtons.length || !initialMeasurementDone) && ( |
| 236 | <CallingMoreVerticalButton hidden={true} onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} /> |
| 237 | )} |
| 238 | </Stack> |
| 239 | |
| 240 | {!!hiddenButtons.length && hiddenMenuVisible && ( |
| 241 | <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}> |
| 242 | <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 243 | <Stack |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 244 | direction="column" |
| 245 | justifyContent="flex-end" |
| 246 | alignItems="flex-end" |
| 247 | sx={{ bottom: 0, right: 0, height: '100%' }} |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 248 | > |
| 249 | {hiddenButtons.map((SecondaryButton, i) => ( |
| 250 | <Fragment key={i}> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 251 | <SecondaryButton hidden={true} /> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 252 | </Fragment> |
| 253 | ))} |
| 254 | </Stack> |
| 255 | </Card> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 256 | </Box> |
| 257 | )} |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 258 | </Card> |
| 259 | ); |
| 260 | }; |