Comprehensive guide to WebRTC. Part 2/5.

The first thing of any WebRTC app is creating an RTCPeerConnection. Creating an RTCPeerConnection will help us to understand the inner workings of peer connections inside the browser.

WebRTC uses UDP(User Datagram Protocol) as the transport protocol, but most web apps nowadays are using TCP(Transmission Control Protocol).

TCP guarantees that:
1. Any data sent will be marked as received
2. If your data is failed to send it is going to be resent blocking the sending of any more data
3. No data will be duplicated on the other side

UDP does not guarantee:
1. The order of your data
2. The receiving of data on the other side
3. The integrity of your data

Anyway, WebRTC uses UDP because it is faster than TCP and we may afford missing few video frames.

The RTCPeerConnection is the core object of the WebRTC API. It handles initializing connections, connection to peers and attaching media streams.

You can create it simply:

The onaddstream event is fired when the remote user adds video or audio stream to their peer connection.

Connecting to another browser means finding where the other browser is located on the Web. Your browser needs to get the IP address, the port number and device information from another browser. This means exchanging data about which protocols your device supports. This process is called as signalling and negotiation in WebRTC. It consists of few steps:
1. Create a list of potential candidates(users we can connect to) for a peer connection
2. The user or an app selects a user to make a connection with
3. The signalling layer notifies that user that someone wants to connect to him, and he can accept or decline
4. The first user is notified of the acceptance of the offer to connect
5. If accepted, the first user initializes RTCPeerConenction with the other user
6. Both users exchange hardware and software information over the signalling level
7. Both users exchange location information over signalling level
8. The connection succeeds or fails

It is just one example. In reality the WebRTC specification does not contain any information of how to exchange data.

To connect to another user we need to know information about his device. This is what SDP(Session Description Protocol) provides us with. The SDP is a string-based data provided by the browser in the key-value format. The SDP is given by the RTCPeerConenction during the establishing a connection with another user. It may look like this:

So SDP is just an information card of your device.

Connecting to another user means finding a clear path not just around your own network but the other user’s network as well. Three technologies are used here:
1. STUN(Session Traversal Utilities for NAT)
2. TURN(Traversal Using Relays around NAT)
3. ICE(Interactive Connectivity Establishment)

STUN makes a request to the server, enabled with the STUN protocol. The server identifies the IP address of the client and sends it back. Currently, in Chrome and Firefox default servers are provided directly from the browser vendors.

In some cases firewall might not allow any STUN traffic to the other user. In this case, we need TURN. It works by adding a relay in between the clients that acts as a peer to peer connection on behalf of the client.

ICE is the process that utilizes STUN and TURN. It works by finding a range of addressed available to each user and testing each address in sorted order until it finds a combination that will work for both users. When the browser finds a new candidate, it notifies the client app that it needs to send the ICE candidate through the signalling channel.

We are going to create an app that will get 2 video streams, one coming from the webcam directly and one coming from a WebRTC connection that the browser has made locally.

Create an index.html:

There are 2 video elements. we will be considered the local user. him will be considered the remote user we are making a connection to.

Our app will be working in this way:
1. Check if the browser supports the WebRTC
2. Get User Media
3. Create peer connection
4. Add ICE handlers
5. Start offer/response
6. Make a connection

Add main.js:


That’s all for today 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *