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 | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 19 | import { useCallback, useContext, useEffect, useMemo, useState } from 'react'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 20 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 21 | import { createOptionalContext } from '../hooks/createOptionalContext'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 22 | import { ConversationMember } from '../models/conversation-member'; |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 23 | import { callTimeoutMs } from '../utils/constants'; |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 24 | import { AsyncSetState, SetState, WithChildren } from '../utils/utils'; |
idillon | cf42c65 | 2023-01-31 18:56:17 -0500 | [diff] [blame] | 25 | import { useWebRtcManager } from '../webrtc/WebRtcManager'; |
| 26 | import { useAuthContext } from './AuthProvider'; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 27 | import { CallData, CallManagerContext } from './CallManagerProvider'; |
| 28 | import ConditionalContextProvider from './ConditionalContextProvider'; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame^] | 29 | import { IWebSocketContext, useWebSocketContext } 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 | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 35 | Loading, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 36 | Ringing, |
| 37 | Connecting, |
| 38 | InCall, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 39 | PermissionsDenied, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 40 | } |
| 41 | |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 42 | export enum VideoStatus { |
| 43 | Off, |
| 44 | Camera, |
| 45 | ScreenShare, |
| 46 | } |
| 47 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 48 | type MediaDeviceIdState = { |
| 49 | id: string | undefined; |
| 50 | setId: (id: string | undefined) => void | Promise<void>; |
| 51 | }; |
| 52 | type CurrentMediaDeviceIds = Record<MediaDeviceKind, MediaDeviceIdState>; |
| 53 | |
idillon | cf42c65 | 2023-01-31 18:56:17 -0500 | [diff] [blame] | 54 | export type MediaDevicesInfo = Record<MediaDeviceKind, MediaDeviceInfo[]>; |
| 55 | export type MediaInputKind = 'audio' | 'video'; |
| 56 | export type MediaInputIds = Record<MediaInputKind, string | false | undefined>; |
| 57 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 58 | export interface ICallContext { |
idillon | cf42c65 | 2023-01-31 18:56:17 -0500 | [diff] [blame] | 59 | localStream: MediaStream | undefined; |
| 60 | screenShareLocalStream: MediaStream | undefined; |
| 61 | remoteStreams: readonly MediaStream[]; |
| 62 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 63 | mediaDevices: MediaDevicesInfo; |
| 64 | currentMediaDeviceIds: CurrentMediaDeviceIds; |
| 65 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 66 | isAudioOn: boolean; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 67 | setIsAudioOn: SetState<boolean>; |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 68 | videoStatus: VideoStatus; |
| 69 | updateVideoStatus: AsyncSetState<VideoStatus>; |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 70 | isChatShown: boolean; |
| 71 | setIsChatShown: SetState<boolean>; |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 72 | isFullscreen: boolean; |
| 73 | setIsFullscreen: SetState<boolean>; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 74 | callRole: CallRole; |
| 75 | callStatus: CallStatus; |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 76 | callStartTime: number | undefined; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 77 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 78 | acceptCall: (withVideoOn: boolean) => void; |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 79 | endCall: () => void; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 80 | } |
| 81 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 82 | const optionalCallContext = createOptionalContext<ICallContext>('CallContext'); |
| 83 | export const useCallContext = optionalCallContext.useOptionalContext; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 84 | |
| 85 | export default ({ children }: WithChildren) => { |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame^] | 86 | const webSocket = useWebSocketContext(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 87 | const { callMembers, callData, exitCall } = useContext(CallManagerContext); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 88 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 89 | const dependencies = useMemo( |
| 90 | () => ({ |
| 91 | webSocket, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 92 | callMembers, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 93 | callData, |
| 94 | exitCall, |
| 95 | conversationId: callData?.conversationId, |
| 96 | }), |
idillon | cf42c65 | 2023-01-31 18:56:17 -0500 | [diff] [blame] | 97 | [webSocket, callMembers, callData, exitCall] |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 98 | ); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 99 | |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 100 | return ( |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 101 | <ConditionalContextProvider |
| 102 | Context={optionalCallContext.Context} |
| 103 | initialValue={undefined} |
| 104 | dependencies={dependencies} |
| 105 | useProviderValue={CallProvider} |
| 106 | > |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 107 | {children} |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 108 | </ConditionalContextProvider> |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 109 | ); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | const CallProvider = ({ |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 113 | callMembers, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 114 | callData, |
| 115 | exitCall, |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 116 | conversationId, |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 117 | webSocket, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 118 | }: { |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 119 | webSocket: IWebSocketContext; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 120 | callMembers: ConversationMember[]; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 121 | callData: CallData; |
| 122 | exitCall: () => void; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 123 | conversationId: string; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 124 | }): ICallContext => { |
idillon | cf42c65 | 2023-01-31 18:56:17 -0500 | [diff] [blame] | 125 | const [localStream, setLocalStream] = useState<MediaStream>(); |
| 126 | const [screenShareLocalStream, setScreenShareLocalStream] = useState<MediaStream>(); |
| 127 | const { account } = useAuthContext(); |
| 128 | const webRtcManager = useWebRtcManager(); |
| 129 | |
| 130 | // TODO: This logic will have to change to support multiple people in a call. Could we move this logic to the server? |
| 131 | // The client could make a single request with the conversationId, and the server would be tasked with sending |
| 132 | // all the individual requests to the members of the conversation. |
| 133 | const contactUri = callMembers[0]?.contact.uri; |
| 134 | const connectionInfos = webRtcManager.connectionsInfos[contactUri]; |
| 135 | const remoteStreams = connectionInfos?.remoteStreams; |
| 136 | const iceConnectionState = connectionInfos?.iceConnectionState; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 137 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 138 | const [mediaDevices, setMediaDevices] = useState<MediaDevicesInfo>({ |
| 139 | audioinput: [], |
| 140 | audiooutput: [], |
| 141 | videoinput: [], |
| 142 | }); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 143 | const [audioInputDeviceId, setAudioInputDeviceId] = useState<string>(); |
| 144 | const [audioOutputDeviceId, setAudioOutputDeviceId] = useState<string>(); |
| 145 | const [videoDeviceId, setVideoDeviceId] = useState<string>(); |
| 146 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 147 | const [isAudioOn, setIsAudioOn] = useState(false); |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 148 | const [videoStatus, setVideoStatus] = useState(VideoStatus.Off); |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 149 | const [isChatShown, setIsChatShown] = useState(false); |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 150 | const [isFullscreen, setIsFullscreen] = useState(false); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 151 | const [callStatus, setCallStatus] = useState(CallStatus.Default); |
| 152 | const [callRole] = useState(callData?.role); |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 153 | const [callStartTime, setCallStartTime] = useState<number | undefined>(undefined); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 154 | |
idillon | cf42c65 | 2023-01-31 18:56:17 -0500 | [diff] [blame] | 155 | // TODO: Replace this by a callback |
| 156 | useEffect(() => { |
| 157 | if (callData.role === 'receiver' && contactUri && localStream) { |
| 158 | webRtcManager.addConnection(webSocket, account, contactUri, callData, localStream, screenShareLocalStream); |
| 159 | } |
| 160 | }, [account, callData, contactUri, localStream, screenShareLocalStream, webRtcManager, webSocket]); |
| 161 | |
| 162 | const getMediaDevices = useCallback(async (): Promise<MediaDevicesInfo> => { |
| 163 | try { |
| 164 | const devices = await navigator.mediaDevices.enumerateDevices(); |
| 165 | |
| 166 | // TODO: On Firefox, some devices can sometime be duplicated (2 devices can share the same deviceId). Using a map |
| 167 | // and then converting it to an array makes it so that there is no duplicate. If we find a way to prevent |
| 168 | // Firefox from listing 2 devices with the same deviceId, we can remove this logic. |
| 169 | const newMediaDevices: Record<MediaDeviceKind, Record<string, MediaDeviceInfo>> = { |
| 170 | audioinput: {}, |
| 171 | audiooutput: {}, |
| 172 | videoinput: {}, |
| 173 | }; |
| 174 | |
| 175 | for (const device of devices) { |
| 176 | newMediaDevices[device.kind][device.deviceId] = device; |
| 177 | } |
| 178 | |
| 179 | return { |
| 180 | audioinput: Object.values(newMediaDevices.audioinput), |
| 181 | audiooutput: Object.values(newMediaDevices.audiooutput), |
| 182 | videoinput: Object.values(newMediaDevices.videoinput), |
| 183 | }; |
| 184 | } catch (e) { |
| 185 | throw new Error('Could not get media devices', { cause: e }); |
| 186 | } |
| 187 | }, []); |
| 188 | |
| 189 | const updateLocalStream = useCallback( |
| 190 | async (mediaDeviceIds?: MediaInputIds) => { |
| 191 | const devices = await getMediaDevices(); |
| 192 | |
| 193 | let audioConstraint: MediaTrackConstraints | boolean = devices.audioinput.length !== 0; |
| 194 | let videoConstraint: MediaTrackConstraints | boolean = devices.videoinput.length !== 0; |
| 195 | |
| 196 | if (!audioConstraint && !videoConstraint) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | if (mediaDeviceIds?.audio !== undefined) { |
| 201 | audioConstraint = mediaDeviceIds.audio !== false ? { deviceId: mediaDeviceIds.audio } : false; |
| 202 | } |
| 203 | if (mediaDeviceIds?.video !== undefined) { |
| 204 | videoConstraint = mediaDeviceIds.video !== false ? { deviceId: mediaDeviceIds.video } : false; |
| 205 | } |
| 206 | |
| 207 | try { |
| 208 | const stream = await navigator.mediaDevices.getUserMedia({ |
| 209 | audio: audioConstraint, |
| 210 | video: videoConstraint, |
| 211 | }); |
| 212 | |
| 213 | for (const track of stream.getTracks()) { |
| 214 | track.enabled = false; |
| 215 | } |
| 216 | |
| 217 | setLocalStream(stream); |
| 218 | } catch (e) { |
| 219 | throw new Error('Could not get media devices', { cause: e }); |
| 220 | } |
| 221 | }, |
| 222 | [getMediaDevices] |
| 223 | ); |
| 224 | |
| 225 | const updateScreenShare = useCallback( |
| 226 | async (isOn: boolean) => { |
| 227 | if (isOn) { |
| 228 | const stream = await navigator.mediaDevices.getDisplayMedia({ |
| 229 | video: true, |
| 230 | audio: false, |
| 231 | }); |
| 232 | |
| 233 | setScreenShareLocalStream(stream); |
| 234 | return stream; |
| 235 | } else { |
| 236 | if (screenShareLocalStream) { |
| 237 | for (const track of screenShareLocalStream.getTracks()) { |
| 238 | track.stop(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | setScreenShareLocalStream(undefined); |
| 243 | } |
| 244 | }, |
| 245 | [screenShareLocalStream] |
| 246 | ); |
| 247 | |
| 248 | // TODO: Transform the effect into a callback |
| 249 | const updateLocalStreams = webRtcManager.updateLocalStreams; |
| 250 | useEffect(() => { |
| 251 | if ((!localStream && !screenShareLocalStream) || !updateLocalStreams) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | updateLocalStreams(localStream, screenShareLocalStream); |
| 256 | }, [localStream, screenShareLocalStream, updateLocalStreams]); |
| 257 | |
| 258 | const sendWebRtcOffer = useCallback(async () => { |
| 259 | if (contactUri) { |
| 260 | webRtcManager.addConnection(webSocket, account, contactUri, callData, localStream, screenShareLocalStream); |
| 261 | } |
| 262 | }, [account, callData, contactUri, localStream, screenShareLocalStream, webRtcManager, webSocket]); |
| 263 | |
| 264 | const closeConnection = useCallback(() => { |
| 265 | const stopStream = (stream: MediaStream) => { |
| 266 | const localTracks = stream.getTracks(); |
| 267 | if (localTracks) { |
| 268 | for (const track of localTracks) { |
| 269 | track.stop(); |
| 270 | } |
| 271 | } |
| 272 | }; |
| 273 | |
| 274 | if (localStream) { |
| 275 | stopStream(localStream); |
| 276 | } |
| 277 | if (screenShareLocalStream) { |
| 278 | stopStream(screenShareLocalStream); |
| 279 | } |
| 280 | |
| 281 | webRtcManager.clean(); |
| 282 | }, [localStream, screenShareLocalStream, webRtcManager]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 283 | |
| 284 | useEffect(() => { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 285 | if (callStatus !== CallStatus.InCall) { |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | const updateMediaDevices = async () => { |
| 290 | try { |
| 291 | const newMediaDevices = await getMediaDevices(); |
| 292 | |
| 293 | if (newMediaDevices.audiooutput.length !== 0 && !audioOutputDeviceId) { |
| 294 | setAudioOutputDeviceId(newMediaDevices.audiooutput[0].deviceId); |
| 295 | } |
| 296 | |
| 297 | setMediaDevices(newMediaDevices); |
| 298 | } catch (e) { |
| 299 | console.error('Could not update media devices:', e); |
| 300 | } |
| 301 | }; |
| 302 | |
| 303 | navigator.mediaDevices.addEventListener('devicechange', updateMediaDevices); |
| 304 | updateMediaDevices(); |
| 305 | |
| 306 | return () => { |
| 307 | navigator.mediaDevices.removeEventListener('devicechange', updateMediaDevices); |
| 308 | }; |
| 309 | }, [callStatus, getMediaDevices, audioOutputDeviceId]); |
| 310 | |
| 311 | useEffect(() => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 312 | if (localStream) { |
| 313 | for (const track of localStream.getAudioTracks()) { |
| 314 | track.enabled = isAudioOn; |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 315 | const deviceId = track.getSettings().deviceId; |
| 316 | if (deviceId) { |
| 317 | setAudioInputDeviceId(deviceId); |
| 318 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 319 | } |
simon | feaa1db | 2022-11-26 20:13:18 -0500 | [diff] [blame] | 320 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 321 | }, [isAudioOn, localStream]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 322 | |
| 323 | useEffect(() => { |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 324 | if (localStream) { |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 325 | for (const track of localStream.getVideoTracks()) { |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 326 | track.enabled = videoStatus === VideoStatus.Camera; |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 327 | const deviceId = track.getSettings().deviceId; |
| 328 | if (deviceId) { |
| 329 | setVideoDeviceId(deviceId); |
| 330 | } |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 331 | } |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 332 | } |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 333 | }, [videoStatus, localStream]); |
| 334 | |
| 335 | const updateVideoStatus = useCallback( |
| 336 | async (newStatus: ((prevState: VideoStatus) => VideoStatus) | VideoStatus) => { |
| 337 | if (typeof newStatus === 'function') { |
| 338 | newStatus = newStatus(videoStatus); |
| 339 | } |
| 340 | |
| 341 | const stream = await updateScreenShare(newStatus === VideoStatus.ScreenShare); |
| 342 | if (stream) { |
| 343 | for (const track of stream.getTracks()) { |
| 344 | track.addEventListener('ended', () => { |
| 345 | console.warn('Browser ended screen sharing'); |
| 346 | updateVideoStatus(VideoStatus.Off); |
| 347 | }); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | setVideoStatus(newStatus); |
| 352 | }, |
| 353 | [videoStatus, updateScreenShare] |
| 354 | ); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 355 | |
| 356 | useEffect(() => { |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 357 | const onFullscreenChange = () => { |
| 358 | setIsFullscreen(document.fullscreenElement !== null); |
| 359 | }; |
| 360 | |
| 361 | document.addEventListener('fullscreenchange', onFullscreenChange); |
| 362 | return () => { |
| 363 | document.removeEventListener('fullscreenchange', onFullscreenChange); |
| 364 | }; |
| 365 | }, []); |
| 366 | |
| 367 | useEffect(() => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 368 | if (callRole === 'caller' && callStatus === CallStatus.Default) { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 369 | const withVideoOn = callData?.withVideoOn ?? false; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 370 | setCallStatus(CallStatus.Loading); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 371 | updateLocalStream() |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 372 | .then(() => { |
| 373 | const callBegin: CallBegin = { |
| 374 | contactId: contactUri, |
| 375 | conversationId, |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 376 | withVideoOn, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | setCallStatus(CallStatus.Ringing); |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 380 | setVideoStatus(withVideoOn ? VideoStatus.Camera : VideoStatus.Off); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 381 | console.info('Sending CallBegin', callBegin); |
| 382 | webSocket.send(WebSocketMessageType.CallBegin, callBegin); |
| 383 | }) |
| 384 | .catch((e) => { |
| 385 | console.error(e); |
| 386 | setCallStatus(CallStatus.PermissionsDenied); |
| 387 | }); |
| 388 | } |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 389 | }, [webSocket, updateLocalStream, callRole, callStatus, contactUri, conversationId, callData]); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 390 | |
| 391 | const acceptCall = useCallback( |
| 392 | (withVideoOn: boolean) => { |
| 393 | setCallStatus(CallStatus.Loading); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 394 | updateLocalStream() |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 395 | .then(() => { |
| 396 | const callAccept: CallAction = { |
| 397 | contactId: contactUri, |
| 398 | conversationId, |
| 399 | }; |
| 400 | |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 401 | setVideoStatus(withVideoOn ? VideoStatus.Camera : VideoStatus.Off); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 402 | setCallStatus(CallStatus.Connecting); |
| 403 | console.info('Sending CallAccept', callAccept); |
| 404 | webSocket.send(WebSocketMessageType.CallAccept, callAccept); |
| 405 | }) |
| 406 | .catch((e) => { |
| 407 | console.error(e); |
| 408 | setCallStatus(CallStatus.PermissionsDenied); |
| 409 | }); |
| 410 | }, |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 411 | [webSocket, updateLocalStream, contactUri, conversationId] |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 412 | ); |
| 413 | |
| 414 | useEffect(() => { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 415 | if (callRole === 'caller' && callStatus === CallStatus.Ringing) { |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 416 | const callAcceptListener = (data: CallAction) => { |
| 417 | console.info('Received event on CallAccept', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 418 | if (data.conversationId !== conversationId) { |
| 419 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 420 | return; |
| 421 | } |
| 422 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 423 | setCallStatus(CallStatus.Connecting); |
| 424 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 425 | sendWebRtcOffer(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | webSocket.bind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 429 | |
| 430 | return () => { |
| 431 | webSocket.unbind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 432 | }; |
| 433 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 434 | }, [callRole, webSocket, sendWebRtcOffer, callStatus, conversationId]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 435 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 436 | const endCall = useCallback(() => { |
| 437 | const callEnd: CallAction = { |
| 438 | contactId: contactUri, |
| 439 | conversationId, |
| 440 | }; |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 441 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 442 | console.info('Sending CallEnd', callEnd); |
| 443 | closeConnection(); |
| 444 | webSocket.send(WebSocketMessageType.CallEnd, callEnd); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 445 | exitCall(); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 446 | // TODO: write in chat that the call ended |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 447 | }, [webSocket, contactUri, conversationId, closeConnection, exitCall]); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 448 | |
| 449 | useEffect(() => { |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 450 | const callEndListener = (data: CallAction) => { |
| 451 | console.info('Received event on CallEnd', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 452 | if (data.conversationId !== conversationId) { |
| 453 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 454 | return; |
| 455 | } |
| 456 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 457 | closeConnection(); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 458 | exitCall(); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 459 | // TODO: write in chat that the call ended |
| 460 | }; |
| 461 | |
| 462 | webSocket.bind(WebSocketMessageType.CallEnd, callEndListener); |
| 463 | return () => { |
| 464 | webSocket.unbind(WebSocketMessageType.CallEnd, callEndListener); |
| 465 | }; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 466 | }, [webSocket, exitCall, conversationId, closeConnection]); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 467 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 468 | useEffect(() => { |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 469 | if ( |
| 470 | callStatus === CallStatus.Connecting && |
| 471 | (iceConnectionState === 'connected' || iceConnectionState === 'completed') |
| 472 | ) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 473 | console.info('Changing call status to InCall'); |
| 474 | setCallStatus(CallStatus.InCall); |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 475 | setCallStartTime(Date.now()); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 476 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 477 | }, [iceConnectionState, callStatus]); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 478 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 479 | useEffect(() => { |
Charlie | 380dc5e | 2022-11-29 16:51:42 -0500 | [diff] [blame] | 480 | if (iceConnectionState === 'disconnected' || iceConnectionState === 'failed') { |
| 481 | console.info('ICE connection disconnected or failed, ending call'); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 482 | endCall(); |
| 483 | } |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 484 | }, [iceConnectionState, callStatus, videoStatus, endCall]); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 485 | |
| 486 | useEffect(() => { |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 487 | const checkStatusTimeout = () => { |
| 488 | if (callStatus !== CallStatus.InCall) { |
| 489 | endCall(); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 490 | } |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 491 | }; |
| 492 | const timeoutId = setTimeout(checkStatusTimeout, callTimeoutMs); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 493 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 494 | return () => { |
| 495 | clearTimeout(timeoutId); |
| 496 | }; |
| 497 | }, [callStatus, endCall]); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 498 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 499 | const currentMediaDeviceIds: CurrentMediaDeviceIds = useMemo(() => { |
| 500 | const createSetIdForDeviceKind = (mediaInputKind: MediaInputKind) => async (id: string | undefined) => { |
| 501 | const mediaDeviceIds = { |
| 502 | audio: audioInputDeviceId, |
| 503 | video: videoDeviceId, |
| 504 | }; |
| 505 | |
| 506 | mediaDeviceIds[mediaInputKind] = id; |
| 507 | |
| 508 | await updateLocalStream(mediaDeviceIds); |
| 509 | }; |
| 510 | |
| 511 | return { |
| 512 | audioinput: { |
| 513 | id: audioInputDeviceId, |
| 514 | setId: createSetIdForDeviceKind('audio'), |
| 515 | }, |
| 516 | audiooutput: { |
| 517 | id: audioOutputDeviceId, |
| 518 | setId: setAudioOutputDeviceId, |
| 519 | }, |
| 520 | videoinput: { |
| 521 | id: videoDeviceId, |
| 522 | setId: createSetIdForDeviceKind('video'), |
| 523 | }, |
| 524 | }; |
| 525 | }, [updateLocalStream, audioInputDeviceId, audioOutputDeviceId, videoDeviceId]); |
| 526 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 527 | return useMemo( |
| 528 | () => ({ |
idillon | cf42c65 | 2023-01-31 18:56:17 -0500 | [diff] [blame] | 529 | localStream, |
| 530 | screenShareLocalStream, |
| 531 | remoteStreams, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 532 | mediaDevices, |
| 533 | currentMediaDeviceIds, |
| 534 | isAudioOn, |
| 535 | setIsAudioOn, |
| 536 | videoStatus, |
| 537 | updateVideoStatus, |
| 538 | isChatShown, |
| 539 | setIsChatShown, |
| 540 | isFullscreen, |
| 541 | setIsFullscreen, |
| 542 | callRole, |
| 543 | callStatus, |
| 544 | callStartTime, |
| 545 | acceptCall, |
| 546 | endCall, |
| 547 | }), |
| 548 | [ |
idillon | cf42c65 | 2023-01-31 18:56:17 -0500 | [diff] [blame] | 549 | localStream, |
| 550 | screenShareLocalStream, |
| 551 | remoteStreams, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 552 | mediaDevices, |
| 553 | currentMediaDeviceIds, |
| 554 | isAudioOn, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 555 | videoStatus, |
| 556 | updateVideoStatus, |
| 557 | isChatShown, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 558 | isFullscreen, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 559 | callRole, |
| 560 | callStatus, |
| 561 | callStartTime, |
| 562 | acceptCall, |
| 563 | endCall, |
| 564 | ] |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 565 | ); |
| 566 | }; |