@observertc/observer-js 1.0.1-ec27289.0 → 1.1.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 (210) hide show
  1. package/README.md +2184 -1
  2. package/dist/index.d.mts +6842 -0
  3. package/dist/index.d.ts +6842 -0
  4. package/dist/index.js +7736 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +7608 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +44 -18
  9. package/.eslintrc.json +0 -143
  10. package/.prettierignore +0 -5
  11. package/.prettierrc +0 -7
  12. package/jest.config.js +0 -9
  13. package/lib/ObservedCall.d.ts +0 -83
  14. package/lib/ObservedCall.d.ts.map +0 -1
  15. package/lib/ObservedCall.js +0 -203
  16. package/lib/ObservedCallSummary.d.ts +0 -18
  17. package/lib/ObservedCallSummary.d.ts.map +0 -1
  18. package/lib/ObservedCallSummary.js +0 -2
  19. package/lib/ObservedCertificate.d.ts +0 -19
  20. package/lib/ObservedCertificate.d.ts.map +0 -1
  21. package/lib/ObservedCertificate.js +0 -38
  22. package/lib/ObservedClient.d.ts +0 -131
  23. package/lib/ObservedClient.d.ts.map +0 -1
  24. package/lib/ObservedClient.js +0 -520
  25. package/lib/ObservedClientEventMonitor.d.ts +0 -89
  26. package/lib/ObservedClientEventMonitor.d.ts.map +0 -1
  27. package/lib/ObservedClientEventMonitor.js +0 -247
  28. package/lib/ObservedClientSummary.d.ts +0 -23
  29. package/lib/ObservedClientSummary.d.ts.map +0 -1
  30. package/lib/ObservedClientSummary.js +0 -2
  31. package/lib/ObservedCodec.d.ts +0 -22
  32. package/lib/ObservedCodec.d.ts.map +0 -1
  33. package/lib/ObservedCodec.js +0 -45
  34. package/lib/ObservedDataChannel.d.ts +0 -30
  35. package/lib/ObservedDataChannel.d.ts.map +0 -1
  36. package/lib/ObservedDataChannel.js +0 -75
  37. package/lib/ObservedIceCandidate.d.ts +0 -29
  38. package/lib/ObservedIceCandidate.d.ts.map +0 -1
  39. package/lib/ObservedIceCandidate.js +0 -59
  40. package/lib/ObservedIceCandidatePair.d.ts +0 -44
  41. package/lib/ObservedIceCandidatePair.d.ts.map +0 -1
  42. package/lib/ObservedIceCandidatePair.js +0 -111
  43. package/lib/ObservedIceTransport.d.ts +0 -36
  44. package/lib/ObservedIceTransport.d.ts.map +0 -1
  45. package/lib/ObservedIceTransport.js +0 -93
  46. package/lib/ObservedInboundRtp.d.ts +0 -92
  47. package/lib/ObservedInboundRtp.d.ts.map +0 -1
  48. package/lib/ObservedInboundRtp.js +0 -193
  49. package/lib/ObservedInboundTrack.d.ts +0 -31
  50. package/lib/ObservedInboundTrack.d.ts.map +0 -1
  51. package/lib/ObservedInboundTrack.js +0 -59
  52. package/lib/ObservedMediaPlayout.d.ts +0 -22
  53. package/lib/ObservedMediaPlayout.d.ts.map +0 -1
  54. package/lib/ObservedMediaPlayout.js +0 -42
  55. package/lib/ObservedMediaSource.d.ts +0 -28
  56. package/lib/ObservedMediaSource.d.ts.map +0 -1
  57. package/lib/ObservedMediaSource.js +0 -55
  58. package/lib/ObservedOutboundRtp.d.ts +0 -62
  59. package/lib/ObservedOutboundRtp.d.ts.map +0 -1
  60. package/lib/ObservedOutboundRtp.js +0 -144
  61. package/lib/ObservedOutboundTrack.d.ts +0 -31
  62. package/lib/ObservedOutboundTrack.d.ts.map +0 -1
  63. package/lib/ObservedOutboundTrack.js +0 -59
  64. package/lib/ObservedPeerConnection.d.ts +0 -185
  65. package/lib/ObservedPeerConnection.d.ts.map +0 -1
  66. package/lib/ObservedPeerConnection.js +0 -710
  67. package/lib/ObservedPeerConnectionTransport.d.ts +0 -17
  68. package/lib/ObservedPeerConnectionTransport.d.ts.map +0 -1
  69. package/lib/ObservedPeerConnectionTransport.js +0 -34
  70. package/lib/ObservedRemoteInboundRtp.d.ts +0 -30
  71. package/lib/ObservedRemoteInboundRtp.d.ts.map +0 -1
  72. package/lib/ObservedRemoteInboundRtp.js +0 -60
  73. package/lib/ObservedRemoteOutboundRtp.d.ts +0 -30
  74. package/lib/ObservedRemoteOutboundRtp.d.ts.map +0 -1
  75. package/lib/ObservedRemoteOutboundRtp.js +0 -60
  76. package/lib/ObservedTURN.d.ts +0 -31
  77. package/lib/ObservedTURN.d.ts.map +0 -1
  78. package/lib/ObservedTURN.js +0 -58
  79. package/lib/ObservedTurnServer.d.ts +0 -25
  80. package/lib/ObservedTurnServer.d.ts.map +0 -1
  81. package/lib/ObservedTurnServer.js +0 -59
  82. package/lib/Observer.d.ts +0 -61
  83. package/lib/Observer.d.ts.map +0 -1
  84. package/lib/Observer.js +0 -156
  85. package/lib/ObserverSummary.d.ts +0 -13
  86. package/lib/ObserverSummary.d.ts.map +0 -1
  87. package/lib/ObserverSummary.js +0 -2
  88. package/lib/common/Middleware.d.ts +0 -17
  89. package/lib/common/Middleware.d.ts.map +0 -1
  90. package/lib/common/Middleware.js +0 -60
  91. package/lib/common/SingleExecutor.d.ts +0 -3
  92. package/lib/common/SingleExecutor.d.ts.map +0 -1
  93. package/lib/common/SingleExecutor.js +0 -31
  94. package/lib/common/logger.d.ts +0 -17
  95. package/lib/common/logger.d.ts.map +0 -1
  96. package/lib/common/logger.js +0 -50
  97. package/lib/common/types.d.ts +0 -3
  98. package/lib/common/types.d.ts.map +0 -1
  99. package/lib/common/types.js +0 -3
  100. package/lib/common/utils.d.ts +0 -11
  101. package/lib/common/utils.d.ts.map +0 -1
  102. package/lib/common/utils.js +0 -63
  103. package/lib/detectors/Detector.d.ts +0 -5
  104. package/lib/detectors/Detector.d.ts.map +0 -1
  105. package/lib/detectors/Detector.js +0 -3
  106. package/lib/detectors/Detectors.d.ts +0 -11
  107. package/lib/detectors/Detectors.d.ts.map +0 -1
  108. package/lib/detectors/Detectors.js +0 -34
  109. package/lib/index.d.ts +0 -27
  110. package/lib/index.d.ts.map +0 -1
  111. package/lib/index.js +0 -47
  112. package/lib/monitors/TurnUsageMonitor.d.ts +0 -1
  113. package/lib/monitors/TurnUsageMonitor.d.ts.map +0 -1
  114. package/lib/monitors/TurnUsageMonitor.js +0 -146
  115. package/lib/schema/ClientEventTypes.d.ts +0 -100
  116. package/lib/schema/ClientEventTypes.d.ts.map +0 -1
  117. package/lib/schema/ClientEventTypes.js +0 -38
  118. package/lib/schema/ClientMetaTypes.d.ts +0 -34
  119. package/lib/schema/ClientMetaTypes.d.ts.map +0 -1
  120. package/lib/schema/ClientMetaTypes.js +0 -16
  121. package/lib/schema/ClientSample.d.ts +0 -1333
  122. package/lib/schema/ClientSample.d.ts.map +0 -1
  123. package/lib/schema/ClientSample.js +0 -4
  124. package/lib/scores/CalculatedScore.d.ts +0 -6
  125. package/lib/scores/CalculatedScore.d.ts.map +0 -1
  126. package/lib/scores/CalculatedScore.js +0 -2
  127. package/lib/scores/DefaultCallScoreCalculator.d.ts +0 -7
  128. package/lib/scores/DefaultCallScoreCalculator.d.ts.map +0 -1
  129. package/lib/scores/DefaultCallScoreCalculator.js +0 -21
  130. package/lib/scores/ScoreCalculator.d.ts +0 -4
  131. package/lib/scores/ScoreCalculator.d.ts.map +0 -1
  132. package/lib/scores/ScoreCalculator.js +0 -2
  133. package/lib/updaters/CallUpdater.d.ts +0 -5
  134. package/lib/updaters/CallUpdater.d.ts.map +0 -1
  135. package/lib/updaters/CallUpdater.js +0 -2
  136. package/lib/updaters/ObserverUpdater.d.ts +0 -5
  137. package/lib/updaters/ObserverUpdater.d.ts.map +0 -1
  138. package/lib/updaters/ObserverUpdater.js +0 -2
  139. package/lib/updaters/OnAllCallObserverUpdater_.d.ts +0 -13
  140. package/lib/updaters/OnAllCallObserverUpdater_.d.ts.map +0 -1
  141. package/lib/updaters/OnAllCallObserverUpdater_.js +0 -38
  142. package/lib/updaters/OnAllClientCallUpdater.d.ts +0 -13
  143. package/lib/updaters/OnAllClientCallUpdater.d.ts.map +0 -1
  144. package/lib/updaters/OnAllClientCallUpdater.js +0 -37
  145. package/lib/updaters/OnAnyCallObserverUpdater_.d.ts +0 -11
  146. package/lib/updaters/OnAnyCallObserverUpdater_.d.ts.map +0 -1
  147. package/lib/updaters/OnAnyCallObserverUpdater_.js +0 -27
  148. package/lib/updaters/OnAnyClientCallUpdater.d.ts +0 -11
  149. package/lib/updaters/OnAnyClientCallUpdater.d.ts.map +0 -1
  150. package/lib/updaters/OnAnyClientCallUpdater.js +0 -27
  151. package/lib/updaters/OnIntervalUpdater.d.ts +0 -10
  152. package/lib/updaters/OnIntervalUpdater.d.ts.map +0 -1
  153. package/lib/updaters/OnIntervalUpdater.js +0 -17
  154. package/lib/updaters/Updater.d.ts +0 -6
  155. package/lib/updaters/Updater.d.ts.map +0 -1
  156. package/lib/updaters/Updater.js +0 -2
  157. package/lib/utils/RemoteTrackResolver.d.ts +0 -7
  158. package/lib/utils/RemoteTrackResolver.d.ts.map +0 -1
  159. package/lib/utils/RemoteTrackResolver.js +0 -2
  160. package/src/ObservedCall.ts +0 -322
  161. package/src/ObservedCallSummary.ts +0 -22
  162. package/src/ObservedCertificate.ts +0 -43
  163. package/src/ObservedClient.ts +0 -673
  164. package/src/ObservedClientEventMonitor.ts +0 -301
  165. package/src/ObservedClientSummary.ts +0 -24
  166. package/src/ObservedCodec.ts +0 -49
  167. package/src/ObservedDataChannel.ts +0 -85
  168. package/src/ObservedIceCandidate.ts +0 -64
  169. package/src/ObservedIceCandidatePair.ts +0 -118
  170. package/src/ObservedIceTransport.ts +0 -99
  171. package/src/ObservedInboundRtp.ts +0 -205
  172. package/src/ObservedInboundTrack.ts +0 -73
  173. package/src/ObservedMediaPlayout.ts +0 -49
  174. package/src/ObservedMediaSource.ts +0 -60
  175. package/src/ObservedOutboundRtp.ts +0 -156
  176. package/src/ObservedOutboundTrack.ts +0 -72
  177. package/src/ObservedPeerConnection.ts +0 -1019
  178. package/src/ObservedPeerConnectionTransport.ts +0 -38
  179. package/src/ObservedRemoteInboundRtp.ts +0 -64
  180. package/src/ObservedRemoteOutboundRtp.ts +0 -65
  181. package/src/ObservedTURN.ts +0 -86
  182. package/src/ObservedTurnServer.ts +0 -72
  183. package/src/Observer.ts +0 -239
  184. package/src/ObserverSummary.ts +0 -15
  185. package/src/common/Middleware.ts +0 -84
  186. package/src/common/SingleExecutor.ts +0 -36
  187. package/src/common/logger.ts +0 -82
  188. package/src/common/types.ts +0 -4
  189. package/src/common/utils.ts +0 -69
  190. package/src/detectors/Detector.ts +0 -6
  191. package/src/detectors/Detectors.ts +0 -38
  192. package/src/index.ts +0 -28
  193. package/src/monitors/TurnUsageMonitor.ts +0 -187
  194. package/src/schema/ClientEventTypes.ts +0 -112
  195. package/src/schema/ClientMetaTypes.ts +0 -41
  196. package/src/schema/ClientSample.ts +0 -1682
  197. package/src/scores/CalculatedScore.ts +0 -6
  198. package/src/scores/DefaultCallScoreCalculator.ts +0 -23
  199. package/src/scores/ScoreCalculator.ts +0 -3
  200. package/src/updaters/CallUpdater.ts +0 -5
  201. package/src/updaters/ObserverUpdater.ts +0 -5
  202. package/src/updaters/OnAllCallObserverUpdater_.ts +0 -46
  203. package/src/updaters/OnAllClientCallUpdater.ts +0 -46
  204. package/src/updaters/OnAnyCallObserverUpdater_.ts +0 -32
  205. package/src/updaters/OnAnyClientCallUpdater.ts +0 -32
  206. package/src/updaters/OnIntervalUpdater.ts +0 -19
  207. package/src/updaters/Updater.ts +0 -5
  208. package/src/utils/RemoteTrackResolver.ts +0 -12
  209. package/tsconfig.json +0 -19
  210. package/tslint.json +0 -6
