trtc-electron-sdk 12.2.702 → 12.2.703-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,439 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const createProgramFromSources = require('./webgl-utils').createProgramFromSources;
4
- const EventEmitter = require('events').EventEmitter;
5
- const { config, isNeedKeepWidthAndHeight } = require('../util');
6
- const TrtcRender = function () {
7
- let gl;
8
- let program;
9
- let positionLocation;
10
- let texCoordLocation;
11
- let yTexture;
12
- let uTexture;
13
- let vTexture;
14
- let texCoordBuffer;
15
- let surfaceBuffer;
16
- const that = {
17
- view: undefined,
18
- mirrorView: false,
19
- container: undefined,
20
- canvas: undefined,
21
- renderImageCount: 0,
22
- initWidth: 0,
23
- initHeight: 0,
24
- initRotation: 0,
25
- clientWidth: 0,
26
- clientHeight: 0,
27
- // 0 - cover, 1 - fit
28
- contentMode: 1,
29
- lastImageWidth: 0,
30
- lastImageHeight: 0,
31
- lastImageRotation: 0,
32
- lastIsNeedRotate: true,
33
- firstFrameRender: false,
34
- event: new EventEmitter()
35
- };
36
- that.getView = function () {
37
- return that.view;
38
- };
39
- this.getView = that.getView;
40
- that.setContentMode = function (mode) {
41
- that.contentMode = mode;
42
- };
43
- that.bind = function (view) {
44
- initCanvas(view, that.mirrorView, view.clientWidth, view.clientHeight, that.initRotation, true, console.warn);
45
- };
46
- that.unbind = function () {
47
- try {
48
- gl.getExtension('WEBGL_lose_context').loseContext();
49
- }
50
- catch (err) {
51
- console.warn('trtc:', err);
52
- }
53
- program = undefined;
54
- positionLocation = undefined;
55
- texCoordLocation = undefined;
56
- deleteTexture(yTexture);
57
- deleteTexture(uTexture);
58
- deleteTexture(vTexture);
59
- yTexture = undefined;
60
- uTexture = undefined;
61
- vTexture = undefined;
62
- deleteBuffer(texCoordBuffer);
63
- deleteBuffer(surfaceBuffer);
64
- texCoordBuffer = undefined;
65
- surfaceBuffer = undefined;
66
- gl = undefined;
67
- try {
68
- that.container && that.container.removeChild(that.canvas);
69
- that.view && that.view.removeChild(that.container);
70
- }
71
- catch (e) {
72
- console.warn('trtc:', e);
73
- }
74
- that.canvas = undefined;
75
- that.container = undefined;
76
- that.view = undefined;
77
- that.mirrorView = false;
78
- };
79
- that.refreshCanvas = function () {
80
- if (that.lastImageWidth) {
81
- updateViewZoomLevel(that.lastImageRotation, that.lastImageWidth, that.lastImageHeight, that.lastIsNeedRotate);
82
- }
83
- };
84
- that.renderImage = function (image) {
85
- if (!gl) {
86
- console.log('trtc:no gl');
87
- return;
88
- }
89
- if (image.width != that.initWidth
90
- || image.height != that.initHeight
91
- || image.rotation != that.initRotation
92
- || image.mirror != that.mirrorView) {
93
- const view = that.view;
94
- that.unbind();
95
- initCanvas(view, image.mirror, image.width, image.height, image.rotation, image.isNeedRotate, e => {
96
- console.error(`trtc:init canvas ${image.width}*${image.height} rotation ${image.rotation} failed. ${e}`);
97
- });
98
- }
99
- gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
100
- const xWidth = image.width + image.left + image.right;
101
- const xHeight = image.height + image.top + image.bottom;
102
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
103
- image.left / xWidth,
104
- image.bottom / xHeight,
105
- 1 - image.right / xWidth,
106
- image.bottom / xHeight,
107
- image.left / xWidth,
108
- 1 - image.top / xHeight,
109
- image.left / xWidth,
110
- 1 - image.top / xHeight,
111
- 1 - image.right / xWidth,
112
- image.bottom / xHeight,
113
- 1 - image.right / xWidth,
114
- 1 - image.top / xHeight
115
- ]), gl.STATIC_DRAW);
116
- gl.enableVertexAttribArray(texCoordLocation);
117
- gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
118
- uploadYuv(xWidth, xHeight, image.yplane, image.uplane, image.vplane);
119
- updateCanvas(image.rotation, image.width, image.height, image.isNeedRotate);
120
- gl.drawArrays(gl.TRIANGLES, 0, 6);
121
- that.renderImageCount += 1;
122
- if (!that.firstFrameRender) {
123
- that.firstFrameRender = true;
124
- that.event.emit('ready');
125
- }
126
- };
127
- that.drawFrame = function (imageData) {
128
- const mirror = 0;
129
- const contentWidth = imageData.width;
130
- const contentHeight = imageData.height;
131
- const isNeedRotate = imageData.isNeedRotate;
132
- const left = 0;
133
- const top = 0;
134
- const right = 0;
135
- const bottom = 0;
136
- const rotation = imageData.rotation;
137
- const width = contentWidth + left + right;
138
- const height = contentHeight + top + bottom;
139
- const headerLength = 0;
140
- const ts = imageData.timestamp;
141
- const xWidth = contentWidth + left + right;
142
- const xHeight = contentHeight + top + bottom;
143
- const yLength = xWidth * xHeight;
144
- const yBegin = headerLength;
145
- const yEnd = yBegin + yLength;
146
- const uLength = yLength / 4;
147
- const uBegin = yEnd;
148
- const uEnd = uBegin + uLength;
149
- const vLength = yLength / 4;
150
- const vBegin = uEnd;
151
- const vEnd = vBegin + vLength;
152
- that.renderImage({
153
- mirror: mirror,
154
- width,
155
- height,
156
- left,
157
- top,
158
- right,
159
- bottom,
160
- rotation: rotation,
161
- yplane: imageData.yUint8Array,
162
- uplane: imageData.uUint8Array,
163
- vplane: imageData.vUint8Array,
164
- isNeedRotate
165
- });
166
- const now32 = (Date.now() & 0xffffffff) >>> 0;
167
- const latency = now32 - ts;
168
- };
169
- function uploadYuv(width, height, yplane, uplane, vplane) {
170
- let e;
171
- gl.activeTexture(gl.TEXTURE0);
172
- gl.bindTexture(gl.TEXTURE_2D, yTexture);
173
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
174
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, width, height, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, yplane);
175
- if (config.getDebugMode()) {
176
- e = gl.getError();
177
- if (e != gl.NO_ERROR) {
178
- console.log('trtc:upload y plane ', width, height, yplane.byteLength, ' error', e);
179
- }
180
- }
181
- gl.activeTexture(gl.TEXTURE1);
182
- gl.bindTexture(gl.TEXTURE_2D, uTexture);
183
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, width / 2, height / 2, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, uplane);
184
- if (config.getDebugMode()) {
185
- e = gl.getError();
186
- if (e != gl.NO_ERROR) {
187
- console.log('trtc:upload u plane ', width, height, uplane.byteLength, ' error', e);
188
- }
189
- }
190
- gl.activeTexture(gl.TEXTURE2);
191
- gl.bindTexture(gl.TEXTURE_2D, vTexture);
192
- ('');
193
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, width / 2, height / 2, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, vplane);
194
- if (config.getDebugMode()) {
195
- e = gl.getError();
196
- if (e != gl.NO_ERROR) {
197
- console.log('trtc:upload v plane ', width, height, vplane.byteLength, ' error', e);
198
- }
199
- }
200
- }
201
- function deleteBuffer(buffer) {
202
- if (buffer && gl) {
203
- gl.deleteBuffer(buffer);
204
- }
205
- }
206
- function deleteTexture(texture) {
207
- if (texture && gl) {
208
- gl.deleteTexture(texture);
209
- }
210
- }
211
- const vertexShaderSource = 'attribute vec2 a_position;'
212
- + 'attribute vec2 a_texCoord;'
213
- + 'uniform vec2 u_resolution;'
214
- + 'varying vec2 v_texCoord;'
215
- + 'void main() {'
216
- + 'vec2 zeroToOne = a_position / u_resolution;'
217
- + ' vec2 zeroToTwo = zeroToOne * 2.0;'
218
- + ' vec2 clipSpace = zeroToTwo - 1.0;'
219
- + ' gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);'
220
- + 'v_texCoord = a_texCoord;'
221
- + '}';
222
- const yuvShaderSource = 'precision mediump float;'
223
- + 'uniform sampler2D Ytex;'
224
- + 'uniform sampler2D Utex,Vtex;'
225
- + 'varying vec2 v_texCoord;'
226
- + 'void main(void) {'
227
- + ' float nx,ny,r,g,b,y,u,v;'
228
- + ' mediump vec4 txl,ux,vx;'
229
- + ' nx=v_texCoord[0];'
230
- + ' ny=v_texCoord[1];'
231
- + ' y=texture2D(Ytex,vec2(nx,ny)).r;'
232
- + ' u=texture2D(Utex,vec2(nx,ny)).r;'
233
- + ' v=texture2D(Vtex,vec2(nx,ny)).r;'
234
- + ' y=1.1643*(y-0.0625);'
235
- + ' u=u-0.5;'
236
- + ' v=v-0.5;'
237
- + ' r=y+1.5958*v;'
238
- + ' g=y-0.39173*u-0.81290*v;'
239
- + ' b=y+2.017*u;'
240
- + ' gl_FragColor=vec4(r,g,b,1.0);'
241
- + '}';
242
- function initCanvas(view, mirror, width, height, rotation, isNeedRotate, onFailure) {
243
- that.clientWidth = view.clientWidth;
244
- that.clientHeight = view.clientHeight;
245
- that.view = view;
246
- that.mirrorView = mirror;
247
- that.container = document.createElement('div');
248
- that.container.style.width = '100%';
249
- that.container.style.height = '100%';
250
- that.container.style.display = 'flex';
251
- that.container.style.justifyContent = 'center';
252
- that.container.style.alignItems = 'center';
253
- that.view.appendChild(that.container);
254
- that.canvas = document.createElement('canvas');
255
- if (isNeedKeepWidthAndHeight(rotation, isNeedRotate)) {
256
- that.canvas.width = width;
257
- that.canvas.height = height;
258
- }
259
- else {
260
- that.canvas.width = height;
261
- that.canvas.height = width;
262
- }
263
- that.initWidth = width;
264
- that.initHeight = height;
265
- that.initRotation = rotation;
266
- if (that.mirrorView) {
267
- that.canvas.style.transform = 'rotateY(180deg)';
268
- }
269
- that.container.appendChild(that.canvas);
270
- try {
271
- gl = that.canvas.getContext('webgl', {
272
- preserveDrawingBuffer: true
273
- }) || that.canvas.getContext('experimental-webgl');
274
- }
275
- catch (e) {
276
- console.log('trtc:', e);
277
- }
278
- if (!gl) {
279
- gl = undefined;
280
- onFailure({ error: 'Browser not support! No WebGL detected.' });
281
- return;
282
- }
283
- gl.enable(gl.DEPTH_TEST);
284
- gl.depthFunc(gl.LEQUAL);
285
- gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
286
- program = createProgramFromSources(gl, [vertexShaderSource, yuvShaderSource]);
287
- gl.useProgram(program);
288
- initTextures();
289
- }
290
- function initTextures() {
291
- positionLocation = gl.getAttribLocation(program, 'a_position');
292
- texCoordLocation = gl.getAttribLocation(program, 'a_texCoord');
293
- surfaceBuffer = gl.createBuffer();
294
- texCoordBuffer = gl.createBuffer();
295
- gl.activeTexture(gl.TEXTURE0);
296
- yTexture = gl.createTexture();
297
- gl.bindTexture(gl.TEXTURE_2D, yTexture);
298
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
299
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
300
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
301
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
302
- gl.activeTexture(gl.TEXTURE1);
303
- uTexture = gl.createTexture();
304
- gl.bindTexture(gl.TEXTURE_2D, uTexture);
305
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
306
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
307
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
308
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
309
- gl.activeTexture(gl.TEXTURE2);
310
- vTexture = gl.createTexture();
311
- gl.bindTexture(gl.TEXTURE_2D, vTexture);
312
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
313
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
314
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
315
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
316
- const y = gl.getUniformLocation(program, 'Ytex');
317
- gl.uniform1i(y, 0);
318
- const u = gl.getUniformLocation(program, 'Utex');
319
- gl.uniform1i(u, 1);
320
- const v = gl.getUniformLocation(program, 'Vtex');
321
- gl.uniform1i(v, 2);
322
- }
323
- function updateViewZoomLevel(rotation, width, height, isNeedRotate) {
324
- that.clientWidth = that.view.clientWidth;
325
- that.clientHeight = that.view.clientHeight;
326
- const isKeepWidthHeight = isNeedKeepWidthAndHeight(rotation, isNeedRotate);
327
- try {
328
- if (that.contentMode === 0) {
329
- if (isKeepWidthHeight) {
330
- if (that.clientWidth / that.clientHeight > width / height) {
331
- that.canvas.style.zoom = that.clientWidth / width;
332
- }
333
- else {
334
- that.canvas.style.zoom = that.clientHeight / height;
335
- }
336
- }
337
- else {
338
- if (that.clientHeight / that.clientWidth > width / height) {
339
- that.canvas.style.zoom = that.clientHeight / width;
340
- }
341
- else {
342
- that.canvas.style.zoom = that.clientWidth / height;
343
- }
344
- }
345
- }
346
- else if (isKeepWidthHeight) {
347
- if (that.clientWidth / that.clientHeight > width / height) {
348
- that.canvas.style.zoom = that.clientHeight / height;
349
- }
350
- else {
351
- that.canvas.style.zoom = that.clientWidth / width;
352
- }
353
- }
354
- else {
355
- if (that.clientHeight / that.clientWidth > width / height) {
356
- that.canvas.style.zoom = that.clientWidth / height;
357
- }
358
- else {
359
- that.canvas.style.zoom = that.clientHeight / width;
360
- }
361
- }
362
- }
363
- catch (e) {
364
- console.log(`trtc:updateCanvas error ${that.canvas}`);
365
- console.log('trtc:', that);
366
- console.error('trtc:', e);
367
- return false;
368
- }
369
- return true;
370
- }
371
- function updateCanvas(rotation, width, height, isNeedRotate) {
372
- if (width || height) {
373
- that.lastImageWidth = width;
374
- that.lastImageHeight = height;
375
- that.lastImageRotation = rotation;
376
- that.lastIsNeedRotate = isNeedRotate;
377
- }
378
- else {
379
- width = that.lastImageWidth;
380
- height = that.lastImageHeight;
381
- rotation = that.lastImageRotation;
382
- isNeedRotate = that.lastIsNeedRotate;
383
- }
384
- if (!updateViewZoomLevel(rotation, width, height, isNeedRotate)) {
385
- return;
386
- }
387
- gl.bindBuffer(gl.ARRAY_BUFFER, surfaceBuffer);
388
- gl.enableVertexAttribArray(positionLocation);
389
- gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
390
- const p1 = { x: 0, y: 0 };
391
- const p2 = { x: width, y: 0 };
392
- const p3 = { x: width, y: height };
393
- const p4 = { x: 0, y: height };
394
- let pp1 = p1, pp2 = p2, pp3 = p3, pp4 = p4;
395
- if (isNeedRotate) {
396
- switch (rotation) {
397
- case 0:
398
- break;
399
- case 90:
400
- pp1 = p2;
401
- pp2 = p3;
402
- pp3 = p4;
403
- pp4 = p1;
404
- break;
405
- case 180:
406
- pp1 = p3;
407
- pp2 = p4;
408
- pp3 = p1;
409
- pp4 = p2;
410
- break;
411
- case 270:
412
- pp1 = p4;
413
- pp2 = p1;
414
- pp3 = p2;
415
- pp4 = p3;
416
- break;
417
- default:
418
- }
419
- }
420
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
421
- pp1.x,
422
- pp1.y,
423
- pp2.x,
424
- pp2.y,
425
- pp4.x,
426
- pp4.y,
427
- pp4.x,
428
- pp4.y,
429
- pp2.x,
430
- pp2.y,
431
- pp3.x,
432
- pp3.y
433
- ]), gl.STATIC_DRAW);
434
- const resolutionLocation = gl.getUniformLocation(program, 'u_resolution');
435
- gl.uniform2f(resolutionLocation, width, height);
436
- }
437
- return that;
438
- };
439
- exports.default = TrtcRender;