X-Git-Url: https://git.r.bdr.sh/rbdr/junction/blobdiff_plain/6a3568bc5942423b93ba2749036489ad9e38e7cf..b9a2baf2a7c957089e63844b857e05c07b87c2fb:/extension/content_script.js diff --git a/extension/content_script.js b/extension/content_script.js index 8ca721a..26da2b0 100644 --- a/extension/content_script.js +++ b/extension/content_script.js @@ -1,13 +1,10 @@ -import { io } from 'socket.io-client'; -import Peers from './peers'; -import Media from './media'; +import { io } from "socket.io-client"; +import Peers from "./peers"; +import Media from "./media"; const internals = { - - kSocketUrl: 'https://junction.tranquil.services', - kIceServers: [ - {url:"stun:stun.l.google.com:19302"} - ], + kSocketUrl: "https://junction.tranquil.services", + kIceServers: [{ urls: "stun:stun.l.google.com:19302" }], port: null, socket: null, @@ -17,63 +14,58 @@ const internals = { internals[message.action](message.data); }, - async joinAudioCall(data) { - - internals.tada = data.tada; // Keeping for fun - + async joinAudioCall({ currentUrl, tada }) { try { const mediaStream = await Media.start(); internals.socket = io(internals.kSocketUrl, { - transports: ['websocket'] + transports: ["websocket"], }); - internals.socket.on('error', function(error) { - - console.error('GENERAL ERROR', error); + internals.socket.on("error", function (error) { + console.error("GENERAL ERROR", error); }); - internals.socket.on('connect_error', function(error) { - - console.error('CONNNECT ERROR', error); + internals.socket.on("connect_error", function (error) { + console.error("CONNNECT ERROR", error); }); - internals.socket.on('connect', function() { - - console.log('Connected to signaling server, group: ', data.currentUrl); - internals.socket.emit('join', { - room: data.currentUrl, + internals.socket.on("connect", function () { + console.log("Connected to signaling server, group: ", currentUrl); + internals.socket.emit("join", { + room: currentUrl, }); }); - internals.socket.on('disconnect', function() { - + internals.socket.on("disconnect", function () { console.log("disconnected from signaling server"); }); - internals.socket.on('addPeer', function(data) { - - Peers.add(data.peerId, internals.tada); - const peerId = data.peerId; - - const peerConnection = new RTCPeerConnection( - { iceServers: internals.kIceServers }, - { optional: [{ DtlsSrtpKeyAgreement: true }] } - ); - - internals.peers[peerId] = peerConnection; - mediaStream.getTracks().forEach((track) => { - peerConnection.addTrack(track, localStream); - }); + internals.socket.on("addPeer", function ({ peerId }) { + /** + * Eventually the whole rtc connection logic should be moved to Peers. + * Now it only plays tadas. + */ + Peers.add(peerId, tada); + + const peerConnection = new RTCPeerConnection( + { iceServers: internals.kIceServers }, + { optional: [{ DtlsSrtpKeyAgreement: true }] }, + ); + + internals.peers[peerId] = peerConnection; + mediaStream.getTracks().forEach((track) => { + peerConnection.addTrack(track, mediaStream); + }); - peerConnection.onicecandidate = (event) => { - if (event.candidate) { - internals.socket.emit('relayICECandidate', { - peerId: peerId, - candidate: event.candidate - }); - } + peerConnection.onicecandidate = (event) => { + if (event.candidate) { + internals.socket.emit("relayICECandidate", { + peerId: peerId, + candidate: event.candidate, + }); } + }; const remoteStream = new MediaStream(); peerConnection.ontrack = (event) => { @@ -89,70 +81,65 @@ const internals = { await peerConnection.setLocalDescription(offer); // Emit the offer to the peer - socket.emit('relayOffer', { offer, peerId }); + internals.socket.emit("relayOffer", { offer, peerId }); }; console.log(`There are now ${Peers.count()} participants`); }); - socket.on('offerReceived', async (data) => { - - const peerConnection = internals.peers[data.peerId]; + internals.socket.on("offerReceived", async ({ offer, peerId }) => { + const peerConnection = internals.peers[peerId]; - const offer = new RTCSessionDescription(data.offer); - await peerConnection.setRemoteDescription(offer); + const remoteDescription = new RTCSessionDescription(offer); + await peerConnection.setRemoteDescription(remoteDescription); const answer = await peerConnection.createAnswer(); await peerConnection.setLocalDescription(answer); // Send the answer to the peer - socket.emit('relayAnswer', { answer, peerId: data.peerId }); + internals.socket.emit("relayAnswer", { answer, peerId }); }); - socket.on('answerReceived', async (data) => { - - const peerConnection = internals.peers[data.peerId]; - const answer = new RTCSessionDescription(data.answer); - await peerConnection.setRemoteDescription(answer); + internals.socket.on("answerReceived", async ({ answer, peerId }) => { + const peerConnection = internals.peers[peerId]; + const remoteDescription = new RTCSessionDescription(answer); + await peerConnection.setRemoteDescription(remoteDescription); }); - socket.on('ICECandidateReceived', async (data) => { - - const peerConnection = internals.peers[data.peerId]; - const candidate = new RTCIceCandidate(data.candidate); - await peerConnection.addIceCandidate(candidate); - }); - - - internals.socket.on('removePeer', function() { - - delete internals.peers[data.peerId]; - Peers.remove('id-'+(Peers.count() - 1)); // This is only for testing, don't use count to remove ids. + internals.socket.on( + "ICECandidateReceived", + async ({ candidate, peerId }) => { + const peerConnection = internals.peers[peerId]; + const iceCandidate = new RTCIceCandidate(candidate); + await peerConnection.addIceCandidate(iceCandidate); + }, + ); + + internals.socket.on("removePeer", function ({ peerId }) { + delete internals.peers[peerId]; + Peers.remove(peerId); console.log(`There are now ${Peers.count()} participants`); }); - } - catch (err) { - + } catch (err) { internals.port.postMessage({ - action: 'error' + action: "error", }); internals.port.disconnect(); } }, hangUp() { - Peers.reset(); Media.stop(); internals.socket.close(); internals.port.disconnect(); - } + }, }; -internals.port = chrome.runtime.connect({ name:"content" }); +internals.port = chrome.runtime.connect({ name: "content" }); internals.port.onMessage.addListener(internals.onMessage); -console.log('Content Script Loaded'); +console.log("Content Script Loaded"); // Indicates to the background script that we executed correctly true;