WebRTC implements P2P file transfer

overview

This article talks about how to use the ability of RTCPeerConnection in WebRTC to realize P2P communication. At the end of the article, there is 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 be directly transmitted through the intranet. .

There will be a lot of technical points involved in this article. I will start with the introduction of WebRTC and P2P, and then learn all the specific content around the process of establishing a P2P connection around WebRTC.

Introduction to WebRTC

webrtc is an Api that provides browsers with real-time audio and video communication capabilities, and is divided into three categories: Network Stream API, RTCPeerConnection, and Peer-to-peer Data API

Network Stream API

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

RTCPeerConnection

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

Peer-to-peer Data API

  • DataChannel: The DataChannel interface represents a bidirectional data channel between two nodes.

WebRTC has many built-in audio and video processing engines, encoders, and network protocols, some of which can be used directly at the API layer, and WebRTC is open source, so we can also directly use the underlying core technology of WebRTC, such as extracting 3A (AEC, AGC , ANC) algorithms are used in their own projects, as well as the evaluation of network bandwidth, smooth processing, and the realization of various network protocols in WebRTC. If you are interested, you can learn about the source code of WebRTC webrtc.googlesource.com/src

P2P network

The professional name is peer-to-peer network, which is a distributed application architecture that distributes tasks and workloads among peers (Peer), and is a form of networking or network formed by the peer-to-peer computing model at the application layer. In the P2P network environment, multiple computers connected to each other are in an equal position, and there is no distinction between master and slave. Each node acts as a server, provides services for other nodes, and enjoys the services provided by other nodes.

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

WebRTC establishes a P2P connection

Establishing a point-to-point connection between two clients requires media negotiation and network negotiation to determine the data compatibility and network interoperability between the two ends.

media consultation

Each other needs to understand the media formats supported by each other

For example: Peer-A can support VP8 and H264 encoding formats, while Peer-B supports VP9 and H264. To ensure correct encoding and decoding at both ends, the easiest way is to take their intersection H264. In WebRTC, The two parties participating in video communication must first exchange SDP information to ensure that both parties can analyze each other's data, and the process of exchanging SDP is also called "media negotiation".

The full name of SDP is Session Description Protocol, which translates to a protocol that describes sessions. It is mainly used for media negotiation between two session entities, mainly to solve the problem of unequal capabilities among members participating in the session. If all members participating in this call support high-quality calls, but we have not made an agreement , for the sake of compatibility, all call formats with normal quality are used, which is a waste of resources.

network negotiation

Each other needs to understand each other's network conditions, so that it is possible to find a link for mutual communication. Network negotiation is mainly to detect the network types at both ends. After WebRTC determines the network type through the STUN/TURN server, it will collect ICE candidates to determine which one to establish type of connection.

STUN

STUN (Session Traversal Utilities for NAT, NAT session traversal application) is a network protocol that allows clients behind NAT (or multiple NATs) to find out their public network address and which type of NAT they are in After that, and the Internet-side port bound by NAT to a certain local port. This information is used to establish UDP communication between two hosts that are also behind a NAT router. This process is commonly known as NAT hole punching or NAT traversal.

STUN server hole punching process:

TURN

The full name of TURN is Traversal Using Relay NAT, that is, traversing NAT through Relay, the client sends an Allocate request to the STUN server, so that the STUN server opens a relay port for user A, which can be regarded as A's public network proxy port, TURN The server forwards the access A user's data like a proxy server.

ICEcandidate

ICEcandidate is used to indicate the protocol, IP address and port used when WebRTC communicates with the remote end. The most important type of ICEcandidate is divided into the following three types: ● host type: local intranet, intranet P2P connection ● srflx type : External network after local NAT mapping, asymmetric NAT network environment, external network P2P connection Relay type: Generally, it is a symmetric NAT network, which needs to go through a TURN relay server, and cannot establish a P2P connection. In fact, ICE is what I mentioned above The process of obtaining various types of Candidates is to collect all host-type Candidates locally, srflx-type Candidates through the STUN protocol, and relay-type Candidates through the TURN protocol.

If you want to determine what type of webrtc client is established in your browser, you can view it through chrome://webrtc-internals/

signaling server

The two ends need to exchange each other's SDP and ICE candidate information. A simple websocket server can be used to obtain the other party's signaling. The signaling server can also perform a heartbeat check to ensure that the other party is online.

point-to-point connection

After understanding media negotiation and network negotiation, how do we use WebRTC to implement point-to-point connections?

The implementation process of WebRTC is as follows:

demo experience

peer.codeasily.net/

This demo implements intranet P2P file transfer, the file will be transferred in pieces, the signaling server is set up with its own cheap server, the experience of the demo version will not be very good, please understand, the source code will not be released, and the library may be packaged in the future open source

After entering the demo, wait for the uuid to be allocated. After the allocation is complete, click the copy button to copy the uuid and send it to the other party

image-20210118203601565

Fill in the uuid of the other party and click the connect button to establish a connection

image-20210118203721082

If the connection is established, you will see the following interface. If it keeps turning, try to refresh and try again.

image-20210118203838491

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 it, and the blue area on the right is the receiver, and the receiver will receive the file from the other party

problems encountered

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

  1. The dataChannel of WebRTC transmits udp packets, and it is easy to lose packets when the amount of concurrent transmission is large;

The solution is to control the number of concurrency, use a concurrent queue, and send a received signaling every time the other party receives a fragment, the sender can release a position from the concurrent pool to continue adding sending tasks

  1. The new version of the chrome kernel has been unable to obtain a clear local ip, but an mDNS pointing to the local ip, resulting in the inability of different devices on the same intranet to transmit via the intranet;

mDNS is a dns domain name protocol in the intranet. When a new device enters the local area network, it will first set an mDNS domain name pointing to the local ip, and broadcast it to all machines in the intranet and record it. The domain name resolution is distributed in each Among the machines, the disadvantages of mDNS are obvious. Intranet broadcasting consumes a certain amount of intranet resources, and there will be an additional layer of dns resolution for intranet machine connections. The compatibility of mDNS is not very good. And WebRTC only uses mDNS to improve security because it is criticized for obtaining real ip.

Author: YJNldm
original text WebRTC realizes P2P file transmission - Nuggets

★The business card at the end of the article can receive audio and video development learning materials for free, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmaps, etc.

see below!

 

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/132603130