W3cubDocs

/Web APIs

RTCStatsReport

The RTCStatsReport interface of the WebRTC API provides a statistics report for a RTCPeerConnection, RTCRtpSender, or RTCRtpReceiver.

An RTCStatsReport instance is a read-only Map-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 new Iterator object that contains a two-member array of [id, statistic-dictionary] for each element in the RTCStatsReport object, in insertion order.

RTCStatsReport.forEach()

Calls callbackFn once for each key-value pair present in the RTCStatsReport object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.

RTCStatsReport.get()

Returns the statistics dictionary associated with the passed id, or undefined if there is none.

RTCStatsReport.has()

Returns a boolean indicating whether the RTCStatsReport contains a statistics dictionary associated with the specified id.

RTCStatsReport.keys()

Returns a new Iterator object that contains the keys (IDs) for each element in the RTCStatsReport object, in insertion order.

RTCStatsReport.values()

Returns a new Iterator object that contains the values (statistics object) for each element in the RTCStatsReport object, in insertion order.

RTCStatsReport.[@@iterator]()

Returns a new Iterator object that contains a two-member array of [id, statistic-dictionary] for each element in the RTCStatsReport object, in insertion order.

Description

A Promise that resolves to an RTCStatsReport is returned from the RTCRtpReceiver.getStats(), RTCRtpSender.getStats() and RTCPeerConnection.getStats() methods. Calling getStats() on an RTCPeerConnection lets you specify whether you wish to obtain outbound statistics, inbound statistics, or statistics for the whole connection. The RTCRtpReceiver and RTCRtpSender versions of getStats() only return inbound and outbound statistics, respectively.

The statistics report is a read-only Map-like object: an ordered dictionary, where the properties are id 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. A RTCStatsReport can be iterated and used the same ways as a read-only Map.

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 its type 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. The types of statistics and their corresponding objects are listed below.

Users typically iterate a RTCStatsReport, using a forEach() or for...of loop, selecting the statistics of interest using the type property. Once a particular statistic object has been identified using its type, the id property can subsequently be used with get() 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 statistics type values and their corresponding dictionaries are listed below.

type Dictionary Description
candidate-pair RTCIceCandidatePairStats Statistics describing the change from one RTCIceTransport to another, such as during an ICE restart.
certificate RTCCertificateStats Statistics about a certificate being used by an RTCIceTransport.
codec RTCCodecStats Statistics about a specific codec being used by streams being sent or received by this connection.
data-channel RTCDataChannelStats Statistics related to one RTCDataChannel 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's RTCIceTransports.
media-source RTCAudioSourceStats or RTCVideoSourceStats Statistics about the media produced by the MediaStreamTrack attached to an RTP sender. The dictionary this key maps to depends on the track's kind.
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 the RTCPeerConnection.
remote-candidate RTCIceCandidateStats Statistics about a remote ICE candidate associated with the connection's RTCIceTransports.
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 local RTCRtpReceiver responsible for receiving streamed media.

Given a variable myPeerConnection, which is an instance of RTCPeerConnection, the code uses await to wait for the statistics report, and then iterates it using RTCStatsReport.forEach(). It then filters the dictionaries for just those reports that have the type of inbound-rtp and kind of video.

js

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 an RTCRtpSender.

The code follows a similar pattern to the previous example, but iterates using a for...of-loop on the RTCStatsReport.values(), and filters on the type of outbound-rtp. It assumes you already have an RTCRtpSender object named "sender".

js

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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
@@iterator 58 79 48 No 45 11 58 58 48 43 11 7.0
RTCStatsReport 58 79 27 No 45 11 58 58 27 43 11 7.0
entries 58 79 48 No 45 11 58 58 48 43 11 7.0
forEach 58 79 27 No 45 11 58 58 27 43 11 7.0
get 58 79 27 No 45 11 58 58 27 43 11 7.0
has 58 79 27 No 45 11 58 58 27 43 11 7.0
keys 58 79 48 No 45 11 58 58 48 43 11 7.0
size 59 79 48 No 46 11 59 59 48 43 11 7.0
type_candidate-pair 58 79 29 No 45 11 58 58 29 43 11 7.0
type_certificate 80 80 No No 67 13.1 80 80 No 57 13.4 13.0
type_codec 80 80 98 No 67 13.1 80 80 98 57 13.4 13.0
type_data-channel 80 80 79 No 67 13.1 80 80 No 57 13.4 13.0
type_inbound-rtp 79 79 27 No 66 11 79 79 27 57 11 12.0
type_local-candidate 80 80 27 No 67 12.1 80 80 27 57 12.2 13.0
type_media-playout 111 111 No No 97 No 111 111 No No No 22.0
type_media-source 80 80 113 No 67 14.1 80 80 113 57 14.5 13.0
type_outbound-rtp 80 80 27 No 67 11 80 80 27 57 11 13.0
type_peer-connection 80 80 113 No 67 13.1 80 80 113 57 13.4 13.0
type_remote-candidate 80 80 27 No 67 12.1 80 80 27 57 12.2 13.0
type_remote-inbound-rtp 80 80 27 No 67 11 80 80 27 57 11 13.0
type_remote-outbound-rtp 91 91 27 No 77 11 91 91 27 64 11 16.0
type_transport 80 80 No No 67 13.1 80 80 No 57 13.4 13.0
values 58 79 48 No 45 11 58 58 48 43 11 7.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/RTCStatsReport