Bladeren bron

Merge pull request #633 from jitsi/fix-and-refactor-shared-video

Fix and refactor shared video
master
hristoterezov 9 jaren geleden
bovenliggende
commit
cdefca9fbd
3 gewijzigde bestanden met toevoegingen van 61 en 57 verwijderingen
  1. 4
    4
      conference.js
  2. 6
    6
      modules/UI/UI.js
  3. 51
    47
      modules/UI/shared_video/SharedVideo.js

+ 4
- 4
conference.js Bestand weergeven

762
             console.log('USER %s LEFT', id, user);
762
             console.log('USER %s LEFT', id, user);
763
             APP.API.notifyUserLeft(id);
763
             APP.API.notifyUserLeft(id);
764
             APP.UI.removeUser(id, user.getDisplayName());
764
             APP.UI.removeUser(id, user.getDisplayName());
765
-            APP.UI.stopSharedVideo(id);
765
+            APP.UI.onSharedVideoStop(id);
766
         });
766
         });
767
 
767
 
768
 
768
 
1130
             this.commands.defaults.SHARED_VIDEO, ({value, attributes}, id) => {
1130
             this.commands.defaults.SHARED_VIDEO, ({value, attributes}, id) => {
1131
 
1131
 
1132
                 if (attributes.state === 'stop') {
1132
                 if (attributes.state === 'stop') {
1133
-                    APP.UI.stopSharedVideo(id, attributes);
1133
+                    APP.UI.onSharedVideoStop(id, attributes);
1134
                 }
1134
                 }
1135
                 else if (attributes.state === 'start') {
1135
                 else if (attributes.state === 'start') {
1136
-                    APP.UI.showSharedVideo(id, value, attributes);
1136
+                    APP.UI.onSharedVideoStart(id, value, attributes);
1137
                 }
1137
                 }
1138
                 else if (attributes.state === 'playing'
1138
                 else if (attributes.state === 'playing'
1139
                     || attributes.state === 'pause') {
1139
                     || attributes.state === 'pause') {
1140
-                    APP.UI.updateSharedVideo(id, value, attributes);
1140
+                    APP.UI.onSharedVideoUpdate(id, value, attributes);
1141
                 }
1141
                 }
1142
             });
1142
             });
1143
     },
1143
     },

+ 6
- 6
modules/UI/UI.js Bestand weergeven

1116
  * @param {string} url video url
1116
  * @param {string} url video url
1117
  * @param {string} attributes
1117
  * @param {string} attributes
1118
 */
1118
 */
1119
-UI.showSharedVideo = function (id, url, attributes) {
1119
+UI.onSharedVideoStart = function (id, url, attributes) {
1120
     if (sharedVideoManager)
1120
     if (sharedVideoManager)
1121
-        sharedVideoManager.showSharedVideo(id, url, attributes);
1121
+        sharedVideoManager.onSharedVideoStart(id, url, attributes);
1122
 };
1122
 };
1123
 
1123
 
1124
 /**
1124
 /**
1127
  * @param {string} url video url
1127
  * @param {string} url video url
1128
  * @param {string} attributes
1128
  * @param {string} attributes
1129
  */
1129
  */
1130
-UI.updateSharedVideo = function (id, url, attributes) {
1130
+UI.onSharedVideoUpdate = function (id, url, attributes) {
1131
     if (sharedVideoManager)
1131
     if (sharedVideoManager)
1132
-        sharedVideoManager.updateSharedVideo(id, url, attributes);
1132
+        sharedVideoManager.onSharedVideoUpdate(id, url, attributes);
1133
 };
1133
 };
1134
 
1134
 
1135
 /**
1135
 /**
1137
  * @param {string} id the id of the sender of the command
1137
  * @param {string} id the id of the sender of the command
1138
  * @param {string} attributes
1138
  * @param {string} attributes
1139
  */
1139
  */
1140
-UI.stopSharedVideo = function (id, attributes) {
1140
+UI.onSharedVideoStop = function (id, attributes) {
1141
     if (sharedVideoManager)
1141
     if (sharedVideoManager)
1142
-        sharedVideoManager.stopSharedVideo(id, attributes);
1142
+        sharedVideoManager.onSharedVideoStop(id, attributes);
1143
 };
1143
 };
1144
 
1144
 
1145
 module.exports = UI;
1145
 module.exports = UI;

+ 51
- 47
modules/UI/shared_video/SharedVideo.js Bestand weergeven

79
     }
79
     }
80
 
80
 
