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