@observertc/observer-js 0.40.8-beta → 0.40.10-beta

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 (65) hide show
  1. package/lib/ObservedClient.d.ts +5 -1
  2. package/lib/ObservedClient.d.ts.map +1 -1
  3. package/lib/ObservedClient.js +3 -1
  4. package/lib/ObservedDataChannel.d.ts +5 -1
  5. package/lib/ObservedDataChannel.d.ts.map +1 -1
  6. package/lib/ObservedDataChannel.js +3 -1
  7. package/lib/ObservedICE.d.ts +17 -1
  8. package/lib/ObservedICE.d.ts.map +1 -1
  9. package/lib/ObservedICE.js +37 -2
  10. package/lib/ObservedInboundTrack.d.ts +5 -1
  11. package/lib/ObservedInboundTrack.d.ts.map +1 -1
  12. package/lib/ObservedInboundTrack.js +3 -1
  13. package/lib/ObservedOutboundTrack.d.ts +5 -1
  14. package/lib/ObservedOutboundTrack.d.ts.map +1 -1
  15. package/lib/ObservedOutboundTrack.js +3 -1
  16. package/lib/ObservedPeerConnection.d.ts +5 -1
  17. package/lib/ObservedPeerConnection.d.ts.map +1 -1
  18. package/lib/ObservedPeerConnection.js +8 -4
  19. package/lib/ObservedSfu.d.ts +5 -1
  20. package/lib/ObservedSfu.d.ts.map +1 -1
  21. package/lib/ObservedSfu.js +6 -2
  22. package/lib/ObservedSfuInboundRtpPad.d.ts +5 -1
  23. package/lib/ObservedSfuInboundRtpPad.d.ts.map +1 -1
  24. package/lib/ObservedSfuInboundRtpPad.js +6 -2
  25. package/lib/ObservedSfuOutboundRtpPad.d.ts +5 -1
  26. package/lib/ObservedSfuOutboundRtpPad.d.ts.map +1 -1
  27. package/lib/ObservedSfuOutboundRtpPad.js +6 -2
  28. package/lib/ObservedSfuSctpChannel.d.ts +5 -1
  29. package/lib/ObservedSfuSctpChannel.d.ts.map +1 -1
  30. package/lib/ObservedSfuSctpChannel.js +6 -2
  31. package/lib/ObservedSfuTransport.d.ts +5 -1
  32. package/lib/ObservedSfuTransport.d.ts.map +1 -1
  33. package/lib/ObservedSfuTransport.js +6 -2
  34. package/lib/Observer.d.ts +7 -0
  35. package/lib/Observer.d.ts.map +1 -1
  36. package/lib/Observer.js +70 -0
  37. package/lib/index.d.ts +1 -0
  38. package/lib/index.d.ts.map +1 -1
  39. package/lib/{CallSummary.d.ts → monitors/CallSummary.d.ts} +7 -1
  40. package/lib/monitors/CallSummary.d.ts.map +1 -0
  41. package/lib/monitors/CallSummaryMonitor.d.ts +25 -0
  42. package/lib/monitors/CallSummaryMonitor.d.ts.map +1 -0
  43. package/lib/monitors/CallSummaryMonitor.js +82 -0
  44. package/lib/monitors/TurnUsageMonitor.d.ts +37 -0
  45. package/lib/monitors/TurnUsageMonitor.d.ts.map +1 -0
  46. package/lib/monitors/TurnUsageMonitor.js +80 -0
  47. package/package.json +3 -3
  48. package/src/ObservedClient.ts +7 -3
  49. package/src/ObservedDataChannel.ts +6 -2
  50. package/src/ObservedICE.ts +53 -4
  51. package/src/ObservedInboundTrack.ts +6 -2
  52. package/src/ObservedOutboundTrack.ts +6 -2
  53. package/src/ObservedPeerConnection.ts +13 -6
  54. package/src/ObservedSfu.ts +10 -3
  55. package/src/ObservedSfuInboundRtpPad.ts +9 -3
  56. package/src/ObservedSfuOutboundRtpPad.ts +9 -3
  57. package/src/ObservedSfuSctpChannel.ts +9 -3
  58. package/src/ObservedSfuTransport.ts +10 -4
  59. package/src/Observer.ts +86 -0
  60. package/src/index.ts +1 -0
  61. package/src/{CallSummary.ts → monitors/CallSummary.ts} +8 -2
  62. package/src/monitors/CallSummaryMonitor.ts +107 -0
  63. package/src/monitors/TurnUsageMonitor.ts +124 -0
  64. package/lib/CallSummary.d.ts.map +0 -1
  65. /package/lib/{CallSummary.js → monitors/CallSummary.js} +0 -0
