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