This repository was archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaskVideo.js
137 lines (107 loc) · 4.11 KB
/
PlaskVideo.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
var fs = require('fs');
var plask = require('plask');
var vert = fs.readFileSync(__dirname + '/PlaskVideo.vert', 'utf8');
var frag = fs.readFileSync(__dirname + '/PlaskVideo.frag', 'utf8');
function PlaskVideo(ctx, file) {
this.ctx = ctx;
this.planeVAO = this.createPlaneVAO();
this.player = new plask.AVPlayer(ctx.getGL());
if (file) {
this.player.appendFile(file);
}
this.videoTexture = ctx.createTexture2D(null, 1, 1);
this.fbo = ctx.createFramebuffer([ { texture: this.videoTexture } ]);
this.program = ctx.createProgram(vert, frag);
this.autoplay = true;
this.autoplayStarted = false;
var loops = false;
var src = '';
Object.defineProperty(this, 'src', {
get: function() { return src; },
set: function(file) {
src = file;
this.player.removeAll();
this.player.appendFile(file);
}
});
Object.defineProperty(this, 'duration', {
get: function() { return this.player.currentDuration(); }
});
Object.defineProperty(this, 'currentTime', {
get: function() { return this.player.currentTime(); },
set: function(time) { this.player.seekToTime(time); }
});
Object.defineProperty(this, 'loop', {
get: function() { return loops; },
set: function(state) { loops = state; this.player.setLoops(state); }
});
Object.defineProperty(this, 'paused', {
get: function() { return this.player.rate() == 0; },
});
Object.defineProperty(this, 'volume', {
get: function() { return this.player.volume(); },
set: function(time) { this.player.setVolume(time); }
});
}
PlaskVideo.prototype.play = function() {
this.player.setRate(1);
}
PlaskVideo.prototype.pause = function() {
this.player.setRate(0);
}
PlaskVideo.prototype.getTexture = function() {
return this.videoTexture;
}
PlaskVideo.prototype.update = function() {
var tex = this.player.currentFrameTexture();
if (this.player.rate() === 0 && this.player.currentDuration() > 0 && this.autoplay && !this.autoplayStarted) {
this.autoplayStarted = true;
this.player.setRate(1);
}
var ctx = this.ctx;
var gl = ctx.getGL();
if (tex && (tex.s1 != this.videoTexture.getWidth() || tex.t1 != this.videoTexture.getHeight())) {
//resize video texture to match the video size
this.videoTexture.update(null, tex.s1, tex.t1)
}
gl.enable(gl.TEXTURE_RECTANGLE);
ctx.pushState(ctx.FRAMEBUFFER_BIT | ctx.PROGRAM_BUT | ctx.TEXTURE_BIT | ctx.VERTEX_ARRAY_BIT | ctx.VIEWPORT_BIT);
ctx.bindFramebuffer(this.fbo);
ctx.setViewport(0, 0, this.videoTexture.getWidth(), this.videoTexture.getHeight())
ctx.setClearColor(1,0,0,1);
ctx.clear(ctx.COLOR_BIT);
ctx.bindProgram(this.program);
if (tex) {
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_RECTANGLE, tex.texture);
gl.texParameteri(gl.TEXTURE_RECTANGLE, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_RECTANGLE, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_RECTANGLE, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_RECTANGLE, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
this.program.setUniform('uTextureSize', [tex.s1, tex.t1])
gl.disable(gl.TEXTURE_RECTANGLE);
}
this.program.setUniform('uTexture', 0);
ctx.bindVertexArray(this.planeVAO);
ctx.drawArrays(ctx.TRIANGLES, 0, 6);
ctx.popState();
}
PlaskVideo.prototype.createPlaneVAO = function() {
var ctx = this.ctx;
var pos = new Float32Array([
-1, -1, 0, 1, -1, 0, 1, 1, 0,
-1, -1, 0, 1, 1, 0,-1, 1, 0
]);
var uv = new Float32Array([
0, 1, 1, 1, 1, 0,
0, 1, 1, 0, 0, 0
]);
var posBuf = ctx.createBuffer(ctx.ARRAY_BUFFER, pos);
var uvBuf = ctx.createBuffer(ctx.ARRAY_BUFFER, uv);
var attributesDesc = [
{ location: ctx.ATTRIB_POSITION, buffer: posBuf, size: 3 },
{ location: ctx.ATTRIB_TEX_COORD_0, buffer: uvBuf, size: 2 },
];
return ctx.createVertexArray(attributesDesc, null);
}
module.exports = PlaskVideo;