@@ -8,7 +8,9 @@ import { ObservedICE } from './ObservedICE';
8
8
  import { ObservedDataChannel } from './ObservedDataChannel';
9
9
 
10
10
  export type ObservedPeerConnectionEvents = {
11
- update: [],
11
+ update: [{
12
+ elapsedTimeInMs: number;
13
+ }],
12
14
  close: [],
13
15
  newinboudaudiotrack: [ObservedInboundTrack<'audio'>],
14
16
  newinboudvideotrack: [ObservedInboundTrack<'video'>],
@@ -119,7 +121,7 @@ export class ObservedPeerConnection extends EventEmitter {
119
121
  }
120
122
 
121
123
  public get usingTURN() {
122
- return this.ICE.selectedRemoteCandidate?.candidateType?.toLowerCase() === 'relay';
124
+ return this.ICE.usingTURN;
123
125
  }
124
126
 
125
127
  public get availableOutgoingBitrate() {
@@ -224,6 +226,8 @@ export class ObservedPeerConnection extends EventEmitter {
224
226
  this.sendingVideoBitrate = 0;
225
227
  this.receivingAudioBitrate = 0;
226
228
  this.receivingVideoBitrate = 0;
229
+
230
+ this.ICE.resetMetrics();
227
231
  }
228
232
 
229
233
  public updateMetrics() {
@@ -275,7 +279,7 @@ export class ObservedPeerConnection extends EventEmitter {
275
279
 
276
280
  this._outboundVideoTracks.forEach((track) => {
277
281
  track.updateMetrics();
278
-
282
+
279
283
  this.deltaOutboundPacketsSent += track.deltaSentPackets;
280
284
  this.deltaOutboundSentBytes += track.deltaSentBytes;
281
285
 
@@ -325,7 +329,8 @@ export class ObservedPeerConnection extends EventEmitter {
325
329
  if (this._model.label !== sample.label) {
326
330
  this._model.label = sample.label;
327
331
  }
328
-
332
+ const now = Date.now();
333
+ const elapsedTimeInMs = now - this._updated;
329
334
  const report: PeerConnectionTransportReport = {
330
335
  serviceId: this.client.call.serviceId,
331
336
  roomId: this.client.call.roomId,
@@ -341,8 +346,10 @@ export class ObservedPeerConnection extends EventEmitter {
341
346
 
342
347
  this.reports.addPeerConnectionTransportReports(report);
343
348
 
344
- this._updated = Date.now();
345
- this.emit('update');
349
+ this._updated = now;
350
+ this.emit('update', {
351
+ elapsedTimeInMs,
352
+ });
346
353
  }
347
354
 
348
355
  public createInboundAudioTrack(config: Omit<ObservedInboundTrackModel<'audio'>, 'kind'>): ObservedInboundTrack<'audio'> {
@@ -12,7 +12,9 @@ export type ObservedSfuModel= {
12
12
  };
13
13
 
14
14
  export type ObservedSfuEvents = {
15
- update: [],
15
+ update: [{
16
+ elapsedTimeInMs: number;
17
+ }],
16
18
  close: [],
17
19
  newtransport: [ObservedSfuTransport],
18
20
  };
@@ -103,6 +105,9 @@ export class ObservedSfu<AppData extends Record<string, unknown> = Record<string
103
105
  this._timeZoneOffsetInHours = sample.timeZoneOffsetInHours;
104
106
  }
105
107
 
108
+ const now = Date.now();
109
+ const elapsedTimeInMs = now - this._updated;
110
+
106
111
  for (const customSfuEvents of sample.customSfuEvents ?? []) {
107
112
  const report: SfuEventReport = {
108
113
  serviceId: this.serviceId,
@@ -174,8 +179,10 @@ export class ObservedSfu<AppData extends Record<string, unknown> = Record<string
174
179
  observedSfuSctpChannel.update(sctpChannel, sample.timestamp);
175
180
  }
176
181
 
177
- this._updated = Date.now();
178
- this.emit('update');
182
+ this._updated = now;
183
+ this.emit('update', {
184
+ elapsedTimeInMs,
185
+ });
179
186
  }
180
187
 
181
188
  private _createTransport(model: ObservedSfuTransportModel) {
@@ -8,7 +8,9 @@ export type ObservedSfuInboundRtpPadModel= {
8
8
  };
9
9
 
10
10
  export type ObservedSfuInboundRtpPadEvents = {
11
- update: [],
11
+ update: [{
12
+ elapsedTimeInMs: number;
13
+ }],
12
14
  close: [],
13
15
  };
14
16
 
@@ -76,6 +78,8 @@ export class ObservedSfuInboundRtpPad extends EventEmitter {
76
78
  }
77
79
 
78
80
  public update(sample: SfuInboundRtpPad, timestamp: number) {
81
+ const now = Date.now();
82
+ const elapsedTimeInMs = now - this._updated;
79
83
 
80
84
  const { streamId: sfuStreamId, padId: rtpPadId, ...reportData } = sample;
81
85
 
@@ -94,7 +98,9 @@ export class ObservedSfuInboundRtpPad extends EventEmitter {
94
98
  this.reports.addSfuInboundRtpPadReport(report);
95
99
  this.stats = sample;
96
100
 
97
- this._updated = Date.now();
98
- this.emit('update');
101
+ this._updated = now;
102
+ this.emit('update', {
103
+ elapsedTimeInMs,
104
+ });
99
105
  }
100
106
  }
@@ -8,7 +8,9 @@ export type ObservedSfuOutboundRtpPadModel= {
8
8
  };
9
9
 
10
10
  export type ObservedSfuOutboundRtpPadEvents = {
11
- update: [],
11
+ update: [{
12
+ elapsedTimeInMs: number;
13
+ }],
12
14
  close: [],
13
15
  };
14
16
 
@@ -76,6 +78,8 @@ export class ObservedSfuOutboundRtpPad extends EventEmitter {
76
78
  }
77
79
 
78
80
  public update(sample: SfuOutboundRtpPad, timestamp: number) {
81
+ const now = Date.now();
82
+ const elapsedTimeInMs = now - this._updated;
79
83
 
80
84
  const { streamId: sfuStreamId, padId: rtpPadId, sinkId: sfuSinkId, ...reportData } = sample;
81
85
 
@@ -95,7 +99,9 @@ export class ObservedSfuOutboundRtpPad extends EventEmitter {
95
99
  this.reports.addSfuOutboundRtpPadReport(report);
96
100
  this.stats = sample;
97
101
 
98
- this._updated = Date.now();
99
- this.emit('update');
102
+ this._updated = now;
103
+ this.emit('update', {
104
+ elapsedTimeInMs,
105
+ });
100
106
  }
101
107
  }
@@ -8,7 +8,9 @@ export type ObservedSfuSctpChannelModel= {
8
8
  };
9
9
 
10
10
  export type ObservedSfuSctpChannelEvents = {
11
- update: [],
11
+ update: [{
12
+ elapsedTimeInMs: number;
13
+ }],
12
14
  close: [],
13
15
  };
14
16
 
@@ -76,6 +78,8 @@ export class ObservedSfuSctpChannel extends EventEmitter {
76
78
  }
77
79
 
78
80
  public update(sample: SfuSctpChannel, timestamp: number) {
81
+ const now = Date.now();
82
+ const elapsedTimeInMs = now - this._updated;
79
83
 
80
84
  const report: SfuSctpStreamReport = {
81
85
  serviceId: this.serviceId,
@@ -91,7 +95,9 @@ export class ObservedSfuSctpChannel extends EventEmitter {
91
95
  this.reports.addSfuTransportReport(report);
92
96
  this.stats = sample;
93
97
 
94
- this._updated = Date.now();
95
- this.emit('update');
98
+ this._updated = now;
99
+ this.emit('update', {
100
+ elapsedTimeInMs,
101
+ });
96
102
  }
97
103
  }
@@ -11,7 +11,9 @@ export type ObservedSfuTransportModel= {
11
11
  };
12
12
 
13
13
  export type ObservedSfuTransportEvents = {
14
- update: [],
14
+ update: [{
15
+ elapsedTimeInMs: number;
16
+ }],
15
17
  close: [],
16
18
  newsfuoutboundrtppad: [ObservedSfuOutboundRtpPad],
17
19
  newsfuinboundrtppad: [ObservedSfuInboundRtpPad],
@@ -110,7 +112,9 @@ export class ObservedSfuTransport extends EventEmitter {
110
112
  }
111
113
 
112
114
  public update(sample: SfuTransport, timestamp: number) {
113
-
115
+ const now = Date.now();
116
+ const elapsedTimeInMs = now - this._updated;
117
+
114
118
  const report: SFUTransportReport = {
115
119
  serviceId: this.serviceId,
116
120
  mediaUnitId: this.mediaUnitId,
@@ -125,8 +129,10 @@ export class ObservedSfuTransport extends EventEmitter {
125
129
  this.reports.addSfuTransportReport(report);
126
130
  this.stats = sample;
127
131
 
128
- this._updated = Date.now();
129
- this.emit('update');
132
+ this._updated = now;
133
+ this.emit('update', {
134
+ elapsedTimeInMs,
135
+ });
130
136
  }
131
137
 
132
138
  public createSfuOutboundRtpPad(model: ObservedSfuOutboundRtpPadModel) {
package/src/Observer.ts CHANGED
@@ -6,6 +6,10 @@ import { PartialBy } from './common/utils';
6
6
  import { createCallEndedEventReport, createCallStartedEventReport } from './common/callEventReports';
7
7
  import { ObserverSinkContext } from './common/types';
8
8
  import { ObservedSfu, ObservedSfuModel } from './ObservedSfu';
9
+ import { CallSummaryMonitor } from './monitors/CallSummaryMonitor';
10
+ import { TurnUsageMonitor } from './monitors/TurnUsageMonitor';
11
+ import { ObservedClient } from './ObservedClient';
12
+ import { ObservedPeerConnection } from './ObservedPeerConnection';
9
13
 
10
14
  const logger = createLogger('Observer');
11
15
 
@@ -69,6 +73,7 @@ export class Observer extends EventEmitter {
69
73
  public readonly reports = new ReportsCollector();
70
74
  private readonly _observedCalls = new Map<string, ObservedCall>();
71
75
  private readonly _observedSfus = new Map<string, ObservedSfu>();
76
+ private readonly _monitors = new Map<string, { close:() => void, once: (e: 'close', l: () => void) => void }>();
72
77
 
73
78
  private _reportTimer?: ReturnType<typeof setTimeout>;
74
79
  private _closed = false;
@@ -160,6 +165,87 @@ export class Observer extends EventEmitter {
160
165
  return sfu;
161
166
  }
162
167
 
168
+ public createCallSummaryMonitor(options?: { timeoutAfterCallClose?: number }): CallSummaryMonitor {
169
+ if (this._closed) throw new Error('Cannot create a call summary monitor on a closed observer');
170
+
171
+ const existingMonitor = this._monitors.get(CallSummaryMonitor.name);
172
+
173
+ if (existingMonitor) return existingMonitor as CallSummaryMonitor;
174
+
175
+ const monitor = new CallSummaryMonitor();
176
+ const onNewCall = (call: ObservedCall) => {
177
+ monitor.addCall(call);
178
+
179
+ call.once('close', () => setTimeout(() => {
180
+ const summary = monitor.takeSummary(call.callId);
181
+
182
+ summary && monitor.emit('summary', summary);
183
+
184
+ }, options?.timeoutAfterCallClose ?? 1000));
185
+ };
186
+
187
+ monitor.once('close', () => {
188
+ this._monitors.delete(CallSummaryMonitor.name);
189
+ this.off('newcall', onNewCall);
190
+ });
191
+
192
+ this._monitors.set(CallSummaryMonitor.name, monitor);
193
+ this.on('newcall', onNewCall);
194
+
195
+ this.once('close', () => {
196
+ monitor.close();
197
+ });
198
+
199
+ return monitor;
200
+ }
201
+
202
+ public createTurnUsageMonitor() {
203
+ if (this._closed) throw new Error('Cannot create a turn usage monitor on a closed observer');
204
+
205
+ const existingMonitor = this._monitors.get(TurnUsageMonitor.name);
206
+
207
+ if (existingMonitor) return existingMonitor as TurnUsageMonitor;
208
+
209
+ const monitor = new TurnUsageMonitor();
210
+
211
+ const onNewCall = (call: ObservedCall) => {
212
+ const onNewClient = (client: ObservedClient) => {
213
+ const onNewPeerConnection = (pc: ObservedPeerConnection) => {
214
+ const onUsingTurnChanged = (usingTurn: boolean) => {
215
+ if (usingTurn) monitor.addPeerConnection(pc);
216
+ else monitor.removePeerConnection(pc);
217
+ };
218
+
219
+ pc.once('close', () => {
220
+ pc.ICE.off('usingturnchanged', onUsingTurnChanged);
221
+ monitor.removePeerConnection(pc);
222
+ });
223
+ pc.ICE.on('usingturnchanged', onUsingTurnChanged);
224
+ };
225
+
226
+ client.once('close', () => client.off('newpeerconnection', onNewPeerConnection));
227
+ client.on('newpeerconnection', onNewPeerConnection);
228
+ };
229
+
230
+ call.once('close', () => call.off('newclient', onNewClient));
231
+ call.on('newclient', onNewClient);
232
+ };
233
+
234
+ monitor.once('close', () => {
235
+ this._monitors.delete(CallSummaryMonitor.name);
236
+ this.off('newcall', onNewCall);
237
+ });
238
+
239
+ this._monitors.set(CallSummaryMonitor.name, monitor);
240
+ this.on('newcall', onNewCall);
241
+
242
+ this.once('close', () => {
243
+ monitor.close();
244
+ });
245
+
246
+ return monitor;
247
+ }
248
+
163
249
  public get observedCalls(): ReadonlyMap<string, ObservedCall> {
164
250
  return this._observedCalls;
165
251
  }
package/src/index.ts CHANGED
@@ -52,6 +52,7 @@ export type {
52
52
  SFUTransportReport,
53
53
  } from '@observertc/report-schemas-js';
54
54
 
55
+ export type { ObservedCall, ObservedCallModel } from './ObservedCall';
55
56
  export type { ObservedClient, ObservedClientModel } from './ObservedClient';
56
57
  export type { ObservedPeerConnection, ObservedPeerConnectionModel, ObservedPeerConnectionEvents } from './ObservedPeerConnection';
57
58
  export type { ObservedInboundTrack, ObservedInboundTrackModel, ObservedInboundTrackEvents } from './ObservedInboundTrack';
@@ -1,8 +1,14 @@
1
1
  export interface ClientSummary {
2
2
  clientId: string;
3
- userId: string;
3
+ mediaUnitId: string;
4
+ userId?: string;
4
5
  joined: number;
5
6
  durationInMs: number;
7
+ avgOutboundAudioBitrate: number,
8
+ avgOutboundVideoBitrate: number,
9
+ avgInboundAudioBitrate: number,
10
+ avgInboundVideoBitrate: number,
11
+ ewmaRttInMs: number,
6
12
  }
7
13
 
8
14
  export interface CallSummary {
@@ -12,4 +18,4 @@ export interface CallSummary {
12
18
  started: number;
13
19
  durationInMs: number;
14
20
  clients: ClientSummary[];
15
- }
21
+ }
@@ -0,0 +1,107 @@
1
+ import { EventEmitter } from 'events';
2
+ import { CallSummary } from './CallSummary';
3
+ import { ObservedCall } from '../ObservedCall';
4
+ import { ObservedClient } from '../ObservedClient';
5
+
6
+ export type CallSummaryMonitorEvents = {
7
+ close: [],
8
+ summary: [CallSummary],
9
+ }
10
+
11
+ export declare interface CallSummaryMonitor {
12
+ on<U extends keyof CallSummaryMonitorEvents>(event: U, listener: (...args: CallSummaryMonitorEvents[U]) => void): this;
13
+ off<U extends keyof CallSummaryMonitorEvents>(event: U, listener: (...args: CallSummaryMonitorEvents[U]) => void): this;
14
+ once<U extends keyof CallSummaryMonitorEvents>(event: U, listener: (...args: CallSummaryMonitorEvents[U]) => void): this;
15
+ emit<U extends keyof CallSummaryMonitorEvents>(event: U, ...args: CallSummaryMonitorEvents[U]): boolean;
16
+ }
17
+
18
+ export class CallSummaryMonitor extends EventEmitter {
19
+ private readonly _summaries = new Map<string, CallSummary>();
20
+
21
+ private _closed = false;
22
+ public constructor() {
23
+ super();
24
+ }
25
+
26
+ public addCall(call: ObservedCall) {
27
+ if (this.closed) return;
28
+
29
+ let callSummary = this._summaries.get(call.callId);
30
+
31
+ if (!callSummary) {
32
+ callSummary = {
33
+ callId: call.callId,
34
+ roomId: call.roomId,
35
+ serviceId: call.serviceId,
36
+ clients: [],
37
+ durationInMs: 0,
38
+ started: call.created,
39
+ };
40
+ this._summaries.set(call.callId, callSummary);
41
+ }
42
+ const constCallSummary = callSummary;
43
+ const onNewClient = (client: ObservedClient) => this._addClient(constCallSummary, client);
44
+
45
+ call.once('close', () => {
46
+ call.off('newclient', onNewClient);
47
+ });
48
+ call.on('newclient', onNewClient);
49
+ }
50
+
51
+ private _addClient(callSummary: CallSummary, client: ObservedClient) {
52
+ if (this.closed) return;
53
+ const clientSummary = {
54
+ clientId: client.clientId,
55
+ mediaUnitId: client.mediaUnitId,
56
+ durationInMs: 0,
57
+ avgInboundAudioBitrate: 0,
58
+ avgInboundVideoBitrate: 0,
59
+ avgOutboundAudioBitrate: 0,
60
+ avgOutboundVideoBitrate: 0,
61
+ ewmaRttInMs: 0,
62
+ joined: client.created,
63
+ userId: client.userId,
64
+ };
65
+
66
+ callSummary.clients.push(clientSummary);
67
+
68
+ const updateClient = () => {
69
+ if (client.avgRttInMs) {
70
+ clientSummary.ewmaRttInMs = (clientSummary.ewmaRttInMs * 0.9) + (client.avgRttInMs * 0.1);
71
+ }
72
+ };
73
+
74
+ client.on('update', updateClient);
75
+ client.once('close', () => {
76
+ const elapsedTimeInS = (Date.now() - client.created) / 1000;
77
+
78
+ clientSummary.avgInboundAudioBitrate = (client.totalReceivedBytes * 8) / elapsedTimeInS;
79
+ clientSummary.avgInboundVideoBitrate = (client.totalReceivedBytes * 8) / elapsedTimeInS;
80
+ clientSummary.avgOutboundAudioBitrate = (client.totalSentBytes * 8) / elapsedTimeInS;
81
+ clientSummary.avgOutboundVideoBitrate = (client.totalSentBytes * 8) / elapsedTimeInS;
82
+ clientSummary.durationInMs = elapsedTimeInS * 1000;
83
+ client.off('update', updateClient);
84
+ });
85
+ }
86
+
87
+ public takeSummary(callId: string): CallSummary | undefined {
88
+ if (this.closed) return;
89
+
90
+ const summary = this._summaries.get(callId);
91
+
92
+ this._summaries.delete(callId);
93
+
94
+ return summary;
95
+ }
96
+
97
+ public get closed() {
98
+ return this._closed;
99
+ }
100
+
101
+ public close() {
102
+ if (this._closed) return;
103
+ this._closed = true;
104
+
105
+ this.emit('close');
106
+ }
107
+ }
@@ -0,0 +1,124 @@
1
+ import { EventEmitter } from 'events';
2
+ import { ObservedPeerConnection } from '../ObservedPeerConnection';
3
+ import { ObservedICEEvents } from '../ObservedICE';
4
+
5
+ export type TurnUsageMonitorEvents = {
6
+ close: [],
7
+ 'newturnip': [string],
8
+ update: [TurnUsage],
9
+ addpeerconnection: [ObservedPeerConnection],
10
+ removepeerconnection: [ObservedPeerConnection],
11
+ }
12
+
13
+ export type TurnUsage = {
14
+ turnIp: string;
15
+ totalBytesSent: number;
16
+ totalBytesReceived: number;
17
+ totalPacketsSent: number;
18
+ totalPacketsReceived: number;
19
+ }
20
+
21
+ type ObservedConnection = {
22
+ peerConnection: ObservedPeerConnection,
23
+ onUpdate: (...e: ObservedICEEvents['update']) => void;
24
+
25
+ }
26
+
27
+ export declare interface TurnUsageMonitor {
28
+ on<U extends keyof TurnUsageMonitorEvents>(event: U, listener: (...args: TurnUsageMonitorEvents[U]) => void): this;
29
+ off<U extends keyof TurnUsageMonitorEvents>(event: U, listener: (...args: TurnUsageMonitorEvents[U]) => void): this;
30
+ once<U extends keyof TurnUsageMonitorEvents>(event: U, listener: (...args: TurnUsageMonitorEvents[U]) => void): this;
31
+ emit<U extends keyof TurnUsageMonitorEvents>(event: U, ...args: TurnUsageMonitorEvents[U]): boolean;
32
+ }
33
+
34
+ export class TurnUsageMonitor extends EventEmitter {
35
+ private readonly _connections = new Map<string, ObservedConnection>();
36
+ private readonly _turnUsage = new Map<string, TurnUsage>();
37
+
38
+ private _closed = false;
39
+ public constructor() {
40
+ super();
41
+ }
42
+
43
+ public addPeerConnection(peerConnection: ObservedPeerConnection) {
44
+ if (this.closed) return;
45
+ if (this._connections.has(peerConnection.peerConnectionId)) return;
46
+
47
+ const ice = peerConnection.ICE;
48
+
49
+ const onUpdate = () => {
50
+ const turnIp = ice?.selectedRemoteCandidate?.address;
51
+
52
+ if (!peerConnection.usingTURN || !turnIp) return;
53
+
54
+ let usage = this._turnUsage.get(turnIp);
55
+
56
+ if (!usage) {
57
+ usage = {
58
+ turnIp,
59
+ totalBytesSent: 0,
60
+ totalBytesReceived: 0,
61
+ totalPacketsSent: 0,
62
+ totalPacketsReceived: 0,
63
+ };
64
+ this._turnUsage.set(turnIp, usage);
65
+
66
+ this.emit('newturnip', turnIp);
67
+ }
68
+
69
+ usage.totalBytesSent += ice.deltaBytesSent;
70
+ usage.totalBytesReceived += ice.deltaBytesReceived;
71
+ usage.totalPacketsSent += ice.deltaPacketsSent;
72
+ usage.totalPacketsReceived += ice.deltaPacketsReceived;
73
+
74
+ this.emit('update', usage);
75
+ };
76
+
77
+ peerConnection.ICE.on('update', onUpdate);
78
+
79
+ this._connections.set(peerConnection.peerConnectionId, {
80
+ peerConnection,
81
+ onUpdate
82
+ });
83
+
84
+ this.emit('addpeerconnection', peerConnection);
85
+ }
86
+
87
+ public removePeerConnection(peerConnection: ObservedPeerConnection) {
88
+ if (this.closed) return;
89
+ if (!this._connections.has(peerConnection.peerConnectionId)) return;
90
+
91
+ const { onUpdate } = this._connections.get(peerConnection.peerConnectionId) ?? {};
92
+
93
+ if (!onUpdate) return;
94
+
95
+ peerConnection.ICE.off('update', onUpdate);
96
+
97
+ this._connections.delete(peerConnection.peerConnectionId);
98
+
99
+ this.emit('removepeerconnection', peerConnection);
100
+ }
101
+
102
+ public get turnIps() {
103
+ return Array.from(this._turnUsage.keys());
104
+ }
105
+
106
+ public get peerConnections() {
107
+ return Array.from(this._connections.values()).map(({ peerConnection }) => peerConnection);
108
+ }
109
+
110
+ public getUsage(turnIp: string) {
111
+ return this._turnUsage.get(turnIp);
112
+ }
113
+
114
+ public get closed() {
115
+ return this._closed;
116
+ }
117
+
118
+ public close() {
119
+ if (this._closed) return;
120
+ this._closed = true;
121
+
122
+ this.emit('close');
123
+ }
124
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"CallSummary.d.ts","sourceRoot":"","sources":["../src/CallSummary.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,WAAW;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,aAAa,EAAE,CAAC;CACzB"}
File without changes