Procházet zdrojové kódy

fix(blur): Error handling and setEffect issues.

dev1
Hristo Terezov před 6 roky
rodič
revize
d9a5afe94b
1 změnil soubory, kde provedl 63 přidání a 69 odebrání
  1. 63
    69
      modules/RTC/JitsiLocalTrack.js

+ 63
- 69
modules/RTC/JitsiLocalTrack.js Zobrazit soubor

@@ -32,7 +32,6 @@ const logger = getLogger(__filename);
32 32
  * One <tt>JitsiLocalTrack</tt> corresponds to one WebRTC MediaStreamTrack.
33 33
  */
34 34
 export default class JitsiLocalTrack extends JitsiTrack {
35
-
36 35
     /**
37 36
      * Constructs new JitsiLocalTrack instance.
38 37
      *
@@ -73,14 +72,11 @@ export default class JitsiLocalTrack extends JitsiTrack {
73 72
             mediaType,
74 73
             videoType);
75 74
 
75
+        this._setEffectInProgress = false;
76 76
         const effect = effects.find(e => e.isEnabled(this));
77 77
 
78
-        this._effectEnabled = false;
79
-        this._streamChangeInProgress = false;
80
-
81 78
         if (effect) {
82
-            this._setStream(this._startStreamEffect(effect));
83
-            this._effectEnabled = true;
79
+            this._startStreamEffect(effect);
84 80
         }
85 81
 
86 82
         /**
@@ -324,98 +320,101 @@ export default class JitsiLocalTrack extends JitsiTrack {
324 320
      *
325 321
      * @private
326 322
      * @param {*} effect - Represents effect instance
327
-     * @returns {MediaStream}
323
+     * @returns {void}
328 324
      */
329 325
     _startStreamEffect(effect) {
330 326
         this._streamEffect = effect;
331 327
         this._originalStream = this.stream;
332
-        this._streamEffect.startEffect(this._originalStream);
333
-
334
-        return this._streamEffect.getStreamWithEffect();
328
+        this._setStream(this._streamEffect.startEffect(this._originalStream));
335 329
     }
336 330
 
337 331
     /**
338 332
      * Stops the effect process and returns the original stream.
339 333
      *
340 334
      * @private
341
-     * @returns {MediaStream}
335
+     * @returns {void}
342 336
      */
343 337
     _stopStreamEffect() {
344
-        this._streamEffect.stopEffect();
338
+        if (this._streamEffect) {
339
+            this._streamEffect.stopEffect();
340
+            this._setStream(this._originalStream);
341
+            this._originalStream = undefined;
342
+        }
343
+    }
345 344
 
346
-        return this._originalStream;
345
+    /**
346
+     * Stops the currently used effect (if there is one) and starts the passed effect (if there is one).
347
+     *
348
+     * @param {Object|undefined} effect - The new effect to be set.
349
+     */
350
+    _switchStreamEffect(effect) {
351
+        if (this._streamEffect) {
352
+            this._stopStreamEffect();
353
+            this._streamEffect = undefined;
354
+        }
355
+        if (effect) {
356
+            this._startStreamEffect(effect);
357
+        }
347 358
     }
348 359
 
349 360
     /**
350
-     * Switches the video stream associated with this track and updates the containers.
361
+     * Sets the effect and switches between the modified stream and original one.
351 362
      *
352
-     * @private
353
-     * @param {MediaStream} newStream - The MediaStream to replace
363
+     * @param {Object} effect - Represents the effect instance to be used.
354 364
      * @returns {Promise}
355 365
      */
356
-    _switchStream(newStream: MediaStream) {
357
-        if (this._streamChangeInProgress === true) {
358
-            return Promise.reject(new Error('Stream change already in progress!'));
366
+    setEffect(effect) {
367
+        if (typeof this._streamEffect === 'undefined' && typeof effect === 'undefined') {
368
+            return Promise.resolve();
369
+        }
370
+
371
+        if (typeof effect !== 'undefined' && !effect.isEnabled(this)) {
372
+            return Promise.reject(new Error('Incompatible effect instance!'));
359 373
         }
360 374
 
361
-        this._streamChangeInProgress = true;
375
+        if (this._setEffectInProgress === true) {
376
+            return Promise.reject(new Error('setEffect already in progress!'));
377
+        }
378
+
379
+        if (this.isMuted()) {
380
+            this._streamEffect = effect;
381
+
382
+            return Promise.resolve();
383
+        }
362 384
 
363 385
         const conference = this.conference;
364 386
 
365 387
         if (!conference) {
388
+            this._switchStreamEffect(effect);
389
+
366 390
             return Promise.resolve();
367 391
         }
368 392
 
393
+        this._setEffectInProgress = true;
394
+
395
+        // TODO: Create new JingleSessionPC method for replacing a stream in JitsiLocalTrack without offer answer.
369 396
         return conference.removeTrack(this)
370 397
             .then(() => {
371
-                this._setStream(newStream);
372
-
373
-                if (!this.isAudioTrack()) {
374
-                    this.containers.forEach(
375
-                        cont => RTCUtils.attachMediaStream(cont, this.stream));
398
+                this._switchStreamEffect(effect);
399
+                if (this.isVideoTrack()) {
400
+                    this.containers.forEach(cont => RTCUtils.attachMediaStream(cont, this.stream));
376 401
                 }
377 402
 
378 403
                 return conference.addTrack(this);
379 404
             })
380 405
             .then(() => {
381
-                this._streamChangeInProgress = false;
406
+                this._setEffectInProgress = false;
382 407
             })
383 408
             .catch(error => {
384
-                this._streamChangeInProgress = false;
385
-                logger.error('Failed to switch to new stream!', error);
409
+                // Any error will be not recovarable and will trigger CONFERENCE_FAILED event. But let's try to cleanup
410
+                // everyhting related to the effect functionality.
411
+                this._setEffectInProgress = false;
412
+                this._switchStreamEffect();
413
+                logger.error('Failed to switch to the new stream!', error);
386 414
                 throw error;
387 415
             });
388 416
     }
389 417
 
390
-    /**
391
-     * Sets the effect and switches between the modified stream and original one.
392
-     *
393
-     * @param {Boolean} enableFlag - Flag to start or stop the effect processing
394
-     * @param {*} effect - Represents the effect instance to use
395
-     * @returns {Promise}
396
-     */
397
-    enableEffect(enableFlag: Boolean, effect) {
398
-        if (this._effectEnabled === enableFlag) {
399
-            return Promise.resolve();
400
-        }
401
-
402
-        this._effectEnabled = enableFlag;
403
-
404
-        if (this.isMuted()) {
405
-            return Promise.resolve();
406
-        }
407
-
408
-        return this._switchStream(enableFlag ? this._startStreamEffect(effect) : this._stopStreamEffect())
409
-                    .catch(error => {
410
-                        if (enableFlag) {
411
-                            this._stopStreamEffect();
412
-                        }
413
-                        this._effectEnabled = false;
414
-                        logger.error('Failed to switch to new stream!', error);
415
-                        throw error;
416
-                    });
417
-    }
418
-
419 418
     /**
420 419
      * Asynchronously mutes this track.
421 420
      *
@@ -481,11 +480,14 @@ export default class JitsiLocalTrack extends JitsiTrack {
481 480
                 this.track.enabled = !muted;
482 481
             }
483 482
         } else if (muted) {
484
-
485 483
             promise = new Promise((resolve, reject) => {
486 484
                 logMuteInfo();
487 485
                 this._removeStreamFromConferenceAsMute(
488 486
                     () => {
487
+                        if (this._streamEffect) {
488
+                            this._stopStreamEffect();
489
+                        }
490
+
489 491
                         // FIXME: Maybe here we should set the SRC for the
490 492
                         // containers to something
491 493
                         // We don't want any events to be fired on this stream
@@ -496,12 +498,6 @@ export default class JitsiLocalTrack extends JitsiTrack {
496 498
                     },
497 499
                     reject);
498 500
             });
499
-
500
-            promise.then(() => {
501
-                if (this._effectEnabled) {
502
-                    this._stopStreamEffect();
503
-                }
504
-            });
505 501
         } else {
506 502
             logMuteInfo();
507 503
 
@@ -553,8 +549,8 @@ export default class JitsiLocalTrack extends JitsiTrack {
553 549
                     throw new JitsiTrackError(TRACK_NO_STREAM_FOUND);
554 550
                 }
555 551
 
556
-                if (this._effectEnabled) {
557
-                    this._setStream(this._startStreamEffect(this._streamEffect));
552
+                if (this._streamEffect) {
553
+                    this._startStreamEffect(this._streamEffect);
558 554
                 }
559 555
 
560 556
                 this.containers.map(
@@ -644,9 +640,7 @@ export default class JitsiLocalTrack extends JitsiTrack {
644 640
      * @returns {Promise}
645 641
      */
646 642
     dispose() {
647
-        if (this._effectEnabled) {
648
-            this._setStream(this._stopStreamEffect());
649
-        }
643
+        this._switchStreamEffect();
650 644
 
651 645
         let promise = Promise.resolve();
652 646
 

Načítá se…
Zrušit
Uložit