WebRTC Statistics API

Draft
This page is not complete.

The WebRTC API has a vast array of statistics available, covering the entire breadth of the WebRTC connectivity system, from sender to receiver and peer to peer.

Collecting statistics

You can collect statistics at various levels throughout the WebRTC hierarchy of objects. Most broadly, you can call getStats() on an RTCPeerConnection to get statistics for the connection overall. In this example, a new RTCPeerConnection is created, and then setInterval() is used to set the function getConnectionStats() to be called every second.

That function, in turn, uses getStats() to obtain statistics for the connection and to make use of that data.

try {
  myPeerConnection = new RTCPeerConnection(pcOptions);

  statsInterval = window.setInterval(getConnectionStats, 1000);
  /* add event handlers, etc */
} catch(err) {
  console.error("Error creating RTCPeerConnection: " + err);
}

function getConnectionStats() {  
  myPeerConnection.getStats(null).then(stats => {
    var statsOutput = "";
 
    stats.forEach(report => {
      if (report.type === "inbound-rtp" && report.kind === "video") {
        Object.keys(report).forEach(statName => {
          statsOutput += `<strong>${statName}:</strong> ${report[statName]}<br>\n`;
        });
      }
    });
    
    document.querySelector(".stats-box").innerHTML = statsOutput;
  });  
}

When the promise returned by getStats() is fulfilled, the resolution handler receives as input an RTCStatsReport object containing the statistics information. This object contains a Map of named dictionaries based on RTCStats and its affiliated types.

This example specifically looks for the report whose type is inbound-rtp and whose kind is video. This way, we look only at the video-related statistics for the local RTCRtpReceiver responsible for receiving the streamed media.

Commonly used statistics

 

Statistics dictionary hierarchy

The RTCStats object you receive contains a set of named dictionaries, each describing a different set of statistics. Many of these dictionaries are constructed using a set of sub-dictionaries which are chosen depending on the type of statistics being collected and how they were received.

 

Specifications

https://w3c.github.io/webrtc-pc/#mandatory-to-implement-stats