2019-12-12 06:26:50 +03:00
|
|
|
/*
|
|
|
|
AUTHOR
|
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2020-01-20 13:30:24 +03:00
|
|
|
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
|
2019-12-12 06:26:50 +03:00
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License in gpl.txt.
|
2020-01-02 04:05:43 +03:00
|
|
|
If not, see http://www.gnu.org/licenses.
|
2019-12-12 06:26:50 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
// MJPEGLexer lexes a byte array containing MJPEG into individual JPEGs.
|
2020-01-02 04:05:43 +03:00
|
|
|
class MJPEGLexer {
|
2020-01-20 13:30:24 +03:00
|
|
|
constructor() {
|
2019-12-12 06:26:50 +03:00
|
|
|
this.off = 0;
|
|
|
|
}
|
|
|
|
|
2020-01-20 13:30:24 +03:00
|
|
|
append(data) {
|
|
|
|
this.src = new Uint8Array(data);
|
|
|
|
}
|
|
|
|
|
2019-12-12 06:26:50 +03:00
|
|
|
// read returns the next single frame.
|
2020-01-02 04:05:43 +03:00
|
|
|
read() {
|
2019-12-12 06:26:50 +03:00
|
|
|
// Check if the src can contain at least the start and end flags (4B).
|
2020-01-02 04:05:43 +03:00
|
|
|
if (this.off + 4 > this.src.length) {
|
2019-12-12 06:26:50 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Iterate through bytes until the start flag is found.
|
2020-01-02 04:05:43 +03:00
|
|
|
while (this.src[this.off] != 0xff || this.src[this.off + 1] != 0xd8) {
|
2019-12-12 06:26:50 +03:00
|
|
|
this.off++;
|
2020-01-02 04:05:43 +03:00
|
|
|
if (this.off + 4 > this.src.length) {
|
2019-12-12 06:26:50 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Start after the start flag and loop until the end flag is found.
|
2020-01-02 04:05:43 +03:00
|
|
|
let end = this.off + 2;
|
|
|
|
while (true) {
|
|
|
|
if (end + 2 > this.src.length) {
|
2019-12-12 06:26:50 +03:00
|
|
|
return null;
|
|
|
|
}
|
2020-01-02 04:05:43 +03:00
|
|
|
if (this.src[end] == 0xff && this.src[end + 1] == 0xd9) {
|
2019-12-12 06:26:50 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
// Copy the frame's bytes to a new array to return.
|
|
|
|
// Note: optimally this would return a reference but since we are in a worker thread,
|
|
|
|
// the main thread doesn't have access to the ArrayBuffer that we are working with.
|
2020-01-02 04:05:43 +03:00
|
|
|
let frame = this.src.slice(this.off, end + 2);
|
|
|
|
this.off = end + 2
|
2019-12-12 06:26:50 +03:00
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
}
|