@rtcstats/rtcstats-shared 2.1.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,24 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2025 rtcstats.com
4
+
5
+ Permission is hereby granted, free of charge,
6
+ to any person obtaining a copy of this software and
7
+ associated documentation files (the "Software"), to
8
+ deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify,
10
+ merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom
12
+ the Software is furnished to do so,
13
+ subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice
16
+ shall be included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
22
+ ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -7,7 +7,7 @@ import SDPUtils from 'sdp';
7
7
  // Based on libWebRTCs IPAddress::ToSensitiveString
8
8
  // https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/rtc_base/ip_address.cc;l=149;drc=0dc30698370bcde67dda4f48b7ca19bf4c1dbc17
9
9
  export function obfuscateIpOrAddress(ipOrAddress) {
10
- if (['::1', '127.0.0.1'].includes(ipOrAddress)) {
10
+ if (['::1', '127.0.0.1', '0.0.0.0'].includes(ipOrAddress)) {
11
11
  return ipOrAddress;
12
12
  }
13
13
  if (ipOrAddress.indexOf(':') !== -1) { // IPv6
package/compression.js CHANGED
@@ -318,6 +318,7 @@ const methodTable = {
318
318
  // These are very common so ordered specifically such that
319
319
  // they get ѕingle-digit ids.
320
320
  getStats: 'g',
321
+ // TODO: getStats: 1, at next major version bump.
321
322
  createOffer: 2,
322
323
  createOfferOnSuccess: 3,
323
324
  createOfferOnFailure: 12,
@@ -381,10 +382,15 @@ const methodTable = {
381
382
  // HTMLMediaElement
382
383
  'HTMLMediaElement.resize': 80,
383
384
  };
385
+ // Properties that are decompressed but for release purpose not yet compressed.
386
+ // These should be moved to the compression table at major version bumps.
387
+ const methodTableDecompression = {
388
+ 1: 'getStats',
389
+ };
384
390
  const reverseMethodTable = Object.keys(methodTable).reduce((table, method) => {
385
391
  table[methodTable[method]] = method;
386
392
  return table;
387
- }, {});
393
+ }, methodTableDecompression);
388
394
 
389
395
  /**
390
396
  * Replace a rtcstats method with a numeric identifier.
@@ -707,10 +713,26 @@ const statsPropertyTable = {
707
713
  base64Certificate: 165,
708
714
  issuerCertificateId: 166,
709
715
  };
716
+ // Properties that are decompressed but for release purpose not yet compressed.
717
+ // These should be moved to the compression table at major version bumps.
718
+ const statsPropertyTableDecompression = {
719
+ // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-psnrsum
720
+ 167: 'psnrMeasurements',
721
+ 168: 'psnrSum',
722
+ // L4S stats, https://github.com/w3c/webrtc-stats/pull/792/
723
+ 169: 'packetsReceivedWithEct1',
724
+ 170: 'packetsReceivedWithCe',
725
+ 171: 'packetsReportedAsLost',
726
+ 172: 'packetsReportedAsLostButRecovered',
727
+ 173: 'packetsWithBleachedEct1Marking',
728
+ 174: 'packetsSentWithEct1',
729
+ 175: 'ccfbMessagesSent',
730
+ 176: 'ccfbMessagesReceived',
731
+ };
710
732
  const reverseStatsPropertyTable = Object.keys(statsPropertyTable).reduce((table, property) => {
711
733
  table[statsPropertyTable[property]] = property;
712
734
  return table;
713
- }, {});
735
+ }, statsPropertyTableDecompression);
714
736
 
715
737
  /**
716
738
  * Replace a stats property name with a numeric key for known properties.
package/dump.js CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  descriptionDecompression,
4
4
  statsDecompression,
5
5
  } from './compression.js';
6
+ import {createInternalsTimeSeries} from './timeseries.js';
6
7
  import {parseTrackWithStreams} from './utils.js';
7
8
 
8
9
  export async function detectRTCStatsDump(blob) {
@@ -122,6 +123,106 @@ export async function readWebRTCInternalsDump(blob) {
122
123
  return JSON.parse(textBlob);
123
124
  }
124
125
 
126
+ // Convert a webrtc-internals dump into the same shape readRTCStatsDump returns
127
+ // so the existing feature extractors (mostly) work.
128
+ export function internalsToRtcstats(data) {
129
+ const peerConnections = {};
130
+ const startTimestamp = data.timestamp || 0;
131
+
132
+ const clientTrace = [{
133
+ type: 'create',
134
+ value: {
135
+ cpuPerformance: data.cpuPerformance,
136
+ deviceMemory: data.deviceMemory,
137
+ hardwareConcurrency: data.hardwareConcurrency,
138
+ userAgent: data.UserAgent,
139
+ userAgentData: data.UserAgentData,
140
+ },
141
+ timestamp: startTimestamp,
142
+ extra: [],
143
+ }];
144
+
145
+ for (const gum of (data.getUserMedia || [])) {
146
+ const baseType = 'navigator.mediaDevices.' + gum.request_type;
147
+ if (gum.error !== undefined) {
148
+ clientTrace.push({type: baseType + 'OnFailure', value: gum.error, timestamp: gum.timestamp, extra: []});
149
+ } else if (gum.audio_track_info !== undefined || gum.video_track_info !== undefined) {
150
+ const tracks = [];
151
+ if (gum.audio_track_info) {
152
+ const t = JSON.parse(gum.audio_track_info);
153
+ tracks.push(['audio', t.id, t.label, gum.stream_id]);
154
+ }
155
+ if (gum.video_track_info) {
156
+ const t = JSON.parse(gum.video_track_info);
157
+ tracks.push(['video', t.id, t.label, gum.stream_id]);
158
+ }
159
+ clientTrace.push({type: baseType + 'OnSuccess', value: tracks, timestamp: gum.timestamp, extra: []});
160
+ } else if (gum.audio !== undefined || gum.video !== undefined) {
161
+ const value = {};
162
+ if (gum.audio !== undefined) value.audio = gum.audio === '' ? true : gum.audio;
163
+ if (gum.video !== undefined) value.video = gum.video === '' ? true : gum.video;
164
+ clientTrace.push({type: baseType, value, timestamp: gum.timestamp, extra: []});
165
+ }
166
+ }
167
+ clientTrace.sort((a, b) => a.timestamp - b.timestamp);
168
+ peerConnections['null'] = clientTrace;
169
+
170
+ for (const id of Object.keys(data.PeerConnections || {})) {
171
+ const pc = data.PeerConnections[id];
172
+ const trace = [{
173
+ type: 'create',
174
+ value: pc.rtcConfiguration,
175
+ timestamp: pc.updateLog?.[0]?.timestamp || startTimestamp,
176
+ extra: [],
177
+ }];
178
+ for (const ev of (pc.updateLog || [])) {
179
+ let value = ev.value;
180
+ if (typeof value === 'string' && value.length > 0) {
181
+ try { value = JSON.parse(value); } catch (_) { /* keep as string */ }
182
+ }
183
+ trace.push({
184
+ type: ev.type,
185
+ value,
186
+ timestamp: ev.timestamp || (ev.time ? new Date(ev.time).getTime() : 0),
187
+ extra: [],
188
+ });
189
+ }
190
+ // Convert the per-property stats timeseries back into per-tick getStats events.
191
+ const series = createInternalsTimeSeries(pc);
192
+ if (series) {
193
+ const byTimestamp = new Map();
194
+ for (const statsId of Object.keys(series)) {
195
+ const statsType = series[statsId].type;
196
+ for (const property of Object.keys(series[statsId])) {
197
+ if (['type', 'timestamp', 'id'].includes(property)) continue;
198
+ for (const [ts, value] of series[statsId][property]) {
199
+ if (!byTimestamp.has(ts)) byTimestamp.set(ts, {});
200
+ const report = byTimestamp.get(ts);
201
+ if (!report[statsId]) {
202
+ report[statsId] = {id: statsId, timestamp: ts, type: statsType};
203
+ }
204
+ report[statsId][property] = value;
205
+ }
206
+ }
207
+ }
208
+ for (const ts of byTimestamp.keys()) {
209
+ trace.push({type: 'getStats', value: byTimestamp.get(ts), timestamp: ts, extra: []});
210
+ }
211
+ }
212
+ trace.sort((a, b) => a.timestamp - b.timestamp);
213
+ peerConnections[id] = trace;
214
+ }
215
+ return {peerConnections, eventSizes: {}};
216
+ }
217
+
218
+ // Auto-detect the dump format and dispatch.
219
+ export async function readDump(blob) {
220
+ if (await detectRTCStatsDump(blob)) {
221
+ return readRTCStatsDump(blob);
222
+ }
223
+ return internalsToRtcstats(await readWebRTCInternalsDump(blob));
224
+ }
225
+
125
226
  export async function extractTracks(peerConnectionTrace) {
126
227
  const tracks = [];
127
228
  for (const traceEvent of peerConnectionTrace) {
@@ -183,6 +284,30 @@ export async function extractTracks(peerConnectionTrace) {
183
284
  }
184
285
  associatedTrack.statsId = id;
185
286
  });
