W3cubDocs

/DOM

RTCPeerConnection.getStats

The RTCPeerConnection method getStats() returns a promise which resolves with data providing statistics about either the overall connection or about the specified MediaStreamTrack.

Syntax

promise = rtcPeerConnection.getStats(selector)

Parameters

selector Optional
A MediaStreamTrack for which to gather statistics. If null (the default value), statistics will be gathered for the entire RTCPeerConnection.

Return value

A Promise which resolves with an RTCStatsReport object providing connection statistics. The contents of the report depend on the selector as well as other details of the connection.

Exceptions

This method does not throw exceptions; instead, it rejects the returned promise with one of the following errors:

InvalidAccessError
There is no RTCRtpSender or RTCRtpReceiver whose track matches the specified selector, or selector matches more than one sender or receiver.

Example

This example creates a periodic function using setInterval() that collects statistics for an RTCPeerConnection every second, generating an HTML-formatted report and inserting it into a specific element in the DOM.

window.setInterval(function() {
  myPeerConnection.getStats(null).then(stats => {
    let statsOutput = "";

    stats.forEach(report => {
      statsOutput += `<h2>Report: ${report.type}</h3>\n<strong>ID:</strong> ${report.id}<br>\n` +
                     `<strong>Timestamp:</strong> ${report.timestamp}<br>\n`;
      
      // Now the statistics for this report; we intentially drop the ones we
      // sorted to the top above

      Object.keys(report).forEach(statName => {
        if (statName !== "id" && statName !== "timestamp" && statName !== "type") {
          statsOutput += `<strong>${statName}:</strong> ${report[statName]}<br>\n`;
        }
      });
    });

    document.querySelector(".stats-box").innerHTML = statsOutput;
  });
}, 1000);

This works by calling getStats(), then, when the promise is resolved, iterates over the RTCStats objects on the returned RTCStatsReport. A section is created for each report with a header and all of the statistics below, with the type, ID, and timestamp handled specially to place them at the top of the list.

Once the HTML for the report is generated, it is injected into the element whose class is "stats-box" by setting its innerHTML property.

Specifications

Browser compatibility

No compatibility data found. Please contribute data for "api.RTCPeerConnection.getStats" (depth: 1) to the MDN compatibility data repository.

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/getStats