WebRTC getStat()API设置 [英] WebRTC getStat() API Set UP
问题描述
我正在尝试使用WebRTC api中的getStat()来查看它是否提供任何有用的信息来衡量延迟和其他视频流数据.问题在于没有太多有关如何使用它的信息. 即使是较旧的现有示例也很少见,但此后api发生了变化.
I am trying to use getStat() from WebRTC's api to see if it provides any useful info measure latency and other video streaming data. The problem is that there's not much info of how to use it. Even older existing examples are pretty rare but the api has changed since then.
例如,我的设置:
peerconnection.getStats(function(stats) {
console.log(stats); } ));
这将返回带有2个函数的RTCStatsResponse对象
This returns a RTCStatsResponse object with 2 functions
RTCStatsResponse {result: function, namedItem: function}
尝试调用result()函数将返回一个RTCStatsReport对象数组,其中第一个对象的类型为"googLibjingleSession",第二个对象的类型为"googTrack".尝试调用另一个nameItem函数时未定义
Trying to call that result() function returns an array of RTCStatsReport objects with type 'googLibjingleSession' for the 1st object and type 'googTrack' for the 2nd object. The other nameItem function is undefined when trying to call it
[RTCStatsReport, RTCStatsReport]
可用的少量信息( https://groups.google.com/forum /#!topic/discuss-webrtc/fpr4yn4-3sg ),我会得到比当前更多的具有更多有用信息的RTCStatObjects.
From what little info available (https://groups.google.com/forum/#!topic/discuss-webrtc/fpr4yn4-3sg), I would be getting alot more RTCStatObjects with more useful info than I am currently getting.
有人有使用webrtc的getStats的经验吗?我相信我可能没有正确执行此操作
Does anyone have experience with using webrtc's getStats? I believe I may not be doing this correctly
推荐答案
以下解决方案对我有用.
The following solution works for me.
创建对等连接
pc = new RTCPeerConnection(pc_config, pc_constraints);
添加onaddstream处理程序
Adding onaddstream handler
pc.onaddstream = onRemoteStreamAdded;
处理程序本身
var onRemoteStreamAdded = function(event) {
attachMediaStream(remoteVideo, event.stream);
remoteStream = event.stream;
getStats(pc);
};
请注意从处理程序调用的getStats函数,该函数在后面
Pay attention on the getStats function called from the handler, the function is following
function getStats(peer) {
myGetStats(peer, function (results) {
for (var i = 0; i < results.length; ++i) {
var res = results[i];
console.log(res);
}
setTimeout(function () {
getStats(peer);
}, 1000);
});
}
myGetStats函数是一个包装器,可使其在不同的浏览器中通用;
The myGetStats function is a wrapper to make it possible universal in different browsers;
function myGetStats(peer, callback) {
if (!!navigator.mozGetUserMedia) {
peer.getStats(
function (res) {
var items = [];
res.forEach(function (result) {
items.push(result);
});
callback(items);
},
callback
);
} else {
peer.getStats(function (res) {
var items = [];
res.result().forEach(function (result) {
var item = {};
result.names().forEach(function (name) {
item[name] = result.stat(name);
});
item.id = result.id;
item.type = result.type;
item.timestamp = result.timestamp;
items.push(item);
});
callback(items);
});
}
};
每秒它将获取统计信息并将原始对象打印到控制台日志中.您可以解析日志,然后更改代码,获取必要的对象字段.
Every second it will get statistics and print raw object into console log. You can parse the log and then change the code, getting necessary object's field.
这篇关于WebRTC getStat()API设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!