TodayTools

Web Real-Time Communication

How WebRTC works

Video calls straight between browsers

Donate
L7ApplicationUDP
What brings you here?

Watch it happen

How much detail

Daniel video calls Kenji. After a short setup, voice and video flow straight between them.

Daniel's laptop
Daniel's router
Internet
Call app server
STUN server
Kenji's router
Kenji's phone
Wi-FiInternetRequestResponseLookupControlSetupDataEncrypted

5 steps, 18 packets

First up: Offer and answer. Press Play to watch it happen, one step at a time.

0 of 50/5

What is WebRTC?

WebRTC lets two browsers or apps send voice, video and data straight to each other, with no plugin. It is not one protocol but a set: ICE, STUN and TURN find a path through home routers, DTLS sets up keys, SRTP carries encrypted voice and video, and SCTP carries data channels.

Most video call, screen sharing and in-browser voice apps use it. A server is still needed to introduce the two sides, and sometimes to relay the call, but when a direct path exists the voice and video go peer to peer, which keeps delay low.

Think of it like this. Two friends in different gated estates want to talk over the fence. A mutual friend passes notes saying where each one lives (signalling). Each asks a guard which gate they appear to come out of (STUN). They try every gate until two meet (ICE). If both estates are too strict, they hand messages to a courier who stands outside both (TURN).

Step by step

Normal

5 steps

Daniel video calls Kenji. After a short setup, voice and video flow straight between them.

  1. 1

    Offer and answer

    Swapped through the app's server

    Each browser describes itself in SDP: codecs, a key fingerprint and ICE passwords. The call app passes these notes along over its own connection.

  2. 2

    What is my address?

    STUN shows the public address

    Behind a home router, a device only knows its private address. A STUN server replies with the public address and port it saw, which becomes a candidate.

  3. 3

    Try the paths

    ICE checks with STUN packets

    Candidates are traded through the app server. Each side sends STUN checks to the other's candidates. The first pair that works in both directions wins.

  4. 4

    Lock the line

    DTLS handshake

    A DTLS handshake runs on the chosen path. Each side checks the other's certificate against the fingerprint from the SDP, then keys for SRTP are made.

  5. 5

    Talk

    SRTP voice and video, peer to peer

    Encrypted voice and video packets go straight between the two routers, about 50 voice packets a second each way. The servers are no longer in the path.

TURN relay

5 steps

Kenji is on a strict office network. No direct path works, so the call goes through a TURN relay.

  1. 1

    Offer and answer

    Same as always

    Daniel and Kenji swap offers and answers through the call app. The app also hands each browser a TURN server and a short-lived password.

  2. 2

    Ask for a relay

    TURN Allocate with a password

    Kenji's browser asks the TURN server for a relay address. The first try is refused with 401 and a nonce, the second carries the password and succeeds.

  3. 3

    Direct path fails

    The firewall drops checks

    The office firewall gives each destination a new port and drops anything that was not asked for. Daniel's checks to Kenji's public address go nowhere.

  4. 4

    Relay path works

    Checks via TURN succeed

    Daniel sends to the relay address. The TURN server forwards to Kenji, who already has a hole open to it. ICE picks this path.

  5. 5

    Talk through the relay

    Media in ChannelData

    Voice and video now go Kenji to TURN to Daniel. Each packet gets a 4-byte channel header. It costs a little delay and server bandwidth, but the call works.

Packet lost

4 steps

A video packet is lost on Kenji's Wi-Fi. The receiver asks for just that packet again, and later for a fresh key frame.

  1. 1

    A packet goes missing

    Sequence number gap

    Video packets 9121, 9122 and 9123 are sent. 9122 is lost on the Wi-Fi. Kenji's phone sees the jump in sequence numbers.

  2. 2

    NACK

    Please resend 9122

    Kenji's phone sends an RTCP NACK listing the missing packet. Daniel's browser keeps recent packets for a moment, so it can resend.

  3. 3

    Resent as RTX

    A copy on the retransmit stream

    The copy comes on a separate payload type (RTX) so it does not confuse the statistics. Voice is never resent like this; it would arrive too late, so Opus hides small gaps instead.

  4. 4

    Key frame when needed

    PLI asks for a full picture

    If too much is lost, the picture cannot be repaired. The receiver sends a Picture Loss Indication and the sender starts again with a full frame.