287
+ } else if (traceEvent.type === 'transceiverAdded') {
288
+ // chrome://webrtc-internals non-spec event.
289
+ const {value} = traceEvent;
290
+ if (['addTrack', 'addTransceiver'].includes(traceEvent.value?.reason)) {
291
+ const trackInformation = {
292
+ startTime: traceEvent.timestamp,
293
+ direction: 'outbound',
294
+ kind: value.kind,
295
+ id: value.sender.track,
296
+ label: value.sender.track,
297
+ streams: value.sender.streams,
298
+ };
299
+ tracks.push(trackInformation);
300
+ } else if (traceEvent.value?.reason === 'setRemoteDescription') {
301
+ const trackInformation = {
302
+ startTime: traceEvent.timestamp,
303
+ direction: 'inbound',
304
+ kind: value.kind,
305
+ id: value.receiver.track,
306
+ label: value.receiver.track,
307
+ streams: value.receiver.streams,
308
+ };
309
+ tracks.push(trackInformation);
310
+ }
186
311
  }
187
312
  }
188
313
  return tracks;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rtcstats/rtcstats-shared",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "main": "index.js",
5
5
  "description": "gather WebRTC API traces and statistics",
6
6
  "type": "module",
package/timeseries.js CHANGED
@@ -33,8 +33,15 @@ export function createInternalsTimeSeries(connection) {
33
33
  return;
34
34
  }
35
35
  const timestamps = JSON.parse(connection.stats[statsId + '-timestamp'].values);
36
- series[statsId][statsProperty] = JSON.parse(stats.values).map((currentValue, index) => {
37
- return [timestamps[index], currentValue];
36
+ // Note: this does not work if a property only shows up on a stat "late".
37
+ // Observed e.g. with targetBitrate when the encoding is initially disabled.
38
+ // Then stats exist (and create a timestamp) but targetBitrate does not.
39
+ // Taking this from the end does not always work either, e.g. for remote rtcp-based
40
+ // reports.
41
+ const values = JSON.parse(stats.values);
42
+ const offset = timestamps.length - values.length;
43
+ series[statsId][statsProperty] = values.map((currentValue, index) => {
44
+ return [timestamps[index + offset], currentValue];
38
45
  });
39
46
  }
40
47
  return series;