81
     /**
81
     /**
82
-     * Shows the player component and starts the checking function
83
-     * that will be sending updates, if we are the one shared the video
82
+     * Shows the player component and starts the process that will be sending
83
+     * updates, if we are the one shared the video.
84
+     *
84
      * @param id the id of the sender of the command
85
      * @param id the id of the sender of the command
85
      * @param url the video url
86
      * @param url the video url
86
      * @param attributes
87
      * @param attributes
87
      */
88
      */
88
-    showSharedVideo (id, url, attributes) {
89
+    onSharedVideoStart (id, url, attributes) {
89
         if (this.isSharedVideoShown)
90
         if (this.isSharedVideoShown)
90
             return;
91
             return;
91
 
92
 
97
         // the owner of the video
98
         // the owner of the video
98
         this.from = id;
99
         this.from = id;
99
 
100
 
101
+        this.mutedWithUserInteraction = APP.conference.isLocalAudioMuted();
102
+
100
         //listen for local audio mute events
103
         //listen for local audio mute events
101
         this.localAudioMutedListener = this.onLocalAudioMuted.bind(this);
104
         this.localAudioMutedListener = this.onLocalAudioMuted.bind(this);
102
         this.emitter.on(UIEvents.AUDIO_MUTED, this.localAudioMutedListener);
105
         this.emitter.on(UIEvents.AUDIO_MUTED, this.localAudioMutedListener);
153
                 }
156
                 }
154
             };
157
             };
155
 
158
 
159
+        /**
160
+         * Indicates that a change in state has occurred for the shared video.
161
+         * @param event the event notifying us of the change
162
+         */
156
         window.onPlayerStateChange = function(event) {
163
         window.onPlayerStateChange = function(event) {
157
             if (event.data == YT.PlayerState.PLAYING) {
164
             if (event.data == YT.PlayerState.PLAYING) {
158
 
165
 
160
 
167
 
161
                 if(self.initialAttributes)
168
                 if(self.initialAttributes)
162
                 {
169
                 {
163
-                    self.processAttributes(
170
+                    // If a network update has occurred already now is the
171
+                    // time to process it.
172
+                    self.processVideoUpdate(
164
                         self.player,
173
                         self.player,
165
-                        self.initialAttributes,
166
-                        false);
174
+                        self.initialAttributes);
167
 
175
 
168
                     self.initialAttributes = null;
176
                     self.initialAttributes = null;
169
                 }
177
                 }
170
-                self.smartMute();
171
-                self.updateCheck();
178
+                self.smartAudioMute();
172
             } else if (event.data == YT.PlayerState.PAUSED) {
179
             } else if (event.data == YT.PlayerState.PAUSED) {
173
-                self.smartUnmute();
174
-                self.updateCheck(true);
180
+                self.smartAudioUnmute();
175
             }
181
             }
182
+            self.fireSharedVideoEvent(event.data == YT.PlayerState.PAUSED);
176
         };
183
         };
177
 
184
 
178
         /**
185
         /**
182
         window.onVideoProgress = function (event) {
189
         window.onVideoProgress = function (event) {
183
             let state = event.target.getPlayerState();
190
             let state = event.target.getPlayerState();
184
             if (state == YT.PlayerState.PAUSED) {
191
             if (state == YT.PlayerState.PAUSED) {
185
-                self.updateCheck(true);
192
+                self.fireSharedVideoEvent(true);
186
             }
193
             }
187
         };
194
         };
188
 
195
 
191
          * @param event
198
          * @param event
192
          */
199
          */
193
         window.onVolumeChange = function (event) {
200
         window.onVolumeChange = function (event) {
194
-            self.updateCheck();
201
+            self.fireSharedVideoEvent();
195
 
202
 
196
             // let's check, if player is not muted lets mute locally
203
             // let's check, if player is not muted lets mute locally
197
             if(event.data.volume > 0 && !event.data.muted) {
204
             if(event.data.volume > 0 && !event.data.muted) {
198
-                self.smartMute();
205
+                self.smartAudioMute();
199
             }
206
             }
200
             else if (event.data.volume <=0 || event.data.muted) {
207
             else if (event.data.volume <=0 || event.data.muted) {
201
-                self.smartUnmute();
208
+                self.smartAudioUnmute();
202
             }
209
             }
203
         };
210
         };
204
 
211
 
230
             // we need to continuously send the player current time position
237
             // we need to continuously send the player current time position
