mirror of https://bitbucket.org/ausocean/av.git
Merged in m3u-update (pull request #342)
mjpeg-player: updated player for m3u changes Approved-by: Saxon Milton Approved-by: Scott Barnard
This commit is contained in:
commit
4bbfa9ee6a
|
@ -1,32 +1,57 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
|
<!--
|
||||||
|
AUTHOR
|
||||||
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
LICENSE
|
||||||
|
This file is Copyright (C) 2020 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.
|
||||||
|
If not, see http://www.gnu.org/licenses.
|
||||||
|
-->
|
||||||
|
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>Mjpeg Player</title>
|
<title>Mjpeg Player</title>
|
||||||
<script type="text/javascript" src="main.js"></script>
|
<script type="module" src="./main.js"></script>
|
||||||
<script type="module" src="player.js"></script>
|
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body style="height: 100%">
|
<body style="height: 100%">
|
||||||
<div class="card m-auto" style="width: 40rem;">
|
<div style="width: 40rem;">
|
||||||
<div class="card-body">
|
<div>
|
||||||
<div class="container-fluid">
|
<div>
|
||||||
<div class="form-group">
|
<div>
|
||||||
<input class="form-control-file" type="file" id="fileinput" onchange="play();">
|
<input type="file" id="fileInput">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="container-fluid">
|
<div>
|
||||||
Frame Rate: <input type="text" id="rate" class="mb-3 mx-3" value="30"> fps
|
<label>Playlist URL: </label>
|
||||||
|
<input type="url" id="url" placeholder="Enter URL">
|
||||||
|
<button id="urlBtn" type="submit">Load</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="container-fluid">
|
<div>
|
||||||
|
Frame Rate: <input type="text" id="rate" value="25"> fps
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
<img src="" id="viewer" style="max-width:100%; height:auto;">
|
<img src="" id="viewer" style="max-width:100%; height:auto;">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<footer id="sticky-footer" class="footer fixed-bottom py-4 bg-dark text-white-50" style="width: 100%;">
|
<footer id="sticky-footer" style="width: 100%;">
|
||||||
<div class="container text-center">
|
<div>
|
||||||
<small>©2019 Australian Ocean Laboratory Limited (AusOcean) (<a rel="license" href="https://www.ausocean.org/license">License</a>)</small>
|
<small>©2020 Australian Ocean Laboratory Limited (AusOcean) (<a rel="license" href="https://www.ausocean.org/license">License</a>)</small>
|
||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
/*
|
/*
|
||||||
NAME
|
|
||||||
main.js
|
|
||||||
|
|
||||||
AUTHOR
|
AUTHOR
|
||||||
Trek Hopton <trek@ausocean.org>
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
LICENSE
|
LICENSE
|
||||||
This file is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
|
||||||
|
|
||||||
It is free software: you can redistribute it and/or modify them
|
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
|
under the terms of the GNU General Public License as published by the
|
||||||
|
@ -22,9 +19,66 @@ LICENSE
|
||||||
If not, see http://www.gnu.org/licenses.
|
If not, see http://www.gnu.org/licenses.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// play will process and play the chosen target file.
|
import Hls from "./hlsjs/hls.js";
|
||||||
|
|
||||||
|
let started = false;
|
||||||
|
let player, viewer;
|
||||||
|
|
||||||
|
// init gets DOM elements once the document has been loaded and adds listeners where necessary.
|
||||||
|
function init() {
|
||||||
|
document.addEventListener('DOMContentLoaded', load);
|
||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
|
document.getElementById('urlBtn').addEventListener('click', load);
|
||||||
|
document.getElementById('fileInput').addEventListener('change', play);
|
||||||
|
viewer = document.getElementById('viewer');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
init();
|
||||||
|
|
||||||
|
// load gets the URL from the URL input element or the browser's URL bar
|
||||||
|
// and creates an Hls instance to load the content from the URL.
|
||||||
|
function load() {
|
||||||
|
let url = document.getElementById('url').value;
|
||||||
|
if (url == "") {
|
||||||
|
// Get everything following the ? from the browser's URL bar.
|
||||||
|
url = window.location.search.slice(1);
|
||||||
|
document.getElementById('url').value = url;
|
||||||
|
} else if (url[0] == '/') {
|
||||||
|
url = window.location.protocol + '//' + window.location.host + url;
|
||||||
|
}
|
||||||
|
if (url == "") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let hls = new Hls();
|
||||||
|
hls.loadSource(url, append);
|
||||||
|
}
|
||||||
|
|
||||||
|
// append, on the first call, starts a player worker and passes it a frame rate and the video data,
|
||||||
|
// on subsequent calls it passes the video data to the player worker.
|
||||||
|
function append(data) {
|
||||||
|
if (!started) {
|
||||||
|
player = new Worker("player.js");
|
||||||
|
|
||||||
|
let rate = document.getElementById('rate');
|
||||||
|
if (rate.value && rate.value > 0) {
|
||||||
|
player.postMessage({ msg: "setFrameRate", data: rate.value });
|
||||||
|
}
|
||||||
|
|
||||||
|
player.onmessage = handleMessage;
|
||||||
|
|
||||||
|
player.postMessage({ msg: "loadMtsMjpeg", data: data }, [data]);
|
||||||
|
started = true;
|
||||||
|
} else {
|
||||||
|
player.postMessage({ msg: "appendMtsMjpeg", data: data }, [data]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// play will process and play the target file chosen with the file input element.
|
||||||
function play() {
|
function play() {
|
||||||
const viewer = document.getElementById('viewer');
|
|
||||||
const input = event.target.files[0];
|
const input = event.target.files[0];
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
|
|
||||||
|
@ -32,29 +86,11 @@ function play() {
|
||||||
const player = new Worker("player.js");
|
const player = new Worker("player.js");
|
||||||
|
|
||||||
let rate = document.getElementById('rate');
|
let rate = document.getElementById('rate');
|
||||||
if (rate.value && rate.value > 0) {
|
if (rate.value > 0) {
|
||||||
player.postMessage({ msg: "setFrameRate", data: rate.value });
|
player.postMessage({ msg: "setFrameRate", data: rate.value });
|
||||||
}
|
}
|
||||||
|
|
||||||
player.onmessage = e => {
|
player.onmessage = handleMessage;
|
||||||
switch (e.data.msg) {
|
|
||||||
case "frame":
|
|
||||||
const blob = new Blob([new Uint8Array(e.data.data)], {
|
|
||||||
type: 'video/x-motion-jpeg'
|
|
||||||
});
|
|
||||||
const url = URL.createObjectURL(blob);
|
|
||||||
viewer.src = url;
|
|
||||||
break;
|
|
||||||
case "log":
|
|
||||||
console.log(e.data.data);
|
|
||||||
break;
|
|
||||||
case "stop":
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
console.error("unknown message from player");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
switch (input.name.split('.')[1]) {
|
switch (input.name.split('.')[1]) {
|
||||||
case "mjpeg":
|
case "mjpeg":
|
||||||
|
@ -71,5 +107,26 @@ function play() {
|
||||||
};
|
};
|
||||||
reader.onerror = error => reject(error);
|
reader.onerror = error => reject(error);
|
||||||
reader.readAsArrayBuffer(input);
|
reader.readAsArrayBuffer(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleMessage handles messgaes from the player workers, its main job is to update the display when a frame is received.
|
||||||
|
function handleMessage(e) {
|
||||||
|
switch (e.data.msg) {
|
||||||
|
case "frame":
|
||||||
|
const blob = new Blob([new Uint8Array(e.data.data)], {
|
||||||
|
type: 'video/x-motion-jpeg'
|
||||||
|
});
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
viewer.src = url;
|
||||||
|
break;
|
||||||
|
case "log":
|
||||||
|
console.log(e.data.data);
|
||||||
|
break;
|
||||||
|
case "stop":
|
||||||
|
console.log("stopped");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.error("unknown message from player");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,12 +1,9 @@
|
||||||
/*
|
/*
|
||||||
NAME
|
|
||||||
player.js
|
|
||||||
|
|
||||||
AUTHOR
|
AUTHOR
|
||||||
Trek Hopton <trek@ausocean.org>
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
LICENSE
|
LICENSE
|
||||||
This file is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
|
||||||
|
|
||||||
It is free software: you can redistribute it and/or modify them
|
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
|
under the terms of the GNU General Public License as published by the
|
||||||
|
@ -22,42 +19,56 @@ LICENSE
|
||||||
If not, see http://www.gnu.org/licenses.
|
If not, see http://www.gnu.org/licenses.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let frameRate = 30;
|
let frameRate = 25; //Keeps track of the frame rate, default is 25fps.
|
||||||
|
self.importScripts('./lex-mjpeg.js');
|
||||||
|
self.importScripts('./hlsjs/mts-demuxer.js');
|
||||||
|
|
||||||
|
const codecs = {
|
||||||
|
MJPEG: 1,
|
||||||
|
MTS_MJPEG: 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
// onmessage is called whenever the main thread sends this worker a message.
|
||||||
onmessage = e => {
|
onmessage = e => {
|
||||||
switch (e.data.msg) {
|
switch (e.data.msg) {
|
||||||
case "setFrameRate":
|
case "setFrameRate":
|
||||||
frameRate = e.data.data;
|
frameRate = e.data.data;
|
||||||
break;
|
break;
|
||||||
case "loadMjpeg":
|
case "loadMjpeg":
|
||||||
self.importScripts('./lex-mjpeg.js');
|
player = new PlayerWorker();
|
||||||
let mjpeg = new Uint8Array(e.data.data);
|
player.init(codecs.MJPEG);
|
||||||
let lex = new MJPEGLexer(mjpeg);
|
player.append(e.data.data);
|
||||||
player = new Player(lex);
|
|
||||||
player.setFrameRate(frameRate);
|
player.setFrameRate(frameRate);
|
||||||
player.start();
|
player.start();
|
||||||
break;
|
break;
|
||||||
case "loadMtsMjpeg":
|
case "loadMtsMjpeg":
|
||||||
self.importScripts('./hlsjs/mts-demuxer.js');
|
player = new PlayerWorker();
|
||||||
let mtsMjpeg = new Uint8Array(e.data.data);
|
player.init(codecs.MTS_MJPEG);
|
||||||
let demux = new MTSDemuxer();
|
player.append(e.data.data);
|
||||||
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();
|
player.start();
|
||||||
break;
|
break;
|
||||||
|
case "appendMtsMjpeg":
|
||||||
|
player.append(e.data.data);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
console.error("unknown message from main thread");
|
console.error("unknown message from main thread");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Player {
|
// PlayerWorker has a FrameBuffer to hold frames and once started, passes them one at a time to the main thread.
|
||||||
constructor(buffer) {
|
class PlayerWorker {
|
||||||
this.buffer = buffer;
|
init(codec) {
|
||||||
this.frameRate = frameRate;
|
this.frameRate = frameRate;
|
||||||
|
this.codec = codec;
|
||||||
|
switch (codec) {
|
||||||
|
case codecs.MJPEG:
|
||||||
|
this.frameSrc = new MJPEGLexer();
|
||||||
|
break;
|
||||||
|
case codecs.MTS_MJPEG:
|
||||||
|
this.frameSrc = new FrameBuffer();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setFrameRate(rate) {
|
setFrameRate(rate) {
|
||||||
|
@ -65,7 +76,7 @@ class Player {
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
let frame = this.buffer.read();
|
let frame = this.frameSrc.read();
|
||||||
if (frame != null) {
|
if (frame != null) {
|
||||||
postMessage({ msg: "frame", data: frame.buffer }, [frame.buffer]);
|
postMessage({ msg: "frame", data: frame.buffer }, [frame.buffer]);
|
||||||
setTimeout(() => { this.start(); }, 1000 / this.frameRate);
|
setTimeout(() => { this.start(); }, 1000 / this.frameRate);
|
||||||
|
@ -73,17 +84,51 @@ class Player {
|
||||||
postMessage({ msg: "stop" });
|
postMessage({ msg: "stop" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
append(data) {
|
||||||
|
this.frameSrc.append(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FrameBuffer allows an array of subarrays (MJPEG frames) to be read one at a time.
|
// FrameBuffer allows an array of subarrays (MJPEG frames) to be read one at a time.
|
||||||
class FrameBuffer {
|
class FrameBuffer {
|
||||||
constructor(src) {
|
constructor() {
|
||||||
this.src = src;
|
this.segments = [];
|
||||||
this.off = 0;
|
this.off = { segment: 0, frame: 0 };
|
||||||
|
this.demuxer = new MTSDemuxer();
|
||||||
}
|
}
|
||||||
|
|
||||||
// read returns the next single frame.
|
// read returns the next single frame.
|
||||||
read() {
|
read() {
|
||||||
return this.src[this.off++];
|
let off = this.off;
|
||||||
|
let prevOff = off;
|
||||||
|
if (this.incrementOff()) {
|
||||||
|
return this.segments[prevOff.segment][prevOff.frame];
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
append(data) {
|
||||||
|
let demuxed = this.demuxer.demux(new Uint8Array(data));
|
||||||
|
this.segments.push(demuxed.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
incrementOff() {
|
||||||
|
if (!this.segments || !this.segments[this.off.segment]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (this.off.frame + 1 >= this.segments[this.off.segment].length) {
|
||||||
|
if (this.off.segment + 1 >= this.segments.length) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
this.off.segment++;
|
||||||
|
this.off.frame = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.off.frame++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue