Skip to content

WebRTC Media Handling Deep Dive

Master getUserMedia, media track manipulation, codecs, and bandwidth adaptation.

What you'll learn

  • getUserMedia constraints: resolution, framerate, device selection
  • MediaTrack operations: enable, disable, stop, replace
  • Codec negotiation and preferences (Simulcast, SVC)
  • Bandwidth estimation and adaptive bitrate

Concept

WebRTC Media Handling Deep Dive

WebRTC media handling involves capturing audio/video, processing tracks, and adapting to network conditions.

getUserMedia

navigator.mediaDevices.getUserMedia(constraints) requests camera/mic access. Constraints specify ideal resolution, frame rate, device ID. Handle denial gracefully.

Track Operations

MediaStreamTrack: track.enabled=false (mute), track.stop() (release device), sender.replaceTrack(newTrack) (swap cameras without renegotiation). Tracks can be processed with canvas.

Codec Negotiation

SDP selects codecs both peers support: VP8, VP9, H.264, AV1 for video; Opus for audio. Simulcast sends multiple resolutions; SVC encodes multiple layers in one stream.

Adaptive Bitrate

WebRTC estimates bandwidth and adjusts send bitrate. If bandwidth drops, encoder reduces resolution/framerate. Use RTCRtpSender.setParameters() to set max bitrate.

Example

              async function startCamera() {
  const stream = await navigator.mediaDevices.getUserMedia({
    audio: {echoCancellation: true}, video: {width: {ideal: 1280}, height: {ideal: 720}}
  });
  const tracks = stream.getTracks().map(t => `${t.kind}:${t.label} (${t.enabled?'on':'off'})`);
  console.log('Tracks:', tracks);
  return stream;
}
function toggleMute(stream) { const t = stream.getAudioTracks()[0]; if (t) { t.enabled = !t.enabled; console.log(t.enabled?'unmuted':'muted'); } }
async function limitBitrate(sender, max) { const p = sender.getParameters(); if (!p.encodings) p.encodings = [{}]; p.encodings[0].maxBitrate = max; await sender.setParameters(p); }
console.log('Media functions defined');
            

getUserMedia requests camera with constraints. toggleMute flips enabled flag. limitBitrate modifies sender encoding parameters.

Try it

  • Text Counter

    Count track metadata and bandwidth stats for media debugging.

Common mistakes

The mistake

Not stopping tracks when done, keeping camera light on.

The fix

Always call track.stop() for every track when the call ends. This releases the camera.

The mistake

Setting max bitrate too low.

The fix

Below ~150kbps, video becomes unusable. Set reasonable minimums and let WebRTC adapt.

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors