|
@@ -0,0 +1,334 @@
|
|
1
|
+/* global __filename */
|
|
2
|
+import { getLogger } from 'jitsi-meet-logger';
|
|
3
|
+import { createE2eRttEvent } from '../../service/statistics/AnalyticsEvents';
|
|
4
|
+import * as E2ePingEvents
|
|
5
|
+ from '../../service/e2eping/E2ePingEvents';
|
|
6
|
+import Statistics from '../statistics/statistics';
|
|
7
|
+
|
|
8
|
+const logger = getLogger(__filename);
|
|
9
|
+
|
|
10
|
+/**
|
|
11
|
+ * The 'type' of a message which designates an e2e ping request.
|
|
12
|
+ * @type {string}
|
|
13
|
+ */
|
|
14
|
+const E2E_PING_REQUEST = 'e2e-ping-request';
|
|
15
|
+
|
|
16
|
+/**
|
|
17
|
+ * The 'type' of a message which designates an e2e ping response.
|
|
18
|
+ * @type {string}
|
|
19
|
+ */
|
|
20
|
+const E2E_PING_RESPONSE = 'e2e-ping-response';
|
|
21
|
+
|
|
22
|
+/**
|
|
23
|
+ * Saves e2e ping related state for a single JitsiParticipant.
|
|
24
|
+ */
|
|
25
|
+class ParticipantWrapper {
|
|
26
|
+ /**
|
|
27
|
+ * Creates a ParticipantWrapper
|
|
28
|
+ * @param {JitsiParticipant} participant - The remote participant that this
|
|
29
|
+ * object wraps.
|
|
30
|
+ * @param {E2ePing} e2eping
|
|
31
|
+ */
|
|
32
|
+ constructor(participant, e2eping) {
|
|
33
|
+ // The JitsiParticipant
|
|
34
|
+ this.participant = participant;
|
|
35
|
+
|
|
36
|
+ // The E2ePing
|
|
37
|
+ this.e2eping = e2eping;
|
|
38
|
+
|
|
39
|
+ // Caches the ID
|
|
40
|
+ this.id = participant.getId();
|
|
41
|
+
|
|
42
|
+ // Recently sent requests
|
|
43
|
+ this.requests = {};
|
|
44
|
+
|
|
45
|
+ // The ID of the last sent request. We just increment it for each new
|
|
46
|
+ // request. Start at 1 so we can consider only thruthy values valid.
|
|
47
|
+ this.lastRequestId = 1;
|
|
48
|
+
|
|
49
|
+ this.clearIntervals = this.clearIntervals.bind(this);
|
|
50
|
+ this.sendRequest = this.sendRequest.bind(this);
|
|
51
|
+ this.handleResponse = this.handleResponse.bind(this);
|
|
52
|
+ this.maybeSendAnalytics = this.maybeSendAnalytics.bind(this);
|
|
53
|
+ this.sendAnalytics = this.sendAnalytics.bind(this);
|
|
54
|
+
|
|
55
|
+ // If the data channel was already open (this is likely a participant
|
|
56
|
+ // joining an existing conference) send a request immediately.
|
|
57
|
+ if (e2eping.isDataChannelOpen) {
|
|
58
|
+ this.sendRequest();
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ this.pingInterval = window.setInterval(
|
|
62
|
+ this.sendRequest, e2eping.pingIntervalMs);
|
|
63
|
+ this.analyticsInterval = window.setTimeout(
|
|
64
|
+ this.maybeSendAnalytics, this.e2eping.analyticsIntervalMs);
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ /**
|
|
68
|
+ * Clears the interval which sends pings.
|
|
69
|
+ * @type {*}
|
|
70
|
+ */
|
|
71
|
+ clearIntervals() {
|
|
72
|
+ if (this.pingInterval) {
|
|
73
|
+ window.clearInterval(this.pingInterval);
|
|
74
|
+ }
|
|
75
|
+ if (this.analyticsInterval) {
|
|
76
|
+ window.clearInterval(this.analyticsInterval);
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ /**
|
|
81
|
+ * Sends the next ping request.
|
|
82
|
+ * @type {*}
|
|
83
|
+ */
|
|
84
|
+ sendRequest() {
|
|
85
|
+ const requestId = this.lastRequestId++;
|
|
86
|
+ const requestMessage = {
|
|
87
|
+ type: E2E_PING_REQUEST,
|
|
88
|
+ id: requestId
|
|
89
|
+ };
|
|
90
|
+
|
|
91
|
+ this.e2eping.sendMessage(requestMessage, this.id);
|
|
92
|
+ this.requests[requestId] = {
|
|
93
|
+ id: requestId,
|
|
94
|
+ timeSent: window.performance.now()
|
|
95
|
+ };
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ /**
|
|
99
|
+ * Handles a response from this participant.
|
|
100
|
+ * @type {*}
|
|
101
|
+ */
|
|
102
|
+ handleResponse(response) {
|
|
103
|
+ const request = this.requests[response.id];
|
|
104
|
+
|
|
105
|
+ if (request) {
|
|
106
|
+ request.rtt = window.performance.now() - request.timeSent;
|
|
107
|
+ this.e2eping.eventEmitter.emit(
|
|
108
|
+ E2ePingEvents.E2E_RTT_CHANGED,
|
|
109
|
+ this.participant,
|
|
110
|
+ request.rtt);
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ this.maybeSendAnalytics();
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ /**
|
|
117
|
+ * Goes over the requests, clearing ones which we don't need anymore, and
|
|
118
|
+ * if it finds at least one request with a valid RTT in the last
|
|
119
|
+ * 'analyticsIntervalMs' then sends an analytics event.
|
|
120
|
+ * @type {*}
|
|
121
|
+ */
|
|
122
|
+ maybeSendAnalytics() {
|
|
123
|
+ const now = window.performance.now();
|
|
124
|
+
|
|
125
|
+ // The RTT we'll report is the minimum RTT measured in the last
|
|
126
|
+ // analyticsInterval
|
|
127
|
+ let rtt = Infinity;
|
|
128
|
+ let request, requestId;
|
|
129
|
+
|
|
130
|
+ // It's time to send analytics. Clean up all requests and find the
|
|
131
|
+ for (requestId in this.requests) {
|
|
132
|
+ if (this.requests.hasOwnProperty(requestId)) {
|
|
133
|
+ request = this.requests[requestId];
|
|
134
|
+
|
|
135
|
+ if (request.timeSent < now - this.e2eping.analyticsIntervalMs) {
|
|
136
|
+ // An old request. We don't care about it anymore.
|
|
137
|
+ delete this.requests[requestId];
|
|
138
|
+ } else if (request.rtt) {
|
|
139
|
+ rtt = Math.min(rtt, request.rtt);
|
|
140
|
+ }
|
|
141
|
+ }
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ if (rtt < Infinity) {
|
|
145
|
+ this.sendAnalytics(rtt);
|
|
146
|
+ }
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ /**
|
|
150
|
+ * Sends an analytics event for this participant with the given RTT.
|
|
151
|
+ * @type {*}
|
|
152
|
+ */
|
|
153
|
+ sendAnalytics(rtt) {
|
|
154
|
+ Statistics.sendAnalytics(createE2eRttEvent(
|
|
155
|
+ this.id,
|
|
156
|
+ this.participant.getProperty('region'),
|
|
157
|
+ rtt));
|
|
158
|
+ }
|
|
159
|
+}
|
|
160
|
+
|
|
161
|
+/**
|
|
162
|
+ * Implements end-to-end ping (from one conference participant to another) via
|
|
163
|
+ * the jitsi-videobridge channel (either WebRTC data channel or web socket).
|
|
164
|
+ *
|
|
165
|
+ * TODO: use a broadcast message instead of individual pings to each remote
|
|
166
|
+ * participant.
|
|
167
|
+ *
|
|
168
|
+ * This class:
|
|
169
|
+ * 1. Sends periodic ping requests to all other participants in the
|
|
170
|
+ * conference.
|
|
171
|
+ * 2. Responds to ping requests from other participants.
|
|
172
|
+ * 3. Fires events with the end-to-end RTT to each participant whenever a
|
|
173
|
+ * response is received.
|
|
174
|
+ * 4. Fires analytics events with the end-to-end RTT periodically.
|
|
175
|
+ */
|
|
176
|
+export default class E2ePing {
|
|
177
|
+ /**
|
|
178
|
+ * @param {EventEmitter} eventEmitter - The object to use to emit events.
|
|
179
|
+ * @param {Function} sendMessage - The function to use to send a message.
|
|
180
|
+ * @param {Object} options
|
|
181
|
+ */
|
|
182
|
+ constructor(eventEmitter, options, sendMessage) {
|
|
183
|
+ this.eventEmitter = eventEmitter;
|
|
184
|
+ this.sendMessage = sendMessage;
|
|
185
|
+
|
|
186
|
+ // The interval at which pings will be sent (<= 0 disables sending).
|
|
187
|
+ this.pingIntervalMs = 10000;
|
|
188
|
+
|
|
189
|
+ // The interval at which analytics events will be sent.
|
|
190
|
+ this.analyticsIntervalMs = 60000;
|
|
191
|
+
|
|
192
|
+ // Maps a participant ID to its ParticipantWrapper
|
|
193
|
+ this.participants = {};
|
|
194
|
+
|
|
195
|
+ // Whether the WebRTC channel has been opened or not.
|
|
196
|
+ this.isDataChannelOpen = false;
|
|
197
|
+
|
|
198
|
+ if (options && options.e2eping) {
|
|
199
|
+ if (typeof options.e2eping.pingInterval === 'number') {
|
|
200
|
+ this.pingIntervalMs = options.e2eping.pingInterval;
|
|
201
|
+ }
|
|
202
|
+ if (typeof options.e2eping.analyticsInterval === 'number') {
|
|
203
|
+ this.analyticsIntervalMs = options.e2eping.analyticsInterval;
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ // We want to report at most once a ping interval.
|
|
207
|
+ if (this.analyticsIntervalMs > 0 && this.analyticsIntervalMs
|
|
208
|
+ < this.pingIntervalMs) {
|
|
209
|
+ this.analyticsIntervalMs = this.pingIntervalMs;
|
|
210
|
+ }
|
|
211
|
+ }
|
|
212
|
+ logger.info(
|
|
213
|
+ `Initializing e2e ping; pingInterval=${
|
|
214
|
+ this.pingIntervalMs}, analyticsInterval=${
|
|
215
|
+ this.analyticsIntervalMs}.`);
|
|
216
|
+ }
|
|
217
|
+
|
|
218
|
+ /**
|
|
219
|
+ * Notifies this instance that the communications channel has been opened
|
|
220
|
+ * and it can now send messages via sendMessage.
|
|
221
|
+ */
|
|
222
|
+ dataChannelOpened() {
|
|
223
|
+ this.isDataChannelOpen = true;
|
|
224
|
+
|
|
225
|
+ // We don't want to wait the whole interval before sending the first
|
|
226
|
+ // request, but we can't send it immediately after the participant joins
|
|
227
|
+ // either, because our data channel might not have initialized.
|
|
228
|
+ // So once the data channel initializes, send requests to everyone.
|
|
229
|
+ // Wait an additional 200ms to give a chance to the remote side (if it
|
|
230
|
+ // also just connected as is the case for the first 2 participants in a
|
|
231
|
+ // conference) to open its data channel.
|
|
232
|
+ for (const id in this.participants) {
|
|
233
|
+ if (this.participants.hasOwnProperty(id)) {
|
|
234
|
+ const participantWrapper = this.participants[id];
|
|
235
|
+
|
|
236
|
+ window.setTimeout(participantWrapper.sendRequest, 200);
|
|
237
|
+ }
|
|
238
|
+ }
|
|
239
|
+ }
|
|
240
|
+
|
|
241
|
+ /**
|
|
242
|
+ * Handles a message that was received.
|
|
243
|
+ *
|
|
244
|
+ * @param participant - The message sender.
|
|
245
|
+ * @param payload - The payload of the message.
|
|
246
|
+ */
|
|
247
|
+ messageReceived(participant, payload) {
|
|
248
|
+ // Listen to E2E PING requests and responses from other participants
|
|
249
|
+ // in the conference.
|
|
250
|
+ if (payload.type === E2E_PING_REQUEST) {
|
|
251
|
+ this.handleRequest(participant.getId(), payload);
|
|
252
|
+ } else if (payload.type === E2E_PING_RESPONSE) {
|
|
253
|
+ this.handleResponse(participant.getId(), payload);
|
|
254
|
+ }
|
|
255
|
+ }
|
|
256
|
+
|
|
257
|
+ /**
|
|
258
|
+ * Handles a participant joining the conference. Starts to send ping
|
|
259
|
+ * requests to the participant.
|
|
260
|
+ *
|
|
261
|
+ * @param {JitsiParticipant} participant - The participant that joined.
|
|
262
|
+ */
|
|
263
|
+ participantJoined(participant) {
|
|
264
|
+ const id = participant.getId();
|
|
265
|
+
|
|
266
|
+ if (this.pingIntervalMs <= 0) {
|
|
267
|
+ return;
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ if (this.participants[id]) {
|
|
271
|
+ logger.info(
|
|
272
|
+ `Participant wrapper already exists for ${id}. Clearing.`);
|
|
273
|
+ this.participants[id].clearIntervals();
|
|
274
|
+ delete this.participants[id];
|
|
275
|
+ }
|
|
276
|
+
|
|
277
|
+ this.participants[id] = new ParticipantWrapper(participant, this);
|
|
278
|
+ }
|
|
279
|
+
|
|
280
|
+ /**
|
|
281
|
+ * Handles a participant leaving the conference. Stops sending requests.
|
|
282
|
+ *
|
|
283
|
+ * @param {JitsiParticipant} participant - The participant that left.
|
|
284
|
+ */
|
|
285
|
+ participantLeft(participant) {
|
|
286
|
+ const id = participant.getId();
|
|
287
|
+
|
|
288
|
+ if (this.pingIntervalMs <= 0) {
|
|
289
|
+ return;
|
|
290
|
+ }
|
|
291
|
+
|
|
292
|
+ if (this.participants[id]) {
|
|
293
|
+ this.participants[id].clearIntervals();
|
|
294
|
+ delete this.participants[id];
|
|
295
|
+ }
|
|
296
|
+ }
|
|
297
|
+
|
|
298
|
+ /**
|
|
299
|
+ * Handles a ping request coming from another participant.
|
|
300
|
+ *
|
|
301
|
+ * @param {string} participantId - The ID of the participant who sent the
|
|
302
|
+ * request.
|
|
303
|
+ * @param {Object} request - The request.
|
|
304
|
+ */
|
|
305
|
+ handleRequest(participantId, request) {
|
|
306
|
+ // If it's a valid request, just send a response.
|
|
307
|
+ if (request && request.id) {
|
|
308
|
+ const response = {
|
|
309
|
+ type: E2E_PING_RESPONSE,
|
|
310
|
+ id: request.id
|
|
311
|
+ };
|
|
312
|
+
|
|
313
|
+ this.sendMessage(response, participantId);
|
|
314
|
+ } else {
|
|
315
|
+ logger.info(
|
|
316
|
+ `Received an invalid e2e ping request from ${participantId}.`);
|
|
317
|
+ }
|
|
318
|
+ }
|
|
319
|
+
|
|
320
|
+ /**
|
|
321
|
+ * Handles a ping response coming from another participant
|
|
322
|
+ * @param {string} participantId - The ID of the participant who sent the
|
|
323
|
+ * response.
|
|
324
|
+ * @param {Object} response - The response.
|
|
325
|
+ */
|
|
326
|
+ handleResponse(participantId, response) {
|
|
327
|
+ const participantWrapper = this.participants[participantId];
|
|
328
|
+
|
|
329
|
+ if (participantWrapper) {
|
|
330
|
+ participantWrapper.handleResponse(response);
|
|
331
|
+ }
|
|
332
|
+ }
|
|
333
|
+}
|
|
334
|
+
|