Understanding of stun and turn in Webrtc

Understanding of stun and turn
Before introducing turn and stun, let's first understand a few concepts

Session Description Protocol (SDP)
Network Address Translation (NAT) (Network Address Translation)
network negotiation candidate
1. SDP protocol
we think about, if two different mobile phones, one mobile phone supports VP8, VP9 media format, the other supports VP8 , H264 protocol, if they communicate, what format of media will they choose to communicate?

At this time, we need to use this SDP protocol. In WebRTC, both parties involved in video communication must first exchange SDP information, so that the two parties can understand the roots, and the process of exchanging SDP is also called "media negotiation".

Remember, SDP is not called media negotiation. The process of exchanging SDP is called media negotiation. The full name of SDP is called session description protocol.

2. Address translation NAT is
often asked me =

Why do I configure stun and p2p penetration, but it still fails in many cases?
Why does ios pass, but android fails?
Why can open up the mobile telecommunications can not get through, wifi and 4g some pass and some unreasonable
Well, actually I do not really know

History tells us that when we cannot touch a certain truth, we can only explain everything we see by analogy or tool simulation.

[External link image transfer failed (img-COwrIGGF-1567468988826) (nat negotiation.png)]

Speaking of nat, in fact, you do n’t know the actual address of the other party, and then throw a probe packet, and then get the address of the other party in response.

The nat we said is unacceptable because our domestic network situation is relatively complicated. The historical reason is that we have to talk about the history of mobile, China Unicom and telecommunications. The length is too long and it is temporarily shelved.

All in all, if it does n’t work, it wo n’t work. There are other ways.

3. Candidate
Let's first look at the attributes in the Ice candidate class

public final String sdpMid;//描述协议的id
public final int sdpMLineIndex;//描述协议的行索引
public final String sdp;//会话描述协议

Ok, so far, you should have learned that this thing is a template

When we call setLocalDescription, the underlying code will help us collect candidate (candidate information), and then callback to the upper layer, then we send it to the server, and then the server sends to the other end

I'm sure to wonder what's in this candidate, in fact, it is a candidate address for some network information, one can't change the other.

We call the process of exchanging candidates called network negotiation

stun is
good, our protagonist debuts

STUN (Session Traversal Utilities for NAT) is a network protocol that allows
clients located behind NAT (or multiple NAT) to find out their public network address and find out what type of NAT they are in After that, the
Internet port bound to a certain local port by NAT . This information is used to create UDP communication between two hosts that are both behind the NAT router. This protocol is defined by RFC 5389
.
In fact, many people asked me, do I need a stun server in the LAN?

I tell you very seriously, no need!

At this time, someone will ask again, why don't you deploy stun in your demo and it won't work in the LAN?

I also tell you very seriously, please see the official demo, there is a directly connected class you can learn from, directly fill in the address of the other party, because you need to know the address of the other party to communicate

Come to a picture

[External link image transfer failed (img-myeYWWDt-1567468988842) (D: \ github \ csdn \ webrtc \ stun.png)]

STUN can not always successful call for the need NAT to assign IP address of the device, P2P in the transmission of media streams used in this
manner the bandwidth, more than during the video call, the call quality is good or bad often need to The user's local bandwidth is determined.

At this time, you need to turn to coordinate to ensure the quality of the call, and use the server to decompress


The full name of TURN TURN is Traversal Using Relays around NAT, which is an extension of STUN / RFC5389 and mainly adds the Relay function. If the
terminal is behind NAT, under certain circumstances, the terminal may not be able to communicate directly with its peer (peer). In this case,
a server on the public network is required as a relay to forward the data to and from. . This forwarding protocol is defined as TURN.

Another picture

[External link image transfer failed (img-Vo7XkDv6-1567468988844) (D: \ github \ csdn \ webrtc \ turn.png)]

After STUN fails to allocate the public IP, you can request the public IP address as the relay address through the TURN server. The bandwidth of this method is borne by the server
. In multi-person video chat, the local bandwidth pressure is less, and, according to Google's instructions, the TURN protocol can be used in all environments.

ICE
ICE is different from STUN and TURN. ICE is not a protocol but a framework, which integrates STUN and TURN.
The coturn open source project integrates the functions of STUN and TURN

Ok, this article is here, take a look at the code to digest

Android: https://github.com/ddssingsong/webrtc_android

Server side: https://github.com/ddssingsong/webrtc_server_node
 

Published 766 original articles · praised 474 · 2.54 million views

Guess you like

Origin blog.csdn.net/u010164190/article/details/105524716