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 | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 20 | import { Navigate } from 'react-router-dom'; |
| 21 | |
| 22 | import { useUrlParams } from '../hooks/useUrlParams'; |
| 23 | import { CallRouteParams } from '../router'; |
| 24 | import { 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; |
| 48 | callRole: CallRole; |
| 49 | callStatus: CallStatus; |
| 50 | |
| 51 | acceptCall: () => void; |
| 52 | } |
| 53 | |
| 54 | const defaultCallContext: ICallContext = { |
| 55 | mediaDevices: { |
| 56 | audioinput: [], |
| 57 | audiooutput: [], |
| 58 | videoinput: [], |
| 59 | }, |
| 60 | |
| 61 | localStream: undefined, |
| 62 | remoteStream: undefined, |
| 63 | |
| 64 | isAudioOn: false, |
| 65 | setAudioStatus: () => {}, |
| 66 | isVideoOn: false, |
| 67 | setVideoStatus: () => {}, |
| 68 | callRole: 'caller', |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame^] | 69 | callStatus: CallStatus.Default, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 70 | |
| 71 | acceptCall: () => {}, |
| 72 | }; |
| 73 | |
| 74 | export const CallContext = createContext<ICallContext>(defaultCallContext); |
| 75 | |
| 76 | export default ({ children }: WithChildren) => { |
| 77 | const { |
| 78 | queryParams: { role: callRole }, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame^] | 79 | state: routeState, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 80 | } = useUrlParams<CallRouteParams>(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 81 | const webSocket = useContext(WebSocketContext); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 82 | const { webRtcConnection, remoteStreams, sendWebRtcOffer, isConnected } = useContext(WebRtcContext); |
| 83 | const { conversationId, conversation } = useContext(ConversationContext); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 84 | |
| 85 | const [mediaDevices, setMediaDevices] = useState<Record<MediaDeviceKind, MediaDeviceInfo[]>>( |
| 86 | defaultCallContext.mediaDevices |
| 87 | ); |
| 88 | const [localStream, setLocalStream] = useState<MediaStream>(); |
| 89 | |
| 90 | const [isAudioOn, setIsAudioOn] = useState(false); |
| 91 | const [isVideoOn, setIsVideoOn] = useState(false); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame^] | 92 | const [callStatus, setCallStatus] = useState(routeState?.callStatus); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 93 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame^] | 94 | // TODO: This logic will have to change to support multiple people in a call. Could we move this logic to the server? |
| 95 | // The client could make a single request with the conversationId, and the server would be tasked with sending |
| 96 | // all the individual requests to the members of the conversation. |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 97 | const contactUri = useMemo(() => conversation.getFirstMember().contact.getUri(), [conversation]); |
| 98 | |
| 99 | useEffect(() => { |
| 100 | // TODO: Wait until status is `InCall` before getting devices |
| 101 | navigator.mediaDevices.enumerateDevices().then((devices) => { |
| 102 | const newMediaDevices: Record<MediaDeviceKind, MediaDeviceInfo[]> = { |
| 103 | audioinput: [], |
| 104 | audiooutput: [], |
| 105 | videoinput: [], |
| 106 | }; |
| 107 | |
| 108 | for (const device of devices) { |
| 109 | newMediaDevices[device.kind].push(device); |
| 110 | } |
| 111 | |
| 112 | setMediaDevices(newMediaDevices); |
| 113 | }); |
| 114 | }, []); |
| 115 | |
| 116 | useEffect(() => { |
| 117 | // TODO: Only ask media permission once the call has been accepted |
| 118 | try { |
| 119 | // TODO: When toggling mute on/off, the camera flickers |
| 120 | // https://git.jami.net/savoirfairelinux/jami-web/-/issues/90 |
| 121 | navigator.mediaDevices |
| 122 | .getUserMedia({ |
| 123 | audio: true, // TODO: Set both to false by default |
| 124 | video: true, |
| 125 | }) |
| 126 | .then((stream) => { |
| 127 | for (const track of stream.getTracks()) { |
| 128 | // TODO: Set default from isVideoOn and isMicOn values |
| 129 | track.enabled = false; |
| 130 | } |
| 131 | setLocalStream(stream); |
| 132 | }); |
| 133 | } catch (e) { |
| 134 | // TODO: Better handle user denial |
| 135 | console.error('Could not get media devices:', e); |
| 136 | } |
| 137 | }, [setLocalStream]); |
| 138 | |
| 139 | useEffect(() => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 140 | if (localStream && webRtcConnection) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 141 | for (const track of localStream.getTracks()) { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 142 | webRtcConnection.addTrack(track, localStream); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 143 | } |
| 144 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 145 | }, [localStream, webRtcConnection]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 146 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame^] | 147 | useEffect(() => { |
| 148 | if (!webSocket) { |
| 149 | return; |
| 150 | } |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 151 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame^] | 152 | if (callRole === 'caller' && callStatus === CallStatus.Default) { |
| 153 | const callBegin: CallAction = { |
| 154 | contactId: contactUri, |
| 155 | conversationId, |
| 156 | }; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 157 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame^] | 158 | console.info('Sending CallBegin', callBegin); |
| 159 | webSocket.send(WebSocketMessageType.CallBegin, callBegin); |
| 160 | setCallStatus(CallStatus.Ringing); |
| 161 | } |
| 162 | }, [webSocket, callRole, callStatus, contactUri, conversationId]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 163 | |
| 164 | useEffect(() => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 165 | if (!webSocket || !webRtcConnection) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
| 169 | if (callRole === 'caller' && callStatus === CallStatus.Ringing) { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 170 | const callAcceptListener = (_data: CallAction) => { |
| 171 | console.info('Received event on CallAccept'); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 172 | setCallStatus(CallStatus.Connecting); |
| 173 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 174 | webRtcConnection |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 175 | .createOffer({ |
| 176 | offerToReceiveAudio: true, |
| 177 | offerToReceiveVideo: true, |
| 178 | }) |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 179 | .then((sdp) => { |
| 180 | sendWebRtcOffer(sdp); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 181 | }); |
| 182 | }; |
| 183 | |
| 184 | webSocket.bind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 185 | |
| 186 | return () => { |
| 187 | webSocket.unbind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 188 | }; |
| 189 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 190 | }, [callRole, webSocket, webRtcConnection, sendWebRtcOffer, callStatus]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 191 | |
| 192 | useEffect(() => { |
| 193 | if (callStatus === CallStatus.Connecting && isConnected) { |
| 194 | console.info('Changing call status to InCall'); |
| 195 | setCallStatus(CallStatus.InCall); |
| 196 | } |
| 197 | }, [isConnected, callStatus]); |
| 198 | |
| 199 | const acceptCall = useCallback(() => { |
| 200 | if (!webSocket) { |
| 201 | throw new Error('Could not accept call'); |
| 202 | } |
| 203 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 204 | const callAccept: CallAction = { |
| 205 | contactId: contactUri, |
| 206 | conversationId, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | console.info('Sending CallAccept', callAccept); |
| 210 | webSocket.send(WebSocketMessageType.CallAccept, callAccept); |
| 211 | setCallStatus(CallStatus.Connecting); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 212 | }, [webSocket, contactUri, conversationId]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 213 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame^] | 214 | const setAudioStatus = useCallback( |
| 215 | (isOn: boolean) => { |
| 216 | if (!localStream) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | for (const track of localStream.getAudioTracks()) { |
| 221 | track.enabled = isOn; |
| 222 | } |
| 223 | |
| 224 | setIsAudioOn(isOn); |
| 225 | }, |
| 226 | [localStream] |
| 227 | ); |
| 228 | |
| 229 | const setVideoStatus = useCallback( |
| 230 | (isOn: boolean) => { |
| 231 | if (!localStream) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | for (const track of localStream.getVideoTracks()) { |
| 236 | track.enabled = isOn; |
| 237 | } |
| 238 | |
| 239 | setIsVideoOn(isOn); |
| 240 | }, |
| 241 | [localStream] |
| 242 | ); |
| 243 | |
| 244 | if (!callRole || callStatus === undefined) { |
| 245 | console.error('Invalid route. Redirecting...'); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 246 | return <Navigate to={'/'} />; |
| 247 | } |
| 248 | |
| 249 | return ( |
| 250 | <CallContext.Provider |
| 251 | value={{ |
| 252 | mediaDevices, |
| 253 | localStream, |
| 254 | remoteStream: remoteStreams?.at(-1), |
| 255 | isAudioOn, |
| 256 | setAudioStatus, |
| 257 | isVideoOn, |
| 258 | setVideoStatus, |
| 259 | callRole, |
| 260 | callStatus, |
| 261 | acceptCall, |
| 262 | }} |
| 263 | > |
| 264 | {children} |
| 265 | </CallContext.Provider> |
| 266 | ); |
| 267 | }; |