|
@@ -1,356 +0,0 @@
|
1
|
|
-import browser from '../browser';
|
2
|
|
-import { createRttByRegionEvent }
|
3
|
|
- from '../../service/statistics/AnalyticsEvents';
|
4
|
|
-import { getLogger } from 'jitsi-meet-logger';
|
5
|
|
-import RTCUtils from '../RTC/RTCUtils';
|
6
|
|
-import Statistics from '../statistics/statistics';
|
7
|
|
-
|
8
|
|
-const logger = getLogger(__filename);
|
9
|
|
-
|
10
|
|
-/**
|
11
|
|
- * The options to pass to createOffer (we need to offer to receive *something*
|
12
|
|
- * for the PC to gather candidates.
|
13
|
|
- */
|
14
|
|
-const offerOptions = {
|
15
|
|
- offerToReceiveAudio: 1,
|
16
|
|
- offerToReceiveVideo: 0
|
17
|
|
-};
|
18
|
|
-
|
19
|
|
-
|
20
|
|
-/**
|
21
|
|
- * The interval at which the webrtc engine sends STUN keep alive requests.
|
22
|
|
- * @type {number}
|
23
|
|
- */
|
24
|
|
-const stunKeepAliveIntervalMs = 10000;
|
25
|
|
-
|
26
|
|
-/**
|
27
|
|
- * Wraps a PeerConnection with one specific STUN server and measures the RTT
|
28
|
|
- * to the STUN server.
|
29
|
|
- */
|
30
|
|
-class PCMonitor {
|
31
|
|
- /* eslint-disable max-params */
|
32
|
|
- /**
|
33
|
|
- *
|
34
|
|
- * @param {String} region - The region of the STUN server.
|
35
|
|
- * @param {String} address - The address of the STUN server.
|
36
|
|
- * @param {number} getStatsIntervalMs how often to call getStats.
|
37
|
|
- * @param {number} delay the delay after which the PeerConnection will be
|
38
|
|
- * started (that is, createOffer and setLocalDescription will be invoked).
|
39
|
|
- *
|
40
|
|
- */
|
41
|
|
- constructor(region, address, getStatsIntervalMs, delay) {
|
42
|
|
- /* eslint-disable max-params */
|
43
|
|
- this.region = region;
|
44
|
|
- this.getStatsIntervalMs = getStatsIntervalMs;
|
45
|
|
- this.getStatsInterval = null;
|
46
|
|
-
|
47
|
|
- // What we consider the current RTT. It is Math.min(this.rtts).
|
48
|
|
- this.rtt = Infinity;
|
49
|
|
-
|
50
|
|
- // The RTT measurements we've made from the latest getStats() calls.
|
51
|
|
- this.rtts = [];
|
52
|
|
-
|
53
|
|
- const iceServers = [ { 'url': `stun:${address}` } ];
|
54
|
|
-
|
55
|
|
- this.pc = new RTCUtils.RTCPeerConnectionType(
|
56
|
|
- {
|
57
|
|
- 'iceServers': iceServers
|
58
|
|
- });
|
59
|
|
-
|
60
|
|
- // Maps a key consisting of the IP address, port and priority of a
|
61
|
|
- // candidate to some state related to it. If we have more than one
|
62
|
|
- // network interface we will might multiple srflx candidates and this
|
63
|
|
- // helps to distinguish between then.
|
64
|
|
- this.candidates = {};
|
65
|
|
-
|
66
|
|
- this.stopped = false;
|
67
|
|
-
|
68
|
|
- this.start = this.start.bind(this);
|
69
|
|
- this.stop = this.stop.bind(this);
|
70
|
|
- this.startStatsInterval = this.startStatsInterval.bind(this);
|
71
|
|
- this.handleCandidateRtt = this.handleCandidateRtt.bind(this);
|
72
|
|
-
|
73
|
|
- window.setTimeout(this.start, delay);
|
74
|
|
- }
|
75
|
|
-
|
76
|
|
- /**
|
77
|
|
- * Starts this PCMonitor. That is, invokes createOffer and
|
78
|
|
- * setLocalDescription on the PeerConnection and starts an interval which
|
79
|
|
- * calls getStats.
|
80
|
|
- */
|
81
|
|
- start() {
|
82
|
|
- if (this.stopped) {
|
83
|
|
- return;
|
84
|
|
- }
|
85
|
|
-
|
86
|
|
- this.pc.createOffer(offerOptions).then(offer => {
|
87
|
|
- this.pc.setLocalDescription(
|
88
|
|
- offer,
|
89
|
|
- () => {
|
90
|
|
- logger.info(
|
91
|
|
- `setLocalDescription success for ${this.region}`);
|
92
|
|
- this.startStatsInterval();
|
93
|
|
- },
|
94
|
|
- error => {
|
95
|
|
- logger.warn(
|
96
|
|
- `setLocalDescription failed for ${this.region}: ${
|
97
|
|
- error}`);
|
98
|
|
- }
|
99
|
|
- );
|
100
|
|
- });
|
101
|
|
- }
|
102
|
|
-
|
103
|
|
- /**
|
104
|
|
- * Starts an interval which invokes getStats on the PeerConnection and
|
105
|
|
- * measures the RTTs for the different candidates.
|
106
|
|
- */
|
107
|
|
- startStatsInterval() {
|
108
|
|
- this.getStatsInterval = window.setInterval(
|
109
|
|
- () => {
|
110
|
|
- // Note that the data that we use to measure the RTT is only
|
111
|
|
- // available in the legacy (callback based) getStats API.
|
112
|
|
- this.pc.getStats(stats => {
|
113
|
|
- const results = stats.result();
|
114
|
|
-
|
115
|
|
- for (let i = 0; i < results.length; ++i) {
|
116
|
|
- const res = results[i];
|
117
|
|
- const rttTotal
|
118
|
|
- = Number(res.stat('stunKeepaliveRttTotal'));
|
119
|
|
-
|
120
|
|
- // We recognize the results that we care for (local
|
121
|
|
- // candidates of type srflx) by the existance of the
|
122
|
|
- // stunKeepaliveRttTotal stat.
|
123
|
|
- if (rttTotal > 0) {
|
124
|
|
- const candidateKey
|
125
|
|
- = `${res.stat('ipAddress')}_${
|
126
|
|
- res.stat('portNumber')}_${
|
127
|
|
- res.stat('priority')}`;
|
128
|
|
-
|
129
|
|
- this.handleCandidateRtt(
|
130
|
|
- candidateKey,
|
131
|
|
- rttTotal,
|
132
|
|
- Number(
|
133
|
|
- res.stat('stunKeepaliveResponsesReceived')),
|
134
|
|
- Number(
|
135
|
|
- res.stat('stunKeepaliveRequestsSent')));
|
136
|
|
- }
|
137
|
|
- }
|
138
|
|
-
|
139
|
|
- // After we've measured the RTT for all candidates we,
|
140
|
|
- // update the state of the PC with the shortest one.
|
141
|
|
- let rtt = Infinity;
|
142
|
|
-
|
143
|
|
- for (const key in this.candidates) {
|
144
|
|
- if (this.candidates.hasOwnProperty(key)
|
145
|
|
- && this.candidates[key].rtt > 0) {
|
146
|
|
- rtt = Math.min(rtt, this.candidates[key].rtt);
|
147
|
|
- }
|
148
|
|
- }
|
149
|
|
-
|
150
|
|
- // We keep the last 6 measured RTTs and choose the shortest
|
151
|
|
- // one to export to analytics. This is because we often see
|
152
|
|
- // failures get a real measurement which end up as Infinity.
|
153
|
|
- this.rtts.push(rtt);
|
154
|
|
- if (this.rtts.length > 6) {
|
155
|
|
- this.rtts = this.rtts.splice(1, 7);
|
156
|
|
- }
|
157
|
|
- this.rtt = Math.min(...this.rtts);
|
158
|
|
- });
|
159
|
|
- },
|
160
|
|
- this.getStatsIntervalMs
|
161
|
|
- );
|
162
|
|
- }
|
163
|
|
-
|
164
|
|
- /* eslint-disable max-params */
|
165
|
|
- /**
|
166
|
|
- * Updates the RTT for a candidate identified by "key" based on the values
|
167
|
|
- * from getStats() and the previously saved state (i.e. old values).
|
168
|
|
- *
|
169
|
|
- * @param {String} key the ID for the candidate
|
170
|
|
- * @param {number} rttTotal the value of the 'stunKeepaliveRttTotal' just
|
171
|
|
- * measured.
|
172
|
|
- * @param {number} responsesReceived the value of the
|
173
|
|
- * 'stunKeepaliveResponsesReceived' stat just measured.
|
174
|
|
- * @param {number} requestsSent the value of the 'stunKeepaliveRequestsSent'
|
175
|
|
- * stat just measured.
|
176
|
|
- */
|
177
|
|
- handleCandidateRtt(key, rttTotal, responsesReceived, requestsSent) {
|
178
|
|
- /* eslist-enable max-params */
|
179
|
|
- if (!this.candidates[key]) {
|
180
|
|
- this.candidates[key] = {
|
181
|
|
- rttTotal: 0,
|
182
|
|
- responsesReceived: 0,
|
183
|
|
- requestsSent: 0,
|
184
|
|
- rtt: NaN
|
185
|
|
- };
|
186
|
|
- }
|
187
|
|
-
|
188
|
|
- const rttTotalDiff = rttTotal - this.candidates[key].rttTotal;
|
189
|
|
- const responsesReceivedDiff
|
190
|
|
- = responsesReceived - this.candidates[key].responsesReceived;
|
191
|
|
-
|
192
|
|
- // We observe that when the difference between the number of requests
|
193
|
|
- // and responses has grown (i.q. when the value below is positive), the
|
194
|
|
- // the RTT measurements are incorrect (too low). For this reason we
|
195
|
|
- // ignore these measurement (setting rtt=NaN), but update our state.
|
196
|
|
- const requestsResponsesDiff
|
197
|
|
- = (requestsSent - responsesReceived)
|
198
|
|
- - (this.candidates[key].requestsSent
|
199
|
|
- - this.candidates[key].responsesReceived);
|
200
|
|
- let rtt = NaN;
|
201
|
|
-
|
202
|
|
- if (responsesReceivedDiff > 0 && requestsResponsesDiff === 0) {
|
203
|
|
- rtt = rttTotalDiff / responsesReceivedDiff;
|
204
|
|
- }
|
205
|
|
-
|
206
|
|
- this.candidates[key].rttTotal = rttTotal;
|
207
|
|
- this.candidates[key].responsesReceived = responsesReceived;
|
208
|
|
- this.candidates[key].requestsSent = requestsSent;
|
209
|
|
- this.candidates[key].rtt = rtt;
|
210
|
|
- }
|
211
|
|
-
|
212
|
|
-
|
213
|
|
- /**
|
214
|
|
- * Stops this PCMonitor, clearing its intervals and stopping the
|
215
|
|
- * PeerConnection.
|
216
|
|
- */
|
217
|
|
- stop() {
|
218
|
|
- if (this.getStatsInterval) {
|
219
|
|
- window.clearInterval(this.getStatsInterval);
|
220
|
|
- }
|
221
|
|
-
|
222
|
|
- this.pc.close();
|
223
|
|
-
|
224
|
|
- this.stopped = true;
|
225
|
|
- }
|
226
|
|
-}
|
227
|
|
-
|
228
|
|
-/**
|
229
|
|
- * A class which monitors the round-trip time (RTT) to a set of STUN servers.
|
230
|
|
- * The measured RTTs are sent as analytics events. It uses a separate
|
231
|
|
- * PeerConnection (represented as a PCMonitor) for each STUN server.
|
232
|
|
- */
|
233
|
|
-export default class RttMonitor {
|
234
|
|
- /**
|
235
|
|
- * Initializes a new RttMonitor.
|
236
|
|
- * @param {Object} config the object holding the configuration.
|
237
|
|
- */
|
238
|
|
- constructor(config) {
|
239
|
|
- if (!config || !config.enabled
|
240
|
|
- || !browser.supportsLocalCandidateRttStatistics()) {
|
241
|
|
- return;
|
242
|
|
- }
|
243
|
|
-
|
244
|
|
- // Maps a region to the PCMonitor instance for that region.
|
245
|
|
- this.pcMonitors = {};
|
246
|
|
-
|
247
|
|
- this.startPCMonitors = this.startPCMonitors.bind(this);
|
248
|
|
- this.sendAnalytics = this.sendAnalytics.bind(this);
|
249
|
|
- this.stop = this.stop.bind(this);
|
250
|
|
-
|
251
|
|
- this.analyticsInterval = null;
|
252
|
|
- this.stopped = false;
|
253
|
|
-
|
254
|
|
- const initialDelay = config.initialDelay || 60000;
|
255
|
|
-
|
256
|
|
-
|
257
|
|
- logger.info(
|
258
|
|
- `Starting RTT monitor with an initial delay of ${initialDelay}`);
|
259
|
|
-
|
260
|
|
-
|
261
|
|
- window.setTimeout(
|
262
|
|
- () => this.startPCMonitors(config),
|
263
|
|
- initialDelay);
|
264
|
|
- }
|
265
|
|
-
|
266
|
|
- /**
|
267
|
|
- * Starts the PCMonitors according to the configuration.
|
268
|
|
- */
|
269
|
|
- startPCMonitors(config) {
|
270
|
|
- if (!config.stunServers) {
|
271
|
|
- logger.warn('No stun servers configured.');
|
272
|
|
-
|
273
|
|
- return;
|
274
|
|
- }
|
275
|
|
-
|
276
|
|
- if (this.stopped) {
|
277
|
|
- return;
|
278
|
|
- }
|
279
|
|
-
|
280
|
|
- const getStatsIntervalMs
|
281
|
|
- = config.getStatsInterval || stunKeepAliveIntervalMs;
|
282
|
|
- const analyticsIntervalMs
|
283
|
|
- = config.analyticsInterval || getStatsIntervalMs;
|
284
|
|
- const count = Object.keys(config.stunServers).length;
|
285
|
|
- const offset = getStatsIntervalMs / count;
|
286
|
|
-
|
287
|
|
- // We delay the initialization of each PC so that they are uniformly
|
288
|
|
- // distributed across the getStatsIntervalMs.
|
289
|
|
- let i = 0;
|
290
|
|
-
|
291
|
|
- for (const region in config.stunServers) {
|
292
|
|
- if (config.stunServers.hasOwnProperty(region)) {
|
293
|
|
- const address = config.stunServers[region];
|
294
|
|
-
|
295
|
|
- this.pcMonitors[region]
|
296
|
|
- = new PCMonitor(
|
297
|
|
- region,
|
298
|
|
- address,
|
299
|
|
- getStatsIntervalMs,
|
300
|
|
- offset * i);
|
301
|
|
- i++;
|
302
|
|
- }
|
303
|
|
- }
|
304
|
|
-
|
305
|
|
- window.setTimeout(
|
306
|
|
- () => {
|
307
|
|
- if (!this.stopped) {
|
308
|
|
- this.analyticsInterval
|
309
|
|
- = window.setInterval(
|
310
|
|
- this.sendAnalytics, analyticsIntervalMs);
|
311
|
|
- }
|
312
|
|
- },
|
313
|
|
- 1000);
|
314
|
|
- }
|
315
|
|
-
|
316
|
|
- /**
|
317
|
|
- * Sends an analytics event with the measured RTT to each region/STUN
|
318
|
|
- * server.
|
319
|
|
- */
|
320
|
|
- sendAnalytics() {
|
321
|
|
- const rtts = {};
|
322
|
|
-
|
323
|
|
- for (const region in this.pcMonitors) {
|
324
|
|
- if (this.pcMonitors.hasOwnProperty(region)) {
|
325
|
|
- const rtt = this.pcMonitors[region].rtt;
|
326
|
|
-
|
327
|
|
- if (!isNaN(rtt) && rtt !== Infinity) {
|
328
|
|
- rtts[region.replace('-', '_')] = rtt;
|
329
|
|
- }
|
330
|
|
- }
|
331
|
|
- }
|
332
|
|
-
|
333
|
|
- if (rtts) {
|
334
|
|
- Statistics.sendAnalytics(createRttByRegionEvent(rtts));
|
335
|
|
- }
|
336
|
|
- }
|
337
|
|
-
|
338
|
|
- /**
|
339
|
|
- * Stops this RttMonitor, clearing all intervals and closing all
|
340
|
|
- * PeerConnections.
|
341
|
|
- */
|
342
|
|
- stop() {
|
343
|
|
- logger.info('Stopping RttMonitor.');
|
344
|
|
- this.stopped = true;
|
345
|
|
- for (const region in this.pcMonitors) {
|
346
|
|
- if (this.pcMonitors.hasOwnProperty(region)) {
|
347
|
|
- this.pcMonitors[region].stop();
|
348
|
|
- }
|
349
|
|
- }
|
350
|
|
- this.pcMonitors = {};
|
351
|
|
-
|
352
|
|
- if (this.analyticsInterval) {
|
353
|
|
- window.clearInterval(this.analyticsInterval);
|
354
|
|
- }
|
355
|
|
- }
|
356
|
|
-}
|