Draft
This page is not complete.
Now that we've covered the protocols individually, we can put them together. This article describes how the various WebRTC-related protocols interact with one another in order to create a connection and transfer data and/or media among peers.
This page needs heavy rewriting for structural integrity and content completeness. Lots of info here is good but the organization is a mess since this is sort of a dumping ground right now.
What is an Offer/Answer and Signal Channel?
Unfortunately WebRTC can’t create connections without some sort of server in the middle. We call this the Signal Channel. It’s any sort of channel of communication to exchange information before setting up a connection, whether by email, post card or a carrier pigeon... it’s up to you.
The information we need to exchange is the Offer and Answer which just contains the SDP mentioned above.
Peer A who will be the initiator of the connection, will create an Offer. They will then send this offer to Peer B using the chosen signal channel. Peer B will receive the Offer from the signal channel and create an Answer. They will then send this back to Peer A along the signal channel.
Session descriptions
The configuration of an endpoint on a WebRTC connection is called a session description. The description includes information about the kind of media being sent, its format, the transfer protocol being used, the endpoint's IP address and port, and other information needed to describe a media transfer endpoint. This information is exchanged and stored using Session Description Protocol (SDP); if you want details on the format of SDP data, you can find it in RFC 2327.
When a user starts a WebRTC call to another user, a special description is created called an offer. This description includes all the information about the caller's proposed configuration for the call. The recipient then responds with an answer, which is a description of their end of the call. In this way, both devices share with one another the information needed in order to exchange media data. This exchange is handled using Interactive Connectivity Establishment (ICE, a protocol which lets two devices use an intermediary to exchange offers and answers even if the two devices are separated by Network Address Translation (NAT).
Each peer, then, keeps two descriptions on hand: the local description, describing itself, and the remote description, describing the other end of the call.
The offer/answer process is performed both when a call is first established, but also any time the call's format or other configuration needs to change. Regardless of whether it's a new call, or reconfiguring an existing one, these are the basic steps which must occur to exchange the offer and answer, leaving out the ICE layer for the moment:
- The caller calls
RTCPeerConnection.createOffer()
to create an offer. - The caller calls
RTCPeerConnection.setLocalDescription()
to set that offer as the local description (that is, the description of the local end of the connection). - The caller uses the signaling server to transmit the offer to the intended receiver of the call.
- The recipient receives the offer and calls
RTCPeerConnection.setRemoteDescription()
to record it as the remote description (the description of the other end of the connection). - The recipient does any setup it needs to do for its end of the call, including adding outgoing streams to the connection.
- The recipient then creates an answer by calling
RTCPeerConnection.createAnswer()
. - The recipient calls
RTCPeerConnection.setLocalDescription()
to set the answer as its local description. The recipient now knows the configuration of both ends of the connection. - The recipient uses the signaling server to send the answer to the caller.
- The caller receives the answer.
- The caller calls
RTCPeerConnection.setRemoteDescription()
to set the answer as the remote description for its end of the call. It now knows the configuration of both peers. Media begins to flow as configured.
Pending and current descriptions
Taking one step deeper into the process, we find that localDescription
and remoteDescription
, the properties which return these two descriptions, aren't as simple as they look. Because during renegotiation, an offer might be rejected because it proposes an incompatible format, it's necessary that each endpoint have the ability to propose a new format but not actually switch to it until it's accepted by the other peer. For that reason, WebRTC uses pending and current descriptions.
The current description (which is returned by the RTCPeerConnection.currentLocalDescription
and RTCPeerConnection.currentRemoteDescription
properties) represents the description currently in actual use by the connection. This is the most recent connection that both sides have fully agreed to use.
The pending description (returned by RTCPeerConnection.pendingLocalDescription
and RTCPeerConnection.pendingRemoteDescription
) indicates a description which is currently under consideration following a call to setLocalDescription()
or setRemoteDescription()
, respectively.
When reading the description (returned by RTCPeerConnection.localDescription
and RTCPeerConnection.remoteDescription
), the returned value is the value of pendingLocalDescription
/pendingRemoteDescription
if there's a pending description (that is, the pending description isn't null
); otherwise, the current description (currentLocalDescription
/currentRemoteDescription
) is returned.
When changing the description by calling setLocalDescription()
or setRemoteDescription()
, the specified description is set as the pending description, and the WebRTC layer begins to evaluate whether or not it's acceptable. Once the proposed description has been agreed upon, the value of currentLocalDescription
or currentRemoteDescription
is changed to the pending description, and the pending description is set to null again, indicating that there isn't a pending description.
The pendingLocalDescription
contains not just the offer or answer under consideration, but any local ICE candidates which have already been gathered since the offer or answer was created. Similarly, pendingRemoteDescription
includes any remote ICE candidates which have been provided by calls to RTCPeerConnection.addIceCandidate()
.
See the individual articles on these properties and methods for more specifics.
What is an ICE candidate?
As well as exchanging information about the media (discussed above in Offer/Answer and SDP), peers must exchange information about the network connection. This is known as an ICE candidate and details the available methods the peer is able to communicate (directly or through a TURN server).