1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file overwrites the webkitRTCPeerConnection constructor with a
6 // new constructor which tracks all created connections. It does this by
7 // periodically gathering statistics on all connections, using the WebRTC
8 // statistics API. All reports are gathered into window.peerConnectionReports,
9 // which contains one list per connection. In each list there is a number of
10 // report batches, which in turn contains metric names mapped to values.
12 window.peerConnectionReports = [];
14 webkitRTCPeerConnection = (function() {
15 function getReportsAsDicts(getStatsResult) {
17 getStatsResult.forEach(function(report) {
19 report.names().forEach(function(name) {
20 values[name] = report.stat(name);
27 function gatherStatsFromOneConnection(peerConnection) {
28 var connectionId = window.peerConnectionReports.length;
29 window.peerConnectionReports.push([]);
30 var pollIntervalMs = 1000;
32 setInterval(function() {
33 peerConnection.getStats(function(response) {
34 var reports = getReportsAsDicts(response.result());
35 window.peerConnectionReports[connectionId].push(reports);
40 var originalConstructor = webkitRTCPeerConnection;
42 // Bind the incoming arguments to the original constructor.
43 var args = [null].concat(Array.prototype.slice.call(arguments));
44 var factoryFunction = originalConstructor.bind.apply(
45 originalConstructor, args);
47 // Create the object and track it.
48 var peerConnection = new factoryFunction();
49 gatherStatsFromOneConnection(peerConnection);
50 return peerConnection;