RTCStatsReport
BaselineWidely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
* Some parts of this feature may have varying levels of support.
TheRTCStatsReport
interface of theWebRTC API provides a statistics report for aRTCPeerConnection
,RTCRtpSender
, orRTCRtpReceiver
.
AnRTCStatsReport
instance is a read-onlyMap
-like object, in which each key is an identifier for an object for which statistics are being reported, and the corresponding value is a dictionary object providing the statistics.
Instance properties
RTCStatsReport.size
Returns the number of items in the
RTCStatsReport
object.
Instance methods
RTCStatsReport.entries()
Returns a newIterator object that contains a two-member array of
[id, statistic-dictionary]
for each element in theRTCStatsReport
object, in insertion order.RTCStatsReport.forEach()
Calls
callbackFn
once for each key-value pair present in theRTCStatsReport
object, in insertion order.If athisArg
parameter is provided toforEach
, it will be used as thethis
value for each callback.RTCStatsReport.get()
Returns the statistics dictionary associated with the passed
id
, orundefined
if there is none.RTCStatsReport.has()
Returns a boolean indicating whether the
RTCStatsReport
contains a statistics dictionary associated with the specifiedid
.RTCStatsReport.keys()
Returns a newIterator object that contains the keys (IDs) for each element in the
RTCStatsReport
object, in insertion order.RTCStatsReport.values()
Returns a newIterator object that contains the values (statistics object) for each element in the
RTCStatsReport
object, in insertion order.RTCStatsReport[Symbol.iterator]()
Returns a newIterator object that contains a two-member array of
[id, statistic-dictionary]
for each element in theRTCStatsReport
object, in insertion order.
Description
APromise
that resolves to anRTCStatsReport
is returned from theRTCRtpReceiver.getStats()
,RTCRtpSender.getStats()
andRTCPeerConnection.getStats()
methods.CallinggetStats()
on anRTCPeerConnection
lets you specify whether you wish to obtain outbound statistics, inbound statistics, or statistics for the whole connection.TheRTCRtpReceiver
andRTCRtpSender
versions ofgetStats()
only return inbound and outbound statistics, respectively.
The statistics report is a read-onlyMap
-like object: an ordered dictionary, where the properties areid
strings that uniquely identify the WebRTC object that was inspected to produce a particular set of statistics, and the value is a dictionary object containing those statistics.ARTCStatsReport
can be iterated and used the same ways as a read-onlyMap
.
The report may contain many different categories of statistics, including inbound and outbound statistics for both the current and remote ends of the peer connection, information about codecs, certificates and media used, and so on.Each category of statistic is provided in a different type of statistics dictionary object, which can be identified from itstype
property.
Common instance properties
All the dictionary types have the following properties:
id
A string that uniquely identifies the object was monitored to produce the set of statistics.This value persists across reports for (at least) the lifetime of the connection.Note however that for some statistics the ID may vary between browsers and for subsequent connections, even to the same peer.
timestamp
A high resolution timestamp object (
DOMHighResTimeStamp
) object indicating the time at which the sample was taken.Many reported statistics are cumulative values; the timestamp allows rates and averages to be calculated between any two reports, at any desired reporting rate.type
A string with a value that indicates the type of statistics that the object contains, such as
candidate-pair
,inbound-rtp
,certificate
, and so on.Thetypes of statistics and their corresponding objects are listed below.
Users typically iterate aRTCStatsReport
, using aforEach()
orfor...of
loop, selecting the statistics of interest using thetype
property.Once a particular statistic object has been identified using itstype
, theid
property can subsequently be used withget()
to obtain the same statistic report at a different time.
The timestamp can be used to calculate average values for statistics that accumulate over the lifetime of a connection.
The statistic types
The statisticstype
values and their corresponding dictionaries are listed below.
type | Dictionary | Description |
---|---|---|
candidate-pair | RTCIceCandidatePairStats | Statistics describing the change from oneRTCIceTransport to another, such as during anICE restart. |
certificate | RTCCertificateStats | Statistics about a certificate being used by anRTCIceTransport . |
codec | RTCCodecStats | Statistics about a specific codec being used by streams being sent or received by this connection. |
data-channel | RTCDataChannelStats | Statistics related to oneRTCDataChannel on the connection. |
inbound-rtp | RTCInboundRtpStreamStats | Statistics describing the state of one of the connection's inbound data streams. |
local-candidate | RTCIceCandidateStats | Statistics about a local ICE candidate associated with the connection'sRTCIceTransport s. |
media-source | RTCAudioSourceStats orRTCVideoSourceStats | Statistics about the media produced by theMediaStreamTrack attached to an RTP sender. The dictionary this key maps to depends on the track'skind . |
outbound-rtp | RTCOutboundRtpStreamStats | Statistics describing the state of one of the outbound data streams on this connection. |
peer-connection | RTCPeerConnectionStats | Statistics describing the state of theRTCPeerConnection . |
remote-candidate | RTCIceCandidateStats | Statistics about a remote ICE candidate associated with the connection'sRTCIceTransport s. |
remote-inbound-rtp | RTCRemoteInboundRtpStreamStats | Statistics describing the state of the inbound data stream from the perspective of the remote peer. |
remote-outbound-rtp | RTCRemoteOutboundRtpStreamStats | Statistics describing the state of the outbound data stream from the perspective of the remote peer. |
transport | RTCTransportStats | Statistics about a transport used by the connection. |
Examples
Iterate report from an RTCPeerConnection using forEach loop
This example logs shows how you might log video-related statistics for the localRTCRtpReceiver
responsible for receiving streamed media.
Given a variablemyPeerConnection
, which is an instance ofRTCPeerConnection
, the code usesawait
to wait for the statistics report, and then iterates it usingRTCStatsReport.forEach()
.It then filters the dictionaries for just those reports that have thetype
ofinbound-rtp
andkind
ofvideo
.
const stats = await myPeerConnection.getStats();stats.forEach((report) => { if (report.type === "inbound-rtp" && report.kind === "video") { // Log the frame rate console.log(report.framesPerSecond); }});
Iterate report from an RTCRtpSender using a for...of loop
This example shows how you might iterate the outbound statistics from anRTCRtpSender
.
The code follows a similar pattern to the previous example, but iterates using afor...of
-loop on theRTCStatsReport.values()
, and filters on thetype
ofoutbound-rtp
.It assumes you already have anRTCRtpSender
object named "sender".
const stats = await sender.getStats();for (const stat of stats.values()) { if (stat.type !== "outbound-rtp") continue; Object.keys(stat).forEach((statName) => { console.log(`${statName}: ${report[statName]}`); });}
Specifications
Specification |
---|
WebRTC: Real-Time Communication in Browsers # rtcstatsreport-object |