forked from Dregu/visio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.js
335 lines (257 loc) · 8.99 KB
/
Player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
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>);
*/
// universal module definition
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(["./Decoder", "./YUVCanvas"], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require("./Decoder"), require("./YUVCanvas"));
} else {
// Browser globals (root is window)
root.Player = factory(root.Decoder, root.YUVCanvas);
}
}(this, function (Decoder, WebGLCanvas) {
"use strict";
var nowValue = Decoder.nowValue;
var Player = function(parOptions){
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) {
// the browser doesn't even know what WebGL is
webgl = false;
} else {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext("webgl");
if (!ctx) {
// browser supports WebGL but initialization failed.
webgl = false;
};
};
}catch(e){
webgl = false;
};
};
this.webgl = webgl;
// choose functions
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
});
};
};
// provide size
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){
// no copy
// instead we are transfering the ownership of the buffer
// dangerous!!!
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){
// Copy the sample so that we only do a structured clone of the
// region of interest
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;
}));