-
Notifications
You must be signed in to change notification settings - Fork 2
/
StatsStorage.ts
300 lines (255 loc) · 10.2 KB
/
StatsStorage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { StatsEntry } from "../utils/StatsVisitor";
import { ContributingSourceEntry, CodecEntry, InboundRtpEntry, OutboundRtpEntry, RemoteInboundRtpEntry, RemoteOutboundRtpEntry, DataChannelEntry, TransceiverEntry, SenderEntry, ReceiverEntry, TransportEntry, SctpTransportEntry, IceCandidatePairEntry, LocalCandidateEntry, RemoteCandidateEntry, CertificateEntry, IceServerEntry, MediaSourceEntry, PeerConnectionEntry } from "./StatsEntryInterfaces";
import { PeerConnectionEntryImpl } from "./PeerConnectionEntryImpl";
import { createLogger } from "../utils/logger";
const logger = createLogger("StatsStorage");
/**
* Interface to read the collected stats.
*/
export interface StatsReader {
/**
* Gives an iterator to read the collected peer connection stats and navigate to its relations.
*/
peerConnections(): Generator<PeerConnectionEntry, void, undefined>;
/**
* Gives an iterator to read the collected codecs and navigate to its relations.
*/
codecs(): Generator<CodecEntry, void, undefined>;
/**
* Gives an iterator to read the collected inbound-rtp stats and navigate to its relations.
*/
inboundRtps(): Generator<InboundRtpEntry, void, undefined>;
/**
* Gives an iterator to read the collected outbound-rtp stats and navigate to its relations.
*/
outboundRtps(): Generator<OutboundRtpEntry, void, undefined>;
/**
* Gives an iterator to read the collected remote-inbound-rtp stats and navigate to its relations.
*/
remoteInboundRtps(): Generator<RemoteInboundRtpEntry, void, undefined>;
/**
* Gives an iterator to read the collected remote-outbound-rtp stats and navigate to its relations.
*/
remoteOutboundRtps(): Generator<RemoteOutboundRtpEntry, void, undefined>;
/**
* Gives an iterator to read the collected media source stats and navigate to its relations.
*/
mediaSources(): Generator<MediaSourceEntry, void, undefined>;
/**
* Gives an iterator to read the collected contributing sources and navigate to its relations.
*/
contributingSources(): Generator<ContributingSourceEntry, void, undefined>;
/**
* Gives an iterator to read the collected data channel stats and navigate to its relations.
*/
dataChannels(): Generator<DataChannelEntry, void, undefined>;
/**
* Gives an iterator to read the collected transceiver stats and navigate to its relations.
*/
transceivers(): Generator<TransceiverEntry, void, undefined>;
/**
* Gives an iterator to read the collected media source stats and navigate to its relations.
*/
senders(): Generator<SenderEntry, void, undefined>;
/**
* Gives an iterator to read the collected receiver stats and navigate to its relations.
*/
receivers(): Generator<ReceiverEntry, void, undefined>;
/**
* Gives an iterator to read the collected transport stats and navigate to its relations.
*/
transports(): Generator<TransportEntry, void, undefined>;
/**
* Gives an iterator to read the SCTP transport stats and navigate to its relations.
*/
sctpTransports(): Generator<SctpTransportEntry, void, undefined>;
/**
* Gives an iterator to read the collected ICE candidate pair stats and navigate to its relations.
*/
iceCandidatePairs(): Generator<IceCandidatePairEntry, void, undefined>;
/**
* Gives an iterator to read the collected local ICE candidate stats and navigate to its relations.
*/
localCandidates(): Generator<LocalCandidateEntry, void, undefined>;
/**
* Gives an iterator to read the collected remote ICE candidate stats and navigate to its relations.
*/
remoteCandidates(): Generator<RemoteCandidateEntry, void, undefined>;
/**
* Gives an iterator to read the collected certificate stats and navigate to its relations.
*/
certificates(): Generator<CertificateEntry, void, undefined>;
/**
* Gives an iterator to read the collected ICE server stats and navigate to its relations.
*/
iceServers(): Generator<IceServerEntry, void, undefined>;
}
export interface StatsWriter {
register(collectorId: string, label?: string): void;
unregister(collectorId: string): void;
accept(collectorId: string, statsEntry: StatsEntry): void;
}
export class StatsStorage implements StatsReader, StatsWriter {
private _peerConnections: Map<string, PeerConnectionEntryImpl> = new Map();
public accept(collectorId: string, statsEntry: StatsEntry): void {
const pcEntry = this._peerConnections.get(collectorId);
if (!pcEntry) {
logger.warn(`PeerConnectionEntry is not registered for collectorId ${collectorId}`);
return;
}
pcEntry.update(statsEntry);
}
public trim(expirationThresholdInMs: number) {
for (const pcEntry of this._peerConnections.values()) {
pcEntry.trim(expirationThresholdInMs);
}
}
public clear() {
for (const pcEntry of this._peerConnections.values()) {
pcEntry.clear();
}
}
public register(collectorId: string, collectorLabel?: string): void {
const pcEntry = PeerConnectionEntryImpl.create({
collectorId,
collectorLabel,
});
this._peerConnections.set(collectorId, pcEntry);
}
public unregister(collectorId: string): void {
if (!this._peerConnections.delete(collectorId)) {
logger.warn(`Peer Connection Entry does not exist for collectorId ${collectorId}`);
}
}
public *peerConnections(): Generator<PeerConnectionEntryImpl, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
yield pcEntry;
}
}
public *receivers(): Generator<ReceiverEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.receivers()) {
yield entry;
}
}
}
public *mediaSources(): Generator<MediaSourceEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.mediaSources()) {
yield entry;
}
}
}
public *outboundRtps(): Generator<OutboundRtpEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.outboundRtps()) {
yield entry;
}
}
}
public *remoteInboundRtps(): Generator<RemoteInboundRtpEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.remoteInboundRtps()) {
yield entry;
}
}
}
public *remoteOutboundRtps(): Generator<RemoteOutboundRtpEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.remoteOutboundRtps()) {
yield entry;
}
}
}
public *contributingSources(): Generator<ContributingSourceEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.contributingSources()) {
yield entry;
}
}
}
public *dataChannels(): Generator<DataChannelEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.dataChannels()) {
yield entry;
}
}
}
public *transceivers(): Generator<TransceiverEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.transceivers()) {
yield entry;
}
}
}
public *senders(): Generator<SenderEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.senders()) {
yield entry;
}
}
}
public *transports(): Generator<TransportEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.transports()) {
yield entry;
}
}
}
public *sctpTransports(): Generator<SctpTransportEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.sctpTransports()) {
yield entry;
}
}
}
public *iceCandidatePairs(): Generator<IceCandidatePairEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.iceCandidatePairs()) {
yield entry;
}
}
}
public *localCandidates(): Generator<LocalCandidateEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.localCandidates()) {
yield entry;
}
}
}
public *remoteCandidates(): Generator<RemoteCandidateEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.remoteCandidates()) {
yield entry;
}
}
}
public *certificates(): Generator<CertificateEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.certificates()) {
yield entry;
}
}
}
public *iceServers(): Generator<IceServerEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.iceServers()) {
yield entry;
}
}
}
public *codecs(): Generator<CodecEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.codecs()) {
yield entry;
}
}
}
public *inboundRtps(): Generator<InboundRtpEntry, void, undefined> {
for (const pcEntry of this._peerConnections.values()) {
for (const entry of pcEntry.inboundRtps()) {
yield entry;
}
}
}
}