Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท Quality and Debugging

Controlling Bitrate and Adapting to Bandwidth

Learn how WebRTC adapts to bandwidth automatically and how you can steer it: maxBitrate, maxFramerate, scaleResolutionDownBy, degradationPreference, contentHint and codec preferences.

You do not tell WebRTC "send 720p". The browser measures the network and decides, many times per second, how much it can send. When the link gets worse it lowers bitrate by reducing resolution, frame rate or compression quality. When the link improves it climbs back. This automatic loop is called congestion control and it is one of the biggest reasons WebRTC calls survive bad Wi-Fi.

You still have a lot of influence. You can cap the bitrate, say whether sharpness or smoothness matters more, choose codecs, and describe what kind of content you are sending. This lesson shows each lever and when to use it.

How the browser adapts

Browsers use algorithms such as Google Congestion Control. The receiver reports arrival times and losses back to the sender (using RTCP feedback such as transport-wide congestion control). From these the sender estimates the available bandwidth and keeps its output below it.

flowchart LR
  S["Sender encoder"] --> N["Network"]
  N --> R["Receiver"]
  R -- "RTCP feedback: delays and losses" --> E["Bandwidth estimator"]
  E -- "target bitrate" --> S

A closed feedback loop: send, measure, adjust.

You can read the estimate in stats: candidate-pair.availableOutgoingBitrate (see Reading getStats). When the encoder cannot reach the target, qualityLimitationReason explains why.

Limit quality with RTCRtpEncodingParameters

Each RTCRtpSender has encodings you can read and change through getParameters() and setParameters():

Field Effect
maxBitrate Upper bound in bit/s for this encoding
maxFramerate Upper bound for frames per second
scaleResolutionDownBy Divide width and height (1 = full size, 2 = half)
active Turn the encoding on or off without renegotiating
priority / networkPriority Hint about importance between streams
JavaScript
const sender = pc.getSenders().find((s) => s.track && s.track.kind === 'video');
const params = sender.getParameters();
params.encodings[0].maxBitrate = 800_000;   // 800 kbit/s
params.encodings[0].maxFramerate = 24;
await sender.setParameters(params);

Changes take effect without renegotiation, so you can respond to events like "the user switched to a small window" instantly.

JS Cap bitrate and frame rate

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Setting parameters that did not come from getParameters() is rejected, which the last lines demonstrate.

Resolution versus frame rate: degradationPreference

When bandwidth drops, the browser must give something up. degradationPreference tells it what:

  • 'maintain-framerate': keep motion smooth and reduce resolution. Good for video calls and games.
  • 'maintain-resolution': keep detail sharp and reduce frame rate. Good for slides and screen sharing.
  • 'balanced': a mix.

Browser support for setting this directly varies; a portable alternative is track.contentHint:

JavaScript
videoTrack.contentHint = 'motion'; // smooth video, like a camera
screenTrack.contentHint = 'detail'; // text and diagrams: keep them crisp
audioTrack.contentHint = 'speech'; // or 'music'

A screen share of code with 'motion' would blur the text; with 'detail' it drops frames but stays readable. The hint is cheap and one of the best quality wins in apps that share screens.

Choosing codecs

The offer lists several codecs per media kind and the answer picks from them. You can reorder the preference list with transceiver.setCodecPreferences() before creating the offer. Typical video choices:

Codec Strength Watch out for
VP8 Works everywhere; low CPU Less efficient than newer codecs
H.264 Hardware acceleration on many phones Profiles and licensing quirks
VP9 Better quality per bit; SVC More CPU
AV1 Best compression; SVC and screen content Heavy CPU unless hardware helps
JS List codecs and prefer one

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Putting a codec first does not force it: the other side may not support it, and negotiation falls back. Always keep the others in the list.

Audio-specific controls

For voice, Opus is the codec. Useful knobs, set through SDP or constraints:

  • DTX (discontinuous transmission) saves bandwidth during silence.
  • FEC (forward error correction) adds redundancy against loss.
  • Constraints such as echoCancellation, noiseSuppression and autoGainControl on getUserMedia often improve quality more than any bitrate setting.

Music apps usually turn those processing constraints off and raise the Opus bitrate.

A practical adaptive policy

JS Stand-in: step bitrate with measured loss

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Common mistakes

  • Setting a huge maxBitrate "for quality". The estimator still limits you; an unrealistic cap does nothing good.
  • Capping too low. 100 kbit/s video looks like mud on a large tile.
  • Using 'motion' for screen sharing of text. Use 'detail'.
  • Fighting the congestion controller with frequent changes. Adjust on user events, not every second.
  • Hard-requiring a codec. Prefer it, do not exclude the others.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does the sender learn how much bandwidth is available?

    Congestion control uses RTCP feedback to estimate capacity and adjusts the target bitrate.
  2. 2. Which contentHint suits sharing a screen full of text?

    'detail' keeps sharpness and lets the frame rate drop when bandwidth is tight.
  3. 3. Which API changes maxBitrate without renegotiation?

    Encoding parameters are updated live on the RTCRtpSender.
  4. 4. Why keep other codecs in setCodecPreferences?

    A preference only reorders the list; negotiation picks a codec both sides support.

Summary

  • Browsers adapt bitrate automatically with congestion control using RTCP feedback.
  • Steer it with maxBitrate, maxFramerate, scaleResolutionDownBy and active through setParameters().
  • Use contentHint (motion, detail, speech, music) to say what matters.
  • Prefer codecs with setCodecPreferences(), but keep alternatives for compatibility.
  • Always fetch parameters with getParameters() before changing them.

Next: Debugging WebRTC Connections