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

Properties

This interface inherits properties from its parent interface, EventTarget.

RTCPeerConnection.canTrickleIceCandidates Read only
true if the remote peer is able to accept trickled ICE candidates, otherwise false. This determination is made by inspecting the remote description as set when the local RTCPeerConnection.setRemoteDescription() function has been called. If that has not yet been done, this value is null (not false, but null).
RTCPeerConnection.connectionState Read only
TBD
RTCPeerConnection.currentLocalDescription Read only
TBD
RTCPeerConnection.currentRemoteDescription Read only
TBD
RTCPeerConnection.defaultIceServers Read only
Returns an array of RTCIceServers which describe the default ICE servers which the browser's ICE agent will fall back on if none are provided in the configuration of the RTCPeerConnection.
RTCPeerConnection.iceConnectionState Read only
Returns an enum of type RTCIceConnectionState that describes the ICE connection state for the connection. When this value changes, a iceconnectionstatechange event is fired on the object.
RTCPeerConnection.iceGatheringState Read only
Returns an enum of type RTCIceGatheringState that describes the ICE gathering state for the connection.
RTCPeerConnection.idpLoginUrl Read only
TBD
RTCPeerConnection.localDescription Read only
Returns a RTCSessionDescription describing the session for the local end of the connection. If it has not yet been set, it can be null.
RTCPeerConnection.peerIdentity Read 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 return null. Once set, via the appropriate method, it can't be changed.
RTCPeerConnection.pendingLocalDescription Read only
TBD
RTCPeerConnection.pendingRemoteDescription Read only
TBD
RTCPeerConnection.remoteDescription Read only
TBD
RTCPeerConnection.sctp Read only
Returns an object of type RTCSctpTransport which provides access to the underlying DTLS transport used to send and receive the packets for all RTCDataChannels on this peer connection.
RTCPeerConnection.signalingState Read only
Returns an enum of type RTCSignalingState that describes the peer connection's signaling state.

Event handlers

RTCPeerConnection.onconnectionstatechange
TBD
RTCPeerConnection.ondatachannel
Is the event handler called when the datachannel event is received. Such an event is sent when a RTCDataChannel is added to this connection.
RTCPeerConnection.onnegotiationneeded
Is the event handler called when the negotiationneeded event, 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 icecandidate event is received. Such an event is sent when a RTCICECandidate object is added to the script.
RTCPeerConnection.onicecandidateerror
TBD
RTCPeerConnection.oniceconnectionstatechange
Is the event handler called when the iceconnectionstatechange event is received. Such an event is sent when the value of iceConnectionState changes.
RTCPeerConnection.onicegatheringstatechange
TBD
RTCPeerConnection.onsignalingstatechange
Is the event handler called when the signalingstatechange event, sent when the value of signalingState changes, is received.
RTCPeerConnection.ontrack
TBD

Old or extension event handlers; they'll be sorted through soon:

RTCPeerConnection.onaddstream
Is the event handler called when the addstream event is received. Such an event is sent when a MediaStream is added to this connection by the remote peer. The event is sent immediately after the call RTCPeerConnection.setRemoteDescription() and doesn't wait for the result of the SDP negotiation.
RTCPeerConnection.onidentityresult
Is the event handler called when the identityresult event is received. Such an event is sent when an identity assertion is generated, via getIdentityAssertion(), or during the creation of an offer or an answer.
RTCPeerConnection.onidpassertionerror
Is the event handler called when the idpassertionerror event 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 idpvalidationerror event 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 peeridentity event, sent when a peer identity has been set and verified on this connection, is received.
RTCPeerConnection.onremovestream
Is the event handler called when the removestream event, sent when a MediaStream is 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.

RTCIceConnectionState enum

The RTCIceConnectionState enum defines the string constants used to describe the current state of the ICE agent and its connection to the ICE server (that is, the STUN or TURN server).

Constant Description
"new" The ICE agent is gathering addresses or is waiting to be given remote candidates through calls to RTCPeerConnection.addIceCandidate() (or both).
"checking" The ICE agent has been given one or more remote candidates and is checking pairs of local and remote candidates against one another to try to find a compatible match, but has not yet found a pair which will allow the peer connection to be made. It's possible that gathering of candidates is also still underway.
"connected" A usable pairing of local and remote candidates has been found for all components of the connection, and the connection has been established. It's possible that gathering is still underway, and it's also possible that the ICE agent is still checking candidates against one another looking for a better connection to use.
"completed" The ICE agent has finished gathering candidates, has checked all pairs against one another, and has found a connection for all components.
"failed" The ICE candidate has checked all candidates pairs against one another and has failed to find compatible matches for all components of the connection. It is, however, possible that the ICE agent did find compatible connections for some components.
"disconnected" Checks to ensure that components are still connected failed for at least one component of the RTCPeerConnection. This is a less stringent test than "failed" and may trigger intermittently and resolve just as spontaneously on less reliable networks.
"closed" The ICE agent for this RTCPeerConnection has shut down and is no longer handling requests.

RTCIceGatheringState enum

The RTCIceGatheringState enum defines string constants which reflect the current status of ICE gathering, as returned using the RTCPeerConnection.iceGatheringState property. You can detect when this value changes by watching for an event of type icegatheringstatechange.

