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