Draft
This page is not complete.
This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The RTCPeerConnection interface represents a WebRTC connection between the local computer and a remote peer. It provides methods to connect to a remote peer, maintain and monitor the connection, and close the connection once it's no longer needed.
RTCPeerConnection and RTCSessionDescription are currently prefixed in many browsers, and the navigator.getUserMedia() method is prefixed in many versions of some browsers. It's strongly recommended you use a shim library such as the excellent and broadly supported Adapter.js, or use code like the following for each WebRTC interface you wish to use:
var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection ||
window.webkitRTCPeerConnection;
var RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription;
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia ||
navigator.webkitGetUserMedia;
This will improve the compatibility of your site or Web app. It's worth noting that Adapter.js goes beyond prefix handling, implementing polyfills to bridge compatibility gaps between browsers' implementations of WebRTC.
RTCPeerConnection can receive events, so it implements the EventTarget interface.
Constructor
RTCPeerConnection.RTCPeerConnection()- Constructor; returns a new
RTCPeerConnectionobject.
Properties
This interface inherits properties from its parent interface, EventTarget.
RTCPeerConnection.canTrickleIceCandidatesRead onlytrueif the remote peer is able to accept trickled ICE candidates, otherwisefalse. This determination is made by inspecting the remote description as set when the localRTCPeerConnection.setRemoteDescription()function has been called. If that has not yet been done, this value isnull(notfalse, butnull).RTCPeerConnection.connectionStateRead only- blah
RTCPeerConnection.currentLocalDescriptionRead only- blah
RTCPeerConnection.currentRemoteDescriptionRead only- blah
RTCPeerConnection.defaultIceServersRead only- blah
RTCPeerConnection.iceConnectionStateRead only- Returns an enum of type
RTCIceConnectionStatethat describes the ICE connection state for the connection. When this value changes, aiceconnectionstatechangeevent is fired on the object. RTCPeerConnection.iceGatheringStateRead only- Returns an enum of type
RTCIceGatheringStatethat describes the ICE gathering state for the connection. RTCPeerConnection.idpLoginUrlRead only- blah
RTCPeerConnection.localDescriptionRead only- Returns a
RTCSessionDescriptiondescribing the session for the local end of the connection. If it has not yet been set, it can benull. RTCPeerConnection.peerIdentityRead only- Returns a
RTCIdentityAssertion, that is a couple of a domain name (idp) and a name (name) representing the identity of the remote peer of this connection, once set and verified. If no peer has yet been set and verified, this property will returnnull. Once set, via the appropriate method, it can't be changed. RTCPeerConnection.pendingLocalDescriptionRead only- blah
RTCPeerConnection.pendingRemoteDescriptionRead only- blah
RTCPeerConnection.remoteDescriptionRead only- blah
RTCPeerConnection.sctpRead only- blah
RTCPeerConnection.signalingStateRead only- Returns an enum of type
RTCSignalingStatethat describes the signaling state of the local connection. This state describes the SDP offer that defines the configuration of the connection; this includes information including the description of the locally associated objects of typeMediaStream, the codec/RTP/RTCP options, and the candidates gathered by the ICE agent. When this value changes, asignalingstatechangeevent is fired on the object.
Event handlers
RTCPeerConnection.onconnectionstatechange- blah
RTCPeerConnection.ondatachannel- Is the event handler called when the
datachannelevent is received. Such an event is sent when aRTCDataChannelis added to this connection. RTCPeerConnection.onnegotiationneeded- Is the event handler called when the
negotiationneededevent, sent by the browser to inform that negotiation will be required at some point in the future, is received. RTCPeerConnection.onicecandidate- Is the event handler called when the
icecandidateevent is received. Such an event is sent when aRTCICECandidateobject is added to the script. RTCPeerConnection.onicecandidateerror- blah
RTCPeerConnection.oniceconnectionstatechange- Is the event handler called when the
iceconnectionstatechangeevent is received. Such an event is sent when the value oficeConnectionStatechanges. RTCPeerConnection.onicegatheringstatechange- blah
RTCPeerConnection.onsignalingstatechange- Is the event handler called when the
signalingstatechangeevent, sent when the value ofsignalingStatechanges, is received. RTCPeerConnection.ontrack- blah
old:
RTCPeerConnection.onaddstream- Is the event handler called when the
addstreamevent is received. Such an event is sent when aMediaStreamis added to this connection by the remote peer. The event is sent immediately after the callRTCPeerConnection.setRemoteDescription()and doesn't wait for the result of the SDP negotiation. RTCPeerConnection.onidentityresult- Is the event handler called when the
identityresultevent is received. Such an event is sent when an identity assertion is generated, viagetIdentityAssertion(), or during the creation of an offer or an answer. RTCPeerConnection.onidpassertionerror- Is the event handler called when the
idpassertionerrorevent is received. Such an event is sent when the associated identity provider (IdP) encounters an error while generating an identity assertion. RTCPeerConnection.onidpvalidationerror- The event handler called when the
idpvalidationerrorevent is received. Such an event is sent when the associated identity provider (IdP) encounters an error while validating an identity assertion. RTCPeerConnection.onpeeridentity- Is the event handler called when the
peeridentityevent, sent when a peer identity has been set and verified on this connection, is received. RTCPeerConnection.onremovestream- Is the event handler called when the
removestreamevent, sent when aMediaStreamis removed from this connection, is received.
Constants
RTCBundlePolicy enum
The RTCBundlePolicy enum defines string constants which are used to request a specific policy for gathering ICE candidates if the remote peer isn't compatible with the SDP BUNDLE standard for bundling multiple media streams on a single transport link.
In technical terms, a BUNDLE lets all media flows between two peers flow across a single 5-tuple; that is, from the same IP and port on one peer to the same IP and port on the other peer, using the same transport protocol.
| Constant | Description |
|---|---|
"balanced" |
On BUNDLE-aware connections, the ICE agent should gather candidates for all of the media types in use (audio, video, and data). Otherwise, the ICE agent should only negotiate one audio and video track on separate transports. |
"max-compat" |
The ICE agent should gather candidates for each track, using separate transports to negotiate all media tracks for connections which aren't BUNDLE-compatible. |
"max-bundle" |
The ICE agent should gather candidates for just one track. If the connection isn't BUNDLE-compatible, then the ICE agent should negotiate just one media track. |
RTCIceTransportPolicy enum
The RTCIceTransportPolicy enum defines string constants which can be used to limit the transport policies of the ICE candidates to be considered during the connection process.
| Constant | Description |
|---|---|
"public" |
Only ICE candidates with public IP addresses will be considered. |
"relay" |
Only ICE candidates whose IP addresses are being relayed, such as those being passed through a TURN server, will be considered. |
"all" |
All ICE candidates will be considered. |
RTCRtcpMuxPolicy enum
The RTCRtcpMuxPolicy enum defines string constants which specify what ICE candidates are gathered to support non-multiplexed RTCP. <<<add a link to info about multiplexed RTCP.
| Constant | Description |
|---|---|
"negotiate" |
Instructs the ICE agent to gather both RTP and RTCP candidates. If the remote peer can multiplex RTCP, then RTCP candidates are multiplexed atop the corresponding RTP candidates. Otherwise, both the RTP and RTCP candidates are returned, separately. |
"relay" |
Tells the ICE agent to gather ICE candidates for only RTP, and to multiplex RTCP atop them. If the remote peer doesn't support RTCP multiplexing, then session negotiation fails. |
Methods
RTCPeerConnection.addIceCandidate()- When a new ICE candidate arrives over the signaling channel, call this method to deliver it to the local WebRTC ICE agent.
RTCPeerConnection.addTrack()- blah
RTCPeerConnection.addTransceiver()- blah
RTCPeerConnection.close()- Abruptly closes a connection.
RTCPeerConnection.createAnswer()- Creates an answer to the offer received by the remote peer, in a two-part offer/answer negotiation of a connection. The two first parameters are respectively success and error callbacks, the optional third one represent options for the answer to be created.
RTCPeerConnection.createDataChannel()- Creates a new
RTCDataChannelassociated with this connection. The method takes a dictionary as parameter, with the configuration required for the underlying data channel, like its reliability. RTCPeerConnection.createOffer()- Creates a request to find a remote peer with a specific configuration.
RTCPeerConnection.generateCertificate()- Creates and stores an X.509 certificate and corresponding private key then returns an
RTCCertificate, providing access to it. RTCPeerConnection.getConfiguration()- blah
RTCPeerConnection.peerIdentity- blah
RTCPeerConnection.getReceivers()- blah
RTCPeerConnection.getSenders()- blah
RTCPeerConnection.getStats()- Creates a new
RTCStatsReportthat contains and allows access to statistics regarding the connection. RTCPeerConnection.getTransceivers()- blah
RTCPeerConnection.removeTrack()- blah
RTCPeerConnection.setConfiguration()- blah
RTCPeerConnection.setIdentityProvider()- Sets the Identity Provider (IdP) to the triplet given in parameter: its name, the protocol used to communicate with it (optional) and an optional username. The IdP will be used only when an assertion will be needed.
RTCPeerConnection.setLocalDescription()- Changes the local description associated with the connection. The description defines the properties of the connection like its codec. The connection is affected by this change and must be able to support both old and new descriptions. The method takes three parameters, a
RTCSessionDescriptionobject to set, and two callbacks, one called if the change of description succeeds, another called if it failed. RTCPeerConnection.setRemoteDescription()- Changes the remote description associated with the connection. The description defines the properties of the connection like its codec. The connection is affected by this change and must be able to support both old and new descriptions. The method takes three parameters, a
RTCSessionDescriptionobject to set, and two callbacks, one called if the change of description succeeds, another called if it failed.
Obsolete methods
These methods were included in older versions of the WebRTC specification, but have since been removed.
Editors: We need to update these to cover how to replace them.
RTCPeerConnection.addStream()- Adds a
MediaStreamas a local source of audio or video. If the negotiation already happened, a new one will be needed for the remote peer to be able to use it. RTCPeerConnection.createDTMFSender()- Creates a new
RTCDTMFSender, associated to a specificMediaStreamTrack, that will be able to send DTMF phone signaling over the connection. RTCPeerConnection.getIdentityAssertion()- Initiates the gathering of an identity assertion. This has an effect only if the
signalingStateis not"closed". It is not expected for the application dealing with theRTCPeerConnection: this is automatically done; an explicit call only allows to anticipate the need. RTCPeerConnection.getLocalStreams()- Returns an array of
MediaStreamassociated with the local end of the connection. The array may be empty. RTCPeerConnection.getRemoteStreams()- Returns an array of
MediaStreamassociated with the remote end of the connection. The array may be empty. RTCPeerConnection.getStreamById()- Returns the
MediaStreamwith the given id that is associated with local or remote end of the connection. If no stream matches, it returnsnull. RTCPeerConnection.removeStream()- Removes a
MediaStreamas a local source of audio or video. If the negotiation already happened, a new one will be needed for the remote peer to stop using it. RTCPeerConnection.setIdentityProvider()- Sets the Identity Provider (IdP) to the triplet given in parameter: its name, the protocol used to communicate with it (optional) and an optional username. The IdP will be used only when an assertion will be needed.
Constructor
new RTCPeerConnection(
optional RTCConfiguration configuration
);
Creates and returns a new RTCPeerConnection object.
Specifications
| Specification | Status | Comment |
|---|---|---|
| WebRTC 1.0: Real-time Communication Between Browser | Working Draft | Initial definition. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | (Yes) | 22 (22) -moz until Firefox 44 |
? | ? | ? |
| Feature | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| Basic support | No support | (Yes) | 22 (22) -moz until Firefox 44 |
22 | ? | ? | ? | (Yes) |