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
diff --git a/webrtc/tools/rtcbot/bot/browser/bot.js b/webrtc/tools/rtcbot/bot/browser/bot.js
index 8839802..808d88a 100644
--- a/webrtc/tools/rtcbot/bot/browser/bot.js
+++ b/webrtc/tools/rtcbot/bot/browser/bot.js
@@ -48,12 +48,49 @@
     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 @@
     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 36675cc..23a3f9c 100644
--- a/webrtc/tools/rtcbot/test.js
+++ b/webrtc/tools/rtcbot/test.js
@@ -79,7 +79,8 @@
 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]);