]> git.r.bdr.sh - rbdr/junction/blobdiff - extension/content_script.js
Add new RTC code
[rbdr/junction] / extension / content_script.js
index 279c52de5adad64e34da41a00eb78d05d235c7d8..8ca721a70cc3a93ef58b0cb29dc22ad9b514d5bb 100644 (file)
@@ -4,11 +4,14 @@ import Media from './media';
 
 const internals = {
 
 
 const internals = {
 
-  kSocketUrl: 'ws://localhost:8000',
+  kSocketUrl: 'https://junction.tranquil.services',
+       kIceServers: [
+               {url:"stun:stun.l.google.com:19302"}
+       ],
 
   port: null,
   socket: null,
 
   port: null,
   socket: null,
-  peers: 0,
+  peers: {},
 
   onMessage(message) {
     internals[message.action](message.data);
 
   onMessage(message) {
     internals[message.action](message.data);
@@ -19,7 +22,7 @@ const internals = {
     internals.tada = data.tada; // Keeping for fun
 
     try {
     internals.tada = data.tada; // Keeping for fun
 
     try {
-      await Media.start();
+      const mediaStream = await Media.start();
 
       internals.socket = io(internals.kSocketUrl, {
         transports: ['websocket']
 
       internals.socket = io(internals.kSocketUrl, {
         transports: ['websocket']
@@ -37,9 +40,9 @@ const internals = {
 
       internals.socket.on('connect', function() {
 
 
       internals.socket.on('connect', function() {
 
-        console.log("Connected to signaling server, group: ", data.currentUrl);
+        console.log('Connected to signaling server, group: ', data.currentUrl);
         internals.socket.emit('join', {
         internals.socket.emit('join', {
-          'url': data.currentUrl,
+          room: data.currentUrl,
         });
       });
 
         });
       });
 
@@ -50,13 +53,80 @@ const internals = {
 
       internals.socket.on('addPeer', function(data) {
 
 
       internals.socket.on('addPeer', function(data) {
 
-        console.log(data);
-        Peers.add('id-'+Peers.count(), internals.tada);
+          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);
+          });
+
+          peerConnection.onicecandidate = (event) => {
+            if (event.candidate) {
+              internals.socket.emit('relayICECandidate', {
+                peerId: peerId,
+                candidate: event.candidate
+              });
+            }
+          }
+
+        const remoteStream = new MediaStream();
+        peerConnection.ontrack = (event) => {
+          remoteStream.addTrack(event.track);
+          const remoteAudioElement = new Audio();
+          remoteAudioElement.srcObject = remoteStream;
+          remoteAudioElement.play();
+        };
+
+        peerConnection.onnegotiationneeded = async () => {
+          console.log("Creating RTC offer to ", peerId);
+          const offer = await peerConnection.createOffer();
+          await peerConnection.setLocalDescription(offer);
+
+          // Emit the offer to the peer
+          socket.emit('relayOffer', { offer, peerId });
+        };
+
         console.log(`There are now ${Peers.count()} participants`);
       });
 
         console.log(`There are now ${Peers.count()} participants`);
       });
 
+      socket.on('offerReceived', async (data) => {
+
+                               const peerConnection = internals.peers[data.peerId];
+
+        const offer = new RTCSessionDescription(data.offer);
+        await peerConnection.setRemoteDescription(offer);
+
+        const answer = await peerConnection.createAnswer();
+        await peerConnection.setLocalDescription(answer);
+
+        // 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() {
 
       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`);
       });
         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`);
       });