@@ -1,673 +0,0 @@
1
- import { EventEmitter } from 'events';
2
- import { ObservedPeerConnection } from './ObservedPeerConnection';
3
- import { createLogger } from './common/logger';
4
- // eslint-disable-next-line camelcase
5
- import { ClientEvent, ClientMetaData, ClientSample, PeerConnectionSample, ClientIssue, ExtensionStat } from './schema/ClientSample';
6
- import * as MetaData from './schema/ClientMetaTypes';
7
- import { ClientEventTypes } from './schema/ClientEventTypes';
8
- import { ObservedCall } from './ObservedCall';
9
- import { ClientMetaTypes } from './schema/ClientMetaTypes';
10
- import { parseJsonAs } from './common/utils';
11
- import { CalculatedScore } from './scores/CalculatedScore';
12
- import { Detectors } from './detectors/Detectors';
13
- import { ObservedClientEventMonitor } from './ObservedClientEventMonitor';
14
-
15
- const logger = createLogger('ObservedClient');
16
-
17
- export type ObservedClientSettings<AppData extends Record<string, unknown> = Record<string, unknown>> = {
18
- clientId: string;
19
- appData?: AppData;
20
- };
21
-
22
- export type ObservedClientEvents = {
23
- update: [
24
- {
25
- sample: ClientSample;
26
- elapsedTimeInMs: number;
27
- }
28
- ];
29
- close: [];
30
- joined: [];
31
- issue: [ClientIssue];
32
- metaData: [ClientMetaData];
33
- rejoined: [timestamp: number];
34
- left: [];
35
- usingturn: [boolean];
36
- usermediaerror: [string];
37
-
38
- // do we need this?
39
- newpeerconnection: [ObservedPeerConnection];
40
- };
41
-
42
- export declare interface ObservedClient {
43
- on<U extends keyof ObservedClientEvents>(event: U, listener: (...args: ObservedClientEvents[U]) => void): this;
44
- off<U extends keyof ObservedClientEvents>(event: U, listener: (...args: ObservedClientEvents[U]) => void): this;
45
- once<U extends keyof ObservedClientEvents>(event: U, listener: (...args: ObservedClientEvents[U]) => void): this;
46
- emit<U extends keyof ObservedClientEvents>(event: U, ...args: ObservedClientEvents[U]): boolean;
47
- }
48
-
49
- export class ObservedClient<AppData extends Record<string, unknown> = Record<string, unknown>> extends EventEmitter {
50
- public readonly detectors: Detectors;
51
-
52
- public readonly clientId: string;
53
- public readonly observedPeerConnections = new Map<string, ObservedPeerConnection>();
54
- public readonly calculatedScore: CalculatedScore = {
55
- weight: 1,
56
- value: undefined,
57
- };
58
-
59
- public appData: AppData;
60
- public attachments?: Record<string, unknown>;
61
-
62
- public updated = Date.now();
63
- public acceptedSamples = 0;
64
- public closed = false;
65
-
66
- public joinedAt?: number;
67
- public leftAt?: number;
68
- public closedAt?: number;
69
- // the timestamp of the CLIENT_JOINED event
70
- public operationSystem?: MetaData.OperationSystem;
71
- public engine?: MetaData.Engine;
72
- public platform?: MetaData.Platform;
73
- public browser?: MetaData.Browser;
74
- public mediaConstraints: string[] = [];
75
-
76
- public usingTURN = false;
77
- public usingTCP = false;
78
- public availableOutgoingBitrate = 0;
79
- public availableIncomingBitrate = 0;
80
-
81
- public totalInboundPacketsLost = 0;
82
- public totalInboundPacketsReceived = 0;
83
- public totalOutboundPacketsSent = 0;
84
- public totalDataChannelBytesSent = 0;
85
- public totalDataChannelBytesReceived = 0;
86
- public totalDataChannelMessagesSent = 0;
87
- public totalDataChannelMessagesReceived = 0;
88
- public totalReceivedAudioBytes = 0;
89
- public totalReceivedVideoBytes = 0;
90
- public totalSentAudioBytes = 0;
91
- public totalSentVideoBytes = 0;
92
- public totalSentBytes = 0;
93
- public totalReceivedBytes = 0;
94
-
95
- public deltaReceivedAudioBytes = 0;
96
- public deltaReceivedVideoBytes = 0;
97
- public deltaSentAudioBytes = 0;
98
- public deltaSentVideoBytes = 0;
99
- public deltaDataChannelBytesSent = 0;
100
- public deltaDataChannelBytesReceived = 0;
101
- public deltaDataChannelMessagesSent = 0;
102
- public deltaDataChannelMessagesReceived = 0;
103
- public deltaInboundPacketsLost = 0;
104
- public deltaInboundPacketsReceived = 0;
105
- public deltaOutboundPacketsSent = 0;
106
- public deltaTransportSentBytes = 0;
107
- public deltaTransportReceivedBytes = 0;
108
- public deltaRttLt50Measurements = 0;
109
- public deltaRttLt150Measurements = 0;
110
- public deltaRttLt300Measurements = 0;
111
- public deltaRttGtOrEq300Measurements = 0;
112
-
113
- public currentMaxRttInMs?: number;
114
- public currentMinRttInMs?: number;
115
- public currentAvgRttInMs?: number;
116
-
117
- public sendingAudioBitrate = 0;
118
- public sendingVideoBitrate = 0;
119
- public receivingAudioBitrate = 0;
120
- public receivingVideoBitrate = 0;
121
-
122
- public numberOfInboundRtpStreams = 0;
123
- public numberOfInbundTracks = 0;
124
- public numberOfOutboundRtpStreams = 0;
125
- public numberOfOutboundTracks = 0;
126
- public numberOfDataChannels = 0;
127
-
128
- public totalRttLt50Measurements = 0;
129
- public totalRttLt150Measurements = 0;
130
- public totalRttLt300Measurements = 0;
131
- public totalRttGtOrEq300Measurements = 0;
132
- public deltaNumberOfIssues = 0;
133
-
134
- public totalScoreSum = 0;
135
- public numberOfScoreMeasurements = 0;
136
- public totalNumberOfIssues = 0;
137
-
138
- public readonly mediaDevices: MetaData.MediaDeviceInfo[] = [];
139
- public issues: ClientIssue[] = [];
140
-
141
- private _injections: Pick<ClientSample, 'clientEvents' | 'clientIssues' | 'extensionStats' | 'attachments' | 'clientMetaItems'> = {};
142
-
143
- public constructor(settings: ObservedClientSettings<AppData>, public readonly call: ObservedCall) {
144
- super();
145
- this.setMaxListeners(Infinity);
146
-
147
- this.clientId = settings.clientId;
148
- this.appData = settings.appData ?? {} as AppData;
149
-
150
- this.detectors = new Detectors();
151
- }
152
-
153
- public get numberOfPeerConnections() {
154
- return this.observedPeerConnections.size;
155
- }
156
-
157
- public get score() {
158
- return this.calculatedScore.value;
159
- }
160
-
161
- public close() {
162
- if (this.closed) return;
163
- this.closed = true;
164
-
165
- Array.from(this.observedPeerConnections.values()).forEach((peerConnection) => peerConnection.close());
166
-
167
- this.closedAt = Date.now();
168
-
169
- this.emit('close');
170
- }
171
-
172
- public accept(sample: ClientSample): void {
173
- if (this.closed) throw new Error(`Client ${this.clientId} is closed`);
174
-
175
- const now = Date.now();
176
- const elapsedInMs = now - this.updated;
177
- const elapsedInSeconds = elapsedInMs / 1000;
178
- let sumOfRtts = 0;
179
- let numberOfRttMeasurements = 0;
180
-
181
- ++this.acceptedSamples;
182
-
183
- this.availableIncomingBitrate = 0;
184
- this.availableOutgoingBitrate = 0;
185
- this.deltaDataChannelBytesReceived = 0;
186
- this.deltaDataChannelBytesSent = 0;
187
- this.deltaDataChannelMessagesReceived = 0;
188
- this.deltaDataChannelMessagesSent = 0;
189
- this.deltaInboundPacketsLost = 0;
190
- this.deltaInboundPacketsReceived = 0;
191
- this.deltaOutboundPacketsSent = 0;
192
- this.deltaReceivedAudioBytes = 0;
193
- this.deltaReceivedVideoBytes = 0;
194
- this.deltaSentAudioBytes = 0;
195
- this.deltaSentVideoBytes = 0;
196
- this.deltaTransportReceivedBytes = 0;
197
- this.deltaTransportSentBytes = 0;
198
- this.deltaRttLt50Measurements = 0;
199
- this.deltaRttLt150Measurements = 0;
200
- this.deltaRttLt300Measurements = 0;
201
- this.deltaRttGtOrEq300Measurements = 0;
202
- this.deltaNumberOfIssues = 0;
203
-
204
- this.numberOfDataChannels = 0;
205
- this.numberOfInboundRtpStreams = 0;
206
- this.numberOfInbundTracks = 0;
207
- this.numberOfOutboundRtpStreams = 0;
208
- this.numberOfOutboundTracks = 0;
209
- this.usingTURN = false;
210
- this.usingTCP = false;
211
- this.currentMinRttInMs = undefined;
212
- this.currentMaxRttInMs = undefined;
213
-
214
- this._mergeInjections(sample);
215
-
216
- const clientEventsPostBuffer: ClientEvent[] = [];
217
-
218
- for (const clientEvent of sample.clientEvents ?? []) {
219
- this._processClientEvent(clientEvent, clientEventsPostBuffer);
220
-
221
- this.call.observer.emit('client-event', this, clientEvent);
222
- }
223
-
224
- for (const metaData of sample.clientMetaItems ?? []) {
225
- this.addMetadata(metaData);
226
- }
227
-
228
- for (const issue of sample.clientIssues ?? []) {
229
- this.addIssue(issue);
230
-
231
- ++this.deltaNumberOfIssues;
232
- }
233
-
234
- for (const extensionStat of sample.extensionStats ?? []) {
235
- this.addExtensionStats(extensionStat);
236
- }
237
-
238
- for (const pcSample of sample.peerConnections ?? []) {
239
- const observedPeerConnection = this._updatePeerConnection(pcSample);
240
-
241
- if (!observedPeerConnection) continue;
242
-
243
- this.deltaDataChannelBytesReceived += observedPeerConnection.deltaDataChannelBytesReceived;
244
- this.deltaDataChannelBytesSent += observedPeerConnection.deltaDataChannelBytesSent;
245
- this.deltaDataChannelMessagesReceived += observedPeerConnection.deltaDataChannelMessagesReceived;
246
- this.deltaDataChannelMessagesSent += observedPeerConnection.deltaDataChannelMessagesSent;
247
- this.deltaInboundPacketsLost += observedPeerConnection.deltaInboundPacketsLost;
248
- this.deltaInboundPacketsReceived += observedPeerConnection.deltaInboundPacketsReceived;
249
- this.deltaOutboundPacketsSent += observedPeerConnection.deltaOutboundPacketsSent;
250
- this.deltaReceivedAudioBytes += observedPeerConnection.deltaReceivedAudioBytes;
251
- this.deltaReceivedVideoBytes += observedPeerConnection.deltaReceivedVideoBytes;
252
- this.deltaSentAudioBytes += observedPeerConnection.deltaSentAudioBytes;
253
- this.deltaSentVideoBytes += observedPeerConnection.deltaSentVideoBytes;
254
- this.deltaTransportReceivedBytes += observedPeerConnection.deltaTransportReceivedBytes;
255
- this.deltaTransportSentBytes += observedPeerConnection.deltaTransportSentBytes;
256
-
257
- this.availableIncomingBitrate += observedPeerConnection.availableIncomingBitrate;
258
- this.availableOutgoingBitrate += observedPeerConnection.availableOutgoingBitrate;
259
-
260
- this.numberOfDataChannels += observedPeerConnection.observedDataChannels.size;
261
- this.numberOfInbundTracks += observedPeerConnection.observedInboundTracks.size;
262
- this.numberOfOutboundRtpStreams += observedPeerConnection.observedOutboundRtps.size;
263
- this.numberOfOutboundTracks += observedPeerConnection.observedOutboundTracks.size;
264
- this.numberOfInboundRtpStreams += observedPeerConnection.observedInboundRtps.size;
265
-
266
- if (observedPeerConnection.usingTURN) {
267
- this.usingTURN = true;
268
- }
269
- if (observedPeerConnection.usingTCP) {
270
- this.usingTCP = true;
271
- }
272
-
273
- if (observedPeerConnection.currentRttInMs) {
274
- if (this.currentMinRttInMs === undefined || observedPeerConnection.currentRttInMs < this.currentMinRttInMs) {
275
- this.currentMinRttInMs = observedPeerConnection.currentRttInMs;
276
- }
277
- if (this.currentMaxRttInMs === undefined || observedPeerConnection.currentRttInMs > this.currentMaxRttInMs) {
278
- this.currentMaxRttInMs = observedPeerConnection.currentRttInMs;
279
- }
280
- if (observedPeerConnection.currentRttInMs < 50) {
281
- this.deltaRttLt50Measurements += 1;
282
- } else if (observedPeerConnection.currentRttInMs < 150) {
283
- this.deltaRttLt150Measurements += 1;
284
- } else if (observedPeerConnection.currentRttInMs < 300) {
285
- this.deltaRttLt300Measurements += 1;
286
- } else if (300 <= observedPeerConnection.currentRttInMs) {
287
- this.deltaRttGtOrEq300Measurements += 1;
288
- }
289
-
290
- sumOfRtts += observedPeerConnection.currentRttInMs;
291
- ++numberOfRttMeasurements;
292
- }
293
- }
294
-
295
- for (const clientEvent of clientEventsPostBuffer) {
296
- this._processClientEvent(clientEvent);
297
- }
298
-
299
- // emit new attachments?
300
- this.attachments = sample.attachments;
301
-
302
- this.totalDataChannelBytesReceived += this.deltaDataChannelBytesReceived;
303
- this.totalDataChannelBytesSent += this.deltaDataChannelBytesSent;
304
- this.totalDataChannelMessagesReceived += this.deltaDataChannelMessagesReceived;
305
- this.totalDataChannelMessagesSent += this.deltaDataChannelMessagesSent;
306
- this.totalInboundPacketsLost += this.deltaInboundPacketsLost;
307
- this.totalInboundPacketsReceived += this.deltaInboundPacketsReceived;
308
- this.totalOutboundPacketsSent += this.deltaOutboundPacketsSent;
309
- this.totalReceivedAudioBytes += this.deltaReceivedAudioBytes;
310
- this.totalReceivedVideoBytes += this.deltaReceivedVideoBytes;
311
- this.totalSentAudioBytes += this.deltaSentAudioBytes;
312
- this.totalSentVideoBytes += this.deltaSentVideoBytes;
313
- this.totalReceivedBytes += this.deltaTransportReceivedBytes;
314
- this.totalSentBytes += this.deltaTransportSentBytes;
315
- this.totalRttLt50Measurements += this.deltaRttLt50Measurements;
316
- this.totalRttLt150Measurements += this.deltaRttLt150Measurements;
317
- this.totalRttLt300Measurements += this.deltaRttLt300Measurements;
318
- this.totalRttGtOrEq300Measurements += this.deltaRttGtOrEq300Measurements;
319
- this.totalNumberOfIssues += this.deltaNumberOfIssues;
320
-
321
- this.receivingAudioBitrate = (this.deltaReceivedAudioBytes * 8) / (elapsedInSeconds);
322
- this.receivingVideoBitrate = (this.totalReceivedVideoBytes * 8) / (elapsedInSeconds);
323
- this.sendingAudioBitrate = (this.deltaSentAudioBytes * 8) / (elapsedInSeconds);
324
- this.sendingVideoBitrate = (this.deltaSentVideoBytes * 8) / (elapsedInSeconds);
325
- this.currentAvgRttInMs = 0 < numberOfRttMeasurements ? sumOfRtts / numberOfRttMeasurements : undefined;
326
-
327
- this.calculatedScore.value = sample.score;
328
- this.detectors.update();
329
-
330
- // emit update
331
- this.emit('update', {
332
- sample,
333
- elapsedTimeInMs: now - this.updated,
334
- });
335
- this.updated = now;
336
-
337
- // if result changed after update
338
- if (this.calculatedScore.value) {
339
- this.totalScoreSum += this.calculatedScore.value;
340
- ++this.numberOfScoreMeasurements;
341
- }
342
- }
343
-
344
- private _processClientEvent(event: ClientEvent, postBuffer?: ClientEvent[]) {
345
- // eslint-disable-next-line no-console
346
- // console.warn('ClientEvent', event);
347
- switch (event.type) {
348
- case ClientEventTypes.CLIENT_JOINED: {
349
- if (event.timestamp) {
350
- if (!this.joinedAt) {
351
- this.joinedAt = event.timestamp;
352
- this.emit('joined');
353
- } else if (this.joinedAt < event.timestamp) {
354
- this.emit('rejoined', event.timestamp);
355
- } else {
356
- this.joinedAt = event.timestamp;
357
- logger.warn(`Client ${this.clientId} joinedAt timestamp was updated to ${event.timestamp}. the joined event will not be emitted.`);
358
- }
359
- }
360
- break;
361
- }
362
- case ClientEventTypes.CLIENT_LEFT: {
363
- if (event.timestamp) {
364
- if (!this.leftAt) {
365
- this.leftAt = event.timestamp;
366
- this.emit('left');
367
- } else {
368
- logger.warn(`Client ${this.clientId} leftAt timestamp was already set`);
369
- }
370
- }
371
- break;
372
- }
373
- case ClientEventTypes.PEER_CONNECTION_OPENED: {
374
- const payload = parseJsonAs<Record<string, unknown>>(event.payload);
375
-
376
- if (payload?.peerConnectionId && typeof payload.peerConnectionId === 'string') {
377
- const observedPeerConnection = this.observedPeerConnections.get(payload.peerConnectionId);
378
-
379
- if (observedPeerConnection) {
380
- observedPeerConnection.openedAt = event.timestamp;
381
- } else if (postBuffer) {
382
- postBuffer.push(event);
383
- } else {
384
- logger.warn('Received PEER_CONNECTION_OPENED event without a corresponding observedPeerConnection', event);
385
- }
386
-
387
- }
388
- break;
389
- }
390
- case ClientEventTypes.PEER_CONNECTION_CLOSED: {
391
- const payload = parseJsonAs<Record<string, unknown>>(event.payload);
392
-
393
- if (payload?.peerConnectionId && typeof payload.peerConnectionId === 'string') {
394
- const observedPeerConnection = this.observedPeerConnections.get(payload.peerConnectionId);
395
-
396
- if (observedPeerConnection) {
397
- observedPeerConnection.closedAt = event.timestamp;
398
- } else if (postBuffer) {
399
- postBuffer.push(event);
400
- } else {
401
- logger.warn('Received PEER_CONNECTION_CLOSED event without a corresponding observedPeerConnection', event);
402
- }
403
- }
404
- break;
405
- }
406
- case ClientEventTypes.MEDIA_TRACK_ADDED: {
407
- const payload = parseJsonAs<Record<string, unknown>>(event.payload);
408
-
409
- if (payload?.peerConnectionId && typeof payload.peerConnectionId === 'string' && payload?.trackId && typeof payload.trackId === 'string') {
410
- const observedPeerConnection = this.observedPeerConnections.get(payload.peerConnectionId);
411
- const observedTrack = observedPeerConnection?.observedInboundTracks.get(payload.trackId) ?? observedPeerConnection?.observedOutboundTracks.get(payload.trackId);
412
-
413
- if (observedTrack) {
414
- observedTrack.addedAt = event.timestamp;
415
- } else if (postBuffer) {
416
- postBuffer.push(event);
417
- } else {
418
- logger.warn('Received MEDIA_TRACK_ADDED event without a corresponding observedPeerConnection or observedTrack', event);
419
- }
420
- }
421
- break;
422
- }
423
- case ClientEventTypes.MEDIA_TRACK_REMOVED: {
424
- const payload = parseJsonAs<Record<string, unknown>>(event.payload);
425
-
426
- if (payload?.peerConnectionId && typeof payload.peerConnectionId === 'string' && payload?.trackId && typeof payload.trackId === 'string') {
427
- const observedPeerConnection = this.observedPeerConnections.get(payload.peerConnectionId);
428
- const observedTrack = observedPeerConnection?.observedInboundTracks.get(payload.trackId) ?? observedPeerConnection?.observedOutboundTracks.get(payload.trackId);
429
-
430
- if (observedTrack) {
431
- observedTrack.removedAt = event.timestamp;
432
- } else if (postBuffer) {
433
- postBuffer.push(event);
434
- } else {
435
- logger.warn('Received MEDIA_TRACK_REMOVED event without a corresponding observedPeerConnection or observedTrack', event);
436
- }
437
- }
438
- break;
439
- }
440
- case ClientEventTypes.DATA_CHANNEL_OPEN: {
441
- const payload = parseJsonAs<Record<string, unknown>>(event.payload);
442
-
443
- if (payload?.peerConnectionId && typeof payload.peerConnectionId === 'string' && payload?.dataChannelId && typeof payload.dataChannelId === 'string') {
444
- const observedPeerConnection = this.observedPeerConnections.get(payload.peerConnectionId);
445
- const observedDataChannel = observedPeerConnection?.observedDataChannels.get(payload.dataChannelId);
446
-
447
- if (observedDataChannel) {
448
- observedDataChannel.addedAt = event.timestamp;
449
- } else if (postBuffer) {
450
- postBuffer.push(event);
451
- } else {
452
- logger.warn('Received DATA_CHANNEL_OPENED event without a corresponding observedPeerConnection or observedDataChannel', event);
453
- }
454
- }
455
- break;
456
- }
457
- case ClientEventTypes.DATA_CHANNEL_CLOSED: {
458
- const payload = parseJsonAs<Record<string, unknown>>(event.payload);
459
-
460
- if (payload?.peerConnectionId && typeof payload.peerConnectionId === 'string' && payload?.dataChannelId && typeof payload.dataChannelId === 'string') {
461
- const observedPeerConnection = this.observedPeerConnections.get(payload.peerConnectionId);
462
- const observedDataChannel = observedPeerConnection?.observedDataChannels.get(payload.dataChannelId);
463
-
464
- if (observedDataChannel) {
465
- observedDataChannel.removedAt = event.timestamp;
466
- } else if (postBuffer) {
467
- postBuffer.push(event);
468
- } else {
469
- logger.warn('Received DATA_CHANNEL_CLOSE event without a corresponding observedPeerConnection or observedDataChannel', event);
470
- }
471
- }
472
- break;
473
- }
474
-
475
- }
476
- }
477
-
478
- public injectMetaData(metaData: ClientMetaData) {
479
- if (this.closed) return;
480
-
481
- if (!this._injections.clientMetaItems) this._injections.clientMetaItems = [];
482
-
483
- this._injections.clientMetaItems.push(metaData);
484
- }
485
-
486
- public injectEvent(event: ClientEvent) {
487
- if (this.closed) return;
488
-
489
- if (!this._injections.clientEvents) this._injections.clientEvents = [];
490
-
491
- this._injections.clientEvents.push(event);
492
- }
493
-
494
- public injectIssue(issue: ClientIssue) {
495
- if (this.closed) return;
496
-
497
- if (!this._injections.clientIssues) this._injections.clientIssues = [];
498
-
499
- this._injections.clientIssues.push(issue);
500
- }
501
-
502
- public injectExtensionStat(stat: ExtensionStat) {
503
- if (this.closed) return;
504
-
505
- if (!this._injections.extensionStats) this._injections.extensionStats = [];
506
-
507
- this._injections.extensionStats.push(stat);
508
- }
509
-
510
- public injectAttachment(key: string, value: unknown) {
511
- if (this.closed) return;
512
-
513
- if (!this._injections.attachments) this._injections.attachments = {};
514
-
515
- this._injections.attachments[key] = value;
516
-
517
- }
518
-
519
- public addMetadata(metadata: ClientMetaData) {
520
- if (this.closed) return;
521
-
522
- switch (metadata.type) {
523
- case ClientMetaTypes.BROWSER: {
524
- this.browser = parseJsonAs(metadata.payload);
525
- break;
526
- }
527
- case ClientMetaTypes.ENGINE: {
528
- this.engine = parseJsonAs(metadata.payload);
529
- break;
530
- }
531
- case ClientMetaTypes.PLATFORM: {
532
- this.platform = parseJsonAs(metadata.payload);
533
- break;
534
- }
535
- case ClientMetaTypes.OPERATION_SYSTEM: {
536
- this.operationSystem = parseJsonAs(metadata.payload);
537
- break;
538
- }
539
- }
540
-
541
- this.call.observer.emit('client-metadata', this, metadata);
542
- }
543
-
544
- public addIssue(issue: ClientIssue) {
545
- if (this.closed) return;
546
-
547
- this.emit('issue', issue);
548
- this.call.observer.emit('client-issue', this, issue);
549
- }
550
-
551
- public addExtensionStats(stats: ExtensionStat) {
552
- this.call.observer.emit('client-extension-stats', this, stats);
553
- }
554
-
555
- private _updatePeerConnection(sample: PeerConnectionSample): ObservedPeerConnection | undefined {
556
- let observedPeerConnection = this.observedPeerConnections.get(sample.peerConnectionId);
557
-
558
- if (!observedPeerConnection) {
559
- if (!sample.peerConnectionId) {
560
- return (logger.warn(
561
- `ObservedClient received an invalid PeerConnectionSample (missing peerConnectionId field). ClientId: ${this.clientId}, CallId: ${this.call.callId}`,
562
- sample
563
- ), void 0);
564
- }
565
-
566
- observedPeerConnection = new ObservedPeerConnection(sample.peerConnectionId, this);
567
-
568
- observedPeerConnection.once('close', () => {
569
- this.observedPeerConnections.delete(sample.peerConnectionId);
570
- });
571
- this.observedPeerConnections.set(sample.peerConnectionId, observedPeerConnection);
572
-
573
- this.emit('newpeerconnection', observedPeerConnection);
574
- }
575
-
576
- observedPeerConnection.accept(sample);
577
-
578
- return observedPeerConnection;
579
- }
580
-
581
- public createEventMonitor<CTX = unknown>(ctx?: CTX): ObservedClientEventMonitor<CTX> {
582
- return new ObservedClientEventMonitor<CTX>(this, ctx ?? {} as CTX);
583
- }
584
-
585
- private _mergeInjections(sample: ClientSample): ClientSample {
586
- if (this.closed) return sample;
587
-
588
- if (this._injections.clientEvents) {
589
- if (!sample.clientEvents) sample.clientEvents = [];
590
- sample.clientEvents.push(...this._injections.clientEvents);
591
-
592
- this._injections.clientEvents = undefined;
593
- }
594
-
595
- if (this._injections.clientIssues) {
596
- if (!sample.clientIssues) sample.clientIssues = [];
597
- sample.clientIssues.push(...this._injections.clientIssues);
598
-
599
- this._injections.clientIssues = undefined;
600
- }
601
-
602
- if (this._injections.extensionStats) {
603
- if (!sample.extensionStats) sample.extensionStats = [];
604
- sample.extensionStats.push(...this._injections.extensionStats);
605
-
606
- this._injections.extensionStats = undefined;
607
- }
608
-
609
- if (this._injections.attachments) {
610
- if (!sample.attachments) sample.attachments = {};
611
- Object.assign(sample.attachments, this._injections.attachments);
612
-
613
- this._injections.attachments = undefined;
614
- }
615
-
616
- if (this._injections.clientMetaItems) {
617
- if (!sample.clientMetaItems) sample.clientMetaItems = [];
618
- sample.clientMetaItems.push(...this._injections.clientMetaItems);
619
-
620
- this._injections.clientMetaItems = undefined;
621
- }
622
-
623
- return sample;
624
- }
625
-
626
- // public resetSummaryMetrics() {
627
- // this.totalDataChannelBytesReceived = 0;
628
- // this.totalDataChannelBytesSent = 0;
629
- // this.totalDataChannelMessagesReceived = 0;
630
- // this.totalDataChannelMessagesSent = 0;
631
- // this.totalInboundPacketsLost = 0;
632
- // this.totalInboundPacketsReceived = 0;
633
- // this.totalOutboundPacketsSent = 0;
634
- // this.totalReceivedAudioBytes = 0;
635
- // this.totalReceivedVideoBytes = 0;
636
- // this.totalSentAudioBytes = 0;
637
- // this.totalSentVideoBytes = 0;
638
- // this.totalSentBytes = 0;
639
- // this.totalReceivedBytes = 0;
640
-
641
- // this.totalNumberOfIssues = 0;
642
-
643
- // this.totalScoreSum = 0;
644
- // this.numberOfScoreMeasurements = 0;
645
- // }
646
-
647
- // public createSummary(): ObservedClientSummary {
648
- // return {
649
- // totalRttLt50Measurements: this.totalRttLt50Measurements,
650
- // totalRttLt150Measurements: this.totalRttLt150Measurements,
651
- // totalRttLt300Measurements: this.totalRttLt300Measurements,
652
- // totalRttGtOrEq300Measurements: this.totalRttGtOrEq300Measurements,
653
- // totalDataChannelBytesReceived: this.totalDataChannelBytesReceived,
654
- // totalDataChannelBytesSent: this.totalDataChannelBytesSent,
655
- // totalDataChannelMessagesReceived: this.totalDataChannelMessagesReceived,
656
- // totalDataChannelMessagesSent: this.totalDataChannelMessagesSent,
657
- // totalInboundPacketsLost: this.totalInboundPacketsLost,
658
- // totalInboundPacketsReceived: this.totalInboundPacketsReceived,
659
- // totalOutboundPacketsSent: this.totalOutboundPacketsSent,
660
- // totalReceivedAudioBytes: this.totalReceivedAudioBytes,
661
- // totalReceivedVideoBytes: this.totalReceivedVideoBytes,
662
- // totalSentAudioBytes: this.totalSentAudioBytes,
663
- // totalSentVideoBytes: this.totalSentVideoBytes,
664
- // totalSentBytes: this.totalSentBytes,
665
- // totalReceivedBytes: this.totalReceivedBytes,
666
-
667
- // numberOfIssues: this.totalNumberOfIssues,
668
-
669
- // totalScoreSum: this.totalScoreSum,
670
- // numberOfScoreMeasurements: this.numberOfScoreMeasurements,
671
- // };
672
- // }
673
- }