agora-electron-sdk 4.2.6 → 4.3.0-dev.1

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.
Files changed (172) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/js/Private/AgoraBase.js +139 -174
  3. package/js/Private/AgoraMediaBase.js +49 -20
  4. package/js/Private/AgoraMediaPlayerTypes.js +32 -23
  5. package/js/Private/IAgoraH265Transcoder.js +39 -0
  6. package/js/Private/IAgoraMediaStreamingSource.js +32 -32
  7. package/js/Private/IAgoraMusicContentCenter.js +19 -19
  8. package/js/Private/IAgoraRhythmPlayer.js +9 -9
  9. package/js/Private/IAgoraRtcEngine.js +12 -12
  10. package/js/Private/IAgoraSpatialAudio.js +3 -31
  11. package/js/Private/extension/IAgoraH265TranscoderExtension.js +2 -0
  12. package/js/Private/impl/AgoraMediaBaseImpl.js +26 -2
  13. package/js/Private/impl/IAgoraH265TranscoderImpl.js +119 -0
  14. package/js/Private/impl/IAgoraMediaEngineImpl.js +13 -13
  15. package/js/Private/impl/IAgoraMediaPlayerImpl.js +92 -74
  16. package/js/Private/impl/IAgoraMediaPlayerSourceImpl.js +12 -2
  17. package/js/Private/impl/IAgoraMediaRecorderImpl.js +2 -2
  18. package/js/Private/impl/IAgoraMusicContentCenterImpl.js +20 -20
  19. package/js/Private/impl/IAgoraRtcEngineExImpl.js +66 -84
  20. package/js/Private/impl/IAgoraRtcEngineImpl.js +456 -440
  21. package/js/Private/impl/IAgoraSpatialAudioImpl.js +157 -181
  22. package/js/Private/impl/IAudioDeviceManagerImpl.js +22 -22
  23. package/js/Private/internal/AgoraH265TranscoderInternal.js +86 -0
  24. package/js/Private/internal/AgoraMediaBaseInternal.js +34 -0
  25. package/js/Private/internal/IrisApiEngine.js +35 -20
  26. package/js/Private/internal/LocalSpatialAudioEngineInternal.js +0 -39
  27. package/js/Private/internal/MediaPlayerInternal.js +22 -21
  28. package/js/Private/internal/MusicContentCenterInternal.js +1 -4
  29. package/js/Private/internal/RtcEngineExInternal.js +101 -134
  30. package/js/Private/ti/AgoraMediaBase-ti.js +1 -1
  31. package/js/Private/ti/IAgoraH265Transcoder-ti.js +40 -0
  32. package/js/Private/ti/IAgoraMediaPlayerSource-ti.js +4 -2
  33. package/js/Private/ti/IAgoraMusicContentCenter-ti.js +5 -5
  34. package/js/Private/ti/IAgoraRtcEngine-ti.js +12 -11
  35. package/js/Renderer/AgoraView.js +28 -14
  36. package/js/Renderer/IRenderer.js +65 -17
  37. package/js/Renderer/IRendererManager.js +230 -0
  38. package/js/Renderer/RendererCache.js +170 -0
  39. package/js/Renderer/RendererManager.js +49 -460
  40. package/js/Renderer/WebGLRenderer/index.js +82 -234
  41. package/js/Renderer/YUVCanvasRenderer/index.js +27 -147
  42. package/js/Types.js +6 -6
  43. package/js/Utils.js +37 -88
  44. package/package.json +4 -3
  45. package/scripts/clean.js +12 -0
  46. package/scripts/synclib.js +8 -2
  47. package/ts/Private/AgoraBase.ts +225 -201
  48. package/ts/Private/AgoraMediaBase.ts +63 -25
  49. package/ts/Private/AgoraMediaPlayerTypes.ts +67 -24
  50. package/ts/Private/IAgoraH265Transcoder.ts +73 -0
  51. package/ts/Private/IAgoraLog.ts +1 -0
  52. package/ts/Private/IAgoraMediaEngine.ts +12 -7
  53. package/ts/Private/IAgoraMediaPlayer.ts +47 -21
  54. package/ts/Private/IAgoraMediaPlayerSource.ts +27 -6
  55. package/ts/Private/IAgoraMediaStreamingSource.ts +38 -37
  56. package/ts/Private/IAgoraMusicContentCenter.ts +20 -20
  57. package/ts/Private/IAgoraRhythmPlayer.ts +7 -6
  58. package/ts/Private/IAgoraRtcEngine.ts +407 -364
  59. package/ts/Private/IAgoraRtcEngineEx.ts +25 -61
  60. package/ts/Private/IAgoraSpatialAudio.ts +80 -191
  61. package/ts/Private/IAudioDeviceManager.ts +27 -14
  62. package/ts/Private/extension/IAgoraH265TranscoderExtension.ts +39 -0
  63. package/ts/Private/impl/AgoraMediaBaseImpl.ts +25 -1
  64. package/ts/Private/impl/IAgoraH265TranscoderImpl.ts +152 -0
  65. package/ts/Private/impl/IAgoraMediaEngineImpl.ts +13 -13
  66. package/ts/Private/impl/IAgoraMediaPlayerImpl.ts +110 -81
  67. package/ts/Private/impl/IAgoraMediaPlayerSourceImpl.ts +17 -2
  68. package/ts/Private/impl/IAgoraMediaRecorderImpl.ts +2 -2
  69. package/ts/Private/impl/IAgoraMusicContentCenterImpl.ts +21 -21
  70. package/ts/Private/impl/IAgoraRtcEngineExImpl.ts +71 -106
  71. package/ts/Private/impl/IAgoraRtcEngineImpl.ts +589 -572
  72. package/ts/Private/impl/IAgoraSpatialAudioImpl.ts +211 -218
  73. package/ts/Private/impl/IAudioDeviceManagerImpl.ts +22 -22
  74. package/ts/Private/internal/AgoraH265TranscoderInternal.ts +97 -0
  75. package/ts/Private/internal/AgoraMediaBaseInternal.ts +15 -0
  76. package/ts/Private/internal/IrisApiEngine.ts +42 -27
  77. package/ts/Private/internal/LocalSpatialAudioEngineInternal.ts +1 -86
  78. package/ts/Private/internal/MediaPlayerInternal.ts +20 -29
  79. package/ts/Private/internal/MusicContentCenterInternal.ts +1 -5
  80. package/ts/Private/internal/RtcEngineExInternal.ts +91 -204
  81. package/ts/Private/ti/AgoraMediaBase-ti.ts +1 -1
  82. package/ts/Private/ti/IAgoraH265Transcoder-ti.ts +16 -0
  83. package/ts/Private/ti/IAgoraMediaPlayerSource-ti.ts +4 -2
  84. package/ts/Private/ti/IAgoraMusicContentCenter-ti.ts +5 -5
  85. package/ts/Private/ti/IAgoraRtcEngine-ti.ts +12 -11
  86. package/ts/Renderer/AgoraView.ts +29 -19
  87. package/ts/Renderer/IRenderer.ts +71 -22
  88. package/ts/Renderer/IRendererManager.ts +273 -19
  89. package/ts/Renderer/RendererCache.ts +167 -0
  90. package/ts/Renderer/RendererManager.ts +62 -607
  91. package/ts/Renderer/WebGLRenderer/index.ts +117 -295
  92. package/ts/Renderer/YUVCanvasRenderer/index.ts +45 -198
  93. package/ts/Types.ts +17 -194
  94. package/ts/Utils.ts +36 -100
  95. package/types/Private/AgoraBase.d.ts +219 -200
  96. package/types/Private/AgoraBase.d.ts.map +1 -1
  97. package/types/Private/AgoraMediaBase.d.ts +63 -27
  98. package/types/Private/AgoraMediaBase.d.ts.map +1 -1
  99. package/types/Private/AgoraMediaPlayerTypes.d.ts +65 -24
  100. package/types/Private/AgoraMediaPlayerTypes.d.ts.map +1 -1
  101. package/types/Private/IAgoraH265Transcoder.d.ts +28 -0
  102. package/types/Private/IAgoraH265Transcoder.d.ts.map +1 -0
  103. package/types/Private/IAgoraLog.d.ts.map +1 -1
  104. package/types/Private/IAgoraMediaEngine.d.ts +11 -6
  105. package/types/Private/IAgoraMediaEngine.d.ts.map +1 -1
  106. package/types/Private/IAgoraMediaPlayer.d.ts +42 -20
  107. package/types/Private/IAgoraMediaPlayer.d.ts.map +1 -1
  108. package/types/Private/IAgoraMediaPlayerSource.d.ts +23 -6
  109. package/types/Private/IAgoraMediaPlayerSource.d.ts.map +1 -1
  110. package/types/Private/IAgoraMediaStreamingSource.d.ts.map +1 -1
  111. package/types/Private/IAgoraMusicContentCenter.d.ts +19 -19
  112. package/types/Private/IAgoraMusicContentCenter.d.ts.map +1 -1
  113. package/types/Private/IAgoraRhythmPlayer.d.ts +6 -6
  114. package/types/Private/IAgoraRhythmPlayer.d.ts.map +1 -1
  115. package/types/Private/IAgoraRtcEngine.d.ts +329 -293
  116. package/types/Private/IAgoraRtcEngine.d.ts.map +1 -1
  117. package/types/Private/IAgoraRtcEngineEx.d.ts +21 -53
  118. package/types/Private/IAgoraRtcEngineEx.d.ts.map +1 -1
  119. package/types/Private/IAgoraSpatialAudio.d.ts +56 -167
  120. package/types/Private/IAgoraSpatialAudio.d.ts.map +1 -1
  121. package/types/Private/IAudioDeviceManager.d.ts +27 -14
  122. package/types/Private/IAudioDeviceManager.d.ts.map +1 -1
  123. package/types/Private/extension/IAgoraH265TranscoderExtension.d.ts +24 -0
  124. package/types/Private/extension/IAgoraH265TranscoderExtension.d.ts.map +1 -0
  125. package/types/Private/impl/AgoraMediaBaseImpl.d.ts +5 -1
  126. package/types/Private/impl/AgoraMediaBaseImpl.d.ts.map +1 -1
  127. package/types/Private/impl/IAgoraH265TranscoderImpl.d.ts +15 -0
  128. package/types/Private/impl/IAgoraH265TranscoderImpl.d.ts.map +1 -0
  129. package/types/Private/impl/IAgoraMediaPlayerImpl.d.ts +6 -4
  130. package/types/Private/impl/IAgoraMediaPlayerImpl.d.ts.map +1 -1
  131. package/types/Private/impl/IAgoraMediaPlayerSourceImpl.d.ts.map +1 -1
  132. package/types/Private/impl/IAgoraRtcEngineExImpl.d.ts +2 -4
  133. package/types/Private/impl/IAgoraRtcEngineExImpl.d.ts.map +1 -1
  134. package/types/Private/impl/IAgoraRtcEngineImpl.d.ts +30 -25
  135. package/types/Private/impl/IAgoraRtcEngineImpl.d.ts.map +1 -1
  136. package/types/Private/impl/IAgoraSpatialAudioImpl.d.ts +20 -22
  137. package/types/Private/impl/IAgoraSpatialAudioImpl.d.ts.map +1 -1
  138. package/types/Private/internal/AgoraH265TranscoderInternal.d.ts +14 -0
  139. package/types/Private/internal/AgoraH265TranscoderInternal.d.ts.map +1 -0
  140. package/types/Private/internal/AgoraMediaBaseInternal.d.ts +8 -0
  141. package/types/Private/internal/AgoraMediaBaseInternal.d.ts.map +1 -0
  142. package/types/Private/internal/IrisApiEngine.d.ts +5 -2
  143. package/types/Private/internal/IrisApiEngine.d.ts.map +1 -1
  144. package/types/Private/internal/LocalSpatialAudioEngineInternal.d.ts +0 -15
  145. package/types/Private/internal/LocalSpatialAudioEngineInternal.d.ts.map +1 -1
  146. package/types/Private/internal/MediaPlayerInternal.d.ts.map +1 -1
  147. package/types/Private/internal/MusicContentCenterInternal.d.ts +0 -1
  148. package/types/Private/internal/MusicContentCenterInternal.d.ts.map +1 -1
  149. package/types/Private/internal/RtcEngineExInternal.d.ts +7 -13
  150. package/types/Private/internal/RtcEngineExInternal.d.ts.map +1 -1
  151. package/types/Private/ti/IAgoraH265Transcoder-ti.d.ts +8 -0
  152. package/types/Private/ti/IAgoraH265Transcoder-ti.d.ts.map +1 -0
  153. package/types/Private/ti/IAgoraMediaPlayerSource-ti.d.ts.map +1 -1
  154. package/types/Private/ti/IAgoraRtcEngine-ti.d.ts.map +1 -1
  155. package/types/Renderer/AgoraView.d.ts +4 -4
  156. package/types/Renderer/AgoraView.d.ts.map +1 -1
  157. package/types/Renderer/IRenderer.d.ts +11 -9
  158. package/types/Renderer/IRenderer.d.ts.map +1 -1
  159. package/types/Renderer/IRendererManager.d.ts +50 -12
  160. package/types/Renderer/IRendererManager.d.ts.map +1 -1
  161. package/types/Renderer/RendererCache.d.ts +36 -0
  162. package/types/Renderer/RendererCache.d.ts.map +1 -0
  163. package/types/Renderer/RendererManager.d.ts +13 -139
  164. package/types/Renderer/RendererManager.d.ts.map +1 -1
  165. package/types/Renderer/WebGLRenderer/index.d.ts +3 -18
  166. package/types/Renderer/WebGLRenderer/index.d.ts.map +1 -1
  167. package/types/Renderer/YUVCanvasRenderer/index.d.ts +4 -10
  168. package/types/Renderer/YUVCanvasRenderer/index.d.ts.map +1 -1
  169. package/types/Types.d.ts +11 -187
  170. package/types/Types.d.ts.map +1 -1
  171. package/types/Utils.d.ts +3 -20
  172. package/types/Utils.d.ts.map +1 -1
