@havue/solutions 1.0.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.
Files changed (53) hide show
  1. package/README.md +0 -0
  2. package/bc-connect/README.md +3 -0
  3. package/bc-connect/dist/bc-connect.mjs +342 -0
  4. package/bc-connect/dist/bc-connect.umd.js +346 -0
  5. package/bc-connect/dist/types/src/index.d.ts +1 -0
  6. package/bc-connect/dist/types/src/manager.d.ts +135 -0
  7. package/bc-connect/package.json +38 -0
  8. package/bc-connect/src/index.ts +1 -0
  9. package/bc-connect/src/manager.ts +388 -0
  10. package/bc-connect/vite.config.ts +4 -0
  11. package/dist/solutions.full.js +8504 -0
  12. package/dist/solutions.full.min.js +18 -0
  13. package/dist/solutions.full.min.js.map +1 -0
  14. package/dist/solutions.mjs +2 -0
  15. package/dist/solutions.umd.js +18 -0
  16. package/dist/types/bc-connect/src/index.d.ts +1 -0
  17. package/dist/types/bc-connect/src/manager.d.ts +135 -0
  18. package/dist/types/src/index.d.ts +2 -0
  19. package/dist/types/ws-video-manager/src/hooks/useVideoPlay.d.ts +73 -0
  20. package/dist/types/ws-video-manager/src/index.d.ts +5 -0
  21. package/dist/types/ws-video-manager/src/loader/index.d.ts +2 -0
  22. package/dist/types/ws-video-manager/src/loader/websocket-loader.d.ts +62 -0
  23. package/dist/types/ws-video-manager/src/manager/index.d.ts +144 -0
  24. package/dist/types/ws-video-manager/src/render/drawer.d.ts +44 -0
  25. package/dist/types/ws-video-manager/src/render/index.d.ts +105 -0
  26. package/package.json +44 -0
  27. package/src/index.ts +2 -0
  28. package/vite.config.ts +4 -0
  29. package/ws-video-manager/README.md +3 -0
  30. package/ws-video-manager/dist/types/src/hooks/useVideoPlay.d.ts +73 -0
  31. package/ws-video-manager/dist/types/src/index.d.ts +5 -0
  32. package/ws-video-manager/dist/types/src/loader/index.d.ts +2 -0
  33. package/ws-video-manager/dist/types/src/loader/websocket-loader.d.ts +62 -0
  34. package/ws-video-manager/dist/types/src/manager/index.d.ts +144 -0
  35. package/ws-video-manager/dist/types/src/render/drawer.d.ts +44 -0
  36. package/ws-video-manager/dist/types/src/render/index.d.ts +105 -0
  37. package/ws-video-manager/dist/ws-video-manager.mjs +8132 -0
  38. package/ws-video-manager/dist/ws-video-manager.umd.js +8135 -0
  39. package/ws-video-manager/node_modules/.bin/tsc +17 -0
  40. package/ws-video-manager/node_modules/.bin/tsc.CMD +12 -0
  41. package/ws-video-manager/node_modules/.bin/tsc.ps1 +41 -0
  42. package/ws-video-manager/node_modules/.bin/tsserver +17 -0
  43. package/ws-video-manager/node_modules/.bin/tsserver.CMD +12 -0
  44. package/ws-video-manager/node_modules/.bin/tsserver.ps1 +41 -0
  45. package/ws-video-manager/package.json +41 -0
  46. package/ws-video-manager/src/hooks/useVideoPlay.ts +357 -0
  47. package/ws-video-manager/src/index.ts +6 -0
  48. package/ws-video-manager/src/loader/index.ts +3 -0
  49. package/ws-video-manager/src/loader/websocket-loader.ts +278 -0
  50. package/ws-video-manager/src/manager/index.ts +429 -0
  51. package/ws-video-manager/src/render/drawer.ts +255 -0
  52. package/ws-video-manager/src/render/index.ts +475 -0
  53. package/ws-video-manager/vite.config.ts +4 -0
