@stream-io/video-filters-web 0.7.1 → 0.7.2

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.
@@ -29,7 +29,7 @@ export declare abstract class BaseVideoProcessor {
29
29
  protected constructor(track: MediaStreamVideoTrack, hooks?: VideoTrackProcessorHooks);
30
30
  start(): Promise<MediaStreamTrack>;
31
31
  stop(): void;
32
- private updateStats;
32
+ protected updateStats(delay: number): void;
33
33
  protected abstract initialize(): Promise<void>;
34
34
  protected abstract transform(frame: VideoFrame): Promise<VideoFrame>;
35
35
  protected onFlush(): void;
@@ -14,7 +14,6 @@ export declare class VirtualBackground extends BaseVideoProcessor {
14
14
  private latestCategoryMask;
15
15
  private latestConfidenceMask;
16
16
  private lastFrameTime;
17
- private count;
18
17
  constructor(track: MediaStreamVideoTrack, options?: BackgroundOptions, hooks?: VideoTrackProcessorHooks);
19
18
  protected initialize(): Promise<void>;
20
19
  private initializeSegmenter;
@@ -39,7 +39,7 @@ export interface VideoTrackProcessorHooks {
39
39
  onError?: (error: unknown) => void;
40
40
  onStats?: (stats: PerformanceStats) => void;
41
41
  }
42
- export declare const BACKGROUND_BLUR_MAP: Record<BackgroundBlurLevel, {
43
- bgBlur: number;
44
- bgBlurRadius: number;
45
- }>;
42
+ /**
43
+ * Maps blur level to blur strength values.
44
+ */
45
+ export declare const BACKGROUND_BLUR_MAP: Record<'low' | 'medium' | 'high', number>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stream-io/video-filters-web",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "main": "./dist/index.cjs.js",
5
5
  "module": "./dist/index.es.js",
6
6
  "types": "./dist/index.d.ts",
@@ -64,11 +64,8 @@ export abstract class BaseVideoProcessor {
64
64
  this.canvas.height = frame.displayHeight;
65
65
  }
66
66
 
67
- const start = performance.now();
68
67
  const processed = await this.transform(frame);
69
- const delay = performance.now() - start;
70
68
 
71
- this.updateStats(delay);
72
69
  controller.enqueue(processed);
73
70
  } catch (e) {
74
71
  this.hooks.onError?.(e);
@@ -98,7 +95,7 @@ export abstract class BaseVideoProcessor {
98
95
  this.onStop();
99
96
  }
100
97
 
101
- private updateStats(delay: number): void {
98
+ protected updateStats(delay: number): void {
102
99
  this.frames++;
103
100
  this.delayTotal += delay;
104
101
 
@@ -24,7 +24,6 @@ export class VirtualBackground extends BaseVideoProcessor {
24
24
  private latestCategoryMask: WebGLTexture | undefined = undefined;
25
25
  private latestConfidenceMask: WebGLTexture | undefined = undefined;
26
26
  private lastFrameTime = -1;
27
- private count = 0;
28
27
 
29
28
  constructor(
30
29
  track: MediaStreamVideoTrack,
@@ -71,10 +70,12 @@ export class VirtualBackground extends BaseVideoProcessor {
71
70
 
72
71
  protected async transform(frame: VideoFrame): Promise<VideoFrame> {
73
72
  const currentTime = frame.timestamp;
74
- const hasNewFrame = currentTime !== this.lastFrameTime;
73
+ const hasNewFrame = currentTime - this.lastFrameTime > 1_000;
75
74
  this.lastFrameTime = currentTime;
76
75
 
77
76
  if (hasNewFrame && this.isSegmenterReady && this.segmenter) {
77
+ const start = performance.now();
78
+
78
79
  await this.runSegmentation(frame);
79
80
 
80
81
  this.webGlRenderer.render(
@@ -83,6 +84,8 @@ export class VirtualBackground extends BaseVideoProcessor {
83
84
  this.latestCategoryMask,
84
85
  this.latestConfidenceMask,
85
86
  );
87
+
88
+ this.updateStats(performance.now() - start);
86
89
  }
87
90
 
88
91
  return new VideoFrame(this.canvas, { timestamp: frame.timestamp });
@@ -125,20 +128,15 @@ export class VirtualBackground extends BaseVideoProcessor {
125
128
  }
126
129
 
127
130
  const blurLevel = this.options.backgroundBlurLevel;
131
+ const strength =
132
+ typeof blurLevel === 'string'
133
+ ? BACKGROUND_BLUR_MAP[blurLevel]
134
+ : Math.round(blurLevel ?? 5);
128
135
 
129
- if (typeof blurLevel === 'string') {
130
- return {
131
- ...BACKGROUND_BLUR_MAP[blurLevel],
132
- backgroundSource: undefined,
133
- isSelfieMode,
134
- };
135
- }
136
-
137
- const numeric = blurLevel ?? 5;
138
136
  return {
139
137
  backgroundSource: undefined,
140
- bgBlur: Math.min(numeric * 3, 30),
141
- bgBlurRadius: Math.min(numeric, 10),
138
+ bgBlur: Math.min(strength * 1.5, 20),
139
+ bgBlurRadius: Math.min(strength, 10),
142
140
  isSelfieMode,
143
141
  };
144
142
  }
package/src/types.ts CHANGED
@@ -43,20 +43,11 @@ export interface VideoTrackProcessorHooks {
43
43
  onStats?: (stats: PerformanceStats) => void;
44
44
  }
45
45
 
46
- export const BACKGROUND_BLUR_MAP: Record<
47
- BackgroundBlurLevel,
48
- { bgBlur: number; bgBlurRadius: number }
49
- > = {
50
- low: {
51
- bgBlur: 5,
52
- bgBlurRadius: 3,
53
- },
54
- medium: {
55
- bgBlur: 15,
56
- bgBlurRadius: 4,
57
- },
58
- high: {
59
- bgBlur: 30,
60
- bgBlurRadius: 5,
61
- },
46
+ /**
47
+ * Maps blur level to blur strength values.
48
+ */
49
+ export const BACKGROUND_BLUR_MAP: Record<'low' | 'medium' | 'high', number> = {
50
+ low: 3,
51
+ medium: 5,
52
+ high: 7,
62
53
  };