RTCStatsReport: forEach() 方法
RTCStatsReport 接口的 forEach() 方法会按照插入顺序,对 RTCStatsReport 对象中的每一对键/值执行一次提供的函数。
键是监视的统计对象(统计数据来源于这些对象)的唯一 id 值,而关联的值是统计信息字典对象。
该方法在其他方面与 Map.prototype.forEach() 相同。
语法
js
forEach(callbackFn)
forEach(callbackFn, thisArg)
参数
callbackFn-
一个函数,为报告中的每个条目执行。该函数将使用以下参数调用:
thisArg可选-
执行
callbackFn时用作this的值。
返回值
示例
给定一个变量 myPeerConnection,它是 RTCPeerConnection 的一个实例,代码调用 getStats() 并使用 await 等待统计报告。然后,它使用 RTCStatsReport.forEach() 迭代报告,并筛选出 type 为 inbound-rtp 且 kind 为 video 的字典。对于匹配的字典,它会记录入站视频的 framesPerSecond 属性。
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);
}
});
规范
| 规范 |
|---|
| WebRTC:浏览器中的实时通信 # dom-rtcstatsreport |
浏览器兼容性
加载中…