Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 36 ยท Peer Connections and ICE

Sending Video and Audio over a Connection

Send audio and video over an RTCPeerConnection: addTrack, the ontrack event, rendering remote streams, removing tracks, measuring frames with getStats, and a complete call template.

You now know how to capture media, negotiate a connection and find a network path. This lesson puts the pieces together into a real media call: add tracks on one side, receive them on the other, show them in <video> elements, remove them again and prove with statistics that frames are flowing. The code is the same you would use for a camera; here the source is a synthetic stream so it runs in the preview.

Sending: addTrack

JavaScript
const sender = pc.addTrack(track, stream);
  • The track is what is sent. Pass the stream too: it tells the remote side which tracks belong together (lip-sync) and fills event.streams there.
  • The call creates a transceiver and fires negotiationneeded.
  • It returns the RTCRtpSender; keep it for replaceTrack and setParameters, or to remove the track later with pc.removeTrack(sender).

Add every track you want to send before the first offer if you can; late additions work but cost a renegotiation.

Receiving: ontrack

JavaScript
pc.ontrack = (event) => {
  const track = event.track;                 // the incoming MediaStreamTrack
  const [stream] = event.streams;            // the stream the sender grouped it in (may be empty)
  video.srcObject = stream ?? new MediaStream([track]);
};

ontrack fires once per remote track: a call with audio and video fires it twice, usually with the same streams[0]. Setting srcObject twice with the same stream is harmless, but creating a new MediaStream each time is not (the second would replace the first and drop the other track). A common robust pattern:

JavaScript
const remoteStream = new MediaStream();
video.srcObject = remoteStream;
pc.ontrack = (e) => remoteStream.addTrack(e.track);

Remote tracks start out muted until packets arrive; the element shows nothing before that.

A complete call, end to end

Video and audio from peer A to peer B

The statistic framesDecoded on the receiving side's inbound-rtp report is the most honest proof that video works: if the <video> element is black, check whether this number grows. If it grows, the media is arriving and the bug is in your rendering (hidden element, autoplay policy, srcObject replaced). If it stays at 0, the media is not arriving (negotiation, direction, ICE or firewall). We build a whole toolbox around getStats in chapter 7.

Reading the receiver's side

Besides ontrack, you can walk the receivers at any time:

JavaScript
pc.getReceivers().forEach((r) => console.log(r.track.kind, r.track.readyState, r.track.muted));

track.onunmute tells you when the first media arrives; track.onmute tells you it stopped (the sender paused, or the network dropped).

Removing a track

JavaScript
pc.removeTrack(sender);       // sets the transceiver direction to recvonly/inactive and fires negotiationneeded

After the renegotiation, the remote side's track fires mute and its transceiver's currentDirection changes; its track is not removed from the connection automatically. Many apps instead call sender.replaceTrack(null), which stops sending immediately without renegotiation and keeps the slot ready for later.

removeTrack and the remote view

A reusable call skeleton

Putting chapters 2 to 4 together, this is the skeleton almost every one-to-one call in a browser follows. It is read-only because it needs a camera and a signalling server:

JavaScript
async function startCall({ signal, polite, stream, videoEl, remoteEl }) {
  const pc = new RTCPeerConnection({ iceServers: ICE_SERVERS });
  let makingOffer = false, ignoreOffer = false;

  stream.getTracks().forEach((t) => pc.addTrack(t, stream));          // chapter 3 + 4
  videoEl.srcObject = stream;                                          // muted local preview

  const remote = new MediaStream();
  remoteEl.srcObject = remote;
  pc.ontrack = (e) => remote.addTrack(e.track);

  pc.onicecandidate = ({ candidate }) => signal.send({ candidate });   // chapter 2
  pc.onnegotiationneeded = async () => {                               // perfect negotiation
    try { makingOffer = true; await pc.setLocalDescription(); signal.send({ description: pc.localDescription }); }
    finally { makingOffer = false; }
  };
  pc.onconnectionstatechange = () => {                                 // lesson 410
    if (pc.connectionState === 'failed') pc.restartIce();              // lesson 430
  };

  signal.onmessage = async ({ description, candidate }) => { /* see "Perfect negotiation" */ };
  return pc;
}

Everything else a call app adds (mute buttons, device pickers, reconnection UI, participant lists) is built around this core.

Common mistakes

  • Forgetting the stream argument in addTrack, then wondering why event.streams is empty.
  • Creating a new MediaStream per ontrack, so audio and video end up in different, competing elements.
  • Leaving remote video unmuted without a user gesture: autoplay policies may block it. Start muted, or begin from a click.
  • Not stopping local tracks after hanging up.
  • Adding tracks after negotiation without handling negotiationneeded: nothing is sent.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many times does ontrack fire for a call with one audio and one video track?

    ontrack fires per remote track. The two events usually share the same event.streams[0].
  2. 2. Why is passing the stream to addTrack(track, stream) useful?

    The stream association becomes the remote event.streams, so related tracks can be played together.
  3. 3. On the receiving side, which statistic best shows video is really arriving?

    If framesDecoded grows, media arrives and decodes; the problem is then in your rendering.
  4. 4. What is a no-renegotiation way to stop sending video but keep the transceiver?

    replaceTrack(null) stops sending immediately and needs no offer or answer; removeTrack triggers negotiation.

Summary

  • addTrack(track, stream) sends media and fires negotiationneeded; ontrack delivers each remote track once.
  • Collect remote tracks into one MediaStream attached to the video element; start local previews muted.
  • getStats() (inbound-rtp framesDecoded) tells you whether media really flows.
  • removeTrack renegotiates; replaceTrack(null) pauses sending without it.
  • The call skeleton combines capture, perfect negotiation, ICE restart and track handling.

Next: Data Channels: Sending Messages Between Peers