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.
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