-
Notifications
You must be signed in to change notification settings - Fork 44
/
getstats-mangle.js
397 lines (385 loc) · 16.4 KB
/
getstats-mangle.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// using shiftwidth=2 since reformating all the adapter code...
module.exports = function(stats) {
// Mangle chrome stats to spec stats. Just chrome stats.
let needsMangling = false;
Object.keys(stats).forEach(id => {
if (!stats[id].id) {
stats[id].id = id; // backfill, might have been removed by client.
}
if (stats[id].type === 'googComponent') {
needsMangling = true;
}
});
if (!needsMangling) {
return stats;
}
// taken from https://github.com/fippo/adapter/tree/getstats-mangling
const standardReport = {};
Object.keys(stats).forEach(id => {
if (id === 'timestamp') {
return;
}
const standardStats = stats[id];
// Step 1: translate to standard types and attribute names.
switch (standardStats.type) {
case 'ssrc':
standardStats.trackIdentifier = standardStats.googTrackId;
// FIXME: not defined in spec, probably whether the track is
// remote?
standardStats.remoteSource =
standardStats.id.indexOf('recv') !== -1;
standardStats.ssrc = parseInt(standardStats.ssrc, 10);
// FIXME: 'only makes sense' <=> not set?
if (standardStats.googFrameWidthReceived ||
standardStats.googFrameWidthSent) {
standardStats.frameWidth = parseInt(
standardStats.googFrameWidthReceived ||
standardStats.googFrameWidthSent, 10);
}
if (standardStats.googFrameHeightReceived ||
standardStats.googFrameHeightSent) {
standardStats.frameHeight = parseInt(
standardStats.googFrameHeightReceived ||
standardStats.googFrameHeightSent, 10);
}
if (standardStats.googFrameRateInput ||
standardStats.googFrameRateReceived) {
// FIXME: might be something else not available currently
standardStats.framesPerSecond = parseInt(
standardStats.googFrameRateInput ||
standardStats.googFrameRateReceived, 10);
}
/* FIXME unfortunately the current stats (googFrameRateSent,
* googFrameRateReceived, googFrameRateDecoded) so we can not
* calculate the cumulative amount.
* FIXME (spec) Firefox has frameRateMean why is this
* not part of the spec?
*/
if (standardStats.googFrameRateSent) {
standardStats.framesSent = 0;
}
if (standardStats.googFrameRateReceived) {
standardStats.framesReceived = 0;
}
// FIXME: both on sender and receiver?
if (standardStats.kind === 'video' || standardStats.mediaType === 'video') {
standardStats.framesDropped = 0;
}
if (standardStats.audioInputLevel ||
standardStats.audioOutputLevel) {
standardStats.audioLevel = parseInt(
standardStats.audioInputLevel ||
standardStats.audioOutputLevel, 10) / 32767.0;
}
if (standardStats.googJitterReceived) {
standardStats.jitter = 1.0 * parseInt(
standardStats.googJitterReceived, 10);
}
// FIXME: fractionLost
if (standardStats.googFirsReceived || standardStats.googFirsSent) {
standardStats.firCount = parseInt(
standardStats.googFirsReceived ||
standardStats.googFirsSent, 10);
}
if (standardStats.googPlisReceived || standardStats.googPlisSent) {
standardStats.pliCount = parseInt(
standardStats.googPlisReceived ||
standardStats.googPlisSent, 10);
}
if (standardStats.googNacksReceived ||
standardStats.googNacksSent) {
standardStats.nackCount = parseInt(
standardStats.googNacksReceived ||
standardStats.googNacksSent, 10);
}
// FIXME: no SLI stats yet?
if (standardStats.bytesSent) {
standardStats.bytesSent = parseInt(standardStats.bytesSent, 10);
}
if (standardStats.bytesReceived) {
standardStats.bytesReceived = parseInt(
standardStats.bytesReceived, 10);
}
if (standardStats.packetsSent) {
standardStats.packetsSent = parseInt(
standardStats.packetsSent, 10);
}
if (standardStats.packetsReceived) {
standardStats.packetsReceived = parseInt(
standardStats.packetsReceived, 10);
}
if (standardStats.packetsLost) {
standardStats.packetsLost = parseInt(
standardStats.packetsLost, 10);
}
if (standardStats.googEchoCancellationReturnLoss) {
standardStats.echoReturnLoss = 1.0 * parseInt(
standardStats.googEchoCancellationReturnLoss, 10);
standardStats.echoReturnLossEnhancement = 1.0 * parseInt(
standardStats.googEchoCancellationReturnLossEnhancement, 10);
}
if (standardStats.googRtt) {
// This is the RTCP RTT.
standardStats.roundTripTime = parseInt(standardStats.googRtt, 10);
}
break;
case 'localcandidate':
case 'remotecandidate':
// https://w3c.github.io/webrtc-stats/#icecandidate-dict*
standardStats.portNumber = parseInt(standardStats.portNumber, 10);
standardStats.priority = parseInt(standardStats.priority, 10);
// FIXME: addressSourceUrl?
// FIXME: https://github.com/w3c/webrtc-stats/issues/12
break;
case 'googCandidatePair':
// https://w3c.github.io/webrtc-stats/#candidatepair-dict*
standardStats.transportId = standardStats.googChannelId;
// FIXME: maybe set depending on iceconnectionstate and read/write?
//standardStats.state = 'FIXME'; // enum
// FIXME: could be calculated from candidate priorities and role.
//standardStats.priority = 'FIXME'; // unsigned long long
standardStats.writable = standardStats.googWritable === 'true';
standardStats.readable = standardStats.googReadable === 'true';
// assumption: nominated is readable and writeable.
standardStats.nominated = standardStats.readable &&
standardStats.writable;
// FIXME: missing from spec
standardStats.selected =
standardStats.googActiveConnection === 'true';
standardStats.bytesSent = parseInt(standardStats.bytesSent, 10);
standardStats.bytesReceived = parseInt(
standardStats.bytesReceived, 10);
// FIXME: packetsSent is not in spec?
// FIXME: no packetsReceived?
standardStats.packetsSent = parseInt(
standardStats.packetsSent, 10);
standardStats.packetsDiscardedOnSend = parseInt(
standardStats.packetsDiscardedOnSend, 10);
// This is the STUN RTT.
standardStats.roundTripTime = parseInt(standardStats.googRtt, 10);
// backfilled later from videoBWE.
standardStats.availableOutgoingBitrate = 0.0;
standardStats.availableIncomingBitrate = 0.0;
break;
case 'googComponent':
// additional RTCTransportStats created later since we
// want the normalized fields and complete snowball.
break;
case 'googCertificate':
standardStats.type = 'certificate'; // FIXME spec: undefined in spec.
standardStats.fingerprint = standardStats.googFingerprint;
standardStats.fingerprintAlgorithm =
standardStats.googFingerprintAlgorithm;
standardStats.base64Certificate = standardStats.googDerBase64;
standardStats.issuerCertificateId = null; // FIXME spec: undefined what 'no issuer' is.
break;
case 'VideoBwe':
standardStats.availableOutgoingBitrate = 1.0 *
parseInt(standardStats.googAvailableSendBandwidth, 10);
standardStats.availableIncomingBitrate = 1.0 *
parseInt(standardStats.googAvailableReceiveBandwidth, 10);
break;
case 'rtcstats-device-report':
standardStats.batteryLevel = parseInt(standardStats.batteryLevel, 10);
break;
default:
break;
}
standardReport[standardStats.id] = standardStats;
});
// Step 2: fix things spanning multiple reports.
Object.keys(standardReport).forEach(id => {
const report = standardReport[id];
let other, newId;
switch (report.type) {
case 'googCandidatePair':
report.type = 'candidate-pair';
if (standardReport.bweforvideo) {
report.availableOutgoingBitrate =
standardReport.bweforvideo.availableOutgoingBitrate;
report.availableIncomingBitrate =
standardReport.bweforvideo.availableIncomingBitrate;
standardReport[report.id] = report;
}
break;
case 'googComponent':
// create a new report since we don't carry over all fields.
other = standardReport[report.selectedCandidatePairId];
newId = 'transport_' + report.id;
standardReport[newId] = {
type: 'transport',
timestamp: report.timestamp,
id: newId,
bytesSent: other && other.bytesSent || 0,
bytesReceived: other && other.bytesReceived || 0,
// FIXME (spec): rtpcpTransportStatsId: rtcp-mux is required so...
activeConnection: other && other.selected,
selectedCandidatePairId: report.selectedCandidatePairId,
localCertificateId: report.localCertificateId,
remoteCertificateId: report.remoteCertificateId
};
break;
case 'ssrc':
newId = 'rtpstream_' + report.id;
// Workaround for https://code.google.com/p/webrtc/issues/detail?id=4808 (fixed in M46)
/* it is not apparently. This can be set to the empty string.
if (!report.googCodecName) {
report.googCodecName = 'VP8';
}
*/
standardReport[newId] = {
//type: 'notastandalonething',
timestamp: report.timestamp,
id: newId,
ssrc: report.ssrc,
mediaType: report.kind || report.mediaType,
kind: report.kind || report.mediaType,
associateStatsId: 'rtcpstream_' + report.id,
isRemote: false,
mediaTrackId: 'mediatrack_' + report.id,
transportId: report.transportId,
};
if (report.googCodecName && report.googCodecName.length) {
standardReport.codecId = 'codec_' + report.googCodecName
}
if (report.kind === 'video' || report.mediaType === 'video') {
standardReport[newId].firCount = report.firCount;
standardReport[newId].pliCount = report.pliCount;
standardReport[newId].nackCount = report.nackCount;
standardReport[newId].sliCount = report.sliCount; // undefined yet
}
if (report.remoteSource) {
standardReport[newId].type = 'inbound-rtp';
standardReport[newId].packetsReceived = report.packetsReceived;
standardReport[newId].bytesReceived = report.bytesReceived;
standardReport[newId].packetsLost = report.packetsLost;
} else {
standardReport[newId].type = 'outbound-rtp';
standardReport[newId].packetsSent = report.packetsSent;
standardReport[newId].bytesSent = report.bytesSent;
standardReport[newId].roundTripTime = report.roundTripTime;
// TODO: targetBitrate
}
// FIXME: this is slightly more complicated. inboundrtp can have packetlost
// but so can outbound-rtp via rtcp (isRemote = true)
// need to unmux with opposite type and put loss into remote report.
newId = 'rtcpstream_' + report.id;
standardReport[newId] = {
//type: 'notastandalonething',
timestamp: report.timestamp,
id: newId,
ssrc: report.ssrc,
associateStatsId: 'rtpstream_' + report.id,
isRemote: true,
mediaTrackId: 'mediatrack_' + report.id,
transportId: report.transportId,
codecId: 'codec_' + report.googCodecName
};
if (report.remoteSource) {
standardReport[newId].type = 'outbound-rtp';
standardReport[newId].packetsSent = report.packetsSent;
standardReport[newId].bytesSent = report.bytesSent;
standardReport[newId].roundTripTime = report.roundTripTime;
standardReport[newId].packetsLost = report.packetsLost;
} else {
standardReport[newId].type = 'inbound-rtp';
standardReport[newId].packetsReceived = report.packetsReceived;
standardReport[newId].bytesReceived = report.bytesReceived;
standardReport[newId].packetsLost = report.packetsLost;
}
// FIXME: one of these is not set?
if (report.jitter) {
standardReport[newId].jitter = report.jitter;
}
newId = 'mediatrack_' + report.id;
standardReport[newId] = {
type: 'track',
timestamp: report.timestamp,
id: newId,
trackIdentifier: report.trackIdentifier,
remoteSource: report.remoteSource,
ssrcIds: ['rtpstream_' + report.id, 'rtcpstream_' + report.id]
};
if (report.kind === 'audio' || report.mediaType === 'audio') {
standardReport[newId].audioLevel = report.audioLevel;
if (report.id.indexOf('send') !== -1) {
standardReport[newId].echoReturnLoss = report.echoReturnLoss;
standardReport[newId].echoReturnLossEnhancement =
report.echoReturnLossEnhancement;
}
} else if (report.kind === 'video' || report.mediaType === 'video') {
standardReport[newId].frameWidth = report.frameWidth;
standardReport[newId].frameHeight = report.frameHeight;
standardReport[newId].framesPerSecond = report.framesPerSecond;
if (report.remoteSource) {
standardReport[newId].framesReceived = report.framesReceived;
standardReport[newId].framesDecoded = report.framesDecoded;
standardReport[newId].framesDropped = report.framesDropped;
standardReport[newId].framesCorrupted = report.framesCorrupted;
} else {
standardReport[newId].framesSent = report.framesSent;
}
}
// We have one codec item per codec name.
// This might be wrong (in theory) since with unified plan
// we can have multiple m-lines and codecs and different
// payload types/parameters but unified is not supported yet.
/*
if (!standardReport['codec_' + report.googCodecName]) {
// determine payload type (from offer) and negotiated (?spec)
// parameters (from answer). (parameters not negotiated yet)
if (pc.localDescription &&
pc.localDescription.type === 'offer') {
sdp = pc.localDescription.sdp;
} else if (pc.remoteDescription &&
pc.remoteDescription.type === 'offer') {
sdp = pc.remoteDescription.sdp;
}
if (sdp) {
// TODO: use a SDP library instead of this regexp-stringsoup approach.
var match = sdp.match(new RegExp('a=rtpmap:(\\d+) ' +
report.googCodecName + '\\/(\\d+)(?:\\/(\\d+))?'));
if (match) {
newId = 'codec_' + report.id;
standardReport[newId] = {
type: 'codec', // FIXME (spec)
timestamp: report.timestamp,
id: newId,
codec: report.googCodecName,
payloadType: parseInt(match[1], 10),
clockRate: parseInt(match[2], 10),
channels: parseInt(match[3] || '1', 10),
parameters: ''
};
}
}
}
*/
break;
default:
break;
}
});
// Step 3: fiddle the transport in between transport and rtp stream
Object.keys(standardReport).forEach(id => {
const report = standardReport[id];
if (report.type === 'transport') {
// RTCTransport has a pointer to the selectedCandidatePair...
let other = standardReport[report.selectedCandidatePairId];
if (other) {
other.transportId = report.id;
}
// but no pointers to the rtpstreams running over it?!
// instead, we rely on having added 'transport_'
Object.keys(standardReport).forEach(otherid => {
other = standardReport[otherid];
if ((other.type === 'inbound-rtp' ||
other.type === 'outbound-rtp') &&
report.id === 'transport_' + other.transportId) {
other.transportId = report.id;
}
});
}
});
return standardReport;
}