At present, EasyGBS platform already supports Webrtc protocol video stream output, which is a great progress for us. Webrtc’s advantages of timeliness and low delay can make the video output industry reach a new height. In this paper, we mainly share with you the process of weBRTC establishing P2P in the development process. The process is mainly divided into two parts, one is the initiating end, one is the receiving end, and the operation of these two parts is explained in detail.

A side

A. The initiator creates a WebSocket long connection.

Create PeerConnection and listen for onicecandiate, addStream, and onConnectionStatechange methods. The onicecandiate method is the collection of data, which is saved and set in the addIceCandiate method sent to the receiver through websocket for PeerConnection. The addStream method takes the remote stream and plays it; The onConnectionStatechange method is PeerConnetion. Disconnected means disconnected.

C. Open the local audio and video device and add audio and video to the PeerConnection object using addStream or addTrack (this method can only add a single stream, and you can use this method twice to add multiple streams).

D. Use createOffer method of PeerConnection to obtain SDP information, which contains various audio and video codec and encryption parameters.

E. The setLocalDescription method of PeerConnection saves and sets the changed SDP and sends it to the receiver through the Websocket server.

F. The originator receives the SDP information sent by the receiver and saves the setting in the setRemoteDescription method of PeerConnection.

G. In this way, we can communicate with each other.

The receiving end

A. The receiving end creates a WebSocket long connection.

Create PeerConnection and listen for onicecandiate, addStream, and onConnectionStatechange methods. The onicecandiate method collects data and sends it to the receiver through websocket for saving and setting through the addIceCandiate method of PeerConnection. The addStream method takes the remote stream and plays it; The onConnectionStatechange method is PeerConnetion. Disconnected means disconnected.

C. Open the local audio and video device and add audio and video to the PeerConnection object using addStream or addTrack (this method can only add a single stream, and you can use this method twice to add multiple streams).

D. Receive the OFFER SDP information sent by the sender and save the Settings through the setRemoteDescription method of PeerConnection.

E. Use createAnswer method of PeerConnection to obtain SDP information. SDP also contains various audio and video related parameters. And sent to the originator through the Websocket server.

F. Receive the SDP information sent by the originator and save the Settings in the setRemoteDescription method of PeerConnection.

So you can communicate with each other.

conclusion

1, a to C steps are the same;

2. Set each other’s SDP and ICE information;

3. Listen to addStream of PeerConnection to obtain the remote stream;

4. The following is my summary of the main flow chart.