-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwebrtc.js
More file actions
221 lines (183 loc) · 6.83 KB
/
webrtc.js
File metadata and controls
221 lines (183 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* This file is part of VILLASweb.
*
* VILLASweb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VILLASweb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VILLASweb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
import AppDispatcher from '../app-dispatcher';
const OFFSET_TYPE = 2;
const OFFSET_VERSION = 4;
class WebRTC {
constructor(sessionurl, identifier, callbacks) {
this.identifier = identifier
this.first = false;
this.polite = false;
this.ignoreOffer = false;
this.makingOffer = false;
this.peerConnection = null;
this.dataChannel = null;
this.signalingClient = null;
this.iceUsername = 'villas';
this.icePassword = 'villas';
this.iceUrls = [
'stun:stun.0l.de:3478',
'turn:turn.0l.de:3478?transport=udp',
'turn:turn.0l.de:3478?transport=tcp'
];
console.log(callbacks)
this.onOpen = callbacks.onOpen.bind(this);
this.onMessage = callbacks.onMessage.bind(this);
this.onClose = callbacks.onClose.bind(this);
this.connectPeers(sessionurl, callbacks);
}
connectPeers(sessionurl, callbacks) {
// Create the local connection and its event listeners
this.peerConnection = new RTCPeerConnection({
iceServers: [{
username: this.iceUsername,
credential: this.icePassword,
urls: this.iceUrls
}]
});
this.peerConnection.onicecandidate = this.handleIceCandidate.bind(this);
this.peerConnection.onnegotiationneeded = this.handleNegotationNeeded.bind(this);
this.peerConnection.ondatachannel = this.handleNewDataChannel.bind(this)
this.peerConnection.onconnectionstatechange = () => console.info('Connection state changed:', this.peerConnection.connectionState);
this.peerConnection.onsignalingstatechange = () => console.info('Signaling state changed:', this.peerConnection.signalingState);
this.peerConnection.oniceconnectionstatechange = () => console.info('ICE connection state changed:', this.peerConnection.iceConnectionState);
this.peerConnection.onicegatheringstatechange = () => console.info('ICE gathering state changed:', this.peerConnection.iceGatheringState);
this.hallo()
this.signalingClient = new WebSocket(sessionurl);
this.signalingClient.onmessage = this.handleSignalingMessage.bind(this);
// Some more logging
this.signalingClient.onopen = (e) => console.info('Connected to signaling channel', e);
this.signalingClient.onerror = (e) => console.error('Failed to establish signaling connection', e);
}
hallo() {
console.info("peer connection (hallo):")
console.info(this.peerConnection)
}
handleIceCandidate(event) {
if (event.candidate == null) {
console.info('Candidate gathering completed');
return;
}
console.info('New local ICE Candidate', event.candidate);
let msg = {
candidate: event.candidate.toJSON()
};
console.info('Sending signaling message', msg);
this.signalingClient.send(JSON.stringify(msg));
}
async handleNegotationNeeded() {
console.info('Negotation needed!');
try {
this.makingOffer = true;
await this.peerConnection.setLocalDescription();
let msg = {
description: this.peerConnection.localDescription.toJSON()
};
console.info('Sending signaling message', msg);
this.signalingClient.send(JSON.stringify(msg));
} catch (err) {
console.error(err);
} finally {
this.makingOffer = false;
}
}
handleNewDataChannel(e) {
console.info('New datachannel', e.channel)
this.handleDataChannel(e.channel);
}
handleDataChannel(ch) {
this.dataChannel = ch;
this.dataChannel.onopen = () => console.info('Datachannel opened');
this.dataChannel.onclose = () => console.info('Datachannel closed');
this.dataChannel.onmessage = this.handleDataChannelMessage.bind(this);
}
async handleSignalingMessage(event) {
let msg = JSON.parse(event.data);
console.info('Received signaling message', msg);
try {
if (msg.control !== undefined) {
this.first = true;
for (var connection of msg.control.connections) {
if (connection.id < msg.control.connection_id)
this.first = false;
}
this.polite = this.first;
console.info('Role', {
polite: this.polite,
first: this.first
})
if (!this.first) {
// Create the data channel and establish its event listeners
let ch = this.peerConnection.createDataChannel('villas');
this.handleDataChannel(ch);
}
} else if (msg.description !== undefined) {
const offerCollision = (msg.description.type == 'offer') &&
(this.makingOffer || this.peerConnection.signalingState != 'stable');
this.ignoreOffer = !this.polite && offerCollision;
if (this.ignoreOffer) {
return;
}
await this.peerConnection.setRemoteDescription(msg.description);
console.info(msg.description);
if (msg.description.type == 'offer') {
await this.peerConnection.setLocalDescription();
let msg = {
description: this.peerConnection.localDescription.toJSON()
}
this.signalingClient.send(JSON.stringify(msg))
}
} else if (msg.candidate !== undefined) {
try {
console.info('New remote ICE candidate', msg.candidate);
await this.peerConnection.addIceCandidate(msg.candidate);
} catch (err) {
if (!this.ignoreOffer) {
throw err;
}
}
}
} catch (err) {
console.error(err);
}
}
// Handle onmessage events for the receiving channel.
// These are the data messages sent by the sending channel.
async handleDataChannelMessage(event) {
let data = await event.data.arrayBuffer()
this.onMessage(data, this.identifier)
}
disconnectPeers() {
console.log("disconnecting peers")
if (this.signalingClient) {
console.info("close signaling client")
this.signalingClient.close()
}
if (this.dataChannel) {
console.info("close data channel")
this.dataChannel.close();
}
if (this.peerConnection) {
console.info("close peer connection")
this.peerConnection.close();
}
this.dataChannel = null;
this.peerConnection = null;
this.signalingClient = null;
}
}
export default WebRTC;