@stream-io/video-filters-web 0.2.1 → 0.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stream-io/video-filters-web",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "main": "./dist/index.cjs.js",
5
5
  "module": "./dist/index.es.js",
6
6
  "types": "./dist/index.d.ts",
@@ -27,6 +27,7 @@
27
27
  "CHANGELOG.md"
28
28
  ],
29
29
  "dependencies": {
30
+ "@stream-io/worker-timer": "^1.2.4",
30
31
  "wasm-feature-detect": "^1.8.0"
31
32
  },
32
33
  "devDependencies": {
@@ -1,5 +1,18 @@
1
1
  import { simd } from 'wasm-feature-detect';
2
2
 
3
+ export type PlatformSupportFlags = {
4
+ /**
5
+ * Forces support for mobile devices, although performance isn't optimal.
6
+ */
7
+ forceMobileSupport?: boolean;
8
+
9
+ /**
10
+ * Forces support for Safari, although in cases where the tab is put in the background,
11
+ * the FPS can drop significantly or freeze completely due to intensive timer throttling.
12
+ */
13
+ forceSafariSupport?: boolean;
14
+ };
15
+
3
16
  /**
4
17
  * Checks if the current platform is a mobile device.
5
18
  *
@@ -8,15 +21,27 @@ import { simd } from 'wasm-feature-detect';
8
21
  */
9
22
  const isMobile = () => /Mobi/i.test(navigator.userAgent);
10
23
 
24
+ /**
25
+ * Checks whether the current browser is Safari.
26
+ */
27
+ const isSafari = () =>
28
+ /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
29
+
11
30
  /**
12
31
  * Runs a check to see if the current platform supports
13
32
  * the necessary APIs required for the video filters.
14
33
  */
15
- export const isPlatformSupported = async () =>
34
+ export const isPlatformSupported = async ({
35
+ forceMobileSupport = false,
36
+ forceSafariSupport = false,
37
+ }: PlatformSupportFlags = {}) =>
16
38
  typeof document !== 'undefined' &&
17
39
  typeof window !== 'undefined' &&
18
40
  typeof navigator !== 'undefined' &&
19
- !isMobile() && // we don't support mobile devices yet due to performance issues
41
+ // we don't support mobile devices yet due to performance issues
42
+ (forceMobileSupport || !isMobile()) &&
43
+ // Safari has issues with timer throttling, causing low FPS when the tab goes to the background
44
+ (forceSafariSupport || !isSafari()) &&
20
45
  typeof WebAssembly !== 'undefined' &&
21
46
  !!window.WebGL2RenderingContext && // WebGL2 is required for the video filters
22
47
  !!document.createElement('canvas').getContext('webgl2') &&
@@ -1,3 +1,4 @@
1
+ import { WorkerTimer } from '@stream-io/worker-timer';
1
2
  import { TFLite } from './tflite';
2
3
  import { buildWebGL2Pipeline } from './webgl2/webgl2Pipeline';
3
4
  import { getSegmentationParams, SegmentationLevel } from './segmentation';
@@ -47,7 +48,8 @@ export function createRenderer(
47
48
  getSegmentationParams(segmentationLevel),
48
49
  );
49
50
 
50
- const id = setInterval(
51
+ const timers = new WorkerTimer({ useWorker: true });
52
+ const id = timers.setInterval(
51
53
  () => {
52
54
  try {
53
55
  pipeline.render();
@@ -59,13 +61,14 @@ export function createRenderer(
59
61
  onError?.(error);
60
62
  }
61
63
  },
62
- 1000 / (fps <= 0 ? 30 : fps),
64
+ Math.floor(1000 / (fps <= 0 ? 30 : fps)),
63
65
  );
64
66
 
65
67
  return {
66
68
  dispose: () => {
67
69
  pipeline.cleanUp();
68
- clearInterval(id);
70
+ timers.clearInterval(id);
71
+ timers.destroy();
69
72
  },
70
73
  };
71
74
  }