@unboundcx/video-sdk-client 2.0.8 → 2.0.9
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/managers/StatsCollector.js +47 -19
- package/package.json +1 -1
|
@@ -11,6 +11,11 @@ export class StatsCollector {
|
|
|
11
11
|
this.tracks = new Map(); // trackId -> { participantId, kind }
|
|
12
12
|
this.onStatsCallback = null; // Host-app callback (UI display)
|
|
13
13
|
this._internalCallbacks = []; // SDK-internal subscribers (QualityMonitor)
|
|
14
|
+
// Previous cumulative byte counts + timestamp per transport, so we can
|
|
15
|
+
// turn the monotonic bytesSent/bytesReceived counters into an actual
|
|
16
|
+
// throughput RATE (kbps) instead of a meaningless running total. Keyed by
|
|
17
|
+
// transportType ('send' | 'recv'). See calculateBandwidth().
|
|
18
|
+
this._prevBytes = { send: null, recv: null };
|
|
14
19
|
}
|
|
15
20
|
|
|
16
21
|
/**
|
|
@@ -214,37 +219,60 @@ export class StatsCollector {
|
|
|
214
219
|
}
|
|
215
220
|
|
|
216
221
|
/**
|
|
217
|
-
* Calculate
|
|
222
|
+
* Calculate actual throughput from stats.
|
|
223
|
+
*
|
|
224
|
+
* bytesSent / bytesReceived are MONOTONIC cumulative counters (total since
|
|
225
|
+
* the connection started), so summing them yields a meaningless ever-growing
|
|
226
|
+
* number. To get a real RATE we diff the current total against the previous
|
|
227
|
+
* sample and divide by the elapsed time. Returns both kbps (for display) and
|
|
228
|
+
* Mbps (back-compat with existing consumers). The first sample per transport
|
|
229
|
+
* has no baseline, so its rate is 0 until the next cycle establishes a delta.
|
|
230
|
+
*
|
|
231
|
+
* `upload`/`download` (Mbps) are kept for back-compat; `uploadKbps`/
|
|
232
|
+
* `downloadKbps` are the values the quality panel shows.
|
|
218
233
|
*/
|
|
219
234
|
calculateBandwidth(stats, transportType) {
|
|
220
235
|
const bandwidth = {
|
|
221
|
-
upload: 0,
|
|
222
|
-
download: 0,
|
|
236
|
+
upload: 0, // Mbps (back-compat)
|
|
237
|
+
download: 0, // Mbps (back-compat)
|
|
238
|
+
uploadKbps: 0,
|
|
239
|
+
downloadKbps: 0,
|
|
223
240
|
unit: 'Mbps',
|
|
224
241
|
};
|
|
225
242
|
|
|
243
|
+
// Sum cumulative bytes across all RTP streams for this transport, and take
|
|
244
|
+
// the freshest report timestamp as "now" (RTCStats timestamps are in ms).
|
|
245
|
+
let totalBytes = 0;
|
|
246
|
+
let ts = 0;
|
|
247
|
+
const wantType = transportType === 'send' ? 'outbound-rtp' : 'inbound-rtp';
|
|
248
|
+
const byteField = transportType === 'send' ? 'bytesSent' : 'bytesReceived';
|
|
226
249
|
for (const report of stats.values()) {
|
|
250
|
+
if (report.type !== wantType) continue;
|
|
251
|
+
totalBytes += Number(report[byteField]) || 0;
|
|
252
|
+
if (report.timestamp && report.timestamp > ts) ts = report.timestamp;
|
|
253
|
+
}
|
|
254
|
+
if (!ts) ts = Date.now();
|
|
255
|
+
|
|
256
|
+
const prev = this._prevBytes[transportType];
|
|
257
|
+
this._prevBytes[transportType] = { bytes: totalBytes, ts };
|
|
258
|
+
|
|
259
|
+
// Need a prior sample to compute a rate. Guard against counter resets
|
|
260
|
+
// (transport recreated → bytes drop) and non-positive elapsed time.
|
|
261
|
+
if (prev && totalBytes >= prev.bytes && ts > prev.ts) {
|
|
262
|
+
const deltaBits = (totalBytes - prev.bytes) * 8;
|
|
263
|
+
const deltaSec = (ts - prev.ts) / 1000;
|
|
264
|
+
const bps = deltaBits / deltaSec;
|
|
265
|
+
const kbps = parseFloat((bps / 1000).toFixed(1));
|
|
266
|
+
const mbps = parseFloat((bps / 1_000_000).toFixed(2));
|
|
227
267
|
if (transportType === 'send') {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
if (report.bytesSent) {
|
|
231
|
-
bandwidth.upload += report.bytesSent * 8; // Convert to bits
|
|
232
|
-
}
|
|
233
|
-
}
|
|
268
|
+
bandwidth.uploadKbps = kbps;
|
|
269
|
+
bandwidth.upload = mbps;
|
|
234
270
|
} else {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if (report.bytesReceived) {
|
|
238
|
-
bandwidth.download += report.bytesReceived * 8; // Convert to bits
|
|
239
|
-
}
|
|
240
|
-
}
|
|
271
|
+
bandwidth.downloadKbps = kbps;
|
|
272
|
+
bandwidth.download = mbps;
|
|
241
273
|
}
|
|
242
274
|
}
|
|
243
275
|
|
|
244
|
-
// Convert to Mbps
|
|
245
|
-
bandwidth.upload = parseFloat((bandwidth.upload / 1000000).toFixed(2));
|
|
246
|
-
bandwidth.download = parseFloat((bandwidth.download / 1000000).toFixed(2));
|
|
247
|
-
|
|
248
276
|
return bandwidth;
|
|
249
277
|
}
|
|
250
278
|
|