forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.js
1257 lines (1073 loc) · 36.2 KB
/
controls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
goog.provide('shaka.ui.Controls');
goog.provide('shaka.ui.ControlsPanel');
goog.require('shaka.log');
goog.require('shaka.ui.Constants');
goog.require('shaka.ui.Enums');
goog.require('shaka.ui.Locales');
goog.require('shaka.ui.Localization');
goog.require('shaka.ui.SeekBar');
goog.require('shaka.ui.Utils');
goog.require('shaka.util.Dom');
goog.require('shaka.util.EventManager');
goog.require('shaka.util.FakeEvent');
goog.require('shaka.util.FakeEventTarget');
goog.require('shaka.util.Timer');
/**
* A container for custom video controls.
* @implements {shaka.util.IDestroyable}
* @export
*/
shaka.ui.Controls = class extends shaka.util.FakeEventTarget {
/**
* @param {!shaka.Player} player
* @param {!HTMLElement} videoContainer
* @param {!HTMLMediaElement} video
* @param {shaka.extern.UIConfiguration} config
*/
constructor(player, videoContainer, video, config) {
super();
/** @private {boolean} */
this.enabled_ = true;
/** @private {boolean} */
this.overrideCssShowControls_ = false;
/** @private {shaka.extern.UIConfiguration} */
this.config_ = config;
/** @private {shaka.cast.CastProxy} */
this.castProxy_ = new shaka.cast.CastProxy(
video, player, this.config_.castReceiverAppId);
/** @private {boolean} */
this.castAllowed_ = true;
/** @private {HTMLMediaElement} */
this.video_ = this.castProxy_.getVideo();
/** @private {HTMLMediaElement} */
this.localVideo_ = video;
/** @private {shaka.Player} */
this.player_ = this.castProxy_.getPlayer();
/** @private {shaka.Player} */
this.localPlayer_ = player;
/** @private {!HTMLElement} */
this.videoContainer_ = videoContainer;
/** @private {shaka.ui.SeekBar} */
this.seekBar_ = null;
/** @private {boolean} */
this.isSeeking_ = false;
/** @private {!Array.<!Element>} */
this.settingsMenus_ = [];
/**
* This timer is used to detect when the user has stopped moving the mouse
* and we should fade out the ui.
*
* @private {shaka.util.Timer}
*/
this.mouseStillTimer_ = new shaka.util.Timer(() => {
this.onMouseStill_();
});
/**
* This timer will be used to hide all settings menus. When the timer ticks
* it will force all controls to invisible.
*
* Rather than calling the callback directly, |Controls| will always call it
* through the timer to avoid conflicts.
*
* @private {shaka.util.Timer}
*/
this.hideSettingsMenusTimer_ = new shaka.util.Timer(() => {
/** @type {function(!HTMLElement)} */
const hide = (control) => {
shaka.ui.Utils.setDisplay(control, /* visible= */ false);
};
for (const menu of this.settingsMenus_) {
hide(/** @type {!HTMLElement} */ (menu));
}
});
/**
* This timer is used to regularly update the time and seek range elements
* so that we are communicating the current state as accurately as possibly.
*
* Unlike the other timers, this timer does not "own" the callback because
* this timer is acting like a heartbeat.
*
* @private {shaka.util.Timer}
*/
this.timeAndSeekRangeTimer_ = new shaka.util.Timer(() => {
// Suppress timer-based updates if the controls are hidden.
if (this.isOpaque_()) {
this.updateTimeAndSeekRange_();
}
});
/** @private {?number} */
this.lastTouchEventTime_ = null;
/** @private {!Array.<!shaka.extern.IUIElement>} */
this.elements_ = [];
/** @private {shaka.ui.Localization} */
this.localization_ = shaka.ui.Controls.createLocalization_();
/** @private {shaka.util.EventManager} */
this.eventManager_ = new shaka.util.EventManager();
// Configure and create the layout of the controls
this.configure(this.config_);
this.addEventListeners_();
/**
* The pressed keys set is used to record which keys are currently pressed
* down, so we can know what keys are pressed at the same time.
* Used by the focusInsideOverflowMenu_() function.
* @private {!Set.<number>}
*/
this.pressedKeys_ = new Set();
// We might've missed a caststatuschanged event from the proxy between
// the controls creation and initializing. Run onCastStatusChange_()
// to ensure we have the casting state right.
this.onCastStatusChange_();
// Start this timer after we are finished initializing everything,
this.timeAndSeekRangeTimer_.tickEvery(/* seconds= */ 0.125);
}
/**
* @override
* @export
*/
async destroy() {
if (this.eventManager_) {
this.eventManager_.release();
this.eventManager_ = null;
}
if (this.mouseStillTimer_) {
this.mouseStillTimer_.stop();
this.mouseStillTimer_ = null;
}
if (this.hideSettingsMenusTimer_) {
this.hideSettingsMenusTimer_.stop();
this.hideSettingsMenusTimer_ = null;
}
if (this.timeAndSeekRangeTimer_) {
this.timeAndSeekRangeTimer_.stop();
this.timeAndSeekRangeTimer_ = null;
}
if (this.castProxy_) {
await this.castProxy_.destroy();
this.castProxy_ = null;
}
if (this.localPlayer_) {
await this.localPlayer_.destroy();
this.localPlayer_ = null;
}
this.player_ = null;
this.localVideo_ = null;
this.video_ = null;
this.releaseChildElements_();
this.localization_ = null;
this.pressedKeys_.clear();
}
/** @private */
releaseChildElements_() {
for (const element of this.elements_) {
element.release();
}
this.elements_ = [];
}
/**
* @event shaka.Controls.CastStatusChangedEvent
* @description Fired upon receiving a 'caststatuschanged' event from
* the cast proxy.
* @property {string} type
* 'caststatuschanged'
* @property {boolean} newStatus
* The new status of the application. True for 'is casting' and
* false otherwise.
* @exportDoc
*/
/**
* @event shaka.Controls.SubMenuOpenEvent
* @description Fired when one of the overflow submenus is opened
* (e. g. language/resolution/subtitle selection).
* @property {string} type
* 'submenuopen'
* @exportDoc
*/
/**
* @event shaka.Controls.CaptionSelectionUpdatedEvent
* @description Fired when the captions/subtitles menu has finished updating.
* @property {string} type
* 'captionselectionupdated'
* @exportDoc
*/
/**
* @event shaka.Controls.ResolutionSelectionUpdatedEvent
* @description Fired when the resolution menu has finished updating.
* @property {string} type
* 'resolutionselectionupdated'
* @exportDoc
*/
/**
* @event shaka.Controls.LanguageSelectionUpdatedEvent
* @description Fired when the audio language menu has finished updating.
* @property {string} type
* 'languageselectionupdated'
* @exportDoc
*/
/**
* @event shaka.Controls.ErrorEvent
* @description Fired when something went wrong with the controls.
* @property {string} type
* 'error'
* @property {!shaka.util.Error} detail
* An object which contains details on the error. The error's 'category'
* and 'code' properties will identify the specific error that occurred.
* In an uncompiled build, you can also use the 'message' and 'stack'
* properties to debug.
* @exportDoc
*/
/**
* @event shaka.Controls.TimeAndSeekRangeUpdatedEvent
* @description Fired when the time and seek range elements have finished
* updating.
* @property {string} type
* 'timeandseekrangeupdated'
* @exportDoc
*/
/**
* @event shaka.Controls.UIUpdatedEvent
* @description Fired after a call to ui.configure() once the UI has finished
* updating.
* @property {string} type
* 'uiupdated'
* @exportDoc
*/
/**
* @param {string} name
* @param {!shaka.extern.IUIElement.Factory} factory
* @export
*/
static registerElement(name, factory) {
shaka.ui.ControlsPanel.elementNamesToFactories_.set(name, factory);
}
/**
* This allows the application to inhibit casting.
*
* @param {boolean} allow
* @export
*/
allowCast(allow) {
this.castAllowed_ = allow;
this.onCastStatusChange_();
}
/**
* Used by the application to notify the controls that a load operation is
* complete. This allows the controls to recalculate play/paused state, which
* is important for platforms like Android where autoplay is disabled.
* @export
*/
loadComplete() {
// If we are on Android or if autoplay is false, video.paused should be
// true. Otherwise, video.paused is false and the content is autoplaying.
this.onPlayStateChange_();
}
/**
* @param {!shaka.extern.UIConfiguration} config
* @export
*/
configure(config) {
this.config_ = config;
this.castProxy_.changeReceiverId(config.castReceiverAppId);
// Deconstruct the old layout if applicable
if (this.seekBar_) {
this.seekBar_ = null;
}
if (this.playButton_) {
this.playButton_ = null;
}
if (this.controlsContainer_) {
shaka.util.Dom.removeAllChildren(this.controlsContainer_);
this.videoContainer_.removeChild(this.spinnerContainer_);
this.releaseChildElements_();
} else {
this.addControlsContainer_();
}
// Create the new layout
this.createDOM_();
// Init the play state
this.onPlayStateChange_();
// Elements that should not propagate clicks (controls panel, menus)
const noPropagationElements = this.videoContainer_.getElementsByClassName(
'shaka-no-propagation');
for (const element of noPropagationElements) {
const cb = (event) => event.stopPropagation();
this.eventManager_.listen(element, 'click', cb);
this.eventManager_.listen(element, 'dblclick', cb);
}
// Keep showing controls if one of those elements is hovered
const showControlsElements = this.videoContainer_.getElementsByClassName(
'shaka-show-controls-on-mouse-over');
for (const element of showControlsElements) {
this.eventManager_.listen(element, 'mouseover', () => {
this.overrideCssShowControls_ = true;
});
this.eventManager_.listen(element, 'mouseleave', () => {
this.overrideCssShowControls_ = false;
});
}
}
/**
* Enable or disable the custom controls. Enabling disables native
* browser controls.
*
* @param {boolean} enabled
* @export
*/
setEnabledShakaControls(enabled) {
this.enabled_ = enabled;
if (enabled) {
shaka.ui.Utils.setDisplay(this.controlsContainer_, true);
// Spinner lives outside of the main controls div
shaka.ui.Utils.setDisplay(
this.spinnerContainer_, this.player_.isBuffering());
// If we're hiding native controls, make sure the video element itself is
// not tab-navigable. Our custom controls will still be tab-navigable.
this.video_.tabIndex = -1;
this.video_.controls = false;
} else {
shaka.ui.Utils.setDisplay(this.controlsContainer_, false);
// Spinner lives outside of the main controls div
shaka.ui.Utils.setDisplay(this.spinnerContainer_, false);
}
// The effects of play state changes are inhibited while showing native
// browser controls. Recalculate that state now.
this.onPlayStateChange_();
}
/**
* Enable or disable native browser controls. Enabling disables shaka
* controls.
*
* @param {boolean} enabled
* @export
*/
setEnabledNativeControls(enabled) {
// If we enable the native controls, the element must be tab-navigable.
// If we disable the native controls, we want to make sure that the video
// element itself is not tab-navigable, so that the element is skipped over
// when tabbing through the page.
this.video_.controls = enabled;
this.video_.tabIndex = enabled ? 0 : -1;
if (enabled) {
this.setEnabledShakaControls(false);
}
}
/**
* @export
* @return {shaka.cast.CastProxy}
*/
getCastProxy() {
return this.castProxy_;
}
/**
* @return {shaka.ui.Localization}
* @export
*/
getLocalization() {
return this.localization_;
}
/**
* @return {!HTMLElement}
* @export
*/
getVideoContainer() {
return this.videoContainer_;
}
/**
* @return {HTMLMediaElement}
* @export
*/
getVideo() {
return this.video_;
}
/**
* @return {HTMLMediaElement}
* @export
*/
getLocalVideo() {
return this.localVideo_;
}
/**
* @return {shaka.Player}
* @export
*/
getPlayer() {
return this.player_;
}
/**
* @return {shaka.Player}
* @export
*/
getLocalPlayer() {
return this.localPlayer_;
}
/**
* @return {!HTMLElement}
* @export
*/
getControlsContainer() {
return this.controlsContainer_;
}
/**
* @return {!shaka.extern.UIConfiguration}
* @export
*/
getConfig() {
return this.config_;
}
/**
* @return {boolean}
* @export
*/
isSeeking() {
return this.isSeeking_;
}
/**
* @param {boolean} seeking
* @export
*/
setSeeking(seeking) {
this.isSeeking_ = seeking;
}
/**
* @return {boolean}
* @export
*/
isCastAllowed() {
return this.castAllowed_;
}
/**
* @return {number}
* @export
*/
getDisplayTime() {
return this.seekBar_ ? this.seekBar_.getValue() : this.video_.currentTime;
}
/**
* @param {?number} time
* @export
*/
setLastTouchEventTime(time) {
this.lastTouchEventTime_ = time;
}
/**
* Display controls even if css says overwise.
* Normally, controls opacity is controled by CSS, but there are
* a few special cases where we want controls to be displayed no
* matter what. For example, if the focus is on one of the settings
* menus. This method is called when we want to signal an exception
* to normal CSS opacity rules and keep the controls visible.
*
* @export
*/
overrideCssShowControls() {
this.overrideCssShowControls_ = true;
}
/**
* @return {boolean}
* @export
*/
anySettingsMenusAreOpen() {
return this.settingsMenus_.some(
(menu) => menu.classList.contains('shaka-displayed'));
}
/** @export */
hideSettingsMenus() {
this.hideSettingsMenusTimer_.tickNow();
}
/** @export */
async toggleFullScreen() {
if (document.fullscreenElement) {
document.exitFullscreen();
} else {
// If we are in PiP mode, leave PiP mode first.
try {
if (document.pictureInPictureElement) {
await document.exitPictureInPicture();
}
await this.videoContainer_.requestFullscreen();
} catch (error) {
this.dispatchEvent(new shaka.util.FakeEvent('error', {
detail: error,
}));
}
}
}
/** @private */
createDOM_() {
this.videoContainer_.classList.add('shaka-video-container');
this.video_.classList.add('shaka-video');
if (this.config_.addBigPlayButton) {
this.addPlayButton_();
}
this.addBufferingSpinner_();
this.addControlsButtonPanel_();
this.settingsMenus_ = Array.from(
this.videoContainer_.getElementsByClassName('shaka-settings-menu'));
if (this.config_.addSeekBar) {
this.seekBar_ = new shaka.ui.SeekBar(this.bottomControls_, this);
this.elements_.push(this.seekBar_);
} else {
// Settings menus need to be positioned lower if the seekbar is absent.
for (const menu of this.settingsMenus_) {
menu.classList.add('shaka-low-position');
}
}
}
/** @private */
addControlsContainer_() {
/** @private {!HTMLElement} */
this.controlsContainer_ = shaka.util.Dom.createHTMLElement('div');
this.controlsContainer_.classList.add('shaka-controls-container');
this.videoContainer_.appendChild(this.controlsContainer_);
this.eventManager_.listen(this.controlsContainer_, 'touchstart', (e) => {
this.onContainerTouch_(e);
}, {passive: false});
this.eventManager_.listen(this.controlsContainer_, 'click', () => {
this.onContainerClick_();
});
}
/** @private */
addPlayButton_() {
const playButtonContainer = shaka.util.Dom.createHTMLElement('div');
playButtonContainer.classList.add('shaka-play-button-container');
this.controlsContainer_.appendChild(playButtonContainer);
/** @private {shaka.ui.BigPlayButton} */
this.playButton_ =
new shaka.ui.BigPlayButton(playButtonContainer, this);
this.elements_.push(this.playButton_);
}
/** @private */
addBufferingSpinner_() {
/** @private {!HTMLElement} */
this.spinnerContainer_ = shaka.util.Dom.createHTMLElement('div');
this.spinnerContainer_.classList.add('shaka-spinner-container');
this.videoContainer_.appendChild(this.spinnerContainer_);
const spinner = shaka.util.Dom.createHTMLElement('div');
spinner.classList.add('shaka-spinner');
this.spinnerContainer_.appendChild(spinner);
// Svg elements have to be created with the svg xml namespace.
const xmlns = 'http://www.w3.org/2000/svg';
const svg =
/** @type {!HTMLElement} */(document.createElementNS(xmlns, 'svg'));
// NOTE: SVG elements do not have a classList on IE, so use setAttribute.
svg.setAttribute('class', 'shaka-spinner-svg');
svg.setAttribute('viewBox', '0 0 30 30');
spinner.appendChild(svg);
// These coordinates are relative to the SVG viewBox above. This is
// distinct from the actual display size in the page, since the "S" is for
// "Scalable." The radius of 14.5 is so that the edges of the 1-px-wide
// stroke will touch the edges of the viewBox.
const spinnerCircle = document.createElementNS(xmlns, 'circle');
spinnerCircle.setAttribute('class', 'shaka-spinner-path');
spinnerCircle.setAttribute('cx', '15');
spinnerCircle.setAttribute('cy', '15');
spinnerCircle.setAttribute('r', '14.5');
spinnerCircle.setAttribute('fill', 'none');
spinnerCircle.setAttribute('stroke-width', '1');
spinnerCircle.setAttribute('stroke-miterlimit', '10');
svg.appendChild(spinnerCircle);
}
/** @private */
addControlsButtonPanel_() {
/** @private {!HTMLElement} */
this.bottomControls_ = shaka.util.Dom.createHTMLElement('div');
this.bottomControls_.classList.add('shaka-bottom-controls');
this.controlsContainer_.appendChild(this.bottomControls_);
/** @private {!HTMLElement} */
this.controlsButtonPanel_ = shaka.util.Dom.createHTMLElement('div');
this.controlsButtonPanel_.classList.add('shaka-controls-button-panel');
this.controlsButtonPanel_.classList.add('shaka-no-propagation');
this.controlsButtonPanel_.classList.add(
'shaka-show-controls-on-mouse-over');
this.bottomControls_.appendChild(this.controlsButtonPanel_);
// Overflow menus are supposed to hide once you click elsewhere
// on the video element. The code in onContainerClick_ ensures that.
// However, clicks on controls panel don't propagate to the container,
// so we have to explicitly hide the menus onclick here.
this.eventManager_.listen(this.controlsButtonPanel_, 'click', () => {
this.hideSettingsMenus();
});
// Create the elements specified by controlPanelElements
for (const name of this.config_.controlPanelElements) {
if (shaka.ui.ControlsPanel.elementNamesToFactories_.get(name)) {
const factory =
shaka.ui.ControlsPanel.elementNamesToFactories_.get(name);
this.elements_.push(factory.create(this.controlsButtonPanel_, this));
} else {
shaka.log.alwaysWarn('Unrecognized control panel element requested:',
name);
}
}
}
/**
* Adds static event listeners. This should only add event listeners to
* things that don't change (e.g. Player). Dynamic elements (e.g. controls)
* should have their event listeners added when they are created.
*
* @private
*/
addEventListeners_() {
this.eventManager_.listen(this.player_, 'buffering', () => {
this.onBufferingStateChange_();
});
// Set the initial state, as well.
this.onBufferingStateChange_();
// Listen for key down events to detect tab and enable outline
// for focused elements.
this.eventManager_.listen(window, 'keydown', (e) => this.onKeyDown_(e));
this.eventManager_.listen(
this.controlsContainer_, 'dblclick', () => this.toggleFullScreen());
this.eventManager_.listen(this.video_, 'play', () => {
this.onPlayStateChange_();
});
this.eventManager_.listen(this.video_, 'pause', () => {
this.onPlayStateChange_();
});
// Since videos go into a paused state at the end, Chrome and Edge both fire
// the 'pause' event when a video ends. IE 11 only fires the 'ended' event.
this.eventManager_.listen(this.video_, 'ended', () => {
this.onPlayStateChange_();
});
this.eventManager_.listen(this.videoContainer_, 'mousemove', (e) => {
this.onMouseMove_(e);
});
this.eventManager_.listen(this.videoContainer_, 'touchmove', (e) => {
this.onMouseMove_(e);
}, {passive: true});
this.eventManager_.listen(this.videoContainer_, 'touchend', (e) => {
this.onMouseMove_(e);
}, {passive: true});
this.eventManager_.listen(this.videoContainer_, 'mouseleave', () => {
this.onMouseLeave_();
});
this.eventManager_.listen(this.castProxy_, 'caststatuschanged', () => {
this.onCastStatusChange_();
});
this.eventManager_.listen(this.videoContainer_, 'keyup', (e) => {
this.onKeyUp_(e);
});
if (screen.orientation) {
this.eventManager_.listen(screen.orientation, 'change', () => {
this.onScreenRotation_();
});
}
}
/**
* When a mobile device is rotated to landscape layout, and the video is
* loaded, make the demo app go into fullscreen.
* Similarly, exit fullscreen when the device is rotated to portrait layout.
* @private
*/
onScreenRotation_() {
if (!this.video_ ||
this.video_.readyState == 0 ||
this.castProxy_.isCasting()) { return; }
if (screen.orientation.type.includes('landscape') &&
!document.fullscreenElement) {
this.videoContainer_.requestFullscreen();
} else if (screen.orientation.type.includes('portrait') &&
document.fullscreenElement) {
document.exitFullscreen();
}
}
/**
* Hiding the cursor when the mouse stops moving seems to be the only
* decent UX in fullscreen mode. Since we can't use pure CSS for that,
* we use events both in and out of fullscreen mode.
* Showing the control bar when a key is pressed, and hiding it after some
* time.
* @param {!Event} event
* @private
*/
onMouseMove_(event) {
// Disable blue outline for focused elements for mouse navigation.
if (event.type == 'mousemove') {
this.controlsContainer_.classList.remove('shaka-keyboard-navigation');
}
if (event.type == 'touchstart' || event.type == 'touchmove' ||
event.type == 'touchend' || event.type == 'keyup') {
this.lastTouchEventTime_ = Date.now();
} else if (this.lastTouchEventTime_ + 1000 < Date.now()) {
// It has been a while since the last touch event, this is probably a real
// mouse moving, so treat it like a mouse.
this.lastTouchEventTime_ = null;
}
// When there is a touch, we can get a 'mousemove' event after touch events.
// This should be treated as part of the touch, which has already been
// handled.
if (this.lastTouchEventTime_ && event.type == 'mousemove') {
return;
}
// Use the cursor specified in the CSS file.
this.videoContainer_.style.cursor = '';
// Make sure we are not about to hide the settings menus and then force them
// open.
this.hideSettingsMenusTimer_.stop();
if (!this.isOpaque_()) {
// Only update the time and seek range on mouse movement if it's the very
// first movement and we're about to show the controls. Otherwise, the
// seek bar will be updated much more rapidly during mouse movement. Do
// this right before making it visible.
this.updateTimeAndSeekRange_();
this.setControlsOpacity_(shaka.ui.Enums.Opacity.OPAQUE);
}
// Hide the cursor when the mouse stops moving.
// Only applies while the cursor is over the video container.
this.mouseStillTimer_.stop();
// Only start a timeout on 'touchend' or for 'mousemove' with no touch
// events.
if (event.type == 'touchend' ||
event.type == 'keyup'|| !this.lastTouchEventTime_) {
this.mouseStillTimer_.tickAfter(/* seconds= */ 3);
}
}
/** @private */
onMouseLeave_() {
// We sometimes get 'mouseout' events with touches. Since we can never
// leave the video element when touching, ignore.
if (this.lastTouchEventTime_) {
return;
}
// Stop the timer and invoke the callback now to hide the controls. If we
// don't, the opacity style we set in onMouseMove_ will continue to override
// the opacity in CSS and force the controls to stay visible.
this.mouseStillTimer_.tickNow();
}
/**
* This callback is for when we are pretty sure that the mouse has stopped
* moving (aka the mouse is still). This method should only be called via
* |mouseStillTimer_|. If this behaviour needs to be invoked directly, use
* |mouseStillTimer_.tickNow()|.
*
* @private
*/
onMouseStill_() {
// Hide the cursor. (NOTE: not supported on IE)
this.videoContainer_.style.cursor = 'none';
// Keep showing the controls if video is paused or one of the control menus
// is hovered.
if ((this.video_.paused && !this.isSeeking_) ||
this.overrideCssShowControls_) {
this.setControlsOpacity_(shaka.ui.Enums.Opacity.OPAQUE);
} else {
this.setControlsOpacity_(shaka.ui.Enums.Opacity.TRANSPARENT);
}
}
/**
* @param {!Event} event
* @private
*/
onContainerTouch_(event) {
if (!this.video_.duration) {
// Can't play yet. Ignore.
return;
}
if (this.isOpaque_()) {
this.lastTouchEventTime_ = Date.now();
// The controls are showing.
// Let this event continue and become a click.
} else {
// The controls are hidden, so show them.
this.onMouseMove_(event);
// Stop this event from becoming a click event.
event.preventDefault();
}
}
/** @private */
onContainerClick_() {
if (!this.enabled_) {
return;
}
if (this.anySettingsMenusAreOpen()) {
this.hideSettingsMenusTimer_.tickNow();
} else {
this.onPlayPauseClick_();
}
}
/** @private */
onPlayPauseClick_() {
if (!this.enabled_) {
return;
}
if (!this.video_.duration) {
// Can't play yet. Ignore.
return;
}
this.player_.cancelTrickPlay();
if (this.video_.paused) {
this.video_.play();
} else {
this.video_.pause();
}
}
/** @private */
onCastStatusChange_() {
const isCasting = this.castProxy_.isCasting();
this.dispatchEvent(new shaka.util.FakeEvent('caststatuschanged', {
newStatus: isCasting,
}));
if (isCasting) {
this.controlsContainer_.setAttribute('casting', 'true');
} else {
this.controlsContainer_.removeAttribute('casting');
}
}
/** @private */
onPlayStateChange_() {
// On IE 11, a video may end without going into a paused state. To correct
// both the UI state and the state of the video tag itself, we explicitly
// pause the video if that happens.
if (this.video_.ended && !this.video_.paused) {
this.video_.pause();
}
}
/**
* Support controls with keyboard inputs.
* @param {!Event} event
* @private
*/