123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- /*
- usage:
- p = new Player({
- useWorker: <bool>,
- workerFile: <defaults to "Decoder.js"> // give path to Decoder.js
- webgl: true | false | "auto" // defaults to "auto"
- });
- // canvas property represents the canvas node
- // put it somewhere in the dom
- p.canvas;
- p.webgl; // contains the used rendering mode. if you pass auto to webgl you can see what auto detection resulted in
- p.decode(<binary>);
- */
- // 通用模块定义
- (function (root, factory) {
- if (typeof define === 'function' && define.amd) {
-
- define(["./Decoder", "./YUVCanvas"], factory);
- } else if (typeof exports === 'object') {
-
- module.exports = factory(require("./Decoder"), require("./YUVCanvas"));
- } else {
- // 浏览器全局(根是窗口)
- root.Player = factory(root.Decoder, root.YUVCanvas);
- }
- }(this, function (Decoder, WebGLCanvas) {
- "use strict";
-
-
- var nowValue = Decoder.nowValue;
-
-
- var Player = function(parOptions){
- console.log(1111111)
- var self = this;
- this._config = parOptions || {};
-
- this.render = true;
- if (this._config.render === false){
- this.render = false;
- };
-
- this.nowValue = nowValue;
-
- this._config.workerFile = this._config.workerFile || "Decoder.js";
- if (this._config.preserveDrawingBuffer){
- this._config.contextOptions = this._config.contextOptions || {};
- this._config.contextOptions.preserveDrawingBuffer = true;
- };
-
- var webgl = "auto";
- if (this._config.webgl === true){
- webgl = true;
- }else if (this._config.webgl === false){
- webgl = false;
- };
-
- if (webgl == "auto"){
- webgl = true;
- try{
- if (!window.WebGLRenderingContext) {
- // 浏览器甚至不知道WebGLjs
- webgl = false;
- } else {
- var canvas = document.createElement('canvas');
- var ctx = canvas.getContext("webgl");
- if (!ctx) {
- // 浏览器支持WebGL,但初始化失败。
- webgl = false;
- };
- };
- }catch(e){
- webgl = false;
- };
- };
-
- this.webgl = webgl;
-
- // 选择函数
- if (this.webgl){
- this.createCanvasObj = this.createCanvasWebGL;
- this.renderFrame = this.renderFrameWebGL;
- }else{
- this.createCanvasObj = this.createCanvasRGB;
- this.renderFrame = this.renderFrameRGB;
- };
-
-
- var lastWidth;
- var lastHeight;
- var onPictureDecoded = function(buffer, width, height, infos) {
- self.onPictureDecoded(buffer, width, height, infos);
-
- var startTime = nowValue();
-
- if (!buffer || !self.render) {
- return;
- };
-
- self.renderFrame({
- canvasObj: self.canvasObj,
- data: buffer,
- width: width,
- height: height
- });
-
- if (self.onRenderFrameComplete){
- self.onRenderFrameComplete({
- data: buffer,
- width: width,
- height: height,
- infos: infos,
- canvasObj: self.canvasObj
- });
- };
-
- };
-
- // 提供大小
-
- if (!this._config.size){
- this._config.size = {};
- };
- this._config.size.width = this._config.size.width || 200;
- this._config.size.height = this._config.size.height || 200;
-
- if (this._config.useWorker){
- var worker = new Worker(this._config.workerFile);
- this.worker = worker;
- worker.addEventListener('message', function(e) {
- var data = e.data;
- if (data.consoleLog){
- console.log(data.consoleLog);
- return;
- };
-
- onPictureDecoded.call(self, new Uint8Array(data.buf, 0, data.length), data.width, data.height, data.infos);
-
- }, false);
-
- worker.postMessage({type: "Broadway.js - Worker init", options: {
- rgb: !webgl,
- memsize: this.memsize,
- reuseMemory: this._config.reuseMemory ? true : false
- }});
-
- if (this._config.transferMemory){
- this.decode = function(parData, parInfo){
-
-
- worker.postMessage({buf: parData.buffer, offset: parData.byteOffset, length: parData.length, info: parInfo}, [parData.buffer]); // Send data to our worker.
- };
-
- }else{
- this.decode = function(parData, parInfo){
-
- var copyU8 = new Uint8Array(parData.length);
- copyU8.set( parData, 0, parData.length );
- worker.postMessage({buf: copyU8.buffer, offset: 0, length: parData.length, info: parInfo}, [copyU8.buffer]); // Send data to our worker.
- };
-
- };
-
- if (this._config.reuseMemory){
- this.recycleMemory = function(parArray){
- //this.beforeRecycle();
- worker.postMessage({reuse: parArray.buffer}, [parArray.buffer]); // Send data to our worker.
- //this.afterRecycle();
- };
- }
-
- }else{
-
- this.decoder = new Decoder({
- rgb: !webgl
- });
- this.decoder.onPictureDecoded = onPictureDecoded;
- this.decode = function(parData, parInfo){
- self.decoder.decode(parData, parInfo);
- };
-
- };
-
-
-
- if (this.render){
- this.canvasObj = this.createCanvasObj({
- contextOptions: this._config.contextOptions
- });
- this.canvas = this.canvasObj.canvas;
- };
- this.domNode = this.canvas;
-
- lastWidth = this._config.size.width;
- lastHeight = this._config.size.height;
-
- };
-
- Player.prototype = {
-
- onPictureDecoded: function(buffer, width, height, infos){},
-
- // call when memory of decoded frames is not used anymore
- recycleMemory: function(buf){
- },
- /*beforeRecycle: function(){},
- afterRecycle: function(){},*/
-
- // for both functions options is:
- //
- // width
- // height
- // enableScreenshot
- //
- // returns a object that has a property canvas which is a html5 canvas
- createCanvasWebGL: function(options){
- var canvasObj = this._createBasicCanvasObj(options);
- canvasObj.contextOptions = options.contextOptions;
- return canvasObj;
- },
-
- createCanvasRGB: function(options){
- var canvasObj = this._createBasicCanvasObj(options);
- return canvasObj;
- },
-
- // part that is the same for webGL and RGB
- _createBasicCanvasObj: function(options){
- options = options || {};
-
- var obj = {};
- var width = options.width;
- if (!width){
- width = this._config.size.width;
- };
- var height = options.height;
- if (!height){
- height = this._config.size.height;
- };
- obj.canvas = document.createElement('canvas');
- obj.canvas.width = width;
- obj.canvas.height = height;
- obj.canvas.style.backgroundColor = "#0D0E1B";
-
-
- return obj;
- },
-
- // options:
- //
- // canvas
- // data
- renderFrameWebGL: function(options){
-
- var canvasObj = options.canvasObj;
-
- var width = options.width || canvasObj.canvas.width;
- var height = options.height || canvasObj.canvas.height;
-
- if (canvasObj.canvas.width !== width || canvasObj.canvas.height !== height || !canvasObj.webGLCanvas){
- canvasObj.canvas.width = width;
- canvasObj.canvas.height = height;
- canvasObj.webGLCanvas = new WebGLCanvas({
- canvas: canvasObj.canvas,
- contextOptions: canvasObj.contextOptions,
- width: width,
- height: height
- });
- };
-
- var ylen = width * height;
- var uvlen = (width / 2) * (height / 2);
-
- canvasObj.webGLCanvas.drawNextOutputPicture({
- yData: options.data.subarray(0, ylen),
- uData: options.data.subarray(ylen, ylen + uvlen),
- vData: options.data.subarray(ylen + uvlen, ylen + uvlen + uvlen)
- });
-
- var self = this;
- self.recycleMemory(options.data);
-
- },
- renderFrameRGB: function(options){
- var canvasObj = options.canvasObj;
- var width = options.width || canvasObj.canvas.width;
- var height = options.height || canvasObj.canvas.height;
-
- if (canvasObj.canvas.width !== width || canvasObj.canvas.height !== height){
- canvasObj.canvas.width = width;
- canvasObj.canvas.height = height;
- };
-
- var ctx = canvasObj.ctx;
- var imgData = canvasObj.imgData;
- if (!ctx){
- canvasObj.ctx = canvasObj.canvas.getContext('2d');
- ctx = canvasObj.ctx;
- canvasObj.imgData = ctx.createImageData(width, height);
- imgData = canvasObj.imgData;
- };
- imgData.data.set(options.data);
- ctx.putImageData(imgData, 0, 0);
- var self = this;
- self.recycleMemory(options.data);
-
- }
-
- };
-
- return Player;
-
- }));
|