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