Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -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 | */ |
| 18 | |
| 19 | import { WebRtcIceCandidate, WebRtcSdp, WebSocketMessageType } from 'jami-web-common'; |
| 20 | import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; |
| 21 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 22 | import LoadingPage from '../components/Loading'; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 23 | import { WithChildren } from '../utils/utils'; |
simon | 71d1c0a | 2022-11-24 15:28:33 -0500 | [diff] [blame] | 24 | import { useAuthContext } from './AuthProvider'; |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 25 | import { useConversationContext } from './ConversationProvider'; |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 26 | import { IWebSocketContext, WebSocketContext } from './WebSocketProvider'; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 27 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 28 | export type MediaDevicesInfo = Record<MediaDeviceKind, MediaDeviceInfo[]>; |
| 29 | export type MediaInputKind = 'audio' | 'video'; |
| 30 | export type MediaInputIds = Record<MediaInputKind, string | false | undefined>; |
| 31 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 32 | interface IWebRtcContext { |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 33 | iceConnectionState: RTCIceConnectionState | undefined; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 34 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 35 | localStream: MediaStream | undefined; |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 36 | screenShareLocalStream: MediaStream | undefined; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 37 | remoteStreams: readonly MediaStream[] | undefined; |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 38 | getMediaDevices: () => Promise<MediaDevicesInfo>; |
| 39 | updateLocalStream: (mediaDeviceIds?: MediaInputIds) => Promise<void>; |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 40 | updateScreenShare: (active: boolean) => Promise<MediaStream | undefined>; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 41 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 42 | sendWebRtcOffer: () => Promise<void>; |
| 43 | closeConnection: () => void; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | const defaultWebRtcContext: IWebRtcContext = { |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 47 | iceConnectionState: undefined, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 48 | localStream: undefined, |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 49 | screenShareLocalStream: undefined, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 50 | remoteStreams: undefined, |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 51 | getMediaDevices: async () => Promise.reject(), |
| 52 | updateLocalStream: async () => Promise.reject(), |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 53 | updateScreenShare: async () => Promise.reject(), |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 54 | sendWebRtcOffer: async () => Promise.reject(), |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 55 | closeConnection: () => {}, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | export const WebRtcContext = createContext<IWebRtcContext>(defaultWebRtcContext); |
| 59 | |
| 60 | export default ({ children }: WithChildren) => { |
simon | 71d1c0a | 2022-11-24 15:28:33 -0500 | [diff] [blame] | 61 | const { account } = useAuthContext(); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 62 | const [webRtcConnection, setWebRtcConnection] = useState<RTCPeerConnection | undefined>(); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 63 | const webSocket = useContext(WebSocketContext); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 64 | |
| 65 | useEffect(() => { |
simon | 71d1c0a | 2022-11-24 15:28:33 -0500 | [diff] [blame] | 66 | if (!webRtcConnection && account) { |
| 67 | const iceServers: RTCIceServer[] = []; |
| 68 | |
| 69 | if (account.getDetails()['TURN.enable'] === 'true') { |
| 70 | iceServers.push({ |
| 71 | urls: 'turn:' + account.getDetails()['TURN.server'], |
| 72 | username: account.getDetails()['TURN.username'], |
| 73 | credential: account.getDetails()['TURN.password'], |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | if (account.getDetails()['STUN.enable'] === 'true') { |
| 78 | iceServers.push({ |
| 79 | urls: 'stun:' + account.getDetails()['STUN.server'], |
| 80 | }); |
| 81 | } |
| 82 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 83 | setWebRtcConnection(new RTCPeerConnection({ iceServers })); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 84 | } |
simon | 71d1c0a | 2022-11-24 15:28:33 -0500 | [diff] [blame] | 85 | }, [account, webRtcConnection]); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 86 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 87 | if (!webRtcConnection || !webSocket) { |
| 88 | return <LoadingPage />; |
| 89 | } |
| 90 | |
| 91 | return ( |
| 92 | <WebRtcProvider webRtcConnection={webRtcConnection} webSocket={webSocket}> |
| 93 | {children} |
| 94 | </WebRtcProvider> |
| 95 | ); |
| 96 | }; |
| 97 | |
| 98 | const WebRtcProvider = ({ |
| 99 | children, |
| 100 | webRtcConnection, |
| 101 | webSocket, |
| 102 | }: WithChildren & { |
| 103 | webRtcConnection: RTCPeerConnection; |
| 104 | webSocket: IWebSocketContext; |
| 105 | }) => { |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 106 | const { conversation, conversationId } = useConversationContext(); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 107 | const [localStream, setLocalStream] = useState<MediaStream>(); |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 108 | const [screenShareLocalStream, setScreenShareLocalStream] = useState<MediaStream>(); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 109 | const [remoteStreams, setRemoteStreams] = useState<readonly MediaStream[]>(); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 110 | const [iceConnectionState, setIceConnectionState] = useState<RTCIceConnectionState | undefined>(); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 111 | |
| 112 | const [audioRtcRtpSenders, setAudioRtcRtpSenders] = useState<RTCRtpSender[]>(); |
| 113 | const [videoRtcRtpSenders, setVideoRtcRtpSenders] = useState<RTCRtpSender[]>(); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 114 | |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 115 | // TODO: The ICE candidate queue is used to cache candidates that were received before `setRemoteDescription` was |
| 116 | // called. This is currently necessary, because the jami-daemon is unreliable as a WebRTC signaling channel, |
| 117 | // because messages can be received with a delay or out of order. This queue is a temporary workaround that |
| 118 | // should be replaced if there is a better way to send messages with the daemon. |
| 119 | // Relevant links: |
| 120 | // - https://github.com/w3c/webrtc-pc/issues/2519#issuecomment-622055440 |
| 121 | // - https://stackoverflow.com/questions/57256828/how-to-fix-invalidstateerror-cannot-add-ice-candidate-when-there-is-no-remote-s |
| 122 | const [isReadyForIceCandidates, setIsReadyForIceCandidates] = useState(false); |
| 123 | const [iceCandidateQueue, setIceCandidateQueue] = useState<RTCIceCandidate[]>([]); |
| 124 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 125 | // TODO: This logic will have to change to support multiple people in a call |
| 126 | const contactUri = useMemo(() => conversation.getFirstMember().contact.getUri(), [conversation]); |
| 127 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 128 | const getMediaDevices = useCallback(async (): Promise<MediaDevicesInfo> => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 129 | try { |
| 130 | const devices = await navigator.mediaDevices.enumerateDevices(); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 131 | |
| 132 | // TODO: On Firefox, some devices can sometime be duplicated (2 devices can share the same deviceId). Using a map |
| 133 | // and then converting it to an array makes it so that there is no duplicate. If we find a way to prevent |
| 134 | // Firefox from listing 2 devices with the same deviceId, we can remove this logic. |
| 135 | const newMediaDevices: Record<MediaDeviceKind, Record<string, MediaDeviceInfo>> = { |
| 136 | audioinput: {}, |
| 137 | audiooutput: {}, |
| 138 | videoinput: {}, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 139 | }; |
| 140 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 141 | for (const device of devices) { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 142 | newMediaDevices[device.kind][device.deviceId] = device; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 143 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 144 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 145 | return { |
| 146 | audioinput: Object.values(newMediaDevices.audioinput), |
| 147 | audiooutput: Object.values(newMediaDevices.audiooutput), |
| 148 | videoinput: Object.values(newMediaDevices.videoinput), |
| 149 | }; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 150 | } catch (e) { |
| 151 | throw new Error('Could not get media devices', { cause: e }); |
| 152 | } |
| 153 | }, []); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 154 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 155 | const updateLocalStream = useCallback( |
| 156 | async (mediaDeviceIds?: MediaInputIds) => { |
| 157 | const devices = await getMediaDevices(); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 158 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 159 | let audioConstraint: MediaTrackConstraints | boolean = devices.audioinput.length !== 0; |
| 160 | let videoConstraint: MediaTrackConstraints | boolean = devices.videoinput.length !== 0; |
| 161 | |
| 162 | if (!audioConstraint && !videoConstraint) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if (mediaDeviceIds?.audio !== undefined) { |
| 167 | audioConstraint = mediaDeviceIds.audio !== false ? { deviceId: mediaDeviceIds.audio } : false; |
| 168 | } |
| 169 | if (mediaDeviceIds?.video !== undefined) { |
| 170 | videoConstraint = mediaDeviceIds.video !== false ? { deviceId: mediaDeviceIds.video } : false; |
| 171 | } |
| 172 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 173 | try { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 174 | const stream = await navigator.mediaDevices.getUserMedia({ |
| 175 | audio: audioConstraint, |
| 176 | video: videoConstraint, |
| 177 | }); |
| 178 | |
| 179 | for (const track of stream.getTracks()) { |
| 180 | track.enabled = false; |
| 181 | } |
| 182 | |
| 183 | setLocalStream(stream); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 184 | } catch (e) { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 185 | throw new Error('Could not get media devices', { cause: e }); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 186 | } |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 187 | }, |
| 188 | [getMediaDevices] |
| 189 | ); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 190 | |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 191 | const updateScreenShare = useCallback( |
| 192 | async (isOn: boolean) => { |
| 193 | if (isOn) { |
| 194 | const stream = await navigator.mediaDevices.getDisplayMedia({ |
| 195 | video: true, |
| 196 | audio: false, |
| 197 | }); |
| 198 | |
| 199 | setScreenShareLocalStream(stream); |
| 200 | return stream; |
| 201 | } else { |
| 202 | if (screenShareLocalStream) { |
| 203 | for (const track of screenShareLocalStream.getTracks()) { |
| 204 | track.stop(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | setScreenShareLocalStream(undefined); |
| 209 | } |
| 210 | }, |
| 211 | [screenShareLocalStream] |
| 212 | ); |
| 213 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 214 | useEffect(() => { |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 215 | if ((!localStream && !screenShareLocalStream) || !webRtcConnection) { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 216 | return; |
| 217 | } |
| 218 | |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 219 | const updateTracks = async (stream: MediaStream, kind: 'audio' | 'video') => { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 220 | const senders = kind === 'audio' ? audioRtcRtpSenders : videoRtcRtpSenders; |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 221 | const tracks = kind === 'audio' ? stream.getAudioTracks() : stream.getVideoTracks(); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 222 | if (senders) { |
| 223 | const promises: Promise<void>[] = []; |
| 224 | for (let i = 0; i < senders.length; i++) { |
| 225 | // TODO: There is a bug where calling multiple times `addTrack` when changing an input device doesn't work. |
| 226 | // Calling `addTrack` doesn't trigger the `track` event listener for the other user. |
| 227 | // This workaround makes it possible to replace a track, but it could be improved by figuring out the |
| 228 | // proper way of changing a track. |
| 229 | promises.push( |
| 230 | senders[i].replaceTrack(tracks[i]).catch((e) => { |
| 231 | console.error('Error replacing track:', e); |
| 232 | }) |
| 233 | ); |
| 234 | } |
| 235 | return Promise.all(promises); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 236 | } |
| 237 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 238 | // TODO: Currently, we do not support adding new devices. To enable this feature, we would need to implement |
| 239 | // the "Perfect negotiation" pattern to renegotiate after `addTrack`. |
| 240 | // https://blog.mozilla.org/webrtc/perfect-negotiation-in-webrtc/ |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 241 | const newSenders = tracks.map((track) => webRtcConnection.addTrack(track, stream)); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 242 | if (kind === 'audio') { |
| 243 | setAudioRtcRtpSenders(newSenders); |
| 244 | } else { |
| 245 | setVideoRtcRtpSenders(newSenders); |
| 246 | } |
| 247 | }; |
| 248 | |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 249 | if (localStream) { |
| 250 | updateTracks(localStream, 'audio'); |
| 251 | updateTracks(localStream, 'video'); |
| 252 | } |
| 253 | |
| 254 | if (screenShareLocalStream) { |
| 255 | updateTracks(screenShareLocalStream, 'video'); |
| 256 | } |
| 257 | }, [localStream, screenShareLocalStream, webRtcConnection, audioRtcRtpSenders, videoRtcRtpSenders]); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 258 | |
| 259 | const sendWebRtcOffer = useCallback(async () => { |
| 260 | const sdp = await webRtcConnection.createOffer({ |
| 261 | offerToReceiveAudio: true, |
| 262 | offerToReceiveVideo: true, |
| 263 | }); |
| 264 | |
| 265 | const webRtcOffer: WebRtcSdp = { |
| 266 | contactId: contactUri, |
| 267 | conversationId: conversationId, |
| 268 | sdp, |
| 269 | }; |
| 270 | |
| 271 | await webRtcConnection.setLocalDescription(new RTCSessionDescription(sdp)); |
| 272 | console.info('Sending WebRtcOffer', webRtcOffer); |
| 273 | webSocket.send(WebSocketMessageType.WebRtcOffer, webRtcOffer); |
| 274 | }, [webRtcConnection, webSocket, conversationId, contactUri]); |
| 275 | |
| 276 | const sendWebRtcAnswer = useCallback(async () => { |
| 277 | const sdp = await webRtcConnection.createAnswer({ |
| 278 | offerToReceiveAudio: true, |
| 279 | offerToReceiveVideo: true, |
| 280 | }); |
| 281 | |
| 282 | const webRtcAnswer: WebRtcSdp = { |
| 283 | contactId: contactUri, |
| 284 | conversationId: conversationId, |
| 285 | sdp, |
| 286 | }; |
| 287 | |
| 288 | await webRtcConnection.setLocalDescription(new RTCSessionDescription(sdp)); |
| 289 | console.info('Sending WebRtcAnswer', webRtcAnswer); |
| 290 | webSocket.send(WebSocketMessageType.WebRtcAnswer, webRtcAnswer); |
| 291 | }, [contactUri, conversationId, webRtcConnection, webSocket]); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 292 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 293 | /* WebSocket Listeners */ |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 294 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 295 | useEffect(() => { |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 296 | const addQueuedIceCandidates = async () => { |
| 297 | console.info('WebRTC remote description has been set. Ready to receive ICE candidates'); |
| 298 | setIsReadyForIceCandidates(true); |
| 299 | if (iceCandidateQueue.length !== 0) { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 300 | console.warn( |
| 301 | 'Found queued ICE candidates that were added before `setRemoteDescription` was called. ' + |
| 302 | 'Adding queued ICE candidates...', |
| 303 | iceCandidateQueue |
| 304 | ); |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 305 | |
| 306 | await Promise.all(iceCandidateQueue.map((iceCandidate) => webRtcConnection.addIceCandidate(iceCandidate))); |
| 307 | } |
| 308 | }; |
| 309 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 310 | const webRtcOfferListener = async (data: WebRtcSdp) => { |
| 311 | console.info('Received event on WebRtcOffer', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 312 | if (data.conversationId !== conversationId) { |
| 313 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 314 | return; |
| 315 | } |
| 316 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 317 | await webRtcConnection.setRemoteDescription(new RTCSessionDescription(data.sdp)); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 318 | await sendWebRtcAnswer(); |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 319 | await addQueuedIceCandidates(); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | const webRtcAnswerListener = async (data: WebRtcSdp) => { |
| 323 | console.info('Received event on WebRtcAnswer', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 324 | if (data.conversationId !== conversationId) { |
| 325 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 326 | return; |
| 327 | } |
| 328 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 329 | await webRtcConnection.setRemoteDescription(new RTCSessionDescription(data.sdp)); |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 330 | await addQueuedIceCandidates(); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 331 | }; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 332 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 333 | webSocket.bind(WebSocketMessageType.WebRtcOffer, webRtcOfferListener); |
| 334 | webSocket.bind(WebSocketMessageType.WebRtcAnswer, webRtcAnswerListener); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 335 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 336 | return () => { |
| 337 | webSocket.unbind(WebSocketMessageType.WebRtcOffer, webRtcOfferListener); |
| 338 | webSocket.unbind(WebSocketMessageType.WebRtcAnswer, webRtcAnswerListener); |
| 339 | }; |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 340 | }, [webSocket, webRtcConnection, sendWebRtcAnswer, conversationId, iceCandidateQueue]); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 341 | |
| 342 | useEffect(() => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 343 | const webRtcIceCandidateListener = async (data: WebRtcIceCandidate) => { |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 344 | if (data.conversationId !== conversationId) { |
| 345 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 346 | return; |
| 347 | } |
| 348 | |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 349 | if (!data.candidate) { |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | if (isReadyForIceCandidates) { |
| 354 | await webRtcConnection.addIceCandidate(data.candidate); |
| 355 | } else { |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 356 | setIceCandidateQueue((v) => { |
| 357 | v.push(data.candidate); |
| 358 | return v; |
| 359 | }); |
| 360 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 361 | }; |
| 362 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 363 | webSocket.bind(WebSocketMessageType.WebRtcIceCandidate, webRtcIceCandidateListener); |
| 364 | |
| 365 | return () => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 366 | webSocket.unbind(WebSocketMessageType.WebRtcIceCandidate, webRtcIceCandidateListener); |
| 367 | }; |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 368 | }, [webRtcConnection, webSocket, conversationId, isReadyForIceCandidates]); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 369 | |
| 370 | /* WebRTC Listeners */ |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 371 | |
| 372 | useEffect(() => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 373 | const iceCandidateEventListener = (event: RTCPeerConnectionIceEvent) => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 374 | if (event.candidate) { |
| 375 | const webRtcIceCandidate: WebRtcIceCandidate = { |
| 376 | contactId: contactUri, |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 377 | conversationId: conversationId, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 378 | candidate: event.candidate, |
| 379 | }; |
| 380 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 381 | webSocket.send(WebSocketMessageType.WebRtcIceCandidate, webRtcIceCandidate); |
| 382 | } |
| 383 | }; |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 384 | webRtcConnection.addEventListener('icecandidate', iceCandidateEventListener); |
| 385 | |
| 386 | return () => { |
| 387 | webRtcConnection.removeEventListener('icecandidate', iceCandidateEventListener); |
| 388 | }; |
| 389 | }, [webRtcConnection, webSocket, contactUri, conversationId]); |
| 390 | |
| 391 | useEffect(() => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 392 | const trackEventListener = (event: RTCTrackEvent) => { |
| 393 | console.info('Received WebRTC event on track', event); |
| 394 | setRemoteStreams(event.streams); |
| 395 | }; |
| 396 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 397 | const iceConnectionStateChangeEventListener = (event: Event) => { |
| 398 | console.info(`Received WebRTC event on iceconnectionstatechange: ${webRtcConnection.iceConnectionState}`, event); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 399 | setIceConnectionState(webRtcConnection.iceConnectionState); |
simon | feaa1db | 2022-11-26 20:13:18 -0500 | [diff] [blame] | 400 | }; |
| 401 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 402 | webRtcConnection.addEventListener('track', trackEventListener); |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 403 | webRtcConnection.addEventListener('iceconnectionstatechange', iceConnectionStateChangeEventListener); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 404 | |
| 405 | return () => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 406 | webRtcConnection.removeEventListener('track', trackEventListener); |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 407 | webRtcConnection.removeEventListener('iceconnectionstatechange', iceConnectionStateChangeEventListener); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 408 | }; |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 409 | }, [webRtcConnection]); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 410 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 411 | const closeConnection = useCallback(() => { |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 412 | const stopStream = (stream: MediaStream) => { |
| 413 | const localTracks = stream.getTracks(); |
| 414 | if (localTracks) { |
| 415 | for (const track of localTracks) { |
| 416 | track.stop(); |
| 417 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 418 | } |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | if (localStream) { |
| 422 | stopStream(localStream); |
| 423 | } |
| 424 | if (screenShareLocalStream) { |
| 425 | stopStream(screenShareLocalStream); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | webRtcConnection.close(); |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 429 | }, [webRtcConnection, localStream, screenShareLocalStream]); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 430 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 431 | return ( |
| 432 | <WebRtcContext.Provider |
| 433 | value={{ |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 434 | iceConnectionState, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 435 | localStream, |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 436 | screenShareLocalStream, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 437 | remoteStreams, |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 438 | getMediaDevices, |
| 439 | updateLocalStream, |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 440 | updateScreenShare, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 441 | sendWebRtcOffer, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 442 | closeConnection, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 443 | }} |
| 444 | > |
| 445 | {children} |
| 446 | </WebRtcContext.Provider> |
| 447 | ); |
| 448 | }; |