docs/content/docs/depth-output.mdx
import { Tab, Tabs } from 'fumadocs-ui/components/tabs'
The CameraDepthFrameOutput allows streaming Depth frames in realtime, making them accessible via a JS worklet function.
<Tabs items={["<Camera /> (view)", "useCamera(...) (hook)", "CameraSession (imperative)"]} groupId="api-style" persist> <Tab value="<Camera /> (view)">
function App() {
const device = useCameraDevice('back')
// [!code ++:8]
const depthOutput = useDepthOutput({
// ...options
onDepth(depth) {
'worklet'
console.log(`Received ${depth.width}x${depth.height} Depth!`)
depth.dispose()
}
})
return (
<Camera
style={StyleSheet.absoluteFill}
isActive={true}
device={device}
// [!code ++]
outputs={[depthOutput]}
/>
)
}
const camera = useCamera({ isActive: true, device: device, // [!code ++] outputs: [depthOutput], }) }
</Tab>
<Tab value="CameraSession (imperative)">
```tsx
const session = await VisionCamera.createCameraSession(false)
const device = await getDefaultCameraDevice('back')
// [!code ++:9]
const depthOutput = VisionCamera.createDepthFrameOutput({ /* options */ })
const workletRuntime = createWorkletRuntimeForThread(depthOutput.thread)
scheduleOnRuntime(workletRuntime, () => {
'worklet'
depthOutput.setOnDepthFrameCallback((depth) => {
console.log(`Received ${depth.width}x${depth.height} Depth!`)
depth.dispose()
})
})
await session.configure([
{
input: device,
outputs: [
// [!code ++]
{ output: depthOutput, mirrorMode: 'auto' }
],
constraints: []
}
], {})
await session.start()
See DepthFrameOutputOptions for a full list of configuration options for the Depth Output.
[!DEPENDENCY] The
CameraDepthFrameOutputrequires react-native-vision-camera-worklets (and react-native-worklets) to be installed to synchronously run theonDepth(...)function on a parallel JS Worklet Runtime.
A Depth frame is a GPU-backed buffer, streamed at high resolution and frame rate.
The Camera pipeline keeps a small pool to re-use buffers, and if that pool is full, the pipeline stalls and subsequent frames will be dropped.
To prevent frame drops, you need to dispose a Depth frame once you are done using it via dispose():
const depthOutput = useDepthOutput({
onDepth(depth) {
'worklet'
try {
// processing...
} finally {
// [!code ++]
depth.dispose()
}
}
})