-
Notifications
You must be signed in to change notification settings - Fork 19
/
draw-graphs.js
220 lines (192 loc) · 8.99 KB
/
draw-graphs.js
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
// helper for graphs.
const bitrateSeries = {};
const bitrateGraphs = {};
const framerateSeries = {};
const framerateGraphs = {};
const qpSeries = {};
const qpGraphs = {};
let lastSendResult;
let lastRecvResult;
// Only for Firefox.
let ssrc2track;
// Show a stream and draw graphs.
function show(stream, isRemote) {
const id = isRemote ? stream.id : 'local';
const container = document.createElement('div');
container.id = id + 'Container';
container.style.display = 'inline-flex';
document.getElementById(isRemote ? 'remotes' : 'local').appendChild(container);
document.getElementById(isRemote ? 'remotes' : 'local').appendChild(document.createElement('br'));
const v = document.createElement('video');
v.autoplay = true;
v.srcObject = stream;
v.onresize = () => v.title = 'video dimensions: ' + v.videoWidth + 'x' + v.videoHeight;
container.appendChild(v);
const bitrateCanvas = document.createElement('canvas');
bitrateCanvas.id = id + 'BitrateCanvas';
bitrateCanvas.title = 'Bitrate';
container.appendChild(bitrateCanvas);
const bitrateGraph = new TimelineGraphView(id + 'Container', id + 'BitrateCanvas');
bitrateGraph.updateEndDate();
bitrateSeries[id] = id === 'local' ? new Map() : new TimelineDataSeries();
bitrateGraphs[id] = bitrateGraph;
const framerateCanvas = document.createElement('canvas');
framerateCanvas.id = id + 'FramerateCanvas';
framerateCanvas.title = 'Framerate';
container.appendChild(framerateCanvas);
const framerateGraph = new TimelineGraphView(id + 'Container', id + 'FramerateCanvas');
framerateGraph.updateEndDate();
framerateSeries[id] = id === 'local' ? new Map() : new TimelineDataSeries();
framerateGraphs[id] = framerateGraph;
const qpCanvas = document.createElement('canvas');
qpCanvas.id = id + 'qpCanvas';
qpCanvas.title = 'qp';
container.appendChild(qpCanvas);
const qpGraph = new TimelineGraphView(id + 'Container', id + 'qpCanvas');
qpGraph.updateEndDate();
qpSeries[id] = id === 'local' ? new Map() : new TimelineDataSeries();
qpGraphs[id] = qpGraph;
const current = document.createElement('span');
current.id = id + '_currentData';
current.style['padding-left'] = '10px';
current.innerText = 'Stream: ' + id;
container.appendChild(current);
}
async function draw(pc1, pc2) {
const res1 = await pc1.getSenders()[0].getStats();
const sentKeyframes = {};
const encoders = {};
res1.forEach((report) => {
if (report.type !== 'outbound-rtp') return;
const graphName = 'local';
const now = report.timestamp;
const bytes = report.bytesSent;
const frames = report.framesEncoded;
// For non-spec simulcast there is no rid.
sentKeyframes[report.rid || report.ssrc] = report.keyFramesEncoded;
encoders[report.rid] = report.encoderImplementation;
if (lastSendResult && lastSendResult.get(report.id)) {
const ssrc = report.ssrc;
// Calculate bitrate
const bitrate = 8000 * (bytes - lastSendResult.get(report.id).bytesSent) /
(now - lastSendResult.get(report.id).timestamp);
if (!bitrateSeries[graphName].has(ssrc)) {
const series = new TimelineDataSeries();
bitrateSeries[graphName].set(ssrc, series);
if (bitrateSeries[graphName].size === 2) {
series.setColor('green');
} else if (bitrateSeries[graphName].size === 3) {
series.setColor('blue');
}
}
if (bitrate >= 0) {
bitrateSeries[graphName].get(ssrc).addPoint(now, bitrate);
}
// Calculate framerate.
const framerate = 1000 * (frames - lastSendResult.get(report.id).framesEncoded) /
(now - lastSendResult.get(report.id).timestamp);
if (!framerateSeries[graphName].has(ssrc)) {
const series = new TimelineDataSeries();
framerateSeries[graphName].set(ssrc, series);
if (framerateSeries[graphName].size === 2) {
series.setColor('green');
} else if (framerateSeries[graphName].size === 3) {
series.setColor('blue');
}
}
if (framerate >= 0) {
framerateSeries[graphName].get(ssrc).addPoint(now, framerate);
}
// Calculate qp-per-frame.
if (report.qpSum) {
const qp = (report.qpSum - lastSendResult.get(report.id).qpSum) /
(report.framesEncoded - lastSendResult.get(report.id).framesEncoded);
if (!qpSeries[graphName].has(ssrc)) {
const series = new TimelineDataSeries();
qpSeries[graphName].set(ssrc, series);
if (qpSeries[graphName].size === 2) {
series.setColor('green');
} else if (qpSeries[graphName].size === 3) {
series.setColor('blue');
}
}
qpSeries[graphName].get(ssrc).addPoint(now, qp);
}
}
});
if (document.getElementById('local_currentData')) {
document.getElementById('local_currentData').innerText = 'Stream: local\n' + Object.keys(sentKeyframes).sort().map(rid => {
return 'rid=' + rid + ': ' +
'keyframes=' + sentKeyframes[rid] +
(encoders[rid] ? ', encoder=' + encoders[rid] : '');
}).join('\n');
}
bitrateGraphs['local'].setDataSeries(Array.from(bitrateSeries['local'].values()));
bitrateGraphs['local'].updateEndDate();
framerateGraphs['local'].setDataSeries(Array.from(framerateSeries['local'].values()));
framerateGraphs['local'].updateEndDate();
qpGraphs['local'].setDataSeries(Array.from(qpSeries['local'].values()));
qpGraphs['local'].updateEndDate();
lastSendResult = res1;
const res2 = await pc2.getStats();
res2.forEach((report) => {
if (report.type !== 'inbound-rtp') return;
const now = report.timestamp;
const bytes = report.bytesReceived;
const frames = report.framesDecoded;
if (lastRecvResult && lastRecvResult.get(report.id)) {
let graphName;
if (adapter.browserDetails.browser === 'firefox') {
const graphId = ssrc2track.indexOf(report.ssrc >>> 0);
graphName = ['low', 'mid', 'hi'][graphId];
} else {
// Prefer inbound-rtp.trackIdentifier, fall back to deprecated track stats trackIdentifier.
graphName = report.trackIdentifier || res2.get(report.trackId).trackIdentifier;
}
if (!bitrateSeries[graphName]) {
return;
}
// Calculate bitrate.
const bitrate = 8000 * (bytes - lastRecvResult.get(report.id).bytesReceived) /
(now - lastRecvResult.get(report.id).timestamp);
bitrateSeries[graphName].addPoint(now, bitrate);
bitrateGraphs[graphName].setDataSeries([bitrateSeries[graphName]]);
bitrateGraphs[graphName].updateEndDate();
// Calculate framerate.
const framerate = 1000 * (frames - lastRecvResult.get(report.id).framesDecoded) /
(now - lastRecvResult.get(report.id).timestamp);
framerateSeries[graphName].addPoint(now, framerate);
framerateGraphs[graphName].setDataSeries([framerateSeries[graphName]]);
framerateGraphs[graphName].updateEndDate();
// Calculate qp-per-frame.
if (report.qpSum) {
const qp = (report.qpSum - lastRecvResult.get(report.id).qpSum) /
(report.framesDecoded - lastRecvResult.get(report.id).framesDecoded);
qpSeries[graphName].addPoint(now, qp);
qpGraphs[graphName].setDataSeries([qpSeries[graphName]]);
qpGraphs[graphName].updateEndDate();
}
const currentData = document.getElementById(graphName + '_currentData');
if (currentData) {
currentData.innerText =
'Stream: ' + graphName +
'\nmid=' + report.mid +
'\nkeyframes=' + report.keyFramesDecoded;
if (report.codecId) {
const codec = res2.get(report.codecId);
currentData.innerText += '\ncodec=' + codec.mimeType;
if (codec.sdpFmtpLine) {
currentData.innerText += ' ' + codec.sdpFmtpLine;
}
}
if (report.decoderImplementation) {
currentData.innerText += '\ndecoder=' + report.decoderImplementation;
}
if (report.powerEfficientDecoder !== undefined) {
currentData.innerText += '\npowerEfficient=' + report.powerEfficientDecoder;
}
}
}
});
lastRecvResult = res2;
}