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