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