浏览代码

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.

dev1
damencho 9 年前
父节点
当前提交
e4d455a242
共有 6 个文件被更改,包括 99 次插入34 次删除
  1. 9
    2
      JitsiConference.js
  2. 2
    1
      JitsiTrackErrors.js
  3. 2
    3
      doc/API.md
  4. 80
    13
      modules/RTC/JitsiLocalTrack.js
  5. 0
    14
      modules/RTC/JitsiTrack.js
  6. 6
    1
      modules/RTC/RTC.js

+ 9
- 2
JitsiConference.js 查看文件

889
 
889
 
890
     conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
890
     conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
891
         function (value) {
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 查看文件

32
     CHROME_EXTENSION_USER_CANCELED:
32
     CHROME_EXTENSION_USER_CANCELED:
33
         "gum.chrome_extension_user_canceled",
33
         "gum.chrome_extension_user_canceled",
34
     GENERAL: "gum.general",
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 查看文件

309
 1. getType() - returns string with the type of the track( "video" for the video tracks and "audio" for the audio tracks)
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
    Note: This method is implemented only for the local tracks.
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
    Note: This method is implemented only for the local tracks.
318
    Note: This method is implemented only for the local tracks.
320
 
319
 

+ 80
- 13
modules/RTC/JitsiLocalTrack.js 查看文件

2
 var JitsiTrack = require("./JitsiTrack");
2
 var JitsiTrack = require("./JitsiTrack");
3
 var RTCBrowserType = require("./RTCBrowserType");
3
 var RTCBrowserType = require("./RTCBrowserType");
4
 var JitsiTrackEvents = require('../../JitsiTrackEvents');
4
 var JitsiTrackEvents = require('../../JitsiTrackEvents');
5
+var JitsiTrackErrors = require("../../JitsiTrackErrors");
5
 var RTCUtils = require("./RTCUtils");
6
 var RTCUtils = require("./RTCUtils");
6
 
7
 
7
 /**
8
 /**
28
             this.dontFireRemoveEvent = false;
29
             this.dontFireRemoveEvent = false;
29
         }.bind(this));
30
         }.bind(this));
30
     this.initialMSID = this.getMSID();
31
     this.initialMSID = this.getMSID();
32
+    this.inMuteOrUnmuteProcess = false;
31
 }
33
 }
32
 
34
 
33
 JitsiLocalTrack.prototype = Object.create(JitsiTrack.prototype);
35
 JitsiLocalTrack.prototype = Object.create(JitsiTrack.prototype);
34
 JitsiLocalTrack.prototype.constructor = JitsiLocalTrack;
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
  * Mutes / unmutes the track.
91
  * Mutes / unmutes the track.
38
  * @param mute {boolean} if true the track will be muted. Otherwise the track will be unmuted.
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
     if (this.isMuted() === mute) {
95
     if (this.isMuted() === mute) {
96
+        resolve();
42
         return;
97
         return;
43
     }
98
     }
44
     if(!this.rtc) {
99
     if(!this.rtc) {
45
         this.startMuted = mute;
100
         this.startMuted = mute;
101
+        resolve();
46
         return;
102
         return;
47
     }
103
     }
48
     var isAudio = this.type === JitsiTrack.AUDIO;
104
     var isAudio = this.type === JitsiTrack.AUDIO;
49
     this.dontFireRemoveEvent = false;
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
     if ((window.location.protocol != "https:") ||
118
     if ((window.location.protocol != "https:") ||
52
         (isAudio) || this.videoType === "desktop" ||
119
         (isAudio) || this.videoType === "desktop" ||
53
         // FIXME FF does not support 'removeStream' method used to mute
120
         // FIXME FF does not support 'removeStream' method used to mute
58
             tracks[idx].enabled = !mute;
125
             tracks[idx].enabled = !mute;
59
         }
126
         }
60
         if(isAudio)
127
         if(isAudio)
61
-            this.rtc.room.setAudioMute(mute);
128
+            this.rtc.room.setAudioMute(mute, callbackFunction);
62
         else
129
         else
63
-            this.rtc.room.setVideoMute(mute);
64
-        this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
130
+            this.rtc.room.setVideoMute(mute, callbackFunction);
65
     } else {
131
     } else {
66
         if (mute) {
132
         if (mute) {
67
             this.dontFireRemoveEvent = true;
133
             this.dontFireRemoveEvent = true;
68
             this.rtc.room.removeStream(this.stream, function () {},
134
             this.rtc.room.removeStream(this.stream, function () {},
69
                 {mtype: this.type, type: "mute", ssrc: this.ssrc});
135
                 {mtype: this.type, type: "mute", ssrc: this.ssrc});
70
             RTCUtils.stopMediaStream(this.stream);
136
             RTCUtils.stopMediaStream(this.stream);
137
+            setStreamToNull = true;
71
             if(isAudio)
138
             if(isAudio)
72
-                this.rtc.room.setAudioMute(mute);
139
+                this.rtc.room.setAudioMute(mute, callbackFunction);
73
             else
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
             //FIXME: Maybe here we should set the SRC for the containers to something
142
             //FIXME: Maybe here we should set the SRC for the containers to something
78
         } else {
143
         } else {
79
             var self = this;
144
             var self = this;
98
                         }
163
                         }
99
                     }
164
                     }
100
 
165
 
101
-                    if(!stream)
166
+                    if(!stream) {
167
+                        reject(new Error('track.no_stream_found'));
102
                         return;
168
                         return;
169
+                    }
103
 
170
 
104
                     for(var i = 0; i < self.containers.length; i++)
171
                     for(var i = 0; i < self.containers.length; i++)
105
                     {
172
                     {
111
                     self.rtc.room.addStream(self.stream,
178
                     self.rtc.room.addStream(self.stream,
112
                         function () {
179
                         function () {
113
                             if(isAudio)
180
                             if(isAudio)
114
-                                self.rtc.room.setAudioMute(mute);
181
+                                self.rtc.room.setAudioMute(
182
+                                    mute, callbackFunction);
115
                             else
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
                             mtype: self.type,
187
                             mtype: self.type,
121
                             type: "unmute",
188
                             type: "unmute",

+ 0
- 14
modules/RTC/JitsiTrack.js 查看文件

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
  * Attaches the MediaStream of this track to an HTML container.
150
  * Attaches the MediaStream of this track to an HTML container.
165
  * Adds the container to the list of containers that are displaying the track.
151
  * Adds the container to the list of containers that are displaying the track.

+ 6
- 1
modules/RTC/RTC.js 查看文件

188
 /**
188
 /**
189
  * Set mute for all local audio streams attached to the conference.
189
  * Set mute for all local audio streams attached to the conference.
190
  * @param value the mute value
190
  * @param value the mute value
191
+ * @returns {Promise}
191
  */
192
  */
192
 RTC.prototype.setAudioMute = function (value) {
193
 RTC.prototype.setAudioMute = function (value) {
194
+    var mutes = [];
193
     for(var i = 0; i < this.localStreams.length; i++) {
195
     for(var i = 0; i < this.localStreams.length; i++) {
194
         var stream = this.localStreams[i];
196
         var stream = this.localStreams[i];
195
         if(stream.getType() !== "audio") {
197
         if(stream.getType() !== "audio") {
196
             continue;
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
 RTC.prototype.removeLocalStream = function (stream) {
207
 RTC.prototype.removeLocalStream = function (stream) {

正在加载...
取消
保存