1 // Copyright (c) 2013 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.
6 var ssrcInfoManager
= null;
7 var peerConnectionUpdateTable
= null;
9 var dumpCreator
= null;
10 /** A map from peer connection id to the PeerConnectionRecord. */
11 var peerConnectionDataStore
= {};
13 /** A simple class to store the updates and stats data for a peer connection. */
14 var PeerConnectionRecord
= (function() {
16 function PeerConnectionRecord() {
27 PeerConnectionRecord
.prototype = {
34 * Adds the initilization info of the peer connection.
35 * @param {string} url The URL of the web page owning the peer connection.
36 * @param {Array} servers STUN servers used by the peer connection.
37 * @param {!Object} constraints Media constraints.
39 initialize: function(url
, servers
, constraints
) {
40 this.record_
.url
= url
;
41 this.record_
.servers
= servers
;
42 this.record_
.constraints
= constraints
;
46 * @param {string} dataSeriesId The TimelineDataSeries identifier.
47 * @return {!TimelineDataSeries}
49 getDataSeries: function(dataSeriesId
) {
50 return this.record_
.stats
[dataSeriesId
];
54 * @param {string} dataSeriesId The TimelineDataSeries identifier.
55 * @param {!TimelineDataSeries} dataSeries The TimelineDataSeries to set to.
57 setDataSeries: function(dataSeriesId
, dataSeries
) {
58 this.record_
.stats
[dataSeriesId
] = dataSeries
;
62 * @param {string} type The type of the update.
63 * @param {string} value The value of the update.
65 addUpdate: function(type
, value
) {
66 this.record_
.updateLog
.push({
67 time
: (new Date()).toLocaleString(),
74 return PeerConnectionRecord
;
77 // The maximum number of data points bufferred for each stats. Old data points
78 // will be shifted out when the buffer is full.
79 var MAX_STATS_DATA_POINT_BUFFER_SIZE
= 1000;
81 <include src
="tab_view.js"/>
82 <include src
="data_series.js"/>
83 <include src
="ssrc_info_manager.js"/>
84 <include src
="stats_graph_helper.js"/>
85 <include src
="stats_table.js"/>
86 <include src
="peer_connection_update_table.js"/>
87 <include src
="dump_creator.js"/>
90 function initialize() {
91 dumpCreator
= new DumpCreator($('content-root'));
92 tabView
= new TabView($('content-root'));
93 ssrcInfoManager
= new SsrcInfoManager();
94 peerConnectionUpdateTable
= new PeerConnectionUpdateTable();
95 statsTable
= new StatsTable(ssrcInfoManager
);
97 chrome
.send('finishedDOMLoad');
99 // Requests stats from all peer connections every second.
100 window
.setInterval(function() {
101 if (Object
.keys(peerConnectionDataStore
).length
> 0)
102 chrome
.send('getAllStats');
105 document
.addEventListener('DOMContentLoaded', initialize
);
109 * A helper function for getting a peer connection element id.
111 * @param {!Object.<string, number>} data The object containing the pid and lid
112 * of the peer connection.
113 * @return {string} The peer connection element id.
115 function getPeerConnectionId(data
) {
116 return data
.pid
+ '-' + data
.lid
;
121 * Extracts ssrc info from a setLocal/setRemoteDescription update.
123 * @param {!PeerConnectionUpdateEntry} data The peer connection update data.
125 function extractSsrcInfo(data
) {
126 if (data
.type
== 'setLocalDescription' ||
127 data
.type
== 'setRemoteDescription') {
128 ssrcInfoManager
.addSsrcStreamInfo(data
.value
);
134 * Helper for adding a peer connection update.
136 * @param {Element} peerConnectionElement
137 * @param {!PeerConnectionUpdateEntry} update The peer connection update data.
139 function addPeerConnectionUpdate(peerConnectionElement
, update
) {
140 peerConnectionUpdateTable
.addPeerConnectionUpdate(peerConnectionElement
,
142 extractSsrcInfo(update
);
143 peerConnectionDataStore
[peerConnectionElement
.id
].addUpdate(
144 update
.type
, update
.value
);
148 /** Browser message handlers. */
152 * Removes all information about a peer connection.
154 * @param {!Object.<string, number>} data The object containing the pid and lid
155 * of a peer connection.
157 function removePeerConnection(data
) {
158 var element
= $(getPeerConnectionId(data
));
160 delete peerConnectionDataStore
[element
.id
];
161 tabView
.removeTab(element
.id
);
167 * Adds a peer connection.
169 * @param {!Object} data The object containing the pid, lid, url, servers, and
170 * constraints of a peer connection.
172 function addPeerConnection(data
) {
173 var id
= getPeerConnectionId(data
);
175 if (!peerConnectionDataStore
[id
]) {
176 peerConnectionDataStore
[id
] = new PeerConnectionRecord();
178 peerConnectionDataStore
[id
].initialize(
179 data
.url
, data
.servers
, data
.constraints
);
181 var peerConnectionElement
= $(id
);
182 if (!peerConnectionElement
) {
183 peerConnectionElement
= tabView
.addTab(id
, data
.url
);
185 peerConnectionElement
.innerHTML
=
186 '<p>' + data
.url
+ ' ' + data
.servers
+ ' ' + data
.constraints
+
189 return peerConnectionElement
;
194 * Adds a peer connection update.
196 * @param {!PeerConnectionUpdateEntry} data The peer connection update data.
198 function updatePeerConnection(data
) {
199 var peerConnectionElement
= $(getPeerConnectionId(data
));
200 addPeerConnectionUpdate(peerConnectionElement
, data
);
205 * Adds the information of all peer connections created so far.
207 * @param {Array.<!Object>} data An array of the information of all peer
208 * connections. Each array item contains pid, lid, url, servers,
209 * constraints, and an array of updates as the log.
211 function updateAllPeerConnections(data
) {
212 for (var i
= 0; i
< data
.length
; ++i
) {
213 var peerConnection
= addPeerConnection(data
[i
]);
215 var log
= data
[i
].log
;
218 for (var j
= 0; j
< log
.length
; ++j
) {
219 addPeerConnectionUpdate(peerConnection
, log
[j
]);
226 * Handles the report of stats.
228 * @param {!Object} data The object containing pid, lid, and reports, where
229 * reports is an array of stats reports. Each report contains id, type,
230 * and stats, where stats is the object containing timestamp and values,
231 * which is an array of strings, whose even index entry is the name of the
232 * stat, and the odd index entry is the value.
234 function addStats(data
) {
235 var peerConnectionElement
= $(getPeerConnectionId(data
));
236 if (!peerConnectionElement
)
239 for (var i
= 0; i
< data
.reports
.length
; ++i
) {
240 var report
= data
.reports
[i
];
241 statsTable
.addStatsReport(peerConnectionElement
, report
);
242 drawSingleReport(peerConnectionElement
, report
);
248 * Adds a getUserMedia request.
250 * @param {!Object} data The object containing rid {number}, pid {number},
251 * origin {string}, audio {Object<string>}, video {Object<string>}.
253 function addGetUserMedia(data
) {
254 // TODO(jiayl): add the getUserMedia info to the tabbed UI.
259 * Removes the getUserMedia requests from the specified |rid|.
261 * @param {!Object} data The object containing rid {number}, the render id.
263 function removeGetUserMediaForRenderer(data
) {
264 // TODO(jiayl): remove the getUserMedia info from the tabbed UI.
270 function enableAecRecording() {
271 dumpCreator
.enableAecRecording();