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