Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does WebRTC decide which TURN Servers to Use

Branching off this question WebRTC - How many STUN/TURN servers do I need to specify?

How does WebRTC determine which TURN servers to use when more than one is provided?

like image 890
Ternary Avatar asked Oct 13 '14 14:10

Ternary


People also ask

Does WebRTC need TURN server?

WebRTC TURN Server Then the TURN server relays the audio and video data between the devices. This method works. However, it's not ideal and should only be used when the peer-to-peer connection fails. TURN server is a necessity for delivering reliability in WebRTC calls.

What is TURN server in WebRTC?

WebRTC applications require a server to relay traffic between the clients over the internet. The server that relays that traffic is called a TURN (Traversal Using Relay NAT) server. TURN is a protocol for relaying network traffic.

How does a TURN server work?

The TURN server receives the peer UDP datagram, checks the permissions and if they are valid, forwards it to the client. This process gets around even symmetric NATs because both the client and peer can at least talk to the TURN server, which has allocated a relay IP address for communication.

What is WebRTC STUN and TURN server?

STUN and TURN servers are two types of WebRTC signaling servers that can be used to create your peer-to-peer (P2P) connection when you are building a real-time communication application.


1 Answers

Every Ice candidate is given a priority when it is gathered. It is a mixture of a couple of things and I believe that each platform(Chrome, FireFox, etc.) has their own preferences.

Here is a link to the RFC explaining how priorities are to be generated. Each priority is guaranteed to be unique as the candidate ID should be unique(if the RFC is followed). So, you should never have a tie in priorities. Those with higher priorities are tried first, if a connection cannot be made with them, then the next in line is used.

Quote from the RFC Regarding priority:

When using the formula, an agent computes the priority by determining a preference for each type of candidate (server reflexive, peer
reflexive, relayed, and host), and, when the agent is multihomed,
choosing a preference for its IP addresses. These two preferences
are then combined to compute the priority for a candidate. That
priority is computed using the following formula:

    priority = (2^24)*(type preference) +
               (2^8)*(local preference) +
               (2^0)*(256 - component ID)

The type preference MUST be an integer from 0 to 126 inclusive, and represents the preference for the type of the candidate (where the
types are local, server reflexive, peer reflexive, and relayed). A
126 is the highest preference, and a 0 is the lowest. Setting the
value to a 0 means that candidates of this type will only be used as
a last resort. The type preference MUST be identical for all
candidates of the same type and MUST be different for candidates of
different types. The type preference for peer reflexive candidates
MUST be higher than that of server reflexive candidates. Note that
candidates gathered based on the procedures of Section 4.1.1 will
never be peer reflexive candidates; candidates of these type are
learned from the connectivity checks performed by ICE.

The local preference MUST be an integer from 0 to 65535 inclusive. It represents a preference for the particular IP address from which
the candidate was obtained, in cases where an agent is multihomed.
65535 represents the highest preference, and a zero, the lowest.
When there is only a single IP address, this value SHOULD be set to 65535. More generally, if there are multiple candidates for a particular component for a particular media stream that have the same type, the local preference MUST be unique for each one. In this
specification, this only happens for multihomed hosts. If a host is
multihomed because it is dual stack, the local preference SHOULD be
set equal to the precedence value for IP addresses described in RFC
3484 [RFC3484].

The component ID is the component ID for the candidate, and MUST be between 1 and 256 inclusive.

You can see the turn server ip and port is shown in a relay candidate. The following is derived from the RFC page 82 and webrtc hacks.

a=candidate:2157334355<ID> 2<Component> udp<NetType> 33562367<Prioirty> 180.6.6.6<NAT pub IP> 54278<NAT pub Port> typ relay<Means it needs to be relayed through Turn> raddr 46.2.2.2<Relay address of turn> rport 38135<relay port of turn> generation 0
like image 80
Benjamin Trent Avatar answered Nov 09 '22 16:11

Benjamin Trent