Networking / WebRTC

WebRTC

How two browsers set up a video call that flows straight between them: swap notes through a server, discover addresses that work across home routers, then encrypt and stream.

Intermediate7 steps~7 min
Ann — starts the call from her browser
Ben — answers from his browser
Server — helper: signalling, STUN or TURN
NAT — router that hides private addresses
// THE SHORT VERSION

In 30 seconds

WebRTC lets two browsers send audio, video and data directly to each other, with no plug-in. It is what runs video calls in a web page.

Connecting takes three helpers. A signalling server passes setup notes between the browsers. A STUN server tells each browser its public address, and ICE tests which pair of addresses gets through the home routers. If none does, a TURN server relays the traffic.

Everything sent is encrypted, always. The keys are agreed directly between the two browsers.

WebRTC, step by step

STEPS01 / 07

Simplified: real browsers gather and test candidates while signalling is still under way, and group calls usually send media through a server (an SFU), which then sees it, rather than linking every pair.

STEP 01 / 07 Browser to browser
THE MATH
new RTCPeerConnection() audio + video + data sent peer to peer
  1. Step 1: Browser to browser. Most of what a browser does goes through a server. WebRTC (Web Real-Time Communication) is a set of browser features that lets two browsers send live audio, video and arbitrary data straight to each other. Skipping the detour keeps the delay low, which is what a conversation needs. The hard part is getting the two connected.
  2. Step 2: Hidden behind a router. Ann and Ben are each on a private network behind a router that does NAT (network address translation). Their devices only know private addresses, which mean nothing on the public internet, and neither knows the public address its router shows to the world. So Ann cannot simply send packets to Ben.
  3. Step 3: Signalling: swap notes. First the two browsers need a way to pass notes. WebRTC leaves that to the website: usually its own server, over a WebSocket. Ann’s browser writes an offer listing the audio and video formats it supports and a fingerprint of its encryption certificate. Ben’s browser replies with an answer. This exchange is called signalling.
  4. Step 4: STUN: what is my address?. To be reachable, Ann needs her public address. Her browser sends a small request to a STUN server on the public internet, which replies with the address and port the request appeared to come from: the ones her router put on it. Ann now has two candidate addresses to offer, her private one and this public one. Ben does the same.
  5. Step 5: ICE: try every pairing. The candidates travel to the other side through signalling. Then a procedure called ICE pairs every address of Ann’s with every address of Ben’s and sends test packets on each pairing. Private to private fails here. Public to public works, because each router has just seen its own device send outwards and lets the reply in.
  6. Step 6: TURN: the relay fallback. Some routers and company firewalls are too strict for the direct trick, and every pairing fails. As a last resort each browser can ask a TURN server for a relay address and send its packets there to be forwarded. It works, but adds delay and the provider pays for all that traffic, so only a minority of calls end up relayed.
  7. Step 7: Encrypted, then streaming. Before any media flows, the two browsers run a DTLS handshake over the chosen path and check each other’s certificate against the fingerprint from signalling. The keys encrypt audio and video (SRTP) and the data channel. WebRTC has no unencrypted mode. Browser video calls and screen sharing run on exactly this.
VALUES SO FAR
Ann····private
Ben····private
offer····signal
Ann····public
Ben····public
path····ICE
fallback····rule
media····encrypted
data····encrypted

Spotted a mistake on this page?