WebRTC call principle

0. WebRTC call principle

1 Introduction

  1. When two browsers (with camera/microphone multimedia devices) in different network environments, to realize peer-to-peer real-time audio and video conversations need to:
    1. media consultation
    2. network negotiation

2. Directory

  1. media consultation
    1. SDP
  2. network negotiation
    1. What is NAT?
    2. What is NAT penetration?

1. Media consultation

  1. Media negotiation refers to the exchange of media information between two clients.
  2. First, two clients (Peer-A and Peer-B) exchange their respective media information through a signaling server accessible to both parties.
  3. The exchanged media information is the SDP session description text protocol, which describes what kind of connection and supported media format the two parties want to establish.
    1. For example, client A supports multiple encoding formats of VP8 and H264, while client B supports VP9 and H264. To ensure correct encoding and decoding at both ends, H264 must be used as the encoding format of both parties.

image.png

  1. SDP session description text protocol, including media description information, such as resolution, media type (video, audio), encoding format, transmission protocol (RTP/UDP/IP) and encryption algorithm, etc. The process of exchanging SDP is called "media negotiation ".
  2. The SDP is parsed as follows.

1. SDP

  1. SDP (Session Description Protocol) is a protocol used to describe media information, and describes terminal functions and preferences in text format. The SDP only contains the media metadata of the terminal, not the content of the media data.
    1. The two parties establishing a connection obtain media information such as resolution, encoding format, and encryption algorithm of each other by exchanging SDP.
  2. SDP usually includes the following contents:
    1. session attributes.
    2. The time the session was active.
    3. The media information contained in the session.
    4. Media codec/decoder.
    5. Media address and port information.
    6. Information about network bandwidth.
  3. WebRTC uses SDP to exchange network and media metadata between the two parties. When problems such as connection failures and black streams are encountered, SDP can be analyzed to find problems.

1. The meaning and format of the SDP field

  1. The following table shows the meaning and format of each field of SDP.image.png
  2. The SDPs are as follows.
v=0
o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5
s=SDP Seminar
i=A Seminar on the session description protocol
u=http://www.example.com/seminars/sdp.pdf
e=j.doe@example.com (Jane Doe)
c=IN IP4 224.2.17.12/127
t=2873397496 2873404696
a=recvonly
m=audio 49170 RTP/AVP 0
m=video 51372 RTP/AVP 99
a=rtpmap:99 h263-1998/90000
  1. The session is created by user jdoe, the email address is [email protected], the source address of the session is 10.47.16.5, the session name is SDP Seminar, and the i and u fields describe the extended information of the session.
  2. The t field indicates that the session is valid within 2 hours, the c field indicates that the IP address of the target is 224.2.17.12, the TTL of the address is 127, and the a field indicates that only data is received.
  3. The two m fields indicate that both use RTP audio and video configuration: the first audio media stream uses port 49170, and the payload type is 0; the second video media stream uses port 51372, and the payload type is 99.
  4. Finally, the a field indicates that the encoding format used by type 99 is h263-1998, and the encoding clock frequency is 90kHz.
  5. Let's look at another SDP information fragment when WebRTC uses H.264 encoding. The example is as follows.
m=video 49170 RTP/AVP 98
a=rtpmap:98 H264/90000
a=fmtp:98 profile-level-id=42A01E;packetization-mode=1;
  1. Indicates that this session contains video content, encoded using H.264, the encoding clock frequency is 90kHz, profile-level-id and packetization-mode are parameters passed to H.264.

2. Network negotiation

  1. Network negotiation requires two steps:
    1. Obtain the external network IP address and port.
    2. "Network information" is exchanged through signaling servers.
  2. Ideally, each client uses a public network IP and can directly connect point-to-point, but in reality, the clients are large or small in a local area network, which requires NAT (Network Address Translation, Network Address Translation) .
  3. However, NAT will protect the security of the internal network address. When the P2P connection method is adopted, NAT will prevent the access of the external network address. At this time, NAT penetration must be used.
  4. There are many NAT penetration technologies. The STUN protocol and TURN protocol used in WebRTC are applied in UDP.
  5. STUN (Session Traversal Utilities for NAT) is a public network address and port discovery protocol. The client sends a request to the STUN service, and the STUN service returns the client's public network address and NAT network information.
  6. For the situation where both parties to establish a connection are located in a symmetric NAT network, the connection cannot be successfully established after using STUN to discover the network address. In this case, it is necessary to use the services provided by the TURN protocol for traffic transfer.
  7. TURN (Traversal Using Relays around NAT) penetrates NAT through data forwarding, which solves the problems of firewalls and symmetric NAT.
    1. TURN supports UDP and TCP protocols.
  8. With the help of the STUN protocol, the communicating parties can successfully establish a P2P connection without using TURN. If there is a special situation where a P2P connection cannot be established, TURN is still required for data forwarding.
  9. The figure below shows the comparison of using STUN alone and combining STUN and TURN.
    1. Using STUN to establish a P2P network model, the network connection is directly established at both ends of the communication, without the intervention of an intermediate server.
    2. TURN is used to establish a network model of traffic relay. Both ends of the user are connected to the TURN service, and the user's network data packets are forwarded through the TURN service.

