From: Ruben Beltran del Rio Date: Wed, 13 Sep 2023 20:13:10 +0000 (+0200) Subject: Only send offer on one side X-Git-Url: https://git.r.bdr.sh/rbdr/junction/commitdiff_plain/3f2e36eabf1494ac60a3cf533d3db86d25a6cc39 Only send offer on one side --- diff --git a/extension/content_script.js b/extension/content_script.js index 721176e..c344bd9 100644 --- a/extension/content_script.js +++ b/extension/content_script.js @@ -50,9 +50,10 @@ const internals = { console.debug("disconnected from signaling server"); }); - socket.on("addPeer", ({ peerId }) => { + socket.on("addPeer", ({ peerId, shouldCreateOffer }) => { addPeer({ peerId, + shouldCreateOffer, mediaStream, onOffer: (data) => socket.emit("relayOffer", data), socket diff --git a/extension/peers.js b/extension/peers.js index f9153fd..be494b0 100644 --- a/extension/peers.js +++ b/extension/peers.js @@ -13,7 +13,7 @@ const internals = { }, }; -export function addPeer({ peerId, mediaStream, onOffer, socket }) { +export function addPeer({ peerId, shouldCreateOffer, mediaStream, onOffer, socket }) { const peerConnection = new RTCPeerConnection( { iceServers: internals.kIceServers }, { optional: [{ DtlsSrtpKeyAgreement: true }] }, @@ -42,11 +42,13 @@ export function addPeer({ peerId, mediaStream, onOffer, socket }) { }; peerConnection.onnegotiationneeded = async () => { - console.debug("Creating RTC offer to ", peerId); - const offer = await peerConnection.createOffer(); - await peerConnection.setLocalDescription(offer); + if (shouldCreateOffer) { + console.debug("Creating RTC offer to ", peerId); + const offer = await peerConnection.createOffer(); + await peerConnection.setLocalDescription(offer); - onOffer({ peerId, offer }); + onOffer({ peerId, offer }); + } }; console.info(`There are now ${countPeers()} participants`); @@ -58,6 +60,7 @@ export function removePeer({ peerId }) { } export async function answerPeerOffer({ peerId, offer }) { + console.info(`Answering peer ${peerId}`); const peerConnection = internals.peers[peerId]; const remoteDescription = new RTCSessionDescription(offer); @@ -70,13 +73,16 @@ export async function answerPeerOffer({ peerId, offer }) { } export async function processPeerAnswer({ peerId, answer }) { + console.info(`Processing answer for peer ${peerId}`); const peerConnection = internals.peers[peerId]; const remoteDescription = new RTCSessionDescription(answer); await peerConnection.setRemoteDescription(remoteDescription); } export async function addIceCandidate({ peerId, candidate }) { + console.info(`Adding ICE candidate for peer ${peerId}`); const peerConnection = internals.peers[peerId]; + console.info(peerConnection.signalingState); const iceCandidate = new RTCIceCandidate(candidate); await peerConnection.addIceCandidate(iceCandidate); } diff --git a/server/events.js b/server/events.js index bad9046..18b9dd4 100644 --- a/server/events.js +++ b/server/events.js @@ -22,9 +22,10 @@ export const types = { ICE_CANDIDATE_RECEIVED: "ICECandidateReceived", }; -export function addPeer(peerId) { +export function addPeer(peerId, shouldCreateOffer) { return internals.emitter.bind(null, types.ADD_PEER, { peerId, + shouldCreateOffer }); } diff --git a/server/index.js b/server/index.js index 8ac427b..e60795f 100644 --- a/server/index.js +++ b/server/index.js @@ -24,8 +24,8 @@ server.on(events.types.CONNECTION, (socket) => { const sockets = await server.in(room).fetchSockets(); sockets.forEach((peer) => { if (peer.id !== me) { - events.addPeer(me)(peer); - events.addPeer(peer.id)(socket); + events.addPeer(me, false)(peer); + events.addPeer(peer.id, true)(socket); } }); });