@observertc/observer-js 0.41.6 → 0.42.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.
Files changed (61) hide show
  1. package/lib/ObservedCall.d.ts +10 -3
  2. package/lib/ObservedCall.d.ts.map +1 -1
  3. package/lib/ObservedCall.js +28 -0
  4. package/lib/ObservedClient.d.ts +4 -0
  5. package/lib/ObservedClient.d.ts.map +1 -1
  6. package/lib/ObservedClient.js +81 -32
  7. package/lib/ObservedInboundAudioTrack.d.ts +94 -0
  8. package/lib/ObservedInboundAudioTrack.d.ts.map +1 -0
  9. package/lib/ObservedInboundAudioTrack.js +215 -0
  10. package/lib/ObservedInboundVideoTrack.d.ts +98 -0
  11. package/lib/ObservedInboundVideoTrack.d.ts.map +1 -0
  12. package/lib/{ObservedInboundTrack.js → ObservedInboundVideoTrack.js} +53 -44
  13. package/lib/ObservedOutboundAudioTrack.d.ts +85 -0
  14. package/lib/ObservedOutboundAudioTrack.d.ts.map +1 -0
  15. package/lib/{ObservedOutboundTrack.js → ObservedOutboundAudioTrack.js} +35 -58
  16. package/lib/ObservedOutboundVideoTrack.d.ts +88 -0
  17. package/lib/ObservedOutboundVideoTrack.d.ts.map +1 -0
  18. package/lib/ObservedOutboundVideoTrack.js +204 -0
  19. package/lib/ObservedPeerConnection.d.ts +26 -16
  20. package/lib/ObservedPeerConnection.d.ts.map +1 -1
  21. package/lib/ObservedPeerConnection.js +157 -86
  22. package/lib/RemoteTrackAssigner.d.ts +14 -0
  23. package/lib/RemoteTrackAssigner.d.ts.map +1 -0
  24. package/lib/RemoteTrackAssigner.js +52 -0
  25. package/lib/common/CalculatedScore.d.ts +79 -0
  26. package/lib/common/CalculatedScore.d.ts.map +1 -0
  27. package/lib/common/CalculatedScore.js +155 -0
  28. package/lib/common/CallEventType.d.ts +13 -1
  29. package/lib/common/CallEventType.d.ts.map +1 -1
  30. package/lib/common/CallEventType.js +12 -0
  31. package/lib/common/QualityScoreUtils.d.ts +1 -0
  32. package/lib/common/QualityScoreUtils.d.ts.map +1 -0
  33. package/lib/common/QualityScoreUtils.js +1 -0
  34. package/lib/common/utils.d.ts +1 -0
  35. package/lib/common/utils.d.ts.map +1 -1
  36. package/lib/common/utils.js +14 -1
  37. package/lib/index.d.ts +4 -2
  38. package/lib/index.d.ts.map +1 -1
  39. package/lib/monitors/ClientQualityMonitor.d.ts +1 -0
  40. package/lib/monitors/ClientQualityMonitor.d.ts.map +1 -0
  41. package/lib/monitors/ClientQualityMonitor.js +1 -0
  42. package/package.json +1 -1
  43. package/src/ObservedCall.ts +40 -4
  44. package/src/ObservedClient.ts +77 -17
  45. package/src/ObservedInboundAudioTrack.ts +336 -0
  46. package/src/{ObservedInboundTrack.ts → ObservedInboundVideoTrack.ts} +85 -66
  47. package/src/{ObservedOutboundTrack.ts → ObservedOutboundAudioTrack.ts} +59 -74
  48. package/src/ObservedOutboundVideoTrack.ts +326 -0
  49. package/src/ObservedPeerConnection.ts +205 -131
  50. package/src/RemoteTrackAssigner.ts +68 -0
  51. package/src/common/CalculatedScore.ts +180 -0
  52. package/src/common/CallEventType.ts +14 -1
  53. package/src/common/QualityScoreUtils.ts +0 -0
  54. package/src/common/utils.ts +17 -1
  55. package/src/index.ts +4 -2
  56. package/src/monitors/CallSummaryMonitor.ts +3 -3
  57. package/src/monitors/ClientQualityMonitor.ts +0 -0
  58. package/lib/ObservedInboundTrack.d.ts +0 -96
  59. package/lib/ObservedInboundTrack.d.ts.map +0 -1
  60. package/lib/ObservedOutboundTrack.d.ts +0 -84
  61. package/lib/ObservedOutboundTrack.d.ts.map +0 -1
