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'; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 25 | import { ConversationContext } 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 | |
| 28 | interface IWebRtcContext { |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 29 | iceConnectionState: RTCIceConnectionState | undefined; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 30 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 31 | mediaDevices: Record<MediaDeviceKind, MediaDeviceInfo[]>; |
| 32 | localStream: MediaStream | undefined; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 33 | remoteStreams: readonly MediaStream[] | undefined; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 34 | getUserMedia: () => Promise<void>; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 35 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 36 | sendWebRtcOffer: () => Promise<void>; |
| 37 | closeConnection: () => void; |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | const defaultWebRtcContext: IWebRtcContext = { |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 41 | iceConnectionState: undefined, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 42 | mediaDevices: { |
| 43 | audioinput: [], |
| 44 | audiooutput: [], |
| 45 | videoinput: [], |
| 46 | }, |
| 47 | localStream: undefined, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 48 | remoteStreams: undefined, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 49 | getUserMedia: async () => {}, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 50 | sendWebRtcOffer: async () => {}, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 51 | closeConnection: () => {}, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | export const WebRtcContext = createContext<IWebRtcContext>(defaultWebRtcContext); |
| 55 | |
| 56 | export default ({ children }: WithChildren) => { |
simon | 71d1c0a | 2022-11-24 15:28:33 -0500 | [diff] [blame] | 57 | const { account } = useAuthContext(); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 58 | const [webRtcConnection, setWebRtcConnection] = useState<RTCPeerConnection | undefined>(); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 59 | const webSocket = useContext(WebSocketContext); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 60 | |
| 61 | useEffect(() => { |
simon | 71d1c0a | 2022-11-24 15:28:33 -0500 | [diff] [blame] | 62 | if (!webRtcConnection && account) { |
| 63 | const iceServers: RTCIceServer[] = []; |
| 64 | |
| 65 | if (account.getDetails()['TURN.enable'] === 'true') { |
| 66 | iceServers.push({ |
| 67 | urls: 'turn:' + account.getDetails()['TURN.server'], |
| 68 | username: account.getDetails()['TURN.username'], |
| 69 | credential: account.getDetails()['TURN.password'], |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | if (account.getDetails()['STUN.enable'] === 'true') { |
| 74 | iceServers.push({ |
| 75 | urls: 'stun:' + account.getDetails()['STUN.server'], |
| 76 | }); |
| 77 | } |
| 78 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 79 | setWebRtcConnection(new RTCPeerConnection({ iceServers })); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 80 | } |
simon | 71d1c0a | 2022-11-24 15:28:33 -0500 | [diff] [blame] | 81 | }, [account, webRtcConnection]); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 82 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 83 | if (!webRtcConnection || !webSocket) { |
| 84 | return <LoadingPage />; |
| 85 | } |
| 86 | |
| 87 | return ( |
| 88 | <WebRtcProvider webRtcConnection={webRtcConnection} webSocket={webSocket}> |
| 89 | {children} |
| 90 | </WebRtcProvider> |
| 91 | ); |
| 92 | }; |
| 93 | |
| 94 | const WebRtcProvider = ({ |
| 95 | children, |
| 96 | webRtcConnection, |
| 97 | webSocket, |
| 98 | }: WithChildren & { |
| 99 | webRtcConnection: RTCPeerConnection; |
| 100 | webSocket: IWebSocketContext; |
| 101 | }) => { |
| 102 | const { conversation, conversationId } = useContext(ConversationContext); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 103 | const [localStream, setLocalStream] = useState<MediaStream>(); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 104 | const [remoteStreams, setRemoteStreams] = useState<readonly MediaStream[]>(); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 105 | const [iceConnectionState, setIceConnectionState] = useState<RTCIceConnectionState | undefined>(); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 106 | const [mediaDevices, setMediaDevices] = useState<Record<MediaDeviceKind, MediaDeviceInfo[]>>( |
| 107 | defaultWebRtcContext.mediaDevices |
| 108 | ); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 109 | |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 110 | // TODO: The ICE candidate queue is used to cache candidates that were received before `setRemoteDescription` was |
| 111 | // called. This is currently necessary, because the jami-daemon is unreliable as a WebRTC signaling channel, |
| 112 | // because messages can be received with a delay or out of order. This queue is a temporary workaround that |
| 113 | // should be replaced if there is a better way to send messages with the daemon. |
| 114 | // Relevant links: |
| 115 | // - https://github.com/w3c/webrtc-pc/issues/2519#issuecomment-622055440 |
| 116 | // - https://stackoverflow.com/questions/57256828/how-to-fix-invalidstateerror-cannot-add-ice-candidate-when-there-is-no-remote-s |
| 117 | const [isReadyForIceCandidates, setIsReadyForIceCandidates] = useState(false); |
| 118 | const [iceCandidateQueue, setIceCandidateQueue] = useState<RTCIceCandidate[]>([]); |
| 119 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 120 | // TODO: This logic will have to change to support multiple people in a call |
| 121 | const contactUri = useMemo(() => conversation.getFirstMember().contact.getUri(), [conversation]); |
| 122 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 123 | const getMediaDevices = useCallback(async () => { |
| 124 | try { |
| 125 | const devices = await navigator.mediaDevices.enumerateDevices(); |
| 126 | const newMediaDevices: Record<MediaDeviceKind, MediaDeviceInfo[]> = { |
| 127 | audioinput: [], |
| 128 | audiooutput: [], |
| 129 | videoinput: [], |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 130 | }; |
| 131 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 132 | for (const device of devices) { |
| 133 | newMediaDevices[device.kind].push(device); |
| 134 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 135 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 136 | return newMediaDevices; |
| 137 | } catch (e) { |
| 138 | throw new Error('Could not get media devices', { cause: e }); |
| 139 | } |
| 140 | }, []); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 141 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 142 | useEffect(() => { |
| 143 | if (iceConnectionState !== 'connected' && iceConnectionState !== 'completed') { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | const updateMediaDevices = async () => { |
| 148 | try { |
| 149 | const newMediaDevices = await getMediaDevices(); |
| 150 | setMediaDevices(newMediaDevices); |
| 151 | } catch (e) { |
| 152 | console.error('Could not update media devices:', e); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | navigator.mediaDevices.addEventListener('devicechange', updateMediaDevices); |
| 157 | updateMediaDevices(); |
| 158 | |
| 159 | return () => { |
| 160 | navigator.mediaDevices.removeEventListener('devicechange', updateMediaDevices); |
| 161 | }; |
| 162 | }, [getMediaDevices, iceConnectionState]); |
| 163 | |
| 164 | const getUserMedia = useCallback(async () => { |
| 165 | const devices = await getMediaDevices(); |
| 166 | |
| 167 | const shouldGetAudio = devices.audioinput.length !== 0; |
| 168 | const shouldGetVideo = devices.videoinput.length !== 0; |
| 169 | |
| 170 | if (!shouldGetAudio && !shouldGetVideo) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | try { |
| 175 | const stream = await navigator.mediaDevices.getUserMedia({ |
| 176 | audio: shouldGetAudio, |
| 177 | video: shouldGetVideo, |
| 178 | }); |
| 179 | |
| 180 | for (const track of stream.getTracks()) { |
| 181 | track.enabled = false; |
| 182 | webRtcConnection.addTrack(track, stream); |
| 183 | } |
| 184 | |
| 185 | setLocalStream(stream); |
| 186 | } catch (e) { |
| 187 | throw new Error('Could not get media devices', { cause: e }); |
| 188 | } |
| 189 | }, [webRtcConnection, getMediaDevices]); |
| 190 | |
| 191 | const sendWebRtcOffer = useCallback(async () => { |
| 192 | const sdp = await webRtcConnection.createOffer({ |
| 193 | offerToReceiveAudio: true, |
| 194 | offerToReceiveVideo: true, |
| 195 | }); |
| 196 | |
| 197 | const webRtcOffer: WebRtcSdp = { |
| 198 | contactId: contactUri, |
| 199 | conversationId: conversationId, |
| 200 | sdp, |
| 201 | }; |
| 202 | |
| 203 | await webRtcConnection.setLocalDescription(new RTCSessionDescription(sdp)); |
| 204 | console.info('Sending WebRtcOffer', webRtcOffer); |
| 205 | webSocket.send(WebSocketMessageType.WebRtcOffer, webRtcOffer); |
| 206 | }, [webRtcConnection, webSocket, conversationId, contactUri]); |
| 207 | |
| 208 | const sendWebRtcAnswer = useCallback(async () => { |
| 209 | const sdp = await webRtcConnection.createAnswer({ |
| 210 | offerToReceiveAudio: true, |
| 211 | offerToReceiveVideo: true, |
| 212 | }); |
| 213 | |
| 214 | const webRtcAnswer: WebRtcSdp = { |
| 215 | contactId: contactUri, |
| 216 | conversationId: conversationId, |
| 217 | sdp, |
| 218 | }; |
| 219 | |
| 220 | await webRtcConnection.setLocalDescription(new RTCSessionDescription(sdp)); |
| 221 | console.info('Sending WebRtcAnswer', webRtcAnswer); |
| 222 | webSocket.send(WebSocketMessageType.WebRtcAnswer, webRtcAnswer); |
| 223 | }, [contactUri, conversationId, webRtcConnection, webSocket]); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 224 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 225 | /* WebSocket Listeners */ |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 226 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 227 | useEffect(() => { |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 228 | const addQueuedIceCandidates = async () => { |
| 229 | console.info('WebRTC remote description has been set. Ready to receive ICE candidates'); |
| 230 | setIsReadyForIceCandidates(true); |
| 231 | if (iceCandidateQueue.length !== 0) { |
| 232 | console.warn('Adding queued ICE candidates...', iceCandidateQueue); |
| 233 | |
| 234 | await Promise.all(iceCandidateQueue.map((iceCandidate) => webRtcConnection.addIceCandidate(iceCandidate))); |
| 235 | } |
| 236 | }; |
| 237 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 238 | const webRtcOfferListener = async (data: WebRtcSdp) => { |
| 239 | console.info('Received event on WebRtcOffer', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 240 | if (data.conversationId !== conversationId) { |
| 241 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 242 | return; |
| 243 | } |
| 244 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 245 | await webRtcConnection.setRemoteDescription(new RTCSessionDescription(data.sdp)); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 246 | await sendWebRtcAnswer(); |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 247 | await addQueuedIceCandidates(); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | const webRtcAnswerListener = async (data: WebRtcSdp) => { |
| 251 | console.info('Received event on WebRtcAnswer', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 252 | if (data.conversationId !== conversationId) { |
| 253 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 254 | return; |
| 255 | } |
| 256 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 257 | await webRtcConnection.setRemoteDescription(new RTCSessionDescription(data.sdp)); |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 258 | await addQueuedIceCandidates(); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 259 | }; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 260 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 261 | webSocket.bind(WebSocketMessageType.WebRtcOffer, webRtcOfferListener); |
| 262 | webSocket.bind(WebSocketMessageType.WebRtcAnswer, webRtcAnswerListener); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 263 | |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 264 | return () => { |
| 265 | webSocket.unbind(WebSocketMessageType.WebRtcOffer, webRtcOfferListener); |
| 266 | webSocket.unbind(WebSocketMessageType.WebRtcAnswer, webRtcAnswerListener); |
| 267 | }; |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 268 | }, [webSocket, webRtcConnection, sendWebRtcAnswer, conversationId, iceCandidateQueue]); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 269 | |
| 270 | useEffect(() => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 271 | const webRtcIceCandidateListener = async (data: WebRtcIceCandidate) => { |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 272 | if (data.conversationId !== conversationId) { |
| 273 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 274 | return; |
| 275 | } |
| 276 | |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 277 | if (!data.candidate) { |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | if (isReadyForIceCandidates) { |
| 282 | await webRtcConnection.addIceCandidate(data.candidate); |
| 283 | } else { |
| 284 | console.warn( |
| 285 | "Received event on WebRtcIceCandidate before 'setRemoteDescription' was called. Pushing to ICE candidates queue...", |
| 286 | data |
| 287 | ); |
| 288 | setIceCandidateQueue((v) => { |
| 289 | v.push(data.candidate); |
| 290 | return v; |
| 291 | }); |
| 292 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 293 | }; |
| 294 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 295 | webSocket.bind(WebSocketMessageType.WebRtcIceCandidate, webRtcIceCandidateListener); |
| 296 | |
| 297 | return () => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 298 | webSocket.unbind(WebSocketMessageType.WebRtcIceCandidate, webRtcIceCandidateListener); |
| 299 | }; |
simon | 25bfee8 | 2022-11-28 19:41:24 -0500 | [diff] [blame] | 300 | }, [webRtcConnection, webSocket, conversationId, isReadyForIceCandidates]); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 301 | |
| 302 | /* WebRTC Listeners */ |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 303 | |
| 304 | useEffect(() => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 305 | const iceCandidateEventListener = (event: RTCPeerConnectionIceEvent) => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 306 | if (event.candidate) { |
| 307 | const webRtcIceCandidate: WebRtcIceCandidate = { |
| 308 | contactId: contactUri, |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 309 | conversationId: conversationId, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 310 | candidate: event.candidate, |
| 311 | }; |
| 312 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 313 | webSocket.send(WebSocketMessageType.WebRtcIceCandidate, webRtcIceCandidate); |
| 314 | } |
| 315 | }; |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 316 | webRtcConnection.addEventListener('icecandidate', iceCandidateEventListener); |
| 317 | |
| 318 | return () => { |
| 319 | webRtcConnection.removeEventListener('icecandidate', iceCandidateEventListener); |
| 320 | }; |
| 321 | }, [webRtcConnection, webSocket, contactUri, conversationId]); |
| 322 | |
| 323 | useEffect(() => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 324 | const trackEventListener = (event: RTCTrackEvent) => { |
| 325 | console.info('Received WebRTC event on track', event); |
| 326 | setRemoteStreams(event.streams); |
| 327 | }; |
| 328 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 329 | const iceConnectionStateChangeEventListener = (event: Event) => { |
| 330 | console.info(`Received WebRTC event on iceconnectionstatechange: ${webRtcConnection.iceConnectionState}`, event); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 331 | setIceConnectionState(webRtcConnection.iceConnectionState); |
simon | feaa1db | 2022-11-26 20:13:18 -0500 | [diff] [blame] | 332 | }; |
| 333 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 334 | webRtcConnection.addEventListener('track', trackEventListener); |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 335 | webRtcConnection.addEventListener('iceconnectionstatechange', iceConnectionStateChangeEventListener); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 336 | |
| 337 | return () => { |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 338 | webRtcConnection.removeEventListener('track', trackEventListener); |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 339 | webRtcConnection.removeEventListener('iceconnectionstatechange', iceConnectionStateChangeEventListener); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 340 | }; |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 341 | }, [webRtcConnection]); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 342 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 343 | const closeConnection = useCallback(() => { |
| 344 | const localTracks = localStream?.getTracks(); |
| 345 | if (localTracks) { |
| 346 | for (const track of localTracks) { |
| 347 | track.stop(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | webRtcConnection.close(); |
| 352 | }, [webRtcConnection, localStream]); |
| 353 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 354 | return ( |
| 355 | <WebRtcContext.Provider |
| 356 | value={{ |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 357 | iceConnectionState, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 358 | mediaDevices, |
| 359 | localStream, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 360 | remoteStreams, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 361 | getUserMedia, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 362 | sendWebRtcOffer, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 363 | closeConnection, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 364 | }} |
| 365 | > |
| 366 | {children} |
| 367 | </WebRtcContext.Provider> |
| 368 | ); |
| 369 | }; |