Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [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 | */ |
| 18 | |
simon | a5c54ef | 2022-11-18 05:26:06 -0500 | [diff] [blame^] | 19 | import { |
| 20 | AccountTextMessage, |
| 21 | WebRTCAnswerMessage, |
| 22 | WebRTCIceCandidate, |
| 23 | WebRTCOfferMessage, |
| 24 | WebSocketMessageType, |
| 25 | } from 'jami-web-common'; |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 26 | import React, { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react'; |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 27 | |
| 28 | import { WithChildren } from '../utils/utils'; |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 29 | import { useAuthContext } from './AuthProvider'; |
| 30 | import { WebSocketContext } from './WebSocketProvider'; |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 31 | |
| 32 | interface IWebRTCContext { |
| 33 | localVideoRef: React.RefObject<HTMLVideoElement> | null; |
| 34 | remoteVideoRef: React.RefObject<HTMLVideoElement> | null; |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 35 | |
simon | 9a8fe20 | 2022-11-15 18:25:49 -0500 | [diff] [blame] | 36 | mediaDevices: Record<MediaDeviceKind, MediaDeviceInfo[]>; |
| 37 | |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 38 | contactId: string; |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 39 | |
| 40 | isAudioOn: boolean; |
| 41 | setAudioStatus: (isOn: boolean) => void; |
| 42 | isVideoOn: boolean; |
| 43 | setVideoStatus: (isOn: boolean) => void; |
| 44 | sendWebRTCOffer: () => void; |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 45 | } |
| 46 | |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 47 | const defaultWebRTCContext: IWebRTCContext = { |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 48 | localVideoRef: null, |
| 49 | remoteVideoRef: null, |
simon | 9a8fe20 | 2022-11-15 18:25:49 -0500 | [diff] [blame] | 50 | mediaDevices: { |
| 51 | audioinput: [], |
| 52 | audiooutput: [], |
| 53 | videoinput: [], |
| 54 | }, |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 55 | |
| 56 | contactId: '', |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 57 | |
| 58 | isAudioOn: false, |
| 59 | setAudioStatus: () => {}, |
| 60 | isVideoOn: false, |
| 61 | setVideoStatus: () => {}, |
| 62 | |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 63 | sendWebRTCOffer: () => {}, |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 64 | }; |
| 65 | |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 66 | export const WebRTCContext = createContext<IWebRTCContext>(defaultWebRTCContext); |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 67 | |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 68 | type WebRTCProviderProps = WithChildren & { |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 69 | contactId: string; |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 70 | isAudioOn?: boolean; |
| 71 | isVideoOn?: boolean; |
| 72 | }; |
| 73 | |
| 74 | // TODO: This is a WIP. The calling logic will be improved in other CRs |
| 75 | export default ({ |
| 76 | children, |
| 77 | isAudioOn: _isAudioOn = defaultWebRTCContext.isAudioOn, |
| 78 | isVideoOn: _isVideoOn = defaultWebRTCContext.isVideoOn, |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 79 | contactId: _contactId = defaultWebRTCContext.contactId, |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 80 | }: WebRTCProviderProps) => { |
| 81 | const [isAudioOn, setIsAudioOn] = useState(_isAudioOn); |
| 82 | const [isVideoOn, setIsVideoOn] = useState(_isVideoOn); |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 83 | const localVideoRef = useRef<HTMLVideoElement>(null); |
| 84 | const remoteVideoRef = useRef<HTMLVideoElement>(null); |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 85 | const { account } = useAuthContext(); |
| 86 | const contactId = _contactId; |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 87 | const [webRTCConnection, setWebRTCConnection] = useState<RTCPeerConnection | undefined>(); |
| 88 | const localStreamRef = useRef<MediaStream>(); |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 89 | const webSocket = useContext(WebSocketContext); |
simon | 9a8fe20 | 2022-11-15 18:25:49 -0500 | [diff] [blame] | 90 | const [mediaDevices, setMediaDevices] = useState<Record<MediaDeviceKind, MediaDeviceInfo[]>>( |
| 91 | defaultWebRTCContext.mediaDevices |
| 92 | ); |
| 93 | |
| 94 | useEffect(() => { |
| 95 | navigator.mediaDevices.enumerateDevices().then((devices) => { |
| 96 | const newMediaDevices: Record<MediaDeviceKind, MediaDeviceInfo[]> = { |
| 97 | audioinput: [], |
| 98 | audiooutput: [], |
| 99 | videoinput: [], |
| 100 | }; |
| 101 | |
| 102 | for (const device of devices) { |
| 103 | newMediaDevices[device.kind].push(device); |
| 104 | } |
| 105 | |
| 106 | setMediaDevices(newMediaDevices); |
| 107 | }); |
| 108 | }, []); |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 109 | |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 110 | useEffect(() => { |
| 111 | if (!webRTCConnection) { |
| 112 | // TODO use SFL iceServers |
| 113 | const iceConfig = { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] }; |
| 114 | setWebRTCConnection(new RTCPeerConnection(iceConfig)); |
| 115 | } |
| 116 | }, [webRTCConnection]); |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 117 | |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 118 | useEffect(() => { |
| 119 | if (!webRTCConnection) { |
| 120 | return; |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 121 | } |
| 122 | |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 123 | if (isVideoOn || isAudioOn) { |
| 124 | try { |
| 125 | // TODO: When toggling mute on/off, the camera flickers |
| 126 | // https://git.jami.net/savoirfairelinux/jami-web/-/issues/90 |
| 127 | navigator.mediaDevices |
| 128 | .getUserMedia({ |
| 129 | audio: true, |
| 130 | video: true, |
| 131 | }) |
| 132 | .then((stream) => { |
| 133 | if (localVideoRef.current) { |
| 134 | localVideoRef.current.srcObject = stream; |
| 135 | } |
| 136 | |
| 137 | stream.getTracks().forEach((track) => { |
| 138 | if (track.kind === 'audio') { |
| 139 | track.enabled = isAudioOn; |
| 140 | } else if (track.kind === 'video') { |
| 141 | track.enabled = isVideoOn; |
| 142 | } |
| 143 | webRTCConnection.addTrack(track, stream); |
| 144 | }); |
| 145 | localStreamRef.current = stream; |
| 146 | }); |
| 147 | } catch (e) { |
| 148 | console.error('Could not get media devices: ', e); |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 149 | } |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | const icecandidateEventListener = (event: RTCPeerConnectionIceEvent) => { |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 153 | if (event.candidate && webSocket) { |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 154 | console.log('webRTCConnection : onicecandidate'); |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 155 | webSocket.send(WebSocketMessageType.IceCandidate, { |
| 156 | from: account.getId(), |
| 157 | to: contactId, |
| 158 | message: { |
| 159 | candidate: event.candidate, |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 160 | }, |
| 161 | }); |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 162 | } |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | const trackEventListener = (event: RTCTrackEvent) => { |
| 166 | console.log('remote TrackEvent'); |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 167 | if (remoteVideoRef.current) { |
| 168 | remoteVideoRef.current.srcObject = event.streams[0]; |
| 169 | console.log('webRTCConnection : add remotetrack success'); |
| 170 | } |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | webRTCConnection.addEventListener('icecandidate', icecandidateEventListener); |
| 174 | webRTCConnection.addEventListener('track', trackEventListener); |
| 175 | |
| 176 | return () => { |
| 177 | webRTCConnection.removeEventListener('icecandidate', icecandidateEventListener); |
| 178 | webRTCConnection.removeEventListener('track', trackEventListener); |
| 179 | }; |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 180 | }, [webRTCConnection, isVideoOn, isAudioOn, webSocket, contactId, account]); |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 181 | |
| 182 | useEffect(() => { |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 183 | if (!webRTCConnection || !webSocket) { |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
simon | a5c54ef | 2022-11-18 05:26:06 -0500 | [diff] [blame^] | 187 | const webRTCOfferListener = async (data: AccountTextMessage<WebRTCOfferMessage>) => { |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 188 | if (webRTCConnection) { |
| 189 | await webRTCConnection.setRemoteDescription(new RTCSessionDescription(data.message.sdp)); |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 190 | const mySdp = await webRTCConnection.createAnswer({ |
| 191 | offerToReceiveAudio: true, |
| 192 | offerToReceiveVideo: true, |
| 193 | }); |
| 194 | await webRTCConnection.setLocalDescription(new RTCSessionDescription(mySdp)); |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 195 | webSocket.send(WebSocketMessageType.WebRTCAnswer, { |
| 196 | from: account.getId(), |
| 197 | to: contactId, |
| 198 | message: { |
| 199 | sdp: mySdp, |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 200 | }, |
| 201 | }); |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 202 | } |
simon | a5c54ef | 2022-11-18 05:26:06 -0500 | [diff] [blame^] | 203 | }; |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 204 | |
simon | a5c54ef | 2022-11-18 05:26:06 -0500 | [diff] [blame^] | 205 | const webRTCAnswerListener = async (data: AccountTextMessage<WebRTCAnswerMessage>) => { |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 206 | await webRTCConnection.setRemoteDescription(new RTCSessionDescription(data.message.sdp)); |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 207 | console.log('get answer'); |
simon | a5c54ef | 2022-11-18 05:26:06 -0500 | [diff] [blame^] | 208 | }; |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 209 | |
simon | a5c54ef | 2022-11-18 05:26:06 -0500 | [diff] [blame^] | 210 | const iceCandidateListener = async (data: AccountTextMessage<WebRTCIceCandidate>) => { |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 211 | await webRTCConnection.addIceCandidate(new RTCIceCandidate(data.message.candidate)); |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 212 | console.log('webRTCConnection : candidate add success'); |
simon | a5c54ef | 2022-11-18 05:26:06 -0500 | [diff] [blame^] | 213 | }; |
| 214 | |
| 215 | webSocket.bind(WebSocketMessageType.WebRTCOffer, webRTCOfferListener); |
| 216 | webSocket.bind(WebSocketMessageType.WebRTCAnswer, webRTCAnswerListener); |
| 217 | webSocket.bind(WebSocketMessageType.IceCandidate, iceCandidateListener); |
| 218 | |
| 219 | return () => { |
| 220 | webSocket.unbind(WebSocketMessageType.WebRTCOffer, webRTCOfferListener); |
| 221 | webSocket.unbind(WebSocketMessageType.WebRTCAnswer, webRTCAnswerListener); |
| 222 | webSocket.unbind(WebSocketMessageType.IceCandidate, iceCandidateListener); |
| 223 | }; |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 224 | }, [account, contactId, webSocket, webRTCConnection]); |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 225 | |
| 226 | const setAudioStatus = useCallback((isOn: boolean) => { |
| 227 | setIsAudioOn(isOn); |
| 228 | localStreamRef.current?.getAudioTracks().forEach((track) => { |
| 229 | track.enabled = isOn; |
| 230 | }); |
| 231 | }, []); |
| 232 | |
| 233 | const setVideoStatus = useCallback((isOn: boolean) => { |
| 234 | setIsVideoOn(isOn); |
| 235 | localStreamRef.current?.getVideoTracks().forEach((track) => { |
| 236 | track.enabled = isOn; |
| 237 | }); |
| 238 | }, []); |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 239 | |
| 240 | const sendWebRTCOffer = useCallback(async () => { |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 241 | if (webRTCConnection && webSocket) { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 242 | const sdp = await webRTCConnection.createOffer({ |
| 243 | offerToReceiveAudio: true, |
| 244 | offerToReceiveVideo: true, |
| 245 | }); |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 246 | webSocket.send(WebSocketMessageType.WebRTCOffer, { |
| 247 | from: account.getId(), |
| 248 | to: contactId, |
| 249 | message: { |
| 250 | sdp, |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 251 | }, |
| 252 | }); |
| 253 | await webRTCConnection.setLocalDescription(new RTCSessionDescription(sdp)); |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 254 | } |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 255 | }, [account, contactId, webSocket, webRTCConnection]); |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 256 | |
| 257 | return ( |
| 258 | <WebRTCContext.Provider |
| 259 | value={{ |
| 260 | localVideoRef, |
| 261 | remoteVideoRef, |
simon | 9a8fe20 | 2022-11-15 18:25:49 -0500 | [diff] [blame] | 262 | mediaDevices, |
Charlie | 461805e | 2022-11-09 10:40:15 -0500 | [diff] [blame] | 263 | contactId, |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 264 | isAudioOn, |
| 265 | setAudioStatus, |
| 266 | isVideoOn, |
| 267 | setVideoStatus, |
| 268 | sendWebRTCOffer, |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 269 | }} |
| 270 | > |
| 271 | {children} |
| 272 | </WebRTCContext.Provider> |
| 273 | ); |
| 274 | }; |