WebRTC getStat() API Set UP

南笙酒味 提交于 2019-11-30 13:50:33

The following solution works for me.

Creating peer connection

pc = new RTCPeerConnection(pc_config, pc_constraints);

Adding onaddstream handler

pc.onaddstream = onRemoteStreamAdded;

The handler itself

var onRemoteStreamAdded = function(event) {
        attachMediaStream(remoteVideo, event.stream);
        remoteStream = event.stream;

        getStats(pc);
    };

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);
    });
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!