Explorar el Código

remove old track from the conference before adding new one

master
isymchych hace 10 años
padre
commit
9581afb612
Se han modificado 1 ficheros con 56 adiciones y 44 borrados
  1. 56
    44
      conference.js

+ 56
- 44
conference.js Ver fichero

@@ -468,8 +468,6 @@ export default {
468 468
             this._getConferenceOptions());
469 469
         this.localId = room.myUserId();
470 470
         localTracks.forEach((track) => {
471
-            room.addTrack(track);
472
-
473 471
             if (track.isAudioTrack()) {
474 472
                 this.useAudioStream(track);
475 473
             } else if (track.isVideoTrack()) {
@@ -505,53 +503,72 @@ export default {
505 503
      * Start using provided video stream.
506 504
      * Stops previous video stream.
507 505
      * @param {JitsiLocalTrack} [stream] new stream to use or null
506
+     * @returns {Promise}
508 507
      */
509 508
     useVideoStream (stream) {
509
+        let promise = Promise.resolve();
510 510
         if (localVideo) {
511
-            localVideo.stop();
511
+            // this calls room.removeTrack internally
512
+            // so we don't need to remove it manually
513
+            promise = localVideo.stop();
512 514
         }
513 515
         localVideo = stream;
514 516
 
515
-        if (stream) {
516
-            this.videoMuted = stream.isMuted();
517
-
518
-            APP.UI.addLocalStream(stream);
517
+        return promise.then(function () {
518
+            if (stream) {
519
+                return room.addTrack(stream);
520
+            }
521
+        }).then(() => {
522
+            if (stream) {
523
+                this.videoMuted = stream.isMuted();
524
+                this.isSharingScreen = stream.videoType === 'desktop';
519 525
 
520
-            this.isSharingScreen = stream.videoType === 'desktop';
521
-        } else {
522
-            this.videoMuted = false;
523
-            this.isSharingScreen = false;
524
-        }
526
+                APP.UI.addLocalStream(stream);
527
+            } else {
528
+                this.videoMuted = false;
529
+                this.isSharingScreen = false;
530
+            }
525 531
 
526
-        APP.UI.setVideoMuted(this.localId, this.videoMuted);
532
+            APP.UI.setVideoMuted(this.localId, this.videoMuted);
527 533
 
528
-        APP.UI.updateDesktopSharingButtons();
534
+            APP.UI.updateDesktopSharingButtons();
535
+        });
529 536
     },
530 537
 
531 538
     /**
532 539
      * Start using provided audio stream.
533 540
      * Stops previous audio stream.
534 541
      * @param {JitsiLocalTrack} [stream] new stream to use or null
542
+     * @returns {Promise}
535 543
      */
536 544
     useAudioStream (stream) {
545
+        let promise = Promise.resolve();
537 546
         if (localAudio) {
538
-            localAudio.stop();
547
+            // this calls room.removeTrack internally
548
+            // so we don't need to remove it manually
549
+            promise = localAudio.stop();
539 550
         }
540 551
         localAudio = stream;
541 552
 
542
-        if (stream) {
543
-            this.audioMuted = stream.isMuted();
553
+        return promise.then(function () {
554
+            if (stream) {
555
+                return room.addTrack(stream);
556
+            }
557
+        }).then(() => {
558
+            if (stream) {
559
+                this.audioMuted = stream.isMuted();
544 560
 
545
-            APP.UI.addLocalStream(stream);
546
-        } else {
547
-            this.audioMuted = false;
548
-        }
561
+                APP.UI.addLocalStream(stream);
562
+            } else {
563
+                this.audioMuted = false;
564
+            }
549 565
 
550
-        APP.UI.setAudioMuted(this.localId, this.audioMuted);
566
+            APP.UI.setAudioMuted(this.localId, this.audioMuted);
567
+        });
551 568
     },
552 569
 
553 570
     videoSwitchInProgress: false,
554
-    toggleScreenSharing () {
571
+    toggleScreenSharing (shareScreen = !this.isSharingScreen) {
555 572
         if (this.videoSwitchInProgress) {
556 573
             console.warn("Switch in progress.");
557 574
             return;
@@ -563,21 +580,7 @@ export default {
563 580
 
564 581
         this.videoSwitchInProgress = true;
565 582
 
566
-        if (this.isSharingScreen) {
567
-            // stop sharing desktop and share video
568
-            createLocalTracks('video').then(function ([stream]) {
569
-                return room.addTrack(stream);
570
-            }).then((stream) => {
571
-                this.useVideoStream(stream);
572
-                this.videoSwitchInProgress = false;
573
-                console.log('sharing local video');
574
-            }).catch(function (err) {
575
-                this.useVideoStream(null);
576
-                this.videoSwitchInProgress = false;
577
-                console.error('failed to share local video', err);
578
-            });
579
-        } else {
580
-            // stop sharing video and share desktop
583
+        if (shareScreen) {
581 584
             createDesktopTrack().then(([stream]) => {
582 585
                 stream.on(
583 586
                     TrackEvents.TRACK_STOPPED,
@@ -587,19 +590,30 @@ export default {
587 590
                         // otherwise we stopped it because we already switched
588 591
                         // to video, so nothing to do here
589 592
                         if (this.isSharingScreen) {
590
-                            this.toggleScreenSharing();
593
+                            this.toggleScreenSharing(false);
591 594
                         }
592 595
                     }
593 596
                 );
594
-                return room.addTrack(stream);
595
-            }).then((stream) => {
596
-                this.useVideoStream(stream);
597
+                return this.useVideoStream(stream);
598
+            }).then(() => {
597 599
                 this.videoSwitchInProgress = false;
598 600
                 console.log('sharing local desktop');
599 601
             }).catch((err) => {
600 602
                 this.videoSwitchInProgress = false;
603
+                this.toggleScreenSharing(false);
601 604
                 console.error('failed to share local desktop', err);
602 605
             });
606
+        } else {
607
+            createLocalTracks('video').then(
608
+                ([stream]) => this.useVideoStream(stream)
609
+            ).then(() => {
610
+                this.videoSwitchInProgress = false;
611
+                console.log('sharing local video');
612
+            }).catch((err) => {
613
+                this.useVideoStream(null);
614
+                this.videoSwitchInProgress = false;
615
+                console.error('failed to share local video', err);
616
+            });
603 617
         }
604 618
     },
605 619
     /**
@@ -953,7 +967,6 @@ export default {
953 967
             (cameraDeviceId) => {
954 968
                 APP.settings.setCameraDeviceId(cameraDeviceId);
955 969
                 createLocalTracks('video').then(([stream]) => {
956
-                    room.addTrack(stream);
957 970
                     this.useVideoStream(stream);
958 971
                     console.log('switched local video device');
959 972
                 });
@@ -965,7 +978,6 @@ export default {
965 978
             (micDeviceId) => {
966 979
                 APP.settings.setMicDeviceId(micDeviceId);
967 980
                 createLocalTracks('audio').then(([stream]) => {
968
-                    room.addTrack(stream);
969 981
                     this.useAudioStream(stream);
970 982
                     console.log('switched local audio device');
971 983
                 });

Loading…
Cancelar
Guardar