Constant Description
"new" The peer connection was just created and hasn't done any networking yet.
"gathering" The ICE agent is in the process of gathering candidates for the connection.
"complete" The ICE agent has finished gathering candidates. If something happens that requires collecting new candidates, such as a new interface being added or the addition of a new ICE server, the state will revert to "gathering" to gather those candidates.

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
"all" All ICE candidates will be considered.
"public" Only ICE candidates with public IP addresses will be considered. Removed from the specification's May 13, 2016 working draft.
"relay" Only ICE candidates whose IP addresses are being relayed, such as those being passed through a TURN server, will be considered.

RTCPeerConnectionState enum

The RTCPeerConnectionState enum defines string constants which describe states in which the RTCPeerConnection may be. These values are returned by the connectionState property. This state essentially represents the aggregate state of all ICE transports (which are of type RTCIceTransport or RTCDtlsTransport) being used by the connection.

Constant Description
"new" At least one of the connection's ICE transports (RTCIceTransports or RTCDtlsTransports) are in the "new" state, and none of them are in one of the following states: "connecting", "checking", "failed", or "disconnected", or all of the connection's transports are in the "closed" state.
"connecting" One or more of the ICE transports are currently in the process of establishing a connection; that is, their RTCIceConnectionState is either "checking" or "connected", and no transports are in the "failed" state. <<< Make this a link once I know where that will be documented
"connected" Every ICE transport used by the connection is either in use (state "connected" or "completed") or is closed (state "closed"); in addition, at least one transport is either "connected" or "completed".
"disconnected" At least one of the ICE transports for the connection is in the "disconnected" state and none of the other transports are in the state "failed", "connecting", or "checking".
"failed" One or more of the ICE transports on the connection is in the "failed" state.
"closed"

The RTCPeerConnection is closed.

This value was in the RTCPeerConnectionState enum (and therefore found by reading the value of signalingState) until the May 13, 2016 draft of the specification.

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.

RTCSignalingState enum

The RTCSignalingState enum specifies the possible values of RTCPeerConnection.signalingState, which indicates where in the process of signaling the exchange of offer and answer the connection currently is.

Constant Description
"stable" There is no ongoing exchange of offer and answer underway. This may mean that the RTCPeerConnection object is new, in which case both the localDescription and remoteDescription are null; it may also mean that negotiation is complete and a connection has been established.
"have-local-offer" The local peer has called RTCPeerConnection.setLocalDescription(), passing in SDP representing an offer (usually created by calling RTCPeerConnection.createOffer()), and the offer has been applied successfully.
"have-remote-offer" The remote peer has created an offer and used the signaling server to deliver it to the local peer, which has set the offer as the remote description by calling RTCPeerConnection.setRemoteDescription().
"have-local-pranswer" The offer sent by the remote peer has been applied and an answer has been created (usually by calling RTCPeerConnection.createAnswer()) and applied by calling RTCPeerConnection.setLocalDescription(). This provisional answer describes the supported media formats and so forth, but may not have a complete set of ICE candidates included. Further candidates will be delivered separately later.
"have-remote-pranswer" A provisional answer has been received and successfully applied in response to an offer previously sent and established by calling setLocalDescription().
"closed"

The connection is closed.

This value moved into the RTCPeerConnectionState enum in the May 13, 2016 draft of the specification, as it reflects the state of the RTCPeerConnection, not the signaling connection. You now detect a closed connection by checking for connectionState to be "closed" instead.

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. In some current browser implementations, this method returns a Promise. See the method page for more information.
RTCPeerConnection.addTrack()
TBD
RTCPeerConnection.addTransceiver()
TBD
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.  In some current browser implementations, this method returns a Promise. See the method page for more information.
RTCPeerConnection.createDataChannel()
Creates a new RTCDataChannel associated 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. In some current browser implementations, this method returns a Promise. See the method page for more information.
RTCPeerConnection.generateCertificate()
Creates and stores an X.509 certificate and corresponding private key then returns an RTCCertificate, providing access to it.
RTCPeerConnection.getConfiguration()
TBD
RTCPeerConnection.peerIdentity
TBD
RTCPeerConnection.getReceivers()
TBD
RTCPeerConnection.getSenders()
TBD
RTCPeerConnection.getStats()
Creates a new RTCStatsReport that contains and allows access to statistics regarding the connection.
RTCPeerConnection.getTransceivers()
TBD
RTCPeerConnection.removeTrack()
TBD
RTCPeerConnection.setConfiguration()
TBD
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 RTCSessionDescription object to set, and two callbacks, one called if the change of description succeeds, another called if it failed. In some current browser implementations, this method returns a Promise. See the method page for more information.
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 RTCSessionDescription object to set, and two callbacks, one called if the change of description succeeds, another called if it failed. In some current browser implementations, this method returns a Promise. See the method page for more information.

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 MediaStream as 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 specific MediaStreamTrack, 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 signalingState is not "closed". It is not expected for the application dealing with the RTCPeerConnection: this is automatically done; an explicit call only allows to anticipate the need.
RTCPeerConnection.getLocalStreams()
Returns an array of MediaStream associated with the local end of the connection. The array may be empty.
RTCPeerConnection.getRemoteStreams()
Returns an array of MediaStream associated with the remote end of the connection. The array may be empty.
RTCPeerConnection.getStreamById()
Returns the MediaStream with the given id that is associated with local or remote end of the connection. If no stream matches, it returns null.
RTCPeerConnection.removeStream()
Removes a MediaStream as 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)

See also

Document Tags and Contributors

 Last updated by: jpmedley,