Kaynağa Gözat

Returns Promise from mute/unmute and ignores requests while mute/unmuting is in progress. Moves mute/unmute methods to JitsiLocalTrack as said in API docs.

master
damencho 9 yıl önce
ebeveyn
işleme
e4d455a242

+ 9
- 2
JitsiConference.js Dosyayı Görüntüle

@@ -889,8 +889,15 @@ function setupListeners(conference) {
889 889
 
890 890
     conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
891 891
         function (value) {
892
-            conference.rtc.setAudioMute(value);
893
-            conference.isMutedByFocus = true;
892
+            // set isMutedByFocus when setAudioMute Promise ends
893
+            conference.rtc.setAudioMute(value).then(
894
+                function() {
895
+                    conference.isMutedByFocus = true;
896
+                },
897
+                function() {
898
+                    logger.warn(
899
+                        "Error while audio muting due to focus request");
900
+                });
894 901
         }
895 902
     );
896 903
 

+ 2
- 1
JitsiTrackErrors.js Dosyayı Görüntüle

@@ -32,5 +32,6 @@ module.exports = {
32 32
     CHROME_EXTENSION_USER_CANCELED:
33 33
         "gum.chrome_extension_user_canceled",
34 34
     GENERAL: "gum.general",
35
-    TRACK_IS_DISPOSED: "track.track_is_disposed"
35
+    TRACK_IS_DISPOSED: "track.track_is_disposed",
36
+    TRACK_MUTE_UNMUTE_IN_PROGRESS: "track.mute_unmute_inprogress"
36 37
 };

+ 2
- 3
doc/API.md Dosyayı Görüntüle

@@ -309,12 +309,11 @@ We have the following methods for controling the tracks:
309 309
 1. getType() - returns string with the type of the track( "video" for the video tracks and "audio" for the audio tracks)
310 310
 
311 311
 
312
-2. mute() - mutes the track.
312
+2. mute() - mutes the track. Returns Promise.
313 313
 
314 314
    Note: This method is implemented only for the local tracks.
315 315
 
316
-
317
-3. unmute() - unmutes the track.
316
+3. unmute() - unmutes the track. Returns Promise.
318 317
 
319 318
    Note: This method is implemented only for the local tracks.
320 319
 

+ 80
- 13
modules/RTC/JitsiLocalTrack.js Dosyayı Görüntüle

@@ -2,6 +2,7 @@
2 2
 var JitsiTrack = require("./JitsiTrack");
3 3
 var RTCBrowserType = require("./RTCBrowserType");
4 4
 var JitsiTrackEvents = require('../../JitsiTrackEvents');
5
+var JitsiTrackErrors = require("../../JitsiTrackErrors");
5 6
 var RTCUtils = require("./RTCUtils");
6 7
 
7 8
 /**
@@ -28,26 +29,92 @@ function JitsiLocalTrack(stream, videoType,
28 29
             this.dontFireRemoveEvent = false;
29 30
         }.bind(this));
30 31
     this.initialMSID = this.getMSID();
32
+    this.inMuteOrUnmuteProcess = false;
31 33
 }
32 34
 
33 35
 JitsiLocalTrack.prototype = Object.create(JitsiTrack.prototype);
34 36
 JitsiLocalTrack.prototype.constructor = JitsiLocalTrack;
35 37
 
38
+/**
39
+ * Mutes the track. Will reject the Promise if there is mute/unmute operation
40
+ * in progress.
41
+ * @returns {Promise}
42
+ */
43
+JitsiLocalTrack.prototype.mute = function () {
44
+    return new Promise(function (resolve, reject) {
45
+
46
+        if(this.inMuteOrUnmuteProcess) {
47
+            reject(new Error(JitsiTrackErrors.TRACK_MUTE_UNMUTE_IN_PROGRESS));
48
+            return;
49
+        }
50
+        this.inMuteOrUnmuteProcess = true;
51
+
52
+        this._setMute(true,
53
+            function(){
54
+                this.inMuteOrUnmuteProcess = false;
55
+                resolve();
56
+            }.bind(this),
57
+            function(status){
58
+                this.inMuteOrUnmuteProcess = false;
59
+                reject(status);
60
+            }.bind(this));
61
+    }.bind(this));
62
+}
63
+
64
+/**
65
+ * Unmutes the stream. Will reject the Promise if there is mute/unmute operation
66
+ * in progress.
67
+ * @returns {Promise}
68
+ */
69
+JitsiLocalTrack.prototype.unmute = function () {
70
+    return new Promise(function (resolve, reject) {
71
+
72
+        if(this.inMuteOrUnmuteProcess) {
73
+            reject(new Error(JitsiTrackErrors.TRACK_MUTE_UNMUTE_IN_PROGRESS));
74
+            return;
75
+        }
76
+        this.inMuteOrUnmuteProcess = true;
77
+
78
+        this._setMute(false,
79
+            function(){
80
+                this.inMuteOrUnmuteProcess = false;
81
+                resolve();
82
+            }.bind(this),
83
+            function(status){
84
+                this.inMuteOrUnmuteProcess = false;
85
+                reject(status);
86
+            }.bind(this));
87
+    }.bind(this));
88
+}
89
+
36 90
 /**
37 91
  * Mutes / unmutes the track.
38 92
  * @param mute {boolean} if true the track will be muted. Otherwise the track will be unmuted.
39 93
  */
