-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplertc.js
248 lines (221 loc) · 7.24 KB
/
simplertc.js
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
let remoteStreams = {};
let localStream;
let defaultVideoConstraints = {
optional: [
{minWidth: 320},
{minWidth: 640},
{minWidth: 1024},
{minWidth: 1280},
{minWidth: 1920},
{minWidth: 2560},
]
};
let defaultAudioConstraints = true;
var defaultServers = {
'iceServers': [{
'urls': ['stun:stun.l.google.com:19302', 'stun:stun1.l.google.com:19302', 'stun:stun2.l.google.com:19302']
}]
};
export function getMedia(video = defaultVideoConstraints, audio = defaultAudioConstraints){
let constraints = {video, audio};
return new Promise(function (resolve, reject){
navigator.mediaDevices.getUserMedia(constraints)
.then(function (stream){
resolve(stream);
})
.catch(function (err){
reject(err);
});
});
};
export class Bond {
constructor(localMedia, id, sendMsgFunction, callbacks = {}, bandwidth = [250, 2250], servers = defaultServers){
console.log("=== CONSTRUCTOR ===");
this.localStream = localMedia;
this.remoteStream;
this.id = id;
this.callbacks = callbacks;
this.sendMessage = sendMsgFunction;
this.servers = servers;
this.bandwidth = bandwidth;
this.pc = new RTCPeerConnection(this.servers);
//function bindings
this.handleIceCandidate = this.handleIceCandidate.bind(this);
this.handleRemoteStreamAdded = this.handleRemoteStreamAdded.bind(this);
this.handleRemoteStreamRemoved = this.handleRemoteStreamRemoved.bind(this);
this.onDataChannelOpen = this.onDataChannelOpen.bind(this);
this.onDataChannelReceive = this.onDataChannelReceive.bind(this);
this.getBandwidth = this.getBandwidth.bind(this);
this.dataChannel = this.pc.createDataChannel("data", {negotiated: true, id: 7});
this.dataChannel.onopen = this.onDataChannelOpen;
this.dataChannel.onmessage = this.onDataChannelReceive;
this.pc.onicecandidate = this.handleIceCandidate;
this.pc.onaddstream = this.handleRemoteStreamAdded;
this.pc.onremovestream = this.handleRemoteStreamRemoved;
this.pc.addStream(this.localStream);
}
createAndSendOffer(){
console.log("=== CREATE AND SEND OFFER ===");
this.pc.createOffer()
.then((createdOffer) => {
this.pc.setLocalDescription(createdOffer);
createdOffer.sdp = this.setSessionDescriptionBandwidth(createdOffer.sdp);
this.sendMessage(createdOffer, this.id);
}).catch((error) => {
console.error('Failed to create session description: ' + error.toString());
});
}
receivedOffer(msg){
console.log("=== RECEIVED OFFER ===");
this.pc.setRemoteDescription(new RTCSessionDescription(msg));
let bw = this.getBandwidth(msg.sdp);
//if their bandwidth is set to lower than ours, default to theirs
if((this.bandwidth[0] + this.bandwidth[0]) > (bw[0] + bw[1])){
this.bandwidth = bw;
}
this.createAndSendAnswer();
}
createAndSendAnswer(){
console.log("=== CREATE AND SEND ANSWER ===");
this.pc.createAnswer()
.then((createdAnswer) => {
this.pc.setLocalDescription(createdAnswer);
createdAnswer.sdp = this.setSessionDescriptionBandwidth(createdAnswer.sdp);
this.sendMessage(createdAnswer, this.id);
}).catch((error) => {
console.error('Failed to create session description: ' + error.toString());
});
}
receivedAnswer(msg){
console.log("=== RECEIVED ANSWER ===");
this.pc.setRemoteDescription(new RTCSessionDescription(msg));
}
receivedIceCandidate(msg){
let candidate = new RTCIceCandidate({
sdpMLineIndex: msg.label,
candidate: msg.candidate
});
this.pc.addIceCandidate(candidate);
}
handleIceCandidate(event){
//so a network candidate became available
const peerConnection = event.target;
const iceCandidate = event.candidate;
if(iceCandidate){
console.log('icecandidate event: ', event);
if (event.candidate) {
this.sendMessage({
type: 'candidate',
label: event.candidate.sdpMLineIndex,
id: event.candidate.sdpMid,
candidate: event.candidate.candidate
}, this.id);
} else {
console.log('End of candidates.');
}
}
}
handleRemoteStreamAdded(event){
this.remoteStream = event.stream;
if(this.callbacks.remoteStreamAdded){
this.callbacks.remoteStreamAdded(this.remoteStream, this.id);
}
}
handleRemoteStreamRemoved(event){
this.remoteStream = null;
if(this.callbacks.remoteStreamRemoved){
this.callbacks.remoteStreamRemoved(this.id);
}
}
hangup(){
this.pc.close();
this.pc = null;
this.sendMessage('bye', this.id);
}
handleRemoteHangup(){
this.pc.close();
this.pc = null;
}
//// data channel
onDataChannelOpen(event){
console.log("=== DATA CHANNEL OPEN ===");
}
onDataChannelReceive(event){
console.log("received data", event);
this.callbacks.onDataReceive(event);
}
sendData(message){
this.dataChannel.send(message);
}
//// getters and setters
getIceConnectionState(){
if(!this.pc){
return false;
}
return this.pc.iceConnectionState;
}
getLocalStream(){
return this.localStream;
}
getRemoteStream(){
return this.remoteStream;
}
getCallbacks(){
return this.callbacks;
}
getSendMessage(){
return this.sendMessage;
}
getConnection(){
return this.pc;
}
getServers(){
return this.servers;
}
setBandwidth(bw = [250, 2250]){
this.bandwidth = bw;
this.createAndSendOffer();
}
getBandwidth(sdp){
var lines = sdp.split("\n");
var ret = [];
for (var i = 0; i < lines.length; i++) {
if(lines[i].indexOf("b=AS:") > -1){
ret.push(parseInt(lines[i].split(":")[1]));
}
}
return ret;
}
// UTILITY
setSessionDescriptionBandwidth(sdp) {
var modifier = 'AS';
var count = 0;
var lines = sdp.split("\n");
var add_audio = 0;
var add_video = 0;
for (var i = 0; i < lines.length; i++) {
let line = lines[i];
if(line.indexOf("m=audio") > -1){
if(lines[i+2].indexOf("b=AS") > -1){
lines[i+2] = "b=AS:" + this.bandwidth[0];
}else{
add_audio = i+2;
}
}
if(line.indexOf("m=video") > -1){
if(lines[i+2].indexOf("b=AS") > -1){
lines[i+2] = "b=AS:"+this.bandwidth[1];
}else{
add_video = i+2;
}
}
}
if(add_audio > 0){
lines.splice(add_audio, 0, "b=AS:"+this.bandwidth[0]);
}
if(add_video){
lines.splice((add_audio > 0) ? (add_video + 1) : add_video, 0, "b=AS:"+this.bandwidth[1]);
}
return lines.join("\n");
}
}