@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.
- package/CHANGELOG.md +12 -0
- package/README.md +27 -24
- package/dist/index.cjs.js +1099 -6
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.es.js +1096 -7
- package/dist/index.es.js.map +1 -1
- package/dist/src/FallbackGenerator.d.ts +17 -0
- package/dist/src/FallbackProcessor.d.ts +38 -0
- package/dist/src/VirtualBackground.d.ts +42 -0
- package/dist/src/WebGLRenderer.d.ts +80 -0
- package/dist/src/compatibility.d.ts +5 -0
- package/dist/src/{createRenderer.d.ts → legacy/createRenderer.d.ts} +1 -2
- package/dist/src/{webgl2 → legacy/webgl2}/backgroundBlurStage.d.ts +1 -1
- package/dist/src/{webgl2 → legacy/webgl2}/webgl2Pipeline.d.ts +1 -1
- package/dist/src/mediapipe.d.ts +4 -0
- package/dist/src/types.d.ts +45 -0
- package/index.ts +6 -3
- package/mediapipe/models/selfie_segmenter.tflite +0 -0
- package/mediapipe/wasm/vision_wasm_internal.js +20 -0
- package/mediapipe/wasm/vision_wasm_internal.wasm +0 -0
- package/mediapipe/wasm/vision_wasm_nosimd_internal.js +20 -0
- package/mediapipe/wasm/vision_wasm_nosimd_internal.wasm +0 -0
- package/package.json +5 -2
- package/src/FallbackGenerator.ts +90 -0
- package/src/FallbackProcessor.ts +132 -0
- package/src/VirtualBackground.ts +276 -0
- package/src/WebGLRenderer.ts +1187 -0
- package/src/compatibility.ts +22 -0
- package/src/{createRenderer.ts → legacy/createRenderer.ts} +1 -2
- package/src/{tflite.ts → legacy/tflite.ts} +1 -1
- package/src/{webgl2 → legacy/webgl2}/backgroundBlurStage.ts +1 -1
- package/src/{webgl2 → legacy/webgl2}/webgl2Pipeline.ts +1 -1
- package/src/mediapipe.ts +25 -0
- package/src/types.ts +62 -0
- /package/dist/src/{helpers → legacy/helpers}/webglHelper.d.ts +0 -0
- /package/dist/src/{segmentation.d.ts → legacy/segmentation.d.ts} +0 -0
- /package/dist/src/{tflite.d.ts → legacy/tflite.d.ts} +0 -0
- /package/dist/src/{webgl2 → legacy/webgl2}/backgroundImageStage.d.ts +0 -0
- /package/dist/src/{webgl2 → legacy/webgl2}/jointBilateralFilterStage.d.ts +0 -0
- /package/dist/src/{webgl2 → legacy/webgl2}/resizingStage.d.ts +0 -0
- /package/dist/src/{webgl2 → legacy/webgl2}/softmaxStage.d.ts +0 -0
- /package/src/{helpers → legacy/helpers}/webglHelper.ts +0 -0
- /package/src/{segmentation.ts → legacy/segmentation.ts} +0 -0
- /package/src/{tflite-simd.js → legacy/tflite-simd.js} +0 -0
- /package/src/{webgl2 → legacy/webgl2}/backgroundImageStage.ts +0 -0
- /package/src/{webgl2 → legacy/webgl2}/jointBilateralFilterStage.ts +0 -0
- /package/src/{webgl2 → legacy/webgl2}/resizingStage.ts +0 -0
- /package/src/{webgl2 → legacy/webgl2}/softmaxStage.ts +0 -0
package/src/compatibility.ts
CHANGED
|
@@ -46,3 +46,25 @@ export const isPlatformSupported = async ({
|
|
|
46
46
|
!!window.WebGL2RenderingContext && // WebGL2 is required for the video filters
|
|
47
47
|
!!document.createElement('canvas').getContext('webgl2') &&
|
|
48
48
|
(await simd()); // SIMD is required for the wasm module
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Runs a check to see if the current platform supports
|
|
52
|
+
* the necessary APIs required for the MediaPipe-based video filters.
|
|
53
|
+
*/
|
|
54
|
+
export const isMediaPipePlatformSupported = async ({
|
|
55
|
+
forceMobileSupport = false,
|
|
56
|
+
forceSafariSupport = false,
|
|
57
|
+
}: PlatformSupportFlags = {}) =>
|
|
58
|
+
typeof document !== 'undefined' &&
|
|
59
|
+
typeof window !== 'undefined' &&
|
|
60
|
+
typeof navigator !== 'undefined' &&
|
|
61
|
+
// we don't support mobile devices yet due to performance issues
|
|
62
|
+
(forceMobileSupport || !isMobile()) &&
|
|
63
|
+
// Safari has issues with timer throttling, causing low FPS when the tab goes to the background
|
|
64
|
+
(forceSafariSupport || !isSafari()) &&
|
|
65
|
+
typeof WebAssembly !== 'undefined' &&
|
|
66
|
+
typeof OffscreenCanvas !== 'undefined' && // OffscreenCanvas is required for efficient rendering
|
|
67
|
+
!!window.WebGL2RenderingContext && // WebGL2 is required for the video filters
|
|
68
|
+
!!new OffscreenCanvas(1, 1).getContext('webgl2') &&
|
|
69
|
+
typeof VideoFrame !== 'undefined' && // VideoFrame API is required for frame processing
|
|
70
|
+
typeof createImageBitmap !== 'undefined'; // createImageBitmap is required for background image processing
|
|
@@ -2,9 +2,8 @@ import { WorkerTimer } from '@stream-io/worker-timer';
|
|
|
2
2
|
import { TFLite } from './tflite';
|
|
3
3
|
import { buildWebGL2Pipeline } from './webgl2/webgl2Pipeline';
|
|
4
4
|
import { getSegmentationParams, SegmentationLevel } from './segmentation';
|
|
5
|
+
import { BackgroundBlurLevel, BackgroundFilter } from '../types';
|
|
5
6
|
|
|
6
|
-
export type BackgroundFilter = 'blur' | 'image';
|
|
7
|
-
export type BackgroundBlurLevel = 'low' | 'medium' | 'high' | number;
|
|
8
7
|
export type Renderer = {
|
|
9
8
|
/**
|
|
10
9
|
* Disposes of the renderer.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @ts-expect-error - module is not declared
|
|
2
2
|
import { createTFLiteSIMDModule } from './tflite-simd.js';
|
|
3
|
-
import { packageName, version } from '
|
|
3
|
+
import { packageName, version } from '../version';
|
|
4
4
|
|
|
5
5
|
// This is a WebAssembly module compiled from the TensorFlow Lite C++ library.
|
|
6
6
|
const createTFLite = createTFLiteSIMDModule as (opts?: any) => Promise<TFLite>;
|
|
@@ -11,8 +11,8 @@ import {
|
|
|
11
11
|
import { buildJointBilateralFilterStage } from './jointBilateralFilterStage';
|
|
12
12
|
import { buildResizingStage } from './resizingStage';
|
|
13
13
|
import { buildSoftmaxStage } from './softmaxStage';
|
|
14
|
-
import { BackgroundBlurLevel, BackgroundFilter } from '../createRenderer';
|
|
15
14
|
import { SegmentationParams } from '../segmentation';
|
|
15
|
+
import type { BackgroundBlurLevel, BackgroundFilter } from '../../types';
|
|
16
16
|
|
|
17
17
|
export function buildWebGL2Pipeline(
|
|
18
18
|
videoSource: HTMLVideoElement,
|
package/src/mediapipe.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { packageName, version } from './version';
|
|
2
|
+
|
|
3
|
+
let lastModelFilePath = '';
|
|
4
|
+
let modelFileCache: ArrayBuffer | undefined;
|
|
5
|
+
export const loadMediaPipe = async (
|
|
6
|
+
options: {
|
|
7
|
+
basePath?: string;
|
|
8
|
+
modelPath?: string;
|
|
9
|
+
} = {},
|
|
10
|
+
): Promise<ArrayBuffer> => {
|
|
11
|
+
const {
|
|
12
|
+
basePath = `https://unpkg.com/${packageName}@${version}/mediapipe`,
|
|
13
|
+
modelPath = `${basePath}/models/selfie_segmenter.tflite`,
|
|
14
|
+
} = options;
|
|
15
|
+
|
|
16
|
+
const model =
|
|
17
|
+
modelPath === lastModelFilePath && modelFileCache
|
|
18
|
+
? modelFileCache
|
|
19
|
+
: await fetch(modelPath).then((r) => r.arrayBuffer());
|
|
20
|
+
|
|
21
|
+
modelFileCache = model;
|
|
22
|
+
lastModelFilePath = modelPath;
|
|
23
|
+
|
|
24
|
+
return model;
|
|
25
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
|
|
11
|
+
export interface SegmenterOptions {
|
|
12
|
+
backgroundSource?: BackgroundSource | null;
|
|
13
|
+
bgBlur: number;
|
|
14
|
+
bgBlurRadius: number;
|
|
15
|
+
isSelfieMode: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Static configuration for the processor, defining which background
|
|
19
|
+
* effect should be applied and how it should behave.
|
|
20
|
+
*/
|
|
21
|
+
export interface BackgroundOptions {
|
|
22
|
+
basePath?: string;
|
|
23
|
+
modelPath?: string;
|
|
24
|
+
backgroundFilter?: BackgroundFilter;
|
|
25
|
+
backgroundBlurLevel?: BackgroundBlurLevel;
|
|
26
|
+
backgroundImage?: string | undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Performance statistics for video processing.
|
|
31
|
+
*/
|
|
32
|
+
export interface PerformanceStats {
|
|
33
|
+
delay: number;
|
|
34
|
+
fps: number;
|
|
35
|
+
timestamp: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Runtime hooks for handling lifecycle or error events.
|
|
40
|
+
*/
|
|
41
|
+
export interface VideoTrackProcessorHooks {
|
|
42
|
+
onError?: (error: unknown) => void;
|
|
43
|
+
onStats?: (stats: PerformanceStats) => void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const BACKGROUND_BLUR_MAP: Record<
|
|
47
|
+
BackgroundBlurLevel,
|
|
48
|
+
{ bgBlur: number; bgBlurRadius: number }
|
|
49
|
+
> = {
|
|
50
|
+
low: {
|
|
51
|
+
bgBlur: 15,
|
|
52
|
+
bgBlurRadius: 5,
|
|
53
|
+
},
|
|
54
|
+
medium: {
|
|
55
|
+
bgBlur: 20,
|
|
56
|
+
bgBlurRadius: 7,
|
|
57
|
+
},
|
|
58
|
+
high: {
|
|
59
|
+
bgBlur: 25,
|
|
60
|
+
bgBlurRadius: 10,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|