Docs Audio, video & screen

Audio, video & screen sharing

Pass any browser MediaStream to room.addStream(): microphone audio, webcam video, screen capture, canvas.captureStream(), or Web Audio output. Trystero coordinates SDP renegotiation and stream metadata across connected peers.

Minimal voice room

import {joinRoom} from 'trystero'

const room = joinRoom({appId: 'oedipa/walkie-talkie'}, 'channel-1')
const peerAudios = {}

const micStream = await navigator.mediaDevices.getUserMedia({
  audio: true,
  video: false
})

// Send to currently connected users and to anyone who joins later
await room.addStream(micStream)
room.onUserJoin(({userId}) => room.addStream(micStream, {target: userId}))

room.onPeerStream((stream, {userId}) => {
  const audio = peerAudios[userId] ?? new Audio()
  audio.srcObject = stream
  audio.autoplay = true
  peerAudios[userId] = audio
})

room.onUserLeave(({userId}) => {
  peerAudios[userId]?.pause()
  delete peerAudios[userId]
})

Media capture requires HTTPS (or localhost) and user permission. Browsers may block unmuted autoplay until user interaction, so surface a play control if play() rejects, and set muted = true on local preview elements to prevent audio feedback.

Multiple streams per user

When a participant shares both a camera feed and a screen capture, attach JSON metadata to addStream() so receivers can route each stream to the right element.

const camStream = await navigator.mediaDevices.getUserMedia({
  audio: true,
  video: true
})
const screenStream = await navigator.mediaDevices.getDisplayMedia({
  video: true
})

await room.addStream(camStream, {metadata: {kind: 'camera'}})
await room.addStream(screenStream, {metadata: {kind: 'screen'}})

room.onUserJoin(({userId}) => {
  room.addStream(camStream, {target: userId, metadata: {kind: 'camera'}})
  room.addStream(screenStream, {target: userId, metadata: {kind: 'screen'}})
})

room.onPeerStream((stream, {userId, metadata}) => {
  if (metadata?.kind === 'screen') stageVideoElement.srcObject = stream
  else getTileForUser(userId).srcObject = stream
})

Calling addStream() again on an active stream with updated metadata (for example, {kind: 'camera', muted: true}) re-emits onPeerStream on recipients with the new metadata.

Stopping, switching, and updating tracks

Removing a stream

room.removeStream(screenStream)

// Or stop sending to a specific user
room.removeStream(screenStream, {target: someUserId})

// Release OS capture hardware and indicator
screenStream.getTracks().forEach(track => track.stop())

Switching cameras with replaceTrack

To switch input devices or toggle between front and rear mobile cameras, replaceTrack() swaps the track on active RTCRtpSenders without renegotiating SDP.

const oldVideoTrack = camStream.getVideoTracks()[0]

const nextCam = await navigator.mediaDevices.getUserMedia({
  video: {deviceId: {exact: selectedDeviceId}}
})
const newVideoTrack = nextCam.getVideoTracks()[0]

await room.replaceTrack(oldVideoTrack, newVideoTrack)
oldVideoTrack.stop()

Adding or removing tracks on an active stream

addStream() is idempotent. If you start an audio-only call, later call stream.addTrack(videoTrack) on the same MediaStream, and call await room.addStream(stream) again, Trystero syncs the added or removed tracks to connected peers.

Replaying active streams to late listeners

If a UI component mounts after remote streams have already arrived, pass {replay: true} to receive all currently active remote streams and their latest metadata immediately.

const unsub = room.onPeerStream(
  (stream, {userId, metadata}) => renderVideoTile(userId, stream, metadata),
  {replay: true}
)

Bandwidth and room size

Peer-to-peer WebRTC sends a separate media stream to each recipient, so upload bandwidth scales linearly with viewer count. Direct meshes work well for small calls; large multi-party video conferences generally require a Selective Forwarding Unit (SFU). Even when scale() is enabled, media always uses direct 1-hop WebRTC links rather than relaying through intermediate browsers.

Negotiation details

  • Stream-to-metadata pairing. Browser ontrack events do not carry custom metadata, and tracks in a multi-track stream can arrive separately. Before adding RTP tracks, addStream() sends a descriptor binding a stream UUID, MediaStream.id, expected track count, and metadata. Receivers wait until expected tracks attach before firing onPeerStream.
  • Direct promotion in scaled rooms. Calling addStream() or replaceTrack() for a multi-hop peer negotiates a direct WebRTC connection and pins that link while media remains active.