RelayrtcDocs
Get started
Documentation
React SDK

Camera controls

Capture, publish, turn off and switch your camera.

On this page

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()Promise<void>Pause sending and await server confirmation without releasing capture.
unmute()Promise<void>Resume sending after server confirmation.
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

Mute pauses the sender, requests a server publication pause and resolves after confirmation. Remote subscribers receive the paused publication state. Capture remains open; use disable to release the camera and remove its publication.

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.