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