231
             if(APP.conference.isLocalId(self.from)) {
238
             if(APP.conference.isLocalId(self.from)) {
232
                 self.intervalId = setInterval(
239
                 self.intervalId = setInterval(
233
-                    self.updateCheck.bind(self),
240
+                    self.fireSharedVideoEvent.bind(self),
234
                     updateInterval);
241
                     updateInterval);
235
             }
242
             }
236
         };
243
         };
246
      * Process attributes, whether player needs to be paused or seek.
253
      * Process attributes, whether player needs to be paused or seek.
247
      * @param player the player to operate over
254
      * @param player the player to operate over
248
      * @param attributes the attributes with the player state we want
255
      * @param attributes the attributes with the player state we want
249
-     * @param playerPaused current saved state for the player
250
      */
256
      */
251
-    processAttributes (player, attributes, playerPaused)
257
+    processVideoUpdate (player, attributes)
252
     {
258
     {
253
         if(!attributes)
259
         if(!attributes)
254
             return;
260
             return;
255
 
261
 
256
         if (attributes.state == 'playing') {
262
         if (attributes.state == 'playing') {
257
 
263
 
258
-            this.processTime(player, attributes, playerPaused);
264
+            let isPlayerPaused
265
+                = (this.player.getPlayerState() === YT.PlayerState.PAUSED);
259
 
266
 
260
-            let isAttrMuted = (attributes.muted === "true");
267
+            // If our player is currently paused force the seek.
268
+            this.processTime(player, attributes, isPlayerPaused);
261
 
269
 
262
-            // Process player unmute
263
-            if (player.isMuted() && !isAttrMuted) {
264
-                console.log("Process player unmute and smart mike mute.");
265
-                this.mutePlayer(false);
266
-            }
267
-            // Process player mute
268
-            else if (!player.isMuted() && isAttrMuted) {
269
-                console.log("Process player mute and smart mike unmute.");
270
-                this.mutePlayer(true);
270
+            // Process mute.
271
+            let isAttrMuted = (attributes.muted === "true");
272
+            if (player.isMuted() !== isAttrMuted) {
273
+                this.smartPlayerMute(isAttrMuted, true);
271
             }
274
             }
272
 
275
 
273
             // Process volume
276
             // Process volume
280
                 this.showSharedVideoMutedPopup(false);
283
                 this.showSharedVideoMutedPopup(false);
281
             }
284
             }
282
 
285
 
283
-            if (playerPaused)
286
+            if (isPlayerPaused)
284
                 player.playVideo();
287
                 player.playVideo();
285
 
288
 
286
         } else if (attributes.state == 'pause') {
289
         } else if (attributes.state == 'pause') {
288
             player.pauseVideo();
291
             player.pauseVideo();
289
 
292
 
290
             this.processTime(player, attributes, true);
293
             this.processTime(player, attributes, true);
291
-        } else if (attributes.state == 'stop') {
292
-            this.stopSharedVideo(this.from);
293
         }
294
         }
294
     }
295
     }
295
 
296
 
323
     /**
324
     /**
324
      * Checks current state of the player and fire an event with the values.
325
      * Checks current state of the player and fire an event with the values.
325
      */
326
      */
326
-    updateCheck(sendPauseEvent)
327
+    fireSharedVideoEvent(sendPauseEvent)
327
     {
328
     {
328
         // ignore update checks if we are not the owner of the video
329
         // ignore update checks if we are not the owner of the video
329
         // or there is still no player defined or we are stopped
330
         // or there is still no player defined or we are stopped
356
      * @param url the video url
357
      * @param url the video url
357
      * @param attributes
358
      * @param attributes
358
      */
359
      */
359
-    updateSharedVideo (id, url, attributes) {
360
+    onSharedVideoUpdate (id, url, attributes) {
360
         // if we are sending the event ignore
361
         // if we are sending the event ignore
361
         if(APP.conference.isLocalId(this.from)) {
362
         if(APP.conference.isLocalId(this.from)) {
362
             return;
363
             return;
363
         }
364
         }
364
 
365
 
365
         if(!this.isSharedVideoShown) {
366
         if(!this.isSharedVideoShown) {
366
-            this.showSharedVideo(id, url, attributes);
367
+            this.onSharedVideoStart(id, url, attributes);
367
             return;
368
             return;
368
         }
369
         }
369
 
370
 
370
         if(!this.player)
371
         if(!this.player)
371
             this.initialAttributes = attributes;
372
             this.initialAttributes = attributes;
372
         else {
373
         else {
373
-            this.processAttributes(this.player, attributes,
374
-                (this.player.getPlayerState() === YT.PlayerState.PAUSED));
374
+            this.processVideoUpdate(this.player, attributes);
375
         }
375
         }
376
     }
376
     }
377
 
377
 
381
      * left and we want to remove video if the user sharing it left).
381
      * left and we want to remove video if the user sharing it left).
