WebRTC Statistics API - Web APIs 编辑

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

Reference

The RTCStatsReport object contains a map of named objects based one of the RTCStats dictionary's subclasses. Upon looking up a statistic category by name, you get an object containing the corresponding data. The table below shows the statistic categories and the corresponding dictionaries; for each statistic category, the full hierarchy of RTCStats-based dictionaries are listed, so you can easily find all the available values.

Mapping of statistic category names to the dictionaries they implement
Statistic category name (RTCStatsType)DescriptionDictionaries implemented
candidate-pairStatistics describing the change from one RTCIceTransport to another, such as during an ICE restart.
certificateStatistics about a certificate being used by an RTCIceTransport.
codecStatistics about a specific codec being used by streams being sent or received by this connection.RTCCodecStats
RTCStats
csrcStatistics for a single contributing source (CSRC) that contributed to one of the connection's inbound RTP streams.
data-channelStatistics related to one RTCDataChannel on the connection.
ice-serverStatistics about the connection to an ICE server (STUN or TURN.
inbound-rtpStatistics describing the state of one of the connection's inbound data streams.
local-candidateStatistics about a local ICE candidate associated with the connection's RTCIceTransports.
media-sourceStatistics about the media produced by the MediaStreamTrack attached to an RTP sender. The dictionary this key maps to depends on the track's kind.RTCAudioSourceStats or RTCVideoSourceStats
outbound-rtpStatistics describing the state of one of the outbound data streams on this connection.
peer-connectionStatistics describing the state of the RTCPeerConnection.
receiverStatistics related to a specific RTCRtpReceiver and the media associated with that receiver. The specific type of object representing the statistics depends on the media kind.
remote-candidateStatistics about a remote ICE candidate associated with the connection's RTCIceTransports.
remote-inbound-rtpStatistics describing the state of the inbound data stream from the perspective of the remote peer.
remote-outbound-rtpStatistics describing the state of the outbound data stream from the perpsective of the remote peer.
sctp-transportStatistics about an RTCSctpTransport.
senderStatistics related to a specific RTCRtpSender and the media associated with that sender. The type of object representing this statistic depends on the kind of the media.
stream This is an obsolete API and is no longer guaranteed to work.Statistics about a particular media MediaStream. This has been obsoleted since the transition to WebRTC being track-based rather than stream-based.
track This is an obsolete API and is no longer guaranteed to work.

Statistics related to a specific MediaStreamTrack's attachment to one of the connection's senders or receivers. The referenced object's type depends on the track type.

These statistics have all been moved to media-source, sender, receiver, outbound-rtp, and inbound-rtp, and this statistic category type is thus obsolete and shouldn't be used anymore.

transceiverStatistics related to a specific RTCRtpTransceiver.
transportStatistics about a transport used by the connection.

Specifications

SpecificationStatusComment
Identifiers for WebRTC's Statistics API
The definition of 'WebRTC statistics types' in that specification.
Candidate RecommendationCompatibility for individual statistic types
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCStatsReport' in that specification.
Candidate RecommendationCompatibility of statistic reporting

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:44 次

字数:25486

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文