@@ -1,6 +1,5 @@
1
- import { RenderModeType } from '../../Private/AgoraMediaBase';
2
- import { ShareVideoFrame } from '../../Types';
3
- import { logError, logWarn } from '../../Utils';
1
+ import { VideoFrame } from '../../Private/AgoraMediaBase';
2
+ import { logWarn } from '../../Utils';
4
3
  import { IRenderer } from '../IRenderer';
5
4
 
6
5
  export type WebGLFallback = (renderer: WebGLRenderer, error: Error) => void;
@@ -53,19 +52,6 @@ export class WebGLRenderer extends IRenderer {
53
52
  vTexture: WebGLTexture | null;
54
53
  texCoordBuffer: WebGLBuffer | null;
55
54
  surfaceBuffer: WebGLBuffer | null;
56
-
57
- initWidth = 0;
58
- initHeight = 0;
59
- initRotation = 0;
60
- clientWidth = 0;
61
- clientHeight = 0;
62
- lastImageWidth = 0;
63
- lastImageHeight = 0;
64
- lastImageRotation = 0;
65
- videoBuffer = {};
66
-
67
- observer?: ResizeObserver;
68
-
69
55
  fallback?: WebGLFallback;
70
56
 
71
57
  constructor(fallback?: WebGLFallback) {
@@ -82,13 +68,6 @@ export class WebGLRenderer extends IRenderer {
82
68
  public override bind(view: HTMLElement) {
83
69
  super.bind(view);
84
70
 
85
- const ResizeObserver = window.ResizeObserver;
86
- if (ResizeObserver) {
87
- this.observer = new ResizeObserver(this.refreshCanvas);
88
- this.observer.observe(view);
89
- }
90
-
91
- // context list after toggle resolution on electron 12.0.6
92
71
  this.canvas?.addEventListener(
93
72
  'webglcontextlost',
94
73
  this.handleContextLost,
@@ -99,15 +78,63 @@ export class WebGLRenderer extends IRenderer {
99
78
  this.handleContextRestored,
100
79
  false
101
80
  );
102
- }
103
81
 
104
- public override unbind() {
105
- if (this.parentElement) {
106
- this.observer?.unobserve(this.parentElement);
82
+ const getContext = (
83
+ contextNames = ['webgl2', 'webgl', 'experimental-webgl']
84
+ ): WebGLRenderingContext | WebGLRenderingContext | null => {
85
+ for (let i = 0; i < contextNames.length; i++) {
86
+ const contextName = contextNames[i]!;
87
+ const context = this.canvas?.getContext(contextName, {
88
+ depth: true,
89
+ stencil: true,
90
+ alpha: false,
91
+ antialias: false,
92
+ premultipliedAlpha: true,
93
+ preserveDrawingBuffer: true,
94
+ powerPreference: 'default',
95
+ failIfMajorPerformanceCaveat: false,
96
+ });
97
+ if (context) {
98
+ return context as WebGLRenderingContext | WebGLRenderingContext;
99
+ }
100
+ }
101
+ return null;
102
+ };
103
+ this.gl ??= getContext();
104
+
105
+ if (!this.gl) {
106
+ this.fallback?.call(
107
+ null,
108
+ this,
109
+ new Error('Browser not support! No WebGL detected.')
110
+ );
111
+ return;
107
112
  }
108
- this.observer?.disconnect();
109
- this.observer = undefined;
110
113
 
114
+ // Set clear color to black, fully opaque
115
+ this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
116
+ // Enable depth testing
117
+ this.gl.enable(this.gl.DEPTH_TEST);
118
+ // Near things obscure far things
119
+ this.gl.depthFunc(this.gl.LEQUAL);
120
+ // Clear the color as well as the depth buffer.
121
+ this.gl.clear(
122
+ this.gl.COLOR_BUFFER_BIT |
123
+ this.gl.DEPTH_BUFFER_BIT |
124
+ this.gl.STENCIL_BUFFER_BIT
125
+ );
126
+
127
+ // Setup GLSL program
128
+ this.program = createProgramFromSources(this.gl, [
129
+ vertexShaderSource,
130
+ yuvShaderSource,
131
+ ]) as WebGLProgram;
132
+ this.gl.useProgram(this.program);
133
+
134
+ this.initTextures();
135
+ }
136
+
137
+ public override unbind() {
111
138
  this.canvas?.removeEventListener(
112
139
  'webglcontextlost',
113
140
  this.handleContextLost,
@@ -125,91 +152,45 @@ export class WebGLRenderer extends IRenderer {
125
152
  super.unbind();
126
153
  }
127
154
 
128
- public override drawFrame(videoFrame: ShareVideoFrame) {
129
- let error;
130
- try {
131
- this.renderImage({
132
- width: videoFrame.width,
133
- height: videoFrame.height,
134
- left: 0,
135
- top: 0,
136
- right: videoFrame.yStride - videoFrame.width,
137
- bottom: 0,
138
- rotation: videoFrame.rotation || 0,
139
- yplane: videoFrame.yBuffer,
140
- uplane: videoFrame.uBuffer,
141
- vplane: videoFrame.vBuffer,
142
- });
143
- } catch (err) {
144
- error = err;
145
- }
146
- if (!this.gl || error) {
147
- this.fallback?.call(
148
- null,
149
- this,
150
- new Error('webgl lost or webgl initialize failed')
151
- );
152
- return;
153
- }
154
- }
155
-
156
- public override refreshCanvas() {
157
- if (this.lastImageWidth) {
158
- this.updateViewZoomLevel(
159
- this.lastImageRotation,
160
- this.lastImageWidth,
161
- this.lastImageHeight
162
- );
163
- }
164
- }
165
-
166
- private renderImage(image: {
167
- width: number;
168
- height: number;
169
- left: number;
170
- top: number;
171
- right: number;
172
- bottom: number;
173
- rotation: number;
174
- yplane: Uint8Array;
175
- uplane: Uint8Array;
176
- vplane: Uint8Array;
177
- }) {
178
- // Rotation, width, height, left, top, right, bottom, yplane, uplane, vplane
179
-
180
- if (
181
- image.width != this.initWidth ||
182
- image.height != this.initHeight ||
183
- image.rotation != this.initRotation
184
- ) {
185
- const view = this.parentElement!;
186
- this.unbind();
187
-
188
- this.initCanvas(view, image.width, image.height, image.rotation);
189
- }
190
-
191
- if (!this.gl || !this.program) {
192
- return;
193
- }
155
+ public override drawFrame({
156
+ width,
157
+ height,
158
+ yStride,
159
+ uStride,
160
+ vStride,
161
+ yBuffer,
162
+ uBuffer,
163
+ vBuffer,
164
+ rotation,
165
+ }: VideoFrame) {
166
+ this.rotateCanvas({ width, height, rotation });
167
+ this.updateRenderMode();
168
+
169
+ if (!this.gl || !this.program) return;
170
+
171
+ const left = 0,
172
+ top = 0,
173
+ right = yStride! - width!,
174
+ bottom = 0;
194
175
 
195
176
  this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.texCoordBuffer);
196
- const xWidth = image.width + image.left + image.right;
197
- const xHeight = image.height + image.top + image.bottom;
177
+ const xWidth = width! + left + right;
178
+ const xHeight = height! + top + bottom;
198
179
  this.gl.bufferData(
199
180
  this.gl.ARRAY_BUFFER,
200
181
  new Float32Array([
201
- image.left / xWidth,
202
- image.bottom / xHeight,
203
- 1 - image.right / xWidth,
204
- image.bottom / xHeight,
205
- image.left / xWidth,
206
- 1 - image.top / xHeight,
207
- image.left / xWidth,
208
- 1 - image.top / xHeight,
209
- 1 - image.right / xWidth,
210
- image.bottom / xHeight,
211
- 1 - image.right / xWidth,
212
- 1 - image.top / xHeight,
182
+ left / xWidth,
183
+ bottom / xHeight,
184
+ 1 - right / xWidth,
185
+ bottom / xHeight,
186
+ left / xWidth,
187
+ 1 - top / xHeight,
188
+ left / xWidth,
189
+ 1 - top / xHeight,
190
+ 1 - right / xWidth,
191
+ bottom / xHeight,
192
+ 1 - right / xWidth,
193
+ 1 - top / xHeight,
213
194
  ]),
214
195
  this.gl.STATIC_DRAW
215
196
  );
@@ -223,37 +204,21 @@ export class WebGLRenderer extends IRenderer {
223
204
  0
224
205
  );
225
206
 
226
- this.uploadYuv(xWidth, xHeight, image.yplane, image.uplane, image.vplane);
227
-
228
- this.updateCanvas(image.rotation, image.width, image.height);
229
- this.gl.drawArrays(this.gl.TRIANGLES, 0, 6);
230
- }
231
-
232
- private uploadYuv(
233
- width: number,
234
- height: number,
235
- yplane: Uint8Array,
236
- uplane: Uint8Array,
237
- vplane: Uint8Array
238
- ) {
239
- if (!this.gl || !this.yTexture || !this.uTexture || !this.vTexture) {
240
- return;
241
- }
242
-
243
207
  this.gl.pixelStorei(this.gl.UNPACK_ALIGNMENT, 1);
208
+
244
209
  this.gl.activeTexture(this.gl.TEXTURE0);
245
210
  this.gl.bindTexture(this.gl.TEXTURE_2D, this.yTexture);
246
-
247
211
  this.gl.texImage2D(
248
212
  this.gl.TEXTURE_2D,
249
213
  0,
250
214
  this.gl.LUMINANCE,
251
- width,
252
- height,
215
+ // Should use xWidth instead of width here (yStide)
216
+ xWidth,
217
+ height!,
253
218
  0,
254
219
  this.gl.LUMINANCE,
255
220
  this.gl.UNSIGNED_BYTE,
256
- yplane
221
+ yBuffer!
257
222
  );
258
223
 
259
224
  this.gl.activeTexture(this.gl.TEXTURE1);
@@ -262,12 +227,12 @@ export class WebGLRenderer extends IRenderer {
262
227
  this.gl.TEXTURE_2D,
263
228
  0,
264
229
  this.gl.LUMINANCE,
265
- width / 2,
266
- height / 2,
230
+ uStride!,
231
+ height! / 2,
267
232
  0,
268
233
  this.gl.LUMINANCE,
269
234
  this.gl.UNSIGNED_BYTE,
270
- uplane
235
+ uBuffer!
271
236
  );
272
237
 
273
238
  this.gl.activeTexture(this.gl.TEXTURE2);
@@ -276,34 +241,24 @@ export class WebGLRenderer extends IRenderer {
276
241
  this.gl.TEXTURE_2D,
277
242
  0,
278
243
  this.gl.LUMINANCE,
279
- width / 2,
280
- height / 2,
244
+ vStride!,
245
+ height! / 2,
281
246
  0,
282
247
  this.gl.LUMINANCE,
283
248
  this.gl.UNSIGNED_BYTE,
284
- vplane
249
+ vBuffer!
285
250
  );
251
+
252
+ this.gl.drawArrays(this.gl.TRIANGLES, 0, 6);
286
253
  }
287
254
 
288
- private updateCanvas(rotation: number, width: number, height: number) {
289
- // if (this.canvasUpdated) {
290
- // return;
291
- // }
292
- if (width || height) {
293
- this.lastImageWidth = width;
294
- this.lastImageHeight = height;
295
- this.lastImageRotation = rotation;
296
- } else {
297
- width = this.lastImageWidth;
298
- height = this.lastImageHeight;
299
- rotation = this.lastImageRotation;
300
- }
301
- if (!this.updateViewZoomLevel(rotation, width, height)) {
302
- return;
303
- }
304
- if (!this.gl) {
305
- return;
306
- }
255
+ protected override rotateCanvas({ width, height, rotation }: VideoFrame) {
256
+ super.rotateCanvas({ width, height, rotation });
257
+
258
+ if (!this.gl) return;
259
+
260
+ this.gl.viewport(0, 0, width!, height!);
261
+
307
262
  this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.surfaceBuffer);
308
263
  this.gl.enableVertexAttribArray(this.positionLocation!);
309
264
  this.gl.vertexAttribPointer(
@@ -321,9 +276,9 @@ export class WebGLRenderer extends IRenderer {
321
276
  // 180: 3,4,2/2,4,1
322
277
  // 270: 4,1,3/3,1,2
323
278
  const p1 = { x: 0, y: 0 };
324
- const p2 = { x: width, y: 0 };
325
- const p3 = { x: width, y: height };
326
- const p4 = { x: 0, y: height };
279
+ const p2 = { x: width!, y: 0 };
280
+ const p3 = { x: width!, y: height! };
281
+ const p4 = { x: 0, y: height! };
327
282
  let pp1 = p1,
328
283
  pp2 = p2,
329
284
  pp3 = p3,
@@ -375,140 +330,7 @@ export class WebGLRenderer extends IRenderer {
375
330
  this.program!,
376
331
  'u_resolution'
377
332
  );
378
- this.gl.uniform2f(resolutionLocation, width, height);
379
- }
380
-
381
- private updateViewZoomLevel(rotation: number, width: number, height: number) {
382
- if (!this.parentElement || !this.canvas) {
383
- return;
384
- }
385
- this.clientWidth = this.parentElement.clientWidth;
386
- this.clientHeight = this.parentElement.clientHeight;
387
-
388
- try {
389
- if (this.contentMode === RenderModeType.RenderModeHidden) {
390
- // Cover
391
- if (rotation === 0 || rotation === 180) {
392
- if (this.clientWidth / this.clientHeight > width / height) {
393
- this.canvas.style.transform = `scale(${this.clientWidth / width})`;
394
- } else {
395
- this.canvas.style.transform = `scale(${
396
- this.clientHeight / height
397
- })`;
398
- }
399
- } else {
400
- // 90, 270
401
- if (this.clientHeight / this.clientWidth > width / height) {
402
- this.canvas.style.transform = `scale(${this.clientHeight / width})`;
403
- } else {
404
- this.canvas.style.transform = `scale(${this.clientWidth / height})`;
405
- }
406
- }
407
- // Contain
408
- } else if (rotation === 0 || rotation === 180) {
409
- if (this.clientWidth / this.clientHeight > width / height) {
410
- this.canvas.style.transform = `scale(${this.clientHeight / height})`;
411
- } else {
412
- this.canvas.style.transform = `scale(${this.clientWidth / width})`;
413
- }
414
- } else {
415
- // 90, 270
416
- if (this.clientHeight / this.clientWidth > width / height) {
417
- this.canvas.style.transform = `scale(${this.clientWidth / height})`;
418
- } else {
419
- this.canvas.style.transform = `scale(${this.clientHeight / width})`;
420
- }
421
- }
422
- } catch (e) {
423
- logError('webgl updateViewZoomLevel', e);
424
- return false;
425
- }
426
-
427
- return true;
428
- }
429
-
430
- private initCanvas(
431
- view: HTMLElement,
432
- width: number,
433
- height: number,
434
- rotation: number
435
- ) {
436
- this.clientWidth = view.clientWidth;
437
- this.clientHeight = view.clientHeight;
438
-
439
- this.bind(view);
440
-
441
- if (this.canvas) {
442
- if (rotation == 0 || rotation == 180) {
443
- this.canvas.width = width;
444
- this.canvas.height = height;
445
- } else {
446
- this.canvas.width = height;
447
- this.canvas.height = width;
448
- }
449
- }
450
- this.initWidth = width;
451
- this.initHeight = height;
452
- this.initRotation = rotation;
453
-
454
- try {
455
- const getContext = (
456
- contextNames = ['webgl2', 'webgl', 'experimental-webgl']
457
- ): WebGLRenderingContext | WebGLRenderingContext | null => {
458
- for (let i = 0; i < contextNames.length; i++) {
459
- const contextName = contextNames[i]!;
460
- const context = this.canvas?.getContext(contextName, {
461
- depth: true,
462
- stencil: true,
463
- alpha: false,
464
- antialias: false,
465
- premultipliedAlpha: true,
466
- preserveDrawingBuffer: false,
467
- powerPreference: 'default',
468
- failIfMajorPerformanceCaveat: true,
469
- });
470
- if (context) {
471
- return context as WebGLRenderingContext | WebGLRenderingContext;
472
- }
473
- }
474
- return null;
475
- };
476
- // Try to grab the standard context. If it fails, fallback to experimental.
477
- this.gl ??= getContext();
478
- } catch (e) {
479
- logWarn('webgl create happen some warming', this.gl, this.canvas);
480
- }
481
-
482
- if (!this.gl) {
483
- this.fallback?.call(
484
- null,
485
- this,
486
- new Error('Browser not support! No WebGL detected.')
487
- );
488
- return;
489
- }
490
-
491
- // Set clear color to black, fully opaque
492
- this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
493
- // Enable depth testing
494
- this.gl.enable(this.gl.DEPTH_TEST);
495
- // Near things obscure far things
496
- this.gl.depthFunc(this.gl.LEQUAL);
497
- // Clear the color as well as the depth buffer.
498
- this.gl.clear(
499
- this.gl.COLOR_BUFFER_BIT |
500
- this.gl.DEPTH_BUFFER_BIT |
501
- this.gl.STENCIL_BUFFER_BIT
502
- );
503
-
504
- // Setup GLSL program
505
- this.program = createProgramFromSources(this.gl, [
506
- vertexShaderSource,
507
- yuvShaderSource,
508
- ]) as WebGLProgram;
509
- this.gl.useProgram(this.program);
510
-
511
- this.initTextures();
333
+ this.gl.uniform2f(resolutionLocation, width!, height!);
512
334
  }
513
335
 
514
336
  private initTextures() {
@@ -533,7 +355,7 @@ export class WebGLRenderer extends IRenderer {
533
355
  this.gl.activeTexture(textureIndex);
534
356
  const texture = this.gl.createTexture();
535
357
  this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
536
- // Set the parameters so we can render any size image.
358
+ // Set the parameters so we can render any size
537
359
  this.gl.texParameteri(
538
360
  this.gl.TEXTURE_2D,
539
361
  this.gl.TEXTURE_WRAP_S,
@@ -593,7 +415,7 @@ export class WebGLRenderer extends IRenderer {
593
415
 
594
416
  private handleContextLost = (event: Event) => {
595
417
  event.preventDefault();
596
- console.warn('webglcontextlost', event);
418
+ logWarn('webglcontextlost', event);
597
419
 
598
420
  this.releaseTextures();
599
421
 
@@ -606,7 +428,7 @@ export class WebGLRenderer extends IRenderer {
606
428
 
607
429
  private handleContextRestored = (event: Event) => {
608
430
  event.preventDefault();
609
- console.warn('webglcontextrestored', event);
431
+ logWarn('webglcontextrestored', event);
610
432
 
611
433
  // Setup GLSL program
612
434
  this.program = createProgramFromSources(this.gl, [