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 peerConnectionsListElem = null;
7 var ssrcInfoManager = null;
8 var peerConnectionUpdateTable = null;
10 var dumpCreator = null;
11 /** A map from peer connection id to the PeerConnectionRecord. */
12 var peerConnectionDataStore = {};
14 /** A simple class to store the updates and stats data for a peer connection. */
15 var PeerConnectionRecord = (function() {
17 function PeerConnectionRecord() {
28 PeerConnectionRecord.prototype = {
35 * Adds the initilization info of the peer connection.
36 * @param {string} url The URL of the web page owning the peer connection.
37 * @param {Array} servers STUN servers used by the peer connection.
38 * @param {!Object} constraints Media constraints.
40 initialize: function(url, servers, constraints) {
41 this.record_.url = url;
42 this.record_.servers = servers;
43 this.record_.constraints = constraints;
47 * @param {string} dataSeriesId The TimelineDataSeries identifier.
48 * @return {!TimelineDataSeries}
50 getDataSeries: function(dataSeriesId) {
51 return this.record_.stats[dataSeriesId];
55 * @param {string} dataSeriesId The TimelineDataSeries identifier.
56 * @param {!TimelineDataSeries} dataSeries The TimelineDataSeries to set to.
58 setDataSeries: function(dataSeriesId, dataSeries) {
59 this.record_.stats[dataSeriesId] = dataSeries;
63 * @param {string} type The type of the update.
64 * @param {string} value The value of the update.
66 addUpdate: function(type, value) {
67 this.record_.updateLog.push({
68 time: (new Date()).toLocaleString(),
75 return PeerConnectionRecord;
78 // The maximum number of data points bufferred for each stats. Old data points
79 // will be shifted out when the buffer is full.
80 var MAX_STATS_DATA_POINT_BUFFER_SIZE = 1000;
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 peerConnectionsListElem = $('peer-connections-list');
92 dumpCreator = new DumpCreator(peerConnectionsListElem);
93 ssrcInfoManager = new SsrcInfoManager();
94 peerConnectionUpdateTable = new PeerConnectionUpdateTable();
95 statsTable = new StatsTable(ssrcInfoManager);
97 chrome.send('getAllUpdates');
99 // Requests stats from all peer connections every second.
100 window.setInterval(function() {
101 if (peerConnectionsListElem.getElementsByTagName('li').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 peerConnectionsListElem.removeChild(element);
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 = document.createElement('li');
184 peerConnectionsListElem.appendChild(peerConnectionElement);
185 peerConnectionElement.id = id;
187 peerConnectionElement.innerHTML =
188 '<h3>PeerConnection ' + peerConnectionElement.id + '</h3>' +
189 '<div>' + data.url + ' ' + data.servers + ' ' + data.constraints +
192 // Clicking the heading can expand or collapse the peer connection item.
193 peerConnectionElement.firstChild.title = 'Click to collapse or expand';
194 peerConnectionElement.firstChild.addEventListener('click', function(e) {
195 if (e.target.parentElement.className == '')
196 e.target.parentElement.className = 'peer-connection-hidden';
198 e.target.parentElement.className = '';
201 return peerConnectionElement;
206 * Adds a peer connection update.
208 * @param {!PeerConnectionUpdateEntry} data The peer connection update data.
210 function updatePeerConnection(data) {
211 var peerConnectionElement = $(getPeerConnectionId(data));
212 addPeerConnectionUpdate(peerConnectionElement, data);
217 * Adds the information of all peer connections created so far.
219 * @param {Array.<!Object>} data An array of the information of all peer
220 * connections. Each array item contains pid, lid, url, servers,
221 * constraints, and an array of updates as the log.
223 function updateAllPeerConnections(data) {
224 for (var i = 0; i < data.length; ++i) {
225 var peerConnection = addPeerConnection(data[i]);
227 var log = data[i].log;
230 for (var j = 0; j < log.length; ++j) {
231 addPeerConnectionUpdate(peerConnection, log[j]);
238 * Handles the report of stats.
240 * @param {!Object} data The object containing pid, lid, and reports, where
241 * reports is an array of stats reports. Each report contains id, type,
242 * and stats, where stats is the object containing timestamp and values,
243 * which is an array of strings, whose even index entry is the name of the
244 * stat, and the odd index entry is the value.
246 function addStats(data) {
247 var peerConnectionElement = $(getPeerConnectionId(data));
248 if (!peerConnectionElement)
251 for (var i = 0; i < data.reports.length; ++i) {
252 var report = data.reports[i];
253 statsTable.addStatsReport(peerConnectionElement, report);
254 drawSingleReport(peerConnectionElement, report);
260 * Delegates to dumpCreator to update the recording status.
261 * @param {!Object.<string>} update Key-value pairs describing the status of the
264 function updateDumpStatus(update) {
265 dumpCreator.onUpdate(update);