@stream-io/video-filters-web 0.1.1 → 0.1.3

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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
5
+ ### [0.1.3](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-filters-web-0.1.2...@stream-io/video-filters-web-0.1.3) (2024-07-02)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * refactor background filters ([#1415](https://github.com/GetStream/stream-video-js/issues/1415)) ([deb6da2](https://github.com/GetStream/stream-video-js/commit/deb6da238f541c733451e84b198434671da8dceb))
11
+
12
+ ### [0.1.2](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-filters-web-0.1.1...@stream-io/video-filters-web-0.1.2) (2024-06-12)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * infinitely adjustable blur filter ([#1399](https://github.com/GetStream/stream-video-js/issues/1399)) ([447e73f](https://github.com/GetStream/stream-video-js/commit/447e73f2363142a0c1b43d05f848400950ecf697))
18
+
5
19
  ### [0.1.1](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-filters-web-0.1.0...@stream-io/video-filters-web-0.1.1) (2024-06-03)
6
20
 
7
21
 
package/dist/index.cjs.js CHANGED
@@ -133,11 +133,21 @@ function buildBackgroundBlurStage(gl, vertexShader, positionBuffer, texCoordBuff
133
133
  };
134
134
  }
135
135
  function buildBlurPass(gl, vertexShader, positionBuffer, texCoordBuffer, personMaskTexture, canvas, blurLevel) {
136
- const weights = blurLevel === 'low'
137
- ? [0.227027027, 0.1545945946, 0.1016216216, 0.0340540541, 0.0142162162]
138
- : blurLevel === 'medium'
139
- ? [0.327027027, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162]
140
- : [0.627027027, 0.3445945946, 0.2216216216, 0.0540540541, 0.0162162162];
136
+ const sigma = typeof blurLevel === 'number'
137
+ ? blurLevel
138
+ : blurLevel === 'low'
139
+ ? 2
140
+ : blurLevel === 'medium'
141
+ ? 4
142
+ : 6;
143
+ const windowSize = Math.max(1, Math.floor(sigma * 3));
144
+ const offset = new Array(windowSize).fill(0).map((v, index) => index);
145
+ const variance = sigma ** 2;
146
+ const weights = offset.map((x) => {
147
+ var m = sigma * Math.sqrt(2 * Math.PI);
148
+ var e = Math.exp(-(x ** 2) / (2 * variance));
149
+ return e / m;
150
+ });
141
151
  const fragmentShaderSource = glsl `#version 300 es
142
152
 
143
153
  precision highp float;
@@ -149,10 +159,8 @@ function buildBlurPass(gl, vertexShader, positionBuffer, texCoordBuffer, personM
149
159
  in vec2 v_texCoord;
150
160
  out vec4 outColor;
151
161
 
152
- const float offset[5] = float[](0.0, 1.0, 2.0, 3.0, 4.0);
153
- const float weight[5] = float[](
154
- ${weights.join(',')}
155
- );
162
+ const float offset[${windowSize}] = float[](${offset.map((i) => i.toFixed(10)).join(', ')});
163
+ const float weight[${windowSize}] = float[](${weights.map((i) => i.toFixed(10)).join(', ')});
156
164
 
157
165
  void main() {
158
166
  vec4 centerColor = texture(u_inputFrame, v_texCoord);
@@ -160,7 +168,7 @@ function buildBlurPass(gl, vertexShader, positionBuffer, texCoordBuffer, personM
160
168
 
161
169
  vec4 frameColor = centerColor * weight[0] * (1.0 - personMask);
162
170
 
163
- for (int i = 1; i < 5; i++) {
171
+ for (int i = 1; i < ${windowSize}; i++) {
164
172
  vec2 offset = vec2(offset[i]) * u_texelSize;
165
173
 
166
174
  vec2 texCoord = v_texCoord + offset;
@@ -667,6 +675,8 @@ function buildWebGL2Pipeline(videoSource, backgroundImage, blurLevel, background
667
675
  const gl = canvas.getContext('webgl2');
668
676
  if (!gl)
669
677
  throw new Error('WebGL2 is not supported');
678
+ if (gl.isContextLost())
679
+ throw new Error('WebGL2 context was lost');
670
680
  const { width: frameWidth, height: frameHeight } = videoSource;
671
681
  const { width: segmentationWidth, height: segmentationHeight } = segmentationConfig;
672
682
  const vertexShaderSource = glsl `#version 300 es
@@ -769,16 +779,21 @@ const getSegmentationParams = (level) => {
769
779
  return { width: 160, height: 96 };
770
780
  };
771
781
 
772
- function createRenderer(tflite, videoSource, targetCanvas, options) {
782
+ function createRenderer(tflite, videoSource, targetCanvas, options, onError) {
773
783
  const { backgroundFilter, backgroundImage, backgroundBlurLevel, segmentationLevel = exports.SegmentationLevel.HIGH, fps = 30, } = options;
774
784
  if (backgroundFilter === 'image' && !backgroundImage) {
775
785
  throw new Error(`backgroundImage element is required when backgroundFilter is image`);
776
786
  }
777
787
  const pipeline = buildWebGL2Pipeline(videoSource, backgroundImage, backgroundBlurLevel, backgroundFilter, targetCanvas, tflite, getSegmentationParams(segmentationLevel));
778
788
  const id = setInterval(() => {
779
- pipeline.render();
780
- if (backgroundFilter === 'image') {
781
- pipeline.updatePostProcessingConfig();
789
+ try {
790
+ pipeline.render();
791
+ if (backgroundFilter === 'image') {
792
+ pipeline.updatePostProcessingConfig();
793
+ }
794
+ }
795
+ catch (error) {
796
+ onError?.(error);
782
797
  }
783
798
  }, 1000 / (fps <= 0 ? 30 : fps));
784
799
  return {
@@ -1523,7 +1538,7 @@ const createTFLiteSIMDModule = (__Module) => {
1523
1538
  return __Module.ready;
1524
1539
  };
1525
1540
 
1526
- const version = "0.1.1" ;
1541
+ const version = "0.1.3" ;
1527
1542
  const packageName = "@stream-io/video-filters-web" ;
1528
1543
 
1529
1544
  // @ts-expect-error - module is not declared