simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [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 | */ |
| 18 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 19 | import { Box, CircularProgress, Grid, IconButtonProps, Stack, Typography } from '@mui/material'; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 20 | import { ComponentType, ReactNode, useEffect, useMemo, useRef } from 'react'; |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 21 | import { useTranslation } from 'react-i18next'; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 22 | import { useLocation } from 'react-router-dom'; |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 23 | |
| 24 | import { |
| 25 | CallingAnswerAudioButton, |
| 26 | CallingAnswerVideoButton, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 27 | CallingCancelButton, |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 28 | CallingRefuseButton, |
| 29 | } from '../components/CallButtons'; |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 30 | import ConversationAvatar from '../components/ConversationAvatar'; |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 31 | import { useCallManagerContext } from '../contexts/CallManagerProvider'; |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 32 | import { useConversationContext } from '../contexts/ConversationProvider'; |
idillon | 27dab02 | 2023-02-02 17:55:47 -0500 | [diff] [blame] | 33 | import { useUserMediaContext } from '../contexts/UserMediaProvider'; |
idillon | c45a43d | 2023-02-10 18:12:10 -0500 | [diff] [blame] | 34 | import { CallStatus } from '../services/CallManager'; |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 35 | import { VideoElementWithSinkId } from '../utils/utils'; |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 36 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 37 | export const CallPending = () => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 38 | const { conversationDisplayName } = useConversationContext(); |
idillon | 27dab02 | 2023-02-02 17:55:47 -0500 | [diff] [blame] | 39 | const { localStream } = useUserMediaContext(); |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 40 | const { callData } = useCallManagerContext(); |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 41 | const localVideoRef = useRef<VideoElementWithSinkId | null>(null); |
Misha Krieger-Raynauld | abc80e5 | 2022-11-29 19:44:41 -0500 | [diff] [blame] | 42 | |
| 43 | useEffect(() => { |
| 44 | if (localStream && localVideoRef.current) { |
| 45 | localVideoRef.current.srcObject = localStream; |
| 46 | } |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 47 | }, [localStream, localVideoRef]); |
Misha Krieger-Raynauld | abc80e5 | 2022-11-29 19:44:41 -0500 | [diff] [blame] | 48 | |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 49 | return ( |
| 50 | <Stack |
| 51 | direction="column" |
| 52 | justifyContent="center" |
| 53 | alignItems="center" |
| 54 | height="100%" |
| 55 | spacing={4} |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 56 | flexGrow={1} |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 57 | sx={{ |
Misha Krieger-Raynauld | abc80e5 | 2022-11-29 19:44:41 -0500 | [diff] [blame] | 58 | position: 'relative', |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 59 | }} |
| 60 | > |
Misha Krieger-Raynauld | abc80e5 | 2022-11-29 19:44:41 -0500 | [diff] [blame] | 61 | <video |
| 62 | ref={localVideoRef} |
| 63 | autoPlay |
| 64 | muted |
| 65 | style={{ |
| 66 | width: '100%', |
| 67 | height: '100%', |
| 68 | position: 'absolute', |
| 69 | objectFit: 'cover', |
| 70 | backgroundColor: 'black', |
| 71 | zIndex: -1, |
| 72 | }} |
| 73 | /> |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 74 | <Box |
| 75 | sx={{ |
| 76 | position: 'relative', |
| 77 | display: 'flex', |
| 78 | alignItems: 'center', |
| 79 | justifyContent: 'center', |
| 80 | width: '100%', |
| 81 | height: '30%', |
| 82 | }} |
| 83 | > |
| 84 | <Box |
| 85 | sx={{ |
| 86 | aspectRatio: '1', |
| 87 | height: '100%', |
| 88 | position: 'absolute', |
| 89 | }} |
| 90 | > |
| 91 | <CircularProgress |
| 92 | disableShrink |
| 93 | thickness={1} |
| 94 | size="100%" |
| 95 | sx={{ |
| 96 | position: 'absolute', |
| 97 | color: 'white', |
| 98 | zIndex: 1, |
| 99 | }} |
| 100 | /> |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 101 | <ConversationAvatar |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 102 | alt="contact profile picture" |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 103 | displayName={conversationDisplayName} |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 104 | style={{ |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 105 | width: '100%', |
| 106 | height: '100%', |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 107 | }} |
| 108 | /> |
| 109 | </Box> |
| 110 | </Box> |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 111 | {callData?.role === 'caller' ? <CallPendingCallerInterface /> : <CallPendingReceiverInterface />} |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 112 | </Stack> |
| 113 | ); |
| 114 | }; |
| 115 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 116 | const CallPendingDetails = ({ |
| 117 | title, |
| 118 | buttons, |
| 119 | }: { |
| 120 | title: ReactNode; |
| 121 | buttons: { |
| 122 | ButtonComponent: ComponentType<IconButtonProps>; |
| 123 | title: ReactNode; |
| 124 | }[]; |
| 125 | }) => { |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 126 | return ( |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 127 | <> |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 128 | <Typography variant="h1" color="white"> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 129 | {title} |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 130 | </Typography> |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 131 | <Box width="50%"> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 132 | <Grid container justifyContent="center"> |
| 133 | {buttons.map(({ ButtonComponent, title: buttonTitle }, i) => ( |
| 134 | <Grid item key={i} xs={4}> |
| 135 | <Stack direction="column" alignItems="center" spacing={1} sx={{}}> |
| 136 | <ButtonComponent color="inherit" size="large" /> |
| 137 | <Typography variant="body2" color="white" sx={{ opacity: 0.75 }}> |
| 138 | {buttonTitle} |
| 139 | </Typography> |
| 140 | </Stack> |
| 141 | </Grid> |
| 142 | ))} |
| 143 | </Grid> |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 144 | </Box> |
| 145 | </> |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 146 | ); |
| 147 | }; |
| 148 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 149 | export const CallPendingCallerInterface = () => { |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 150 | const { callStatus } = useCallManagerContext(); |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 151 | const { t } = useTranslation(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 152 | const { members } = useConversationContext(); |
| 153 | const memberName = useMemo(() => members[0].getDisplayName(), [members]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 154 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 155 | let title = t('loading'); |
| 156 | |
| 157 | switch (callStatus) { |
| 158 | case CallStatus.Ringing: |
| 159 | title = t('calling', { |
| 160 | member0: memberName, |
| 161 | }); |
| 162 | break; |
| 163 | case CallStatus.Connecting: |
| 164 | title = t('connecting'); |
| 165 | break; |
| 166 | } |
| 167 | |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 168 | return ( |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 169 | <CallPendingDetails |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 170 | title={title} |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 171 | buttons={[ |
| 172 | { |
| 173 | ButtonComponent: CallingCancelButton, |
| 174 | title: t('end_call'), |
| 175 | }, |
| 176 | ]} |
| 177 | /> |
| 178 | ); |
| 179 | }; |
| 180 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 181 | export const CallPendingReceiverInterface = () => { |
| 182 | const { state } = useLocation(); |
idillon | 255682e | 2023-02-06 13:25:26 -0500 | [diff] [blame] | 183 | const { callStatus } = useCallManagerContext(); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 184 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 185 | const { t } = useTranslation(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 186 | const { members } = useConversationContext(); |
| 187 | const memberName = useMemo(() => members[0].getDisplayName(), [members]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 188 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 189 | let title = t('loading'); |
| 190 | |
| 191 | switch (callStatus) { |
| 192 | case CallStatus.Ringing: |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 193 | case CallStatus.Default: |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 194 | title = t('incoming_call', { |
| 195 | context: state?.isVideoOn ? 'video' : 'audio', |
| 196 | member0: memberName, |
| 197 | }); |
| 198 | break; |
| 199 | case CallStatus.Connecting: |
| 200 | title = t('connecting'); |
| 201 | break; |
| 202 | } |
| 203 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 204 | return ( |
| 205 | <CallPendingDetails |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 206 | title={title} |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 207 | buttons={[ |
| 208 | { |
| 209 | ButtonComponent: CallingRefuseButton, |
| 210 | title: t('refuse_call'), |
| 211 | }, |
| 212 | { |
| 213 | ButtonComponent: CallingAnswerAudioButton, |
| 214 | title: t('accept_call_audio'), |
| 215 | }, |
| 216 | { |
| 217 | ButtonComponent: CallingAnswerVideoButton, |
| 218 | title: t('accept_call_video'), |
| 219 | }, |
| 220 | ]} |
| 221 | /> |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 222 | ); |
| 223 | }; |