If you’re new to WebRTC, you must’ve heard that it’s a way to do video calls in your browser without needing to install an app. It’s pretty great!
However, it uses a bunch of really arcane terminology because it builds upon older technologies such as RTP, RTCP, SDP, ICE, STUN, etc. To understand what WebRTC Signalling is, you must first understand these foundational technologies.
Readers who are well-versed in this subject might find some of the explanations annoyingly simplistic to read. They will also notice that I am omitting a lot of of detail, leading to potentially misleading statements.
I apologize in advance to these people. I am merely trying to avoid turning this post into a book. If you find a sub-heading too simplistic, please feel free to skip it. :-)
RTP
Real-time Transport Protocol is a standardized way of taking video or audio data (media) and chopping it up into “packets” (you can literally think of them as packets / parcels) that are sent over the internet using UDP. The purpose is to try and deliver them to the destination as quickly as possible.
UDP (user datagram protocol) is a packet-based alternative to TCP (transmission control protocol), which is connection-based. So when you send something to a destination (IP address + port number), it will be delivered if possible but you have no protocol-level mechanism for finding out if it was received, unlike, say, TCP ACKs.
You can think of this as chucking parcels over a wall towards someone whom you can’t see or hear. A bunch of them will probably be lost, and you have no straightforward way to know how many were actually received.
UDP is used instead of TCP for a number of reasons, but the most important ones are:
-
TCP is designed for perfect delivery of all data, so networks will often try too hard to do that and use ridiculous amounts of buffering (sometimes 30 seconds or more!), which leads to latencies that are too large for two people to be able to talk over a call.
-
UDP doesn’t have that problem, but the trade-off is that it gives no guarantees of delivery at all!
You’d be right to wonder why nothing new has been created to be a mid-way point between these two extremes. The reason is that new transport protocols don’t get any uptake because existing systems on the Internet (operating systems, routers, switches, etc) don’t (want to) support them. This is called Protocol ossification, and it's a big problem for the Internet.
Due to this, new protocols are just built on top of UDP and try to add mechanisms to detect packet loss and such. One such mechanism is…
RTCP
RTP Control Protocol refers to standardized messages (closely related to RTP) that are sent by a media sender to all receivers, and also messages that are sent back by the receiver to the sender (feedback). As you might imagine, this message-passing system has been extended to do a lot of things, but the most important are:
- Receivers use this to send feedback to the sender about how many packets were actually received, what the latency was, etc.
- Senders send information about the stream to receivers using this, for instance to synchronize audio and video streams (also known as lipsync), to tell receivers that the stream has ended (a BYE message), etc.
Similar to RTP, these messages are also sent over UDP. You might ask “what if these are lost too”? Good question!
RTCP packets are sent at regular intervals, so you’d know if you missed one, and network routers and switches will prioritize RTCP packets over other data, so you’re unlikely to lose too many in a row unless there was a complete loss of connectivity.
Peer
WebRTC is often called a “peer-to-peer” (P2P) protocol. You might’ve heard that phrase in a different context: P2P file transfer, such as Bittorrent.
The word “peer” contrasts with “server-client” architectures, in which “client” computers can only talk to (or via) “server” computers, not directly to each other.
We can contrast server-client architecture with peer-to-peer using a real-world example:
- If you send a letter to your friend using a postal service, that’s a server-client architecture.
- If you leave the letter in your friend’s mailbox yourself, that’s peer-to-peer.
But what if you don’t know what kind of messages the recipient can receive or understand? For that we have…
SDP
Stands for Session Description Protocol which is a standardized message format to tell the other side the following:
- Whether you want to send and/or receive, audio and/or video
- How many streams of audio and/or video you want to send / receive
- What formats you can send or receive, for audio and/or video
This is called an “offer”. Then the other peer uses the same message format to reply with the same information, which is called an “answer”.
This constitutes media “negotiation”, also called “SDP exchange”. One side sends an “offer” SDP, the other side replies with an “answer” SDP, and now both sides know what to do.
As you might expect, there’s a bunch of other technical details here, and you can know all about them at this excellent page that explains every little detail. It even explains the format for ICE messages! Which is…
ICE
Interactive Connectivity Establishment is a standardized mechanism for peers to tell each other how to transmit and receive UDP packets. The simplest way to think of it is that it’s just a list of IP address and port pairs.
Once both sides have successfully sent each other (“exchanged”) ICE messages, both sides know how to send RTP and RTCP packets to each other.
Why do we need IP address + port pairs to know how to send and receive packets? For that you need to understand…
How The Internet Works
If you’re connected to the internet, you always have an IP address. That’s usually something like 192.168.1.150
– a private address that is specific to your local (home) network and has no meaning outside of that. Having someone’s private IP address is basically like having just their house number but no other parts of their address, like the street or the city. Useful if you're living in the same building, but not otherwise.
Most personal devices (computer or phone or whatever) with access to the Internet don’t actually have a public IP address. Picking up the analogy from earlier, a public IP address is the internet equivalent of a full address with a house number, street address, pin code, country.
When you want to connect to (visit) a website, your device actually talks to an ISP (internet service provider) router, which will then talk to the web server on your behalf and ask it for the data (website in this case) that you requested. This process of packet-hopping is called “routing” of network packets.
This ISP router with a public address is called a NAT (Network Address Translator). Like the name suggests, its job is to translate the addresses embedded in packets sent to it from public to private and vice-versa.
Let’s say you want to send a UDP packet to www.google.com
. Your browser will resolve that domain to an IP address, say 142.250.205.228
. Next, it needs a port to send that packet to, and both sides have to pre-agree on that port. Let’s pick 16789
for now.
Your device will then allocate a port on your device from which to send this packet, let’s say 11111
. So the packet header looks a bit like this:
From | To |
---|---|
192.168.1.150:11111 |
142.250.205.228:16789 |
Your ISP’s NAT will intercept this packet, and it will replace your private address and port in the From
field in the packet header to its own public address, say 169.13.42.111
, and it will allocate a new sender port, say 22222
:
From | To |
---|---|
169.13.42.111:22222 |
142.250.205.228:16789 |
Due to this, the web server never sees your private address, and all it can see is the public address of the NAT.
When the server wants to reply, it can send data back to the From
address, and it can use the same port that it received the packet on:
From | To |
---|---|
142.250.205.228:16789 |
169.13.42.111:22222 |
The NAT remembers that this port 22222
was recently used for your From
address, and it will do the reverse of what it did before:
From | To |
---|---|
142.250.205.228:16789 |
192.168.1.150:11111 |
And that’s how packets are send and received by your phone, computer, tablet, whatever when talking to a server.
Since at least one side needs to have a public IP address for this to work, how can your phone send messages to your friend’s phone? Both only have private addresses.
Solution 1: Just Use A Server As A Relay
The simplest solution is to have a server in the middle that relays your messages. This is how all text messaging apps such as iMessage, WhatsApp, Instagram, Telegram, etc work.
You will need to buy a server with a public address, but that’s relatively cheap if you want to send small messages.
For sending RTP (video and audio) this is accomplished with a TURN (Traversal Using Relays around NAT) server.
Bandwidth can get expensive very quickly, so you don’t want to always use a TURN server. But this is a fool-proof method to transmit data, so it’s used a backup.
Solution 2: STUN The NAT Into Doing What You Want
STUN stands for “Simple Traversal of UDP through NATs”, and it works due to a fun trick we can do with most NATs.
Previously we saw how the NAT will remember the mapping between a “port on its public address” and “your device’s private address and port”. With many NATs, this actually works for any packet sent on that public port by anyone.
This means if a public server can be used to create such mappings on the NATs of both peers, then the two can send messages to each other from NAT-to-NAT without a relay server!
Let’s dig into this, and let’s substitute hard-to-follow IP addresses with simple names: AlicePhone
, AliceNAT
, BobPhone
, BobNAT
, and finally STUNServer:19302
.
First, AlicePhone
follows this sequence:
-
AlicePhone
sends a STUN packet intended forSTUNServer:19302
using UDPFrom To AlicePhone:11111
STUNServer:19302
-
AliceNAT
will intercept this and convert it to:From To AliceNAT:22222
STUNServer:19302
-
When
STUNServer
receives this packet, it will know that if someone wants to send a packet toAlicePhone:11111
, they could useAliceNAT:22222
as theTo
address. This is an example of an ICE candidate. -
STUNServer
will then send a packet back toAlicePhone
with this information.
Next, BobPhone
does the same sequence and discovers that if someone wants to send a packet to BobPhone:33333
they can use BobNAT:44444
as the To
address. This is BobPhone
’s ICE candidate.
Now, AlicePhone
and BobPhone
must exchange these ICE candidates.
How do they do this? They have no idea how to talk to each other yet.
The answer is… they Just Use A Server As A Relay! The server used for this purpose is called a Signalling Server.
Note that these called “candidates” because this mechanism won’t work if one of the two NATs changes the public port also based on the public To
address, not just the private From
address. This is called a Symmetric NAT, and in these (and other) cases, you have to fallback to TURN.
Signalling Server
Signalling is a technical term that simply means: “a way to pass small messages between peers”. In this case, it’s a way for peers to exchange SDP and ICE candidates.
Once these small messages have been exchanged, the peers know how to send data to each other over the internet without needing a relay.
Now open your mind: you could use literally any out of band-mechanism for this. You can use Amazon Kinesis Video Signalling Channels. You can use a custom websocket server or a ProtoBuf server.
Heck, Alice and Bob can copy/paste these messages into iMessage on both ends. In theory, you can even use carrier pigeons — it’ll just take a very long time to exchange messages 😉
That’s it, this is what Signalling means in a WebRTC context, and why it’s necessary for a successful connection!
What a Signalling Server gives you on top of this is state management: checking whether a peer is allowed to send messages to another peer, whether a peer is allowed to join a call, can be invited to a call, which peers are in a call right now, etc.
Based on your use-case, this part can be really easy to implement or really difficult and heavy in corner-cases. Most people can get away with a really simple protocol, just by adding authorization to this multi-party protocol I wrote for the GStreamer WebRTC multiparty send-receive examples. More complex setups require a more bespoke solution, where all peers aren’t equal.
No comments:
Post a Comment