Camera Session

Understanding what a Camera Session is and how to add stream from inputs to outputs

A CameraSession connects inputs (see "Camera Devices") to outputs (see "Camera Outputs") and orchestrates lifecycle.

Creating a CameraSession

To create a CameraSession, either use the <Camera /> view, the useCamera(...) hook, or create a CameraSession manually:

function App() {
  return (
    <Camera
      style={StyleSheet.absoluteFill}
      isActive={true}
      device="back"
    />
  )
}
function App() {
  const camera = useCamera({
    isActive: true,
    device: 'back',
  })
}
const device = ...

const isMultiCam = false
const session = await HybridCameraFactory.createCameraSession(isMultiCam)
await session.configure([
  {
    input: device,
    outputs: [],
    config: {}
  }
], {})
await session.start()

Tip

See "Multi-Camera" for more information about multi-camera sessions.

CameraSession Configuration

While the <Camera /> view and useCamera(...) hook automatically manage session configuration and lifecycle for you (see "Lifecycle"), manually using a CameraSession requires you to explicitly configure, start and stop the session yourself.

Tip

If you use manually use a CameraSession, ensure that most configuration (see configure(...)) is handled before you start the session (see start(...)), as any configuration while the session is running can be expensive and cause stutters.

There's three stages to Camera configuration:

  1. Session-wide configuration: (see CameraSessionConfiguration)
  2. Connection-wide configuration (see CameraConnectionConfiguration)
  3. Output-specific configuration (see CameraOutputConfiguration)

Note

Properties like zoom, exposure or focus can be adjusted without Session re-configuration - see "Camera Controller"

Interruptions

A CameraSession can be interrupted, for example due to an incoming FaceTime call or device thermal throttling (see InterruptionReason). You can listen to interruptions:

function App() {
  return (
    <Camera
      style={StyleSheet.absoluteFill}
      isActive={true}
      device="back"
      onInterruptionStarted={(reason) => console.log(`Interrupted: ${reason}`)}
      onInterruptionEnded={() => console.log(`Interruption ended`)}
    />
  )
}
function App() {
  const camera = useCamera({
    isActive: true,
    device: 'back',
    onInterruptionStarted(reason) {
      console.log(`Interrupted: ${reason}`)
    },
    onInterruptionEnded() {
      console.log(`Interruption ended`)
    }
  })
}
const session = ...

session.addOnInterruptionStartedListener((reason) => {
  console.log(`Interrupted: ${reason}`)
})
session.addOnInterruptionEndedListener(() => {
  console.log(`Interruption ended`)
})

Stopping a CameraSession

To stop a CameraSession, use stop(). To fully dispose all resources it holds, use dispose() after it has been stopped.