1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This file contains helper methods to draw the stats timeline graphs.
// Each graph represents a series of stats report for a PeerConnection,
// e.g. 1234-0-ssrc-abcd123-bytesSent is the graph for the series of bytesSent
// for ssrc-abcd123 of PeerConnection 0 in process 1234.
// The graphs are drawn as CANVAS, grouped per report type per PeerConnection.
// Each group has an expand/collapse button and is collapsed initially.
//
<include src="data_series.js"/>
<include src="timeline_graph_view.js"/>
var STATS_GRAPH_CONTAINER_HEADING_CLASS = 'stats-graph-container-heading';
// Specifies which stats should be drawn on the 'bweCompound' graph and how.
var bweCompoundGraphConfig = {
googAvailableSendBandwidth: {color: 'red'},
googTargetEncBitrateCorrected: {color: 'purple'},
googActualEncBitrate: {color: 'orange'},
googRetransmitBitrate: {color: 'blue'},
googTransmitBitrate: {color: 'green'},
};
// Converts the last entry of |srcDataSeries| from the total amount to the
// amount per second.
var totalToPerSecond = function(srcDataSeries) {
var length = srcDataSeries.dataPoints_.length;
if (length >= 2) {
var lastDataPoint = srcDataSeries.dataPoints_[length - 1];
var secondLastDataPoint = srcDataSeries.dataPoints_[length - 2];
return (lastDataPoint.value - secondLastDataPoint.value) * 1000 /
(lastDataPoint.time - secondLastDataPoint.time);
}
return 0;
};
// Converts the value of total bytes to bits per second.
var totalBytesToBitsPerSecond = function(srcDataSeries) {
return totalToPerSecond(srcDataSeries) * 8;
};
// Specifies which stats should be converted before drawn and how.
// |convertedName| is the name of the converted value, |convertFunction|
// is the function used to calculate the new converted value based on the
// original dataSeries.
var dataConversionConfig = {
packetsSent: {
convertedName: 'packetsSentPerSecond',
convertFunction: totalToPerSecond,
},
bytesSent: {
convertedName: 'bitsSentPerSecond',
convertFunction: totalBytesToBitsPerSecond,
},
packetsReceived: {
convertedName: 'packetsReceivedPerSecond',
convertFunction: totalToPerSecond,
},
bytesReceived: {
convertedName: 'bitsReceivedPerSecond',
convertFunction: totalBytesToBitsPerSecond,
},
// This is due to a bug of wrong units reported for googTargetEncBitrate.
// TODO (jiayl): remove this when the unit bug is fixed.
googTargetEncBitrate: {
convertedName: 'googTargetEncBitrateCorrected',
convertFunction: function (srcDataSeries) {
var length = srcDataSeries.dataPoints_.length;
var lastDataPoint = srcDataSeries.dataPoints_[length - 1];
if (lastDataPoint.value < 5000)
return lastDataPoint.value * 1000;
return lastDataPoint.value;
}
}
};
var graphViews = {};
var dataSeries = {};
// Adds the stats report |singleReport| to the timeline graph for the given
// |peerConnectionElement| and |reportName|.
function drawSingleReport(
peerConnectionElement, reportType, reportId, singleReport) {
if (!singleReport || !singleReport.values)
return;
var reportName = reportType + '-' + reportId;
for (var i = 0; i < singleReport.values.length - 1; i = i + 2) {
var rawLabel = singleReport.values[i];
var rawValue = parseInt(singleReport.values[i + 1]);
if (isNaN(rawValue))
return;
var rawDataSeriesId =
peerConnectionElement.id + '-' + reportName + '-' + rawLabel;
var finalDataSeriesId = rawDataSeriesId;
var finalLabel = rawLabel;
var finalValue = rawValue;
// We need to convert the value if dataConversionConfig[rawLabel] exists.
if (dataConversionConfig[rawLabel]) {
// Updates the original dataSeries before the conversion.
addDataSeriesPoint(rawDataSeriesId, singleReport.timestamp,
rawLabel, rawValue);
// Convert to another value to draw on graph, using the original
// dataSeries as input.
finalValue = dataConversionConfig[rawLabel].convertFunction(
dataSeries[rawDataSeriesId]);
finalLabel = dataConversionConfig[rawLabel].convertedName;
finalDataSeriesId =
peerConnectionElement.id + '-' + reportName + '-' + finalLabel;
}
// Updates the final dataSeries to draw.
addDataSeriesPoint(
finalDataSeriesId, singleReport.timestamp, finalLabel, finalValue);
// Updates the graph.
var graphType = bweCompoundGraphConfig[finalLabel] ?
'bweCompound' : finalLabel;
var graphViewId =
peerConnectionElement.id + '-' + reportName + '-' + graphType;
if (!graphViews[graphViewId]) {
graphViews[graphViewId] = createStatsGraphView(peerConnectionElement,
reportType, reportId,
graphType);
var date = new Date(singleReport.timestamp);
graphViews[graphViewId].setDateRange(date, date);
}
// Adds the new dataSeries to the graphView. We have to do it here to cover
// both the simple and compound graph cases.
if (!graphViews[graphViewId].hasDataSeries(dataSeries[finalDataSeriesId]))
graphViews[graphViewId].addDataSeries(dataSeries[finalDataSeriesId]);
graphViews[graphViewId].updateEndDate();
}
}
// Makes sure the TimelineDataSeries with id |dataSeriesId| is created,
// and adds the new data point to it.
function addDataSeriesPoint(dataSeriesId, time, label, value) {
if (!dataSeries[dataSeriesId]) {
dataSeries[dataSeriesId] = new TimelineDataSeries();
if (bweCompoundGraphConfig[label]) {
dataSeries[dataSeriesId].setColor(
bweCompoundGraphConfig[label].color);
}
}
dataSeries[dataSeriesId].addPoint(time, value);
}
// Ensures a div container to hold all stats graphs for one track is created as
// a child of |peerConnectionElement|.
function ensureStatsGraphTopContainer(
peerConnectionElement, reportType, reportId) {
var containerId = peerConnectionElement.id + '-' +
reportType + '-' + reportId + '-graph-container';
var container = $(containerId);
if (!container) {
container = document.createElement('details');
container.id = containerId;
container.className = 'stats-graph-container';
peerConnectionElement.appendChild(container);
container.innerHTML ='<summary><span></span></summary>';
container.firstChild.firstChild.className =
STATS_GRAPH_CONTAINER_HEADING_CLASS;
container.firstChild.firstChild.textContent =
'Stats graphs for ' + reportType + '-' + reportId;
if (reportType == 'ssrc') {
var ssrcInfoElement = document.createElement('div');
container.firstChild.appendChild(ssrcInfoElement);
ssrcInfoManager.populateSsrcInfo(ssrcInfoElement, reportId);
}
}
return container;
}
// Creates the container elements holding a timeline graph
// and the TimelineGraphView object.
function createStatsGraphView(
peerConnectionElement, reportType, reportId, statsName) {
var topContainer = ensureStatsGraphTopContainer(peerConnectionElement,
reportType, reportId);
var graphViewId = peerConnectionElement.id + '-' +
reportType + '-' + reportId + '-' + statsName;
var divId = graphViewId + '-div';
var canvasId = graphViewId + '-canvas';
var container = document.createElement("div");
container.className = 'stats-graph-sub-container';
topContainer.appendChild(container);
container.innerHTML = '<div>' + statsName + '</div>' +
'<div id=' + divId + '><canvas id=' + canvasId + '></canvas></div>';
if (statsName == 'bweCompound') {
container.insertBefore(
createBweCompoundLegend(
peerConnectionElement, reportType + '-' + reportId),
$(divId));
}
return new TimelineGraphView(divId, canvasId);
}
// Creates the legend section for the bweCompound graph.
// Returns the legend element.
function createBweCompoundLegend(peerConnectionElement, reportName) {
var legend = document.createElement('div');
for (var prop in bweCompoundGraphConfig) {
var div = document.createElement('div');
legend.appendChild(div);
div.innerHTML = '<input type=checkbox checked></input>' + prop;
div.style.color = bweCompoundGraphConfig[prop].color;
div.dataSeriesId = peerConnectionElement.id + '-' + reportName + '-' + prop;
div.graphViewId =
peerConnectionElement.id + '-' + reportName + '-bweCompound';
div.firstChild.addEventListener('click', function(event) {
var target = dataSeries[event.target.parentNode.dataSeriesId];
target.show(event.target.checked);
graphViews[event.target.parentNode.graphViewId].repaint();
});
}
return legend;
}
|