# Camera and video

> `@typegpu/kit` 0.1.0. If it is installed, read `node_modules/@typegpu/kit/docs/` instead.

Copy camera or video frames into a texture you own, fitted, turned and mirrored, or sample the newest frame in place without a copy.

> `import { video } from '@typegpu/kit'`: `video.create(root, { size, … }) → Feed`, `video.live(root, options?) → LiveView`. Fits camera frames into a texture, or samples them in place. **stability** unstable: may change in any way in a minor release. **records** `update` frame, `record` encoder: `update` and `record` into a render pass of their own, before the passes that read the texture. **owns** a texture, a buffer, a sampler, a pipeline, bind groups: a feed's picture, fitting, fit and each recorded frame's group; a live view's blank frame, orientation and sampler. **allocates** every `record`: a bind group for the imported frame. For a live view, each new frame: an external texture from the source and a bind group from the share that holds its part; a web video imports again every frame. **lifecycle** `initAsync()`, `destroy()`, `[Symbol.dispose]`. **injects** hook: the frame, sampler and orientation `video.sample` reads, from a live view's part. **GPU functions** `sample`. **types** `Fit`, `Rotation`, `Imported`, `Latest`, `Source`, `Crop`. `src/video.ts`; ember-mirror.
>
> `Options`, taken by `create`, `live`:
> - `size: readonly [number, number]` Sets the texture's width and height in texels.
> - `fit?: Fit` Selects how a frame of another aspect fills the texture. Default `'cover'`.
>
> `LiveOptions`, taken by `live`:
> - `crop?: Options` Crops each frame as a feed with this `size` and `fit` does, so `video.sample(uv)` reads the part of the frame the feed's texture holds at `uv`. Default the whole frame.
>
> `Feed`, from `create`:
> - `texture: ReturnType<typeof createTexture>` Holds the last frame as sRGB-encoded `rgba8unorm`, so a model reads byte values and a lit shader decodes with `srgbToLinear`.
> - `frames: number` Counts frames written.
> - `frameSize: readonly [number, number]` The last frame's width and height after rotation, or zero before the first.
> - `crop: Crop` Where the texture lies on the last frame, upright.
> - `time: number` Reports when the last frame was captured, or where it sits in a video, in seconds on its source's clock.
> - `attach(source)` Sets where `update` takes frames from, or `undefined` to stop and keep the last frame.
> - `update(frame)` Records the attached source's frame fitted into `frame.encoder` if it is new.
> - `record(target, picture)` Records `picture` fitted into the texture, in a render pass of its own in `target`, a frame or an encoder.
> - `initAsync()` Compiles the fitting pipeline.
> - `destroy()` Destroys the texture.
>
> `LiveView`, from `live`:
> - `part: bindings.Part` Provides the newest frame, its sampler and orientation for `video.sample`.
> - `frames: number` Counts new frames shown.
> - `frameSize: readonly [number, number]` The newest frame's width and height after rotation, or zero before the first.
> - `time: number` Reports when the newest frame was captured, or where it sits in a video, in seconds on its source's clock.
> - `attach(source)` Sets the source, or `undefined` to stop and show transparent black, because a live view holds no frame of its own.
> - `update(frame)` Binds the source's newest frame.
> - `inject()` Fills `video.sample` for pipelines created from the configuration it is piped into.
> - `destroy()` Destroys the placeholder texture.

A camera hands over frames at its own pace, in its own size and orientation, and each frame is valid only briefly: on the web an imported video frame expires when its task ends, and a native camera needs its buffer returned. A scene that samples frames directly has to handle all of that. `video.create` gives the scene a texture of a size it picks instead, and copies each new frame into it, fitted to that size, turned upright and mirrored if asked. The texture keeps the last frame until the next one arrives.

```ts
import { video } from '@typegpu/kit';
import * as web from '@typegpu/kit/web';

const element = document.createElement('video');
element.srcObject = await navigator.mediaDevices.getUserMedia({ video: true });
element.muted = true;
await element.play();

const feed = video.create(root, { size: [640, 360] });
feed.attach(web.video(element, { mirrored: true }));
await feed.initAsync();

const loop = frames.create(root, (frame) => {
  feed.update(frame); // copies a new frame, or does nothing
  // ...passes that read feed.texture
});
```

- Compare `feed.frames` with the last frame's count to run work only on new pictures, such as a model or a comparison with the previous frame.
- `feed.frameSize` tells a portrait camera from a landscape one.
- Match a model's result to the frame it saw with `feed.time`.

## Fitting

`fit` decides what happens when the frame's shape differs from the texture's. `'cover'`, the default, fills the texture and crops the frame's overhang. `'contain'` shows the whole frame and leaves transparent black around it. `'stretch'` fills the texture and changes the frame's aspect.