image.png

  1. Answer the questions below:
    1. What is NAT?
    2. What is NAT penetration?

1. What is NAT?

  1. NAT (Network Address Translator) is network address translation, which is used to solve the problem of insufficient IPv4 addresses.
  2. Although IPv6 can completely solve the problem of insufficient addresses, the process of replacing IPv4 with IPv6 is slow, and the coverage of IPv6 is not high.
  3. Therefore, a solution to alleviate the address problem in the IPv4 protocol is needed, that is, NAT.
  4. The basic idea of ​​NAT is that a NAT device (such as a home router) modifies the source address field of the IP packet sent from the private network to the Internet, and modifies the destination address field of the IP packet sent from the Internet to the private network.
  5. NAT is widely used. When connecting to a local area network or connecting to Wi-Fi, you are already in the NAT network. NAT has the following advantages:
    1. Shared Internet access: NAT technology uses a small number of public network IPs to realize a large number of intranet IP addresses to share the Internet through address and port mapping.
    2. Improve network security: Different intranet IP addresses are mapped to a small number of public IP addresses, which hides the intranet network structure from the outside, thereby preventing external attacks on intranet servers and reducing network risks.
    3. Convenient network management: By changing the mapping relationship, the intranet server can be migrated and changed, which is convenient for network management.
    4. Cost saving: A small number of public network IP addresses are used, which saves the registration and use fees of IP addresses.
  6. The figure below shows this process.

image.png

  1. Two hosts behind different NAT devices cannot directly establish a TCP or UDP connection. This problem can be overcome with the help of NAT traversal technology, making P2P communication possible.
  2. There are many NAT penetration technologies. The STUN protocol and TURN protocol used in WebRTC are applied in UDP, and both protocols can also be used in TCP.

2. What is NAT penetration?

  1. According to the address translation method, NAT is divided into the following four types.
    1. Full cone NAT (Full cone NAT)
      1. Once an internal network address (ip1:port1) is mapped to a public network address (ip2:port2), all packets from ip1:port1 are sent out via ip2:port2. Any external host can reach ip1: port1 by sending packets to ip2: port2.
    2. Address-Restricted cone NAT (Address-Restricted cone NAT)
      1. Only receive packets that have been sent to the peer IP address. Once an internal network address (ip1:port1) is mapped to a public network address (ip2:port2), all packets from ip1:port1 are sent out via ip2:port2. Any external host (hostAddr:any) can reach ip1:port1 by sending a packet to ip2:port2, but the premise is that ip1:port1 has sent a packet to hostAddr:any before, and any means that the port is not restricted.
    3. Port-Restricted cone NAT (Port-Restricted cone NAT)
      1. Similar to address-restricted cone NAT, but the ports are also restricted. Once an internal network address (ip1:port1) is mapped to an external network address (ip2:port2), all packets from ip1:port1 are sent out via ip2:port2. The premise that an external host (hostAddr:port3) can send packets to ip1:port1 is that ip1:port1 has sent packets to hostAddr:port3 before.
    4. Symmetric NAT (Symmetric NAT)
      1. The port number of the mapped external network address is not fixed and will change with the change of the destination address.
  2. The difference between cone NAT and symmetric NAT is that in the case where NAT has assigned port number port2 to the client, if the client continues to use port1 port to communicate with another external network server, cone NAT will continue to use the original port2 port, that is The assigned port number does not change.
  3. For symmetric NAT, NAT will assign another port number (such as port3) to port1 of Client.
    1. That is, the same intranet host and the same port number, for cone NAT, no matter which external network host communicates with, the assigned port number will not be changed.
    2. For symmetric NAT, the same intranet host and the same port number will reassign a port number every time they communicate with different external network hosts.
  4. This feature of symmetric NAT makes it impossible for WebRTC users under the network to establish P2P connections using the STUN protocol.
    1. Symmetric NAT is often used in large corporate networks. This type of network needs to use TURN technology to penetrate NAT through data forwarding, which solves the problems of firewalls and symmetric NAT.
  5. The WebRTC open source community provides the coturn project to implement STUN/TURN services.

Guess you like

Origin blog.csdn.net/weixin_41910694/article/details/128905855