WebRTC API
WebRTC (Web Real-Time Communication) is a technology that enables Web applications and sites to capture and optionally stream audio and/or video media, as well as to exchange arbitrary data between browsers without requiring an intermediary. The set of standards that comprise WebRTC makes it possible to share data and perform teleconferencing peer-to-peer, without requiring that the user install plug-ins or any other third-party software.
WebRTC consists of several interrelated APIs and protocols which work together to achieve this. The documentation you'll find here will help you understand the fundamentals of WebRTC, how to set up and use both data and media connections, and more.
In this article
WebRTC concepts and usage
WebRTC serves multiple purposes; together with theMedia Capture and Streams API, they provide powerful multimedia capabilities to the Web, including support for audio and video conferencing, file exchange, screen sharing, identity management, and interfacing with legacy telephone systems including support for sendingDTMF (touch-tone dialing) signals. Connections between peers can be made without requiring any special drivers or plug-ins, and can often be made without any intermediary servers.
Connections between two peers are represented by theRTCPeerConnection interface. Once a connection has been established and opened usingRTCPeerConnection, media streams (MediaStreams) and/or data channels (RTCDataChannels) can be added to the connection.
Media streams can consist of any number of tracks of media information; tracks, which are represented by objects based on theMediaStreamTrack interface, may contain one of a number of types of media data, including audio, video, and text (such as subtitles or even chapter names). Most streams consist of at least one audio track and likely also a video track, and can be used to send and receive both live media or stored media information (such as a streamed movie).
You can also use the connection between two peers to exchange arbitrary binary data using theRTCDataChannel interface. This can be used for back-channel information, metadata exchange, game status packets, file transfers, or even as a primary channel for data transfer.
Interoperability
WebRTC is in general well supported in modern browsers, but some incompatibilities remain. Theadapter.js library is a shim to insulate apps from these incompatibilities.
WebRTC reference
Because WebRTC provides interfaces that work together to accomplish a variety of tasks, we have divided up the reference by category. Please see the sidebar for an alphabetical list.
Connection setup and management
These interfaces, dictionaries, and types are used to set up, open, and manage WebRTC connections. Included are interfaces representing peer media connections, data channels, and interfaces used when exchanging information on the capabilities of each peer in order to select the best possible configuration for a two-way media connection.
Interfaces
RTCPeerConnectionRepresents a WebRTC connection between the local computer and a remote peer. It is used to handle efficient streaming of data between the two peers.
RTCDataChannelRepresents a bi-directional data channel between two peers of a connection.
RTCDataChannelEventRepresents events that occur while attaching a
RTCDataChannelto aRTCPeerConnection. The only event sent with this interface isdatachannel.RTCSessionDescriptionRepresents the parameters of a session. Each
RTCSessionDescriptionconsists of a descriptiontypeindicating which part of the offer/answer negotiation process it describes and of theSDP descriptor of the session.RTCStatsReportProvides information detailing statistics for a connection or for an individual track on the connection; the report can be obtained by calling
RTCPeerConnection.getStats().RTCIceCandidateRepresents a candidate Interactive Connectivity Establishment (ICE) server for establishing an
RTCPeerConnection.RTCIceTransportRepresents information about anICE transport.
RTCPeerConnectionIceEventRepresents events that occur in relation to ICE candidates with the target, usually an
RTCPeerConnection. Only one event is of this type:icecandidate.RTCRtpSenderManages the encoding and transmission of data for a
MediaStreamTrackon anRTCPeerConnection.RTCRtpReceiverManages the reception and decoding of data for a
MediaStreamTrackon anRTCPeerConnection.RTCTrackEventThe interface used to represent a
trackevent, which indicates that anRTCRtpReceiverobject was added to theRTCPeerConnectionobject, indicating that a new incomingMediaStreamTrackwas created and added to theRTCPeerConnection.RTCSctpTransportProvides information which describes a Stream Control Transmission Protocol (SCTP) transport and also provides a way to access the underlying Datagram Transport Layer Security (DTLS) transport over which SCTP packets for all of an
RTCPeerConnection's data channels are sent and received.
Events
bufferedamountlowThe amount of data currently buffered by the data channel—as indicated by its
bufferedAmountproperty—has decreased to be at or below the channel's minimum buffered data size, as specified bybufferedAmountLowThreshold.closeThe data channel has completed the closing process and is now in the
closedstate. Its underlying data transport is completely closed at this point. You can be notifiedbefore closing completes by watching for theclosingevent instead.closingThe
RTCDataChannelhas transitioned to theclosingstate, indicating that it will be closed soon. You can detect the completion of the closing process by watching for thecloseevent.connectionstatechangeThe connection's state, which can be accessed in
connectionState, has changed.datachannelA new
RTCDataChannelis available following the remote peer opening a new data channel. This event's type isRTCDataChannelEvent.errorAn
RTCErrorEventindicating that an error occurred on the data channel.errorAn
RTCErrorEventindicating that an error occurred on theRTCDtlsTransport. This error will be eitherdtls-failureorfingerprint-failure.gatheringstatechangeThe
RTCIceTransport's gathering state has changed.icecandidateAn
RTCPeerConnectionIceEventwhich is sent whenever the local device has identified a new ICE candidate which needs to be added to the local peer by callingsetLocalDescription().icecandidateerrorAn
RTCPeerConnectionIceErrorEventindicating that an error has occurred while gathering ICE candidates.iceconnectionstatechangeSent to an
RTCPeerConnectionwhen its ICE connection's state—found in theiceConnectionStateproperty—changes.icegatheringstatechangeSent to an
RTCPeerConnectionwhen its ICE gathering state—found in theiceGatheringStateproperty—changes.messageA message has been received on the data channel. The event is of type
MessageEvent.negotiationneededInforms the
RTCPeerConnectionthat it needs to perform session negotiation by callingcreateOffer()followed bysetLocalDescription().openThe underlying data transport for the
RTCDataChannelhas been successfully opened or re-opened.selectedcandidatepairchangeThe currently-selected pair of ICE candidates has changed for the
RTCIceTransporton which the event is fired.trackThe
trackevent, of typeRTCTrackEventis sent to anRTCPeerConnectionwhen a new track is added to the connection following the successful negotiation of the media's streaming.signalingstatechangeSent to the peer connection when its
signalingStatehas changed. This happens as a result of a call to eithersetLocalDescription()orsetRemoteDescription().statechangeThe state of the
RTCDtlsTransporthas changed.statechangeThe state of the
RTCIceTransporthas changed.statechangeThe state of the
RTCSctpTransporthas changed.rtctransformAn encoded video or audio frame is ready to process using a transform stream in a worker.
Types
RTCSctpTransport.stateIndicates the state of an
RTCSctpTransportinstance.
Identity and security
These APIs are used to manage user identity and security, in order to authenticate the user for a connection.
RTCIdentityProviderEnables a user agent is able to request that an identity assertion be generated or validated.
RTCIdentityAssertionRepresents the identity of the remote peer of the current connection. If no peer has yet been set and verified this interface returns
null. Once set it can't be changed.RTCIdentityProviderRegistrarRegisters an identity provider (idP).
RTCCertificateRepresents a certificate that an
RTCPeerConnectionuses to authenticate.
Telephony
These interfaces and events are related to interactivity with Public-Switched Telephone Networks (PSTNs). They're primarily used to send tone dialing sounds—or packets representing those tones—across the network to the remote peer.
Interfaces
RTCDTMFSenderManages the encoding and transmission of Dual-Tone Multi-Frequency (DTMF) signaling for an
RTCPeerConnection.RTCDTMFToneChangeEventUsed by the
tonechangeevent to indicate that a DTMF tone has either begun or ended. This event does not bubble (except where otherwise stated) and is not cancelable (except where otherwise stated).
Events
tonechangeEither a newDTMF tone has begun to play over the connection, or the last tone in the
RTCDTMFSender'stoneBufferhas been sent and the buffer is now empty. The event's type isRTCDTMFToneChangeEvent.
Encoded Transforms
These interfaces and events are used to process incoming and outgoing encoded video and audio frames using a transform stream running in a worker.
Interfaces
RTCRtpScriptTransformAn interface for inserting transform stream(s) running in a worker into the RTC pipeline.
RTCRtpScriptTransformerThe worker-side counterpart of an
RTCRtpScriptTransformthat passes options from the main thread, along with a readable stream and writeable stream that can be used to pipe encoded frames through aTransformStream.RTCEncodedVideoFrameRepresents an encoded video frame to be transformed in the RTC pipeline.
RTCEncodedAudioFrameRepresents an encoded audio frame to be transformed in the RTC pipeline.
Properties
RTCRtpReceiver.transformA property used to insert a transform stream into the receiver pipeline for incoming encoded video and audio frames.
RTCRtpSender.transformA property used to insert a transform stream into the sender pipeline for outgoing encoded video and audio frames.
Events
rtctransformAn RTC transform is ready to run in the worker, or an encoded video or audio frame is ready to process.
Guides
- Introduction to the Real-time Transport Protocol (RTP)
The Real-time Transport Protocol (RTP), defined inRFC 3550, is an IETF standard protocol to enable real-time connectivity for exchanging data that needs real-time priority. This article provides an overview of what RTP is and how it functions in the context of WebRTC.
- Introduction to WebRTC protocols
This article introduces the protocols on top of which the WebRTC API is built.
- WebRTC connectivity
A guide to how WebRTC connections work and how the various protocols and interfaces can be used together to build powerful communication apps.
- Lifetime of a WebRTC session
WebRTC lets you build peer-to-peer communication of arbitrary data, audio, or video—or any combination thereof—into a browser application. In this article, we'll look at the lifetime of a WebRTC session, from establishing the connection all the way through closing the connection when it's no longer needed.
- Establishing a connection: The perfect negotiation pattern
Perfect negotiation is a design pattern which is recommended for your signaling process to follow, which provides transparency in negotiation while allowing both sides to be either the offerer or the answerer, without significant coding needed to differentiate the two.
- Signaling and two-way video calling
A tutorial and example which turns a WebSocket-based chat system created for a previous example and adds support for opening video calls among participants. The chat server's WebSocket connection is used for WebRTC signaling.
- Codecs used by WebRTC
A guide to the codecs which WebRTC requires browsers to support as well as the optional ones supported by various popular browsers. Included is a guide to help you choose the best codecs for your needs.
- Using WebRTC data channels
This guide covers how you can use a peer connection and an associated
RTCDataChannelto exchange arbitrary data between two peers.- Using DTMF with WebRTC
WebRTC's support for interacting with gateways that link to old-school telephone systems includes support for sending DTMF tones using the
RTCDTMFSenderinterface. This guide shows how to do so.- Using WebRTC Encoded Transforms
This guide shows how a web application can modify incoming and outgoing WebRTC encoded video and audio frames, using a
TransformStreamrunning into a worker.
Tutorials
- Improving compatibility using WebRTC adapter.js
The WebRTC organizationprovides on GitHub the WebRTC adapter to work around compatibility issues in different browsers' WebRTC implementations. The adapter is a JavaScript shim which lets your code to be written to the specification so that it will "just work" in all browsers with WebRTC support.
- A simple RTCDataChannel sample
The
RTCDataChannelinterface is a feature which lets you open a channel between two peers over which you may send and receive arbitrary data. The API is intentionally similar to theWebSocket API, so that the same programming model can be used for each.- Building an internet connected phone with Peer.js
This tutorial is a step-by-step guide on how to build a phone using Peer.js
Specifications
| Specification |
|---|
| WebRTC: Real-Time Communication in Browsers> |
| Media Capture and Streams> |
| Media Capture from DOM Elements> |
WebRTC-proper protocols
Related supporting protocols
- Interactive Connectivity Establishment (ICE): A Protocol for Network Address Translator (NAT) Traversal for Offer/Answer Protocol
- Session Traversal Utilities for NAT (STUN)
- URI Scheme for the Session Traversal Utilities for NAT (STUN) Protocol
- Traversal Using Relays around NAT (TURN) Uniform Resource Identifiers
- An Offer/Answer Model with Session Description Protocol (SDP)
- Session Traversal Utilities for NAT (STUN) Extension for Third Party Authorization
See also
MediaDevicesMediaStreamEventMediaStreamTrackMessageEventMediaStream- Media Capture and Streams API
- Firefox multistream and renegotiation for Jitsi Videobridge
- Peering Through the WebRTC Fog with SocketPeer
- Inside the Party Bus: Building a Web App with Multiple Live Video Streams + Interactive Graphics
- Web media technologies