]>
Commit | Line | Data |
---|---|---|
80172072 RBR |
1 | import { io } from 'socket.io-client'; |
2 | import Peers from './peers'; | |
3 | import Media from './media'; | |
d9e5fa1a | 4 | |
80172072 | 5 | const 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 | |
80172072 RBR |
43 | console.log("Connected to signaling server, group: ", data.currentUrl); |
44 | internals.socket.emit('join', { | |
45 | 'url': 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 | |
80172072 | 56 | console.log(data); |
139f43c6 RBR |
57 | Peers.add(data.peer_id, internals.tada); |
58 | const peerId = data.peer_id; | |
59 | ||
60 | const peerConn = new RTCPeerConnection( | |
61 | {"iceServers": internals.kIceServers}, | |
62 | {"optional": [{"DtlsSrtpKeyAgreement": true}]} | |
63 | ); | |
64 | ||
65 | internals.peers[peerId] = peerConn; | |
66 | ||
67 | peerConn.onicecandidate = (event) => { | |
68 | if (event.candidate) { | |
69 | internals.socket.emit('relayICECandidate', { | |
70 | 'peer_id': peerId, | |
71 | 'ice_candidate': { | |
72 | 'sdpMLineIndex': event.candidate.sdpMLineIndex, | |
73 | 'candidate': event.candidate.candidate | |
74 | } | |
75 | }); | |
76 | } | |
77 | } | |
78 | ||
79 | peerConn.onaddstream = (stream) => { | |
80 | console.log(`Received stream for peer ${peerId}`); | |
81 | console.log(stream); | |
82 | } | |
83 | ||
84 | peerConn.addStream(mediaStream); | |
85 | ||
86 | if (data.should_create_offer) { | |
87 | console.log("Creating RTC offer to ", peerId); | |
88 | peerConn.createOffer((local_description) => { | |
89 | console.log("Local offer description is: ", local_description); | |
90 | peerConn.setLocalDescription(local_description, () => { | |
91 | internals.socket.emit('relaySessionDescription', { | |
92 | 'peer_id': peerId, | |
93 | 'session_description': local_description | |
94 | }); | |
95 | ||
96 | console.log("Offer setLocalDescription succeeded"); | |
97 | }, () => { console.log("Offer setLocalDescription failed!"); } | |
98 | ); | |
99 | }, | |
100 | (error) => { console.log("Error sending offer: ", error) } | |
101 | ); | |
102 | } | |
80172072 RBR |
103 | console.log(`There are now ${Peers.count()} participants`); |
104 | }); | |
d4fb48eb | 105 | |
80172072 | 106 | internals.socket.on('removePeer', function() { |
d4fb48eb | 107 | |
80172072 RBR |
108 | Peers.remove('id-'+(Peers.count() - 1)); // This is only for testing, don't use count to remove ids. |
109 | console.log(`There are now ${Peers.count()} participants`); | |
110 | }); | |
111 | } | |
112 | catch (err) { | |
d4fb48eb | 113 | |
80172072 RBR |
114 | internals.port.postMessage({ |
115 | action: 'error' | |
116 | }); | |
117 | internals.port.disconnect(); | |
118 | } | |
119 | }, | |
4757c461 | 120 | |
80172072 | 121 | hangUp() { |
4757c461 | 122 | |
80172072 RBR |
123 | Peers.reset(); |
124 | Media.stop(); | |
125 | internals.socket.close(); | |
126 | internals.port.disconnect(); | |
127 | } | |
128 | }; | |
d9e5fa1a | 129 | |
80172072 RBR |
130 | internals.port = chrome.runtime.connect({ name:"content" }); |
131 | internals.port.onMessage.addListener(internals.onMessage); | |
4757c461 | 132 | |
80172072 | 133 | console.log('Content Script Loaded'); |
4757c461 RBR |
134 | |
135 | // Indicates to the background script that we executed correctly | |
136 | true; |