From 97656aa9d68e325a1c8c571d029cc5e4cd3e8bf5 Mon Sep 17 00:00:00 2001 From: Juan Navarro Date: Tue, 15 Feb 2022 14:08:34 +0100 Subject: [PATCH] openvidu-browser: Use the RTCIceCandidate constructor on new candidates The ice candidate event provides an "RTCIceCandidateInit" object, which should be passed to the RTCIceCandidate() constructor, to build a proper instance of RTCIceCandidate. --- openvidu-browser/src/OpenVidu/Session.ts | 27 +++++++------------ .../OpenViduInternal/WebRtcPeer/WebRtcPeer.ts | 20 +++++++++----- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/openvidu-browser/src/OpenVidu/Session.ts b/openvidu-browser/src/OpenVidu/Session.ts index 94bef3fb..7b84ee29 100644 --- a/openvidu-browser/src/OpenVidu/Session.ts +++ b/openvidu-browser/src/OpenVidu/Session.ts @@ -995,29 +995,20 @@ export class Session extends EventDispatcher { * @hidden */ recvIceCandidate(event: { senderConnectionId: string, endpointName: string, sdpMLineIndex: number, sdpMid: string, candidate: string }): void { - const candidate: RTCIceCandidate = { - address: null, + // The event contains fields that can be used to obtain a proper candidate, + // using the RTCIceCandidate constructor: + // https://w3c.github.io/webrtc-pc/#dom-rtcicecandidate-constructor + const candidateInit: RTCIceCandidateInit = { candidate: event.candidate, - sdpMid: event.sdpMid, sdpMLineIndex: event.sdpMLineIndex, - component: null, - foundation: null, - port: null, - priority: null, - protocol: null, - relatedAddress: null, - relatedPort: null, - tcpType: null, - usernameFragment: null, - type: null, - toJSON: () => { - return { candidate: event.candidate }; - } + sdpMid: event.sdpMid, }; - this.getConnection(event.senderConnectionId, 'Connection not found for connectionId ' + event.senderConnectionId + ' owning endpoint ' + event.endpointName + '. Ice candidate will be ignored: ' + candidate) + const iceCandidate = new RTCIceCandidate(candidateInit); + + this.getConnection(event.senderConnectionId, 'Connection not found for connectionId ' + event.senderConnectionId + ' owning endpoint ' + event.endpointName + '. Ice candidate will be ignored: ' + iceCandidate) .then(connection => { const stream: Stream = connection.stream!; - stream.getWebRtcPeer().addIceCandidate(candidate).catch(error => { + stream.getWebRtcPeer().addIceCandidate(iceCandidate).catch(error => { logger.error('Error adding candidate for ' + stream!.streamId + ' stream of endpoint ' + event.endpointName + ': ' + error); }); diff --git a/openvidu-browser/src/OpenViduInternal/WebRtcPeer/WebRtcPeer.ts b/openvidu-browser/src/OpenViduInternal/WebRtcPeer/WebRtcPeer.ts index f11ad6f0..e8ebd58f 100644 --- a/openvidu-browser/src/OpenViduInternal/WebRtcPeer/WebRtcPeer.ts +++ b/openvidu-browser/src/OpenViduInternal/WebRtcPeer/WebRtcPeer.ts @@ -80,12 +80,20 @@ export class WebRtcPeer { this.pc = new RTCPeerConnection({ iceServers: this.configuration.iceServers }); - this.pc.addEventListener('icecandidate', (event: RTCPeerConnectionIceEvent) => { - if (event.candidate != null) { - const candidate: RTCIceCandidate = event.candidate; - this.configuration.onIceCandidate(candidate); - if (candidate.candidate !== '') { - this.localCandidatesQueue.push({ candidate: candidate.candidate }); + this.pc.addEventListener("icecandidate", (event: RTCPeerConnectionIceEvent) => { + if (event.candidate !== null) { + // `RTCPeerConnectionIceEvent.candidate` is supposed to be an RTCIceCandidate: + // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnectioniceevent-candidate + // + // But in practice, it is actually an RTCIceCandidateInit that can be used to + // obtain a proper candidate, using the RTCIceCandidate constructor: + // https://w3c.github.io/webrtc-pc/#dom-rtcicecandidate-constructor + const candidateInit: RTCIceCandidateInit = event.candidate as RTCIceCandidateInit; + const iceCandidate = new RTCIceCandidate(candidateInit); + + this.configuration.onIceCandidate(iceCandidate); + if (iceCandidate.candidate !== '') { + this.localCandidatesQueue.push(iceCandidate); } } });