Docs
React SDK

Camera controls

Capture, publish, turn off and switch your camera.

Import and access

React
import { useLocalParticipant, useLocalMedia } from '@relayrtc/react'
 
const local = useLocalParticipant()
const { camera } = useLocalMedia()

Requirements

Join the room, grant video:publish in the token and allow camera access. Use HTTPS outside localhost.

Methods

MethodReturnsPurpose
enable(constraints?)Promise<MediaStreamTrack>Capture and publish. Repeated enable returns the existing track.
disable()Promise<void>Stop capture and remove the publication.
switchDevice(deviceId)Promise<MediaStreamTrack or null>Select a camera and replace the active capture when enabled.
mute()voidPause the local sender without releasing capture.
unmute()voidResume the local sender.
subscribe(listener)() => voidObserve snapshots outside React; return value removes the listener.

Capture options

enable accepts browser MediaTrackConstraints. With no argument, the SDK uses an empty constraint object and lets the browser choose capture settings. Common camera fields are:

SettingExampleMeaning
width{ ideal: 1280 }Requested capture width.
height{ ideal: 720 }Requested capture height.
frameRate{ ideal: 30, max: 30 }Requested frames per second.
deviceId{ exact: selectedDeviceId }Capture a specific device.
facingMode{ ideal: 'user' }Prefer front-facing or environment-facing camera.
aspectRatio{ ideal: 16 / 9 }Requested capture aspect ratio.

These fields are browser constraints, not quality presets. Support varies by browser and device. ideal permits fallback; exact can reject unsupported capture.

Example

React
await local?.camera.enable({
  width: { ideal: 1280 },
  height: { ideal: 720 },
  frameRate: { ideal: 30 },
})
await local?.camera.disable()

Use these statements in an async handler and catch errors. For camera-off buttons, use disable/enable so other participants can replace video with a placeholder.

Snapshot

FieldMeaning
sourcecamera.
enabledA local publication exists.
pendingA capture/device/cleanup operation is in progress.
mutedLocal sender is paused.
deviceIdSelected capture device ID, or null.
trackLocal MediaStreamTrack, or null.
publicationPublished Track object, or null.

Limits

Local mute does not signal a remote paused publication in the current server. Use disable for camera-off.

Operations can fail with MEDIA_OPERATION_PENDING, MEDIA_PERMISSION_DENIED or another media error. Catch enable/disable/device-switch promises and avoid starting another operation while pending is true.