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 | }) => { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 113 | const { |
| 114 | queryParams: { role: callRole }, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 115 | state: routeState, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 116 | } = useUrlParams<CallRouteParams>(); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 117 | const { remoteStreams, sendWebRtcOffer, isConnected } = useContext(WebRtcContext); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 118 | const { conversationId, conversation } = useContext(ConversationContext); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 119 | const navigate = useNavigate(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 120 | |
| 121 | const [mediaDevices, setMediaDevices] = useState<Record<MediaDeviceKind, MediaDeviceInfo[]>>( |
| 122 | defaultCallContext.mediaDevices |
| 123 | ); |
| 124 | const [localStream, setLocalStream] = useState<MediaStream>(); |
| 125 | |
| 126 | const [isAudioOn, setIsAudioOn] = useState(false); |
| 127 | const [isVideoOn, setIsVideoOn] = useState(false); |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 128 | const [isChatShown, setIsChatShown] = useState(false); |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 129 | const [isFullscreen, setIsFullscreen] = useState(false); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 130 | const [callStatus, setCallStatus] = useState(routeState?.callStatus); |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 131 | const [callStartTime, setCallStartTime] = useState<Date | undefined>(undefined); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 132 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 133 | // TODO: This logic will have to change to support multiple people in a call. Could we move this logic to the server? |
| 134 | // The client could make a single request with the conversationId, and the server would be tasked with sending |
| 135 | // all the individual requests to the members of the conversation. |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 136 | const contactUri = useMemo(() => conversation.getFirstMember().contact.getUri(), [conversation]); |
| 137 | |
| 138 | useEffect(() => { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 139 | try { |
simon | feaa1db | 2022-11-26 20:13:18 -0500 | [diff] [blame] | 140 | // TODO: Wait until status is `InCall` before getting devices |
| 141 | navigator.mediaDevices.enumerateDevices().then((devices) => { |
| 142 | const newMediaDevices: Record<MediaDeviceKind, MediaDeviceInfo[]> = { |
| 143 | audioinput: [], |
| 144 | audiooutput: [], |
| 145 | videoinput: [], |
| 146 | }; |
| 147 | |
| 148 | for (const device of devices) { |
| 149 | newMediaDevices[device.kind].push(device); |
| 150 | } |
| 151 | |
| 152 | setMediaDevices(newMediaDevices); |
| 153 | }); |
| 154 | } catch (e) { |
| 155 | console.error('Could not get media devices:', e); |
| 156 | } |
| 157 | |
| 158 | try { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 159 | navigator.mediaDevices |
| 160 | .getUserMedia({ |
| 161 | audio: true, // TODO: Set both to false by default |
| 162 | video: true, |
| 163 | }) |
| 164 | .then((stream) => { |
| 165 | for (const track of stream.getTracks()) { |
| 166 | // TODO: Set default from isVideoOn and isMicOn values |
| 167 | track.enabled = false; |
| 168 | } |
| 169 | setLocalStream(stream); |
| 170 | }); |
| 171 | } catch (e) { |
| 172 | // TODO: Better handle user denial |
| 173 | console.error('Could not get media devices:', e); |
| 174 | } |
simon | 8b4756f | 2022-11-27 17:15:50 -0500 | [diff] [blame] | 175 | }, []); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 176 | |
| 177 | useEffect(() => { |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 178 | if (localStream) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 179 | for (const track of localStream.getTracks()) { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 180 | webRtcConnection.addTrack(track, localStream); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 181 | } |
| 182 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 183 | }, [localStream, webRtcConnection]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 184 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 185 | const setAudioStatus = useCallback( |
| 186 | (isOn: boolean) => { |
| 187 | if (!localStream) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | for (const track of localStream.getAudioTracks()) { |
| 192 | track.enabled = isOn; |
| 193 | } |
| 194 | |
| 195 | setIsAudioOn(isOn); |
| 196 | }, |
| 197 | [localStream] |
| 198 | ); |
| 199 | |
| 200 | const setVideoStatus = useCallback( |
| 201 | (isOn: boolean) => { |
| 202 | if (!localStream) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | for (const track of localStream.getVideoTracks()) { |
| 207 | track.enabled = isOn; |
| 208 | } |
| 209 | |
| 210 | setIsVideoOn(isOn); |
| 211 | }, |
| 212 | [localStream] |
| 213 | ); |
| 214 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 215 | useEffect(() => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 216 | if (callRole === 'caller' && callStatus === CallStatus.Default) { |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 217 | const callBegin: CallBegin = { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 218 | contactId: contactUri, |
| 219 | conversationId, |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 220 | withVideoOn: routeState?.isVideoOn ?? false, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 221 | }; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 222 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 223 | console.info('Sending CallBegin', callBegin); |
| 224 | webSocket.send(WebSocketMessageType.CallBegin, callBegin); |
| 225 | setCallStatus(CallStatus.Ringing); |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 226 | setIsVideoOn(routeState?.isVideoOn ?? false); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 227 | } |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 228 | }, [webSocket, callRole, callStatus, contactUri, conversationId, routeState]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 229 | |
| 230 | useEffect(() => { |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 231 | const onFullscreenChange = () => { |
| 232 | setIsFullscreen(document.fullscreenElement !== null); |
| 233 | }; |
| 234 | |
| 235 | document.addEventListener('fullscreenchange', onFullscreenChange); |
| 236 | return () => { |
| 237 | document.removeEventListener('fullscreenchange', onFullscreenChange); |
| 238 | }; |
| 239 | }, []); |
| 240 | |
| 241 | useEffect(() => { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 242 | if (callRole === 'caller' && callStatus === CallStatus.Ringing) { |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 243 | const callAcceptListener = (data: CallAction) => { |
| 244 | console.info('Received event on CallAccept', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 245 | if (data.conversationId !== conversationId) { |
| 246 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 247 | return; |
| 248 | } |
| 249 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 250 | setCallStatus(CallStatus.Connecting); |
| 251 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 252 | webRtcConnection |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 253 | .createOffer({ |
| 254 | offerToReceiveAudio: true, |
| 255 | offerToReceiveVideo: true, |
| 256 | }) |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 257 | .then((sdp) => { |
| 258 | sendWebRtcOffer(sdp); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 259 | }); |
| 260 | }; |
| 261 | |
| 262 | webSocket.bind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 263 | |
| 264 | return () => { |
| 265 | webSocket.unbind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 266 | }; |
| 267 | } |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 268 | }, [callRole, webSocket, webRtcConnection, sendWebRtcOffer, callStatus, conversationId]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 269 | |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 270 | const quitCall = useCallback(() => { |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 271 | const localTracks = localStream?.getTracks(); |
| 272 | if (localTracks) { |
| 273 | for (const track of localTracks) { |
| 274 | track.stop(); |
| 275 | } |
| 276 | } |
| 277 | |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 278 | webRtcConnection.close(); |
| 279 | navigate(`/conversation/${conversationId}`); |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 280 | }, [webRtcConnection, localStream, navigate, conversationId]); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 281 | |
| 282 | useEffect(() => { |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 283 | const callEndListener = (data: CallAction) => { |
| 284 | console.info('Received event on CallEnd', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 285 | if (data.conversationId !== conversationId) { |
| 286 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 287 | return; |
| 288 | } |
| 289 | |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 290 | quitCall(); |
| 291 | // TODO: write in chat that the call ended |
| 292 | }; |
| 293 | |
| 294 | webSocket.bind(WebSocketMessageType.CallEnd, callEndListener); |
| 295 | return () => { |
| 296 | webSocket.unbind(WebSocketMessageType.CallEnd, callEndListener); |
| 297 | }; |
| 298 | }, [webSocket, navigate, conversationId, quitCall]); |
| 299 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 300 | useEffect(() => { |
| 301 | if (callStatus === CallStatus.Connecting && isConnected) { |
| 302 | console.info('Changing call status to InCall'); |
| 303 | setCallStatus(CallStatus.InCall); |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 304 | setVideoStatus(isVideoOn); |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 305 | setCallStartTime(new Date()); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 306 | } |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 307 | }, [isConnected, callStatus, setVideoStatus, isVideoOn]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 308 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 309 | const acceptCall = useCallback( |
| 310 | (withVideoOn: boolean) => { |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 311 | const callAccept: CallAction = { |
| 312 | contactId: contactUri, |
| 313 | conversationId, |
| 314 | }; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 315 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 316 | console.info('Sending CallAccept', callAccept); |
| 317 | webSocket.send(WebSocketMessageType.CallAccept, callAccept); |
| 318 | setIsVideoOn(withVideoOn); |
| 319 | setCallStatus(CallStatus.Connecting); |
| 320 | }, |
| 321 | [webSocket, contactUri, conversationId] |
| 322 | ); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 323 | |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 324 | const endCall = useCallback(() => { |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 325 | const callEnd: CallAction = { |
| 326 | contactId: contactUri, |
| 327 | conversationId, |
| 328 | }; |
| 329 | |
| 330 | console.info('Sending CallEnd', callEnd); |
| 331 | webSocket.send(WebSocketMessageType.CallEnd, callEnd); |
| 332 | quitCall(); |
| 333 | // TODO: write in chat that the call ended |
| 334 | }, [webSocket, contactUri, conversationId, quitCall]); |
| 335 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 336 | useEffect(() => { |
| 337 | const checkStatusTimeout = () => { |
| 338 | if (callStatus !== CallStatus.InCall) { |
| 339 | endCall(); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 340 | } |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 341 | }; |
| 342 | const timeoutId = setTimeout(checkStatusTimeout, callTimeoutMs); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 343 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 344 | return () => { |
| 345 | clearTimeout(timeoutId); |
| 346 | }; |
| 347 | }, [callStatus, endCall]); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 348 | |
| 349 | if (!callRole || callStatus === undefined) { |
| 350 | console.error('Invalid route. Redirecting...'); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 351 | return <Navigate to={'/'} />; |
| 352 | } |
| 353 | |
| 354 | return ( |
| 355 | <CallContext.Provider |
| 356 | value={{ |
| 357 | mediaDevices, |
| 358 | localStream, |
| 359 | remoteStream: remoteStreams?.at(-1), |
| 360 | isAudioOn, |
| 361 | setAudioStatus, |
| 362 | isVideoOn, |
| 363 | setVideoStatus, |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 364 | isChatShown, |
| 365 | setIsChatShown, |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 366 | isFullscreen, |
| 367 | setIsFullscreen, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 368 | callRole, |
| 369 | callStatus, |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 370 | callStartTime, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 371 | acceptCall, |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 372 | endCall, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 373 | }} |
| 374 | > |
| 375 | {children} |
| 376 | </CallContext.Provider> |
| 377 | ); |
| 378 | }; |