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