|
@@ -0,0 +1,388 @@
|
|
1
|
+import { getLogger } from 'jitsi-meet-logger';
|
|
2
|
+
|
|
3
|
+import RTC from '../RTC/RTC';
|
|
4
|
+import RTCEvents from '../../service/RTC/RTCEvents';
|
|
5
|
+import XMPPEvents from '../../service/xmpp/XMPPEvents';
|
|
6
|
+
|
|
7
|
+import JingleSessionPC from '../xmpp/JingleSessionPC';
|
|
8
|
+import { DEFAULT_STUN_SERVERS } from '../xmpp/xmpp';
|
|
9
|
+
|
|
10
|
+import { ACTIONS } from './constants';
|
|
11
|
+
|
|
12
|
+const logger = getLogger(__filename);
|
|
13
|
+
|
|
14
|
+/**
|
|
15
|
+ * An adapter around {@code JingleSessionPC} so its logic can be re-used without
|
|
16
|
+ * an XMPP connection. It is being re-used for consistency with the rest of the
|
|
17
|
+ * codebase and to leverage existing peer connection event handling. Also
|
|
18
|
+ * this class provides a facade to hide most of the API for
|
|
19
|
+ * {@code JingleSessionPC}.
|
|
20
|
+ */
|
|
21
|
+export default class ProxyConnectionPC {
|
|
22
|
+ /**
|
|
23
|
+ * Initializes a new {@code ProxyConnectionPC} instance.
|
|
24
|
+ *
|
|
25
|
+ * @param {Object} options - Values to initialize the instance with.
|
|
26
|
+ * @param {Object} [options.iceConfig] - The {@code RTCConfiguration} to use
|
|
27
|
+ * for the peer connection.
|
|
28
|
+ * @param {boolean} [options.isInitiator] - If true, the local client should
|
|
29
|
+ * send offers. If false, the local client should send answers. Defaults to
|
|
30
|
+ * false.
|
|
31
|
+ * @param {Function} options.onRemoteStream - Callback to invoke when a
|
|
32
|
+ * remote media stream has been received through the peer connection.
|
|
33
|
+ * @param {string} options.peerJid - The jid of the remote client with which
|
|
34
|
+ * the peer connection is being establish and which should receive direct
|
|
35
|
+ * messages regarding peer connection updates.
|
|
36
|
+ * @param {boolean} [options.receiveVideo] - Whether or not the peer
|
|
37
|
+ * connection should accept incoming video streams. Defaults to false.
|
|
38
|
+ * @param {Function} options.onSendMessage - Callback to invoke when a
|
|
39
|
+ * message has to be sent (signaled) out.
|
|
40
|
+ */
|
|
41
|
+ constructor(options = {}) {
|
|
42
|
+ this._options = {
|
|
43
|
+ iceConfig: {},
|
|
44
|
+ isInitiator: false,
|
|
45
|
+ receiveAudio: false,
|
|
46
|
+ receiveVideo: false,
|
|
47
|
+ ...options
|
|
48
|
+ };
|
|
49
|
+
|
|
50
|
+ /**
|
|
51
|
+ * Instances of {@code JitsiTrack} associated with this instance of
|
|
52
|
+ * {@code ProxyConnectionPC}.
|
|
53
|
+ *
|
|
54
|
+ * @type {Array<JitsiTrack>}
|
|
55
|
+ */
|
|
56
|
+ this._tracks = [];
|
|
57
|
+
|
|
58
|
+ /**
|
|
59
|
+ * The active instance of {@code JingleSessionPC}.
|
|
60
|
+ *
|
|
61
|
+ * @type {JingleSessionPC|null}
|
|
62
|
+ */
|
|
63
|
+ this._peerConnection = null;
|
|
64
|
+
|
|
65
|
+ // Bind event handlers so they are only bound once for every instance.
|
|
66
|
+ this._onError = this._onError.bind(this);
|
|
67
|
+ this._onRemoteStream = this._onRemoteStream.bind(this);
|
|
68
|
+ this._onSendMessage = this._onSendMessage.bind(this);
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ /**
|
|
72
|
+ * Returns the jid of the remote peer with which this peer connection should
|
|
73
|
+ * be established with.
|
|
74
|
+ *
|
|
75
|
+ * @returns {string}
|
|
76
|
+ */
|
|
77
|
+ getPeerJid() {
|
|
78
|
+ return this._options.peerJid;
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ /**
|
|
82
|
+ * Updates the peer connection based on the passed in jingle.
|
|
83
|
+ *
|
|
84
|
+ * @param {Object} $jingle - An XML jingle element, wrapped in query,
|
|
85
|
+ * describing how the peer connection should be updated.
|
|
86
|
+ * @returns {void}
|
|
87
|
+ */
|
|
88
|
+ processMessage($jingle) {
|
|
89
|
+ switch ($jingle.attr('action')) {
|
|
90
|
+ case ACTIONS.ACCEPT:
|
|
91
|
+ this._onSessionAccept($jingle);
|
|
92
|
+ break;
|
|
93
|
+
|
|
94
|
+ case ACTIONS.INITIATE:
|
|
95
|
+ this._onSessionInitiate($jingle);
|
|
96
|
+ break;
|
|
97
|
+
|
|
98
|
+ case ACTIONS.TERMINATE:
|
|
99
|
+ this._onSessionTerminate($jingle);
|
|
100
|
+ break;
|
|
101
|
+
|
|
102
|
+ case ACTIONS.TRANSPORT_INFO:
|
|
103
|
+ this._onTransportInfo($jingle);
|
|
104
|
+ break;
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ /**
|
|
109
|
+ * Instantiates a peer connection and starts the offer/answer cycle to
|
|
110
|
+ * establish a connection with a remote peer.
|
|
111
|
+ *
|
|
112
|
+ * @param {Array<JitsiLocalTrack>} localTracks - Initial local tracks to add
|
|
113
|
+ * to add to the peer connection.
|
|
114
|
+ * @returns {void}
|
|
115
|
+ */
|
|
116
|
+ start(localTracks = []) {
|
|
117
|
+ if (this._peerConnection) {
|
|
118
|
+ return;
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+ this._tracks = this._tracks.concat(localTracks);
|
|
122
|
+
|
|
123
|
+ this._peerConnection = this._createPeerConnection();
|
|
124
|
+
|
|
125
|
+ this._peerConnection.invite(localTracks);
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ /**
|
|
129
|
+ * Begins the process of disconnecting from a remote peer and cleaning up
|
|
130
|
+ * the peer connection.
|
|
131
|
+ *
|
|
132
|
+ * @returns {void}
|
|
133
|
+ */
|
|
134
|
+ stop() {
|
|
135
|
+ if (this._peerConnection) {
|
|
136
|
+ this._peerConnection.terminate();
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ this._onSessionTerminate();
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ /**
|
|
143
|
+ * Instantiates a new {@code JingleSessionPC} by stubbing out the various
|
|
144
|
+ * dependencies of {@code JingleSessionPC}.
|
|
145
|
+ *
|
|
146
|
+ * @private
|
|
147
|
+ * @returns {JingleSessionPC}
|
|
148
|
+ */
|
|
149
|
+ _createPeerConnection() {
|
|
150
|
+ /**
|
|
151
|
+ * {@code JingleSessionPC} takes in the entire jitsi-meet config.js
|
|
152
|
+ * object, which may not be accessible from the caller.
|
|
153
|
+ *
|
|
154
|
+ * @type {Object}
|
|
155
|
+ */
|
|
156
|
+ const configStub = {};
|
|
157
|
+
|
|
158
|
+ /**
|
|
159
|
+ * {@code JingleSessionPC} assumes an XMPP/Strophe connection object is
|
|
160
|
+ * passed through, which also has the jingle plugin initialized on it.
|
|
161
|
+ * This connection object is used to signal out peer connection updates
|
|
162
|
+ * via iqs, and those updates need to be piped back out to the remote
|
|
163
|
+ * peer.
|
|
164
|
+ *
|
|
165
|
+ * @type {Object}
|
|
166
|
+ */
|
|
167
|
+ const connectionStub = {
|
|
168
|
+ jingle: {
|
|
169
|
+ terminate: () => { /** no-op */ }
|
|
170
|
+ },
|
|
171
|
+ sendIQ: this._onSendMessage
|
|
172
|
+ };
|
|
173
|
+
|
|
174
|
+ /**
|
|
175
|
+ * {@code JingleSessionPC} can take in a custom ice configuration,
|
|
176
|
+ * depending on the peer connection type, peer-to-peer or other.
|
|
177
|
+ * However, {@code ProxyConnectionPC} always assume a peer-to-peer
|
|
178
|
+ * connection so the ice configuration is hard-coded with defaults.
|
|
179
|
+ *
|
|
180
|
+ * @type {Object}
|
|
181
|
+ */
|
|
182
|
+ const iceConfigStub = {
|
|
183
|
+ jvb: { iceServers: [] },
|
|
184
|
+ p2p: {
|
|
185
|
+ iceServers: DEFAULT_STUN_SERVERS,
|
|
186
|
+ ...this._options.iceConfig
|
|
187
|
+ }
|
|
188
|
+ };
|
|
189
|
+
|
|
190
|
+ /**
|
|
191
|
+ * {@code JingleSessionPC} expects an instance of
|
|
192
|
+ * {@code JitsiConference}, which has an event emitter that is used
|
|
193
|
+ * to signal various connection updates that the local client should
|
|
194
|
+ * act upon. The conference instance is not a dependency of a proxy
|
|
195
|
+ * connection, but the emitted events can be relevant to the proxy
|
|
196
|
+ * connection so the event emitter is stubbed.
|
|
197
|
+ *
|
|
198
|
+ * @param {string} event - The constant for the event type.
|
|
199
|
+ * @type {Function}
|
|
200
|
+ * @returns {void}
|
|
201
|
+ */
|
|
202
|
+ const emitter = event => {
|
|
203
|
+ switch (event) {
|
|
204
|
+ case XMPPEvents.CONNECTION_ICE_FAILED:
|
|
205
|
+ case XMPPEvents.CONNECTION_FAILED:
|
|
206
|
+ this._onError(ACTIONS.CONNECTION_ERROR, event);
|
|
207
|
+ break;
|
|
208
|
+ }
|
|
209
|
+ };
|
|
210
|
+
|
|
211
|
+ /**
|
|
212
|
+ * {@code JingleSessionPC} expects an instance of
|
|
213
|
+ * {@code JitsiConference} to be passed in. {@code ProxyConnectionPC}
|
|
214
|
+ * is instantiated outside of the {@code JitsiConference}, so it must be
|
|
215
|
+ * stubbed to prevent errors.
|
|
216
|
+ *
|
|
217
|
+ * @type {Object}
|
|
218
|
+ */
|
|
219
|
+ const roomStub = {
|
|
220
|
+ addPresenceListener: () => { /** no-op */ },
|
|
221
|
+ connectionTimes: [],
|
|
222
|
+ eventEmitter: { emit: emitter },
|
|
223
|
+ getMediaPresenceInfo: () => {
|
|
224
|
+ // Errors occur if this function does not return an object
|
|
225
|
+
|
|
226
|
+ return {};
|
|
227
|
+ },
|
|
228
|
+ removePresenceListener: () => { /** no-op */ }
|
|
229
|
+ };
|
|
230
|
+
|
|
231
|
+ /**
|
|
232
|
+ * Create an instance of {@code RTC} as it is required for peer
|
|
233
|
+ * connection creation by {@code JingleSessionPC}. An existing instance
|
|
234
|
+ * of {@code RTC} from elsewhere should not be re-used because it is
|
|
235
|
+ * a stateful grouping of utilities.
|
|
236
|
+ */
|
|
237
|
+ const rtc = new RTC(this, {});
|
|
238
|
+
|
|
239
|
+ /**
|
|
240
|
+ * Add the remote track listener here as {@code JingleSessionPC} has
|
|
241
|
+ * {@code TraceablePeerConnection} which uses {@code RTC}'s event
|
|
242
|
+ * emitter.
|
|
243
|
+ */
|
|
244
|
+ rtc.addListener(
|
|
245
|
+ RTCEvents.REMOTE_TRACK_ADDED,
|
|
246
|
+ this._onRemoteStream
|
|
247
|
+ );
|
|
248
|
+
|
|
249
|
+ const peerConnection = new JingleSessionPC(
|
|
250
|
+ undefined, // sid
|
|
251
|
+ undefined, // localJid
|
|
252
|
+ this._options.peerJid, // remoteJid
|
|
253
|
+ connectionStub, // connection
|
|
254
|
+ {
|
|
255
|
+ offerToReceiveAudio: this._options.receiveAudio,
|
|
256
|
+ offerToReceiveVideo: this._options.receiveVideo
|
|
257
|
+ }, // mediaConstraints
|
|
258
|
+ iceConfigStub, // iceConfig
|
|
259
|
+ true, // isP2P
|
|
260
|
+ this._options.isInitiator // isInitiator
|
|
261
|
+ );
|
|
262
|
+
|
|
263
|
+ /**
|
|
264
|
+ * An additional initialize call is necessary to properly set instance
|
|
265
|
+ * variable for calling.
|
|
266
|
+ */
|
|
267
|
+ peerConnection.initialize(roomStub, rtc, configStub);
|
|
268
|
+
|
|
269
|
+ return peerConnection;
|
|
270
|
+ }
|
|
271
|
+
|
|
272
|
+ /**
|
|
273
|
+ * Invoked when a connection related issue has been encountered.
|
|
274
|
+ *
|
|
275
|
+ * @param {string} errorType - The constant indicating the type of the error
|
|
276
|
+ * that occured.
|
|
277
|
+ * @param {string} details - Optional additional data about the error.
|
|
278
|
+ * @private
|
|
279
|
+ * @returns {void}
|
|
280
|
+ */
|
|
281
|
+ _onError(errorType, details = '') {
|
|
282
|
+ this._options.onError(this._options.peerJid, errorType, details);
|
|
283
|
+ }
|
|
284
|
+
|
|
285
|
+ /**
|
|
286
|
+ * Callback invoked when the peer connection has received a remote media
|
|
287
|
+ * stream.
|
|
288
|
+ *
|
|
289
|
+ * @param {JitsiRemoteTrack} jitsiRemoteTrack - The remote media stream
|
|
290
|
+ * wrapped in {@code JitsiRemoteTrack}.
|
|
291
|
+ * @private
|
|
292
|
+ * @returns {void}
|
|
293
|
+ */
|
|
294
|
+ _onRemoteStream(jitsiRemoteTrack) {
|
|
295
|
+ this._tracks.push(jitsiRemoteTrack);
|
|
296
|
+
|
|
297
|
+ this._options.onRemoteStream(jitsiRemoteTrack);
|
|
298
|
+ }
|
|
299
|
+
|
|
300
|
+ /**
|
|
301
|
+ * Callback invoked when {@code JingleSessionPC} needs to signal a message
|
|
302
|
+ * out to the remote peer.
|
|
303
|
+ *
|
|
304
|
+ * @param {XML} iq - The message to signal out.
|
|
305
|
+ * @private
|
|
306
|
+ * @returns {void}
|
|
307
|
+ */
|
|
308
|
+ _onSendMessage(iq) {
|
|
309
|
+ this._options.onSendMessage(this._options.peerJid, iq);
|
|
310
|
+ }
|
|
311
|
+
|
|
312
|
+ /**
|
|
313
|
+ * Callback invoked in response to an agreement to start a proxy connection.
|
|
314
|
+ * The passed in jingle element should contain an SDP answer to a previously
|
|
315
|
+ * sent SDP offer.
|
|
316
|
+ *
|
|
317
|
+ * @param {Object} $jingle - The jingle element wrapped in jQuery.
|
|
318
|
+ * @private
|
|
319
|
+ * @returns {void}
|
|
320
|
+ */
|
|
321
|
+ _onSessionAccept($jingle) {
|
|
322
|
+ if (!this._peerConnection) {
|
|
323
|
+ logger.error('Received an answer when no peer connection exists.');
|
|
324
|
+
|
|
325
|
+ return;
|
|
326
|
+ }
|
|
327
|
+
|
|
328
|
+ this._peerConnection.setAnswer($jingle);
|
|
329
|
+ }
|
|
330
|
+
|
|
331
|
+ /**
|
|
332
|
+ * Callback invoked in response to a request to start a proxy connection.
|
|
333
|
+ * The passed in jingle element should contain an SDP offer.
|
|
334
|
+ *
|
|
335
|
+ * @param {Object} $jingle - The jingle element wrapped in jQuery.
|
|
336
|
+ * @private
|
|
337
|
+ * @returns {void}
|
|
338
|
+ */
|
|
339
|
+ _onSessionInitiate($jingle) {
|
|
340
|
+ if (this._peerConnection) {
|
|
341
|
+ logger.error('Received an offer when an offer was already sent.');
|
|
342
|
+
|
|
343
|
+ return;
|
|
344
|
+ }
|
|
345
|
+
|
|
346
|
+ this._peerConnection = this._createPeerConnection();
|
|
347
|
+
|
|
348
|
+ this._peerConnection.acceptOffer(
|
|
349
|
+ $jingle,
|
|
350
|
+ () => { /** no-op */ },
|
|
351
|
+ () => this._onError(
|
|
352
|
+ this._options.peerJid,
|
|
353
|
+ ACTIONS.CONNECTION_ERROR,
|
|
354
|
+ 'session initiate error'
|
|
355
|
+ )
|
|
356
|
+ );
|
|
357
|
+ }
|
|
358
|
+
|
|
359
|
+ /**
|
|
360
|
+ * Callback invoked in response to a request to disconnect an active proxy
|
|
361
|
+ * connection. Cleans up tracks and the peer connection.
|
|
362
|
+ *
|
|
363
|
+ * @private
|
|
364
|
+ * @returns {void}
|
|
365
|
+ */
|
|
366
|
+ _onSessionTerminate() {
|
|
367
|
+ this._tracks.forEach(track => track.dispose());
|
|
368
|
+ this._tracks = [];
|
|
369
|
+
|
|
370
|
+ if (!this._peerConnection) {
|
|
371
|
+ return;
|
|
372
|
+ }
|
|
373
|
+
|
|
374
|
+ this._peerConnection.onTerminated();
|
|
375
|
+ }
|
|
376
|
+
|
|
377
|
+ /**
|
|
378
|
+ * Callback invoked in response to ICE candidates from the remote peer.
|
|
379
|
+ * The passed in jingle element should contain an ICE candidate.
|
|
380
|
+ *
|
|
381
|
+ * @param {Object} $jingle - The jingle element wrapped in jQuery.
|
|
382
|
+ * @private
|
|
383
|
+ * @returns {void}
|
|
384
|
+ */
|
|
385
|
+ _onTransportInfo($jingle) {
|
|
386
|
+ this._peerConnection.addIceCandidates($jingle);
|
|
387
|
+ }
|
|
388
|
+}
|