Skip to content

Lesson 2 of 9 · 27m

SDP Offer/Answer in Practice

Read SDP as a negotiated media contract, use the browser offer/answer state machine correctly, and avoid the classic glare and rollback failures.

SDP scares people because it looks like a modem dial tone exploded. You do not need to memorize every line, but you do need to know what the browser is promising. Offer/answer is not a file transfer. It is a strict state machine in which both sides converge on compatible codecs, transport parameters, encryption fingerprints, and media directions.

Read the skeleton, not every byte

A browser SDP is a stack of sections:

v=0
o=- 7130418992402247754 2 IN IP4 127.0.0.1
t=0 0
a=group:BUNDLE 0 1
a=fingerprint:sha-256 5B:0A:...:AF
m=audio 9 UDP/TLS/RTP/SAVPF 111 0 8
a=mid:0
a=sendrecv
a=rtpmap:111 opus/48000/2
m=video 9 UDP/TLS/RTP/SAVPF 96 97
a=mid:1
a=sendonly

The important questions are simple: which media sections exist, what direction does each allow, which codecs are offered, are they bundled onto one transport, and which DTLS fingerprint will authenticate the media keys?

The browser state machine is the product

A minimal caller flow is:

const pc = new RTCPeerConnection(config);
pc.addTrack(audioTrack, stream);
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
sendSignal({type: "offer", sdp: pc.localDescription.sdp});

// Later, when the answer arrives:
await pc.setRemoteDescription({type: "answer", sdp: msg.sdp});

The callee reverses it: setRemoteDescription(offer), createAnswer(), setLocalDescription(answer), then send the answer. Calling these out of order is not a logic error you can patch around; the peer connection enters signaling states (stable, have-local-offer, have-remote-offer) that determine what is legal.

Glare is normal, not exceptional

Glare happens when both sides create offers at the same time. This is common after renegotiation for screen share or when two users unmute simultaneously. Solve it with a deterministic tie-breaker. One side is polite, one is impolite:

const polite = localUserId < remoteUserId;

pc.onnegotiationneeded = async () => {
  makingOffer = true;
  await pc.setLocalDescription();
  send({type: pc.localDescription.type, sdp: pc.localDescription.sdp});
  makingOffer = false;
};

async function onOffer(desc) {
  const collision = makingOffer || pc.signalingState !== "stable";
  if (collision && !polite) return; // impolite side keeps its offer
  if (collision) await pc.setLocalDescription({type: "rollback"});
  await pc.setRemoteDescription(desc);
  await pc.setLocalDescription();
  send({type: pc.localDescription.type, sdp: pc.localDescription.sdp});
}

The exact implementation matters less than having one. Without glare handling, calls fail only under timing pressure — which is exactly when production traffic supplies plenty of it.

Renegotiation is a new negotiation

Adding screen share, replacing a camera, changing direction, or adding a track can trigger negotiationneeded. Do not batch multiple mutations blindly. Queue negotiation, wait for stable, and include a session epoch in signaling so stale offers cannot land after a user has already left.

Field notes

  • createOffer() is asynchronous; localDescription may not be complete until ICE gathering progresses, even though trickle ICE means you rarely wait for completion.
  • sendonly and recvonly are directional proposals, not UI state. Muting a track is not the same as renegotiating media direction.
  • Never assume SDP ordering on both sides is identical. Match media sections by mid, not array position.
  • If you log SDP, cap size and retention. It is useful debugging data and still metadata about private communications.

Was this lesson helpful?

Sign in to rate