react-native-image-stitcher 0.7.1 → 0.9.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 (58) hide show
  1. package/CHANGELOG.md +241 -0
  2. package/android/build.gradle +35 -1
  3. package/android/src/main/cpp/CMakeLists.txt +64 -2
  4. package/android/src/main/cpp/stitcher_jsi_install_jni.cpp +227 -0
  5. package/android/src/main/java/io/imagestitcher/rn/IncrementalStitcher.kt +30 -11
  6. package/android/src/main/java/io/imagestitcher/rn/RNImageStitcherPackage.kt +21 -3
  7. package/android/src/main/java/io/imagestitcher/rn/RNSARCameraView.kt +78 -3
  8. package/android/src/main/java/io/imagestitcher/rn/SaveFrameAsJpegPlugin.kt +162 -0
  9. package/android/src/main/java/io/imagestitcher/rn/StitcherJsiInstallerModule.kt +103 -0
  10. package/android/src/main/java/io/imagestitcher/rn/StitcherWorkletRuntime.kt +256 -0
  11. package/android/src/main/java/io/imagestitcher/rn/TransferredNV21.kt +100 -0
  12. package/cpp/stitcher_frame_data.hpp +141 -0
  13. package/cpp/stitcher_frame_jsi.cpp +214 -0
  14. package/cpp/stitcher_frame_jsi.hpp +108 -0
  15. package/cpp/stitcher_proxy_jsi.cpp +109 -0
  16. package/cpp/stitcher_proxy_jsi.hpp +46 -0
  17. package/cpp/stitcher_worklet_dispatch.cpp +103 -0
  18. package/cpp/stitcher_worklet_dispatch.hpp +71 -0
  19. package/cpp/stitcher_worklet_registry.cpp +81 -0
  20. package/cpp/stitcher_worklet_registry.hpp +136 -0
  21. package/dist/camera/Camera.d.ts +62 -12
  22. package/dist/camera/Camera.js +30 -15
  23. package/dist/index.d.ts +6 -0
  24. package/dist/index.js +30 -1
  25. package/dist/stitching/StitcherFrame.d.ts +170 -0
  26. package/dist/stitching/StitcherFrame.js +4 -0
  27. package/dist/stitching/StitcherWorkletRegistry.d.ts +117 -0
  28. package/dist/stitching/StitcherWorkletRegistry.js +78 -0
  29. package/dist/stitching/ensureStitcherProxyInstalled.d.ts +8 -0
  30. package/dist/stitching/ensureStitcherProxyInstalled.js +81 -0
  31. package/dist/stitching/useFrameProcessor.d.ts +119 -0
  32. package/dist/stitching/useFrameProcessor.js +196 -0
  33. package/dist/stitching/useFrameStream.d.ts +34 -0
  34. package/dist/stitching/useFrameStream.js +219 -0
  35. package/dist/stitching/useThrottledFrameProcessor.d.ts +33 -0
  36. package/dist/stitching/useThrottledFrameProcessor.js +132 -0
  37. package/dist/types.d.ts +87 -0
  38. package/ios/Sources/RNImageStitcher/RNSARSession.swift +46 -10
  39. package/ios/Sources/RNImageStitcher/RNSARWorkletRuntime.h +128 -0
  40. package/ios/Sources/RNImageStitcher/RNSARWorkletRuntime.mm +313 -0
  41. package/ios/Sources/RNImageStitcher/SaveFrameAsJpegPlugin.mm +185 -0
  42. package/ios/Sources/RNImageStitcher/StitcherFrameHostObject.h +60 -0
  43. package/ios/Sources/RNImageStitcher/StitcherFrameHostObject.mm +214 -0
  44. package/ios/Sources/RNImageStitcher/StitcherJsiInstaller.h +42 -0
  45. package/ios/Sources/RNImageStitcher/StitcherJsiInstaller.mm +103 -0
  46. package/package.json +1 -1
  47. package/src/camera/Camera.tsx +93 -28
  48. package/src/index.ts +35 -0
  49. package/src/stitching/StitcherFrame.ts +197 -0
  50. package/src/stitching/StitcherWorkletRegistry.ts +156 -0
  51. package/src/stitching/__tests__/StitcherWorkletRegistry.test.ts +176 -0
  52. package/src/stitching/__tests__/ensureStitcherProxyInstalled.test.ts +94 -0
  53. package/src/stitching/__tests__/useThrottledFrameProcessor.test.ts +178 -0
  54. package/src/stitching/ensureStitcherProxyInstalled.ts +141 -0
  55. package/src/stitching/useFrameProcessor.ts +226 -0
  56. package/src/stitching/useFrameStream.ts +255 -0
  57. package/src/stitching/useThrottledFrameProcessor.ts +145 -0
  58. package/src/types.ts +95 -0