The fit is one render pass over the texture, which also turns the frame by its `rotation` and flips it when `mirrored`. It writes a uniform only when the frame's size, rotation or mirroring changes.

`feed.crop` says where the texture lies on the upright frame, as an `origin` and `extent` from 0 to 1 across it. `'cover'` keeps it within the frame and `'contain'` reaches past it.

## Without a copy

`video.live` binds the source's newest frame where it lies, and shaders read it with `video.sample(uv)`, turned upright and mirrored as the source asks. Nothing is copied. With `crop: { size, fit }`, `video.sample(uv)` reads the part of the frame that a feed of that size and fit holds at `uv`, which lines up work done on a small feed with the full frame. The ember mirror above does both: its reflection samples the camera in place at full resolution, and its embers compare frames in a small feed. Each new frame is a new external texture, so share the live view's part before creating the pipelines that sample:

```ts
import { bindings, video } from '@typegpu/kit';

const camera = video.live(root);
camera.attach(web.video(element, { mirrored: true }));
const shared = bindings.share(root, { camera: camera.part }); // before the pipelines that sample

const backdrop = tgpu.fragmentFn({ in: { uv: d.vec2f }, out: d.vec4f })(({ uv }) => {
  'use gpu';
  return video.sample(uv);
});
const drawing = root.pipe(camera.inject()).createRenderPipeline({
  vertex: common.fullScreenTriangle,
  fragment: backdrop,
  targets: { format: surface.format },
});

const loop = frames.create(root, (frame) => {
  camera.update(frame); // binds the newest frame, new or not
  const pass = frame.encoder.beginRenderPass({
    colorAttachments: [{ view: surface.view(frame), loadOp: 'clear', storeOp: 'store' }],
  });
  pass.setBindGroup(shared.layout, shared.group());
  pass.setPipeline(drawing);
  pass.draw(3);
  pass.end();
});
```

- Call `update(frame)` every frame before the passes that sample. An imported frame is valid only until the frame that read it submits. It returns whether the frame is new.
- `video.sample` reads an external texture, so it samples with filtering, without mips or `textureLoad`. Copy frames with `video.create` to keep them, compare them or read them as an ordinary texture.
- Before the first frame, after `attach(undefined)` and where `'contain'` leaves the crop uncovered, `video.sample` returns transparent black.
- A web video imports again every frame, once per device for the feeds and live views of that frame. A native camera frame is imported once and stays bound until the next arrives, so steady frames create no bind group.

## Sources

A source hands out its newest frame, numbered, and any number of feeds and live views read it. Each remembers the last number it saw, so a feed copies a frame once and a live view knows when it is new. `web.video(element)` reads a playing `<video>`, from a camera or from a file, and numbers frames as the browser presents them. Readers import a frame in the same task as the frame loop's submit, while it is valid.

On React Native, `runtimes.camera(runtime)` reads VisionCamera 5. Call its `write(frame)` from a frame output's `onFrame` on the camera thread, and attach it to the feed on the runtime that hosts the scene. The camera thread wraps each frame's pixels and gives the camera its buffer back at once. The scene's runtime imports the newest frame once, on the device that reads it first, and keeps it until a newer one arrives, so every feed and live view reads the same frame:

```tsx
import * as runtimes from '@typegpu/kit/native/runtimes';
import { useCamera, useFrameOutput } from 'react-native-vision-camera';

const source = runtimes.camera(worker); // the runtime that hosts the scene
hosted.send(({ scene }) => {
  'worklet';
  scene.feed.attach(source);
});

const output = useFrameOutput({
  onFrame: (frame) => {
    'worklet';
    source.write(frame);
    frame.dispose();
  },
});
useCamera({ isActive: true, device: 'front', outputs: [output], orientationSource: 'interface' });
```

The feed turns each frame by its `orientation` and mirrors it when `isMirrored` is set, so VisionCamera's `mirrorMode` decides which frames show mirrored. Call `source.destroy()` after detaching it to release the frame it holds.

Any object with `latest(device, seen?)` is a source. It returns a `video.Latest`: an external texture imported on that device, the frame's size, rotation and mirroring, its optional `time` and its `serial` number, counting from 1. It returns `undefined` before the first frame and when the newest is numbered `seen`, so a feed imports nothing until a new frame arrives. The source keeps each frame until a newer one replaces it. A program with its own frames, such as decoded `VideoFrame`s, can also call `feed.record(frame, imported)` directly and close each frame after the submit.

After `feed.attach(undefined)`, stop the camera's tracks yourself, and destroy the `web.video` source to stop watching the element.

## Limits

- Importing a video frame needs Chrome 113, Safari 26 or Firefox 144 on Windows. A live view binds a texture view in the external texture slot before its first frame, which needs Chrome 137.
- Each copied frame creates a bind group, because each imported frame is a new external texture.
- `runtimes.camera` needs VisionCamera 5 and react-native-webgpu 0.17.
