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
@@ -16,7 +16,6 @@ var __extends = (this && this.__extends) || (function () {
16
16
  })();
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.WebGLRenderer = void 0;
19
- var AgoraMediaBase_1 = require("../../Private/AgoraMediaBase");
20
19
  var Utils_1 = require("../../Utils");
21
20
  var IRenderer_1 = require("../IRenderer");
22
21
  var createProgramFromSources = require('./webgl-utils').createProgramFromSources;
@@ -56,26 +55,17 @@ var WebGLRenderer = /** @class */ (function (_super) {
56
55
  __extends(WebGLRenderer, _super);
57
56
  function WebGLRenderer(fallback) {
58
57
  var _this = _super.call(this) || this;
59
- _this.initWidth = 0;
60
- _this.initHeight = 0;
61
- _this.initRotation = 0;
62
- _this.clientWidth = 0;
63
- _this.clientHeight = 0;
64
- _this.lastImageWidth = 0;
65
- _this.lastImageHeight = 0;
66
- _this.lastImageRotation = 0;
67
- _this.videoBuffer = {};
68
58
  _this.handleContextLost = function (event) {
69
59
  var _a;
70
60
  event.preventDefault();
71
- console.warn('webglcontextlost', event);
61
+ (0, Utils_1.logWarn)('webglcontextlost', event);
72
62
  _this.releaseTextures();
73
63
  (_a = _this.fallback) === null || _a === void 0 ? void 0 : _a.call(null, _this, new Error('Browser not support! No WebGL detected.'));
74
64
  };
75
65
  _this.handleContextRestored = function (event) {
76
66
  var _a;
77
67
  event.preventDefault();
78
- console.warn('webglcontextrestored', event);
68
+ (0, Utils_1.logWarn)('webglcontextrestored', event);
79
69
  // Setup GLSL program
80
70
  _this.program = createProgramFromSources(_this.gl, [
81
71
  vertexShaderSource,
@@ -94,130 +84,109 @@ var WebGLRenderer = /** @class */ (function (_super) {
94
84
  return _this;
95
85
  }
96
86
  WebGLRenderer.prototype.bind = function (view) {
97
- var _a, _b;
87
+ var _this = this;
88
+ var _a, _b, _c, _d;
98
89
  _super.prototype.bind.call(this, view);
99
- var ResizeObserver = window.ResizeObserver;
100
- if (ResizeObserver) {
101
- this.observer = new ResizeObserver(this.refreshCanvas);
102
- this.observer.observe(view);
103
- }
104
- // context list after toggle resolution on electron 12.0.6
105
90
  (_a = this.canvas) === null || _a === void 0 ? void 0 : _a.addEventListener('webglcontextlost', this.handleContextLost, false);
106
91
  (_b = this.canvas) === null || _b === void 0 ? void 0 : _b.addEventListener('webglcontextrestored', this.handleContextRestored, false);
92
+ var getContext = function (contextNames) {
93
+ var _a;
94
+ if (contextNames === void 0) { contextNames = ['webgl2', 'webgl', 'experimental-webgl']; }
95
+ for (var i = 0; i < contextNames.length; i++) {
96
+ var contextName = contextNames[i];
97
+ var context = (_a = _this.canvas) === null || _a === void 0 ? void 0 : _a.getContext(contextName, {
98
+ depth: true,
99
+ stencil: true,
100
+ alpha: false,
101
+ antialias: false,
102
+ premultipliedAlpha: true,
103
+ preserveDrawingBuffer: true,
104
+ powerPreference: 'default',
105
+ failIfMajorPerformanceCaveat: false,
106
+ });
107
+ if (context) {
108
+ return context;
109
+ }
110
+ }
111
+ return null;
112
+ };
113
+ (_c = this.gl) !== null && _c !== void 0 ? _c : (this.gl = getContext());
114
+ if (!this.gl) {
115
+ (_d = this.fallback) === null || _d === void 0 ? void 0 : _d.call(null, this, new Error('Browser not support! No WebGL detected.'));
116
+ return;
117
+ }
118
+ // Set clear color to black, fully opaque
119
+ this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
120
+ // Enable depth testing
121
+ this.gl.enable(this.gl.DEPTH_TEST);
122
+ // Near things obscure far things
123
+ this.gl.depthFunc(this.gl.LEQUAL);
124
+ // Clear the color as well as the depth buffer.
125
+ this.gl.clear(this.gl.COLOR_BUFFER_BIT |
126
+ this.gl.DEPTH_BUFFER_BIT |
127
+ this.gl.STENCIL_BUFFER_BIT);
128
+ // Setup GLSL program
129
+ this.program = createProgramFromSources(this.gl, [
130
+ vertexShaderSource,
131
+ yuvShaderSource,
132
+ ]);
133
+ this.gl.useProgram(this.program);
134
+ this.initTextures();
107
135
  };
108
136
  WebGLRenderer.prototype.unbind = function () {
109
- var _a, _b, _c, _d;
110
- if (this.parentElement) {
111
- (_a = this.observer) === null || _a === void 0 ? void 0 : _a.unobserve(this.parentElement);
112
- }
113
- (_b = this.observer) === null || _b === void 0 ? void 0 : _b.disconnect();
114
- this.observer = undefined;
115
- (_c = this.canvas) === null || _c === void 0 ? void 0 : _c.removeEventListener('webglcontextlost', this.handleContextLost, false);
116
- (_d = this.canvas) === null || _d === void 0 ? void 0 : _d.removeEventListener('webglcontextrestored', this.handleContextRestored, false);
137
+ var _a, _b;
138
+ (_a = this.canvas) === null || _a === void 0 ? void 0 : _a.removeEventListener('webglcontextlost', this.handleContextLost, false);
139
+ (_b = this.canvas) === null || _b === void 0 ? void 0 : _b.removeEventListener('webglcontextrestored', this.handleContextRestored, false);
117
140
  this.releaseTextures();
118
141
  this.gl = undefined;
119
142
  _super.prototype.unbind.call(this);
120
143
  };
121
- WebGLRenderer.prototype.drawFrame = function (videoFrame) {
122
- var _a;
123
- var error;
124
- try {
125
- this.renderImage({
126
- width: videoFrame.width,
127
- height: videoFrame.height,
128
- left: 0,
129
- top: 0,
130
- right: videoFrame.yStride - videoFrame.width,
131
- bottom: 0,
132
- rotation: videoFrame.rotation || 0,
133
- yplane: videoFrame.yBuffer,
134
- uplane: videoFrame.uBuffer,
135
- vplane: videoFrame.vBuffer,
136
- });
137
- }
138
- catch (err) {
139
- error = err;
140
- }
141
- if (!this.gl || error) {
142
- (_a = this.fallback) === null || _a === void 0 ? void 0 : _a.call(null, this, new Error('webgl lost or webgl initialize failed'));
143
- return;
144
- }
145
- };
146
- WebGLRenderer.prototype.refreshCanvas = function () {
147
- if (this.lastImageWidth) {
148
- this.updateViewZoomLevel(this.lastImageRotation, this.lastImageWidth, this.lastImageHeight);
149
- }
150
- };
151
- WebGLRenderer.prototype.renderImage = function (image) {
152
- // Rotation, width, height, left, top, right, bottom, yplane, uplane, vplane
153
- if (image.width != this.initWidth ||
154
- image.height != this.initHeight ||
155
- image.rotation != this.initRotation) {
156
- var view = this.parentElement;
157
- this.unbind();
158
- this.initCanvas(view, image.width, image.height, image.rotation);
159
- }
160
- if (!this.gl || !this.program) {
144
+ WebGLRenderer.prototype.drawFrame = function (_a) {
145
+ var width = _a.width, height = _a.height, yStride = _a.yStride, uStride = _a.uStride, vStride = _a.vStride, yBuffer = _a.yBuffer, uBuffer = _a.uBuffer, vBuffer = _a.vBuffer, rotation = _a.rotation;
146
+ this.rotateCanvas({ width: width, height: height, rotation: rotation });
147
+ this.updateRenderMode();
148
+ if (!this.gl || !this.program)
161
149
  return;
162
- }
150
+ var left = 0, top = 0, right = yStride - width, bottom = 0;
163
151
  this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.texCoordBuffer);
164
- var xWidth = image.width + image.left + image.right;
165
- var xHeight = image.height + image.top + image.bottom;
152
+ var xWidth = width + left + right;
153
+ var xHeight = height + top + bottom;
166
154
  this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array([
167
- image.left / xWidth,
168
- image.bottom / xHeight,
169
- 1 - image.right / xWidth,
170
- image.bottom / xHeight,
171
- image.left / xWidth,
172
- 1 - image.top / xHeight,
173
- image.left / xWidth,
174
- 1 - image.top / xHeight,
175
- 1 - image.right / xWidth,
176
- image.bottom / xHeight,
177
- 1 - image.right / xWidth,
178
- 1 - image.top / xHeight,
155
+ left / xWidth,
156
+ bottom / xHeight,
157
+ 1 - right / xWidth,
158
+ bottom / xHeight,
159
+ left / xWidth,
160
+ 1 - top / xHeight,
161
+ left / xWidth,
162
+ 1 - top / xHeight,
163
+ 1 - right / xWidth,
164
+ bottom / xHeight,
165
+ 1 - right / xWidth,
166
+ 1 - top / xHeight,
179
167
  ]), this.gl.STATIC_DRAW);
180
168
  this.gl.enableVertexAttribArray(this.texCoordLocation);
181
169
  this.gl.vertexAttribPointer(this.texCoordLocation, 2, this.gl.FLOAT, false, 0, 0);
182
- this.uploadYuv(xWidth, xHeight, image.yplane, image.uplane, image.vplane);
183
- this.updateCanvas(image.rotation, image.width, image.height);
184
- this.gl.drawArrays(this.gl.TRIANGLES, 0, 6);
185
- };
186
- WebGLRenderer.prototype.uploadYuv = function (width, height, yplane, uplane, vplane) {
187
- if (!this.gl || !this.yTexture || !this.uTexture || !this.vTexture) {
188
- return;
189
- }
190
170
  this.gl.pixelStorei(this.gl.UNPACK_ALIGNMENT, 1);
191
171
  this.gl.activeTexture(this.gl.TEXTURE0);
192
172
  this.gl.bindTexture(this.gl.TEXTURE_2D, this.yTexture);
193
- this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.LUMINANCE, width, height, 0, this.gl.LUMINANCE, this.gl.UNSIGNED_BYTE, yplane);
173
+ this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.LUMINANCE,
174
+ // Should use xWidth instead of width here (yStide)
175
+ xWidth, height, 0, this.gl.LUMINANCE, this.gl.UNSIGNED_BYTE, yBuffer);
194
176
  this.gl.activeTexture(this.gl.TEXTURE1);
195
177
  this.gl.bindTexture(this.gl.TEXTURE_2D, this.uTexture);
196
- this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.LUMINANCE, width / 2, height / 2, 0, this.gl.LUMINANCE, this.gl.UNSIGNED_BYTE, uplane);
178
+ this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.LUMINANCE, uStride, height / 2, 0, this.gl.LUMINANCE, this.gl.UNSIGNED_BYTE, uBuffer);
197
179
  this.gl.activeTexture(this.gl.TEXTURE2);
198
180
  this.gl.bindTexture(this.gl.TEXTURE_2D, this.vTexture);
199
- this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.LUMINANCE, width / 2, height / 2, 0, this.gl.LUMINANCE, this.gl.UNSIGNED_BYTE, vplane);
181
+ this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.LUMINANCE, vStride, height / 2, 0, this.gl.LUMINANCE, this.gl.UNSIGNED_BYTE, vBuffer);
182
+ this.gl.drawArrays(this.gl.TRIANGLES, 0, 6);
200
183
  };
