-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathws.html
192 lines (159 loc) · 7.41 KB
/
ws.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<div id="media_source"></div>
<div id="ws"></div>
<div id="init_frag"></div>
<div id="first_frame"></div>
<div id="total"></div>
<br>
<div id="current"></div>
<div id="buffered"></div>
<div id="rate"></div>
<div id="frag_wait"></div>
<br>
<button id="live">LIVE</button>
<br>
<video width="1280" height="720" controls></video>
<script>
var video = document.querySelector('video');
var live = document.getElementById('live');
live.disabled = true;
live.onclick = function () {
if (video.currentTime != 0) {
video.currentTime = sourceBuffer.buffered.end(0);
}
};
var needData = true;
var queue = [];
var sourceBuffer = null;
var mimeCodec = 'video/mp4; codecs="avc1.42e028"';
var mediaSource = null;
var mediaSourceCreateStartTime = 0, mediaSourceCreateEndTime = 0;
var wsConnectStartTime = 0, wsConnectEndTime = 0;
var initFragmentStartTime = 0, initFragmentEndTime = 0;
var firstFrameShowStartTime = 0, firstFrameShowEndTime = 0;
var maxFragWaitTime = 0, totalFragWaitTime = 0, totalFragWaitTimeCount = 0;
var prevFragTimestamp = 0;
var totalStartTime = (new Date()).getTime();
var stopTimer = function () {
firstFrameShowEndTime = (new Date()).getTime();
document.getElementById('first_frame').innerHTML = 'First frame decode/render Time: ' + (firstFrameShowEndTime - firstFrameShowStartTime) + ' ms';
var sum = (mediaSourceCreateEndTime - mediaSourceCreateStartTime) +
(wsConnectEndTime - wsConnectStartTime) +
(initFragmentEndTime - initFragmentStartTime) +
(firstFrameShowEndTime - firstFrameShowStartTime);
document.getElementById('total').innerHTML = 'Total: ' + ((new Date()).getTime() - totalStartTime) + ' ms (' + sum + ')';
video.removeEventListener('playing', stopTimer);
};
var wsURL = 'wss://rocket.umbocv.com:8080/client';
createMediaSource();
function createMediaSource() {
if ('MediaSource' in window && MediaSource.isTypeSupported(mimeCodec)) {
mediaSource = new MediaSource;
mediaSourceCreateStartTime = (new Date()).getTime();
video.src = URL.createObjectURL(mediaSource);
video.addEventListener('playing', stopTimer);
mediaSource.addEventListener('sourceopen', addSourceBuffer);
} else {
console.error('Unsupported MIME type or codec: ', mimeCodec);
}
console.log("Done with createMediaSource");
}
function addSourceBuffer (_) {
mediaSourceCreateEndTime = (new Date()).getTime();
document.getElementById('media_source').innerHTML = 'MediaSource Create Time: ' + (mediaSourceCreateEndTime - mediaSourceCreateStartTime) + ' ms';
sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
//sourceBuffer.mode = 'sequence';
sourceBuffer.addEventListener('update', function() {
if (queue.length > 0) {
console.log("update");
sourceBuffer.appendBuffer(queue.shift());
} else {
needData = true;
}
});
video.addEventListener('timeupdate', function () {
document.getElementById('current').innerHTML = '' + 'Current time: ' + video.currentTime + 'sec';
});
video.addEventListener('canplay', function () {
console.log('Got can play event');
video.play();
live.disabled = false;
});
console.log("Done with addSourceBuffer");
var ws = new WebSocket(wsURL);
ws.binaryType = "arraybuffer";
wsConnectStartTime = (new Date()).getTime();
ws.onopen = function() {
wsConnectEndTime = (new Date()).getTime();
initFragmentStartTime = (new Date()).getTime();
document.getElementById('ws').innerHTML = 'WS Connect Time: ' + (wsConnectEndTime - wsConnectStartTime) + ' ms';
};
ws.onmessage = function (evt) {
if (initFragmentEndTime === 0) {
initFragmentEndTime = (new Date()).getTime();
firstFrameShowStartTime = (new Date()).getTime();
document.getElementById('init_frag').innerHTML = 'Wait Init Fragment Time: ' + (initFragmentEndTime - initFragmentStartTime) + ' ms';
}
if (prevFragTimestamp != 0) {
var instantFragWait = (new Date()).getTime() - prevFragTimestamp;
if (instantFragWait > maxFragWaitTime) maxFragWaitTime = instantFragWait;
totalFragWaitTime += instantFragWait;
var avg = Math.round(totalFragWaitTime / totalFragWaitTimeCount);
document.getElementById('frag_wait').innerHTML =
'Instantaneous Fragment Wait: ' + instantFragWait + ' ms (max: ' + maxFragWaitTime + '), ' +
'Avg Fragment Wait: ' + avg + 'ms';
}
if (evt.data instanceof ArrayBuffer) {
console.log("evt: ", evt.data.byteLength);
if (needData) {
sourceBuffer.appendBuffer(new Uint8Array(evt.data));
needData = false;
} else {
if (queue.length > 0) {
console.log("Got other chunks in queue");
}
queue.push(new Uint8Array(evt.data));
}
}
var bufferedRange = sourceBuffer.buffered;
document.getElementById('buffered').innerHTML = '';
for (var i = 0; i < bufferedRange.length; i++) {
document.getElementById('buffered').innerHTML += '' + 'Buffer(' + i +'): ' + bufferedRange.start(i) + ' - ' + bufferedRange.end(i) + ', buffer lead time: ' + (bufferedRange.end(i) - video.currentTime).toFixed(2) + 'sec';
}
if (video.currentTime != 0 && !video.paused) {
var buffered_time = (bufferedRange.end(0) - video.currentTime);
if (buffered_time < 1) {
console.log("Not enough buffered time. Make it pause 500 ms then auto replay");
setTimeout(function () {
video.play();
}, 500);
video.pause();
} else if (buffered_time > 2) {
//console.log("Too much buffered time which cause latency. Just jump to live");
//video.currentTime = sourceBuffer.buffered.end(0) - 1;
} else if (buffered_time > 1.5) {
//console.log("Too much buffered time which cause latency. Speed up the playback rate");
//video.playbackRate = 1.2
} else if (video.playbackRate !== 1) {
//video.playbackRate = 1
}
document.getElementById('rate').innerHTML = 'Playback Rate: ' + video.playbackRate;
}
prevFragTimestamp = (new Date()).getTime();
totalFragWaitTimeCount += 1;
};
ws.onclose = function(evt) {
console.log("WebSocket is closed: ", evt.reason, evt.code);
};
ws.onerror = function (evt) {
console.log("WebSocket error: ", evt.data);
}
}
</script>
</body>
</html>