-'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']
- });
-
- internals.socket.on('error', function(error) {
-
- console.error('GENERAL ERROR', error);
- });
-
- internals.socket.on('connect_error', function(error) {
+ console.error('GENERAL ERROR', error);
+ });
- console.error('CONNNECT ERROR', error);
- });
-
- internals.socket.on('connect', function() {
+ internals.socket.on('connect_error', function(error) {
- console.log("Connected to signaling server, group: ", data.currentUrl);
- internals.socket.emit('join', {
- 'url': data.currentUrl,
- });
- });
+ console.error('CONNNECT ERROR', error);
+ });
- internals.socket.on('disconnect', function() {
+ internals.socket.on('connect', function() {
- console.log("disconnected from signaling server");
+ console.log("Connected to signaling server, group: ", data.currentUrl);
+ internals.socket.emit('join', {
+ 'url': data.currentUrl,
});
+ });
+
+ internals.socket.on('disconnect', function() {
+
+ console.log("disconnected from signaling server");
+ });
+
+ internals.socket.on('addPeer', function(data) {
+
+ console.log(data);
+ Peers.add(data.peer_id, internals.tada);
+ const peerId = data.peer_id;
+
+ const peerConn = new RTCPeerConnection(
+ {"iceServers": internals.kIceServers},
+ {"optional": [{"DtlsSrtpKeyAgreement": true}]}
+ );
+
+ internals.peers[peerId] = peerConn;
+
+ peerConn.onicecandidate = (event) => {
+ if (event.candidate) {
+ internals.socket.emit('relayICECandidate', {
+ 'peer_id': peerId,
+ 'ice_candidate': {
+ 'sdpMLineIndex': event.candidate.sdpMLineIndex,
+ 'candidate': event.candidate.candidate
+ }
+ });
+ }
+ }
+
+ peerConn.onaddstream = (stream) => {
+ console.log(`Received stream for peer ${peerId}`);
+ console.log(stream);
+ }
+
+ peerConn.addStream(mediaStream);
+
+ if (data.should_create_offer) {
+ console.log("Creating RTC offer to ", peerId);
+ peerConn.createOffer((local_description) => {
+ console.log("Local offer description is: ", local_description);
+ peerConn.setLocalDescription(local_description, () => {
+ internals.socket.emit('relaySessionDescription', {
+ 'peer_id': peerId,
+ 'session_description': local_description
+ });
+
+ console.log("Offer setLocalDescription succeeded");
+ }, () => { console.log("Offer setLocalDescription failed!"); }
+ );
+ },
+ (error) => { console.log("Error sending offer: ", error) }
+ );
+ }
+ console.log(`There are now ${Peers.count()} participants`);
+ });
+
+ internals.socket.on('removePeer', function() {
+
+ 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.socket.on('addPeer', function(data) {
-
- console.log(data);
- Peers.add('id-'+Peers.count(), internals.tada);
- console.log(`There are now ${Peers.count()} participants`);
- });
-
- internals.socket.on('removePeer', function() {
-
- 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();
+ }
+ },
- internals.port.postMessage({
- action: 'error'
- });
- internals.port.disconnect();
- }
- },
+ hangUp() {
- hangUp() {
+ Peers.reset();
+ Media.stop();
+ internals.socket.close();
+ internals.port.disconnect();
+ }
+};
- 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;