forked from mozilla/webrtc-landing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_test.html
419 lines (375 loc) · 12.6 KB
/
data_test.html
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<html>
<head>
<title>Simple WebRTC Data Channel Test</title>
</head>
<body>
<table width=100% height=100%>
<tr><td><h1>Simple WebRTC Data Channel Test</h1></td></tr>
<tr><td>(Note: this JS code is REALLY UGLY)<b><br>
It also has a bunch of code to allow testing connections in both
directions, and pre-negotiated connections.
</td></tr>
<tr><td><div>
<button id="thebutton" onClick="start();">Start!</button>
<button id="closechan" onClick="stopchannel();">Stop Channel</button>
<input type="checkbox" id="preset" value="Use pre-negotiated channel 5">
<label for="preset">Use pre-negotiated channel</label>
<input type="number" id="stream_num" value="5">
<input type="checkbox" id="unordered" value="Use unordered data transfer">
<label for="unordered">Use unordered data transfer</label>
<button id="throughput" onClick="TestSpeed();">Throughput Test</button>
<label for="throughput">Test throughput</label>
</div><br/></td></tr>
<tr><td><form id="pc1_form" action="javascript:sendit(1)">
<div>pc1 says: <input id="pc1_input" type="text" value="type here" onKeyPress="return submitenter(this,event)"/>
<input type="submit"/></div></form></td></tr>
<tr><td><form id="pc1_blob" action="javascript:sendblob(1)">
<div>pc1 sends a blob: <input id="pc1_browse" type="file"/>
<input type="submit"/></div></form></td></tr>
<tr><td><form id="pc2_form" action="javascript:sendit(2)">
<div>pc2 says: <input id="pc2_input" type="text" value="type here" onKeyPress="return submitenter(this,event)"/>
<input type="submit"/></div></form></td></tr>
<tr><td><form id="pc2_blob" action="javascript:sendblob(2)">
<div>pc2 sends a blob: <input id="pc2_browse" type="file"/>
<input type="submit"/></div></form></td></tr>
<tr><td><div id="datawindow" style="
width: 100%;
height: 500px;
overflow: auto;
border: 1px solid red;"></div></td></tr>
<script type="application/javascript">
let button = document.getElementById("thebutton");
let text_pc1 = document.getElementById("pc1_input");
let text_pc2 = document.getElementById("pc2_input");
let blob_pc1 = document.getElementById("pc1_browse");
let blob_pc2 = document.getElementById("pc2_browse");
let datawindow = document.getElementById("datawindow");
let preset = document.getElementById("preset");
preset.checked = false;
let unordered = document.getElementById("unordered");
unordered.checked = false;
let stream_num = document.getElementById("stream_num");
let pc1;
let pc2;
let dc1;
let dc2;
let channel1;
let channel2;
let num_channels;
num_channels = 0;
var datachannels = new Array(0);
let pc1_offer;
let pc2_answer;
let iter = 0;
let iter2 = 0;
var testDataChannelInterval;
function log(msg) {
let div = document.getElementById("datawindow");
div.innerHTML = div.innerHTML + "<p>" + msg + "</p>";
}
var fancy_log = function(msg,color) {
var pre = document.createElement("p");
var message = '<span style="color: ' + color + ';">' + msg + '</span>';
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
datawindow.appendChild(pre); // (window).* here doesn't work right
pre.scrollIntoView(false);
};
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13) {
myfield.form.submit();
return false;
} else {
return true;
}
}
var sendit = function (which) {
iter = iter + 1;
//log("Sending message #" + iter + " this = " + this);
if (which == 1) {
dc1.send(text_pc1.value);
text_pc1.value = "";
} else if (which == 2) {
dc2.send(text_pc2.value);
text_pc2.value = "";
} else {
log("Unknown send " + which);
}
};
var sendblob = function (which) {
iter = iter + 1;
//log("Sending blob #" + iter + " this = " + this);
if (which == 1) {
dc1.send(blob_pc1.files[0]);
blob_pc1.value = "";
} else if (which == 2) {
dc2.send(blob_pc2.files[0]);
blob_pc2.value = "";
} else {
log("Unknown sendblob " + which);
}
};
function failed(code) {
log("Failure callback: " + code);
}
// pc1.createOffer finished, call pc1.setLocal
function step1(offer) {
pc2 = new RTCPeerConnection();
pc1.didSetRemote = false;
pc2.didSetRemote = false;
pc1.ice_queued = [];
pc2.ice_queued = [];
pc2.ondatachannel = function(event) {
let mychannel = event.channel;
log("pc2 onDataChannel [" +num_channels + "] = " + mychannel +
", stream=" + mychannel.stream + " id=" + mychannel.id +
", ordered=" + mychannel.ordered +
", label='" + mychannel.label + "'" +
", protocol='" + mychannel.protocol + "'");
datachannels[num_channels] = mychannel;
num_channels++;
mychannel.binaryType = "blob";
if (!preset.checked) {
dc2 = mychannel;
}
mychannel.onmessage = function(evt) {
iter2 = iter2 + 1;
if (evt.data instanceof Blob) {
fancy_log("*** pc1 sent Blob: " + evt.data + ", length=" + evt.data.size,"red");
} else {
if (!testDataChannelInterval) {
fancy_log("pc1 said: " + evt.data,"red");
}
}
};
mychannel.onopen = function() {
log("*** pc2 onopen fired, sending to " + mychannel);
mychannel.send("pc2 says Hi there!");
};
mychannel.onclose = function() {
log("*** pc2 onclose fired");
if (testDataChannelInterval) {
clearInterval(testDataChannelInterval);
testDataChannelInterval = null;
}
};
mychannel.onbufferedamountlow = function() {
log("*** bufferedAmountLow went below " + mychannel.bufferAmountLowThreshold +
" (currently " + mychannel.bufferedAmount + ")");
};
mychannel.bufferedAmountLowThreshold = 1024*1024;
};
pc2.onclosedconnection = function() {
log("pc2 onClosedConnection ");
}
pc2.onaddstream = function(obj) {
log("pc2 got remote stream from pc1 " + obj.type);
}
pc2.onicecandidate = function(obj) {
if (obj.candidate) {
log("pc2 found ICE candidate: " + JSON.stringify(obj.candidate));
if (pc1.didSetRemote) {
pc1.addIceCandidate(obj.candidate);
} else {
pc1.ice_queued.push(obj.candidate);
}
} else {
log("pc2 got end-of-candidates signal");
}
}
pc1_offer = offer;
log("Offer: " + offer.sdp);
pc1.onicecandidate = function(obj) {
if (obj.candidate) {
log("pc1 found ICE candidate: " + JSON.stringify(obj.candidate));
if (pc2.didSetRemote) {
pc2.addIceCandidate(obj.candidate);
} else {
pc2.ice_queued.push(obj.candidate);
}
} else {
log("pc1 got end-of-candidates signal");
}
}
pc1.setLocalDescription(offer, step1_5, failed);
}
function step1_5() {
setTimeout(step2,0);
}
// pc1.setLocal finished, call pc2.setRemote
function step2() {
pc2.setRemoteDescription(pc1_offer, step3, failed);
};
// pc2.setRemote finished, call pc2.createAnswer
function step3() {
pc2.didSetRemote = true;
while (pc2.ice_queued.length > 0) {
pc2.addIceCandidate(pc2.ice_queued.shift());
}
pc2.createAnswer(step4, failed);
}
// pc2.createAnswer finished, call pc2.setLocal
function step4(answer) {
pc2_answer = answer;
log("Answer: " + answer.sdp);
pc2.setLocalDescription(answer, step5, failed);
}
// pc2.setLocal finished, call pc1.setRemote
function step5() {
pc1.setRemoteDescription(pc2_answer, step6, failed);
}
// pc1.setRemote finished, make a data channel
function step6() {
pc1.didSetRemote = true;
while (pc1.ice_queued.length > 0) {
pc1.addIceCandidate(pc1.ice_queued.shift());
}
log("HIP HIP HOORAY");
}
function start() {
button.innerHTML = "Stop!";
button.onclick = stop;
while (datawindow.firstChild) {
datawindow.removeChild(datawindow.firstChild);
}
pc1 = new RTCPeerConnection();
pc1.onaddstream = function(obj) {
log("pc1 got remote stream from pc2 " + obj.type);
}
pc1.ondatachannel = function(event) {
let mychannel = event.channel;
// In case pc2 opens a channel
log("pc1 onDataChannel [" +num_channels + "] = " + mychannel +
", stream=" + mychannel.stream + " id=" + mychannel.id +
", ordered=" + mychannel.ordered +
", label='" + mychannel.label + "'" +
", protocol='" + mychannel.protocol + "'");
datachannels[num_channels] = mychannel;
num_channels++;
mychannel.onmessage = function(evt) {
if (evt.data instanceof Blob) {
fancy_log("*** pc2 sent Blob: " + evt.data + ", length=" + evt.data.size,"blue");
} else {
if (!testDataChannelInterval) {
fancy_log('pc2 said: ' + evt.data,"blue");
}
}
}
mychannel.onopen = function() {
log("pc1 onopen fired for " + mychannel);
mychannel.send("pc1 says Hello out there...");
log("pc1 state: " + mychannel.state);
}
mychannel.onclose = function() {
log("pc1 onclose fired");
if (testDataChannelInterval) {
clearInterval(testDataChannelInterval);
testDataChannelInterval = null;
}
};
mychannel.onbufferedamountlow = function() {
log("*** bufferedAmountLow went below " + mychannel.bufferAmountLowThreshold +
" (currently " + mychannel.bufferedAmount + ")");
};
mychannel.bufferedAmountLowThreshold = 1024*1024;
}
pc1.onclosedconnection = function() {
log("pc1 onClosedConnection ");
}
dict = preset.checked ? {protocol: "text/plain", negotiated:true,
id:stream_num.value} : {}; // reliable (TCP-like)
dc1 = pc1.createDataChannel("This is pc1", dict);
log("pc1 ordered=" + dc1.ordered);
dc1.binaryType = "blob";
log("pc1 label " + dc1.label +
", stream=" + dc1.stream + " id=" + dc1.id);
dc1.onmessage = function(evt) {
if (evt.data instanceof Blob) {
fancy_log("*** pc2 sent Blob: " + evt.data + ", length=" + evt.data.size,"blue");
} else {
if (!testDataChannelInterval) {
fancy_log('pc2 said: ' + evt.data,"blue");
}
}
}
dc1.onopen = function() {
log("pc1 onopen fired for " + dc1);
if (preset.checked) {
dict = preset.checked ? {protocol:"text/chat", negotiated:true,
id:stream_num.value } : {}; // reliable (TCP-like)
dc2 = pc2.createDataChannel("This is pc2", dict);
let ev = {};
ev.channel = dc2;
pc2.ondatachannel(ev);
}
dc1.send("pc1 says this will likely be queued...");
}
dc1.onclose = function() {
log("pc1 onclose fired");
if (testDataChannelInterval) {
clearInterval(testDataChannelInterval);
testDataChannelInterval = null;
}
};
dc1.onbufferedamountlow = function() {
log("*** bufferedAmountLow went below " + dc1.bufferAmountLowThreshold +
" (currently " + dc1.bufferedAmount + ")");
};
dc1.bufferedAmountLowThreshold = 1024*1024;
pc1.createOffer(step1, failed);
}
function stopchannel() {
log("close one channel");
dc1.close();
// dc2.close();
}
function stop() {
log("closed");
pc1.close();
pc2.close();
button.innerHTML = "Start!";
button.onclick = start;
}
function TestSpeed() {
if (!testDataChannelInterval) {
TestThroughput(dc1, 200000);
} else {
clearInterval(testDataChannelInterval);
testDataChannelInterval = null;
}
}
function TestThroughput(channel, size) {
if (channel) {
var amountSent = 0;
var startTime = new Date().getTime();
var rate = 0;
var msg = "";
for(var i = 0; i < size; ++i) {
msg += "A";
}
var index = 0;
testDataChannelInterval = setInterval(
function () {
//var currentTime = new Date().getTime();
var tmp = index + ": " + msg;
index++;
channel.send(tmp);
amountSent += tmp.length;
//rate = amountSent / (currentTime - startTime);
if (index % 500 == 0) {
var currentTime = new Date().getTime();
rate = amountSent / (currentTime - startTime);
log("Current throughput: " + rate);
dump("Current throughput: " + rate + "\n");
}
},
2);
}
}
</script>
</html>