40
-JitsiLocalTrack.prototype._setMute = function (mute) {
94
+JitsiLocalTrack.prototype._setMute = function (mute, resolve, reject) {
41 95
     if (this.isMuted() === mute) {
96
+        resolve();
42 97
         return;
43 98
     }
44 99
     if(!this.rtc) {
45 100
         this.startMuted = mute;
101
+        resolve();
46 102
         return;
47 103
     }
48 104
     var isAudio = this.type === JitsiTrack.AUDIO;
49 105
     this.dontFireRemoveEvent = false;
50 106
 
107
+    var setStreamToNull = false;
108
+    // the callback that will notify that operation had finished
109
+    var callbackFunction = function() {
110
+
111
+        if(setStreamToNull)
112
+            this.stream = null;
113
+        this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
114
+
115
+        resolve();
116
+    }.bind(this);
117
+
51 118
     if ((window.location.protocol != "https:") ||
52 119
         (isAudio) || this.videoType === "desktop" ||
53 120
         // FIXME FF does not support 'removeStream' method used to mute
@@ -58,22 +125,20 @@ JitsiLocalTrack.prototype._setMute = function (mute) {
58 125
             tracks[idx].enabled = !mute;
59 126
         }
60 127
         if(isAudio)
61
-            this.rtc.room.setAudioMute(mute);
128
+            this.rtc.room.setAudioMute(mute, callbackFunction);
62 129
         else
63
-            this.rtc.room.setVideoMute(mute);
64
-        this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
130
+            this.rtc.room.setVideoMute(mute, callbackFunction);
65 131
     } else {
66 132
         if (mute) {
67 133
             this.dontFireRemoveEvent = true;
68 134
             this.rtc.room.removeStream(this.stream, function () {},
69 135
                 {mtype: this.type, type: "mute", ssrc: this.ssrc});
70 136
             RTCUtils.stopMediaStream(this.stream);
137
+            setStreamToNull = true;
71 138
             if(isAudio)
72
-                this.rtc.room.setAudioMute(mute);
139
+                this.rtc.room.setAudioMute(mute, callbackFunction);
73 140
             else
74
-                this.rtc.room.setVideoMute(mute);
75
-            this.stream = null;
76
-            this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
141
+                this.rtc.room.setVideoMute(mute, callbackFunction);
77 142
             //FIXME: Maybe here we should set the SRC for the containers to something
78 143
         } else {
79 144
             var self = this;
@@ -98,8 +163,10 @@ JitsiLocalTrack.prototype._setMute = function (mute) {
98 163
                         }
99 164
                     }
100 165
 
101
-                    if(!stream)
166
+                    if(!stream) {
167
+                        reject(new Error('track.no_stream_found'));
102 168
                         return;
169
+                    }
103 170
 
104 171
                     for(var i = 0; i < self.containers.length; i++)
105 172
                     {
@@ -111,11 +178,11 @@ JitsiLocalTrack.prototype._setMute = function (mute) {
111 178
                     self.rtc.room.addStream(self.stream,
112 179
                         function () {
113 180
                             if(isAudio)
114
-                                self.rtc.room.setAudioMute(mute);
181
+                                self.rtc.room.setAudioMute(
182
+                                    mute, callbackFunction);
115 183
                             else
116
-                                self.rtc.room.setVideoMute(mute);
117
-                            self.eventEmitter.emit(
118
-                                JitsiTrackEvents.TRACK_MUTE_CHANGED);
184
+                                self.rtc.room.setVideoMute(
185
+                                    mute, callbackFunction);
119 186
                         }, {
120 187
                             mtype: self.type,
121 188
                             type: "unmute",

+ 0
- 14
modules/RTC/JitsiTrack.js Dosyayı Görüntüle

@@ -146,20 +146,6 @@ JitsiTrack.prototype._maybeFireTrackAttached = function (container) {
146 146
     }
147 147
 };
148 148
 
149
-/**
150
- * Mutes the track.
151
- */
152
-JitsiTrack.prototype.mute = function () {
153
-    this._setMute(true);
154
-}
155
-
156
-/**
157
- * Unmutes the stream.
158
- */
159
-JitsiTrack.prototype.unmute = function () {
160
-    this._setMute(false);
161
-}
162
-
163 149
 /**
164 150
  * Attaches the MediaStream of this track to an HTML container.
165 151
  * Adds the container to the list of containers that are displaying the track.

+ 6
- 1
modules/RTC/RTC.js Dosyayı Görüntüle

@@ -188,15 +188,20 @@ RTC.prototype.getLocalVideoStream = function () {
188 188
 /**
189 189
  * Set mute for all local audio streams attached to the conference.
190 190
  * @param value the mute value
191
+ * @returns {Promise}
191 192
  */
192 193
 RTC.prototype.setAudioMute = function (value) {
194
+    var mutes = [];
193 195
     for(var i = 0; i < this.localStreams.length; i++) {
194 196
         var stream = this.localStreams[i];
195 197
         if(stream.getType() !== "audio") {
196 198
             continue;
197 199
         }
198
-        stream._setMute(value);
200
+        // this is a Promise
201
+        mutes.push(stream.mute(value));
199 202
     }
203
+    // we return a Promise from all Promises so we can wait for their execution
204
+    return Promise.all(mutes);
200 205
 }
201 206
 
202 207
 RTC.prototype.removeLocalStream = function (stream) {

Loading…
İptal
Kaydet