Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snap/react-camera-kit",
"version": "0.4.0",
"version": "0.5.0",
"description": "React Camera Kit for web applications",
"type": "module",
"main": "./dist/cjs/index.js",
Expand Down
51 changes: 50 additions & 1 deletion src/internal/sourceUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,56 @@ describe("sourceUtils", () => {
videoElement.dispatchEvent(new Event("canplay"));
await promise;

expect(mockCreateVideoSource).toHaveBeenCalledWith(videoElement, undefined);
// createVideoSource is always called with an options object; trackingData is just undefined here.
const options = mockCreateVideoSource.mock.calls[0]?.[1];
expect(options?.trackingData).toBeUndefined();
});

it("should pass fpsLimit through", async () => {
const source = { kind: "video" as const, url: "https://example.com/video.mp4", fpsLimit: 30 };

const promise = createCameraKitSource(source);
videoElement.dispatchEvent(new Event("canplay"));
await promise;

expect(mockCreateVideoSource.mock.calls[0]?.[1]?.fpsLimit).toBe(30);
});

it("should pass cameraFacing through as cameraType", async () => {
const source = {
kind: "video" as const,
url: "https://example.com/video.mp4",
cameraFacing: "environment" as const,
};

const promise = createCameraKitSource(source);
videoElement.dispatchEvent(new Event("canplay"));
await promise;

expect(mockCreateVideoSource).toHaveBeenCalledWith(videoElement, { cameraType: "environment" });
});

it("should combine cameraFacing and tracking data", async () => {
const buffer = new ArrayBuffer(8);
(globalThis as { fetch?: typeof fetch }).fetch = jest
.fn()
.mockResolvedValue({ ok: true, arrayBuffer: () => Promise.resolve(buffer) }) as typeof fetch;

const source = {
kind: "video" as const,
url: "https://example.com/video.mp4",
trackingDataUrl: "https://example.com/clip.td",
cameraFacing: "environment" as const,
};

const promise = createCameraKitSource(source);
videoElement.dispatchEvent(new Event("canplay"));
await promise;

expect(mockCreateVideoSource).toHaveBeenCalledWith(videoElement, {
trackingData: buffer,
cameraType: "environment",
});
});

it("should reject when tracking data fails to load", async () => {
Expand Down
8 changes: 7 additions & 1 deletion src/internal/sourceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export async function createCameraKitSource(source: SourceInput): Promise<Source
videoUrl: source.url,
autoplay: source.autoplay,
trackingDataUrl: source.trackingDataUrl,
cameraFacing: source.cameraFacing,
fpsLimit: source.fpsLimit,
});
} else if (source.kind === "image") {
return createCameraKitImageSource({
Expand Down Expand Up @@ -110,10 +112,14 @@ function createCameraKitVideoSource({
videoUrl,
autoplay,
trackingDataUrl,
cameraFacing,
Comment thread
msilivonik-sc marked this conversation as resolved.
fpsLimit,
}: {
videoUrl: string;
autoplay?: boolean;
trackingDataUrl?: string;
cameraFacing?: CameraFacing;
fpsLimit?: number;
}) {
return new Promise<SourceApplication>((res, rej) => {
autoplay = autoplay ?? true;
Expand All @@ -136,7 +142,7 @@ function createCameraKitVideoSource({
const trackingData = trackingDataUrl ? await fetchTrackingData(trackingDataUrl) : undefined;
if (autoplay) await videoInput.play();
res({
cameraKitSource: createVideoSource(videoInput, trackingData ? { trackingData } : undefined),
cameraKitSource: createVideoSource(videoInput, { trackingData, cameraType: cameraFacing, fpsLimit }),
transform: Transform2D.Identity,
inputSize: [videoInput.videoWidth, videoInput.videoHeight],
initializedSourceInput: {
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ export type VideoSourceInput = {
* previewing world-facing lenses from a recorded environment.
*/
trackingDataUrl?: string;
/**
* Front/rear camera semantic the video should be treated as. Passed to CameraKit as `cameraType`,
* which affects Lens feature behavior such as surface tracking. Defaults to "user".
* "user" is front-facing; "environment" is rear-facing.
*/
cameraFacing?: CameraFacing;
/**
* Optional cap on frames per second processed from the video. Passed to `createVideoSource` as
* `fpsLimit`. Defaults to the video's native frame rate when omitted.
*/
fpsLimit?: number;
};
export type ImageSourceInput = { kind: "image"; url: string };

Expand Down
Loading