Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 36 ยท Peer Connections and ICE

Transceivers and Senders

Master RTCRtpTransceiver, RTCRtpSender and RTCRtpReceiver: direction, mid, replaceTrack without renegotiation, setParameters bitrate limits and setCodecPreferences.

pc.addTrack(track, stream) is all most tutorials show, but behind it sits a more powerful model. Each media section in the SDP is controlled by an RTCRtpTransceiver, a pair made of one sender (what we send) and one receiver (what we receive). Learning this model lets you swap cameras without a new offer, cap the bitrate, prefer a codec, pause sending, and understand why ontrack fires when it does.

Three objects, one media section

flowchart LR
  subgraph T["RTCRtpTransceiver (one m= line, one mid)"]
    S["RTCRtpSender<br/>track, parameters"]
    R["RTCRtpReceiver<br/>incoming track"]
  end
  Local["local track"] --> S
  R --> Remote["remote track in ontrack"]

A transceiver is the unit that offer and answer negotiate. The sender and receiver inside it are controlled separately.

Object Key members
RTCRtpTransceiver mid, direction, currentDirection, sender, receiver, setCodecPreferences(), stop()
RTCRtpSender track, replaceTrack(), getParameters(), setParameters(), getStats()
RTCRtpReceiver track, getParameters(), getContributingSources()

pc.addTrack() creates (or reuses) a transceiver for you; pc.addTransceiver() gives you full control up front. pc.getTransceivers(), getSenders() and getReceivers() list them.

Direction

A transceiver has a direction (what you want) and a currentDirection (what was negotiated, or null before the first negotiation):

direction Sends Receives
sendrecv yes yes
sendonly yes no
recvonly no yes
inactive no no
stopped permanently disabled

addTransceiver('video', { direction: 'recvonly' }) is how a viewer declares "I want to watch, I will not send": the offer includes a video section even though there is no local track. This is the core of one-way streaming and of server-based setups where clients only receive.

Directions before and after negotiation

Notice: Bob never called addTrack, yet he has a transceiver created from the offer, with direction recvonly. Alice's direction stays sendrecv (what she wants), but her currentDirection becomes sendonly, because Bob's answer said recvonly. The mid is "0" on both sides: it is the shared name of the media section.

replaceTrack: change the source without renegotiating

sender.replaceTrack(newTrack) swaps the media source on an existing sender. As long as the new track is the same kind, no offer or answer is needed, and the receiver just starts getting frames from the new source on the same incoming track. Typical uses:

  • switch cameras,
  • switch between camera and screen share,
  • send black or nothing with replaceTrack(null) to stop sending while keeping the transceiver.
Swap the video source on a live call

negotiations stays at 1 and the remote side got exactly one track event: the picture changed, but the connection did not renegotiate.

setParameters: bitrate, framerate, scaling

sender.getParameters() returns the current encodings (one per simulcast layer) and other settings. You change them and write them back:

JavaScript
const params = sender.getParameters();
if (!params.encodings || params.encodings.length === 0) params.encodings = [{}];
params.encodings[0].maxBitrate = 500_000;            // bits per second
params.encodings[0].maxFramerate = 15;
params.encodings[0].scaleResolutionDownBy = 2;       // half width and height
await sender.setParameters(params);

Important rules:

  1. Call getParameters() first, modify that object and pass it back; the browser checks a transactionId to make sure you worked from fresh data.
  2. In some browsers, the encodings list is empty until negotiation has completed. Set parameters after the first answer.
  3. Setting active = false on an encoding pauses just that layer; priority and networkPriority hint at what matters most.
Limit the bitrate of a sender

Codec preferences

The order of codecs in an offer decides which one the peers try first. You can influence it without touching SDP, using setCodecPreferences() on a transceiver before creating the offer or answer:

Prefer a video codec

The first payload type on the m=video line is the preferred codec. The browser still decides what both sides support in the answer. Use this to try AV1 or H.264 for hardware acceleration, and keep VP8 in the list as a fallback.

Receivers and pausing

A receiver's track is the incoming media; you cannot set its enabled to stop packets from arriving (it only stops playing them locally). To really pause, ask the sender to stop (an application message) or change the transceiver direction to inactive, then renegotiate.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does an RTCRtpTransceiver pair together?

    One transceiver corresponds to one m= line, with a sender and a receiver.
  2. 2. Which call switches camera sources without a new offer and answer?

    replaceTrack changes the source on the same sender; receivers just see new frames on the same track.
  3. 3. A viewer wants to watch a stream but send nothing. What should it add?

    A recvonly transceiver puts a video section in the offer so the other side can send.
  4. 4. Why call getParameters() before setParameters()?

    setParameters rejects stale objects; always modify and return the object you just read.

Summary

  • A transceiver is a sender plus a receiver bound to one media section (mid); direction says what you want, currentDirection what was negotiated.
  • replaceTrack swaps a sender's source without renegotiation; replaceTrack(null) stops sending.
  • setParameters controls bitrate, frame rate, resolution scaling and layer activity per encoding.
  • setCodecPreferences reorders codecs without editing SDP.
  • Use recvonly transceivers for viewers.

Next: Sending Video and Audio over a Connection