WEBRTC is one of the important features of HTML5 support, it is no longer necessary to use audio and video-related clients, directly through the browser's web page to achieve audio and video chat function. And the WEBRTC project is open source, we can use the WEBRTC source code to quickly build their own audio and video chat function. Whether using the WEBRTC API interface of the front-end JS or building your own chat framework on the WEBRTC source code, you need to follow the following execution processes:
In the above sequence, WEBRTC does not provide stun server and signal server, the server side needs to implement itself. The stun server can be used by Google to implement the stun Protocol test Server (stun:stun.l.google.com:19302), signal server is completely required to implement their own, It requires both SDP and candidate information to be transmitted between Clienta and CLIENTB, and Clienta and CLIENTB Use this information to establish a peer-to connection to deliver audio and video data. Due to the complexity of the network environment, not all clients can establish peer-to connection, in this case, there is a need for a relay server to do audio and video data transfer, this article in line with the analysis of the attitude of the source, this situation is not considered. Here explain, Stun/turn, relay server implementation in WEBRTC source code, there are examples, is really a big treasure trove.
In the above sequence, the annotated scene is Clienta to ClientB to initiate the chat request, the invocation is described as follows:
- Clienta first creates the Peerconnection object, then turns on the local AV device, encapsulates the audio and video data into MediaStream and adds it to the peerconnection.
- Clienta calls Peerconnection's Createoffer method to create an SDP object for an offer that holds the relevant parameters for the current audio and video in the SDP object. Clienta saves the SDP object through the Peerconnection Setlocaldescription method and sends it to CLIENTB via the signal server.
- CLIENTB receives the offer SDP object sent by Clienta, which is saved by Peerconnection's Setremotedescription method, and call Peerconnection's Createanswer method to create an answer to the SDP object, The answer SDP object is saved by Peerconnection's Setlocaldescription method and sent to Clienta through the signal server.
- Clienta receives the reply SDP object sent by CLIENTB and saves it through the Peerconnection setremotedescription method.
- In the offer/answer process of SDP information, Clienta and CLIENTB have created appropriate audio channel and video channel based on SDP information and turned on the collection of candidate data. Candidate data can be simply understood as client IP address information (local IP address, public IP address, relay server assigned address).
- When Clienta collects candidate information, Peerconnection will send a notification to Clienta via the Onicecandidate interface. Clienta will receive the candidate information by signal server sent to CLIENTB,CLIENTB through Peerconnection Addicecandidate method saved up. The same operation ClientB to Clienta again.
- So Clienta and CLIENTB have established the audio and video transmission of the peer channel, CLIENTB received the Clienta transmitted over the audio and video stream, A MediaStream object that identifies the Clienta-side audio and video stream is returned via the Peerconnection Onaddstream callback interface, which is rendered on the ClientB side. The same operation also adapts to the transmission of CLIENTB to Clienta audio and video streams.
Transferred from: http://www.cnblogs.com/fangkm/p/4364553.html
WEBRTC Communication Process