201
- WebGLRenderer.prototype.updateCanvas = function (rotation, width, height) {
202
- // if (this.canvasUpdated) {
203
- // return;
204
- // }
205
- if (width || height) {
206
- this.lastImageWidth = width;
207
- this.lastImageHeight = height;
208
- this.lastImageRotation = rotation;
209
- }
210
- else {
211
- width = this.lastImageWidth;
212
- height = this.lastImageHeight;
213
- rotation = this.lastImageRotation;
214
- }
215
- if (!this.updateViewZoomLevel(rotation, width, height)) {
216
- return;
217
- }
218
- if (!this.gl) {
184
+ WebGLRenderer.prototype.rotateCanvas = function (_a) {
185
+ var width = _a.width, height = _a.height, rotation = _a.rotation;
186
+ _super.prototype.rotateCanvas.call(this, { width: width, height: height, rotation: rotation });
187
+ if (!this.gl)
219
188
  return;
220
- }
189
+ this.gl.viewport(0, 0, width, height);
221
190
  this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.surfaceBuffer);
222
191
  this.gl.enableVertexAttribArray(this.positionLocation);
223
192
  this.gl.vertexAttribPointer(this.positionLocation, 2, this.gl.FLOAT, false, 0, 0);
@@ -271,127 +240,6 @@ var WebGLRenderer = /** @class */ (function (_super) {
271
240
  var resolutionLocation = this.gl.getUniformLocation(this.program, 'u_resolution');
272
241
  this.gl.uniform2f(resolutionLocation, width, height);
273
242
  };
274
- WebGLRenderer.prototype.updateViewZoomLevel = function (rotation, width, height) {
275
- if (!this.parentElement || !this.canvas) {
276
- return;
277
- }
278
- this.clientWidth = this.parentElement.clientWidth;
279
- this.clientHeight = this.parentElement.clientHeight;
280
- try {
281
- if (this.contentMode === AgoraMediaBase_1.RenderModeType.RenderModeHidden) {
282
- // Cover
283
- if (rotation === 0 || rotation === 180) {
284
- if (this.clientWidth / this.clientHeight > width / height) {
285
- this.canvas.style.transform = "scale(".concat(this.clientWidth / width, ")");
286
- }
287
- else {
288
- this.canvas.style.transform = "scale(".concat(this.clientHeight / height, ")");
289
- }
290
- }
291
- else {
292
- // 90, 270
293
- if (this.clientHeight / this.clientWidth > width / height) {
294
- this.canvas.style.transform = "scale(".concat(this.clientHeight / width, ")");
295
- }
296
- else {
297
- this.canvas.style.transform = "scale(".concat(this.clientWidth / height, ")");
298
- }
299
- }
300
- // Contain
301
- }
302
- else if (rotation === 0 || rotation === 180) {
303
- if (this.clientWidth / this.clientHeight > width / height) {
304
- this.canvas.style.transform = "scale(".concat(this.clientHeight / height, ")");
305
- }
306
- else {
307
- this.canvas.style.transform = "scale(".concat(this.clientWidth / width, ")");
308
- }
309
- }
310
- else {
311
- // 90, 270
312
- if (this.clientHeight / this.clientWidth > width / height) {
313
- this.canvas.style.transform = "scale(".concat(this.clientWidth / height, ")");
314
- }
315
- else {
316
- this.canvas.style.transform = "scale(".concat(this.clientHeight / width, ")");
317
- }
318
- }
319
- }
320
- catch (e) {
321
- (0, Utils_1.logError)('webgl updateViewZoomLevel', e);
322
- return false;
323
- }
324
- return true;
325
- };
326
- WebGLRenderer.prototype.initCanvas = function (view, width, height, rotation) {
327
- var _this = this;
328
- var _a, _b;
329
- this.clientWidth = view.clientWidth;
330
- this.clientHeight = view.clientHeight;
331
- this.bind(view);
332
- if (this.canvas) {
333
- if (rotation == 0 || rotation == 180) {
334
- this.canvas.width = width;
335
- this.canvas.height = height;
336
- }
337
- else {
338
- this.canvas.width = height;
339
- this.canvas.height = width;
340
- }
341
- }
342
- this.initWidth = width;
343
- this.initHeight = height;
344
- this.initRotation = rotation;
345
- try {
346
- var getContext = function (contextNames) {
347
- var _a;
348
- if (contextNames === void 0) { contextNames = ['webgl2', 'webgl', 'experimental-webgl']; }
349
- for (var i = 0; i < contextNames.length; i++) {
350
- var contextName = contextNames[i];
351
- var context = (_a = _this.canvas) === null || _a === void 0 ? void 0 : _a.getContext(contextName, {
352
- depth: true,
353
- stencil: true,
354
- alpha: false,
355
- antialias: false,
356
- premultipliedAlpha: true,
357
- preserveDrawingBuffer: false,
358
- powerPreference: 'default',
359
- failIfMajorPerformanceCaveat: true,
360
- });
361
- if (context) {
362
- return context;
363
- }
364
- }
365
- return null;
366
- };
367
- // Try to grab the standard context. If it fails, fallback to experimental.
368
- (_a = this.gl) !== null && _a !== void 0 ? _a : (this.gl = getContext());
369
- }
370
- catch (e) {
371
- (0, Utils_1.logWarn)('webgl create happen some warming', this.gl, this.canvas);
372
- }
373
- if (!this.gl) {
374
- (_b = this.fallback) === null || _b === void 0 ? void 0 : _b.call(null, this, new Error('Browser not support! No WebGL detected.'));
375
- return;
376
- }
377
- // Set clear color to black, fully opaque
378
- this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
379
- // Enable depth testing
380
- this.gl.enable(this.gl.DEPTH_TEST);
381
- // Near things obscure far things
382
- this.gl.depthFunc(this.gl.LEQUAL);
383
- // Clear the color as well as the depth buffer.
384
- this.gl.clear(this.gl.COLOR_BUFFER_BIT |
385
- this.gl.DEPTH_BUFFER_BIT |
386
- this.gl.STENCIL_BUFFER_BIT);
387
- // Setup GLSL program
388
- this.program = createProgramFromSources(this.gl, [
389
- vertexShaderSource,
390
- yuvShaderSource,
391
- ]);
392
- this.gl.useProgram(this.program);
393
- this.initTextures();
394
- };
395
243
  WebGLRenderer.prototype.initTextures = function () {
396
244
  var _this = this;
397
245
  if (!this.gl)
@@ -407,7 +255,7 @@ var WebGLRenderer = /** @class */ (function (_super) {
407
255
  _this.gl.activeTexture(textureIndex);
408
256
  var texture = _this.gl.createTexture();
409
257
  _this.gl.bindTexture(_this.gl.TEXTURE_2D, texture);
410
- // Set the parameters so we can render any size image.
258
+ // Set the parameters so we can render any size
411
259
  _this.gl.texParameteri(_this.gl.TEXTURE_2D, _this.gl.TEXTURE_WRAP_S, _this.gl.CLAMP_TO_EDGE);
412
260
  _this.gl.texParameteri(_this.gl.TEXTURE_2D, _this.gl.TEXTURE_WRAP_T, _this.gl.CLAMP_TO_EDGE);
413
261
  _this.gl.texParameteri(_this.gl.TEXTURE_2D, _this.gl.TEXTURE_MIN_FILTER, _this.gl.NEAREST);
@@ -14,171 +14,51 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
- var __importDefault = (this && this.__importDefault) || function (mod) {
18
- return (mod && mod.__esModule) ? mod : { "default": mod };
19
- };
20
17
  Object.defineProperty(exports, "__esModule", { value: true });
21
18
  exports.YUVCanvasRenderer = void 0;
22
- var lodash_isequal_1 = __importDefault(require("lodash.isequal"));
23
- var AgoraMediaBase_1 = require("../../Private/AgoraMediaBase");
24
19
  var IRenderer_1 = require("../IRenderer");
25
20
  var YUVBuffer = require('yuv-buffer');
26
21
  var YUVCanvas = require('yuv-canvas');
27
22
  var YUVCanvasRenderer = /** @class */ (function (_super) {
28
23
  __extends(YUVCanvasRenderer, _super);
29
24
  function YUVCanvasRenderer() {
30
- var _this = _super.call(this) || this;
31
- _this._videoFrame = {
32
- rotation: 0,
33
- width: 0,
34
- height: 0,
35
- yStride: 0,
36
- yBuffer: new Uint8Array(0),
37
- uBuffer: new Uint8Array(0),
38
- vBuffer: new Uint8Array(0),
39
- };
40
- return _this;
25
+ return _super !== null && _super.apply(this, arguments) || this;
41
26
  }
42
27
  YUVCanvasRenderer.prototype.bind = function (element) {
43
28
  _super.prototype.bind.call(this, element);
44
- this._yuvCanvasSink = YUVCanvas.attach(this.canvas, {
29
+ this.frameSink = YUVCanvas.attach(this.canvas, {
45
30
  webGL: false,
46
31
  });
47
32
  };
48
- YUVCanvasRenderer.prototype.unbind = function () {
49
- var _a, _b;
50
- if (this._yuvCanvasSink && ((_a = this._yuvCanvasSink) === null || _a === void 0 ? void 0 : _a.loseContext)) {
51
- (_b = this._yuvCanvasSink) === null || _b === void 0 ? void 0 : _b.loseContext();
52
- }
53
- _super.prototype.unbind.call(this);
54
- };
55
- YUVCanvasRenderer.prototype.drawFrame = function (frame) {
56
- if (!this.container || !this._yuvCanvasSink) {
33
+ YUVCanvasRenderer.prototype.drawFrame = function (_a) {
34
+ var width = _a.width, height = _a.height, yStride = _a.yStride, uStride = _a.uStride, vStride = _a.vStride, yBuffer = _a.yBuffer, uBuffer = _a.uBuffer, vBuffer = _a.vBuffer, rotation = _a.rotation;
35
+ this.rotateCanvas({ width: width, height: height, rotation: rotation });
36
+ this.updateRenderMode();
37
+ if (!this.frameSink)
57
38
  return;
58
- }
59
- var frameWidth = frame.width;
60
- var frameHeight = frame.height;
61
- if (this._videoFrame.yStride === 0 ||
62
- this._videoFrame.height === 0 ||
63
- this._videoFrame.yStride != frame.yStride ||
64
- this._videoFrame.height != frame.height) {
65
- this._videoFrame.yBuffer = new Uint8Array(frame.yStride * frameHeight);
66
- this._videoFrame.uBuffer = new Uint8Array((frame.yStride * frameHeight) / 4);
67
- this._videoFrame.vBuffer = new Uint8Array((frame.yStride * frameHeight) / 4);
68
- }
69
- this._videoFrame.yBuffer.set(frame.yBuffer);
70
- this._videoFrame.uBuffer.set(frame.uBuffer);
71
- this._videoFrame.vBuffer.set(frame.vBuffer);
72
- this._videoFrame.width = frame.width;
73
- this._videoFrame.height = frame.height;
74
- this._videoFrame.yStride = frame.yStride;
75
- this._videoFrame.rotation = frame.rotation;
76
- var options = {
77
- frameWidth: frameWidth,
78
- frameHeight: frameHeight,
79
- rotation: frame.rotation ? frame.rotation : 0,
80
- contentMode: this.contentMode,
81
- clientWidth: this.container.clientWidth,
82
- clientHeight: this.container.clientHeight,
83
- };
84
- this.updateCanvas(options);
85
- var format = YUVBuffer.format({
86
- width: frameWidth,
87
- height: frameHeight,
88
- chromaWidth: frameWidth / 2,
89
- chromaHeight: frameHeight / 2,
90
- cropLeft: frame.yStride - frameWidth,
91
- });
92
- var yuvBufferFrame = YUVBuffer.frame(format, {
93
- bytes: this._videoFrame.yBuffer,
94
- stride: frame.yStride,
39
+ this.frameSink.drawFrame(YUVBuffer.frame(YUVBuffer.format({
40
+ width: width,
41
+ height: height,
42
+ chromaWidth: width / 2,
43
+ chromaHeight: height / 2,
44
+ cropLeft: yStride - width,
45
+ }), {
46
+ bytes: yBuffer,
47
+ stride: yStride,
95
48
  }, {
96
- bytes: this._videoFrame.uBuffer,
97
- stride: frame.yStride / 2,
49
+ bytes: uBuffer,
50
+ stride: uStride,
98
51
  }, {
99
- bytes: this._videoFrame.vBuffer,
100
- stride: frame.yStride / 2,
101
- });
102
- this._yuvCanvasSink.drawFrame(yuvBufferFrame);
52
+ bytes: vBuffer,
53
+ stride: vStride,
54
+ }));
103
55
  };
104
- YUVCanvasRenderer.prototype.refreshCanvas = function () {
105
- if (this._cacheCanvasOptions) {
106
- this.zoom(this._cacheCanvasOptions.rotation === 90 ||
107
- this._cacheCanvasOptions.rotation === 270, this._cacheCanvasOptions.contentMode, this._cacheCanvasOptions.frameWidth, this._cacheCanvasOptions.frameHeight, this._cacheCanvasOptions.clientWidth, this._cacheCanvasOptions.clientHeight);
108
- }
109
- };
110
- YUVCanvasRenderer.prototype.updateCanvas = function (options) {
111
- if (options === void 0) { options = {
112
- frameWidth: 0,
113
- frameHeight: 0,
114
- rotation: 0,
115
- contentMode: AgoraMediaBase_1.RenderModeType.RenderModeHidden,
116
- clientWidth: 0,
117
- clientHeight: 0,
118
- }; }
119
- if (this._cacheCanvasOptions) {
120
- if ((0, lodash_isequal_1.default)(this._cacheCanvasOptions, options)) {
121
- return;
122
- }
123
- }
124
- this._cacheCanvasOptions = Object.assign({}, options);
125
- if (this.canvas) {
126
- if (options.rotation === 0 || options.rotation === 180) {
127
- this.canvas.width = options.frameWidth;
128
- this.canvas.height = options.frameHeight;
129
- Object.assign(this.canvas.style, {
130
- 'width': options.frameWidth + 'px',
131
- 'height': options.frameHeight + 'px',
132
- 'object-fit': 'cover',
133
- });
134
- }
135
- else if (options.rotation === 90 || options.rotation === 270) {
136
- this.canvas.height = options.frameWidth;
137
- this.canvas.width = options.frameHeight;
138
- }
139
- else {
140
- throw new Error('Invalid value for rotation. Only support 0, 90, 180, 270');
141
- }
142
- var transformItems = [];
143
- transformItems.push("rotateZ(".concat(options.rotation, "deg)"));
144
- var scale = this.zoom(options.rotation === 90 || options.rotation === 270, options.contentMode, options.frameWidth, options.frameHeight, options.clientWidth, options.clientHeight);
145
- this.canvas.style.transform = "scale(".concat(scale.toString(), ")");
146
- if (transformItems.length > 0) {
147
- this.canvas.style.transform += " ".concat(transformItems.join(' '));
148
- }
149
- }
150
- };
151
- YUVCanvasRenderer.prototype.zoom = function (vertical, contentMode, width, height, clientWidth, clientHeight) {
152
- if (contentMode === void 0) { contentMode = AgoraMediaBase_1.RenderModeType.RenderModeFit; }
153
- var localRatio = clientWidth / clientHeight;
154
- var tempRatio = width / height;
155
- if (isNaN(localRatio) || isNaN(tempRatio)) {
156
- return 1;
157
- }
158
- if (contentMode === AgoraMediaBase_1.RenderModeType.RenderModeHidden) {
159
- if (vertical) {
160
- return clientHeight / clientWidth < width / height
161
- ? clientWidth / height
162
- : clientHeight / width;
163
- }
164
- else {
165
- return clientWidth / clientHeight > width / height
166
- ? clientWidth / width
167
- : clientHeight / height;
168
- }
169
- }
170
- else {
171
- if (vertical) {
172
- return clientHeight / clientWidth < width / height
173
- ? clientHeight / width
174
- : clientWidth / height;
175
- }
176
- else {
177
- return clientWidth / clientHeight > width / height
178
- ? clientHeight / height
179
- : clientWidth / width;
180
- }
181
- }
56
+ YUVCanvasRenderer.prototype.rotateCanvas = function (_a) {
57
+ var width = _a.width, height = _a.height, rotation = _a.rotation;
58
+ _super.prototype.rotateCanvas.call(this, { width: width, height: height, rotation: rotation });
59
+ if (!this.canvas)
60
+ return;
61
+ this.canvas.style.transform += " rotateZ(".concat(rotation, "deg)");
182
62
  };
183
63
  return YUVCanvasRenderer;
184
64
  }(IRenderer_1.IRenderer));
package/js/Types.js CHANGED
@@ -1,17 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RENDER_MODE = void 0;
3
+ exports.RendererType = void 0;
4
4
  /**
5
5
  * @ignore
6
6
  */
7
- var RENDER_MODE;
8
- (function (RENDER_MODE) {
7
+ var RendererType;
8
+ (function (RendererType) {
9
9
  /**
10
10
  * @ignore
11
11
  */
12
- RENDER_MODE[RENDER_MODE["WEBGL"] = 1] = "WEBGL";
12
+ RendererType[RendererType["WEBGL"] = 1] = "WEBGL";
13
13
  /**
14
14
  * @ignore
15
15
  */
16
- RENDER_MODE[RENDER_MODE["SOFTWARE"] = 2] = "SOFTWARE";
17
- })(RENDER_MODE = exports.RENDER_MODE || (exports.RENDER_MODE = {}));
16
+ RendererType[RendererType["SOFTWARE"] = 2] = "SOFTWARE";
17
+ })(RendererType = exports.RendererType || (exports.RendererType = {}));