Skip to content

Commit 1009d2c

Browse files
committed
feat: tweak quality score and session management
1 parent 9bc20d1 commit 1009d2c

File tree

4 files changed

+171
-308
lines changed

4 files changed

+171
-308
lines changed

peer/src/peer.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import {
1414
} from "@protobuf-ts/grpcweb-transport";
1515
import { asleep, retry } from "./util.ts";
1616
import { jwtDecode } from "jwt-decode";
17-
import { calculateWebRTCQuality, collectWebRTCStats } from "./quality.ts";
17+
import { calculateQualityScore } from "./quality.ts";
1818

1919
export type { PeerInfo } from "./signaling.ts";
2020

21-
const ANALYTICS_POLL_INTERVAL_MS = 60_000;
21+
const ANALYTICS_POLL_INTERVAL_MS = 10_000;
2222

2323
/**
2424
* Streamline real-time application development.`@pulsebeam/peer` abstracts
@@ -247,11 +247,11 @@ export class Peer {
247247
* Callback invoked when a new session is established.
248248
* @param _s Session object
249249
*/
250-
public onsession = (_s: ISession) => { };
250+
public onsession = (_s: ISession) => {};
251251
/**
252252
* Callback invoked when the peer’s state changes.
253253
*/
254-
public onstatechange = () => { };
254+
public onstatechange = () => {};
255255
/**
256256
* Identifier for the peer. Valid UTF-8 string of 1-16 characters.
257257
*/
@@ -299,6 +299,11 @@ export class Peer {
299299
}
300300

301301
get sessions(): Session[] {
302+
// lazily remove closed sessions. This is to maintain session list without listening to
303+
// each session's event.
304+
this._sessions = this._sessions.filter((s) =>
305+
s.connectionState != "closed"
306+
);
302307
return [...this._sessions];
303308
}
304309

@@ -326,26 +331,20 @@ export class Peer {
326331
const events: AnalyticsEvent[] = [];
327332
for (const sess of this.sessions) {
328333
const rawStats = await sess.getStats();
334+
329335
const at = Date.now() * 1_000;
330-
const stats = collectWebRTCStats(rawStats);
331-
332-
if (
333-
stats.audio.length != 0 ||
334-
stats.video.length != 0 && stats.data.length != 0
335-
) {
336-
const quality = calculateWebRTCQuality(stats);
337-
338-
events.push({
339-
timestampUs: BigInt(at),
340-
tags: {
341-
src: this.transport.info,
342-
dst: sess.other,
343-
},
344-
metrics: {
345-
qualityScore: BigInt(quality),
346-
},
347-
});
348-
}
336+
const quality = calculateQualityScore(rawStats);
337+
338+
events.push({
339+
timestampUs: BigInt(at),
340+
tags: {
341+
src: this.transport.info,
342+
dst: sess.other,
343+
},
344+
metrics: {
345+
qualityScore: BigInt(quality),
346+
},
347+
});
349348
}
350349

351350
const request: AnalyticsReportReq = { events };

peer/src/quality.test.ts

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)