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