forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.js
1390 lines (1177 loc) · 39.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 LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.ui.Controls');
goog.provide('shaka.ui.ControlsPanel');
goog.require('shaka.log');
goog.require('shaka.ui.Constants');
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 {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.extern.IAdManager} */
this.adManager_ = this.player_.getAdManager();
/** @private {shaka.extern.IAd} */
this.ad_ = null;
/** @private {shaka.ui.SeekBar} */
this.seekBar_ = null;
/** @private {boolean} */
this.isSeeking_ = false;
/** @private {!Array.<!Element>} */
this.settingsMenus_ = [];
/**
* Individual controls which, when hovered or tab-focused, will force the
* controls to be shown.
* @private {!Array.<!Element>}
*/
this.showOnHoverControls_ = [];
/** @private {boolean} */
this.recentMouseMovement_ = false;
/**
* 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 is used to delay the fading of the UI.
*
* @private {shaka.util.Timer}
*/
this.fadeControlsTimer_ = new shaka.util.Timer(() => {
this.controlsContainer_.removeAttribute('shown');
// If there's an overflow menu open, keep it this way for a couple of
// seconds in case a user immediately initiates another mouse move to
// interact with the menus. If that didn't happen, go ahead and hide
// the menus.
this.hideSettingsMenusTimer_.tickAfter(/* seconds= */ 2);
});
/**
* 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.fadeControlsTimer_) {
this.fadeControlsTimer_.stop();
this.fadeControlsTimer_ = 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.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);
}
}
/**
* 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 {!HTMLElement}
* @export
*/
getAdContainer() {
return this.adContainer_;
}
/**
* @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;
}
/**
* @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) {
await document.exitFullscreen();
} else {
// If we are in PiP mode, leave PiP mode first.
try {
if (document.pictureInPictureElement) {
await document.exitPictureInPicture();
}
await this.videoContainer_.requestFullscreen({navigationUI: 'hide'});
} catch (error) {
this.dispatchEvent(new shaka.util.FakeEvent('error', {
detail: error,
}));
}
}
}
/** @export */
showAdUI() {
// TODO: other ad states (seek bar display etc)
shaka.ui.Utils.setDisplay(this.adPanel_, true);
}
/** @export */
hideAdUI() {
// TODO: other ad states (seek bar display etc)
shaka.ui.Utils.setDisplay(this.adPanel_, false);
}
/**
* Play or pause the current presentation.
*/
playPausePresentation() {
if (!this.enabled_) {
return;
}
if (!this.video_.duration) {
// Can't play yet. Ignore.
return;
}
this.player_.cancelTrickPlay();
if (this.presentationIsPaused()) {
this.video_.play();
} else {
this.video_.pause();
}
}
/**
* Play or pause the current ad.
*/
playPauseAd() {
if (this.ad_ && this.ad_.isPaused()) {
this.ad_.play();
} else if (this.ad_) {
this.ad_.pause();
}
}
/**
* Return true if the presentation is paused.
*
* @return {boolean}
*/
presentationIsPaused() {
// The video element is in a paused state while seeking, but we don't count
// that.
return this.video_.paused && !this.isSeeking();
}
/** @private */
createDOM_() {
this.videoContainer_.classList.add('shaka-video-container');
this.video_.classList.add('shaka-video');
this.addSkimContainer_();
if (this.config_.addBigPlayButton) {
this.addPlayButton_();
}
if (!this.spinnerContainer_) {
this.addBufferingSpinner_();
}
if (!this.adContainer_) {
this.addAdContainer_();
}
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');
}
}
this.showOnHoverControls_ = Array.from(
this.videoContainer_.getElementsByClassName(
'shaka-show-controls-on-mouse-over'));
}
/** @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 */
addSkimContainer_() {
// This is the container that gets styled by CSS to have the
// black gradient skim at the end of the controls.
const skimContainer = shaka.util.Dom.createHTMLElement('div');
skimContainer.classList.add('shaka-skim-container');
this.controlsContainer_.appendChild(skimContainer);
}
/** @private */
addAdContainer_() {
// Ad container. IMA will use this div to display client-side ads.
/** @private {!HTMLElement} */
this.adContainer_ = shaka.util.Dom.createHTMLElement('div');
this.adContainer_.classList.add('shaka-ad-container');
this.videoContainer_.appendChild(this.adContainer_);
}
/** @private */
addAdControls_() {
/** @private {!HTMLElement} */
this.adPanel_ = shaka.util.Dom.createHTMLElement('div');
this.adPanel_.classList.add('shaka-ad-controls');
shaka.ui.Utils.setDisplay(this.adPanel_, false);
this.bottomControls_.appendChild(this.adPanel_);
const adPosition = new shaka.ui.AdPosition(this.adPanel_, this);
this.elements_.push(adPosition);
const adCounter = new shaka.ui.AdCounter(this.adPanel_, this);
this.elements_.push(adCounter);
const spacer = new shaka.ui.Spacer(this.adPanel_, this);
this.elements_.push(spacer);
const skipButton = new shaka.ui.SkipAdButton(this.adPanel_, this);
this.elements_.push(skipButton);
}
/** @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.bottomControls_.classList.add('shaka-no-propagation');
this.controlsContainer_.appendChild(this.bottomControls_);
// Overflow menus are supposed to hide once you click elsewhere
// on the video element. The code in onContainerClick_ ensures that.
// However, clicks on the bottom controls don't propagate to the container,
// so we have to explicitly hide the menus onclick here.
this.eventManager_.listen(this.bottomControls_, 'click', () => {
this.hideSettingsMenus();
});
this.addAdControls_();
/** @private {!HTMLElement} */
this.controlsButtonPanel_ = shaka.util.Dom.createHTMLElement('div');
this.controlsButtonPanel_.classList.add('shaka-controls-button-panel');
this.controlsButtonPanel_.classList.add(
'shaka-show-controls-on-mouse-over');
this.bottomControls_.appendChild(this.controlsButtonPanel_);
// 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);
});
this.eventManager_.listen(
this.adManager_, shaka.ads.AdManager.AD_STARTED, (e) => {
this.ad_ = (/** @type {!Object} */ (e))['ad'];
this.showAdUI();
});
this.eventManager_.listen(
this.adManager_, shaka.ads.AdManager.AD_STOPPED, () => {
this.ad_ = null;
this.hideAdUI();
});
if (screen.orientation) {
this.eventManager_.listen(screen.orientation, 'change', async () => {
await this.onScreenRotation_();
});
}
this.eventManager_.listen(document, 'fullscreenchange', () => {
if (this.ad_) {
this.ad_.resize(this.video_.offsetWidth, this.video_.offsetHeight);
}
});
}
/**
* 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
*/
async onScreenRotation_() {
if (!this.video_ ||
this.video_.readyState == 0 ||
this.castProxy_.isCasting()) { return; }
if (screen.orientation.type.includes('landscape') &&
!document.fullscreenElement) {
await this.videoContainer_.requestFullscreen({navigationUI: 'hide'});
} else if (screen.orientation.type.includes('portrait') &&
document.fullscreenElement) {
await 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');
this.computeOpacity();
}
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 = '';
this.recentMouseMovement_ = true;
// 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.computeOpacity();
}