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 RTCPeerConnection constructor with a new constructor | |
6 // which tracks all created connections. It does this by periodically gathering | |
7 // statistics on all connections, using the WebRTC statistics API. All reports | |
8 // are gathered into window.peerConnectionReports, which contains one list per | |
9 // connection. In each list there is a number of report batches, which in turn | |
10 // contains metric names mapped to values. | |
11 | |
12 window.peerConnectionReports = []; | |
13 | |
14 RTCPeerConnection = 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 = RTCPeerConnection; | |
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 |