X-Git-Url: https://git.r.bdr.sh/rbdr/junction/blobdiff_plain/d9e5fa1a04a38e02c4dc5b5967e9198ddad3a15d..8a872a93634a516782d05934188ba2db3849fec9:/extension/content_script.js?ds=sidebyside diff --git a/extension/content_script.js b/extension/content_script.js index 70c085f..8ca721a 100644 --- a/extension/content_script.js +++ b/extension/content_script.js @@ -1,91 +1,158 @@ -'use strict'; +import { io } from 'socket.io-client'; +import Peers from './peers'; +import Media from './media'; -(() => { +const internals = { - const io = require('socket.io-client'); - const Peers = require('./peers'); - const Media = require('./media'); + kSocketUrl: 'https://junction.tranquil.services', + kIceServers: [ + {url:"stun:stun.l.google.com:19302"} + ], - const internals = { + port: null, + socket: null, + peers: {}, - kSocketUrl: 'https://junction.unlimited.pizza/', + onMessage(message) { + internals[message.action](message.data); + }, - port: null, - socket: null, - peers: 0, + async joinAudioCall(data) { - onMessage(message) { - internals[message.action](message.data); - }, + internals.tada = data.tada; // Keeping for fun - async joinAudioCall(data) { + try { + const mediaStream = await Media.start(); - internals.tada = data.tada; // Keeping for fun + internals.socket = io(internals.kSocketUrl, { + transports: ['websocket'] + }); - try { - const mediaStream = await Media.start(); + internals.socket.on('error', function(error) { - internals.socket = io(internals.kSocketUrl, { - transports: ['websocket'] - }); + console.error('GENERAL ERROR', error); + }); - internals.socket.on('error', function(error) { + internals.socket.on('connect_error', function(error) { - console.error('GENERAL ERROR', error); - }); + console.error('CONNNECT ERROR', error); + }); - internals.socket.on('connect_error', function(error) { + internals.socket.on('connect', function() { - console.error('CONNNECT ERROR', error); + console.log('Connected to signaling server, group: ', data.currentUrl); + internals.socket.emit('join', { + room: data.currentUrl, }); + }); + + 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; - internals.socket.on('connect', function() { + const peerConnection = new RTCPeerConnection( + { iceServers: internals.kIceServers }, + { optional: [{ DtlsSrtpKeyAgreement: true }] } + ); - console.log("Connected to signaling server, group: ", data.currentUrl); - internals.socket.emit('join', { - 'url': data.currentUrl, + internals.peers[peerId] = peerConnection; + mediaStream.getTracks().forEach((track) => { + peerConnection.addTrack(track, localStream); }); - }); - internals.socket.on('disconnect', function() { + peerConnection.onicecandidate = (event) => { + if (event.candidate) { + internals.socket.emit('relayICECandidate', { + peerId: peerId, + candidate: event.candidate + }); + } + } - console.log("disconnected from signaling server"); - }); + const remoteStream = new MediaStream(); + peerConnection.ontrack = (event) => { + remoteStream.addTrack(event.track); + const remoteAudioElement = new Audio(); + remoteAudioElement.srcObject = remoteStream; + remoteAudioElement.play(); + }; - internals.socket.on('addPeer', function(data) { + peerConnection.onnegotiationneeded = async () => { + console.log("Creating RTC offer to ", peerId); + const offer = await peerConnection.createOffer(); + await peerConnection.setLocalDescription(offer); - console.log(data); - Peers.add('id-'+Peers.count(), internals.tada); - console.log(`There are now ${Peers.count()} participants`); - }); + // Emit the offer to the peer + socket.emit('relayOffer', { offer, peerId }); + }; - internals.socket.on('removePeer', function() { + console.log(`There are now ${Peers.count()} participants`); + }); - Peers.remove('id-'+(Peers.count() - 1)); // This is only for testing, don't use count to remove ids. - console.log(`There are now ${Peers.count()} participants`); - }); - } - catch (err) { + socket.on('offerReceived', async (data) => { - internals.port.postMessage({ - action: 'error' - }); - internals.port.disconnect(); - } - }, + const peerConnection = internals.peers[data.peerId]; + + const offer = new RTCSessionDescription(data.offer); + await peerConnection.setRemoteDescription(offer); - hangUp() { + const answer = await peerConnection.createAnswer(); + await peerConnection.setLocalDescription(answer); - Peers.reset(); - Media.stop(); - internals.socket.close(); + // Send the answer to the peer + socket.emit('relayAnswer', { answer, peerId: data.peerId }); + }); + + socket.on('answerReceived', async (data) => { + + const peerConnection = internals.peers[data.peerId]; + const answer = new RTCSessionDescription(data.answer); + await peerConnection.setRemoteDescription(answer); + }); + + 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. + console.log(`There are now ${Peers.count()} participants`); + }); + } + catch (err) { + + internals.port.postMessage({ + 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.onMessage.addListener(internals.onMessage); - internals.port = chrome.runtime.connect({ name:"content" }); - internals.port.onMessage.addListener(internals.onMessage); -})(); +console.log('Content Script Loaded'); // Indicates to the background script that we executed correctly true;