1
0
Fork 0
mirror of https://gitlab.com/Shinobi-Systems/ShinobiCE.git synced 2025-03-09 15:40:15 +00:00
ShinobiCE/tools/ffmpegToWeb/Player/broadway/wsavc/index.js

159 lines
3.3 KiB
JavaScript

"use strict";
var Avc = require('../broadway/Decoder');
var YUVWebGLCanvas = require('../canvas/YUVWebGLCanvas');
var YUVCanvas = require('../canvas/YUVCanvas');
var Size = require('../utils/Size');
var Class = require('uclass');
var Events = require('uclass/events');
var debug = require('debug');
var log = debug("wsavc");
var WSAvcPlayer = new Class({
Implements : [Events],
initialize : function(canvas, canvastype) {
this.canvas = canvas;
this.canvastype = canvastype;
// AVC codec initialization
this.avc = new Avc();
if(false) this.avc.configure({
filter: "original",
filterHorLuma: "optimized",
filterVerLumaEdge: "optimized",
getBoundaryStrengthsA: "optimized"
});
//WebSocket variable
this.ws;
this.pktnum = 0;
},
decode : function(data) {
var naltype = "invalid frame";
if (data.length > 4) {
if (data[4] == 0x65) {
naltype = "I frame";
}
else if (data[4] == 0x41) {
naltype = "P frame";
}
else if (data[4] == 0x67) {
naltype = "SPS";
}
else if (data[4] == 0x68) {
naltype = "PPS";
}
}
//log("Passed " + naltype + " to decoder");
this.avc.decode(data);
},
connect : function(url) {
// Websocket initialization
if (this.ws != undefined) {
this.ws.close();
delete this.ws;
}
this.ws = new WebSocket(url);
this.ws.binaryType = "arraybuffer";
this.ws.onopen = () => {
log("Connected to " + url);
};
var framesList = [];
this.ws.onmessage = (evt) => {
if(typeof evt.data == "string")
return this.cmd(JSON.parse(evt.data));
this.pktnum++;
var frame = new Uint8Array(evt.data);
//log("[Pkt " + this.pktnum + " (" + evt.data.byteLength + " bytes)]");
//this.decode(frame);
framesList.push(frame);
};
var running = true;
var shiftFrame = function() {
if(!running)
return;
if(framesList.length > 10) {
log("Dropping frames", framesList.length);
framesList = [];
}
var frame = framesList.shift();
if(frame)
this.decode(frame);
requestAnimationFrame(shiftFrame);
}.bind(this);
shiftFrame();
this.ws.onclose = () => {
running = false;
log("WSAvcPlayer: Connection closed")
};
},
initCanvas : function(width, height) {
var canvasFactory = this.canvastype == "webgl" || this.canvastype == "YUVWebGLCanvas"
? YUVWebGLCanvas
: YUVCanvas;
var canvas = new canvasFactory(this.canvas, new Size(width, height));
this.avc.onPictureDecoded = canvas.decode;
this.emit("canvasReady", width, height);
},
cmd : function(cmd){
log("Incoming request", cmd);
if(cmd.action == "init") {
this.initCanvas(cmd.width, cmd.height);
this.canvas.width = cmd.width;
this.canvas.height = cmd.height;
}
},
disconnect : function() {
this.ws.close();
},
playStream : function() {
var message = "REQUESTSTREAM ";
this.ws.send(message);
log("Sent " + message);
},
stopStream : function() {
this.ws.send("STOPSTREAM");
log("Sent STOPSTREAM");
},
});
module.exports = WSAvcPlayer;
module.exports.debug = debug;