@@ -0,0 +1,255 @@
1
+ /**
2
+ * 绘制视频到canvas中
3
+ */
4
+ class CanvasDrawer {
5
+ private _canvas: HTMLCanvasElement | null = null
6
+ private _useGl: boolean = false
7
+ private _ctx2d: CanvasRenderingContext2D | null = null
8
+ private _gl: WebGLRenderingContext | null = null
9
+ private _program: WebGLProgram | null = null
10
+ private _positionBuffer: WebGLBuffer | null = null
11
+ private _texCoordBuffer: WebGLBuffer | null = null
12
+ private _texture: WebGLTexture | null = null
13
+ private _positionLocation: number = -1
14
+ private _texCoordLocation: number = -1
15
+ private _textureLocation: WebGLUniformLocation | null = null
16
+ private _glReady: boolean = false
17
+
18
+ constructor(canvas: HTMLCanvasElement, useWebgl: boolean = false) {
19
+ this._canvas = canvas
20
+ this._useGl = useWebgl
21
+ if (useWebgl) {
22
+ this.initGl()
23
+ } else {
24
+ this.init2d()
25
+ }
26
+ }
27
+
28
+ private init2d() {
29
+ if (!this._canvas) return
30
+ this._ctx2d = this._canvas.getContext('2d')
31
+ if (!this._ctx2d) {
32
+ return
33
+ }
34
+ this._ctx2d.fillStyle = 'black'
35
+ this._ctx2d.fillRect(0, 0, this._canvas.width, this._canvas.height)
36
+ }
37
+
38
+ /**
39
+ * 初始化 webgl
40
+ */
41
+ private initGl() {
42
+ if (!this._canvas) return
43
+
44
+ // 初始化 gl
45
+ this._gl = this._canvas.getContext('webgl') || this._canvas.getContext('webgl2')
46
+ if (!this._gl) {
47
+ console.error('WebGL not supported')
48
+ this._useGl = false
49
+ this.init2d()
50
+ return
51
+ }
52
+
53
+ const gl = this._gl!
54
+
55
+ const vertexShader = this.createShader(gl, gl.VERTEX_SHADER, this.createShaderSource(gl, gl.VERTEX_SHADER))
56
+ const fragmentShader = this.createShader(gl, gl.FRAGMENT_SHADER, this.createShaderSource(gl, gl.FRAGMENT_SHADER))
57
+
58
+ if (!vertexShader || !fragmentShader) return
59
+
60
+ const program = this.createProgram(gl, vertexShader, fragmentShader)
61
+
62
+ if (!program) return
63
+
64
+ // 坐标缓冲
65
+ const positionBuffer = gl.createBuffer()
66
+ gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer)
67
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]), gl.STATIC_DRAW)
68
+
69
+ // 纹理坐标缓冲
70
+ const texCoordBuffer = gl.createBuffer()
71
+ gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer)
72
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1]), gl.STATIC_DRAW)
73
+
74
+ // 纹理
75
+ const texture = gl.createTexture()
76
+ gl.bindTexture(gl.TEXTURE_2D, texture)
77
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
78
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
79
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
80
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
81
+
82
+ // 着色器变量的引用
83
+ const positionLocation = gl.getAttribLocation(program, 'a_position')
84
+ const texCoordLocation = gl.getAttribLocation(program, 'a_texCoord')
85
+ const textureLocation = gl.getUniformLocation(program, 'u_texture')
86
+
87
+ // 缓存数据
88
+ this._program = program
89
+ this._positionBuffer = positionBuffer
90
+ this._texCoordBuffer = texCoordBuffer
91
+ this._texture = texture
92
+ this._positionLocation = positionLocation
93
+ this._texCoordLocation = texCoordLocation
94
+ this._textureLocation = textureLocation
95
+
96
+ // 设置标志
97
+ this._glReady = true
98
+ }
99
+
100
+ /**
101
+ * 创建着色器源码
102
+ */
103
+ private createShaderSource(gl: WebGLRenderingContext, type: number) {
104
+ // 顶点着色器
105
+ const vertexShaderSource = `
106
+ attribute vec2 a_position;
107
+ attribute vec2 a_texCoord;
108
+ varying vec2 v_texCoord;
109
+ void main() {
110
+ gl_Position = vec4(-a_position.x, -a_position.y, 0.0, 1.0);
111
+ v_texCoord = vec2(1.0 - a_texCoord.x, a_texCoord.y);
112
+ }
113
+ `
114
+
115
+ // 片段着色器
116
+ const fragmentShaderSource = `
117
+ precision mediump float;
118
+ varying vec2 v_texCoord;
119
+ uniform sampler2D u_texture;
120
+ void main() {
121
+ gl_FragColor = texture2D(u_texture, v_texCoord);
122
+ }
123
+ `
124
+
125
+ if (type === gl.VERTEX_SHADER) {
126
+ return vertexShaderSource
127
+ } else {
128
+ return fragmentShaderSource
129
+ }
130
+ }
131
+
132
+ /**
133
+ * 创建着色器
134
+ */
135
+ private createShader(gl: WebGLRenderingContext, type: number, source: string) {
136
+ const shader = gl.createShader(type)
137
+
138
+ if (!shader) {
139
+ return null
140
+ }
141
+
142
+ gl.shaderSource(shader, source)
143
+ gl.compileShader(shader)
144
+
145
+ const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS)
146
+ if (!success) {
147
+ gl.deleteShader(shader)
148
+ console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader))
149
+
150
+ return null
151
+ }
152
+
153
+ return shader
154
+ }
155
+
156
+ /**
157
+ * 创建着色器程序
158
+ */
159
+ private createProgram(gl: WebGLRenderingContext, vertexShader: WebGLShader, fragmentShader: WebGLShader) {
160
+ const program = gl.createProgram()
161
+
162
+ if (!program) {
163
+ return null
164
+ }
165
+
166
+ gl.attachShader(program, vertexShader)
167
+ gl.attachShader(program, fragmentShader)
168
+ gl.linkProgram(program)
169
+
170
+ const success = gl.getProgramParameter(program, gl.LINK_STATUS)
171
+ if (!success) {
172
+ console.error('An error occurred linking the program: ' + gl.getProgramInfoLog(program))
173
+ gl.deleteProgram(program)
174
+
175
+ return null
176
+ }
177
+
178
+ return program
179
+ }
180
+
181
+ /**
182
+ * 绘制
183
+ */
184
+ public draw(video: HTMLVideoElement) {
185
+ if (this._useGl) {
186
+ if (!this._glReady || !this._canvas) return
187
+ const gl = this._gl!
188
+ const program = this._program!
189
+ const positionBuffer = this._positionBuffer!
190
+ const texCoordBuffer = this._texCoordBuffer!
191
+ const texture = this._texture!
192
+ const positionLocation = this._positionLocation
193
+ const texCoordLocation = this._texCoordLocation
194
+ const textureLocation = this._textureLocation!
195
+
196
+ // 更新视口区域
197
+ gl.viewport(0, 0, this._canvas.width, this._canvas.height)
198
+
199
+ if (video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) return
200
+
201
+ // 更新纹理
202
+ gl.bindTexture(gl.TEXTURE_2D, texture)
203
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video)
204
+
205
+ // 清理画布
206
+ gl.clear(gl.COLOR_BUFFER_BIT)
207
+
208
+ // 使用着色器程序
209
+ gl.useProgram(program)
210
+
211
+ // 设置着色器变量对应的 buffer 数据
212
+ gl.enableVertexAttribArray(positionLocation)
213
+ gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer)
214
+ gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0)
215
+
216
+ gl.enableVertexAttribArray(texCoordLocation)
217
+ gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer)
218
+ gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0)
219
+
220
+ gl.uniform1i(textureLocation, 0)
221
+
222
+ // 绘制(两个三角形六个顶点, 组成一个矩形)
223
+ gl.drawArrays(gl.TRIANGLES, 0, 6)
224
+ } else {
225
+ const ctx = this._ctx2d
226
+ const canvas = this._canvas
227
+ if (!ctx || !canvas) {
228
+ return
229
+ }
230
+ ctx.fillStyle = 'black'
231
+ ctx.fillRect(0, 0, canvas.width, canvas.height)
232
+ ctx.drawImage(video, 0, 0, canvas.width, canvas.height)
233
+ }
234
+ }
235
+
236
+ /**
237
+ * 销毁
238
+ */
239
+ public destroy() {
240
+ this._canvas = null
241
+ this._useGl = false
242
+ this._ctx2d = null
243
+ this._gl = null
244
+ this._program = null
245
+ this._positionBuffer = null
246
+ this._texCoordBuffer = null
247
+ this._texture = null
248
+ this._positionLocation = -1
249
+ this._texCoordLocation = -1
250
+ this._textureLocation = null
251
+ this._glReady = false
252
+ }
253
+ }
254
+
255
+ export { CanvasDrawer }