Streaming, Gaming & Smart Devices

WebRTC: Real-Time Audio and Video in the Browser

How WebRTC lets web pages make live calls: signaling, ICE, STUN and TURN, encryption, group call designs and privacy trade-offs.

Key takeaways

  • WebRTC provides real-time media and data transfer built into modern browsers, with standards published by the W3C and the IETF.
  • Applications supply their own signaling channel to exchange connection details before media can flow.
  • ICE tries several network paths, using STUN to discover public addresses and TURN to relay traffic when a direct route fails.
  • Media is encrypted in transit as a required part of WebRTC, though a server in the middle may still be able to access it.
  • Group calls usually route through a forwarding server because a full mesh of direct connections grows too expensive.

WebRTC (Web Real-Time Communication) is a set of browser APIs and network protocols that lets a web page send live audio, video and data to another device with very little delay, without installing a plug-in. It is the machinery behind many browser-based meeting tools, customer support video windows, screen sharing features and some cloud gaming services.

It handles capturing from your camera and microphone, compressing the media, finding a network path, encrypting everything and adapting when conditions change. The one thing it deliberately leaves out is how the two sides first find each other, which each application has to build.

The three main pieces of the API

Developers work mostly with three browser interfaces. getUserMedia asks permission to use the camera or microphone and returns a media stream. RTCPeerConnection manages the connection to the other side, including encoding, encryption and network adaptation. RTCDataChannel sends arbitrary data, such as chat messages, file pieces or game input, over the same connection.

Screen sharing uses a related call, getDisplayMedia, which makes the browser show its own picker so you choose what to share. A page cannot silently select your whole screen.

Browsers only offer camera and microphone access in secure contexts, which in practice means pages served over HTTPS or from localhost. You grant permission per site, and an indicator shows when a device is in use.

Signaling: the part you build yourself

Before two browsers can talk, they need to agree on codecs and exchange network information. WebRTC describes these with SDP (Session Description Protocol) text: one side creates an offer, the other replies with an answer. Each also gathers candidate addresses where it might be reached.

The standards do not dictate how that text travels. Most applications relay it through their web server, often using WebSockets. Once the connection is established, the signaling server is out of the media path, though it usually stays involved to handle people joining and leaving.

Setting up a WebRTC call
Caller browser Signaling server Callee browser Send offer 1 Codecs and media the caller supports Forward offer 2 Send answer 3 The agreed settings Forward answer 4 Exchange ICE candidates 5 Relayed through signaling in both directions Connectivity checks, then DTLS handshake 6 Encrypted media flows 7 Caller browser Signaling server Callee browser Send offer 1 Codecs and media the caller supports Forward offer 2 Send answer 3 The agreed settings Forward answer 4 Exchange ICE candidates 5 Relayed through signaling in both directions Connectivity checks, then DTLS handshake 6 Encrypted media flows 7

In the diagram, the candidate exchange also passes through the signaling server; the checks and media that follow travel on the network path the two sides selected.

ICE, STUN and TURN: finding a path

Most devices sit behind network address translation, so they do not know the public address others would use to reach them, and unsolicited incoming packets are dropped. WebRTC solves this with ICE (Interactive Connectivity Establishment).

ICE collects several kinds of candidate address:

  • Host candidates are the device's own local addresses, which work when both sides share a network.
  • Server-reflexive candidates are the public address and port that a STUN server reports seeing.
  • Relay candidates are addresses on a TURN server that forwards traffic on the client's behalf.

The two sides then test pairs of candidates and use the best one that works. A direct path gives the lowest delay and costs the service nothing in relay bandwidth. A relay is the fallback for strict firewalls and some carrier-grade NAT setups. NAT traversal explains the underlying hole-punching technique.

Anyone running a WebRTC service needs to plan for TURN. Skipping it produces an app that works in testing and then fails for users on restrictive corporate or hotel networks. TURN over TCP or TLS can help where UDP is blocked.

What carries the media

WebRTC prefers UDP, because a retransmitted audio packet that arrives late is useless. Audio and video travel as SRTP, the secure form of the Real-time Transport Protocol. Companion control messages report loss and delay so the sender can adjust its bitrate, request a fresh keyframe or retransmit selectively.

Standards require browsers to support the Opus and G.711 audio codecs, and both VP8 and H.264 constrained baseline for video. Many browsers also support VP9, AV1 or others, and the two sides negotiate what to use.

Data channels use SCTP carried inside DTLS. You can configure each channel to be reliable and ordered, like a typical web request, or unreliable and unordered for data where freshness matters more than completeness, such as a game character's position.

