simon | f929a36 | 2022-11-18 16:53:45 -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 | */ |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 18 | import { CallAction, CallBegin, WebSocketMessageType } from 'jami-web-common'; |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 19 | import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 20 | import { Navigate, useNavigate } from 'react-router-dom'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 21 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 22 | import LoadingPage from '../components/Loading'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 23 | import { useUrlParams } from '../hooks/useUrlParams'; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 24 | import CallPermissionDenied from '../pages/CallPermissionDenied'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 25 | import { CallRouteParams } from '../router'; |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 26 | import { callTimeoutMs } from '../utils/constants'; |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 27 | import { SetState, WithChildren } from '../utils/utils'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 28 | import { ConversationContext } from './ConversationProvider'; |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 29 | import { MediaDevicesInfo, MediaInputKind, WebRtcContext } from './WebRtcProvider'; |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 30 | import { IWebSocketContext, WebSocketContext } from './WebSocketProvider'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 31 | |
| 32 | export type CallRole = 'caller' | 'receiver'; |
| 33 | |
| 34 | export enum CallStatus { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 35 | Default, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 36 | Loading, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 37 | Ringing, |
| 38 | Connecting, |
| 39 | InCall, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 40 | PermissionsDenied, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 41 | } |
| 42 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 43 | type MediaDeviceIdState = { |
| 44 | id: string | undefined; |
| 45 | setId: (id: string | undefined) => void | Promise<void>; |
| 46 | }; |
| 47 | type CurrentMediaDeviceIds = Record<MediaDeviceKind, MediaDeviceIdState>; |
| 48 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 49 | export interface ICallContext { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 50 | mediaDevices: MediaDevicesInfo; |
| 51 | currentMediaDeviceIds: CurrentMediaDeviceIds; |
| 52 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 53 | isAudioOn: boolean; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 54 | setIsAudioOn: SetState<boolean>; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 55 | isVideoOn: boolean; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 56 | setIsVideoOn: SetState<boolean>; |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 57 | isChatShown: boolean; |
| 58 | setIsChatShown: SetState<boolean>; |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 59 | isFullscreen: boolean; |
| 60 | setIsFullscreen: SetState<boolean>; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 61 | callRole: CallRole; |
| 62 | callStatus: CallStatus; |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 63 | callStartTime: number | undefined; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 64 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 65 | acceptCall: (withVideoOn: boolean) => void; |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 66 | endCall: () => void; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | const defaultCallContext: ICallContext = { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 70 | mediaDevices: { |
| 71 | audioinput: [], |
| 72 | audiooutput: [], |
| 73 | videoinput: [], |
| 74 | }, |
| 75 | currentMediaDeviceIds: { |
| 76 | audioinput: { |
| 77 | id: undefined, |
| 78 | setId: async () => {}, |
| 79 | }, |
| 80 | audiooutput: { |
| 81 | id: undefined, |
| 82 | setId: async () => {}, |
| 83 | }, |
| 84 | videoinput: { |
| 85 | id: undefined, |
| 86 | setId: async () => {}, |
| 87 | }, |
| 88 | }, |
| 89 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 90 | isAudioOn: false, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 91 | setIsAudioOn: () => {}, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 92 | isVideoOn: false, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 93 | setIsVideoOn: () => {}, |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 94 | isChatShown: false, |
| 95 | setIsChatShown: () => {}, |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 96 | isFullscreen: false, |
| 97 | setIsFullscreen: () => {}, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 98 | callRole: 'caller', |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 99 | callStatus: CallStatus.Default, |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 100 | callStartTime: undefined, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 101 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 102 | acceptCall: (_: boolean) => {}, |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 103 | endCall: () => {}, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | export const CallContext = createContext<ICallContext>(defaultCallContext); |
| 107 | |
| 108 | export default ({ children }: WithChildren) => { |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 109 | const webSocket = useContext(WebSocketContext); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 110 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 111 | if (!webSocket) { |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 112 | return <LoadingPage />; |
| 113 | } |
| 114 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 115 | return <CallProvider webSocket={webSocket}>{children}</CallProvider>; |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | const CallProvider = ({ |
| 119 | children, |
| 120 | webSocket, |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 121 | }: WithChildren & { |
| 122 | webSocket: IWebSocketContext; |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 123 | }) => { |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 124 | const { state: routeState } = useUrlParams<CallRouteParams>(); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 125 | const { localStream, sendWebRtcOffer, iceConnectionState, closeConnection, getMediaDevices, updateLocalStream } = |
| 126 | useContext(WebRtcContext); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 127 | const { conversationId, conversation } = useContext(ConversationContext); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 128 | const navigate = useNavigate(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 129 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 130 | const [mediaDevices, setMediaDevices] = useState(defaultCallContext.mediaDevices); |
| 131 | const [audioInputDeviceId, setAudioInputDeviceId] = useState<string>(); |
| 132 | const [audioOutputDeviceId, setAudioOutputDeviceId] = useState<string>(); |
| 133 | const [videoDeviceId, setVideoDeviceId] = useState<string>(); |
| 134 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 135 | const [isAudioOn, setIsAudioOn] = useState(false); |
| 136 | const [isVideoOn, setIsVideoOn] = useState(false); |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 137 | const [isChatShown, setIsChatShown] = useState(false); |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 138 | const [isFullscreen, setIsFullscreen] = useState(false); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 139 | const [callStatus, setCallStatus] = useState(routeState?.callStatus); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 140 | const [callRole] = useState(routeState?.role); |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 141 | const [callStartTime, setCallStartTime] = useState<number | undefined>(undefined); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 142 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 143 | // TODO: This logic will have to change to support multiple people in a call. Could we move this logic to the server? |
| 144 | // The client could make a single request with the conversationId, and the server would be tasked with sending |
| 145 | // all the individual requests to the members of the conversation. |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 146 | const contactUri = useMemo(() => conversation.getFirstMember().contact.getUri(), [conversation]); |
| 147 | |
| 148 | useEffect(() => { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 149 | if (callStatus !== CallStatus.InCall) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | const updateMediaDevices = async () => { |
| 154 | try { |
| 155 | const newMediaDevices = await getMediaDevices(); |
| 156 | |
| 157 | if (newMediaDevices.audiooutput.length !== 0 && !audioOutputDeviceId) { |
| 158 | setAudioOutputDeviceId(newMediaDevices.audiooutput[0].deviceId); |
| 159 | } |
| 160 | |
| 161 | setMediaDevices(newMediaDevices); |
| 162 | } catch (e) { |
| 163 | console.error('Could not update media devices:', e); |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | navigator.mediaDevices.addEventListener('devicechange', updateMediaDevices); |
| 168 | updateMediaDevices(); |
| 169 | |
| 170 | return () => { |
| 171 | navigator.mediaDevices.removeEventListener('devicechange', updateMediaDevices); |
| 172 | }; |
| 173 | }, [callStatus, getMediaDevices, audioOutputDeviceId]); |
| 174 | |
| 175 | useEffect(() => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 176 | if (localStream) { |
| 177 | for (const track of localStream.getAudioTracks()) { |
| 178 | track.enabled = isAudioOn; |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 179 | const deviceId = track.getSettings().deviceId; |
| 180 | if (deviceId) { |
| 181 | setAudioInputDeviceId(deviceId); |
| 182 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 183 | } |
simon | feaa1db | 2022-11-26 20:13:18 -0500 | [diff] [blame] | 184 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 185 | }, [isAudioOn, localStream]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 186 | |
| 187 | useEffect(() => { |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 188 | if (localStream) { |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 189 | for (const track of localStream.getVideoTracks()) { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 190 | track.enabled = isVideoOn; |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 191 | const deviceId = track.getSettings().deviceId; |
| 192 | if (deviceId) { |
| 193 | setVideoDeviceId(deviceId); |
| 194 | } |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 195 | } |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 196 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 197 | }, [isVideoOn, localStream]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 198 | |
| 199 | useEffect(() => { |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 200 | const onFullscreenChange = () => { |
| 201 | setIsFullscreen(document.fullscreenElement !== null); |
| 202 | }; |
| 203 | |
| 204 | document.addEventListener('fullscreenchange', onFullscreenChange); |
| 205 | return () => { |
| 206 | document.removeEventListener('fullscreenchange', onFullscreenChange); |
| 207 | }; |
| 208 | }, []); |
| 209 | |
| 210 | useEffect(() => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 211 | if (callRole === 'caller' && callStatus === CallStatus.Default) { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 212 | const withVideoOn = routeState?.isVideoOn ?? false; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 213 | setCallStatus(CallStatus.Loading); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 214 | updateLocalStream() |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 215 | .then(() => { |
| 216 | const callBegin: CallBegin = { |
| 217 | contactId: contactUri, |
| 218 | conversationId, |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 219 | withVideoOn, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | setCallStatus(CallStatus.Ringing); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 223 | setIsVideoOn(withVideoOn); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 224 | console.info('Sending CallBegin', callBegin); |
| 225 | webSocket.send(WebSocketMessageType.CallBegin, callBegin); |
| 226 | }) |
| 227 | .catch((e) => { |
| 228 | console.error(e); |
| 229 | setCallStatus(CallStatus.PermissionsDenied); |
| 230 | }); |
| 231 | } |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 232 | }, [webSocket, updateLocalStream, callRole, callStatus, contactUri, conversationId, routeState]); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 233 | |
| 234 | const acceptCall = useCallback( |
| 235 | (withVideoOn: boolean) => { |
| 236 | setCallStatus(CallStatus.Loading); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 237 | updateLocalStream() |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 238 | .then(() => { |
| 239 | const callAccept: CallAction = { |
| 240 | contactId: contactUri, |
| 241 | conversationId, |
| 242 | }; |
| 243 | |
| 244 | setIsVideoOn(withVideoOn); |
| 245 | setCallStatus(CallStatus.Connecting); |
| 246 | console.info('Sending CallAccept', callAccept); |
| 247 | webSocket.send(WebSocketMessageType.CallAccept, callAccept); |
| 248 | }) |
| 249 | .catch((e) => { |
| 250 | console.error(e); |
| 251 | setCallStatus(CallStatus.PermissionsDenied); |
| 252 | }); |
| 253 | }, |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 254 | [webSocket, updateLocalStream, contactUri, conversationId] |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 255 | ); |
| 256 | |
| 257 | useEffect(() => { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 258 | if (callRole === 'caller' && callStatus === CallStatus.Ringing) { |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 259 | const callAcceptListener = (data: CallAction) => { |
| 260 | console.info('Received event on CallAccept', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 261 | if (data.conversationId !== conversationId) { |
| 262 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 263 | return; |
| 264 | } |
| 265 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 266 | setCallStatus(CallStatus.Connecting); |
| 267 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 268 | sendWebRtcOffer(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | webSocket.bind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 272 | |
| 273 | return () => { |
| 274 | webSocket.unbind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 275 | }; |
| 276 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 277 | }, [callRole, webSocket, sendWebRtcOffer, callStatus, conversationId]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 278 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 279 | const endCall = useCallback(() => { |
| 280 | const callEnd: CallAction = { |
| 281 | contactId: contactUri, |
| 282 | conversationId, |
| 283 | }; |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 284 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 285 | console.info('Sending CallEnd', callEnd); |
| 286 | closeConnection(); |
| 287 | webSocket.send(WebSocketMessageType.CallEnd, callEnd); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 288 | navigate(`/conversation/${conversationId}`); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 289 | // TODO: write in chat that the call ended |
| 290 | }, [webSocket, contactUri, conversationId, closeConnection, navigate]); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 291 | |
| 292 | useEffect(() => { |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 293 | const callEndListener = (data: CallAction) => { |
| 294 | console.info('Received event on CallEnd', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 295 | if (data.conversationId !== conversationId) { |
| 296 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 297 | return; |
| 298 | } |
| 299 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 300 | closeConnection(); |
| 301 | navigate(`/conversation/${conversationId}`); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 302 | // TODO: write in chat that the call ended |
| 303 | }; |
| 304 | |
| 305 | webSocket.bind(WebSocketMessageType.CallEnd, callEndListener); |
| 306 | return () => { |
| 307 | webSocket.unbind(WebSocketMessageType.CallEnd, callEndListener); |
| 308 | }; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 309 | }, [webSocket, navigate, conversationId, closeConnection]); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 310 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 311 | useEffect(() => { |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 312 | if ( |
| 313 | callStatus === CallStatus.Connecting && |
| 314 | (iceConnectionState === 'connected' || iceConnectionState === 'completed') |
| 315 | ) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 316 | console.info('Changing call status to InCall'); |
| 317 | setCallStatus(CallStatus.InCall); |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 318 | setCallStartTime(Date.now()); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 319 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 320 | }, [iceConnectionState, callStatus]); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 321 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 322 | useEffect(() => { |
Charlie | 380dc5e | 2022-11-29 16:51:42 -0500 | [diff] [blame] | 323 | if (iceConnectionState === 'disconnected' || iceConnectionState === 'failed') { |
| 324 | console.info('ICE connection disconnected or failed, ending call'); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 325 | endCall(); |
| 326 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 327 | }, [iceConnectionState, callStatus, isVideoOn, endCall]); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 328 | |
| 329 | useEffect(() => { |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 330 | const checkStatusTimeout = () => { |
| 331 | if (callStatus !== CallStatus.InCall) { |
| 332 | endCall(); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 333 | } |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 334 | }; |
| 335 | const timeoutId = setTimeout(checkStatusTimeout, callTimeoutMs); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 336 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 337 | return () => { |
| 338 | clearTimeout(timeoutId); |
| 339 | }; |
| 340 | }, [callStatus, endCall]); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 341 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 342 | const currentMediaDeviceIds: CurrentMediaDeviceIds = useMemo(() => { |
| 343 | const createSetIdForDeviceKind = (mediaInputKind: MediaInputKind) => async (id: string | undefined) => { |
| 344 | const mediaDeviceIds = { |
| 345 | audio: audioInputDeviceId, |
| 346 | video: videoDeviceId, |
| 347 | }; |
| 348 | |
| 349 | mediaDeviceIds[mediaInputKind] = id; |
| 350 | |
| 351 | await updateLocalStream(mediaDeviceIds); |
| 352 | }; |
| 353 | |
| 354 | return { |
| 355 | audioinput: { |
| 356 | id: audioInputDeviceId, |
| 357 | setId: createSetIdForDeviceKind('audio'), |
| 358 | }, |
| 359 | audiooutput: { |
| 360 | id: audioOutputDeviceId, |
| 361 | setId: setAudioOutputDeviceId, |
| 362 | }, |
| 363 | videoinput: { |
| 364 | id: videoDeviceId, |
| 365 | setId: createSetIdForDeviceKind('video'), |
| 366 | }, |
| 367 | }; |
| 368 | }, [updateLocalStream, audioInputDeviceId, audioOutputDeviceId, videoDeviceId]); |
| 369 | |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 370 | useEffect(() => { |
| 371 | navigate('.', { |
| 372 | replace: true, |
| 373 | state: {}, |
| 374 | }); |
| 375 | }, [navigate]); |
| 376 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 377 | if (!callRole || callStatus === undefined) { |
| 378 | console.error('Invalid route. Redirecting...'); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 379 | return <Navigate to={'/'} />; |
| 380 | } |
| 381 | |
| 382 | return ( |
| 383 | <CallContext.Provider |
| 384 | value={{ |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 385 | mediaDevices, |
| 386 | currentMediaDeviceIds, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 387 | isAudioOn, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 388 | setIsAudioOn, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 389 | isVideoOn, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 390 | setIsVideoOn, |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 391 | isChatShown, |
| 392 | setIsChatShown, |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 393 | isFullscreen, |
| 394 | setIsFullscreen, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 395 | callRole, |
| 396 | callStatus, |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 397 | callStartTime, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 398 | acceptCall, |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 399 | endCall, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 400 | }} |
| 401 | > |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 402 | {callStatus === CallStatus.PermissionsDenied ? <CallPermissionDenied /> : children} |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 403 | </CallContext.Provider> |
| 404 | ); |
| 405 | }; |