This repository has been archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.js
160 lines (111 loc) · 3.28 KB
/
demo.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
XHRShaper.useGlobal();
var DEFAULT_URL = "http://www.streambox.fr/playlists/test_001/stream_110k_48k_416x234_000.ts";
var stats;
var requestActive = false;
function resetStats() {
stats = {
timestamps: [],
bytes: [],
t0: null,
totalDuration: null,
throughput: null
};
}
function pushStats(bytes, total) {
document.querySelector('#progress').value = bytes;
document.querySelector('#progress').max = total;
if (stats.t0 === null) {
stats.t0 = Date.now();
}
stats.timestamps.push(Date.now() - stats.t0);
stats.bytes.push(bytes);
console.log('stats pushed:', stats);
}
function onRequestDone(xhr) {
requestActive = false;
var el = document.querySelector('#graph');
Plotly.plot(
el,
[{
x: stats.timestamps,
y: stats.bytes
}],
{
margin: { t: 0 }
}
);
document.querySelector('#throughput').innerHTML = stats.throughput;
document.querySelector('#duration').innerHTML = stats.totalDuration;
document.querySelector('#status').innerHTML = 'Ready';
}
function onClickLoad() {
if (requestActive) {
console.warn('Request currently happening, please wait until done');
return;
}
var minLatency = parseInt(document.querySelector('#latency').value);
var maxBandwidth = parseInt(document.querySelector('#bandwidth').value);
if (isNaN(minLatency) || minLatency < 0) {
minLatency = 0;
document.querySelector('#latency').value = minLatency;
}
if (isNaN(maxBandwidth) || maxBandwidth <= 0) {
maxBandwidth = Infinity;
document.querySelector('#bandwidth').value = maxBandwidth;
}
document.querySelector('#status').innerHTML = 'Loading';
makeRequest(DEFAULT_URL, onRequestDone, minLatency, maxBandwidth);
}
function makeRequest(url, onRequestDone, minLatency, maxBandwidth) {
var loaded, total;
console.log('url:', url);
requestActive = true;
var xhr = new XMLHttpRequest();
xhr.caching = false;
xhr.shaper.minLatency = minLatency;
xhr.shaper.maxBandwidth = maxBandwidth;
xhr.onreadystatechange = function(e) {
console.log('readyState changed: ' + xhr.readyState);
if (xhr.readyState === 4) {
doneTime = Date.now();
var duration = doneTime - reqTime;
var bitrate = Math.round(8 * total / duration);
stats.totalDuration = duration;
stats.throughput = bitrate;
console.log('Loaded ' + total + ' bytes in ' + duration + ' ms, computed bitrate: ' + bitrate + ' kbps');
}
};
xhr.onprogress = function(e) {
loaded = e.loaded;
total = e.total;
pushStats(e.loaded, e.total);
console.log('Progress: ' + e.loaded + ' of ' + e.total);
};
xhr.onload = function(e) {
if (xhr.readyState < 4) {
console.warn('onload called with readyState:', xhr.readyState, ' id:', xhr.id);
return;
}
console.log('Loading done');
//console.log(e);
console.log('readyState:', xhr.readyState);
if (xhr.response.byteLength < 1024) {
console.log(new TextDecoder("utf-8").decode(xhr.response));
}
}
xhr.onloadend = function(e) {
console.log('Loading ended');
pushStats(loaded, total);
onRequestDone(xhr);
}
xhr.withCredentials = false;
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
console.debug('Max bandwidth: ' + xhr.shaper.maxBandwidth);
console.debug('Min latency: ' + xhr.shaper.minLatency);
var reqTime = Date.now(), doneTime;
xhr.send();
resetStats();
pushStats(0, 0);
return xhr;
}