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>
|
||||
<!--
|
||||
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">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Mjpeg Player</title>
|
||||
<script type="text/javascript" src="main.js"></script>
|
||||
<script type="module" src="player.js"></script>
|
||||
<script type="module" src="./main.js"></script>
|
||||
</head>
|
||||
|
||||
<body style="height: 100%">
|
||||
<div class="card m-auto" style="width: 40rem;">
|
||||
<div class="card-body">
|
||||
<div class="container-fluid">
|
||||
<div class="form-group">
|
||||
<input class="form-control-file" type="file" id="fileinput" onchange="play();">
|
||||
<div style="width: 40rem;">
|
||||
<div>
|
||||
<div>
|
||||
<div>
|
||||
<input type="file" id="fileInput">
|
||||
</div>
|
||||
</div>
|
||||
<div class="container-fluid">
|
||||
Frame Rate: <input type="text" id="rate" class="mb-3 mx-3" value="30"> fps
|
||||
<div>
|
||||
<label>Playlist URL: </label>
|
||||
<input type="url" id="url" placeholder="Enter URL">
|
||||
<button id="urlBtn" type="submit">Load</button>
|
||||
</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;">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<footer id="sticky-footer" class="footer fixed-bottom py-4 bg-dark text-white-50" style="width: 100%;">
|
||||
<div class="container text-center">
|
||||
<small>©2019 Australian Ocean Laboratory Limited (AusOcean) (<a rel="license" href="https://www.ausocean.org/license">License</a>)</small>
|
||||
<footer id="sticky-footer" style="width: 100%;">
|
||||
<div>
|
||||
<small>©2020 Australian Ocean Laboratory Limited (AusOcean) (<a rel="license" href="https://www.ausocean.org/license">License</a>)</small>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
/*
|
||||
NAME
|
||||
main.js
|
||||
|
||||
AUTHOR
|
||||
Trek Hopton <trek@ausocean.org>
|
||||
|
||||
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
|
||||
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.
|
||||
*/
|
||||
|
||||
// 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() {
|
||||
const viewer = document.getElementById('viewer');
|
||||
const input = event.target.files[0];
|
||||
const reader = new FileReader();
|
||||
|
||||
|
@ -32,29 +86,11 @@ function play() {
|
|||
const player = new Worker("player.js");
|
||||
|
||||
let rate = document.getElementById('rate');
|
||||
if (rate.value && rate.value > 0) {
|
||||
if (rate.value > 0) {
|
||||
player.postMessage({ msg: "setFrameRate", data: rate.value });
|
||||
}
|
||||
|
||||
player.onmessage = 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":
|
||||
break;
|
||||
default:
|
||||
console.error("unknown message from player");
|
||||
break;
|
||||
}
|
||||
};
|
||||
player.onmessage = handleMessage;
|
||||
|
||||
switch (input.name.split('.')[1]) {
|
||||
case "mjpeg":
|
||||
|
@ -71,5 +107,26 @@ function play() {
|
|||
};
|
||||
reader.onerror = error => reject(error);
|
||||
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
|
||||
Trek Hopton <trek@ausocean.org>
|
||||
|
||||
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
|
||||
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.
|
||||
*/
|
||||
|
||||
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 => {
|
||||
switch (e.data.msg) {
|
||||
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 = new PlayerWorker();
|
||||
player.init(codecs.MJPEG);
|
||||
player.append(e.data.data);
|
||||
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 = new PlayerWorker();
|
||||
player.init(codecs.MTS_MJPEG);
|
||||
player.append(e.data.data);
|
||||
player.start();
|
||||
break;
|
||||
case "appendMtsMjpeg":
|
||||
player.append(e.data.data);
|
||||
break;
|
||||
default:
|
||||
console.error("unknown message from main thread");
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
class Player {
|
||||
constructor(buffer) {
|
||||
this.buffer = buffer;
|
||||
// PlayerWorker has a FrameBuffer to hold frames and once started, passes them one at a time to the main thread.
|
||||
class PlayerWorker {
|
||||
init(codec) {
|
||||
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) {
|
||||
|
@ -65,7 +76,7 @@ class Player {
|
|||
}
|
||||
|
||||
start() {
|
||||
let frame = this.buffer.read();
|
||||
let frame = this.frameSrc.read();
|
||||
if (frame != null) {
|
||||
postMessage({ msg: "frame", data: frame.buffer }, [frame.buffer]);
|
||||
setTimeout(() => { this.start(); }, 1000 / this.frameRate);
|
||||
|
@ -73,17 +84,51 @@ class Player {
|
|||
postMessage({ msg: "stop" });
|
||||
}
|
||||
}
|
||||
|
||||
append(data) {
|
||||
this.frameSrc.append(data);
|
||||
}
|
||||
}
|
||||
|
||||
// FrameBuffer allows an array of subarrays (MJPEG frames) to be read one at a time.
|
||||
class FrameBuffer {
|
||||
constructor(src) {
|
||||
this.src = src;
|
||||
this.off = 0;
|
||||
constructor() {
|
||||
this.segments = [];
|
||||
this.off = { segment: 0, frame: 0 };
|
||||
this.demuxer = new MTSDemuxer();
|
||||
}
|
||||
|
||||
// read returns the next single frame.
|
||||
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