WebRTC getStat() API 设置 [英] WebRTC getStat() API Set UP

查看:23
本文介绍了WebRTC getStat() API 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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);
    };

注意从handler调用的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