]> git.r.bdr.sh - rbdr/junction/blame - extension/content_script.js
Refactor server code
[rbdr/junction] / extension / content_script.js
CommitLineData
80172072
RBR
1import { io } from 'socket.io-client';
2import Peers from './peers';
3import Media from './media';
d9e5fa1a 4
80172072 5const internals = {
4757c461 6
139f43c6
RBR
7 kSocketUrl: 'https://junction.tranquil.services',
8 kIceServers: [
9 {url:"stun:stun.l.google.com:19302"}
10 ],
d4fb48eb 11
80172072
RBR
12 port: null,
13 socket: null,
139f43c6 14 peers: {},
d4fb48eb 15
80172072
RBR
16 onMessage(message) {
17 internals[message.action](message.data);
18 },
d4fb48eb 19
80172072 20 async joinAudioCall(data) {
4757c461 21
80172072 22 internals.tada = data.tada; // Keeping for fun
4757c461 23
80172072 24 try {
139f43c6 25 const mediaStream = await Media.start();
4757c461 26
80172072
RBR
27 internals.socket = io(internals.kSocketUrl, {
28 transports: ['websocket']
29 });
d9e5fa1a 30
80172072 31 internals.socket.on('error', function(error) {
d4fb48eb 32
80172072
RBR
33 console.error('GENERAL ERROR', error);
34 });
02071d8e 35
80172072 36 internals.socket.on('connect_error', function(error) {
02071d8e 37
80172072
RBR
38 console.error('CONNNECT ERROR', error);
39 });
02071d8e 40
80172072 41 internals.socket.on('connect', function() {
02071d8e 42
284fc661 43 console.log('Connected to signaling server, group: ', data.currentUrl);
80172072 44 internals.socket.emit('join', {
284fc661 45 room: data.currentUrl,
02071d8e 46 });
80172072 47 });
d4fb48eb 48
80172072 49 internals.socket.on('disconnect', function() {
d4fb48eb 50
80172072
RBR
51 console.log("disconnected from signaling server");
52 });
d4fb48eb 53
80172072 54 internals.socket.on('addPeer', function(data) {
d4fb48eb 55
284fc661
RBR
56 Peers.add(data.peerId, internals.tada);
57 const peerId = data.peerId;
139f43c6 58
284fc661
RBR
59 const peerConnection = new RTCPeerConnection(
60 { iceServers: internals.kIceServers },
61 { optional: [{ DtlsSrtpKeyAgreement: true }] }
139f43c6
RBR
62 );
63
284fc661
RBR
64 internals.peers[peerId] = peerConnection;
65 mediaStream.getTracks().forEach((track) => {
66 peerConnection.addTrack(track, localStream);
67 });
139f43c6 68
284fc661 69 peerConnection.onicecandidate = (event) => {
139f43c6
RBR
70 if (event.candidate) {
71 internals.socket.emit('relayICECandidate', {
284fc661
RBR
72 peerId: peerId,
73 candidate: event.candidate
139f43c6
RBR
74 });
75 }
76 }
77
284fc661
RBR
78 const remoteStream = new MediaStream();
79 peerConnection.ontrack = (event) => {
80 remoteStream.addTrack(event.track);
81 const remoteAudioElement = new Audio();
82 remoteAudioElement.srcObject = remoteStream;
83 remoteAudioElement.play();
84 };
85
86 peerConnection.onnegotiationneeded = async () => {
87 console.log("Creating RTC offer to ", peerId);
88 const offer = await peerConnection.createOffer();
89 await peerConnection.setLocalDescription(offer);
90
91 // Emit the offer to the peer
92 socket.emit('relayOffer', { offer, peerId });
93 };
139f43c6 94
80172072
RBR
95 console.log(`There are now ${Peers.count()} participants`);
96 });
d4fb48eb 97
284fc661
RBR
98 socket.on('offerReceived', async (data) => {
99
100 const peerConnection = internals.peers[data.peerId];
101
102 const offer = new RTCSessionDescription(data.offer);
103 await peerConnection.setRemoteDescription(offer);
104
105 const answer = await peerConnection.createAnswer();
106 await peerConnection.setLocalDescription(answer);
107
108 // Send the answer to the peer
109 socket.emit('relayAnswer', { answer, peerId: data.peerId });
110 });
111
112 socket.on('answerReceived', async (data) => {
113
114 const peerConnection = internals.peers[data.peerId];
115 const answer = new RTCSessionDescription(data.answer);
116 await peerConnection.setRemoteDescription(answer);
117 });
118
119 socket.on('ICECandidateReceived', async (data) => {
120
121 const peerConnection = internals.peers[data.peerId];
122 const candidate = new RTCIceCandidate(data.candidate);
123 await peerConnection.addIceCandidate(candidate);
124 });
125
126
80172072 127 internals.socket.on('removePeer', function() {
d4fb48eb 128
284fc661 129 delete internals.peers[data.peerId];
80172072
RBR
130 Peers.remove('id-'+(Peers.count() - 1)); // This is only for testing, don't use count to remove ids.
131 console.log(`There are now ${Peers.count()} participants`);
132 });
133 }
134 catch (err) {
d4fb48eb 135
80172072
RBR
136 internals.port.postMessage({
137 action: 'error'
138 });
139 internals.port.disconnect();
140 }
141 },
4757c461 142
80172072 143 hangUp() {
4757c461 144
80172072
RBR
145 Peers.reset();
146 Media.stop();
147 internals.socket.close();
148 internals.port.disconnect();
149 }
150};
d9e5fa1a 151
80172072
RBR
152internals.port = chrome.runtime.connect({ name:"content" });
153internals.port.onMessage.addListener(internals.onMessage);
4757c461 154
80172072 155console.log('Content Script Loaded');
4757c461
RBR
156
157// Indicates to the background script that we executed correctly
158true;