382
      * @param id the id of the sender of the command
382
      * @param id the id of the sender of the command
383
      */
383
      */
384
-    stopSharedVideo (id, attributes) {
384
+    onSharedVideoStop (id, attributes) {
385
         if (!this.isSharedVideoShown)
385
         if (!this.isSharedVideoShown)
386
             return;
386
             return;
387
 
387
 
421
                     this.errorInPlayer.destroy();
421
                     this.errorInPlayer.destroy();
422
                     this.errorInPlayer = null;
422
                     this.errorInPlayer = null;
423
                 }
423
                 }
424
-                this.smartUnmute();
424
+                this.smartAudioUnmute();
425
                 // revert to original behavior (prevents pausing
425
                 // revert to original behavior (prevents pausing
426
                 // for participants not sharing the video to pause it)
426
                 // for participants not sharing the video to pause it)
427
                 $("#sharedVideo").css("pointer-events","auto");
427
                 $("#sharedVideo").css("pointer-events","auto");
446
             this.mutedWithUserInteraction = userInteraction;
446
             this.mutedWithUserInteraction = userInteraction;
447
         }
447
         }
448
         else if (this.player.getPlayerState() !== YT.PlayerState.PAUSED) {
448
         else if (this.player.getPlayerState() !== YT.PlayerState.PAUSED) {
449
-            this.mutePlayer(true);
449
+            this.smartPlayerMute(true, false);
450
+            // Check if we need to update other participants
451
+            this.fireSharedVideoEvent();
450
         }
452
         }
451
     }
453
     }
452
 
454
 
453
     /**
455
     /**
454
      * Mutes / unmutes the player.
456
      * Mutes / unmutes the player.
455
      * @param mute true to mute the shared video, false - otherwise.
457
      * @param mute true to mute the shared video, false - otherwise.
458
+     * @param {boolean} Indicates if this mute is a consequence of a network
459
+     * video update or is called locally.
456
      */
460
      */
457
-    mutePlayer(mute) {
461
+    smartPlayerMute(mute, isVideoUpdate) {
458
         if (!this.player.isMuted() && mute) {
462
         if (!this.player.isMuted() && mute) {
459
             this.player.mute();
463
             this.player.mute();
460
-            this.smartUnmute();
464
+
465
+            if (isVideoUpdate)
466
+                this.smartAudioUnmute();
461
         }
467
         }
462
         else if (this.player.isMuted() && !mute) {
468
         else if (this.player.isMuted() && !mute) {
463
             this.player.unMute();
469
             this.player.unMute();
464
-            this.smartMute();
470
+            if (isVideoUpdate)
471
+                this.smartAudioMute();
465
         }
472
         }
466
 
473
 
467
-        // Check if we need to update other participants
468
-        this.updateCheck();
469
-
470
         this.showSharedVideoMutedPopup(mute);
474
         this.showSharedVideoMutedPopup(mute);
471
     }
475
     }
472
 
476
 
475
      * by the user via the mike button and the volume of the shared video is on
479
      * by the user via the mike button and the volume of the shared video is on
476
      * we're unmuting the mike automatically.
480
      * we're unmuting the mike automatically.
477
      */
481
      */
478
-    smartUnmute() {
482
+    smartAudioUnmute() {
479
         if (APP.conference.isLocalAudioMuted()
483
         if (APP.conference.isLocalAudioMuted()
480
             && !this.mutedWithUserInteraction
484
             && !this.mutedWithUserInteraction
481
             && !this.isSharedVideoVolumeOn()) {
485
             && !this.isSharedVideoVolumeOn()) {
489
      * Smart mike mute. If the mike isn't currently muted and the shared video
493
      * Smart mike mute. If the mike isn't currently muted and the shared video
490
      * volume is on we mute the mike.
494
      * volume is on we mute the mike.
491
      */
495
      */
492
-    smartMute() {
496
+    smartAudioMute() {
493
         if (!APP.conference.isLocalAudioMuted()
497
         if (!APP.conference.isLocalAudioMuted()
494
             && this.isSharedVideoVolumeOn()) {
498
             && this.isSharedVideoVolumeOn()) {
495
 
499
 

Laden…
Annuleren
Opslaan