OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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. |
| 11 |
| 12 window.peerConnectionReports = []; |
| 13 |
| 14 webkitRTCPeerConnection = (function() { |
| 15 function getReportsAsDicts(getStatsResult) { |
| 16 var result = []; |
| 17 getStatsResult.forEach(function(report) { |
| 18 var values = {}; |
| 19 report.names().forEach(function(name) { |
| 20 values[name] = report.stat(name); |
| 21 }); |
| 22 result.push(values); |
| 23 }); |
| 24 return result; |
| 25 } |
| 26 |
| 27 function gatherStatsFromOneConnection(peerConnection) { |
| 28 var connectionId = window.peerConnectionReports.length; |
| 29 window.peerConnectionReports.push([]); |
| 30 var pollIntervalMs = 1000; |
| 31 |
| 32 setInterval(function() { |
| 33 peerConnection.getStats(function(response) { |
| 34 var reports = getReportsAsDicts(response.result()); |
| 35 window.peerConnectionReports[connectionId].push(reports); |
| 36 }); |
| 37 }, pollIntervalMs); |
| 38 } |
| 39 |
| 40 var originalConstructor = webkitRTCPeerConnection; |
| 41 return function() { |
| 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); |
| 46 |
| 47 // Create the object and track it. |
| 48 var peerConnection = new factoryFunction(); |
| 49 gatherStatsFromOneConnection(peerConnection); |
| 50 return peerConnection; |
| 51 } |
| 52 })(); |
OLD | NEW |