// Copy and paste code into Google Chrome javascript console
// Pass any instance of RTCPeerConnection to the 'statsFor' function in order to log stats
// Tested on http://googlechrome.github.io/webrtc/samples/web/content/peerconnection/constraints/
// RTCPeerConnection objects exposed as 'remotePeerConnection' and 'localPeerConnection'
// Enter statsFor(localPeerConnection) or statsFor(remotePeerConnection) after pasting code into js console

function statsFor(peer) {
  peer.getStats(function(stats) {
    var results = stats.result();
    var statsString = '';
    for (var i = 0; i < results.length; ++i) {
      var res = results[i];
      statsString += '*** Report ' + i + " ***\n";
      if (!res.local || res.local === res) {
        statsString += getStats(res) + "\n";
      }
    }
    console.log(statsString);
  });
}

function getStats(obj) {
  var statsString = 'Timestamp:' + obj.timestamp + '\n';
  if (obj.id) {
    statsString += 'id: ' + obj.id + '\n';
  }
  if (obj.type) {
    statsString += 'type: ' + obj.type + '\n';
  }
  if (obj.names) {
    var names = obj.names();
    for (var i = 0; i < names.length; ++i) {
      statsString += names[i] + ': ' + obj.stat(names[i]) + '\n';
    }
  } else {
    if (obj.stat('audioOutputLevel')) {
      statsString += '\n' + 'audioOutputLevel: ' + obj.stat('audioOutputLevel') + '\n';
    }
  }
  return statsString;
}