Myths and mix-ups

  • WebRTC needs no servers; you always need signalling, usually STUN, and sometimes a TURN relay.
  • A call through a TURN relay can be listened to by the relay; media stays end-to-end encrypted with SRTP.
  • WebRTC calls can be sent without encryption; the standard requires encryption for all media and data.
  • WebRTC is only for video; data channels carry chat, files and game moves between peers too.

Try it yourself

Run these on your own computer to see WebRTC at work.

  • Watch a live call in Chrome

    chrome://webrtc-internals

    Open it in a new tab during a video call to see candidates, the chosen path, codecs and packet loss graphs.

  • Firefox version

    about:webrtc

    Shows the same details for calls in Firefox.

  • Find your public address with STUN

    stunclient stun.example.net 3478

    Needs the stuntman tools and a STUN server address. Shows the public address and port your router gives you.

WebRTC at a glance

Layer
Application (7), a set of protocols
Transport
UDP, TCP only as a fallback
Media
SRTP (voice and video), SCTP over DTLS (data)
Encryption
Always on, keys from DTLS
NAT help
ICE with STUN and TURN, port 3478
Common codecs
Opus voice; VP8, VP9, H.264, AV1 video
Became a standard
2021 (W3C and IETF)

Defined inRFC 8825RFC 8445RFC 8489RFC 8656RFC 9429RFC 5764RFC 3550RFC 8831W3C WebRTC 1.0

Header fields

What WebRTC puts in its messages, with example values from the stories above.

WebSocket over TLS

Message
offer (SDP)WebRTC does not say how offers travel. Each app uses its own channel, here a WebSocket.

STUN

Message type
0x0001 (Binding Request)
Magic cookie
0x2112a442A fixed number that marks the packet as STUN, so it can share a port with media.
Transaction ID
96 random bits
XOR-MAPPED-ADDRESS
203.0.113.5:61234The public address the router gave this call. It is scrambled with the magic cookie so routers do not rewrite it.
USERNAME
Kx9q:aB3dBoth ICE user fragments from the SDP, so only the real peer is answered.
PRIORITY
1845501695
ICE-CONTROLLED
SetDaniel made the offer, so his side picks the final path.
MESSAGE-INTEGRITY
HMAC-SHA1 with the ICE password
USE-CANDIDATE
SetThe controlling side nominates this pair. The call will use it.
ICE-CONTROLLING
Set

SRTP

Payload type
111 (Opus)Agreed in the offer and answer, for example 111 for Opus voice.
Sequence number
23001Goes up by one per packet, so gaps show what was lost.
SSRC
0x1A2B3C4DNames the stream, so voice and video can share one port.
Payload
Encrypted with keys from DTLS
Marker
1Last packet of this video frame.
Original sequence
4411RTX packets carry the original number inside, so the receiver puts it in the right place.
Frame type
Key frameBigger than normal frames, because it does not depend on earlier ones.

Test yourself

Question 1 of 5Score 0

What does a STUN server tell your device?

Questions people ask

Is WebRTC peer to peer?

Usually the voice and video go directly between the two devices. Setting up the call needs a signalling server, and some calls are relayed by a TURN server when no direct path works.

What is the difference between STUN and TURN?

STUN only tells a device its public address so peers can connect directly. TURN relays the whole call when a direct connection is impossible.

Is WebRTC secure?

Media and data are always encrypted with keys from a DTLS handshake. How safe a call is also depends on the app's signalling server, which passes the key fingerprints.

What ports does WebRTC use?

It picks UDP ports dynamically for media. STUN and TURN servers use port 3478, and TURN over TLS often uses 443 to pass strict firewalls.

Can WebRTC leak my IP address?

Yes, candidates include your public and sometimes local addresses, and the other side sees them. Browsers now hide local addresses behind random .local names, and forcing calls through TURN hides your public one.

Donate