@@ -0,0 +1,117 @@
1
+ import type { StitcherFrameProcessor } from './StitcherFrame';
2
+ /**
3
+ * v0.8.0 Phase 4a — process-scope registry of host-supplied worklets
4
+ * that the v0.8.0 `useFrameProcessor` hook registers into.
5
+ *
6
+ * ## What this is (Phase 4a)
7
+ *
8
+ * A plain JS singleton holding an ordered list of registered
9
+ * worklets. Hosts mount the `useFrameProcessor` hook (in this
10
+ * directory); the hook registers its worklet into this singleton
11
+ * on mount and unregisters on unmount. Each entry carries:
12
+ *
13
+ * - `id`: stable identifier issued by `register`; passed to
14
+ * `unregister`.
15
+ * - `worklet`: the host's `StitcherFrameProcessor` function.
16
+ * MUST be `'worklet'`-prefixed at the call site (TS can't
17
+ * enforce that — convention).
18
+ * - `isFirstParty`: `false` for host-supplied worklets;
19
+ * reserved for the lib's own first-party stitching path which
20
+ * today is wired natively (not through this registry).
21
+ *
22
+ * Order is stable: first-party entries (none in Phase 4a) come
23
+ * first, then host entries by registration order. Re-registration
24
+ * of the same worklet by identity yields a new entry — hosts that
25
+ * re-render and call `register` again ARE responsible for calling
26
+ * `unregister` first. The `useFrameProcessor` hook handles this
27
+ * via its `deps` dependency array.
28
+ *
29
+ * ## What this is NOT (Phase 4b)
30
+ *
31
+ * **The native AR worklet runtime does NOT yet read this registry.**
32
+ * Worklets registered here for AR-mode captures will not fire
33
+ * until Phase 4b lands the cross-runtime handoff (a
34
+ * worklets-core `SharedValue` mirror that `RNSARWorkletRuntime`
35
+ * reads on each `dispatchFrame:pose:` call; the runtime then
36
+ * constructs a `StitcherFrameHostObject` + invokes each
37
+ * registered worklet via `RNWorklet::WorkletInvoker::call`).
38
+ *
39
+ * In non-AR mode the host-supplied worklet IS invoked, but via
40
+ * vision-camera's Frame Processor runtime directly (the
41
+ * `useFrameProcessor` hook returns vc's processor object which
42
+ * `<Camera>` passes to vision-camera). So Phase 4a's public API
43
+ * is fully functional for non-AR; AR is API-stable but
44
+ * runtime-deferred.
45
+ *
46
+ * ## Singleton lifetime
47
+ *
48
+ * The registry is a module-level instance. It lives for the
49
+ * lifetime of the JS runtime (= until app reload). Entries
50
+ * accumulate only via `register` and shed only via `unregister`
51
+ * — no GC / weak-ref logic. Hosts that mount `useFrameProcessor`
52
+ * inside React components MUST rely on the hook's effect cleanup
53
+ * to unregister on unmount, or they'll leak entries until
54
+ * reload. The hook handles this correctly today.
55
+ *
56
+ * ## Why a singleton (vs context provider)
57
+ *
58
+ * The native AR worklet runtime is itself a process-scope
59
+ * singleton (`RNSARWorkletRuntime`, `StitcherWorkletRuntime`).
60
+ * The Phase 4b handoff between TS and native is necessarily
61
+ * process-scope. Wrapping the registry in a React context
62
+ * would force every consumer to be in the same provider tree
63
+ * which is friction for layer-2 hosts that compose
64
+ * `<ARCameraView>` / `useIncrementalStitcher` themselves. The
65
+ * singleton is the right shape; the React-level ergonomics are
66
+ * provided by the `useFrameProcessor` hook.
67
+ */
68
+ export interface StitcherWorkletEntry {
69
+ readonly id: string;
70
+ readonly worklet: StitcherFrameProcessor;
71
+ readonly isFirstParty: boolean;
72
+ }
73
+ declare class Registry {
74
+ private entries;
75
+ private nextHostCounter;
76
+ /**
77
+ * Register a worklet. Returns a stable ID for `unregister`.
78
+ *
79
+ * Entries are appended in registration order; first-party
80
+ * entries (if any are added in future) sort to the front.
81
+ */
82
+ register(opts: {
83
+ worklet: StitcherFrameProcessor;
84
+ isFirstParty?: boolean;
85
+ }): string;
86
+ /**
87
+ * Remove a previously-registered worklet by ID. No-op if the ID
88
+ * isn't found. Hosts call this in their effect's cleanup.
89
+ */
90
+ unregister(id: string): void;
91
+ /**
92
+ * Snapshot the current entries. Returned array is a copy —
93
+ * mutations don't affect the registry. Phase 4b's native
94
+ * handoff will read a `SharedValue` mirror of this list so the
95
+ * AR runtime doesn't need a JS-thread hop on the hot per-frame
96
+ * path; for Phase 4a this method is the JS-side accessor.
97
+ */
98
+ getEntries(): readonly StitcherWorkletEntry[];
99
+ /**
100
+ * Total number of registered worklets (first-party + host).
101
+ * Useful for diagnostics + tests.
102
+ */
103
+ get count(): number;
104
+ /**
105
+ * Test-only — clear all entries. NOT exported from
106
+ * `src/index.ts`. Used in unit tests to reset state between
107
+ * cases.
108
+ */
109
+ _resetForTests(): void;
110
+ }
111
+ /**
112
+ * Process-scope singleton. Imported by `useFrameProcessor` (in
113
+ * this directory) + by the Phase 4b native-handoff code (TBD).
114
+ */
115
+ export declare const StitcherWorkletRegistry: Registry;
116
+ export {};
117
+ //# sourceMappingURL=StitcherWorkletRegistry.d.ts.map
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.StitcherWorkletRegistry = void 0;
5
+ class Registry {
6
+ constructor() {
7
+ this.entries = [];
8
+ this.nextHostCounter = 0;
9
+ }
10
+ /**
11
+ * Register a worklet. Returns a stable ID for `unregister`.
12
+ *
13
+ * Entries are appended in registration order; first-party
14
+ * entries (if any are added in future) sort to the front.
15
+ */
16
+ register(opts) {
17
+ const isFirstParty = opts.isFirstParty ?? false;
18
+ const id = isFirstParty
19
+ ? `fp-${this.nextHostCounter++}`
20
+ : `host-${this.nextHostCounter++}`;
21
+ const entry = {
22
+ id,
23
+ worklet: opts.worklet,
24
+ isFirstParty,
25
+ };
26
+ this.entries.push(entry);
27
+ // Re-sort so first-party always runs before host entries.
28
+ // Stable sort: registration order is preserved within each
29
+ // partition. Single-pass O(n log n) is fine — registration
30
+ // is rare (per-`<Camera>`-mount, not per-frame).
31
+ this.entries.sort((a, b) => {
32
+ if (a.isFirstParty !== b.isFirstParty) {
33
+ return a.isFirstParty ? -1 : 1;
34
+ }
35
+ return 0;
36
+ });
37
+ return id;
38
+ }
39
+ /**
40
+ * Remove a previously-registered worklet by ID. No-op if the ID
41
+ * isn't found. Hosts call this in their effect's cleanup.
42
+ */
43
+ unregister(id) {
44
+ this.entries = this.entries.filter((e) => e.id !== id);
45
+ }
46
+ /**
47
+ * Snapshot the current entries. Returned array is a copy —
48
+ * mutations don't affect the registry. Phase 4b's native
49
+ * handoff will read a `SharedValue` mirror of this list so the
50
+ * AR runtime doesn't need a JS-thread hop on the hot per-frame
51
+ * path; for Phase 4a this method is the JS-side accessor.
52
+ */
53
+ getEntries() {
54
+ return [...this.entries];
55
+ }
56
+ /**
57
+ * Total number of registered worklets (first-party + host).
58
+ * Useful for diagnostics + tests.
59
+ */
60
+ get count() {
61
+ return this.entries.length;
62
+ }
63
+ /**
64
+ * Test-only — clear all entries. NOT exported from
65
+ * `src/index.ts`. Used in unit tests to reset state between
66
+ * cases.
67
+ */
68
+ _resetForTests() {
69
+ this.entries = [];
70
+ this.nextHostCounter = 0;
71
+ }
72
+ }
73
+ /**
74
+ * Process-scope singleton. Imported by `useFrameProcessor` (in
75
+ * this directory) + by the Phase 4b native-handoff code (TBD).
76
+ */
77
+ exports.StitcherWorkletRegistry = new Registry();
78
+ //# sourceMappingURL=StitcherWorkletRegistry.js.map
@@ -0,0 +1,8 @@
1
+ export declare function ensureStitcherProxyInstalled(): boolean;
2
+ /**
3
+ * Test-only — reset module-internal state. Used by jest to allow
4
+ * multiple test cases to re-trigger the install path independently.
5
+ * NOT exported from `src/index.ts`.
6
+ */
7
+ export declare function _resetStitcherProxyInstallStateForTests(): void;
8
+ //# sourceMappingURL=ensureStitcherProxyInstalled.d.ts.map
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.ensureStitcherProxyInstalled = ensureStitcherProxyInstalled;
5
+ exports._resetStitcherProxyInstallStateForTests = _resetStitcherProxyInstallStateForTests;
6
+ const react_native_1 = require("react-native");
7
+ /**
8
+ * `__DEV__` is RN's global dev-flag. Guard the read with `typeof`
9
+ * so the helper works in any environment that imports it without
10
+ * defining __DEV__ (jest, SSR, custom tooling). Same pattern RN's
11
+ * own debug code uses.
12
+ */
13
+ function isDev() {
14
+ return typeof __DEV__ !== 'undefined' && __DEV__;
15
+ }
16
+ let installed = false;
17
+ function ensureStitcherProxyInstalled() {
18
+ if (installed)
19
+ return true;
20
+ // Already installed by an earlier hook mount. Cheap fast-path.
21
+ if (typeof globalThis.__stitcherProxy !== 'undefined') {
22
+ installed = true;
23
+ return true;
24
+ }
25
+ const mod = react_native_1.NativeModules
26
+ .StitcherJsiInstaller;
27
+ if (mod == null || typeof mod.install !== 'function') {
28
+ // Module not present — Android until Phase 4b.ii lands, or
29
+ // an old iOS build. Surface this once at debug-info level so
30
+ // the host can see "your worklets are JS-registered only" in
31
+ // logcat / Console.app without a noisy per-frame warning.
32
+ if (isDev() && !warnedAboutMissingModule) {
33
+ warnedAboutMissingModule = true;
34
+ console.info('[react-native-image-stitcher] StitcherJsiInstaller native ' +
35
+ 'module not found; host worklets registered in JS-side ' +
36
+ 'registry only. AR-mode dispatch requires the native install ' +
37
+ '(iOS Phase 4b.i — included in v0.8.0; Android Phase 4b.ii ' +
38
+ '— follow-up release).');
39
+ }
40
+ return false;
41
+ }
42
+ try {
43
+ const ok = mod.install();
44
+ if (!ok) {
45
+ // Native module ran but couldn't install (JSI runtime
46
+ // unreachable). Same fallback as the missing-module case.
47
+ if (isDev() && !warnedAboutFailedInstall) {
48
+ warnedAboutFailedInstall = true;
49
+ console.info('[react-native-image-stitcher] StitcherJsiInstaller.install() ' +
50
+ 'returned false (JSI runtime unreachable — remote debug ' +
51
+ 'mode?). Falling back to JS-side host worklet registry.');
52
+ }
53
+ return false;
54
+ }
55
+ installed = true;
56
+ return true;
57
+ }
58
+ catch (err) {
59
+ if (isDev() && !warnedAboutFailedInstall) {
60
+ warnedAboutFailedInstall = true;
61
+ console.info('[react-native-image-stitcher] StitcherJsiInstaller.install() ' +
62
+ 'threw: ' +
63
+ String(err) +
64
+ '. Falling back to JS-side host worklet registry.');
65
+ }
66
+ return false;
67
+ }
68
+ }
69
+ let warnedAboutMissingModule = false;
70
+ let warnedAboutFailedInstall = false;
71
+ /**
72
+ * Test-only — reset module-internal state. Used by jest to allow
73
+ * multiple test cases to re-trigger the install path independently.
74
+ * NOT exported from `src/index.ts`.
75
+ */
76
+ function _resetStitcherProxyInstallStateForTests() {
77
+ installed = false;
78
+ warnedAboutMissingModule = false;
79
+ warnedAboutFailedInstall = false;
80
+ }
81
+ //# sourceMappingURL=ensureStitcherProxyInstalled.js.map
@@ -0,0 +1,119 @@
1
+ import { type DependencyList } from 'react';
2
+ import { type DrawableFrameProcessor, type ReadonlyFrameProcessor } from 'react-native-vision-camera';
3
+ import type { StitcherFrameProcessor } from './StitcherFrame';
4
+ /**
5
+ * v0.8.0 Phase 4a — public hook for hosts to attach a per-frame
6
+ * worklet that runs in BOTH AR and non-AR capture modes.
7
+ *
8
+ * ## Quick start
9
+ *
10
+ * ```tsx
11
+ * import { useFrameProcessor, type StitcherFrame } from 'react-native-image-stitcher';
12
+ *
13
+ * function MyOcrOverlay() {
14
+ * const processor = useFrameProcessor((frame: StitcherFrame) => {
15
+ * 'worklet';
16
+ * // Pixel data is in `frame.toArrayBuffer()`.
17
+ * // AR-only fields: `frame.arDepth`, `frame.arAnchors`, `frame.arTrackingState`.
18
+ * // Discriminate via `frame.source === 'ar'` / `'vc'`.
19
+ * }, []);
20
+ * return <Camera frameProcessor={processor} ... />;
21
+ * }
22
+ * ```
23
+ *
24
+ * ## Two behaviours, depending on mode
25
+ *
26
+ * **Non-AR mode (today, fully working):** the worklet runs on
27
+ * vision-camera's Frame Processor runtime. Same thread + same
28
+ * cost envelope as a plain `useFrameProcessor` from
29
+ * `react-native-vision-camera`. The lib's own first-party
30
+ * stitching plugin runs alongside on the same producer-thread
31
+ * runtime (composition is handled by vision-camera's own dispatch
32
+ * order).
33
+ *
34
+ * Your worklet receives whatever vision-camera delivers — vc's raw
35
+ * `Frame`. This is a structural subset of `StitcherFrame`: the
36
+ * vc-shaped fields (`width`, `height`, `pixelFormat`, `orientation`,
37
+ * `timestamp`, `toArrayBuffer`) are guaranteed; the
38
+ * `StitcherFrame`-only fields (`source`, `pose`, `arDepth`,
39
+ * `arAnchors`, `arTrackingState`) are **undefined** at runtime
40
+ * because the lib does NOT wrap or augment vc's `Frame` in Phase 4a
41
+ * (cross-worklet-boundary field injection is Phase 4b work).
42
+ * Worklets that need to read `source` / `pose` MUST guard for
43
+ * `undefined`:
44
+ *
45
+ * ```ts
46
+ * if (frame.source === 'ar') { ... } // false in non-AR mode
47
+ * if (frame.pose) { ... } // skipped in non-AR mode
48
+ * ```
49
+ *
50
+ * **AR mode — iOS Phase 4b.i (this release):** the worklet is
51
+ * installed into the native registry via
52
+ * `globalThis.__stitcherProxy.install(workletFn)`, where
53
+ * `__stitcherProxy` is a JSI host object installed at lib
54
+ * bootstrap by the native `StitcherJsiInstaller` module. The
55
+ * AR worklet runtime (`RNSARWorkletRuntime`) reads from the
56
+ * native registry on each `dispatchFrame:pose:` call and fans
57
+ * out invocations — your worklet fires alongside the lib's
58
+ * first-party stitching path.
59
+ *
60
+ * **AR mode — Android Phase 4b.ii (deferred):** the native
61
+ * installer + JNI bridge from `StitcherWorkletRuntime.kt`'s
62
+ * `runFirstParty {...}` path to a parallel C++ registry land in
63
+ * a follow-up release. Until then, on Android the hook falls
64
+ * back to the JS-side `StitcherWorkletRegistry`; AR-mode host
65
+ * worklets register but do not invoke. No regression vs.
66
+ * Phase 4a; iOS gets the API first.
67
+ *
68
+ * ### When Phase 4b.ii lands (Android)
69
+ *
70
+ * The hook's call signature does NOT change. Android hosts that
71
+ * write code today against this API will see their worklets
72
+ * start firing in AR mode automatically when Phase 4b.ii is
73
+ * merged. No migration required.
74
+ *
75
+ * ## Frame contract
76
+ *
77
+ * The worklet receives a {@link StitcherFrame} (see
78
+ * `src/stitching/StitcherFrame.ts` for the full contract +
79
+ * lifecycle). Highlights:
80
+ *
81
+ * - **`source`** discriminator: `'vc'` or `'ar'`. Branch on this
82
+ * before reading `arDepth` / `arAnchors` / `arTrackingState`
83
+ * so non-AR captures don't break.
84
+ * - **`pose`** always present. `pose.translation` is `undefined`
85
+ * in non-AR mode (gyro provides only rotation; no spatial
86
+ * anchor).
87
+ * - **Buffer lifetime**: pixel data is valid only for the
88
+ * duration of the worklet call. Worklets that need to retain
89
+ * data must `toArrayBuffer()` synchronously inside the
90
+ * worklet body — returning a reference and reading it later
91
+ * reads freed memory.
92
+ *
93
+ * ## Threading
94
+ *
95
+ * The worklet runs on the producer thread (vision-camera's
96
+ * runtime in non-AR mode; the AR-session callback thread under
97
+ * Phase 4b). Worklets MUST NOT block the producer thread for
98
+ * more than a few ms — the next frame's processing is gated on
99
+ * the previous frame returning. Long work belongs on a queue
100
+ * crossed via Reanimated / worklets-core's `runOnJS`.
101
+ *
102
+ * @param worklet The host's frame processor function. Must be
103
+ * `'worklet'`-prefixed at the call site. TS
104
+ * cannot enforce the prefix; the runtime will
105
+ * throw at attempt to invoke a non-worklet
106
+ * function.
107
+ * @param deps Standard React deps array. When `deps` change,
108
+ * the previous registration is removed and the
109
+ * new worklet is registered. Same semantics as
110
+ * vision-camera's `useFrameProcessor`.
111
+ *
112
+ * @returns A vision-camera frame-processor object that
113
+ * `<Camera frameProcessor={...}>` accepts. In non-AR
114
+ * mode this is what drives the per-frame worklet
115
+ * invocation; in AR mode it's currently a no-op (vc
116
+ * isn't mounted in AR mode anyway).
117
+ */
118
+ export declare function useFrameProcessor(worklet: StitcherFrameProcessor, deps: DependencyList): ReadonlyFrameProcessor | DrawableFrameProcessor;
119
+ //# sourceMappingURL=useFrameProcessor.d.ts.map
@@ -0,0 +1,196 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.useFrameProcessor = useFrameProcessor;
5
+ const react_1 = require("react");
6
+ const react_native_vision_camera_1 = require("react-native-vision-camera");
7
+ const ensureStitcherProxyInstalled_1 = require("./ensureStitcherProxyInstalled");
8
+ const StitcherWorkletRegistry_1 = require("./StitcherWorkletRegistry");
9
+ /**
10
+ * v0.8.0 Phase 4a — public hook for hosts to attach a per-frame
11
+ * worklet that runs in BOTH AR and non-AR capture modes.
12
+ *
13
+ * ## Quick start
14
+ *
15
+ * ```tsx
16
+ * import { useFrameProcessor, type StitcherFrame } from 'react-native-image-stitcher';
17
+ *
18
+ * function MyOcrOverlay() {
19
+ * const processor = useFrameProcessor((frame: StitcherFrame) => {
20
+ * 'worklet';
21
+ * // Pixel data is in `frame.toArrayBuffer()`.
22
+ * // AR-only fields: `frame.arDepth`, `frame.arAnchors`, `frame.arTrackingState`.
23
+ * // Discriminate via `frame.source === 'ar'` / `'vc'`.
24
+ * }, []);
25
+ * return <Camera frameProcessor={processor} ... />;
26
+ * }
27
+ * ```
28
+ *
29
+ * ## Two behaviours, depending on mode
30
+ *
31
+ * **Non-AR mode (today, fully working):** the worklet runs on
32
+ * vision-camera's Frame Processor runtime. Same thread + same
33
+ * cost envelope as a plain `useFrameProcessor` from
34
+ * `react-native-vision-camera`. The lib's own first-party
35
+ * stitching plugin runs alongside on the same producer-thread
36
+ * runtime (composition is handled by vision-camera's own dispatch
37
+ * order).
38
+ *
39
+ * Your worklet receives whatever vision-camera delivers — vc's raw
40
+ * `Frame`. This is a structural subset of `StitcherFrame`: the
41
+ * vc-shaped fields (`width`, `height`, `pixelFormat`, `orientation`,
42
+ * `timestamp`, `toArrayBuffer`) are guaranteed; the
43
+ * `StitcherFrame`-only fields (`source`, `pose`, `arDepth`,
44
+ * `arAnchors`, `arTrackingState`) are **undefined** at runtime
45
+ * because the lib does NOT wrap or augment vc's `Frame` in Phase 4a
46
+ * (cross-worklet-boundary field injection is Phase 4b work).
47
+ * Worklets that need to read `source` / `pose` MUST guard for
48
+ * `undefined`:
49
+ *
50
+ * ```ts
51
+ * if (frame.source === 'ar') { ... } // false in non-AR mode
52
+ * if (frame.pose) { ... } // skipped in non-AR mode
53
+ * ```
54
+ *
55
+ * **AR mode — iOS Phase 4b.i (this release):** the worklet is
56
+ * installed into the native registry via
57
+ * `globalThis.__stitcherProxy.install(workletFn)`, where
58
+ * `__stitcherProxy` is a JSI host object installed at lib
59
+ * bootstrap by the native `StitcherJsiInstaller` module. The
60
+ * AR worklet runtime (`RNSARWorkletRuntime`) reads from the
61
+ * native registry on each `dispatchFrame:pose:` call and fans
62
+ * out invocations — your worklet fires alongside the lib's
63
+ * first-party stitching path.
64
+ *
65
+ * **AR mode — Android Phase 4b.ii (deferred):** the native
66
+ * installer + JNI bridge from `StitcherWorkletRuntime.kt`'s
67
+ * `runFirstParty {...}` path to a parallel C++ registry land in
68
+ * a follow-up release. Until then, on Android the hook falls
69
+ * back to the JS-side `StitcherWorkletRegistry`; AR-mode host
70
+ * worklets register but do not invoke. No regression vs.
71
+ * Phase 4a; iOS gets the API first.
72
+ *
73
+ * ### When Phase 4b.ii lands (Android)
74
+ *
75
+ * The hook's call signature does NOT change. Android hosts that
76
+ * write code today against this API will see their worklets
77
+ * start firing in AR mode automatically when Phase 4b.ii is
78
+ * merged. No migration required.
79
+ *
80
+ * ## Frame contract
81
+ *
82
+ * The worklet receives a {@link StitcherFrame} (see
83
+ * `src/stitching/StitcherFrame.ts` for the full contract +
84
+ * lifecycle). Highlights:
85
+ *
86
+ * - **`source`** discriminator: `'vc'` or `'ar'`. Branch on this
87
+ * before reading `arDepth` / `arAnchors` / `arTrackingState`
88
+ * so non-AR captures don't break.
89
+ * - **`pose`** always present. `pose.translation` is `undefined`
90
+ * in non-AR mode (gyro provides only rotation; no spatial
91
+ * anchor).
92
+ * - **Buffer lifetime**: pixel data is valid only for the
93
+ * duration of the worklet call. Worklets that need to retain
94
+ * data must `toArrayBuffer()` synchronously inside the
95
+ * worklet body — returning a reference and reading it later
96
+ * reads freed memory.
97
+ *
98
+ * ## Threading
99
+ *
100
+ * The worklet runs on the producer thread (vision-camera's
101
+ * runtime in non-AR mode; the AR-session callback thread under
102
+ * Phase 4b). Worklets MUST NOT block the producer thread for
103
+ * more than a few ms — the next frame's processing is gated on
104
+ * the previous frame returning. Long work belongs on a queue
105
+ * crossed via Reanimated / worklets-core's `runOnJS`.
106
+ *
107
+ * @param worklet The host's frame processor function. Must be
108
+ * `'worklet'`-prefixed at the call site. TS
109
+ * cannot enforce the prefix; the runtime will
110
+ * throw at attempt to invoke a non-worklet
111
+ * function.
112
+ * @param deps Standard React deps array. When `deps` change,
113
+ * the previous registration is removed and the
114
+ * new worklet is registered. Same semantics as
115
+ * vision-camera's `useFrameProcessor`.
116
+ *
117
+ * @returns A vision-camera frame-processor object that
118
+ * `<Camera frameProcessor={...}>` accepts. In non-AR
119
+ * mode this is what drives the per-frame worklet
120
+ * invocation; in AR mode it's currently a no-op (vc
121
+ * isn't mounted in AR mode anyway).
122
+ */
123
+ function useFrameProcessor(worklet, deps) {
124
+ // Non-AR path: delegate to vision-camera's hook. The returned
125
+ // processor object is what `<Camera>` hands to vc. Worklet
126
+ // fires on vc's producer-thread runtime.
127
+ //
128
+ // Cast rationale: vc's hook expects `(frame: Frame) => void`.
129
+ // Our worklet is typed `(frame: StitcherFrame) => void`.
130
+ // `StitcherFrame` is a structural superset of `Frame` (it adds
131
+ // required `source` + `pose` and the optional AR fields), so
132
+ // assigning a function that consumes `StitcherFrame` to a
133
+ // `Frame`-consuming slot is unsound at the type level — TS is
134
+ // right to reject the direct assignment. At RUNTIME the worklet
135
+ // will see vc's raw `Frame`; the `source` / `pose` / AR fields
136
+ // are undefined (the hook's docstring above documents this and
137
+ // tells hosts to guard). We double-cast through `unknown` to
138
+ // suppress, accepting the explicit type-system gap as the price
139
+ // of Phase 4a's pre-Phase-4b deferral on cross-runtime frame
140
+ // wrapping.
141
+ const vcProcessor = (0, react_native_vision_camera_1.useFrameProcessor)(worklet, deps);
142
+ // AR path: install into the native registry if available (iOS
143
+ // Phase 4b.i — and Android Phase 4b.ii once it lands). Falls
144
+ // back to the JS-side `StitcherWorkletRegistry` when the native
145
+ // installer isn't present (Android in 4b.i; remote debug mode;
146
+ // unit tests). The fallback path matches Phase 4a's
147
+ // register-but-not-invoke semantics.
148
+ //
149
+ // eslint-disable-next-line react-hooks/exhaustive-deps
150
+ (0, react_1.useEffect)(() => {
151
+ const nativeReady = (0, ensureStitcherProxyInstalled_1.ensureStitcherProxyInstalled)();
152
+ if (nativeReady &&
153
+ typeof globalThis.__stitcherProxy !== 'undefined') {
154
+ // Native path — install through the JSI proxy. Errors here
155
+ // most commonly mean the worklet doesn't have the
156
+ // `'worklet'` directive at the call site (the worklets-core
157
+ // babel plugin didn't transform it). Surface them via the
158
+ // proxy's own throw with a host-side log so the failure is
159
+ // obvious.
160
+ let id;
161
+ try {
162
+ id = globalThis.__stitcherProxy.install(worklet);
163
+ }
164
+ catch (err) {
165
+ // Guard `__DEV__` read so the hook works in any environment
166
+ // that imports it without defining the flag (jest, SSR,
167
+ // custom tooling).
168
+ if (typeof __DEV__ !== 'undefined' && __DEV__) {
169
+ console.error('[react-native-image-stitcher] __stitcherProxy.install ' +
170
+ 'threw — is the worklet function decorated with ' +
171
+ "`'worklet';` and processed by react-native-worklets-core's " +
172
+ 'babel plugin? Original error: ' +
173
+ String(err));
174
+ }
175
+ return; // No cleanup needed — nothing was installed.
176
+ }
177
+ return () => {
178
+ try {
179
+ globalThis.__stitcherProxy.uninstall(id);
180
+ }
181
+ catch {
182
+ // Uninstall is best-effort; an exception here means the
183
+ // proxy was already gone (e.g., app reload mid-cleanup).
184
+ }
185
+ };
186
+ }
187
+ // Fallback — JS-side registry. Same as Phase 4a.
188
+ const jsId = StitcherWorkletRegistry_1.StitcherWorkletRegistry.register({
189
+ worklet,
190
+ isFirstParty: false,
191
+ });
192
+ return () => StitcherWorkletRegistry_1.StitcherWorkletRegistry.unregister(jsId);
193
+ }, deps);
194
+ return vcProcessor;
195
+ }
196
+ //# sourceMappingURL=useFrameProcessor.js.map
@@ -0,0 +1,34 @@
1
+ import { useThrottledFrameProcessor } from './useThrottledFrameProcessor';
2
+ import type { FrameStreamOptions, SampledFrame } from '../types';
3
+ /**
4
+ * `useFrameStream` — Layer 3. See module docstring for the full
5
+ * design + use-case mapping. Quick start:
6
+ *
7
+ * ```tsx
8
+ * import { Camera, useFrameStream } from 'react-native-image-stitcher';
9
+ *
10
+ * function MyScreen() {
11
+ * const fp = useFrameStream(
12
+ * { sampleHz: 2, quality: 75 },
13
+ * (sample) => {
14
+ * setThumbnail(sample.jpegPath);
15
+ * },
16
+ * );
17
+ * return <Camera frameProcessor={fp} ... />;
18
+ * }
19
+ * ```
20
+ *
21
+ * @param options `{ sampleHz, quality?, outputDir? }`. `sampleHz`
22
+ * clamped to `[0.5, 10]`.
23
+ * @param handler JS-thread callback fired per sample. Receives a
24
+ * `SampledFrame`. May return a Promise; rejections
25
+ * are caught + logged (not re-thrown) so one
26
+ * misbehaving handler doesn't break the stream.
27
+ *
28
+ * @returns A `useFrameProcessor`-shaped processor object — pass to
29
+ * `<Camera frameProcessor={...}>` for non-AR mode wiring.
30
+ * (AR mode auto-registration via `__stitcherProxy` is
31
+ * handled inside `useFrameProcessor`.)
32
+ */
33
+ export declare function useFrameStream(options: FrameStreamOptions, handler: (sample: SampledFrame) => void | Promise<void>): ReturnType<typeof useThrottledFrameProcessor>;
34
+ //# sourceMappingURL=useFrameStream.d.ts.map