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