An overview of the

This article is about how to realize P2P communication by using the ability of RTCPeerConnection in WebRTC. At the end of the article, there is also a demo to realize the application of P2P file transfer. The advantage of P2P file transfer is that two machines on the same Intranet can directly transfer through the Intranet.

This article will cover a lot of technical points, but I will start by introducing WebRTC and P2P, and then learn all about the process of establishing a P2P connection with WebRTC.

Introduction of WebRTC

Webrtc is an Api that provides real-time audio and video communication capability for browsers. It is divided into Network Stream Api, RTCPeerConnection Api and Peer-to-peer Data Api

Network Stream API

  • MediaStream: MediaStream is used to represent a media data stream.
  • MediaStreamTrack represents a media source in the browser.

RTCPeerConnection

  • RTCPeerConnection: An RTCPeerConnection object allows users to communicate directly between two browsers.
  • RTCIceCandidate: indicates an ICE protocol candidate.
  • RTCIceServer: indicates an ICE Server.

Peer-to-peer Data API

  • DataChannel: DataChannel an interface represents a bidirectional DataChannel between two nodes.

WebRTC has many built-in audio and video processing engines, encoders and network protocols, some of which can be directly used in the API layer, and WebRTC is open source, so we can also directly use the underlying core technology of WebRTC, for example, we can extract algorithms of 3A (AEC, AGC, ANC) from it and use them in our own projects. And the evaluation of network bandwidth, smooth processing, all kinds of the realization of the network protocol in the WebRTC has everything, interested in can understand the source code of WebRTC webrtc.googlesource.com/src

Peer-to-peer (P2P) network

Peer-to-peer network is a distributed application architecture that distributes tasks and workloads among peers. It is a networking or network form formed by peer-to-peer computing model at the application layer. In the P2P network environment, multiple computers connected to each other are in a peer position, without the distinction of master and slave. Each node acts as a server to provide services for other nodes and enjoy the services provided by other nodes.

Peer-to-peer network is a successful extension of the distributed concept. It distributes the burden of servers in the traditional way to each node in the network, and each node will undertake limited storage and computing tasks. The more nodes join the network, the more resources the nodes contribute, and the higher the quality of service.

WebRTC establishes P2P connections

To establish a point-to-point connection between two clients, media negotiation and network negotiation are required to determine data compatibility and network communication between the two clients

Media negotiation

Understand each other’s supported media formats

Such as: Peer-a can support multiple encoding formats of VP8 and H264, while peer-B can support VP9 and H264. The simplest way to ensure the correct encoding and decoding of both ends is to take their intersection. H264 In WebRTC, both parties involved in video communication must exchange SDP information first to ensure that both parties can parse each other’s data. The process of exchanging SDP is also called “media negotiation”.

SDP stands for Session Description Protocol, which translates to the Protocol for describing sessions. Is mainly used for two sessions between entities media, mainly in order to solve the participate in the session between each member’s ability to the problem of unequal, if attend this call members support high quality calls, but we don’t have to deal, for the sake of compatibility, use common quality of call format, it is a waste of resources.

Network consultation

Only in this way can we find a link to communicate with each other. Network negotiation is mainly to detect the network types at both ends. After WebRTC determines the network types through STUN/TURN server, ICEcandidate will be collected to determine which type of connection to establish.

STUN

STUN (Session Traversal Utilities for NAT) is a network protocol that allows a client behind a NAT (or multiple NAT) to find out its public address. Find out which type of NAT you are behind and which Internet port the NAT binds to a local port. This information is used to establish UDP communication between two hosts that are behind the NAT router. This process is commonly known as NAT tunneling or NAT traversal,

STUN server drilling process:

TURN

Traversal Using Relay NAT in TURN, the client sends Allocate request to the STUN server to enable A Relay port for user A, which can be regarded as the public network proxy port of USER A. The TURN server acts as A proxy server to forward data to user A.

ICEcandidate

ICEcandidate indicates the protocol, IP address, and port used by the WebRTC to communicate with the remote end. The main ICEcandidate types are as follows: ● Host type: local Intranet, Intranet P2P connection ● SRFLX type: Relay type: generally symmetric NAT network, need to TURN the relay server, can not establish P2P connection. All host candidates are collected locally, SRFLX candidates are collected using STUN protocol, and relay candidates are collected using TURN protocol.

If you want to determine what type of webrTC client is set up in your browser, you can check it with Chrome ://webrtc-internals/

Signaling server

Both ends need to exchange SDP and ICEcandidate information of each other. You can use a simple Websocket server to obtain the signaling of the other end. The signaling server can also perform heartbeat check to ensure that the other end is online.

Point-to-point connections

Now that we understand media negotiation and network negotiation, how can we use WebRTC to achieve point-to-point connectivity?

WebRTC implementation process is as follows:

The demo experience

peer.codeasily.net/

This demo realizes Intranet P2P file transmission, file will be fragmented transmission, signaling server is built with their own cheap server, demo version experience is not too good, hope understanding, source code will not put, the follow-up may package library open source

After entering demo, wait for the UUID to be assigned. After the assignment, click copy button to copy the UUID and send it to the other party

Fill in the uUID of the other party and click the Connect button to establish a connection

If the connection is completed, you will see the following interface. If the connection continues, refresh it and try again

According to the content prompt, the red area on the left is the sender to select the file, and then click the Send button to Send, while the blue area on the right is the receiver, and the receiver will receive the file of the other party

Problems encountered

In the process of P2P transmission, the following problems are encountered:

  1. The dataChannel of WebRTC transmits UDP packets, which are easy to lose when a large number of concurrent packets are transmitted.

The solution is to control the number of concurrent tasks, use a concurrent queue, and send a received signal every time a shard is received, so that the sender can free up a place in the concurrent pool to continue adding sending tasks

  1. The new Chrome kernel does not obtain a specific local IP address, but an mDNS pointing to the local IP address. As a result, different devices on the same Intranet cannot transfer data through the Intranet.

MDNS is a DNS domain name protocol on the Intranet. When a new device enters the LAN, it first assigns an mDNS domain name that points to the local IP address and broadcasts it to all devices on the Intranet for recording. Domain name resolution is distributed on each device. Intranet machine connections will also have a layer of DNS resolution, mDNS compatibility is not very good. WebRTC has been criticized for using mDNS to improve security by obtaining real IP addresses.