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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
content / test / data / gpu / vc / webgl_video.js [blame]
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function webglInit(canvasWidth, canvasHeight, mode = 'webgl') {
const container = document.getElementById('container');
const canvas = container.appendChild(document.createElement('canvas'));
canvas.width = canvasWidth;
canvas.height = canvasHeight;
const gl = canvas.getContext(mode);
if (!gl) {
console.warn('WebGL not supported. canvas.getContext("webgl") fails!');
return null;
}
return gl;
}
const webglShaders = {
vertex: `
attribute vec2 aVertPos;
attribute vec2 aTexCoord;
varying mediump vec2 vTexCoord;
void main(void) {
gl_Position = vec4(aVertPos, 0.0, 1.0);
vTexCoord = aTexCoord;
}
`,
fragment: `
precision mediump float;
varying mediump vec2 vTexCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vTexCoord);
}
`,
vertexIcons: `
attribute vec2 aVertPos;
void main(void) {
gl_Position = vec4(aVertPos, 0.0, 1.0);
}
`,
fragmentOutputBlue: `
void main(void) {
gl_FragColor = vec4(0.11328125, 0.4296875, 0.84375, 1.0);
}
`,
fragmentOutputLightBlue: `
void main(void) {
gl_FragColor = vec4(0.3515625, 0.50390625, 0.75390625, 1.0);
}
`,
fragmentOutputWhite: `
void main(void) {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
`,
};
function setupShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.log('An error occurred compiling the shaders: '
+ gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
function setupProgram(gl, vertexSource, fragmentSource) {
const vertexShader = setupShader(gl, gl.VERTEX_SHADER, vertexSource);
const fragmentShader = setupShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.log('Unable to initialize the shader program: '
+ gl.getProgramInfoLog(program));
return null;
}
return program;
}
function setupProgramForVideo(gl, vertexSource, fragmentSource) {
const program = setupProgram(gl, vertexSource, fragmentSource);
return program;
}
const webglPrograms = {
video: null,
icon: null,
animation: null,
border: null,
};
function initializePrograms(gl) {
webglPrograms.video = setupProgramForVideo(gl, webglShaders.vertex,
webglShaders.fragment);
webglPrograms.icon = setupProgram(gl, webglShaders.vertexIcons,
webglShaders.fragmentOutputBlue);
webglPrograms.animation = setupProgram(gl, webglShaders.vertexIcons,
webglShaders.fragmentOutputWhite);
webglPrograms.border = setupProgram(gl, webglShaders.vertexIcons,
webglShaders.fragmentOutputLightBlue);
}
function createVertexBufferForVideos(gl, videos, videoRows, videoColumns) {
const rectVerts = getArrayForVideoVertexBuffer(videos, videoRows,
videoColumns);
const verticesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, rectVerts, gl.STATIC_DRAW);
return verticesBuffer;
}
function bindVertexBufferForTextureQuad(gl, vertexBuffer) {
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
const pos = gl.getAttribLocation(webglPrograms.video, "aVertPos");
gl.vertexAttribPointer(pos, 2, gl.FLOAT, false, 16, 0);
gl.enableVertexAttribArray(pos);
const coord = gl.getAttribLocation(webglPrograms.video, "aTexCoord");
gl.vertexAttribPointer(coord, 2, gl.FLOAT, false, 16, 8);
gl.enableVertexAttribArray(coord);
}
function bindVertexBufferForVideos(gl) {
bindVertexBufferForTextureQuad(gl, webglVertexBuffers.video);
}
function createVertexBufferForIcons(gl, videos, videoRows, videoColumns) {
const rectVerts = getArrayForIconVertexBuffer(videos, videoRows,
videoColumns);
const verticesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, rectVerts, gl.STATIC_DRAW);
return verticesBuffer;
}
function bindVertexBufferForIcons(gl) {
gl.bindBuffer(gl.ARRAY_BUFFER, webglVertexBuffers.icon);
const pos = gl.getAttribLocation(webglPrograms.icon, "aVertPos");
gl.vertexAttribPointer(pos, 2, gl.FLOAT, false, 8, 0);
gl.enableVertexAttribArray(pos);
}
function createVertexBufferForAnimation(gl, videos, videoRows, videoColumns) {
const rectVerts = getArrayForAnimationVertexBuffer(videos, videoRows,
videoColumns);
const verticesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, rectVerts, gl.STATIC_DRAW);
return verticesBuffer;
}
function bindVertexBufferForAnimation(gl) {
gl.bindBuffer(gl.ARRAY_BUFFER, webglVertexBuffers.animation);
let pos = gl.getAttribLocation(webglPrograms.animation, "aVertPos");
gl.vertexAttribPointer(pos, 2, gl.FLOAT, false, 8, 0);
gl.enableVertexAttribArray(pos);
pos = gl.getAttribLocation(webglPrograms.border, "aVertPos");
gl.vertexAttribPointer(pos, 2, gl.FLOAT, false, 8, 0);
gl.enableVertexAttribArray(pos);
}
function createVertexBufferForFPS(gl) {
const rectVerts = getArrayForFPSVertexBuffer(fpsPanels.length);
const verticesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, rectVerts, gl.STATIC_DRAW);
return verticesBuffer;
}
function bindVertexBufferForFPS(gl) {
bindVertexBufferForTextureQuad(gl, webglVertexBuffers.fps);
}
const webglVertexBuffers = {
video: null,
icon: null,
animation: null,
};
function initializeVertexBuffers(gl, videos, videoRows, videoColumns, addFPS) {
webglVertexBuffers.video = createVertexBufferForVideos(gl, videos, videoRows,
videoColumns);
webglVertexBuffers.icon = createVertexBufferForIcons(gl, videos, videoRows,
videoColumns);
webglVertexBuffers.animation = createVertexBufferForAnimation(gl, videos,
videoRows, videoColumns);
if (addFPS) {
webglVertexBuffers.fps = createVertexBufferForFPS(gl);
}
}
function initTexture(gl) {
const texture = gl.createTexture();
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
return texture;
}
const webglVideoTextures = [];
function initializeVideoTextures(gl, count) {
for (let i = 0; i < count; ++i) {
const texture = initTexture(gl);
webglVideoTextures.push({texture});
}
}
function updateTextureViaTexStorage2D(
gl, textureInfo, source, texWidth, texHeight) {
if (textureInfo.texWidth !== texWidth ||
textureInfo.texHeight !== texHeight) {
gl.deleteTexture(textureInfo.texture);
textureInfo.texture = initTexture(gl);
textureInfo.texWidth = texWidth;
textureInfo.texHeight = texHeight;
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA8, texWidth, texHeight);
console.log(`Reinitializing texture (${texWidth}x${texHeight})`);
} else {
gl.activeTexture(gl.TEXTURE0);
}
texture = textureInfo.texture;
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texSubImage2D(
gl.TEXTURE_2D, 0, 0, 0, texWidth, texHeight, gl.RGBA, gl.UNSIGNED_BYTE,
source);
}
function updateTextureViaTexImage2D(gl, textureInfo, source) {
if (textureInfo.texWidth || textureInfo.texHeight) {
gl.deleteTexture(textureInfo.texture);
textureInfo.texture = initTexture(gl);
textureInfo.texWidth = 0;
textureInfo.texHeight = 0;
console.log('Reinitializing texture');
} else {
gl.activeTexture(gl.TEXTURE0);
}
texture = textureInfo.texture;
gl.bindTexture(gl.TEXTURE_2D, texture);
const level = 0;
const internalFormat = gl.RGBA;
const srcFormat = gl.RGBA;
const srcType = gl.UNSIGNED_BYTE;
gl.texImage2D(
gl.TEXTURE_2D, level, internalFormat, srcFormat, srcType, source);
}
const webglFPSTextures = [];
function initializeFPSTextures(gl, count) {
for (let i = 0; i < count; ++i) {
const texture = initTexture(gl);
webglFPSTextures.push({texture});
}
}
function webglDrawVideoFrames(
gl, videos, videoRows, videoColumns, addUI, addFPS, capUIFPS,
fixedTextureSize) {
initializePrograms(gl);
initializeVideoTextures(gl, videos.length);
if (addFPS) {
initializeFPSPanels();
initializeFPSTextures(gl, fpsPanels.length);
}
initializeVertexBuffers(gl, videos, videoRows, videoColumns, addFPS);
// videos #0-#3 : 30 fps.
// videos #3-#15: 15 fps.
// videos #16+: 7 fps.
// Not every video frame is ready in rAF callback. Only draw videos that
// are ready.
var videoIsReady = new Array(videos.length);
function updateIsVideoReady(video) {
videoIsReady[video.id] = true;
video.requestVideoFrameCallback(function () {
updateIsVideoReady(video);
});
}
for (const video of videos) {
video.requestVideoFrameCallback(function () {
updateIsVideoReady(video);
});
}
let lastTimestamp = performance.now();
let index_voice_bar = 0;
const oneFrame = () => {
const timestamp = performance.now();
if (capUIFPS) {
const elapsed = timestamp - lastTimestamp;
if (elapsed < kFrameTime30Fps) {
window.requestAnimationFrame(oneFrame);
return;
}
lastTimestamp = timestamp;
}
uiFrames++;
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(webglPrograms.video);
gl.uniform1i(gl.getUniformLocation(webglPrograms.video, "uSampler"), 0);
bindVertexBufferForVideos(gl);
for (let i = 0; i < videos.length; ++i) {
if (videoIsReady[videos[i].id]) {
if (fixedTextureSize) {
updateTextureViaTexStorage2D(
gl, webglVideoTextures[i], videos[i], videos[i].videoWidth,
videos[i].videoHeight);
} else {
updateTextureViaTexImage2D(gl, webglVideoTextures[i], videos[i]);
}
videoIsReady[videos[i].id] = false;
totalVideoFrames++;
}
}
for (let i = 0; i < videos.length; ++i) {
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, webglVideoTextures[i].texture);
gl.drawArrays(gl.TRIANGLES, 6 * i, 6);
}
// Add UI on Top of all videos.
if (addUI) {
gl.useProgram(webglPrograms.icon);
bindVertexBufferForIcons(gl);
gl.drawArrays(gl.TRIANGLES, 0, 6 * videos.length);
// Animated voice bar on the last video.
index_voice_bar++;
if (index_voice_bar >= 10) {
index_voice_bar = 0;
}
gl.useProgram(webglPrograms.animation);
bindVertexBufferForAnimation(gl);
gl.drawArrays(gl.TRIANGLES, index_voice_bar * 6, 6);
gl.useProgram(webglPrograms.border);
gl.bindBuffer(gl.ARRAY_BUFFER, webglVertexBuffers.animation);
gl.drawArrays(gl.LINES, 60, 8);
}
if (addFPS) {
updateFPS(timestamp, videos);
// Re-use the video program to draw FPS panels.
gl.useProgram(webglPrograms.video);
bindVertexBufferForFPS(gl);
for (let i = 0; i < fpsPanels.length; ++i) {
updateTextureViaTexImage2D(gl, webglFPSTextures[i], fpsPanels[i].dom);
gl.drawArrays(gl.TRIANGLES, 6 * i, 6);
}
}
window.requestAnimationFrame(oneFrame);
};
window.requestAnimationFrame(oneFrame);
}