Переглянути джерело

feat(RTC) remove unnecessary wrapAttachMediaStream

There is a single implementation of it, so inline it.

Also make sure we only initialize device change listeners once.
master
Saúl Ibarra Corretgé 6 місяці тому
джерело
коміт
e41bcf15ec
1 змінених файлів з 53 додано та 48 видалено
  1. 53
    48
      modules/RTC/RTCUtils.js

+ 53
- 48
modules/RTC/RTCUtils.js Переглянути файл

@@ -234,6 +234,15 @@ function sendDeviceListToAnalytics(deviceList) {
234 234
  *
235 235
  */
236 236
 class RTCUtils extends Listenable {
237
+    /**
238
+     *
239
+     */
240
+    constructor() {
241
+        super();
242
+
243
+        this._initOnce = false;
244
+    }
245
+
237 246
     /**
238 247
      * Depending on the browser, sets difference instance methods for
239 248
      * interacting with user media and adds methods to native WebRTC-related
@@ -265,18 +274,16 @@ class RTCUtils extends Listenable {
265 274
             logger.info(`Stereo: ${stereo}`);
266 275
         }
267 276
 
277
+        if (this._initOnce) {
278
+            return;
279
+        }
280
+
281
+        // Anything beyond this point needs to be initialized only once.
282
+        this._initOnce = true;
283
+
268 284
         window.clearInterval(availableDevicesPollTimer);
269 285
         availableDevicesPollTimer = undefined;
270 286
 
271
-        if (!browser.isReactNative()) {
272
-            this.attachMediaStream
273
-                = wrapAttachMediaStream((element, stream) => {
274
-                    if (element) {
275
-                        element.srcObject = stream;
276
-                    }
277
-                });
278
-        }
279
-
280 287
         screenObtainer.init(options);
281 288
 
282 289
         this.enumerateDevices(ds => {
@@ -303,6 +310,42 @@ class RTCUtils extends Listenable {
303 310
         });
304 311
     }
305 312
 
313
+    /**
314
+     * Attaches the given media stream to the given element.
315
+     *
316
+     * @param {*} element DOM element.
317
+     * @param {*} stream MediaStream.
318
+     * @returns Promise<void>
319
+     */
320
+    attachMediaStream(element, stream) {
321
+        if (element) {
322
+            element.srcObject = stream;
323
+        }
324
+
325
+        if (element && stream
326
+                && this.isDeviceChangeAvailable('output')
327
+                && stream.getAudioTracks().length
328
+
329
+                // we skip setting audio output if there was no explicit change
330
+                && audioOutputChanged) {
331
+            return element.setSinkId(this.getAudioOutputDevice()).catch(ex => {
332
+                const err
333
+                    = new JitsiTrackError(ex, null, [ 'audiooutput' ]);
334
+
335
+                logger.warn(
336
+                    'Failed to set audio output device for the element.'
337
+                        + ' Default audio output device will be used'
338
+                        + ' instead',
339
+                    element?.id,
340
+                    err);
341
+
342
+                throw err;
343
+            });
344
+        }
345
+
346
+        return Promise.resolve();
347
+    }
348
+
306 349
     /**
307 350
      *
308 351
      * @param {Function} callback
@@ -846,43 +889,5 @@ class RTCUtils extends Listenable {
846 889
     }
847 890
 }
848 891
 
849
-const rtcUtils = new RTCUtils();
850
-
851
-/**
852
- * Wraps original attachMediaStream function to set current audio output device
853
- * if this is supported.
854
- * @param {Function} origAttachMediaStream
855
- * @returns {Function}
856
- */
857
-function wrapAttachMediaStream(origAttachMediaStream) {
858
-    return function(element, stream) {
859
-        // eslint-disable-next-line prefer-rest-params
860
-        origAttachMediaStream.apply(rtcUtils, arguments);
861
-
862
-        if (stream
863
-                && rtcUtils.isDeviceChangeAvailable('output')
864
-                && stream.getAudioTracks
865
-                && stream.getAudioTracks().length
866
-
867
-                // we skip setting audio output if there was no explicit change
868
-                && audioOutputChanged) {
869
-            return element.setSinkId(rtcUtils.getAudioOutputDevice()).catch(ex => {
870
-                const err
871
-                    = new JitsiTrackError(ex, null, [ 'audiooutput' ]);
872
-
873
-                logger.warn(
874
-                    'Failed to set audio output device for the element.'
875
-                        + ' Default audio output device will be used'
876
-                        + ' instead',
877
-                    element?.id,
878
-                    err);
879
-
880
-                throw err;
881
-            });
882
-        }
883
-
884
-        return Promise.resolve();
885
-    };
886
-}
887 892
 
888
-export default rtcUtils;
893
+export default new RTCUtils();

Завантаження…
Відмінити
Зберегти