@@ -1,27 +1,26 @@
1
1
  import { EventEmitter } from 'events';
2
2
  import { ObservedPeerConnection } from './ObservedPeerConnection';
3
3
  import { OutboundAudioTrack, OutboundVideoTrack } from '@observertc/sample-schemas-js';
4
- import { ObservedInboundTrack } from './ObservedInboundTrack';
5
4
  import { OutboundAudioTrackReport, OutboundVideoTrackReport } from '@observertc/report-schemas-js';
6
- import { MediaKind } from './common/types';
5
+ import { ObservedInboundAudioTrack } from './ObservedInboundAudioTrack';
6
+ import { calculateBaseAudioScore, CalculatedScore } from './common/CalculatedScore';
7
+ import { ClientIssue } from './monitors/CallSummary';
7
8
 
8
- export type ObservedOutboundTrackModel<K extends MediaKind> = {
9
+ export type ObservedOutboundAudioTrackModel = {
9
10
  trackId: string;
10
- kind: K;
11
11
  sfuStreamId?: string;
12
12
  }
13
13
 
14
- export type ObservedOutboundTrackEvents = {
14
+ export type ObservedOutboundAudioTrackEvents = {
15
15
  qualitylimitationchanged: [string];
16
16
  update: [{
17
17
  elapsedTimeInMs: number;
18
18
  }],
19
+ score: [CalculatedScore],
19
20
  close: [],
20
21
  };
21
22
 
22
- export type ObservedOutboundTrackStatsUpdate<K extends MediaKind> = K extends 'audio' ? OutboundAudioTrack : OutboundVideoTrack;
23
-
24
- export type ObservedOutboundTrackStats<K extends MediaKind> = ObservedOutboundTrackStatsUpdate<K> & {
23
+ export type ObservedOutboundAudioTrackStats = OutboundAudioTrack & {
25
24
  ssrc: number;
26
25
  bitrate: number;
27
26
  rttInMs?: number;
@@ -35,24 +34,25 @@ export type ObservedOutboundTrackStats<K extends MediaKind> = ObservedOutboundTr
35
34
  statsTimestamp: number;
36
35
  };
37
36
 
38
- // export type ObservedOutboundTrackStatsUpdate<K extends MediaKind> = {
37
+ // export type ObservedOutboundTrackStatsUpdate = {
39
38
  // [Property in keyof ObservedOutboundTrackStats<K>]: ObservedOutboundTrackStats<K>[Property];
40
39
  // }
41
40
 
42
- export declare interface ObservedOutboundTrack<Kind extends MediaKind> {
43
- on<U extends keyof ObservedOutboundTrackEvents>(event: U, listener: (...args: ObservedOutboundTrackEvents[U]) => void): this;
44
- off<U extends keyof ObservedOutboundTrackEvents>(event: U, listener: (...args: ObservedOutboundTrackEvents[U]) => void): this;
45
- once<U extends keyof ObservedOutboundTrackEvents>(event: U, listener: (...args: ObservedOutboundTrackEvents[U]) => void): this;
46
- emit<U extends keyof ObservedOutboundTrackEvents>(event: U, ...args: ObservedOutboundTrackEvents[U]): boolean;
47
- update(sample: ObservedOutboundTrackStatsUpdate<Kind>, timestamp: number): void;
41
+ export declare interface ObservedOutboundAudioTrack {
42
+ on<U extends keyof ObservedOutboundAudioTrackEvents>(event: U, listener: (...args: ObservedOutboundAudioTrackEvents[U]) => void): this;
43
+ off<U extends keyof ObservedOutboundAudioTrackEvents>(event: U, listener: (...args: ObservedOutboundAudioTrackEvents[U]) => void): this;
44
+ once<U extends keyof ObservedOutboundAudioTrackEvents>(event: U, listener: (...args: ObservedOutboundAudioTrackEvents[U]) => void): this;
45
+ emit<U extends keyof ObservedOutboundAudioTrackEvents>(event: U, ...args: ObservedOutboundAudioTrackEvents[U]): boolean;
46
+ update(sample: OutboundAudioTrack, timestamp: number): void;
48
47
  }
49
48
 
50
- export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter {
49
+ export class ObservedOutboundAudioTrack extends EventEmitter {
51
50
  public readonly created = Date.now();
52
51
  public visited = false;
53
52
 
54
53
  public bitrate = 0;
55
54
  public rttInMs?: number;
55
+ public jitter?: number;
56
56
  public marker?: string;
57
57
 
58
58
  public totalLostPackets = 0;
@@ -68,21 +68,24 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
68
68
 
69
69
  public sendingBitrate = 0;
70
70
 
71
- private readonly _stats = new Map<number, ObservedOutboundTrackStats<Kind>>();
71
+ private readonly _stats = new Map<number, ObservedOutboundAudioTrackStats>();
72
72
  private _lastMaxStatsTimestamp = 0;
73
73
 
74
74
  private _closed = false;
75
75
  private _updated = Date.now();
76
- private _remoteInboundTracks = new Map<string, ObservedInboundTrack<Kind>>();
76
+ private _lastUpdateMetrics?: number;
77
+
78
+ public score?: CalculatedScore;
79
+ public ωpendingIssuesForScores: ClientIssue[] = [];
80
+
81
+ public readonly remoteInboundTracks = new Map<string, ObservedInboundAudioTrack>();
77
82
 
78
83
  public constructor(
79
- private readonly _model: ObservedOutboundTrackModel<Kind>,
84
+ private readonly _model: ObservedOutboundAudioTrackModel,
80
85
  public readonly peerConnection: ObservedPeerConnection,
81
86
  ) {
82
87
  super();
83
88
  this.setMaxListeners(Infinity);
84
-
85
- if (this._model.sfuStreamId) this._assignSfuStreamId(this._model.sfuStreamId);
86
89
  }
87
90
 
88
91
  public get serviceId() {
@@ -109,10 +112,6 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
109
112
  return this.peerConnection.peerConnectionId;
110
113
  }
111
114
 
112
- public get kind(): Kind {
113
- return this._model.kind as Kind;
114
- }
115
-
116
115
  public get trackId() {
117
116
  return this._model.trackId;
118
117
  }
@@ -121,19 +120,19 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
121
120
  return this._model.sfuStreamId;
122
121
  }
123
122
 
124
- public get remoteInboundTracks(): ReadonlyMap<string, ObservedInboundTrack<Kind>> {
125
- return this._remoteInboundTracks;
126
- }
127
-
128
123
  public get reports() {
129
124
  return this.peerConnection.reports;
130
125
  }
131
126
 
127
+ public get statsTimestamp() {
128
+ return this._lastMaxStatsTimestamp;
129
+ }
130
+
132
131
  public get updated() {
133
132
  return this._updated;
134
133
  }
135
134
 
136
- public get stats(): ReadonlyMap<number, ObservedOutboundTrackStats<Kind>> {
135
+ public get stats(): ReadonlyMap<number, ObservedOutboundAudioTrackStats> {
137
136
  return this._stats;
138
137
  }
139
138
 
@@ -148,7 +147,7 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
148
147
  this.emit('close');
149
148
  }
150
149
 
151
- public update(sample: ObservedOutboundTrackStatsUpdate<Kind>, statsTimestamp: number): void {
150
+ public update(sample: OutboundAudioTrack, statsTimestamp: number): void {
152
151
  if (this._closed) return;
153
152
 
154
153
  const now = Date.now();
@@ -166,8 +165,7 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
166
165
  marker: this.marker,
167
166
  };
168
167
 
169
- if (this.kind === 'audio') this.reports.addOutboundAudioTrackReport(report);
170
- else if (this.kind === 'video') this.reports.addOutboundVideoTrackReport(report);
168
+ this.reports.addOutboundAudioTrackReport(report);
171
169
 
172
170
  const elapsedTimeInMs = Math.max(1, now - this._updated);
173
171
  const lastStat = this._stats.get(sample.ssrc);
@@ -193,26 +191,21 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
193
191
  let deltaEncodedFrames: number | undefined;
194
192
  let deltaSentFrames: number | undefined;
195
193
 
196
- if (this.kind === 'video') {
197
- const videoSample = sample as OutboundVideoTrack;
198
- const lastVideoStats = lastStat as OutboundVideoTrack | undefined;
194
+ const videoSample = sample as OutboundVideoTrack;
195
+ const lastVideoStats = lastStat as OutboundVideoTrack | undefined;
199
196
 
200
- if (videoSample?.framesEncoded && lastVideoStats?.framesEncoded && lastVideoStats.framesEncoded < videoSample.framesEncoded) {
201
- deltaEncodedFrames = videoSample.framesEncoded - lastVideoStats.framesEncoded;
202
- }
203
- if (videoSample?.framesSent && lastVideoStats?.framesSent && lastVideoStats.framesSent < videoSample.framesSent) {
204
- deltaSentFrames = videoSample.framesSent - lastVideoStats.framesSent;
205
- }
206
-
207
- if (videoSample.qualityLimitationReason && lastVideoStats?.qualityLimitationReason !== videoSample.qualityLimitationReason) {
208
- this.emit('qualitylimitationchanged', videoSample.qualityLimitationReason);
209
- }
210
- } else if (this.kind === 'audio') {
211
- // const audioSample = sample as OutboundAudioTrack;
212
- // const lastAudioStats = lastStat as OutboundAudioTrack | undefined;
197
+ if (videoSample?.framesEncoded && lastVideoStats?.framesEncoded && lastVideoStats.framesEncoded < videoSample.framesEncoded) {
198
+ deltaEncodedFrames = videoSample.framesEncoded - lastVideoStats.framesEncoded;
199
+ }
200
+ if (videoSample?.framesSent && lastVideoStats?.framesSent && lastVideoStats.framesSent < videoSample.framesSent) {
201
+ deltaSentFrames = videoSample.framesSent - lastVideoStats.framesSent;
202
+ }
213
203
 
204
+ if (videoSample.qualityLimitationReason && lastVideoStats?.qualityLimitationReason !== videoSample.qualityLimitationReason) {
205
+ this.emit('qualitylimitationchanged', videoSample.qualityLimitationReason);
214
206
  }
215
- const stats: ObservedOutboundTrackStats<Kind> = {
207
+
208
+ const stats: ObservedOutboundAudioTrackStats = {
216
209
  ...sample,
217
210
  rttInMs,
218
211
  bitrate,
@@ -244,9 +237,6 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
244
237
  // this.deltaSentBytes = [ ...this._stats.values() ].reduce((acc, stat) => acc + stat.deltaSentBytes, 0);
245
238
 
246
239
  // setting up sfu connection as it is not always available at the first sample
247
- if (sample.sfuStreamId && !this._model.sfuStreamId) {
248
- this._assignSfuStreamId(sample.sfuStreamId);
249
- }
250
240
 
251
241
  this.visited = true;
252
242
  this._updated = now;
@@ -255,21 +245,10 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
255
245
  });
256
246
  }
257
247
 
258
- public connectInboundTrack(track: ObservedInboundTrack<Kind>) {
259
- if (this._closed) return;
260
-
261
- track.remoteOutboundTrack = this;
262
- track.once('close', () => {
263
- this._remoteInboundTracks.delete(track.trackId);
264
- });
265
-
266
- this._remoteInboundTracks.set(track.trackId, track);
267
- }
268
- private _lastUpdateMetrics?: number;
269
-
270
248
  public updateMetrics() {
271
249
  let maxStatsTimestamp = 0;
272
250
  let rttInMsSum = 0;
251
+ let jitterSum = 0;
273
252
  let size = 0;
274
253
 
275
254
  this.bitrate = 0;
@@ -295,6 +274,7 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
295
274
  maxStatsTimestamp = Math.max(maxStatsTimestamp, stats.statsTimestamp);
296
275
 
297
276
  rttInMsSum += stats.rttInMs ?? 0;
277
+ jitterSum += stats.jitter ?? 0;
298
278
  ++size;
299
279
  }
300
280
 
@@ -311,20 +291,25 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
311
291
  this.totalSentFrames += this.deltaSentFrames;
312
292
 
313
293
  this.rttInMs = rttInMsSum / Math.max(size, 1);
294
+ this.jitter = jitterSum / Math.max(size, 1);
314
295
 
315
296
  this._lastMaxStatsTimestamp = maxStatsTimestamp;
297
+ this._updateQualityScore(maxStatsTimestamp);
316
298
  }
317
299
 
318
- private _assignSfuStreamId(sfuStreamId: string) {
319
- this._model.sfuStreamId = sfuStreamId;
300
+ private _updateQualityScore(timestamp: number) {
301
+ const newIssues = this.ωpendingIssuesForScores;
302
+ const score = calculateBaseAudioScore(this, newIssues);
303
+
304
+ if (0 < newIssues.length) {
305
+ this.ωpendingIssuesForScores = [];
306
+ }
307
+
308
+ if (!score) return (this.score = undefined);
320
309
 
321
- this.once('close', () => {
322
- if (!this._model.sfuStreamId) return;
323
- if (this.kind === 'audio') this.peerConnection.client.call.sfuStreamIdToOutboundAudioTrack.delete(this._model.sfuStreamId);
324
- else if (this.kind === 'video') this.peerConnection.client.call.sfuStreamIdToOutboundVideoTrack.delete(this._model.sfuStreamId);
325
- });
310
+ score.timestamp = timestamp;
311
+ this.score = score;
326
312
 
327
- if (this.kind === 'audio') this.peerConnection.client.call.sfuStreamIdToOutboundAudioTrack.set(this._model.sfuStreamId, this as ObservedOutboundTrack<'audio'>);
328
- else if (this.kind === 'video') this.peerConnection.client.call.sfuStreamIdToOutboundVideoTrack.set(this._model.sfuStreamId, this as ObservedOutboundTrack<'video'>);
313
+ this.emit('score', this.score);
329
314
  }
330
315
  }
@@ -0,0 +1,326 @@
1
+ import { EventEmitter } from 'events';
2
+ import { ObservedPeerConnection } from './ObservedPeerConnection';
3
+ import { OutboundVideoTrack } from '@observertc/sample-schemas-js';
4
+ import { OutboundAudioTrackReport, OutboundVideoTrackReport } from '@observertc/report-schemas-js';
5
+ import { ObservedInboundVideoTrack } from './ObservedInboundVideoTrack';
6
+ import { calculateBaseVideoScore, CalculatedScore } from './common/CalculatedScore';
7
+ import { ClientIssue } from './monitors/CallSummary';
8
+
9
+ export type ObservedOutboundVideoTrackModel = {
10
+ trackId: string;
11
+ sfuStreamId?: string;
12
+ }
13
+
14
+ export type ObservedOutboundVideoTrackEvents = {
15
+ qualitylimitationchanged: [string];
16
+ update: [{
17
+ elapsedTimeInMs: number;
18
+ }],
19
+ score: [CalculatedScore],
20
+ close: [],
21
+ };
22
+
23
+ export type ObservedOutboundVideoTrackStats = OutboundVideoTrack & {
24
+ ssrc: number;
25
+ bitrate: number;
26
+ rttInMs?: number;
27
+
28
+ deltaLostPackets: number;
29
+ deltaSentPackets: number;
30
+ deltaSentBytes: number;
31
+ deltaSentFrames?: number;
32
+ deltaEncodedFrames?: number;
33
+
34
+ statsTimestamp: number;
35
+ };
36
+
37
+ // export type ObservedOutboundTrackStatsUpdate = {
38
+ // [Property in keyof ObservedOutboundTrackStats<K>]: ObservedOutboundTrackStats<K>[Property];
39
+ // }
40
+
41
+ export declare interface ObservedOutboundVideoTrack {
42
+ on<U extends keyof ObservedOutboundVideoTrackEvents>(event: U, listener: (...args: ObservedOutboundVideoTrackEvents[U]) => void): this;
43
+ off<U extends keyof ObservedOutboundVideoTrackEvents>(event: U, listener: (...args: ObservedOutboundVideoTrackEvents[U]) => void): this;
44
+ once<U extends keyof ObservedOutboundVideoTrackEvents>(event: U, listener: (...args: ObservedOutboundVideoTrackEvents[U]) => void): this;
45
+ emit<U extends keyof ObservedOutboundVideoTrackEvents>(event: U, ...args: ObservedOutboundVideoTrackEvents[U]): boolean;
46
+ update(sample: OutboundVideoTrack, timestamp: number): void;
47
+ }
48
+
49
+ export class ObservedOutboundVideoTrack extends EventEmitter {
50
+ public readonly created = Date.now();
51
+ public visited = false;
52
+
53
+ public contentType: 'lowmotion' | 'standard' | 'highmotion' = 'standard';
54
+ public codec: 'vp8' | 'vp9' | 'h264' = 'vp8';
55
+ public highestLayer?: ObservedOutboundVideoTrackStats;
56
+
57
+ public bitrate = 0;
58
+ public jitter?: number;
59
+ public rttInMs?: number;
60
+ public marker?: string;
61
+
62
+ public totalLostPackets = 0;
63
+ public totalSentPackets = 0;
64
+ public totalSentBytes = 0;
65
+ public totalSentFrames = 0;
66
+
67
+ public deltaLostPackets = 0;
68
+ public deltaSentPackets = 0;
69
+ public deltaSentBytes = 0;
70
+ public deltaSentFrames = 0;
71
+ public deltaEncodedFrames = 0;
72
+
73
+ public sendingBitrate = 0;
74
+
75
+ private readonly _stats = new Map<number, ObservedOutboundVideoTrackStats>();
76
+ private _lastMaxStatsTimestamp = 0;
77
+
78
+ private _closed = false;
79
+ private _updated = Date.now();
80
+ public readonly remoteInboundTracks = new Map<string, ObservedInboundVideoTrack>();
81
+
82
+ public score?: CalculatedScore;
83
+ public ωpendingIssuesForScores: ClientIssue[] = [];
84
+
85
+ public constructor(
86
+ private readonly _model: ObservedOutboundVideoTrackModel,
87
+ public readonly peerConnection: ObservedPeerConnection,
88
+ ) {
89
+ super();
90
+ this.setMaxListeners(Infinity);
91
+ }
92
+
93
+ public get serviceId() {
94
+ return this.peerConnection.serviceId;
95
+ }
96
+
97
+ public get roomId() {
98
+ return this.peerConnection.roomId;
99
+ }
100
+
101
+ public get callId() {
102
+ return this.peerConnection.callId;
103
+ }
104
+
105
+ public get clientId() {
106
+ return this.peerConnection.clientId;
107
+ }
108
+
109
+ public get mediaUnitId() {
110
+ return this.peerConnection.mediaUnitId;
111
+ }
112
+
113
+ public get peerConnectionId() {
114
+ return this.peerConnection.peerConnectionId;
115
+ }
116
+
117
+ public get trackId() {
118
+ return this._model.trackId;
119
+ }
120
+
121
+ public get sfuStreamId() {
122
+ return this._model.sfuStreamId;
123
+ }
124
+
125
+ public get reports() {
126
+ return this.peerConnection.reports;
127
+ }
128
+
129
+ public get updated() {
130
+ return this._updated;
131
+ }
132
+
133
+ public get statsTimestamp() {
134
+ return this._lastMaxStatsTimestamp;
135
+ }
136
+
137
+ public get stats(): ReadonlyMap<number, ObservedOutboundVideoTrackStats> {
138
+ return this._stats;
139
+ }
140
+
141
+ public get closed() {
142
+ return this._closed;
143
+ }
144
+
145
+ public close() {
146
+ if (this._closed) return;
147
+ this._closed = true;
148
+
149
+ this.emit('close');
150
+ }
151
+
152
+ public update(sample: OutboundVideoTrack, statsTimestamp: number): void {
153
+ if (this._closed) return;
154
+
155
+ const now = Date.now();
156
+ const report: OutboundAudioTrackReport | OutboundVideoTrackReport = {
157
+ serviceId: this.peerConnection.client.serviceId,
158
+ roomId: this.peerConnection.client.call.roomId,
159
+ callId: this.peerConnection.client.call.callId,
160
+ clientId: this.peerConnection.client.clientId,
161
+ userId: this.peerConnection.client.userId,
162
+ mediaUnitId: this.peerConnection.client.mediaUnitId,
163
+ peerConnectionId: this.peerConnection.peerConnectionId,
164
+ ...sample,
165
+ timestamp: statsTimestamp,
166
+ sampleSeq: -1,
167
+ marker: this.marker,
168
+ };
169
+
170
+ this.reports.addOutboundVideoTrackReport(report);
171
+
172
+ const elapsedTimeInMs = Math.max(1, now - this._updated);
173
+ const lastStat = this._stats.get(sample.ssrc);
174
+ const rttInMs = sample.roundTripTime ? sample.roundTripTime * 1000 : undefined;
175
+ let bitrate = 0;
176
+ let deltaLostPackets = 0;
177
+ let deltaSentPackets = 0;
178
+ let deltaSentBytes = 0;
179
+
180
+ if (sample.bytesSent && lastStat?.bytesSent && lastStat.bytesSent < sample.bytesSent) {
181
+ bitrate = (sample.bytesSent - lastStat.bytesSent) * 8 / (elapsedTimeInMs / 1000);
182
+ }
183
+ if (sample.packetsLost && lastStat?.packetsLost && lastStat.packetsLost < sample.packetsLost) {
184
+ deltaLostPackets = sample.packetsLost - lastStat.packetsLost;
185
+ }
186
+ if (sample.packetsSent && lastStat?.packetsSent && lastStat.packetsSent < sample.packetsSent) {
187
+ deltaSentPackets = sample.packetsSent - lastStat.packetsSent;
188
+ }
189
+ if (sample.bytesSent && lastStat?.bytesSent && lastStat.bytesSent < sample.bytesSent) {
190
+ deltaSentBytes = sample.bytesSent - lastStat.bytesSent;
191
+ }
192
+
193
+ let deltaEncodedFrames: number | undefined;
194
+ let deltaSentFrames: number | undefined;
195
+
196
+ const videoSample = sample as OutboundVideoTrack;
197
+ const lastVideoStats = lastStat as OutboundVideoTrack | undefined;
198
+
199
+ if (videoSample?.framesEncoded && lastVideoStats?.framesEncoded && lastVideoStats.framesEncoded < videoSample.framesEncoded) {
200
+ deltaEncodedFrames = videoSample.framesEncoded - lastVideoStats.framesEncoded;
201
+ }
202
+ if (videoSample?.framesSent && lastVideoStats?.framesSent && lastVideoStats.framesSent < videoSample.framesSent) {
203
+ deltaSentFrames = videoSample.framesSent - lastVideoStats.framesSent;
204
+ }
205
+
206
+ if (videoSample.qualityLimitationReason && lastVideoStats?.qualityLimitationReason !== videoSample.qualityLimitationReason) {
207
+ this.emit('qualitylimitationchanged', videoSample.qualityLimitationReason);
208
+ }
209
+
210
+ const stats: ObservedOutboundVideoTrackStats = {
211
+ ...sample,
212
+ rttInMs,
213
+ bitrate,
214
+ ssrc: sample.ssrc,
215
+
216
+ deltaLostPackets,
217
+ deltaSentPackets,
218
+ deltaSentBytes,
219
+ deltaEncodedFrames,
220
+ deltaSentFrames,
221
+
222
+ statsTimestamp,
223
+ };
224
+
225
+ this._stats.set(sample.ssrc, stats);
226
+
227
+ // this.bitrate = [ ...this._stats.values() ].reduce((acc, stat) => acc + stat.bitrate, 0);
228
+ // this.rttInMs = [ ...this._stats.values() ].reduce((acc, stat) => acc + (stat.rttInMs ?? 0), 0) / (this._stats.size || 1);
229
+
230
+ // this.totalLostPackets = [ ...this._stats.values() ].reduce((acc, stat) => acc + (stat.packetsLost ?? 0), 0);
231
+ // this.totalSentPackets = [ ...this._stats.values() ].reduce((acc, stat) => acc + (stat.packetsSent ?? 0), 0);
232
+ // this.totalSentBytes = [ ...this._stats.values() ].reduce((acc, stat) => acc + (stat.bytesSent ?? 0), 0);
233
+ // this.totalSentFrames = [ ...this._stats.values() ].reduce((acc, stat) => acc + (stat.deltaSentPackets ?? 0), 0);
234
+
235
+ // this.deltaEncodedFrames = [ ...this._stats.values() ].reduce((acc, stat) => acc + (stat.deltaEncodedFrames ?? 0), 0);
236
+ // this.deltaSentFrames = [ ...this._stats.values() ].reduce((acc, stat) => acc + (stat.deltaSentFrames ?? 0), 0);
237
+ // this.deltaLostPackets = [ ...this._stats.values() ].reduce((acc, stat) => acc + stat.deltaLostPackets, 0);
238
+ // this.deltaSentPackets = [ ...this._stats.values() ].reduce((acc, stat) => acc + stat.deltaSentPackets, 0);
239
+ // this.deltaSentBytes = [ ...this._stats.values() ].reduce((acc, stat) => acc + stat.deltaSentBytes, 0);
240
+
241
+ // setting up sfu connection as it is not always available at the first sample
242
+
243
+ this.visited = true;
244
+ this._updated = now;
245
+ this.emit('update', {
246
+ elapsedTimeInMs,
247
+ });
248
+ }
249
+
250
+ private _lastUpdateMetrics?: number;
251
+
252
+ public updateMetrics() {
253
+ let maxStatsTimestamp = 0;
254
+ let rttInMsSum = 0;
255
+ let jitterSum = 0;
256
+ let size = 0;
257
+
258
+ this.bitrate = 0;
259
+ this.rttInMs = undefined;
260
+
261
+ this.sendingBitrate = 0;
262
+ this.deltaLostPackets = 0;
263
+ this.deltaSentPackets = 0;
264
+ this.deltaSentBytes = 0;
265
+ this.deltaSentFrames = 0;
266
+ this.deltaEncodedFrames = 0;
267
+
268
+ const highestLayer = { ssrc: 0, bitrate: -1 };
269
+
270
+ for (const [ ssrc, stats ] of this._stats) {
271
+ if (stats.statsTimestamp <= this._lastMaxStatsTimestamp) continue;
272
+
273
+ this.deltaLostPackets += stats.deltaLostPackets ?? 0;
274
+ this.deltaSentPackets += stats.deltaSentPackets ?? 0;
275
+ this.deltaSentBytes += stats.deltaSentBytes ?? 0;
276
+ this.deltaSentFrames += stats.deltaSentFrames ?? 0;
277
+ this.deltaEncodedFrames += stats.deltaEncodedFrames ?? 0;
278
+ this.bitrate += stats.bitrate;
279
+
280
+ maxStatsTimestamp = Math.max(maxStatsTimestamp, stats.statsTimestamp);
281
+
282
+ rttInMsSum += stats.rttInMs ?? 0;
283
+ jitterSum += stats.jitter ?? 0;
284
+ ++size;
285
+
286
+ if (stats.bitrate > highestLayer.bitrate) {
287
+ highestLayer.ssrc = ssrc;
288
+ highestLayer.bitrate = stats.bitrate;
289
+ }
290
+ }
291
+
292
+ const now = Date.now();
293
+
294
+ if (this._lastUpdateMetrics) {
295
+ this.sendingBitrate = (this.deltaSentBytes * 8) / ((now - this._lastUpdateMetrics) / 1000);
296
+ }
297
+ this._lastUpdateMetrics = now;
298
+ this.highestLayer = this._stats.get(highestLayer.ssrc);
299
+ this.totalLostPackets += this.deltaLostPackets;
300
+ this.totalSentPackets += this.deltaSentPackets;
301
+ this.totalSentBytes += this.deltaSentBytes;
302
+ this.totalSentFrames += this.deltaSentFrames;
303
+
304
+ this.rttInMs = rttInMsSum / Math.max(size, 1);
305
+ this.jitter = jitterSum / Math.max(size, 1);
306
+
307
+ this._lastMaxStatsTimestamp = maxStatsTimestamp;
308
+ this._updateQualityScore(maxStatsTimestamp);
309
+ }
310
+
311
+ private _updateQualityScore(timestamp: number) {
312
+ const newIssues = this.ωpendingIssuesForScores;
313
+ const score = calculateBaseVideoScore(this, newIssues);
314
+
315
+ if (0 < newIssues.length) {
316
+ this.ωpendingIssuesForScores = [];
317
+ }
318
+
319
+ if (!score) return (this.score = undefined);
320
+
321
+ score.timestamp = timestamp;
322
+ this.score = score;
323
+
324
+ this.emit('score', this.score);
325
+ }
326
+ }