@stream-io/video-filters-web 0.3.0 → 0.5.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 (49) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +27 -24
  3. package/dist/index.cjs.js +1099 -6
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.d.ts +6 -3
  6. package/dist/index.es.js +1096 -7
  7. package/dist/index.es.js.map +1 -1
  8. package/dist/src/FallbackGenerator.d.ts +17 -0
  9. package/dist/src/FallbackProcessor.d.ts +38 -0
  10. package/dist/src/VirtualBackground.d.ts +42 -0
  11. package/dist/src/WebGLRenderer.d.ts +80 -0
  12. package/dist/src/compatibility.d.ts +5 -0
  13. package/dist/src/{createRenderer.d.ts → legacy/createRenderer.d.ts} +1 -2
  14. package/dist/src/{webgl2 → legacy/webgl2}/backgroundBlurStage.d.ts +1 -1
  15. package/dist/src/{webgl2 → legacy/webgl2}/webgl2Pipeline.d.ts +1 -1
  16. package/dist/src/mediapipe.d.ts +4 -0
  17. package/dist/src/types.d.ts +45 -0
  18. package/index.ts +6 -3
  19. package/mediapipe/models/selfie_segmenter.tflite +0 -0
  20. package/mediapipe/wasm/vision_wasm_internal.js +20 -0
  21. package/mediapipe/wasm/vision_wasm_internal.wasm +0 -0
  22. package/mediapipe/wasm/vision_wasm_nosimd_internal.js +20 -0
  23. package/mediapipe/wasm/vision_wasm_nosimd_internal.wasm +0 -0
  24. package/package.json +5 -2
  25. package/src/FallbackGenerator.ts +90 -0
  26. package/src/FallbackProcessor.ts +132 -0
  27. package/src/VirtualBackground.ts +276 -0
  28. package/src/WebGLRenderer.ts +1187 -0
  29. package/src/compatibility.ts +22 -0
  30. package/src/{createRenderer.ts → legacy/createRenderer.ts} +1 -2
  31. package/src/{tflite.ts → legacy/tflite.ts} +1 -1
  32. package/src/{webgl2 → legacy/webgl2}/backgroundBlurStage.ts +1 -1
  33. package/src/{webgl2 → legacy/webgl2}/webgl2Pipeline.ts +1 -1
  34. package/src/mediapipe.ts +25 -0
  35. package/src/types.ts +62 -0
  36. /package/dist/src/{helpers → legacy/helpers}/webglHelper.d.ts +0 -0
  37. /package/dist/src/{segmentation.d.ts → legacy/segmentation.d.ts} +0 -0
  38. /package/dist/src/{tflite.d.ts → legacy/tflite.d.ts} +0 -0
  39. /package/dist/src/{webgl2 → legacy/webgl2}/backgroundImageStage.d.ts +0 -0
  40. /package/dist/src/{webgl2 → legacy/webgl2}/jointBilateralFilterStage.d.ts +0 -0
  41. /package/dist/src/{webgl2 → legacy/webgl2}/resizingStage.d.ts +0 -0
  42. /package/dist/src/{webgl2 → legacy/webgl2}/softmaxStage.d.ts +0 -0
  43. /package/src/{helpers → legacy/helpers}/webglHelper.ts +0 -0
  44. /package/src/{segmentation.ts → legacy/segmentation.ts} +0 -0
  45. /package/src/{tflite-simd.js → legacy/tflite-simd.js} +0 -0
  46. /package/src/{webgl2 → legacy/webgl2}/backgroundImageStage.ts +0 -0
  47. /package/src/{webgl2 → legacy/webgl2}/jointBilateralFilterStage.ts +0 -0
  48. /package/src/{webgl2 → legacy/webgl2}/resizingStage.ts +0 -0
  49. /package/src/{webgl2 → legacy/webgl2}/softmaxStage.ts +0 -0
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Type representing a video track generator that can be either the native
3
+ * MediaStreamTrackGenerator or the fallback implementation.
4
+ */
5
+ export interface MediaStreamTrackGenerator<T> extends MediaStreamTrack {
6
+ writable: WritableStream<T>;
7
+ }
8
+ /**
9
+ * Configuration options for creating a track generator.
10
+ */
11
+ export interface TrackGeneratorOptions {
12
+ readonly kind: 'video';
13
+ readonly signalTarget?: MediaStreamTrack;
14
+ }
15
+ type VideoTrackGeneratorConstructor = new (options: TrackGeneratorOptions) => MediaStreamTrackGenerator<VideoFrame>;
16
+ declare const TrackGenerator: VideoTrackGeneratorConstructor;
17
+ export { TrackGenerator };
@@ -0,0 +1,38 @@
1
+ import { WorkerTimer } from '@stream-io/worker-timer';
2
+ /**
3
+ * Representing a video track processor that can be either the native
4
+ * MediaStreamTrackProcessor or the fallback implementation.
5
+ */
6
+ export interface MediaStreamTrackProcessor<T> {
7
+ readonly readable: ReadableStream<T>;
8
+ }
9
+ /**
10
+ * Configuration options for creating a track processor.
11
+ */
12
+ export interface TrackProcessorOptions {
13
+ readonly track: MediaStreamTrack;
14
+ }
15
+ /**
16
+ * Fallback implementation for browsers without MediaStreamTrackGenerator.
17
+ *
18
+ * Produces a video MediaStreamTrack sourced from a canvas and exposes a
19
+ * WritableStream<VideoFrame> on track.writable. Written frames are drawn
20
+ * into the canvas and update the underlying track automatically.
21
+ */
22
+ declare class FallbackProcessor implements MediaStreamTrackProcessor<VideoFrame> {
23
+ readonly readable: ReadableStream<VideoFrame>;
24
+ readonly workerTimer: WorkerTimer;
25
+ readonly video: HTMLVideoElement;
26
+ constructor({ track }: TrackProcessorOptions);
27
+ close: () => void;
28
+ }
29
+ declare const TrackProcessor: typeof FallbackProcessor | {
30
+ new (init: MediaStreamTrackProcessorInit & {
31
+ track: MediaStreamAudioTrack;
32
+ }): globalThis.MediaStreamTrackProcessor<AudioData>;
33
+ new (init: MediaStreamTrackProcessorInit & {
34
+ track: MediaStreamVideoTrack;
35
+ }): globalThis.MediaStreamTrackProcessor<VideoFrame>;
36
+ prototype: globalThis.MediaStreamTrackProcessor<any>;
37
+ };
38
+ export { TrackProcessor };
@@ -0,0 +1,42 @@
1
+ import { BackgroundOptions, VideoTrackProcessorHooks } from './types';
2
+ /**
3
+ * Wraps a video MediaStreamTrack in a real-time processing pipeline.
4
+ * Incoming frames are processed through a transformer and re-emitted
5
+ * on a new MediaStreamVideoTrack for downstream consumption.
6
+ */
7
+ export declare class VirtualBackground {
8
+ private readonly track;
9
+ private readonly options;
10
+ private readonly hooks;
11
+ private readonly processor;
12
+ private readonly generator;
13
+ private canvas;
14
+ private segmenter;
15
+ private isSegmenterReady;
16
+ private webGlRenderer;
17
+ private abortController;
18
+ private segmenterDelayTotal;
19
+ private frames;
20
+ private lastStatsTime;
21
+ constructor(track: MediaStreamVideoTrack, options?: BackgroundOptions, hooks?: VideoTrackProcessorHooks);
22
+ start(): Promise<MediaStreamTrack>;
23
+ /**
24
+ * Loads and initializes the MediaPipe `ImageSegmenter`.
25
+ */
26
+ private initializeSegmenter;
27
+ /**
28
+ * Processes a single video frame.
29
+ *
30
+ * Performs segmentation via MediaPipe and then composites the frame
31
+ * through the WebGL renderer to apply background effects.
32
+ *
33
+ * @param frame - The incoming frame from the processor.
34
+ * @param opts - The segmentation options to use.
35
+ *
36
+ * @returns A new `VideoFrame` containing the processed image.
37
+ */
38
+ private transform;
39
+ private loadBackground;
40
+ private initializeSegmenterOptions;
41
+ stop(): void;
42
+ }
@@ -0,0 +1,80 @@
1
+ import { BackgroundSource } from './types';
2
+ export declare class WebGLRenderer {
3
+ readonly canvas: OffscreenCanvas;
4
+ readonly gl: WebGL2RenderingContext;
5
+ readonly stateUpdateProgram: WebGLProgram;
6
+ readonly maskRefineProgram: WebGLProgram;
7
+ readonly blurProgram: WebGLProgram;
8
+ readonly blendProgram: WebGLProgram;
9
+ readonly stateUpdateLocations: {
10
+ position: number;
11
+ texCoord: number;
12
+ categoryTexture: WebGLUniformLocation | null;
13
+ confidenceTexture: WebGLUniformLocation | null;
14
+ prevStateTexture: WebGLUniformLocation | null;
15
+ smoothingFactor: WebGLUniformLocation | null;
16
+ smoothstepMin: WebGLUniformLocation | null;
17
+ smoothstepMax: WebGLUniformLocation | null;
18
+ selfieModel: WebGLUniformLocation | null;
19
+ };
20
+ readonly maskRefineLocations: {
21
+ position: number;
22
+ texCoord: number;
23
+ maskTexture: WebGLUniformLocation | null;
24
+ frameTexture: WebGLUniformLocation | null;
25
+ texelSize: WebGLUniformLocation | null;
26
+ sigmaSpatial: WebGLUniformLocation | null;
27
+ sigmaRange: WebGLUniformLocation | null;
28
+ };
29
+ readonly blurLocations: {
30
+ position: number;
31
+ texCoord: number;
32
+ image: WebGLUniformLocation | null;
33
+ texelSize: WebGLUniformLocation | null;
34
+ sigma: WebGLUniformLocation | null;
35
+ radiusScale: WebGLUniformLocation | null;
36
+ personMask: WebGLUniformLocation | null;
37
+ direction: WebGLUniformLocation | null;
38
+ };
39
+ readonly blendLocations: {
40
+ position: number;
41
+ texCoord: number;
42
+ frameTexture: WebGLUniformLocation | null;
43
+ currentStateTexture: WebGLUniformLocation | null;
44
+ backgroundTexture: WebGLUniformLocation | null;
45
+ bgImageDimensions: WebGLUniformLocation | null;
46
+ canvasDimensions: WebGLUniformLocation | null;
47
+ borderSmooth: WebGLUniformLocation | null;
48
+ bgBlur: WebGLUniformLocation | null;
49
+ bgBlurRadius: WebGLUniformLocation | null;
50
+ enabled: WebGLUniformLocation | null;
51
+ };
52
+ readonly positionBuffer: WebGLBuffer | null;
53
+ readonly texCoordBuffer: WebGLBuffer | null;
54
+ readonly storedStateTextures: (WebGLTexture | null)[];
55
+ readonly fbo: WebGLFramebuffer | null;
56
+ readonly refineFbo: WebGLFramebuffer | null;
57
+ readonly refinedMaskTexture: WebGLTexture | null;
58
+ readonly frameTexture: WebGLTexture | null;
59
+ readonly blurTexture1: WebGLTexture | null;
60
+ readonly blurTexture2: WebGLTexture | null;
61
+ readonly blurFbo1: WebGLFramebuffer | null;
62
+ readonly blurFbo2: WebGLFramebuffer | null;
63
+ private running;
64
+ private static readonly DEFAULT_BG_COLOR;
65
+ private currentStateIndex;
66
+ private backgroundRenderInfo;
67
+ private activeBackgroundSourceIdentifier;
68
+ constructor(canvas: OffscreenCanvas);
69
+ private createAndLinkProgram;
70
+ private createShader;
71
+ private createColorTexture;
72
+ private updateBackgroundIfNeeded;
73
+ render(videoFrame: VideoFrame, options: {
74
+ backgroundSource?: BackgroundSource | null;
75
+ bgBlur: number;
76
+ bgBlurRadius: number;
77
+ isSelfieMode: boolean;
78
+ }, categoryTexture?: WebGLTexture, confidenceTexture?: WebGLTexture): void;
79
+ close(): void;
80
+ }
@@ -14,3 +14,8 @@ export type PlatformSupportFlags = {
14
14
  * the necessary APIs required for the video filters.
15
15
  */
16
16
  export declare const isPlatformSupported: ({ forceMobileSupport, forceSafariSupport, }?: PlatformSupportFlags) => Promise<boolean>;
17
+ /**
18
+ * Runs a check to see if the current platform supports
19
+ * the necessary APIs required for the MediaPipe-based video filters.
20
+ */
21
+ export declare const isMediaPipePlatformSupported: ({ forceMobileSupport, forceSafariSupport, }?: PlatformSupportFlags) => Promise<boolean>;
@@ -1,7 +1,6 @@
1
1
  import { TFLite } from './tflite';
2
2
  import { SegmentationLevel } from './segmentation';
3
- export type BackgroundFilter = 'blur' | 'image';
4
- export type BackgroundBlurLevel = 'low' | 'medium' | 'high' | number;
3
+ import { BackgroundBlurLevel, BackgroundFilter } from '../types';
5
4
  export type Renderer = {
6
5
  /**
7
6
  * Disposes of the renderer.
@@ -1,4 +1,4 @@
1
- import type { BackgroundBlurLevel } from '../createRenderer';
1
+ import { BackgroundBlurLevel } from '../../types';
2
2
  export type BackgroundBlurStage = {
3
3
  render(): void;
4
4
  updateCoverage(coverage: [number, number]): void;
@@ -1,6 +1,6 @@
1
1
  import { TFLite } from '../tflite';
2
- import { BackgroundBlurLevel, BackgroundFilter } from '../createRenderer';
3
2
  import { SegmentationParams } from '../segmentation';
3
+ import type { BackgroundBlurLevel, BackgroundFilter } from '../../types';
4
4
  export declare function buildWebGL2Pipeline(videoSource: HTMLVideoElement, backgroundImage: HTMLImageElement | undefined, blurLevel: BackgroundBlurLevel | undefined, backgroundFilter: BackgroundFilter, canvas: HTMLCanvasElement, tflite: TFLite, segmentationConfig: SegmentationParams, onError?: (error: any) => void): {
5
5
  render: () => void;
6
6
  updatePostProcessingConfig: () => void;
@@ -0,0 +1,4 @@
1
+ export declare const loadMediaPipe: (options?: {
2
+ basePath?: string;
3
+ modelPath?: string;
4
+ }) => Promise<ArrayBuffer>;
@@ -0,0 +1,45 @@
1
+ export type BackgroundSource = {
2
+ type: string;
3
+ media?: ImageBitmap | ReadableStream;
4
+ url: string;
5
+ video?: HTMLVideoElement;
6
+ track?: MediaStreamTrack;
7
+ };
8
+ export type BackgroundFilter = 'blur' | 'image';
9
+ export type BackgroundBlurLevel = 'low' | 'medium' | 'high' | number;
10
+ export interface SegmenterOptions {
11
+ backgroundSource?: BackgroundSource | null;
12
+ bgBlur: number;
13
+ bgBlurRadius: number;
14
+ isSelfieMode: boolean;
15
+ }
16
+ /**
17
+ * Static configuration for the processor, defining which background
18
+ * effect should be applied and how it should behave.
19
+ */
20
+ export interface BackgroundOptions {
21
+ basePath?: string;
22
+ modelPath?: string;
23
+ backgroundFilter?: BackgroundFilter;
24
+ backgroundBlurLevel?: BackgroundBlurLevel;
25
+ backgroundImage?: string | undefined;
26
+ }
27
+ /**
28
+ * Performance statistics for video processing.
29
+ */
30
+ export interface PerformanceStats {
31
+ delay: number;
32
+ fps: number;
33
+ timestamp: number;
34
+ }
35
+ /**
36
+ * Runtime hooks for handling lifecycle or error events.
37
+ */
38
+ export interface VideoTrackProcessorHooks {
39
+ onError?: (error: unknown) => void;
40
+ onStats?: (stats: PerformanceStats) => void;
41
+ }
42
+ export declare const BACKGROUND_BLUR_MAP: Record<BackgroundBlurLevel, {
43
+ bgBlur: number;
44
+ bgBlurRadius: number;
45
+ }>;
package/index.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  export * from './src/compatibility';
2
- export * from './src/createRenderer';
3
- export { SegmentationLevel } from './src/segmentation';
4
- export * from './src/tflite';
2
+ export * from './src/legacy/createRenderer';
3
+ export { SegmentationLevel } from './src/legacy/segmentation';
4
+ export * from './src/legacy/tflite';
5
+ export * from './src/mediapipe';
6
+ export * from './src/types';
7
+ export * from './src/VirtualBackground';