Overview of the JavaScript API for WebRTC

Many internet browsers support the WebRTC protocol for real-time video and voice communication.  Application developers that require support for WebRTC on web browsers can use a JavaScript API to interface with the WebRTC protocols.  The JavaScript code for this is usually provided by the webserver that the internet browser connects to, and executed by the browser.  The browser has code to interface with audio and video hardware and handles most of those details internally.  The JavaScript API is mainly used for starting and stopping the WebRTC sessions, and communicating session parameters between endpoints.

When starting a WebRTC session, the first step is accessing the audio and video hardware.  This step is separate from the WebRTC protocol, and usually involves the browser asking for permission to use the hardware.  The next step is to create an instance of the RTCPeerConnection class which will implement the WebRTC protocol.

After this, SDP messages are relayed between the two endpoints.  One endpoint will call the createOffer() method of the peer connection class to generate an SDP offer.  The offer is saved by calling setLocalDescription() and transferred to the other side using a pre-established signaling link.  When the other side receives the offer, it saves the offer by calling setRemoteDescription() and generates an answer by calling createAnswer().  The answer is also saved and transferred.  When the answer is received by the other side, it is saved so both sides have the offer and answer SDP.

While the session is being established, the peer connection object will generate ice candidates.  It will run the onicecandidate callback for each one.  Ice candidates generated by one side of the connection must be transferred to the other side using the pre-established signaling link.  When an ice candidate is received, it should be added to the peer connection by calling addIceCandidate().

Once the SDP and ICE candidates are relayed, the WebRTC session will start automatically.  The RTCPeerConnection class will establish a secure RTP link with the remote end and relay the packets directly.  At any time, either side of the connection can close it by calling close() on the peer connection class.

JavaScript API for WebRTC

More Information