From 24f62e1a28f8e3f9e50e55c2ac271e2b063c02f0 Mon Sep 17 00:00:00 2001 From: "houssainy@google.com" Date: Mon, 29 Sep 2014 09:36:28 +0000 Subject: [PATCH] Adding getStats function to the exposed PeerConnection in RtcBot Exposed Peerconnection object has new function "getStats". This function returns the stats as array of reports, and each report is RTCStatReport with additional attributes names and stats. names: array of all the stat names in current report. Stats: dictionary and the key is the stat name, and value is the value of this stat. R=andresp@webrtc.org Review URL: https://webrtc-codereview.appspot.com/23779004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@7319 4adac7df-926f-26a2-2b94-8c16560cd09d --- webrtc/tools/rtcbot/bot/browser/bot.js | 41 ++++++++++++++++++++++++-- webrtc/tools/rtcbot/test.js | 3 +- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/webrtc/tools/rtcbot/bot/browser/bot.js b/webrtc/tools/rtcbot/bot/browser/bot.js index 8839802134..808d88a8b1 100644 --- a/webrtc/tools/rtcbot/bot/browser/bot.js +++ b/webrtc/tools/rtcbot/bot/browser/bot.js @@ -48,12 +48,49 @@ function createPeerConnection(doneCallback, failCallback) { pc.addStream(tempStream); }; + // Return an array of Objects, each Object is a copy of RTCStateReport + // and has the following attributes (id, type, names, and stats). + // names: array originaly returned by calling RTCStateReport.names(). + // stats: dictionary of stat name as key and stat value as dictionary + // value. + obj.getStats = function(callback, mediaTrack) { + pc.getStats(onStatsReady, mediaTrack); + + function onStatsReady(stateResponse) { + var outputReports = []; + var reports = stateResponse.result(); + for (index in reports) { + var report = {}; + report.id = reports[index].id; + report.type = reports[index].type; + report.names = reports[index].names(); + report.stats = []; + populateStats(reports[index], report.stats); + + outputReports.push(report); + } + + callback(outputReports); + } + + function populateStats(report, stats) { + var names = report.names(); + for (index in names) { + stats.push({ + name: names[index], + stat: report.stat(names[index]), + }); + } + + } + }; + pc.addEventListener('addstream', function(event) { remoteStreams[event.stream.id] = event.stream; }); doneCallback(obj); -} +}; function showStream(streamId, autoplay, muted) { var stream = getStreamFromIdentifier_(streamId); @@ -74,7 +111,7 @@ function getStreamFromIdentifier_(id) { return tempStream; console.log(id + " is not id for stream."); return null; -} +}; connectToServer({ ping: ping, diff --git a/webrtc/tools/rtcbot/test.js b/webrtc/tools/rtcbot/test.js index 36675ccbf1..23a3f9c219 100644 --- a/webrtc/tools/rtcbot/test.js +++ b/webrtc/tools/rtcbot/test.js @@ -79,7 +79,8 @@ Test.prototype = { function runTest(botType, testfile) { console.log("Running test: " + testfile); var script = vm.createScript(fs.readFileSync(testfile), testfile); - script.runInNewContext({ test: new Test(botType) }); + script.runInNewContext({ test: new Test(botType), setInterval: setInterval + setTimeout: setTimeout }); } runTest(process.argv[2], process.argv[3]);