2019-12-12 06:26:50 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
player.js
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This file is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
let frameRate = 30;
|
|
|
|
|
|
|
|
onmessage = e => {
|
2020-01-02 04:05:43 +03:00
|
|
|
switch (e.data.msg) {
|
2019-12-12 06:26:50 +03:00
|
|
|
case "setFrameRate":
|
|
|
|
frameRate = e.data.data;
|
|
|
|
break;
|
|
|
|
case "loadMjpeg":
|
|
|
|
self.importScripts('./lex-mjpeg.js');
|
|
|
|
let mjpeg = new Uint8Array(e.data.data);
|
|
|
|
let lex = new MJPEGLexer(mjpeg);
|
|
|
|
player = new Player(lex);
|
|
|
|
player.setFrameRate(frameRate);
|
|
|
|
player.start();
|
|
|
|
break;
|
|
|
|
case "loadMtsMjpeg":
|
|
|
|
self.importScripts('./hlsjs/mts-demuxer.js');
|
|
|
|
let mtsMjpeg = new Uint8Array(e.data.data);
|
|
|
|
let demux = new MTSDemuxer();
|
|
|
|
let tracks = demux._getTracks();
|
|
|
|
demux.append(mtsMjpeg);
|
|
|
|
buf = new FrameBuffer(tracks.video.data);
|
|
|
|
player = new Player(buf);
|
|
|
|
player.setFrameRate(frameRate); //TODO: read frame rate from metadata.
|
|
|
|
player.start();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error("unknown message from main thread");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-02 04:05:43 +03:00
|
|
|
class Player {
|
|
|
|
constructor(buffer) {
|
2019-12-12 06:26:50 +03:00
|
|
|
this.buffer = buffer;
|
|
|
|
this.frameRate = frameRate;
|
|
|
|
}
|
|
|
|
|
2020-01-02 04:05:43 +03:00
|
|
|
setFrameRate(rate) {
|
2019-12-12 06:26:50 +03:00
|
|
|
this.frameRate = rate;
|
|
|
|
}
|
|
|
|
|
2020-01-02 04:05:43 +03:00
|
|
|
start() {
|
2019-12-12 06:26:50 +03:00
|
|
|
let frame = this.buffer.read();
|
2020-01-02 04:05:43 +03:00
|
|
|
if (frame != null) {
|
|
|
|
postMessage({ msg: "frame", data: frame.buffer }, [frame.buffer]);
|
|
|
|
setTimeout(() => { this.start(); }, 1000 / this.frameRate);
|
2019-12-12 06:26:50 +03:00
|
|
|
} else {
|
2020-01-02 04:05:43 +03:00
|
|
|
postMessage({ msg: "stop" });
|
2019-12-12 06:26:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FrameBuffer allows an array of subarrays (MJPEG frames) to be read one at a time.
|
2020-01-02 04:05:43 +03:00
|
|
|
class FrameBuffer {
|
|
|
|
constructor(src) {
|
2019-12-12 06:26:50 +03:00
|
|
|
this.src = src;
|
|
|
|
this.off = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read returns the next single frame.
|
2020-01-02 04:05:43 +03:00
|
|
|
read() {
|
2019-12-12 06:26:50 +03:00
|
|
|
return this.src[this.off++];
|
|
|
|
}
|
|
|
|
}
|