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