26.1 The WebRTC media stack
WebRTCWebRTC. Audio, video, and data in the browser, with no plugin. A JavaScript API in the browser, and a set of protocols underneath it (RFC 8825). gives a web page access to the microphone and camera, and a way to send the media to another endpoint, with no plugin:
“This memo describes a set of building blocks that (1) can be made accessible and controllable through a JavaScript API in a browser and (2) together form a sufficient set of functions to allow the use of interactive audio and video in applications that communicate directly between browsers across the Internet.”Read the section ↗
WebRTC defines the media, but not the signaling. A web app can use any protocol to exchange the SDP; a SIP web app uses SIP over WebSocket. The media side is fixed, and stricter than classic SIP:
| Layer | WebRTC | A classic SIP phone | Module |
|---|---|---|---|
| Finding a path | ICEICE. A method (RFC 8445) in which both sides exchange their candidates in SDP, test the candidate pairs with STUN, and send media on a pair that works., always, with STUN and TURNTURN. A STUN extension (RFC 8656). A server on the Internet gives a device a relayed address and relays media to and from it. | The address in the SDP | 20 |
| Keys | DTLSDTLS. TLS for UDP. DTLS-SRTP runs a DTLS handshake on the media ports to create the SRTP keys.-SRTP, always | None, or SDES in the SDP | 18 |
| Media | SRTP, always; plain RTP is not allowed | RTP, sometimes SRTP | 16, 18 |
| Ports | BUNDLEBUNDLE. An SDP grouping (RFC 9143) that sends several media streams, such as audio and video, over one transport address and port. and rtcp-muxrtcp-mux. An SDP attribute (RFC 5761) that sends RTCP on the same port as RTP. WebRTC requires it.: one port for everything | One port per stream, RTCP on port + 1 | 15, 17 |
| Audio codecs | OpusOpus. An audio codec for speech and music, from 6 to 510 kbit/s, built to hide packet loss. Every WebRTC endpoint has it, but few desk phones do., G.711 | G.711, G.722, G.729 | 16 |
| Feedback | RTCP feedback: NACK, PLI, transport-cc | Plain RTCP reports | 17 |
“All media channels MUST be secured via SRTP and the Secure Real-time Transport Control Protocol (SRTCP). Media traffic MUST NOT be sent over plain (unencrypted) RTP or RTCP;”Read the section ↗
“Within a BUNDLE group, each "m=" section uses a BUNDLE transport for sending and receiving bundled media. Each endpoint uses a single address:port combination for sending and receiving the bundled media.”Read the section ↗
Every row is a reason why a browser cannot simply call a classic SIP phone. Something in the middle must convert: a WebRTC gatewayWebRTC gateway. A B2BUA between WebRTC clients and classic SIP. It converts WebSocket to UDP or TCP, ends ICE and DTLS-SRTP, splits BUNDLE, and transcodes if needed. (26.5).
26.2 JSEP and WebRTC SDP
The browser builds its SDP itself, through the JavaScript API. The web app calls createOffer(), gets an SDP, applies it with setLocalDescription(), and sends it in any way it likes: here, in the body of an INVITE. This model is JSEPJSEP. How a browser creates and applies SDP offers and answers through the RTCPeerConnection API (RFC 9429). The web app carries them in any signaling it likes, such as SIP.:
“In this approach, the key information that needs to be exchanged is the multimedia session description, which specifies the transport and media configuration information necessary to establish the media plane.”Read the section ↗
The result looks very different from the SDP of a desk phone. Compare a browser’s offer for an audio and video call with a SIP phone’s offer for the same call. Pick a difference to highlight its lines:
SDP comparison
The same call, offered by a browser and by a SIP phone
Transport profile. UDP/TLS/RTP/SAVPF: SRTP keyed with DTLS, with RTCP feedback. A classic phone offers RTP/AVP: plain RTP, no feedback.
The gateway: Terminates DTLS-SRTP on the browser side, and sends plain RTP (or SDES-SRTP) on the SIP side.
Browser (WebRTC) · 46 lines, 1407 bytes
SIP phone (classic) · 13 lines, 271 bytes
The WebRTC offer is about five times longer than the classic one: the SDP alone is over 1300 bytes. Over UDP the INVITE would break the 1300-byte rule (Module 19); over WebSocket, on TCP, size does not matter.
26.3 SIP over secure WebSocket
A browser cannot open a UDP or TCP socket. It can open a WebSocketWebSocket. A two-way connection that a web page opens over HTTP or HTTPS. SIP runs over it in browsers (RFC 7118).: a connection that starts as an HTTPS request and then carries messages in both directions. RFC 7118 puts one SIP message in each WebSocket message (Module 19). The browser has no address that anyone else can reach, so it writes a random .invalid host in Via and Contact, and the server sends everything back over the same connection:
“This document defines the value "WS" to be used for requests over plain WebSocket connections and "WSS" for requests over secure WebSocket connections (in which the WebSocket connection is established using TLS [RFC5246] with TCP transport).”Read the section ↗
Call flow · a browser opens WSS and registers
WSS with a valid certificate: the browser registers
- SIP
- TCP / IP
The web app opens wss://wss.atlanta.example. The browser starts TLS, and names that host in SNI.
All steps as text
- Browser → WebRTC gateway: TLS ClientHello. The web app opens wss://wss.atlanta.example. The browser starts TLS, and names that host in SNI.
- WebRTC gateway → Browser: TLS ServerHello, Certificate. The gateway presents a certificate for wss.atlanta.example, from a public certificate authority.
- Browser → WebRTC gateway: WebSocket upgrade (GET). The browser checks the certificate, then asks for a WebSocket with the sip subprotocol.
- WebRTC gateway → Browser: 101 Switching Protocols. The gateway accepts. From now on, each WebSocket message carries one SIP message.
- Browser → WebRTC gateway: REGISTER. The browser registers over the new connection. The .invalid host means: reach me over this connection.
- WebRTC gateway → Browser: 200 OK. The gateway stores the binding. Calls to Alice come down the same WebSocket.
Three points matter in production:
- The certificate must name the WSS host, and come from a certificate authority that browsers trust. A browser shows no warning page for a WebSocket; it just fails (see Common mistakes).
- The server must check the Origin header, so that only the company’s own web app can open a SIP connection.
- The connection is the registration. When the tab closes or the laptop sleeps, the WebSocket closes, and the server cannot reach the browser any more. Many web apps register with short Expires values and use push notifications for incoming calls.
26.4 Trickle ICE
A browser finds its candidates one by one: the host address at once, the server-reflexive address after a STUN round trip, the relay address after a TURN allocation. Plain ICE waits until all are found, then sends the offer. A slow or unreachable TURN server can add seconds before anything rings. trickle ICEtrickle ICE. ICE in which candidates are sent as they are found, not all at once in the offer (RFC 8838). With SIP, the later candidates travel in INFO requests (RFC 8840). sends the offer at once, and the candidates as they appear:
“This document defines "Trickle ICE", a supplementary mode of ICE operation in which candidates can be exchanged incrementally as soon as they become available (and simultaneously with the gathering of other candidates).”Read the section ↗
SIP carries trickled candidates in INFO requests, but only inside a dialog. The answer must therefore come early, in a reliable 183:
“In order to be able to start trickling, the following two conditions need to be satisfied at the SIP UAs: * Trickle ICE support at the peer agent MUST be confirmed. * A dialog MUST have been created between the peers.”Read the section ↗
“This specification defines an additional mechanism where candidates can be exchanged using SIP INFO messages and a newly defined Info Package [RFC6086].”Read the section ↗
Flow builder · trickle ICE over SIP INFO
A browser calls a SIP phone: the call works
- SIP
- RTP media
- DNS / STUN / ICE
- TCP / IP
The browser sends its offer at once, with no candidates: c= and the port are placeholders. It will trickle the candidates later.
All steps as text
- Browser → WebRTC gateway: INVITE. The browser sends its offer at once, with no candidates: c= and the port are placeholders. It will trickle the candidates later.
- WebRTC gateway → Browser: 100 Trying. The gateway takes the call.
- WebRTC gateway → Browser: 183 Session Progress. The gateway answers at once, in a reliable 183. The early dialog lets the browser trickle.
- Browser → WebRTC gateway: PRACK. The browser confirms the 183.
- WebRTC gateway → Browser: 200 OK (PRACK). Both sides now know that the other can trickle, and the dialog exists.
- Browser → WebRTC gateway: INFO. The browser sends its candidates as it finds them, in INFO requests in the early dialog.
- WebRTC gateway → Browser: 200 OK (INFO). The gateway takes the candidates. ICE starts while Bob's phone is still ringing.
- Browser → WebRTC gateway: STUN Binding request. The browser checks its candidate pairs against the gateway's one address.
- WebRTC gateway → Browser: STUN Binding response. The gateway, an ICE lite agent, only answers checks. The pair works.
- Browser → WebRTC gateway: DTLS ClientHello. On the selected pair, the browser starts the DTLS handshake: the gateway said setup:passive.
- WebRTC gateway → Browser: DTLS ServerHello, Certificate. The browser checks the gateway's certificate against the fingerprint in the answer. Both sides now have SRTP keys.
- WebRTC gateway → Bob: INVITE. The gateway calls Bob with a classic SDP: plain RTP on its own address, one port, G.711.
- Bob → WebRTC gateway: 180 Ringing. Bob's phone rings.
- Bob → WebRTC gateway: 200 OK. Bob answers, with G.711.
- WebRTC gateway → Browser: 200 OK. The gateway answers the INVITE. The SDP answer went in the 183 already.
- Browser → WebRTC gateway: ACK. The browser acknowledges.
- WebRTC gateway → Bob: ACK. The gateway acknowledges Bob's 200 OK.
- Browser → WebRTC gateway: SRTP. SRTP with G.711, keyed by DTLS, on one port for RTP and RTCP.
- WebRTC gateway → Bob: RTP. The gateway decrypts, and sends plain RTP with G.711 to Bob, RTCP on the next port.
26.5 The gateway between WebRTC and classic SIP
A WebRTC gateway (often a function of the SBC) is a B2BUA with a media relay. On the browser side it speaks WebRTC; on the other side, classic SIP. Select a layer to see what it converts, and what breaks without it:
WebRTC ↔ SIP gateway
What the gateway converts, layer by layer
The gateway: Keeps the WebSocket connection open and sends the browser's calls and requests over it.
Without it: A SIP phone or carrier cannot open a WebSocket connection to the browser.
The gateway usually has a public address, so it uses ICE liteICE lite. A simple form of ICE for servers with a public address. A lite agent offers only its host candidate and answers connectivity checks, but sends none.: it offers one host candidate, answers the browser’s connectivity checks, and sends none of its own:
“Lite agents only use host candidates and do not generate connectivity checks or run state machines, though they need to be able to respond to connectivity checks.”Read the section ↗
Follow the whole call in the builder. Turn off Gateway converts media to see what a classic phone does with a WebRTC offer, and set Network to UDP blocked to see why a web app needs a TURN server:
Flow builder · a browser calls a SIP phone through a gateway
A browser calls a SIP phone: the call works
- SIP
- RTP media
- DNS / STUN / ICE
- TCP / IP
The browser waits until it has gathered all candidates, then sends the offer over its WebSocket connection.
All steps as text
- Browser → WebRTC gateway: INVITE. The browser waits until it has gathered all candidates, then sends the offer over its WebSocket connection.
- WebRTC gateway → Browser: 100 Trying. The gateway takes the call.
- WebRTC gateway → Bob: INVITE. The gateway calls Bob with a classic SDP: plain RTP on its own address, one port, G.711.
- Bob → WebRTC gateway: 180 Ringing. Bob's phone rings.
- WebRTC gateway → Browser: 180 Ringing. The gateway tells the browser. The web app plays a ringback tone.
- Bob → WebRTC gateway: 200 OK. Bob answers, with G.711.
- WebRTC gateway → Browser: 200 OK. The gateway answers the browser in WebRTC SDP: ICE lite, its fingerprint, setup:passive, and G.711.
- Browser → WebRTC gateway: ACK. The browser acknowledges.
- WebRTC gateway → Bob: ACK. The gateway acknowledges Bob's 200 OK.
- Browser → WebRTC gateway: STUN Binding request. The browser checks its candidate pairs against the gateway's one address.
- WebRTC gateway → Browser: STUN Binding response. The gateway, an ICE lite agent, only answers checks. The pair works.
- Browser → WebRTC gateway: DTLS ClientHello. On the selected pair, the browser starts the DTLS handshake: the gateway said setup:passive.
- WebRTC gateway → Browser: DTLS ServerHello, Certificate. The browser checks the gateway's certificate against the fingerprint in the answer. Both sides now have SRTP keys.
- Browser → WebRTC gateway: SRTP. SRTP with G.711, keyed by DTLS, on one port for RTP and RTCP.
- WebRTC gateway → Bob: RTP. The gateway decrypts, and sends plain RTP with G.711 to Bob, RTCP on the next port.
“In order to deal with firewalls that block all UDP traffic, the mode of TURN that uses TCP between the WebRTC endpoint and the TURN server MUST be supported, and the mode of TURN that uses TLS over TCP between the WebRTC endpoint and the TURN server MUST be supported.”Read the section ↗
On a company network that allows only web traffic, TURN over TLS on port 443 is often the only way through. The web app gets the TURN credentials from its own server, usually short-lived ones, and passes them to the browser.
26.6 Opus, G.711, and transcoding
Every WebRTC endpoint has both Opus and G.711:
“WebRTC endpoints are REQUIRED to implement the following audio codecs: o Opus [RFC6716] with the payload format specified in [RFC7587]. o PCMA and PCMU (as specified in ITU-T Recommendation G.711 [G.711]) with the payload format specified in Section 4.5.14 of [RFC3551].”Read the section ↗
| Opus | G.711 (PCMU, PCMA) | |
|---|---|---|
| Audio band | Narrowband to fullband (up to 20 kHz) | Narrowband (300–3400 Hz) |
| Bit rate | 6–510 kbit/s, variable | 64 kbit/s |
| Packet loss | Hides loss well; in-band FEC | Clicks and gaps |
| Who has it | Every browser, most softphones | Every SIP phone, gateway, and carrier |
So a call between a browser and a desk phone needs no transcoding, as long as the browser offers G.711 and the gateway passes it through. Browsers offer Opus first, then G.711, and the gateway can choose G.711 for the whole path. Transcoding is needed only when one side has no codec in common with the other, for example a web app that removes G.711 from its offers to save bandwidth:
“If other suitable audio codecs are available for the WebRTC endpoint to use, it is RECOMMENDED that they also be included in the offer in order to maximize the possibility of establishing the session without the need for audio transcoding.”Read the section ↗
Flow builder · Opus on one side, G.711 on the other
A browser calls a SIP phone: the call works
- SIP
- RTP media
- DNS / STUN / ICE
- TCP / IP
The browser waits until it has gathered all candidates, then sends the offer over its WebSocket connection.
All steps as text
- Browser → WebRTC gateway: INVITE. The browser waits until it has gathered all candidates, then sends the offer over its WebSocket connection.
- WebRTC gateway → Browser: 100 Trying. The gateway takes the call.
- WebRTC gateway → Bob: INVITE. The gateway offers Bob G.711, and will transcode between Opus and G.711.
- Bob → WebRTC gateway: 180 Ringing. Bob's phone rings.
- WebRTC gateway → Browser: 180 Ringing. The gateway tells the browser. The web app plays a ringback tone.
- Bob → WebRTC gateway: 200 OK. Bob answers, with G.711.
- WebRTC gateway → Browser: 200 OK. The gateway answers the browser with Opus, ICE lite, its fingerprint, and setup:passive.
- Browser → WebRTC gateway: ACK. The browser acknowledges.
- WebRTC gateway → Bob: ACK. The gateway acknowledges Bob's 200 OK.
- Browser → WebRTC gateway: STUN Binding request. The browser checks its candidate pairs against the gateway's one address.
- WebRTC gateway → Browser: STUN Binding response. The gateway, an ICE lite agent, only answers checks. The pair works.
- Browser → WebRTC gateway: DTLS ClientHello. On the selected pair, the browser starts the DTLS handshake: the gateway said setup:passive.
- WebRTC gateway → Browser: DTLS ServerHello, Certificate. The browser checks the gateway's certificate against the fingerprint in the answer. Both sides now have SRTP keys.
- Browser → WebRTC gateway: SRTP. SRTP with Opus, keyed by DTLS, on one port for RTP and RTCP.
- WebRTC gateway → Bob: RTP. The gateway decrypts, transcodes Opus to G.711, and sends plain RTP to Bob.
Transcoding costs CPU on the gateway for every call, adds delay, and loses the wideband audio that Opus brought. Where both sides can do it, G.711 end to end is simpler; where quality matters, use a wideband codec on both sides (G.722, or Opus on the SIP phones that support it).
Common mistakes
No TURN server
A web app works in every test from home and from the office Wi-Fi. A customer’s company firewall blocks all UDP. The browser’s connectivity checks never get through, the call connects in SIP, and nobody hears anything.
Broken: no TURN server
A browser calls a SIP phone: ICE fails
- SIP
- DNS / STUN / ICE
- ⚠Problem
The browser waits until it has gathered all candidates, then sends the offer over its WebSocket connection.
All steps as text
- Browser → WebRTC gateway: INVITE. The browser waits until it has gathered all candidates, then sends the offer over its WebSocket connection.
- WebRTC gateway → Browser: 100 Trying. The gateway takes the call.
- WebRTC gateway → Bob: INVITE. The gateway calls Bob with a classic SDP: plain RTP on its own address, one port, G.711.
- Bob → WebRTC gateway: 180 Ringing. Bob's phone rings.
- WebRTC gateway → Browser: 180 Ringing. The gateway tells the browser. The web app plays a ringback tone.
- Bob → WebRTC gateway: 200 OK. Bob answers, with G.711.
- WebRTC gateway → Browser: 200 OK. The gateway answers the browser in WebRTC SDP: ICE lite, its fingerprint, setup:passive, and G.711.
- Browser → WebRTC gateway: ACK. The browser acknowledges.
- WebRTC gateway → Bob: ACK. The gateway acknowledges Bob's 200 OK.
- Browser → WebRTC gateway: STUN Binding request. The browser checks its only candidate pair. The firewall drops the UDP packet.
- Browser → WebRTC gateway: STUN Binding request. The browser retries for several seconds. Every check fails, so ICE fails. Problem: No TURN server: on a network that blocks UDP, the call has no media at all.
- Browser → WebRTC gateway: BYE. ICE failed, so the web app ends the call. Bob answered, but heard nothing.
- WebRTC gateway → Browser: 200 OK. The gateway confirms.
- WebRTC gateway → Bob: BYE. The gateway ends Bob's side too.
- Bob → WebRTC gateway: 200 OK. Bob's phone confirms.
Remember: give every WebRTC client a TURN server that it can reach over UDP, TCP, and TLS on port 443. Test from a network that blocks UDP.
A SIP side that does not understand DTLS-SRTP
A proxy forwards the browser’s INVITE to a desk phone, with the WebRTC SDP unchanged. The phone does not know UDP/TLS/RTP/SAVPF, ICE, or BUNDLE, and rejects the call with 488.
Broken: SDP passed through
A browser calls a SIP phone: the phone rejects the offer
- SIP
- ⚠Problem
The browser waits until it has gathered all candidates, then sends the offer over its WebSocket connection.
All steps as text
- Browser → WebRTC gateway: INVITE. The browser waits until it has gathered all candidates, then sends the offer over its WebSocket connection.
- WebRTC gateway → Browser: 100 Trying. The gateway takes the call.
- WebRTC gateway → Bob: INVITE. This gateway converts only the signaling. It sends the WebRTC SDP as it is, over TCP: it is too big for UDP. Problem: A classic SIP phone does not know UDP/TLS/RTP/SAVPF, ICE, or BUNDLE.
- Bob → WebRTC gateway: 488 Not Acceptable Here. Bob's phone does not know the transport profile, so it rejects the offer.
- WebRTC gateway → Bob: ACK. The gateway acknowledges the 488.
- WebRTC gateway → Browser: 488 Not Acceptable Here. The gateway passes the failure to the browser. The web app shows "Call failed".
- Browser → WebRTC gateway: ACK. The browser acknowledges.
Remember: between WebRTC and classic SIP, use a gateway that ends ICE and DTLS-SRTP and anchors the media, such as an SBC with WebRTC support, or a proxy with rtpengine.
A certificate that does not name the WSS host
The gateway is installed with its default, self-signed certificate for its internal name. Browsers refuse the TLS connection to wss://wss.atlanta.example. There is no warning page to click through; the web app sees error 1006 and cannot register.
Broken: wrong certificate
WSS with the wrong certificate: no connection
- TCP / IP
- Error
- ⚠Problem
The web app opens wss://wss.atlanta.example. The browser starts TLS, and names that host in SNI.
All steps as text
- Browser → WebRTC gateway: TLS ClientHello. The web app opens wss://wss.atlanta.example. The browser starts TLS, and names that host in SNI.
- WebRTC gateway → Browser: TLS ServerHello, Certificate. The gateway presents its default certificate: a self-signed one, for its internal name.
- Browser → WebRTC gateway: TLS alert (bad_certificate). The name does not match, and nobody the browser trusts signed it. The browser closes the connection. Problem: A browser shows no "accept the risk" page for a WebSocket. The web app just gets error 1006, and cannot register or call.
Remember: install a certificate from a public certificate authority whose name matches the host in the wss:// URL, with the full chain. Renew it before it expires; an expired certificate stops every web client at once.
Opus on one side, G.711 on the other, and no transcoding
A web app offers only Opus. The gateway passes the offer on, and the desk phone has only G.711. There is no codec in common, so the phone answers 488.
Broken: no codec in common
A browser calls a SIP phone: the phone rejects the offer
- SIP
The browser waits until it has gathered all candidates, then sends the offer over its WebSocket connection.
All steps as text
- Browser → WebRTC gateway: INVITE. The browser waits until it has gathered all candidates, then sends the offer over its WebSocket connection.
- WebRTC gateway → Browser: 100 Trying. The gateway takes the call.
- WebRTC gateway → Bob: INVITE. The gateway cannot transcode, so it can offer Bob only Opus.
- Bob → WebRTC gateway: 488 Not Acceptable Here. Bob's phone has no Opus, so there is no codec in common.
- WebRTC gateway → Bob: ACK. The gateway acknowledges the 488.
- WebRTC gateway → Browser: 488 Not Acceptable Here. The gateway passes the failure to the browser. The web app shows "Call failed".
- Browser → WebRTC gateway: ACK. The browser acknowledges.
Remember: keep G.711 in the browser’s offer, so that no transcoding is needed. If one side cannot change, the gateway must transcode, and needs the CPU for it.
Summary
- WebRTC media always uses ICE, DTLS-SRTP, rtcp-mux, and usually BUNDLE, with Opus and G.711. A classic SIP phone has none of this except G.711.
- A browser builds its SDP through JSEP. Its offer carries candidates, an ICE username and password, a DTLS fingerprint, BUNDLE and mid, RTCP feedback, and stream IDs.
- A browser sends SIP over secure WebSocket, with
.invalidaddresses in Via and Contact. The certificate must name the WSS host; a browser shows no warning, it just fails. - Trickle ICE sends the offer before all candidates are found. With SIP, later candidates go in INFO requests, inside an early dialog created by a reliable 183.
- A WebRTC gateway converts each layer: WebSocket to UDP or TCP, ICE (as ICE lite), DTLS-SRTP to RTP, BUNDLE to separate ports, and, if it must, Opus to G.711.
- Give every web client a TURN server reachable over TLS on 443. Keep G.711 in the offer to avoid transcoding.