preface

In the previous WebRTC Server Setup, we have set up the server environment required by WebRTC, mainly three servers: room server, signaling server and TURN penetration server.

Below, we will use the built server to implement 1-to-1 real-time call on Android using WebRTC.

WebRTC Communication process

It can be seen from the figure above that the communication process of WebRTC is very tedious, which can be summarized as three steps: signaling service registration, SDP negotiation and ICE exchange.

The following analysis is mainly for the analysis of the graph.

Signaling Service Registration:

In the whole process of WebRTC interaction, the core of its business logic is signaling, and all modules are connected in series by signaling. Metaphor In media negotiation, the ICE exchange process is completed by signaling.

In this practice, the author uses okHttp WebSocket to complete signaling communication. We have mainly done the following:

1, room news notification, such as joining the room, leaving the room, etc

2. Send the offer message 3. Receive the Answer message 4

Note: the signaling exchange process is not a WebRTC standard, developers only need to be able to complete the timely exchange of information, not necessarily using WebSocket

ICE exchange:

For more on ICE, check out my previous article STUN, TURN, and ICE at WebRTC.

An important class used in both ICE exchange and SDP negotiation is PeerConnection. When creating a PeerConnection, you need to pass a callback parameter peerConnection.observer. All we need to do is add the IceCandidate information to the PeerConnection via the addIceCandidate method of PeerConnection in the callback onIceCandidate method, The IceCandidate information is sent to the signaling server.

After receiving the IceCandidate information, the signaling server sends the IceCandidate information to other user terminals in the room. After receiving the IceCandidate information from the signaling server, other users add the information to the addIceCandidate method of the PeerConnection to the addIceCandidate method. Then the ICE exchange is complete.

SDP consultation:

Why SDP is needed, and for more information about SDP, please refer to my previous article: SDP protocol for WebRTC.

The interaction process of SDP consists of the following steps:

1. The inviter creates an offer by calling the createOffer API using PeerConnection

2, set the local SDP in the createOffer callback, that is, call the setLocalDescription method of PeerConnection. 3, set the local SDP in the callback, that is, send the offer information to the signaling server. Let the signaling server notify the invitee. 4. After the invitee gets the offer information through the signaling, Set the remote SDP by using the setRemoteDescription method of PeerConnection. 5. Create an answer by using the createAnswer method of PeerConnection in the callback after setting the remote SDP. 6. The invited party creates the successful answer and sends it to the inviter through signaling. After receiving the answer, the inviter sets the remote SDP through the setRemoteDescription method of PeerConnection to complete the SDP interaction

At this point, the two sides of the information exchange is completed, if smooth can enter the P2P call phase.

The main dependency libraries used

/ / okhttp, Mainly use the webSocket implementation (" com. Squareup. Okhttp3: okhttp: 4.9.0 ") / / WebRTC depend on implementation 'org. Webrtc: Google - webrtc: 1.0 +'Copy the code

Get the full source code

Because the code is a bit much, so I don’t stick here, if you are interested in children’s shoes can send WebRTC call demo in the public number to obtain.

Problems encountered

Error WSS 403 using okHTTP webSocket

Add a request header with key “Origin” and value “http://*******” to the request parameters.

2, called PeerConnection. SetLocalDescription collapse problem

This is because the lack of permissions: android. Permission. ACCESS_NETWORK_STATE.

Pay attention to me, progress together, life is more than coding!!