The establishment process of the peerconnection of WEBRTC

Source: Internet
Author: User

The development of video conferencing based on the third party WEBRTC open source platform is not very difficult, mainly the business aspects. However, once involved in the core of the underlying issues need to read the source code, to find out the bug, the difficulty is not small.

The project needs to analyze the creation process of peerconnection.

assuming clienta,clientb is divided into offer and answer.

    1. Offer end

PC =new rtcpeerconnection (null);

Pc.onicecandidate=handleicecandidate;

pc.onaddstream=handleremotestreamadded;

pc.onremovestream=handleremotestreamremoved;

Pc.addstream (Localstream);

Pc.createoffer (Setlocalandsend,handlecreateoffererror);


Functionhandleicecandidate (event) {

Send locally generated candidate to each other

SendMessage ({

Type: "Candidate";

Candidate:event.candidate.candidate;

......;

});

}


Functionsetlocalandsend (SDP) {

Pc.setlocaldescription (SDP);// set local SDP, Onicecandidate event is calledafter completion of Setup .

SendMessage (SDP);//Send offer to each other

}


when an offer is provided to receive answer from the other side :p c.setremotedescription (New rtcsessiondescription ( message));


when The offer is received from the other side of the candidate ,pc.addicecandidate (candidate);// will come from the other side of the candidate set to local


2. The code on the Answer side is similar to the offer side, with the red part code different

PC =newrtcpeerconnection (NULL);

Pc.onicecandidate=handleicecandidate;

pc.onaddstream=handleremotestreamadded;

pc.onremovestream=handleremotestreamremoved;

Pc.addstream (Localstream);


Note the difference: The offer side is the active call the createoffer function and Offer sent to each other. the Answer end receives Offer , you will not create peerconnection a series of operations:

pc.setremotedescription (SDP);// set the received remote Offer


Pc.createanswer (setlocalandsend,null,sdpconstraints);// Create answer are sent to each other concurrently.


The local setlocalandsend is set in the SDP, after Setup is complete The onicecandidate event is called. Then the candidate sent to each other, each other received called after candidate addicecandidate function Complete The creation of the peerconnection.

For the Onaddstream event call timing, for the offer side, after receiving an offer, it is possible that the Onaddstream event is triggered.

The establishment process of the peerconnection of WEBRTC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.