react-native-roku 0.0.0-stage → 0.2.0-preview.3
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 +328 -0
- package/CONTRIBUTING.md +34 -0
- package/LICENSE +21 -0
- package/NOTICE.md +10 -0
- package/README.md +66 -2
- package/bin/react-native-roku.mjs +91 -0
- package/build/assets.cjs +311 -0
- package/build/assets.d.cts +50 -0
- package/build/compatibility.d.mts +40 -0
- package/build/compatibility.mjs +272 -0
- package/build/dev.d.mts +34 -0
- package/build/dev.mjs +297 -0
- package/build/devWorkspace.d.mts +11 -0
- package/build/devWorkspace.mjs +86 -0
- package/build/device.d.mts +47 -0
- package/build/device.mjs +310 -0
- package/build/environment.d.mts +29 -0
- package/build/environment.mjs +144 -0
- package/build/esbuild.d.mts +5 -0
- package/build/esbuild.mjs +118 -0
- package/build/fontVariants.d.mts +27 -0
- package/build/fontVariants.mjs +424 -0
- package/build/frontend.d.mts +52 -0
- package/build/frontend.mjs +75 -0
- package/build/identity.mjs +45 -0
- package/build/index.d.mts +22 -0
- package/build/index.mjs +87 -0
- package/build/intl.mjs +121 -0
- package/build/metro.cjs +38 -0
- package/build/metro.d.cts +37 -0
- package/build/metroFrontend.mjs +222 -0
- package/build/metroMetadata.mjs +119 -0
- package/build/metroModules.mjs +397 -0
- package/build/metroRefresh.d.mts +64 -0
- package/build/metroRefresh.mjs +346 -0
- package/build/metroTransformer.cjs +146 -0
- package/build/nativeTextMemo.mjs +296 -0
- package/build/nativewind.d.mts +9 -0
- package/build/nativewind.mjs +92 -0
- package/build/project.d.mts +111 -0
- package/build/project.mjs +615 -0
- package/build/reactCompiler.mjs +113 -0
- package/build/reactProfile.cjs +35 -0
- package/build/refreshRuntime.mjs +187 -0
- package/build/refreshSourceMap.mjs +53 -0
- package/build/release.mjs +99 -0
- package/build/resolution.d.mts +3 -0
- package/build/resolution.mjs +25 -0
- package/build/surfaces.d.mts +1 -0
- package/build/surfaces.mjs +42 -0
- package/build/testing/node.d.mts +29 -0
- package/build/testing/node.mjs +106 -0
- package/build/yoga.d.mts +8 -0
- package/build/yoga.mjs +186 -0
- package/compatibility/public-api.json +327 -0
- package/compatibility/registry.json +2258 -0
- package/config/release.json +15 -0
- package/config/toolchain.json +34 -0
- package/docs/compatibility.md +813 -0
- package/docs/getting-started.md +460 -0
- package/docs/maintenance.md +368 -0
- package/docs/qualification.md +241 -0
- package/docs/standalone-testing.md +56 -0
- package/examples/hello-roku/README.md +35 -0
- package/examples/hello-roku/package.template.json +31 -0
- package/examples/hello-roku/pnpm-workspace.yaml +11 -0
- package/examples/hello-roku/roku.config.json +22 -0
- package/examples/hello-roku/src/App.tsx +54 -0
- package/examples/hello-roku/src/Catalog.tsx +200 -0
- package/examples/hello-roku/src/assets.d.ts +16 -0
- package/examples/hello-roku/src/catalog-cover.png +0 -0
- package/examples/hello-roku/tsconfig.json +1 -0
- package/examples/hello-roku/tsconfig.react.json +1 -0
- package/native/components/RNRAppControl.brs +215 -0
- package/native/components/RNRAppControl.xml +13 -0
- package/native/components/RNRDeviceFacts.brs +19 -0
- package/native/components/RNRDeviceFacts.xml +9 -0
- package/native/components/RNRDeviceFactsTask.brs +74 -0
- package/native/components/RNRDeviceFactsTask.xml +9 -0
- package/native/components/RNRTextRaster.brs +15 -0
- package/native/components/RNRTextRaster.xml +9 -0
- package/native/components/RNRTextRasterTask.brs +77 -0
- package/native/components/RNRTextRasterTask.xml +9 -0
- package/native/manifest.json +7 -0
- package/package.json +183 -3
- package/src/animation/clock.ts +10 -0
- package/src/animation/derivedValue.ts +163 -0
- package/src/animation/legacyAnimated.ts +255 -0
- package/src/animation/reanimated.ts +243 -0
- package/src/animation/sharedValue.ts +285 -0
- package/src/animation/worklets.ts +23 -0
- package/src/application/AppRegistry.ts +31 -0
- package/src/application/mountApplication.ts +136 -0
- package/src/graphics/BitmapLayer.tsx +68 -0
- package/src/graphics/RoundedBorder.tsx +116 -0
- package/src/graphics/TextRasterCache.ts +259 -0
- package/src/graphics/gradient.ts +37 -0
- package/src/graphics/inlineRaster.ts +85 -0
- package/src/graphics/png.ts +77 -0
- package/src/graphics/rasterPixels.ts +89 -0
- package/src/graphics/surfaces.ts +105 -0
- package/src/graphics/textRaster.types.ts +60 -0
- package/src/graphics/textRasterGeometry.ts +116 -0
- package/src/host/host.types.ts +57 -0
- package/src/index.ts +22 -0
- package/src/interop/nativewind.ts +2 -0
- package/src/layout/FontResources.ts +252 -0
- package/src/layout/affine.ts +89 -0
- package/src/layout/flexLayout.ts +398 -0
- package/src/layout/fontFaces.d.mts +19 -0
- package/src/layout/fontFaces.mjs +85 -0
- package/src/layout/fontVariants.d.mts +2 -0
- package/src/layout/fontVariants.mjs +8 -0
- package/src/layout/inlineParagraph.ts +158 -0
- package/src/layout/inlineText.ts +68 -0
- package/src/layout/layoutReuse.ts +117 -0
- package/src/layout/nativeNodeFields.ts +179 -0
- package/src/layout/preparedFields.ts +78 -0
- package/src/layout/textLayout.ts +138 -0
- package/src/layout/transform.ts +113 -0
- package/src/media/SceneGraphAudio.ts +161 -0
- package/src/media/SceneGraphVideo.ts +167 -0
- package/src/media/audioSession.ts +84 -0
- package/src/media/index.ts +2 -0
- package/src/media/videoErrorDetails.ts +40 -0
- package/src/media/videoSession.ts +136 -0
- package/src/navigation/RouterStore.ts +244 -0
- package/src/navigation/expoRouter.ts +115 -0
- package/src/react-native/NativeInput.ts +494 -0
- package/src/react-native/NativeSystemState.ts +95 -0
- package/src/react-native/accessibility.ts +265 -0
- package/src/react-native/activityIndicator.ts +72 -0
- package/src/react-native/compilerPressable.ts +26 -0
- package/src/react-native/createNativeRoot.ts +2126 -0
- package/src/react-native/directionalFocus.ts +55 -0
- package/src/react-native/environment.ts +275 -0
- package/src/react-native/flatList.ts +1001 -0
- package/src/react-native/flatListViewability.ts +231 -0
- package/src/react-native/focusEvents.ts +85 -0
- package/src/react-native/image.ts +247 -0
- package/src/react-native/index.ts +54 -0
- package/src/react-native/interactionManager.ts +76 -0
- package/src/react-native/keyboard.ts +60 -0
- package/src/react-native/linearGradient.ts +94 -0
- package/src/react-native/modal.tsx +148 -0
- package/src/react-native/nativeHostContext.ts +80 -0
- package/src/react-native/nativeModules.ts +86 -0
- package/src/react-native/nativeRoot.types.ts +22 -0
- package/src/react-native/nativeViewHandle.ts +191 -0
- package/src/react-native/nativewindPressable.ts +38 -0
- package/src/react-native/panResponder.ts +73 -0
- package/src/react-native/primitives.ts +685 -0
- package/src/react-native/safeArea.ts +76 -0
- package/src/react-native/scrollView.ts +289 -0
- package/src/react-native/styleInterop.ts +8 -0
- package/src/react-native/styleSheet.ts +54 -0
- package/src/react-native/textInput.ts +334 -0
- package/src/react-native/unsupported.ts +25 -0
- package/src/renderer/SceneRoot.ts +1006 -0
- package/src/scene/SdkDeviceFacts.ts +151 -0
- package/src/scene/SdkDeviceServices.ts +72 -0
- package/src/scene/SdkSceneGraph.ts +124 -0
- package/src/scene/SdkTextEntry.ts +321 -0
- package/src/scene/SdkTextMetrics.ts +136 -0
- package/src/scene/SdkTextRasterTransport.ts +191 -0
- package/src/scene/SdkWebTransport.ts +122 -0
- package/src/scene/createRokuHost.ts +213 -0
- package/src/scene/sceneGraph.types.ts +38 -0
- package/src/services/DeviceServices.ts +53 -0
- package/src/services/RegistryStorage.ts +85 -0
- package/src/services/RokuKeySession.ts +29 -0
- package/src/services/asyncStorage.ts +23 -0
- package/src/services/base64.ts +52 -0
- package/src/services/crypto.ts +18 -0
- package/src/services/expoConstants.ts +11 -0
- package/src/services/expoLinking.ts +12 -0
- package/src/services/expoStatusBar.ts +4 -0
- package/src/services/installWebRuntime.ts +105 -0
- package/src/services/intlEnglish.ts +54 -0
- package/src/services/languageRuntime.ts +65 -0
- package/src/services/nativeTimers.ts +96 -0
- package/src/services/secureStore.ts +23 -0
- package/src/services/textEntryRequest.ts +22 -0
- package/src/services/utf8.ts +57 -0
- package/src/services/webRuntime.ts +346 -0
- package/src/testing/CanvasResources.ts +141 -0
- package/src/testing/CanvasSceneGraph.ts +496 -0
- package/src/testing/CanvasTextEntry.ts +237 -0
- package/src/testing/PreviewTextObservations.ts +82 -0
- package/src/testing/RecordingSceneGraph.ts +136 -0
- package/src/types/normalize-colors.d.ts +4 -0
- package/src/vector/dynamicSvg.tsx +226 -0
- package/src/vector/svg.ts +222 -0
- package/src/vector/svg.types.ts +4 -0
- package/src/vector/svgElements.ts +58 -0
- package/src/vector/svgKey.d.mts +4 -0
- package/src/vector/svgKey.mjs +16 -0
- package/src/vector/svgPath.ts +157 -0
- package/src/vector/svgPixels.ts +133 -0
- package/tsconfig.consumer.json +29 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { RokuHostError, type SceneFields } from "../scene/sceneGraph.types";
|
|
2
|
+
import type { LayoutRect } from "./flexLayout";
|
|
3
|
+
|
|
4
|
+
/** Compose RN's ordered 2D transforms, then admit only matrices RSG can represent exactly. */
|
|
5
|
+
export function sceneTransform(value: unknown, rect: LayoutRect, origin?: unknown): SceneFields {
|
|
6
|
+
if (!Array.isArray(value))
|
|
7
|
+
throw new RokuHostError("C03_TRANSFORM", "Transform must be an ordered array");
|
|
8
|
+
let a = 1,
|
|
9
|
+
b = 0,
|
|
10
|
+
c = 0,
|
|
11
|
+
d = 1,
|
|
12
|
+
x = 0,
|
|
13
|
+
y = 0;
|
|
14
|
+
for (const operation of value) {
|
|
15
|
+
if (!operation || typeof operation !== "object" || Object.keys(operation).length !== 1)
|
|
16
|
+
throw new RokuHostError("C03_TRANSFORM", "Each transform needs one operation");
|
|
17
|
+
const [key, operand] = Object.entries(operation)[0];
|
|
18
|
+
let na = 1,
|
|
19
|
+
nb = 0,
|
|
20
|
+
nc = 0,
|
|
21
|
+
nd = 1,
|
|
22
|
+
nx = 0,
|
|
23
|
+
ny = 0;
|
|
24
|
+
if (typeof operand === "number" && Number.isFinite(operand)) {
|
|
25
|
+
if (key === "translateX") nx = operand;
|
|
26
|
+
else if (key === "translateY") ny = operand;
|
|
27
|
+
else if (key === "scale") na = nd = operand;
|
|
28
|
+
else if (key === "scaleX") na = operand;
|
|
29
|
+
else if (key === "scaleY") nd = operand;
|
|
30
|
+
else throw new RokuHostError("C03_TRANSFORM", `Transform ${key} needs an implementation`);
|
|
31
|
+
} else if (
|
|
32
|
+
(key === "rotate" || key === "rotateZ") &&
|
|
33
|
+
typeof operand === "string" &&
|
|
34
|
+
/^-?(?:\d+(?:\.\d+)?|\.\d+)(?:deg|rad)$/.test(operand)
|
|
35
|
+
) {
|
|
36
|
+
const angle = Number.parseFloat(operand) * (operand.endsWith("deg") ? Math.PI / 180 : 1);
|
|
37
|
+
na = nd = Math.cos(angle);
|
|
38
|
+
nb = Math.sin(angle);
|
|
39
|
+
nc = -nb;
|
|
40
|
+
} else
|
|
41
|
+
throw new RokuHostError(
|
|
42
|
+
"C03_TRANSFORM",
|
|
43
|
+
`Transform ${key} is not a finite supported 2D operation`,
|
|
44
|
+
);
|
|
45
|
+
const nextX = a * nx + c * ny + x,
|
|
46
|
+
nextY = b * nx + d * ny + y;
|
|
47
|
+
[a, b, c, d, x, y] = [
|
|
48
|
+
a * na + c * nb,
|
|
49
|
+
b * na + d * nb,
|
|
50
|
+
a * nc + c * nd,
|
|
51
|
+
b * nc + d * nd,
|
|
52
|
+
nextX,
|
|
53
|
+
nextY,
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
const first = Math.hypot(a, b),
|
|
57
|
+
second = Math.hypot(c, d),
|
|
58
|
+
determinant = a * d - b * c;
|
|
59
|
+
if (Math.abs(a * c + b * d) > 1e-8 * Math.max(1, first * second))
|
|
60
|
+
throw new RokuHostError(
|
|
61
|
+
"C03_TRANSFORM_SHEAR",
|
|
62
|
+
"This ordered transform requires nested RSG groups to preserve shear",
|
|
63
|
+
);
|
|
64
|
+
const rotation = first > 1e-12 ? Math.atan2(b, a) : second > 1e-12 ? Math.atan2(-c, d) : 0;
|
|
65
|
+
const sx = first > 1e-12 ? first : second > 1e-12 ? determinant / second : 0;
|
|
66
|
+
const sy = first > 1e-12 ? determinant / first : second;
|
|
67
|
+
return {
|
|
68
|
+
translation: [rect.x + x, rect.y + y],
|
|
69
|
+
scale: [sx, sy],
|
|
70
|
+
rotation,
|
|
71
|
+
scaleRotateCenter: transformOrigin(origin, rect.width, rect.height),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** RN's two-dimensional CSS/array origin, in the view's local coordinates. */
|
|
76
|
+
export function transformOrigin(
|
|
77
|
+
value: unknown,
|
|
78
|
+
width: number,
|
|
79
|
+
height: number,
|
|
80
|
+
): readonly [number, number] {
|
|
81
|
+
if (value == null) return [width / 2, height / 2];
|
|
82
|
+
const parts: unknown[] =
|
|
83
|
+
typeof value === "string" ? value.trim().split(/\s+/) : Array.isArray(value) ? [...value] : [];
|
|
84
|
+
if (parts.length < 1 || parts.length > 3)
|
|
85
|
+
throw new RokuHostError(
|
|
86
|
+
"C03_TRANSFORM_ORIGIN",
|
|
87
|
+
"Transform origin requires two planar coordinates",
|
|
88
|
+
);
|
|
89
|
+
if (parts.length === 3 && ![0, "0", "0px"].includes(parts.pop() as never))
|
|
90
|
+
throw new RokuHostError(
|
|
91
|
+
"C03_TRANSFORM_ORIGIN",
|
|
92
|
+
"A nonzero transform origin depth requires a 3D renderer",
|
|
93
|
+
);
|
|
94
|
+
if (parts.length === 1) {
|
|
95
|
+
if (["top", "bottom"].includes(String(parts[0]))) parts.unshift("center");
|
|
96
|
+
else parts.push("center");
|
|
97
|
+
}
|
|
98
|
+
if (["top", "bottom"].includes(String(parts[0])) || ["left", "right"].includes(String(parts[1])))
|
|
99
|
+
parts.reverse();
|
|
100
|
+
const coordinate = (part: unknown, size: number, start: string, end: string) => {
|
|
101
|
+
if (part === start) return 0;
|
|
102
|
+
if (part === end) return size;
|
|
103
|
+
if (part === "center") return size / 2;
|
|
104
|
+
if (typeof part === "number" && Number.isFinite(part)) return part;
|
|
105
|
+
if (typeof part === "string" && /^-?(?:\d+(?:\.\d+)?|\.\d+)(?:%|px)?$/.test(part))
|
|
106
|
+
return Number.parseFloat(part) * (part.endsWith("%") ? size / 100 : 1);
|
|
107
|
+
throw new RokuHostError("C03_TRANSFORM_ORIGIN", "Unsupported transform origin coordinate");
|
|
108
|
+
};
|
|
109
|
+
return [
|
|
110
|
+
coordinate(parts[0], width, "left", "right"),
|
|
111
|
+
coordinate(parts[1], height, "top", "bottom"),
|
|
112
|
+
];
|
|
113
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { createElement, forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from "react";
|
|
2
|
+
import { useElement } from "../react-native/primitives";
|
|
3
|
+
import { useNativeHost } from "../react-native/nativeHostContext";
|
|
4
|
+
import type { SceneElementProps } from "../renderer/SceneRoot";
|
|
5
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
6
|
+
import { createAudioSession, type AudioObservation } from "./audioSession";
|
|
7
|
+
export type { AudioObservation, AudioSnapshot, AudioState } from "./audioSession";
|
|
8
|
+
|
|
9
|
+
export interface SceneAudioSource {
|
|
10
|
+
readonly uri: string;
|
|
11
|
+
readonly format: "mp3" | "mp4" | "flac" | "hls" | "es.aac-adts";
|
|
12
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
13
|
+
}
|
|
14
|
+
export interface SceneGraphAudioRef {
|
|
15
|
+
seek(time: number): void;
|
|
16
|
+
pause(): void;
|
|
17
|
+
resume(): void;
|
|
18
|
+
stop(): void;
|
|
19
|
+
setMuted(muted: boolean): void;
|
|
20
|
+
getCurrentPosition(): Promise<number>;
|
|
21
|
+
}
|
|
22
|
+
export interface SceneGraphAudioProps {
|
|
23
|
+
readonly source: SceneAudioSource;
|
|
24
|
+
readonly paused?: boolean;
|
|
25
|
+
readonly muted?: boolean;
|
|
26
|
+
readonly repeat?: boolean;
|
|
27
|
+
/** Milliseconds, matching SceneGraphVideo. Zero disables position notifications. */
|
|
28
|
+
readonly progressUpdateInterval?: number;
|
|
29
|
+
readonly onObservation?: (event: AudioObservation) => void;
|
|
30
|
+
/** Host/command failures; native decoder errors arrive through onObservation. */
|
|
31
|
+
readonly onError?: (error: Error) => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function validateSource(source: SceneAudioSource) {
|
|
35
|
+
if (!/^(https?:\/\/|pkg:\/)[^\s]+$/.test(source.uri))
|
|
36
|
+
throw new RokuHostError("C31_AUDIO_URI", "Audio requires an HTTP or packaged media URI");
|
|
37
|
+
if (!["mp3", "mp4", "flac", "hls", "es.aac-adts"].includes(source.format))
|
|
38
|
+
throw new RokuHostError(
|
|
39
|
+
"C31_AUDIO_FORMAT",
|
|
40
|
+
"Audio requires an explicit supported stream format",
|
|
41
|
+
);
|
|
42
|
+
return Object.entries(source.headers ?? {}).map(([key, value]) => {
|
|
43
|
+
if (
|
|
44
|
+
!/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(key) ||
|
|
45
|
+
typeof value !== "string" ||
|
|
46
|
+
/[\r\n]/.test(value)
|
|
47
|
+
)
|
|
48
|
+
throw new RokuHostError("C31_AUDIO_HEADER", "Invalid audio request header");
|
|
49
|
+
return `${key}: ${value}`;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const MountedSceneGraphAudio = forwardRef<SceneGraphAudioRef, SceneGraphAudioProps>(
|
|
54
|
+
(props, ref) => {
|
|
55
|
+
const host = useNativeHost(),
|
|
56
|
+
element = useElement({}, null);
|
|
57
|
+
const latest = useRef(props);
|
|
58
|
+
latest.current = props;
|
|
59
|
+
const session = useMemo(
|
|
60
|
+
() => createAudioSession((event) => latest.current.onObservation?.(event)),
|
|
61
|
+
[],
|
|
62
|
+
);
|
|
63
|
+
const alive = useRef(false),
|
|
64
|
+
generation = useRef(0);
|
|
65
|
+
const command = (fields: Readonly<Record<string, string | number | boolean>>) => {
|
|
66
|
+
const owner = generation.current;
|
|
67
|
+
if (alive.current && host.active && host.placement(element.id)?.handle.nativeRef) {
|
|
68
|
+
host.audioCommand(element.id, fields);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
void host
|
|
72
|
+
.whenCommitted()
|
|
73
|
+
.then(() => {
|
|
74
|
+
if (alive.current && host.active && owner === generation.current)
|
|
75
|
+
host.audioCommand(element.id, fields);
|
|
76
|
+
})
|
|
77
|
+
.catch((error) => {
|
|
78
|
+
if (alive.current && owner === generation.current)
|
|
79
|
+
latest.current.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
useImperativeHandle(ref, () => ({
|
|
83
|
+
seek(time) {
|
|
84
|
+
session.seek(time);
|
|
85
|
+
command({ seek: time });
|
|
86
|
+
},
|
|
87
|
+
pause() {
|
|
88
|
+
command({ autoplayAfterSeek: false, control: "pause" });
|
|
89
|
+
},
|
|
90
|
+
resume() {
|
|
91
|
+
command({
|
|
92
|
+
autoplayAfterSeek: true,
|
|
93
|
+
control: session.snapshot.state === "paused" ? "resume" : "play",
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
stop() {
|
|
97
|
+
command({ autoplayAfterSeek: false, control: "stop" });
|
|
98
|
+
},
|
|
99
|
+
setMuted(mute) {
|
|
100
|
+
command({ mute });
|
|
101
|
+
},
|
|
102
|
+
getCurrentPosition: () => session.position(),
|
|
103
|
+
}));
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
alive.current = true;
|
|
106
|
+
session.activate();
|
|
107
|
+
return () => {
|
|
108
|
+
alive.current = false;
|
|
109
|
+
generation.current++;
|
|
110
|
+
session.retire();
|
|
111
|
+
};
|
|
112
|
+
}, [session]);
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
command({
|
|
115
|
+
autoplayAfterSeek: !props.paused,
|
|
116
|
+
control: props.paused
|
|
117
|
+
? session.snapshot.state === "none"
|
|
118
|
+
? "prebuffer"
|
|
119
|
+
: "pause"
|
|
120
|
+
: session.snapshot.state === "paused"
|
|
121
|
+
? "resume"
|
|
122
|
+
: "play",
|
|
123
|
+
});
|
|
124
|
+
}, [props.paused]);
|
|
125
|
+
const headers = validateSource(props.source);
|
|
126
|
+
const interval = props.progressUpdateInterval ?? 250;
|
|
127
|
+
if (!Number.isFinite(interval) || interval < 0)
|
|
128
|
+
throw new RokuHostError(
|
|
129
|
+
"C31_AUDIO_INTERVAL",
|
|
130
|
+
"Audio notification interval must be finite and nonnegative",
|
|
131
|
+
);
|
|
132
|
+
return createElement(
|
|
133
|
+
"audio",
|
|
134
|
+
{
|
|
135
|
+
fields: {
|
|
136
|
+
...element.fields,
|
|
137
|
+
mute: props.muted ?? false,
|
|
138
|
+
loop: props.repeat ?? false,
|
|
139
|
+
notificationInterval: interval / 1000,
|
|
140
|
+
},
|
|
141
|
+
layout: { ...element.layout, resource: true },
|
|
142
|
+
events: Object.fromEntries(
|
|
143
|
+
["state", "duration", "position", "audioFormat", "errorCode", "errorMsg"].map((field) => [
|
|
144
|
+
field,
|
|
145
|
+
(value: unknown) => session.observe(field, value),
|
|
146
|
+
]),
|
|
147
|
+
),
|
|
148
|
+
} satisfies SceneElementProps,
|
|
149
|
+
createElement("contentNode", {
|
|
150
|
+
layout: { resource: true },
|
|
151
|
+
fields: { url: props.source.uri, streamFormat: props.source.format, httpHeaders: headers },
|
|
152
|
+
}),
|
|
153
|
+
);
|
|
154
|
+
},
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
/** Nonvisual resource. Source replacement retires the old decoder and its listeners. */
|
|
158
|
+
export const SceneGraphAudio = forwardRef<SceneGraphAudioRef, SceneGraphAudioProps>((props, ref) =>
|
|
159
|
+
createElement(MountedSceneGraphAudio, { ...props, ref, key: JSON.stringify(props.source) }),
|
|
160
|
+
);
|
|
161
|
+
SceneGraphAudio.displayName = "RokuSceneGraphAudio";
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { createElement, forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from "react";
|
|
2
|
+
import type { NativeStyle } from "../react-native/styleSheet";
|
|
3
|
+
import { useElement } from "../react-native/primitives";
|
|
4
|
+
import { useNativeHost } from "../react-native/nativeHostContext";
|
|
5
|
+
import type { SceneElementProps } from "../renderer/SceneRoot";
|
|
6
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
7
|
+
import { createVideoSession, type VideoObservation } from "./videoSession";
|
|
8
|
+
|
|
9
|
+
export interface SceneVideoSource {
|
|
10
|
+
readonly uri: string;
|
|
11
|
+
readonly format: "hls" | "dash" | "mp4" | "mkv" | "ism";
|
|
12
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
13
|
+
readonly startPosition?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface SceneGraphVideoRef {
|
|
16
|
+
seek(time: number): void;
|
|
17
|
+
pause(): void;
|
|
18
|
+
resume(): void;
|
|
19
|
+
stop(): void;
|
|
20
|
+
setMuted(muted: boolean): void;
|
|
21
|
+
getCurrentPosition(): Promise<number>;
|
|
22
|
+
}
|
|
23
|
+
export interface SceneGraphVideoProps {
|
|
24
|
+
readonly source: SceneVideoSource;
|
|
25
|
+
readonly style?: NativeStyle;
|
|
26
|
+
readonly paused?: boolean;
|
|
27
|
+
readonly muted?: boolean;
|
|
28
|
+
readonly repeat?: boolean;
|
|
29
|
+
readonly progressUpdateInterval?: number;
|
|
30
|
+
readonly audioTrack?: string;
|
|
31
|
+
readonly onObservation?: (event: VideoObservation) => void;
|
|
32
|
+
readonly onAudioTracks?: (tracks: unknown) => void;
|
|
33
|
+
readonly onCurrentAudioTrack?: (id: unknown) => void;
|
|
34
|
+
readonly onError?: (error: Error) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const MountedSceneGraphVideo = forwardRef<SceneGraphVideoRef, SceneGraphVideoProps>(
|
|
38
|
+
(props, ref) => {
|
|
39
|
+
const host = useNativeHost(),
|
|
40
|
+
element = useElement({ style: props.style }, null);
|
|
41
|
+
const latest = useRef(props);
|
|
42
|
+
latest.current = props;
|
|
43
|
+
const session = useMemo(
|
|
44
|
+
() => createVideoSession((event) => latest.current.onObservation?.(event)),
|
|
45
|
+
[],
|
|
46
|
+
);
|
|
47
|
+
const alive = useRef(false);
|
|
48
|
+
const generation = useRef(0);
|
|
49
|
+
const command = (fields: Readonly<Record<string, string | number | boolean>>) => {
|
|
50
|
+
const owner = generation.current;
|
|
51
|
+
// Retirement must reach the native transaction before the OS freezes JS.
|
|
52
|
+
// Only pre-mount commands need to wait for allocation.
|
|
53
|
+
if (alive.current && host.active && host.placement(element.id)?.handle.nativeRef) {
|
|
54
|
+
host.videoCommand(element.id, fields);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
void host
|
|
58
|
+
.whenCommitted()
|
|
59
|
+
.then(() => {
|
|
60
|
+
if (alive.current && host.active && owner === generation.current)
|
|
61
|
+
host.videoCommand(element.id, fields);
|
|
62
|
+
})
|
|
63
|
+
.catch((error) => {
|
|
64
|
+
if (alive.current)
|
|
65
|
+
latest.current.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
useImperativeHandle(ref, () => ({
|
|
69
|
+
seek(time) {
|
|
70
|
+
session.seek(time);
|
|
71
|
+
command({ seek: time });
|
|
72
|
+
},
|
|
73
|
+
pause() {
|
|
74
|
+
command({ control: "pause" });
|
|
75
|
+
},
|
|
76
|
+
resume() {
|
|
77
|
+
command({
|
|
78
|
+
control: session.snapshot.state === "paused" ? "resume" : "play",
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
stop() {
|
|
82
|
+
command({ control: "stop" });
|
|
83
|
+
},
|
|
84
|
+
setMuted(mute) {
|
|
85
|
+
command({ mute });
|
|
86
|
+
},
|
|
87
|
+
getCurrentPosition: () => session.position(),
|
|
88
|
+
}));
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
alive.current = true;
|
|
91
|
+
session.activate();
|
|
92
|
+
return () => {
|
|
93
|
+
alive.current = false;
|
|
94
|
+
generation.current++;
|
|
95
|
+
session.retire();
|
|
96
|
+
};
|
|
97
|
+
}, [session]);
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
command({
|
|
100
|
+
control: props.paused
|
|
101
|
+
? session.snapshot.state === "none"
|
|
102
|
+
? "prebuffer"
|
|
103
|
+
: "pause"
|
|
104
|
+
: session.snapshot.state === "paused"
|
|
105
|
+
? "resume"
|
|
106
|
+
: "play",
|
|
107
|
+
});
|
|
108
|
+
}, [props.paused]);
|
|
109
|
+
const source = props.source;
|
|
110
|
+
if (!/^(https?|pkg):\//.test(source.uri))
|
|
111
|
+
throw new RokuHostError("C30_VIDEO_URI", "Video requires an HTTP or packaged media URI");
|
|
112
|
+
if (
|
|
113
|
+
source.startPosition !== undefined &&
|
|
114
|
+
(!Number.isFinite(source.startPosition) || source.startPosition < 0)
|
|
115
|
+
)
|
|
116
|
+
throw new RokuHostError("C30_VIDEO_START", "Invalid source start position");
|
|
117
|
+
const headers = Object.entries(source.headers ?? {}).map(([key, value]) => {
|
|
118
|
+
if (!/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(key) || /[\r\n]/.test(value))
|
|
119
|
+
throw new RokuHostError("C30_VIDEO_HEADER", "Invalid media request header");
|
|
120
|
+
return `${key}: ${value}`;
|
|
121
|
+
});
|
|
122
|
+
return createElement(
|
|
123
|
+
"video",
|
|
124
|
+
{
|
|
125
|
+
fields: {
|
|
126
|
+
...element.fields,
|
|
127
|
+
enableUI: false,
|
|
128
|
+
enableTrickPlay: false,
|
|
129
|
+
mute: props.muted ?? false,
|
|
130
|
+
loop: props.repeat ?? false,
|
|
131
|
+
suppressCaptions: true,
|
|
132
|
+
subtitleTrack: "",
|
|
133
|
+
notificationInterval: Math.max(0.1, (props.progressUpdateInterval ?? 250) / 1000),
|
|
134
|
+
...(props.audioTrack === undefined ? {} : { audioTrack: props.audioTrack }),
|
|
135
|
+
},
|
|
136
|
+
layout: element.layout,
|
|
137
|
+
events: {
|
|
138
|
+
state: (value) => session.observe("state", value),
|
|
139
|
+
duration: (value) => session.observe("duration", value),
|
|
140
|
+
position: (value) => session.observe("position", value),
|
|
141
|
+
positionInfo: (value) => session.observe("positionInfo", value),
|
|
142
|
+
errorCode: (value) => session.observe("errorCode", value),
|
|
143
|
+
errorInfo: (value) => session.observe("errorInfo", value),
|
|
144
|
+
availableAudioTracks: (value) => latest.current.onAudioTracks?.(value),
|
|
145
|
+
currentAudioTrack: (value) => latest.current.onCurrentAudioTrack?.(value),
|
|
146
|
+
},
|
|
147
|
+
} satisfies SceneElementProps,
|
|
148
|
+
createElement("contentNode", {
|
|
149
|
+
layout: { resource: true },
|
|
150
|
+
fields: {
|
|
151
|
+
url: source.uri,
|
|
152
|
+
streamFormat: source.format,
|
|
153
|
+
httpHeaders: headers,
|
|
154
|
+
...(source.startPosition === undefined ? {} : { playStart: source.startPosition }),
|
|
155
|
+
},
|
|
156
|
+
}),
|
|
157
|
+
);
|
|
158
|
+
},
|
|
159
|
+
);
|
|
160
|
+
export const SceneGraphVideo = forwardRef<SceneGraphVideoRef, SceneGraphVideoProps>((props, ref) =>
|
|
161
|
+
createElement(MountedSceneGraphVideo, {
|
|
162
|
+
...props,
|
|
163
|
+
ref,
|
|
164
|
+
key: JSON.stringify(props.source),
|
|
165
|
+
}),
|
|
166
|
+
);
|
|
167
|
+
SceneGraphVideo.displayName = "RokuSceneGraphVideo";
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
2
|
+
|
|
3
|
+
export type AudioState =
|
|
4
|
+
"none" | "buffering" | "playing" | "paused" | "stopped" | "finished" | "error";
|
|
5
|
+
export interface AudioSnapshot {
|
|
6
|
+
readonly state: AudioState;
|
|
7
|
+
readonly position?: number;
|
|
8
|
+
readonly duration?: number;
|
|
9
|
+
/** Decoder-reported format, independent of the requested stream container. */
|
|
10
|
+
readonly format?: string;
|
|
11
|
+
readonly errorCode?: number;
|
|
12
|
+
readonly errorMessage?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface AudioObservation {
|
|
15
|
+
readonly kind: "state" | "progress" | "duration" | "format" | "error";
|
|
16
|
+
readonly snapshot: AudioSnapshot;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** One content lifetime. Commands never manufacture a position, duration or codec. */
|
|
20
|
+
export function createAudioSession(notify: (event: AudioObservation) => void) {
|
|
21
|
+
let active = true;
|
|
22
|
+
let snapshot: AudioSnapshot = Object.freeze({ state: "none" });
|
|
23
|
+
const publish = (kind: AudioObservation["kind"], patch: Partial<AudioSnapshot>) => {
|
|
24
|
+
snapshot = Object.freeze({ ...snapshot, ...patch });
|
|
25
|
+
notify({ kind, snapshot });
|
|
26
|
+
};
|
|
27
|
+
const finite = (value: unknown): value is number =>
|
|
28
|
+
typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
29
|
+
return {
|
|
30
|
+
activate() {
|
|
31
|
+
active = true;
|
|
32
|
+
},
|
|
33
|
+
get snapshot() {
|
|
34
|
+
return snapshot;
|
|
35
|
+
},
|
|
36
|
+
seek(time: number) {
|
|
37
|
+
if (!active) throw new RokuHostError("C31_AUDIO_RETIRED", "The audio session is retired");
|
|
38
|
+
if (!finite(time))
|
|
39
|
+
throw new RokuHostError(
|
|
40
|
+
"C31_AUDIO_SEEK",
|
|
41
|
+
"Seek targets must be finite nonnegative seconds",
|
|
42
|
+
);
|
|
43
|
+
},
|
|
44
|
+
observe(field: string, value: unknown) {
|
|
45
|
+
if (!active) return;
|
|
46
|
+
if (
|
|
47
|
+
field === "state" &&
|
|
48
|
+
typeof value === "string" &&
|
|
49
|
+
["none", "buffering", "playing", "paused", "stopped", "finished", "error"].includes(value)
|
|
50
|
+
)
|
|
51
|
+
publish(value === "error" ? "error" : "state", { state: value as AudioState });
|
|
52
|
+
else if (field === "position" && finite(value)) publish("progress", { position: value });
|
|
53
|
+
else if (field === "duration" && finite(value)) publish("duration", { duration: value });
|
|
54
|
+
else if (
|
|
55
|
+
field === "audioFormat" &&
|
|
56
|
+
typeof value === "string" &&
|
|
57
|
+
/^[a-z0-9_]{1,32}$/.test(value)
|
|
58
|
+
)
|
|
59
|
+
publish("format", { format: value });
|
|
60
|
+
else if (
|
|
61
|
+
field === "errorCode" &&
|
|
62
|
+
typeof value === "number" &&
|
|
63
|
+
Number.isInteger(value) &&
|
|
64
|
+
value !== 0
|
|
65
|
+
)
|
|
66
|
+
publish("error", { errorCode: value });
|
|
67
|
+
else if (field === "errorMsg" && typeof value === "string" && value.length)
|
|
68
|
+
publish("error", { errorMessage: value.slice(0, 2048) });
|
|
69
|
+
},
|
|
70
|
+
position(): Promise<number> {
|
|
71
|
+
return active && snapshot.position !== undefined
|
|
72
|
+
? Promise.resolve(snapshot.position)
|
|
73
|
+
: Promise.reject(
|
|
74
|
+
new RokuHostError(
|
|
75
|
+
"C31_AUDIO_POSITION",
|
|
76
|
+
"No current native audio position has been observed",
|
|
77
|
+
),
|
|
78
|
+
);
|
|
79
|
+
},
|
|
80
|
+
retire() {
|
|
81
|
+
active = false;
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Only bounded machine fields cross the adapter. Native debug/message/source
|
|
2
|
+
* strings may contain media URLs, headers or credentials and are not retained. */
|
|
3
|
+
export interface NativeVideoErrorDetails {
|
|
4
|
+
readonly nativeCode?: number;
|
|
5
|
+
readonly internalCode?: number;
|
|
6
|
+
readonly drmCode?: number;
|
|
7
|
+
readonly category?: "http" | "drm" | "mediaerror" | "mediaplayer";
|
|
8
|
+
readonly clipId?: number;
|
|
9
|
+
readonly ignored?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const nativeErrorCode = (value: unknown): value is number =>
|
|
13
|
+
typeof value === "number" &&
|
|
14
|
+
Number.isInteger(value) &&
|
|
15
|
+
value >= -2147483648 &&
|
|
16
|
+
value <= 2147483647;
|
|
17
|
+
|
|
18
|
+
export function nativeVideoErrorInfo(value: unknown): NativeVideoErrorDetails | undefined {
|
|
19
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return;
|
|
20
|
+
const info = value as Record<string, unknown>;
|
|
21
|
+
const own = (key: string) => (Object.hasOwn(info, key) ? info[key] : undefined);
|
|
22
|
+
const result: { -readonly [K in keyof NativeVideoErrorDetails]: NativeVideoErrorDetails[K] } = {};
|
|
23
|
+
const code = own("errcode"),
|
|
24
|
+
drmCode = own("drmerrcode"),
|
|
25
|
+
clipId = own("clipId"),
|
|
26
|
+
ignored = own("ignored"),
|
|
27
|
+
category = own("category");
|
|
28
|
+
if (nativeErrorCode(code) && code !== 0) result.internalCode = code;
|
|
29
|
+
if (nativeErrorCode(drmCode) && drmCode !== 0) result.drmCode = drmCode;
|
|
30
|
+
if (nativeErrorCode(clipId) && clipId >= 0) result.clipId = clipId;
|
|
31
|
+
if (ignored === 0 || ignored === 1) result.ignored = ignored === 1;
|
|
32
|
+
if (
|
|
33
|
+
category === "http" ||
|
|
34
|
+
category === "drm" ||
|
|
35
|
+
category === "mediaerror" ||
|
|
36
|
+
category === "mediaplayer"
|
|
37
|
+
)
|
|
38
|
+
result.category = category;
|
|
39
|
+
return Object.keys(result).length ? Object.freeze(result) : undefined;
|
|
40
|
+
}
|