How WebRTC Uses SDP in Signaling
WebRTC (Web Real-Time Communication) relies on the Session Description Protocol (SDP) to negotiate media and network capabilities between peers. This article explains how WebRTC utilizes SDP during the signaling phase, detailing the offer/answer model, the metadata exchanged, and how this process establishes a direct peer-to-peer connection.
What is SDP in WebRTC?
Session Description Protocol (SDP) is a standardized format for describing multimedia sessions. It does not transmit the actual audio or video stream. Instead, SDP is a text-based format that lists a peer’s capabilities, including supported video/audio codecs, encryption keys, and network connection candidates.
The Offer/Answer Negotiation Model
WebRTC uses an “Offer/Answer” architecture to exchange SDP information between two peers (typically called Peer A and Peer B). Because WebRTC does not specify a built-in signaling protocol, peers must exchange these SDP messages using an external signaling mechanism, such as WebSockets, gRPC, or HTTP.
The negotiation process follows these steps:
Creating the Offer: Peer A initiates the connection by calling
createOffer(). The browser generates an SDP block containing Peer A’s media and network configurations. Peer A then saves this description locally usingsetLocalDescription(offer).Sending the Offer: Peer A sends this local SDP offer through the signaling server to Peer B.
Receiving the Offer: Peer B receives the offer via the signaling channel and saves it as its remote configuration using
setRemoteDescription(offer).Creating the Answer: Peer B generates a corresponding response by calling
createAnswer(). This step creates a new SDP block containing Peer B’s compatible configurations. Peer B saves this as its local configuration usingsetLocalDescription(answer).Sending the Answer: Peer B transmits the SDP answer back to Peer A through the signaling server.
Completing the Handshake: Peer A receives the answer and saves it using
setRemoteDescription(answer).
Once both peers have set their local and remote descriptions, the SDP handshake is complete, and they have successfully negotiated their media session parameters.
What Information is Exchanged in the SDP?
The SDP text blocks contain critical metadata grouped into session-level and media-level descriptions. The primary components include:
- Media Types and Codecs: Specifies whether the session includes audio, video, or data, along with supported codecs (such as Opus for audio, or VP8 and H.264 for video).
- Transport and Security Parameters: Includes security fingerprints (DTLS/SRTP) used to encrypt the media streams once the connection is established.
- ICE Candidates: Although ICE candidates (IP addresses and ports) are often gathered dynamically via trickle ICE, the initial SDP contains base network connection candidates to help establish the peer-to-peer path.
By using SDP during this signaling phase, WebRTC ensures that both peers agree on how to encode, decode, and transmit the real-time data before any actual media transmission begins.