The WebRTC protocol stack
Your web application Interface, signaling logic, room management. Media and data APIs Peer connection, media streams and data channels. SRTP media and SCTP data Audio and video packets alongside configurable data transfer. DTLS security Handshake that authenticates the peer and sets up keys. ICE with STUN and TURN Chooses a working path, direct or relayed. UDP or TCP over IP UDP preferred; TCP or TLS when networks block it. Your web application Interface, signaling logic, room management. Media and data APIs Peer connection, media streams and data channels. SRTP media and SCTP data Audio and video packets alongside configurable data transfer. DTLS security Handshake that authenticates the peer and sets up keys. ICE with STUN and TURN Chooses a working path, direct or relayed. UDP or TCP over IP UDP preferred; TCP or TLS when networks block it.

Group calls: mesh, forwarding or mixing

A direct connection between two people is simple. Group calls need a design decision.

Design How it works Strength Weakness
Mesh Everyone sends to everyone No media server Upload grows with each person
SFU A server forwards streams selectively Scales well, low delay Each client still decodes several streams
MCU A server mixes into one stream Light work for clients Heavy server processing, added delay

In a mesh of five people, each participant uploads four copies of their video. That becomes impractical quickly on home upload links. Most services use a selective forwarding unit (SFU). Each client uploads once, often in several quality layers (called simulcast or scalable video coding), and the server sends each viewer the version that fits their screen and bandwidth.

Encryption and what it does not cover

WebRTC has no unencrypted mode for media. Keys are negotiated with DTLS, and certificate fingerprints in the session description tie the media connection to the signaling exchange. That makes the security of signaling important: whoever controls it could, in principle, put themselves in the middle.

This protection is hop by hop. Between two browsers connected directly, or through a TURN relay that merely forwards packets, only the endpoints hold the media keys. An SFU or mixer is itself a WebRTC endpoint, so it can access media unless the application adds a second layer of end-to-end encryption on top. If that matters to you, check what the service documents.

Adapting to a changing network

A live call cannot buffer its way out of trouble, so WebRTC adjusts continuously. The receiver reports which packets arrived and when. The sender uses that feedback to estimate how much the path can carry and sets the encoder's target bitrate accordingly.

When capacity falls, video usually degrades first, through lower resolution, fewer frames per second or both. Applications can express a preference: screen sharing of text benefits from keeping resolution, while a view of a moving person benefits from keeping frame rate. Audio needs little bandwidth and gets priority in most designs.

Lost packets are handled in several ways. Opus can include redundant information to reconstruct missing audio, the receiver can ask for a retransmission when there is still time, and a jitter buffer reorders packets and absorbs uneven arrival. If a video frame cannot be recovered, the receiver requests a new keyframe, a picture that can be decoded without any earlier ones.

The privacy question about IP addresses

Because ICE gathers addresses, a web page using WebRTC can learn information about your network. A direct call also reveals each party's public IP address to the other. Browsers have added protections, such as replacing local addresses with randomized names and limiting what is exposed before you grant media permission, though behavior differs between browsers.

If you use a VPN, test whether WebRTC reveals an address you expected to hide, and check your browser or VPN settings. Applications that connect strangers can force all media through TURN to conceal participants' addresses, at the cost of extra delay and relay bandwidth. What your IP address reveals puts that exposure in perspective.

When WebRTC is the right tool

Choose WebRTC when delay must stay well under a second and people interact: calls, telehealth visits, remote control, interactive classes or auctions. For one-way broadcasts to very large audiences, segmented streaming over a content delivery network is usually cheaper and easier, though some services use WebRTC-based delivery to cut delay. How live streaming works contrasts the two.

Building directly on the browser APIs teaches you a lot, but production use brings TURN servers, an SFU, reconnection handling, device quirks and monitoring. Many teams use an existing open-source media server or a hosted platform for those parts.

What users should check

If you are on the receiving end of a WebRTC app that misbehaves, a few checks cover most cases. Confirm that the site has permission to use your camera and microphone, both in the browser and in the operating system's privacy settings. Make sure no other program is holding the camera.

Then try a different network, such as your phone's hotspot. If the call works there, a firewall or filter on the first network is probably blocking the traffic, and its administrator may need to allow the service's published addresses and ports. Extensions that block scripts or alter WebRTC behavior can also interfere, so a test in a clean browser profile is informative.

Debugging a connection that fails

Chromium-based browsers provide chrome://webrtc-internals (or the equivalent under the browser's own scheme), and Firefox offers about:webrtc. These pages show candidate pairs, the selected route, codecs and live statistics.

Read them in order. If no relay candidates appear, the TURN configuration or credentials are likely wrong. If candidates exist but no pair succeeds, a firewall is probably blocking traffic. If the connection succeeds but quality is poor, look at packet loss and round-trip time. In your own code, log changes to the connection state and offer an ICE restart when a user switches networks, such as from Wi-Fi to mobile data.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Streaming, Gaming & Smart Devices →

Latest news on this topic