]> git.r.bdr.sh - rbdr/junction/blame - extension/content_script.js
Only send offer on one side
[rbdr/junction] / extension / content_script.js
CommitLineData
b9a2baf2 1import { io } from "socket.io-client";
06b7617a
RBR
2import { initializeTada } from "./tada";
3import {
4 addPeer,
5 addIceCandidate,
6 answerPeerOffer,
7 processPeerAnswer,
8 removePeer,
9 resetPeers,
10} from "./peers";
11import { startMedia, stopMedia } from "./media";
d9e5fa1a 12
80172072 13const internals = {
b9a2baf2
RBR
14 kSocketUrl: "https://junction.tranquil.services",
15 kIceServers: [{ urls: "stun:stun.l.google.com:19302" }],
d4fb48eb 16
80172072
RBR
17 port: null,
18 socket: null,
d4fb48eb 19
80172072
RBR
20 onMessage(message) {
21 internals[message.action](message.data);
22 },
d4fb48eb 23
06b7617a
RBR
24 onError(error) {
25 // TODO: How do we want to handle errors?
26 console.error(error.stack || error);
27 },
28
b9a2baf2 29 async joinAudioCall({ currentUrl, tada }) {
80172072 30 try {
06b7617a
RBR
31 const mediaStream = await startMedia();
32 const playTada = initializeTada(tada);
4757c461 33
06b7617a 34 const socket = (internals.socket = io(internals.kSocketUrl, {
b9a2baf2 35 transports: ["websocket"],
06b7617a 36 }));
d9e5fa1a 37
06b7617a
RBR
38 socket.on("error", internals.handleError);
39 socket.on("connect_error", internals.handleError);
02071d8e 40
06b7617a
RBR
41 socket.on("connect", () => {
42 console.debug("Connected to signaling server, group: ", currentUrl);
43 playTada();
44 socket.emit("join", {
b9a2baf2 45 room: currentUrl,
02071d8e 46 });
80172072 47 });
d4fb48eb 48
06b7617a
RBR
49 socket.on("disconnect", () => {
50 console.debug("disconnected from signaling server");
80172072 51 });
d4fb48eb 52
3f2e36ea 53 socket.on("addPeer", ({ peerId, shouldCreateOffer }) => {
06b7617a
RBR
54 addPeer({
55 peerId,
3f2e36ea 56 shouldCreateOffer,
06b7617a
RBR
57 mediaStream,
58 onOffer: (data) => socket.emit("relayOffer", data),
59 socket
b9a2baf2 60 });
06b7617a 61 playTada();
80172072 62 });
d4fb48eb 63
06b7617a
RBR
64 socket.on("offerReceived", async ({ peerId, offer }) => {
65 socket.emit("relayAnswer", await answerPeerOffer({ peerId, offer }));
284fc661
RBR
66 });
67
06b7617a
RBR
68 socket.on("answerReceived", (data) => processPeerAnswer(data));
69 socket.on("ICECandidateReceived", (data) => addIceCandidate(data));
70 socket.on("removePeer", (data) => removePeer(data));
b9a2baf2 71 } catch (err) {
80172072 72 internals.port.postMessage({
b9a2baf2 73 action: "error",
80172072
RBR
74 });
75 internals.port.disconnect();
76 }
77 },
4757c461 78
80172072 79 hangUp() {
06b7617a
RBR
80 resetPeers();
81 stopMedia();
80172072
RBR
82 internals.socket.close();
83 internals.port.disconnect();
b9a2baf2 84 },
80172072 85};
d9e5fa1a 86
b9a2baf2 87internals.port = chrome.runtime.connect({ name: "content" });
80172072 88internals.port.onMessage.addListener(internals.onMessage);
4757c461 89
06b7617a 90console.debug("Content Script Loaded");
4757c461
RBR
91
92// Indicates to the background script that we executed correctly
93true;