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 | */ |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 18 | import { CallAction, WebSocketMessageType } from 'jami-web-common'; |
| 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 | |
| 22 | import { useUrlParams } from '../hooks/useUrlParams'; |
| 23 | import { CallRouteParams } from '../router'; |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 24 | import { SetState, WithChildren } from '../utils/utils'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 25 | import { ConversationContext } from './ConversationProvider'; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 26 | import { WebRtcContext } from './WebRtcProvider'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 27 | import { WebSocketContext } from './WebSocketProvider'; |
| 28 | |
| 29 | export type CallRole = 'caller' | 'receiver'; |
| 30 | |
| 31 | export enum CallStatus { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 32 | Default, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 33 | Ringing, |
| 34 | Connecting, |
| 35 | InCall, |
| 36 | } |
| 37 | |
| 38 | export interface ICallContext { |
| 39 | mediaDevices: Record<MediaDeviceKind, MediaDeviceInfo[]>; |
| 40 | |
| 41 | localStream: MediaStream | undefined; |
| 42 | remoteStream: MediaStream | undefined; // TODO: should be an array of participants. find a way to map MediaStream id to contactid https://stackoverflow.com/a/68663155/6592293 |
| 43 | |
| 44 | isAudioOn: boolean; |
| 45 | setAudioStatus: (isOn: boolean) => void; |
| 46 | isVideoOn: boolean; |
| 47 | setVideoStatus: (isOn: boolean) => void; |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 48 | isChatShown: boolean; |
| 49 | setIsChatShown: SetState<boolean>; |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 50 | isFullscreen: boolean; |
| 51 | setIsFullscreen: SetState<boolean>; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 52 | callRole: CallRole; |
| 53 | callStatus: CallStatus; |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 54 | callStartTime: Date | undefined; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 55 | |
| 56 | acceptCall: () => void; |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame^] | 57 | endCall: () => void; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | const defaultCallContext: ICallContext = { |
| 61 | mediaDevices: { |
| 62 | audioinput: [], |
| 63 | audiooutput: [], |
| 64 | videoinput: [], |
| 65 | }, |
| 66 | |
| 67 | localStream: undefined, |
| 68 | remoteStream: undefined, |
| 69 | |
| 70 | isAudioOn: false, |
| 71 | setAudioStatus: () => {}, |
| 72 | isVideoOn: false, |
| 73 | setVideoStatus: () => {}, |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 74 | isChatShown: false, |
| 75 | setIsChatShown: () => {}, |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 76 | isFullscreen: false, |
| 77 | setIsFullscreen: () => {}, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 78 | callRole: 'caller', |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 79 | callStatus: CallStatus.Default, |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 80 | callStartTime: undefined, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 81 | |
| 82 | acceptCall: () => {}, |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame^] | 83 | endCall: () => {}, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | export const CallContext = createContext<ICallContext>(defaultCallContext); |
| 87 | |
| 88 | export default ({ children }: WithChildren) => { |
| 89 | const { |
| 90 | queryParams: { role: callRole }, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 91 | state: routeState, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 92 | } = useUrlParams<CallRouteParams>(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 93 | const webSocket = useContext(WebSocketContext); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 94 | const { webRtcConnection, remoteStreams, sendWebRtcOffer, isConnected } = useContext(WebRtcContext); |
| 95 | const { conversationId, conversation } = useContext(ConversationContext); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame^] | 96 | const navigate = useNavigate(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 97 | |
| 98 | const [mediaDevices, setMediaDevices] = useState<Record<MediaDeviceKind, MediaDeviceInfo[]>>( |
| 99 | defaultCallContext.mediaDevices |
| 100 | ); |
| 101 | const [localStream, setLocalStream] = useState<MediaStream>(); |
| 102 | |
| 103 | const [isAudioOn, setIsAudioOn] = useState(false); |
| 104 | const [isVideoOn, setIsVideoOn] = useState(false); |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 105 | const [isChatShown, setIsChatShown] = useState(false); |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 106 | const [isFullscreen, setIsFullscreen] = useState(false); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 107 | const [callStatus, setCallStatus] = useState(routeState?.callStatus); |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 108 | const [callStartTime, setCallStartTime] = useState<Date | undefined>(undefined); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 109 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 110 | // TODO: This logic will have to change to support multiple people in a call. Could we move this logic to the server? |
| 111 | // The client could make a single request with the conversationId, and the server would be tasked with sending |
| 112 | // all the individual requests to the members of the conversation. |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 113 | const contactUri = useMemo(() => conversation.getFirstMember().contact.getUri(), [conversation]); |
| 114 | |
| 115 | useEffect(() => { |
simon | feaa1db | 2022-11-26 20:13:18 -0500 | [diff] [blame] | 116 | if (!isConnected) { |
| 117 | return; |
| 118 | } |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 119 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 120 | try { |
simon | feaa1db | 2022-11-26 20:13:18 -0500 | [diff] [blame] | 121 | // TODO: Wait until status is `InCall` before getting devices |
| 122 | navigator.mediaDevices.enumerateDevices().then((devices) => { |
| 123 | const newMediaDevices: Record<MediaDeviceKind, MediaDeviceInfo[]> = { |
| 124 | audioinput: [], |
| 125 | audiooutput: [], |
| 126 | videoinput: [], |
| 127 | }; |
| 128 | |
| 129 | for (const device of devices) { |
| 130 | newMediaDevices[device.kind].push(device); |
| 131 | } |
| 132 | |
| 133 | setMediaDevices(newMediaDevices); |
| 134 | }); |
| 135 | } catch (e) { |
| 136 | console.error('Could not get media devices:', e); |
| 137 | } |
| 138 | |
| 139 | try { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 140 | navigator.mediaDevices |
| 141 | .getUserMedia({ |
| 142 | audio: true, // TODO: Set both to false by default |
| 143 | video: true, |
| 144 | }) |
| 145 | .then((stream) => { |
| 146 | for (const track of stream.getTracks()) { |
| 147 | // TODO: Set default from isVideoOn and isMicOn values |
| 148 | track.enabled = false; |
| 149 | } |
| 150 | setLocalStream(stream); |
| 151 | }); |
| 152 | } catch (e) { |
| 153 | // TODO: Better handle user denial |
| 154 | console.error('Could not get media devices:', e); |
| 155 | } |
simon | feaa1db | 2022-11-26 20:13:18 -0500 | [diff] [blame] | 156 | }, [isConnected]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 157 | |
| 158 | useEffect(() => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 159 | if (localStream && webRtcConnection) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 160 | for (const track of localStream.getTracks()) { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 161 | webRtcConnection.addTrack(track, localStream); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 162 | } |
| 163 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 164 | }, [localStream, webRtcConnection]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 165 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 166 | useEffect(() => { |
| 167 | if (!webSocket) { |
| 168 | return; |
| 169 | } |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 170 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 171 | if (callRole === 'caller' && callStatus === CallStatus.Default) { |
| 172 | const callBegin: CallAction = { |
| 173 | contactId: contactUri, |
| 174 | conversationId, |
| 175 | }; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 176 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 177 | console.info('Sending CallBegin', callBegin); |
| 178 | webSocket.send(WebSocketMessageType.CallBegin, callBegin); |
| 179 | setCallStatus(CallStatus.Ringing); |
| 180 | } |
| 181 | }, [webSocket, callRole, callStatus, contactUri, conversationId]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 182 | |
| 183 | useEffect(() => { |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 184 | const onFullscreenChange = () => { |
| 185 | setIsFullscreen(document.fullscreenElement !== null); |
| 186 | }; |
| 187 | |
| 188 | document.addEventListener('fullscreenchange', onFullscreenChange); |
| 189 | return () => { |
| 190 | document.removeEventListener('fullscreenchange', onFullscreenChange); |
| 191 | }; |
| 192 | }, []); |
| 193 | |
| 194 | useEffect(() => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 195 | if (!webSocket || !webRtcConnection) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
| 199 | if (callRole === 'caller' && callStatus === CallStatus.Ringing) { |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame^] | 200 | const callAcceptListener = (data: CallAction) => { |
| 201 | console.info('Received event on CallAccept', data); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 202 | setCallStatus(CallStatus.Connecting); |
| 203 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 204 | webRtcConnection |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 205 | .createOffer({ |
| 206 | offerToReceiveAudio: true, |
| 207 | offerToReceiveVideo: true, |
| 208 | }) |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 209 | .then((sdp) => { |
| 210 | sendWebRtcOffer(sdp); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 211 | }); |
| 212 | }; |
| 213 | |
| 214 | webSocket.bind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 215 | |
| 216 | return () => { |
| 217 | webSocket.unbind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 218 | }; |
| 219 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 220 | }, [callRole, webSocket, webRtcConnection, sendWebRtcOffer, callStatus]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 221 | |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame^] | 222 | const quitCall = useCallback(() => { |
| 223 | if (!webRtcConnection) { |
| 224 | throw new Error('Could not quit call: webRtcConnection is not defined'); |
| 225 | } |
| 226 | |
| 227 | webRtcConnection.close(); |
| 228 | navigate(`/conversation/${conversationId}`); |
| 229 | }, [webRtcConnection, navigate, conversationId]); |
| 230 | |
| 231 | useEffect(() => { |
| 232 | if (!webSocket) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | const callEndListener = (data: CallAction) => { |
| 237 | console.info('Received event on CallEnd', data); |
| 238 | quitCall(); |
| 239 | // TODO: write in chat that the call ended |
| 240 | }; |
| 241 | |
| 242 | webSocket.bind(WebSocketMessageType.CallEnd, callEndListener); |
| 243 | return () => { |
| 244 | webSocket.unbind(WebSocketMessageType.CallEnd, callEndListener); |
| 245 | }; |
| 246 | }, [webSocket, navigate, conversationId, quitCall]); |
| 247 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 248 | useEffect(() => { |
| 249 | if (callStatus === CallStatus.Connecting && isConnected) { |
| 250 | console.info('Changing call status to InCall'); |
| 251 | setCallStatus(CallStatus.InCall); |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 252 | setCallStartTime(new Date()); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 253 | } |
| 254 | }, [isConnected, callStatus]); |
| 255 | |
| 256 | const acceptCall = useCallback(() => { |
| 257 | if (!webSocket) { |
| 258 | throw new Error('Could not accept call'); |
| 259 | } |
| 260 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 261 | const callAccept: CallAction = { |
| 262 | contactId: contactUri, |
| 263 | conversationId, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | console.info('Sending CallAccept', callAccept); |
| 267 | webSocket.send(WebSocketMessageType.CallAccept, callAccept); |
| 268 | setCallStatus(CallStatus.Connecting); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 269 | }, [webSocket, contactUri, conversationId]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 270 | |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame^] | 271 | const endCall = useCallback(() => { |
| 272 | if (!webSocket) { |
| 273 | throw new Error('Could not end call'); |
| 274 | } |
| 275 | |
| 276 | const callEnd: CallAction = { |
| 277 | contactId: contactUri, |
| 278 | conversationId, |
| 279 | }; |
| 280 | |
| 281 | console.info('Sending CallEnd', callEnd); |
| 282 | webSocket.send(WebSocketMessageType.CallEnd, callEnd); |
| 283 | quitCall(); |
| 284 | // TODO: write in chat that the call ended |
| 285 | }, [webSocket, contactUri, conversationId, quitCall]); |
| 286 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 287 | const setAudioStatus = useCallback( |
| 288 | (isOn: boolean) => { |
| 289 | if (!localStream) { |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | for (const track of localStream.getAudioTracks()) { |
| 294 | track.enabled = isOn; |
| 295 | } |
| 296 | |
| 297 | setIsAudioOn(isOn); |
| 298 | }, |
| 299 | [localStream] |
| 300 | ); |
| 301 | |
| 302 | const setVideoStatus = useCallback( |
| 303 | (isOn: boolean) => { |
| 304 | if (!localStream) { |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | for (const track of localStream.getVideoTracks()) { |
| 309 | track.enabled = isOn; |
| 310 | } |
| 311 | |
| 312 | setIsVideoOn(isOn); |
| 313 | }, |
| 314 | [localStream] |
| 315 | ); |
| 316 | |
| 317 | if (!callRole || callStatus === undefined) { |
| 318 | console.error('Invalid route. Redirecting...'); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 319 | return <Navigate to={'/'} />; |
| 320 | } |
| 321 | |
| 322 | return ( |
| 323 | <CallContext.Provider |
| 324 | value={{ |
| 325 | mediaDevices, |
| 326 | localStream, |
| 327 | remoteStream: remoteStreams?.at(-1), |
| 328 | isAudioOn, |
| 329 | setAudioStatus, |
| 330 | isVideoOn, |
| 331 | setVideoStatus, |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 332 | isChatShown, |
| 333 | setIsChatShown, |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 334 | isFullscreen, |
| 335 | setIsFullscreen, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 336 | callRole, |
| 337 | callStatus, |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 338 | callStartTime, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 339 | acceptCall, |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame^] | 340 | endCall, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 341 | }} |
| 342 | > |
| 343 | {children} |
| 344 | </CallContext.Provider> |
| 345 | ); |
| 346 | }; |