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