-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
162 lines (140 loc) · 4.6 KB
/
Copy pathindex.html
File metadata and controls
162 lines (140 loc) · 4.6 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
'use strict';
var DEFAULT_FPS = 24,
DEFAULT_AUTOLOOP = true,
JPEG_MAGIG_NUMBER = [0xff, 0xd8, 0xff],
JPEG_END_NUMBER = [0xff, 0xd9];
var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || window.setTimeout;
var jpegs = [];
var oldReminder = new Uint8Array(0);
function splitMJPEG(mjpegUrl, callback) {
fetch(mjpegUrl)
.then(response => response.body)
.then(rb => {
const reader = rb.getReader();
return new ReadableStream({
start(controller) {
// The following function handles each data chunk
function push() {
// "done" is a Boolean and value a "Uint8Array"
reader.read().then(({done, value}) => {
// If there is no more data to read
if (done) {
console.log('done', done);
controller.close();
return;
}
///// Get the data and send it to the browser via the controller
///controller.enqueue(value);
// Check chunks by logging to the console
///console.log(done, value);
var tmp = new Uint8Array(value),
iStart;
///alert(tmp.length);
var array = new Uint8Array(oldReminder.length + tmp.length);
array.set(oldReminder);
array.set(tmp, oldReminder.length);
oldReminder = new Uint8Array(0);
find_jpeg_loop: for (var i = 0, len = array.length; i <= len - 2; ++i) {
if (array[i] === JPEG_MAGIG_NUMBER[0] && array[i + 1] === JPEG_MAGIG_NUMBER[1] && array[i + 2] === JPEG_MAGIG_NUMBER[2]) {
if (i > 0 && typeof iStart === 'number') {
///alert(array.subarray(iStart, i));
///alert(String.fromCharCode.apply(null, array.subarray(iStart, i)));
for (var iEnd = iStart + 2; iEnd <= len - 2; ++iEnd) { // search iEnd from old iStart
if (array[iEnd] === JPEG_END_NUMBER[0] && array[iEnd + 1] === JPEG_END_NUMBER[1]) {
jpegs.push(new Blob([array.subarray(iStart, iEnd)], {type: 'image/jpeg'}));
iStart = Math.max(i, iStart); // next iStart
i = Math.max(i, iEnd) + 1;
continue find_jpeg_loop;
}
}
}
iStart = i; // next iStart
}
}
///console.log(iStart, array.length);
oldReminder = array.subarray(iStart);
if (oldReminder.length > 20000000) {
oldReminder = new Uint8Array(0); ///
}
////
callback(jpegs);
push();
})
}
push();
}
});
});
}
function playMJPEGInternal(wrapperElement, mjpegUrl, fps, autoloop) {
fps = (typeof fps === 'number') ? fps : DEFAULT_FPS;
autoloop = (typeof autoloop === 'boolean') ? autoloop : DEFAULT_AUTOLOOP;
var playbackFinished = false,
imageElement = document.createElement('img'),
jpegUrl;
imageElement.setAttribute('style', 'width:100%;');
wrapperElement.appendChild(imageElement);
splitMJPEG(mjpegUrl, function(jpegFiles) {
if (jpegFiles.length > 0) {
var nextFrameIndex = 0;
///console.log(jpegFiles[nextFrameIndex]);
function showNextFrame() {
if (imageElement) {
if (jpegUrl) {
URL.revokeObjectURL(jpegUrl);
}
if (nextFrameIndex >= jpegFiles.length || jpegFiles.length == 0) {
return;
}
jpegUrl = URL.createObjectURL(jpegFiles.shift());
imageElement.onerror = function() {
setTimeout(function() {
requestAnimationFrame(showNextFrame);
}, 1000 / fps);
};
imageElement.onload = function() {
if (imageElement) {
if (autoloop || nextFrameIndex < jpegFiles.length) {
nextFrameIndex = (nextFrameIndex === jpegFiles.length) ? 0 : nextFrameIndex;
setTimeout(function() {
requestAnimationFrame(showNextFrame);
}, 1000 / fps);
}
}
};
imageElement.setAttribute('src', jpegUrl);
}
};
setTimeout(function() {
requestAnimationFrame(showNextFrame);
}, 1000 / fps);
}
});
return {
finish: function() {
if (imageElement) {
imageElement.src = '';
wrapperElement.removeChild(imageElement);
imageElement = undefined;
}
}
};
};
</script>
</head>
<body>
<div id="mjpeg_stream_player_wrapper">
</div>
<script>
var mjpegUrl = './data/bumblebee_flower_nature.mjpeg';
var fps = 55;
var autoloop = false; // not supported now
playMJPEGInternal(document.getElementById('mjpeg_stream_player_wrapper'), mjpegUrl, fps, autoloop);
</script>
</body>
</html>