Source: ui/controls.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.Controls');
  7. goog.provide('shaka.ui.ControlsPanel');
  8. goog.require('goog.asserts');
  9. goog.require('shaka.ads.Utils');
  10. goog.require('shaka.cast.CastProxy');
  11. goog.require('shaka.log');
  12. goog.require('shaka.ui.AdCounter');
  13. goog.require('shaka.ui.AdPosition');
  14. goog.require('shaka.ui.BigPlayButton');
  15. goog.require('shaka.ui.ContextMenu');
  16. goog.require('shaka.ui.HiddenFastForwardButton');
  17. goog.require('shaka.ui.HiddenRewindButton');
  18. goog.require('shaka.ui.Locales');
  19. goog.require('shaka.ui.Localization');
  20. goog.require('shaka.ui.SeekBar');
  21. goog.require('shaka.ui.SkipAdButton');
  22. goog.require('shaka.ui.Utils');
  23. goog.require('shaka.ui.VRManager');
  24. goog.require('shaka.util.Dom');
  25. goog.require('shaka.util.EventManager');
  26. goog.require('shaka.util.FakeEvent');
  27. goog.require('shaka.util.FakeEventTarget');
  28. goog.require('shaka.util.IDestroyable');
  29. goog.require('shaka.util.Platform');
  30. goog.require('shaka.util.Timer');
  31. goog.requireType('shaka.Player');
  32. /**
  33. * A container for custom video controls.
  34. * @implements {shaka.util.IDestroyable}
  35. * @export
  36. */
  37. shaka.ui.Controls = class extends shaka.util.FakeEventTarget {
  38. /**
  39. * @param {!shaka.Player} player
  40. * @param {!HTMLElement} videoContainer
  41. * @param {!HTMLMediaElement} video
  42. * @param {?HTMLCanvasElement} vrCanvas
  43. * @param {shaka.extern.UIConfiguration} config
  44. */
  45. constructor(player, videoContainer, video, vrCanvas, config) {
  46. super();
  47. /** @private {boolean} */
  48. this.enabled_ = true;
  49. /** @private {shaka.extern.UIConfiguration} */
  50. this.config_ = config;
  51. /** @private {shaka.cast.CastProxy} */
  52. this.castProxy_ = new shaka.cast.CastProxy(
  53. video, player, this.config_.castReceiverAppId,
  54. this.config_.castAndroidReceiverCompatible);
  55. /** @private {boolean} */
  56. this.castAllowed_ = true;
  57. /** @private {HTMLMediaElement} */
  58. this.video_ = this.castProxy_.getVideo();
  59. /** @private {HTMLMediaElement} */
  60. this.localVideo_ = video;
  61. /** @private {shaka.Player} */
  62. this.player_ = this.castProxy_.getPlayer();
  63. /** @private {shaka.Player} */
  64. this.localPlayer_ = player;
  65. /** @private {!HTMLElement} */
  66. this.videoContainer_ = videoContainer;
  67. /** @private {?HTMLCanvasElement} */
  68. this.vrCanvas_ = vrCanvas;
  69. /** @private {shaka.extern.IAdManager} */
  70. this.adManager_ = this.player_.getAdManager();
  71. /** @private {?shaka.extern.IAd} */
  72. this.ad_ = null;
  73. /** @private {?shaka.extern.IUISeekBar} */
  74. this.seekBar_ = null;
  75. /** @private {boolean} */
  76. this.isSeeking_ = false;
  77. /** @private {!Array<!HTMLElement>} */
  78. this.menus_ = [];
  79. /**
  80. * Individual controls which, when hovered or tab-focused, will force the
  81. * controls to be shown.
  82. * @private {!Array<!Element>}
  83. */
  84. this.showOnHoverControls_ = [];
  85. /** @private {boolean} */
  86. this.recentMouseMovement_ = false;
  87. /**
  88. * This timer is used to detect when the user has stopped moving the mouse
  89. * and we should fade out the ui.
  90. *
  91. * @private {shaka.util.Timer}
  92. */
  93. this.mouseStillTimer_ = new shaka.util.Timer(() => {
  94. this.onMouseStill_();
  95. });
  96. /**
  97. * This timer is used to delay the fading of the UI.
  98. *
  99. * @private {shaka.util.Timer}
  100. */
  101. this.fadeControlsTimer_ = new shaka.util.Timer(() => {
  102. this.controlsContainer_.removeAttribute('shown');
  103. if (this.contextMenu_) {
  104. this.contextMenu_.closeMenu();
  105. }
  106. // If there's an overflow menu open, keep it this way for a couple of
  107. // seconds in case a user immediately initiates another mouse move to
  108. // interact with the menus. If that didn't happen, go ahead and hide
  109. // the menus.
  110. this.hideSettingsMenusTimer_.tickAfter(
  111. /* seconds= */ this.config_.closeMenusDelay);
  112. });
  113. /**
  114. * This timer will be used to hide all settings menus. When the timer ticks
  115. * it will force all controls to invisible.
  116. *
  117. * Rather than calling the callback directly, |Controls| will always call it
  118. * through the timer to avoid conflicts.
  119. *
  120. * @private {shaka.util.Timer}
  121. */
  122. this.hideSettingsMenusTimer_ = new shaka.util.Timer(() => {
  123. for (const menu of this.menus_) {
  124. shaka.ui.Utils.setDisplay(menu, /* visible= */ false);
  125. }
  126. });
  127. /**
  128. * This timer is used to regularly update the time and seek range elements
  129. * so that we are communicating the current state as accurately as possibly.
  130. *
  131. * Unlike the other timers, this timer does not "own" the callback because
  132. * this timer is acting like a heartbeat.
  133. *
  134. * @private {shaka.util.Timer}
  135. */
  136. this.timeAndSeekRangeTimer_ = new shaka.util.Timer(() => {
  137. // Suppress timer-based updates if the controls are hidden.
  138. if (this.isOpaque()) {
  139. this.updateTimeAndSeekRange_();
  140. }
  141. });
  142. /** @private {?number} */
  143. this.lastTouchEventTime_ = null;
  144. /** @private {!Array<!shaka.extern.IUIElement>} */
  145. this.elements_ = [];
  146. /** @private {shaka.ui.Localization} */
  147. this.localization_ = shaka.ui.Controls.createLocalization_();
  148. /** @private {shaka.util.EventManager} */
  149. this.eventManager_ = new shaka.util.EventManager();
  150. /** @private {?shaka.ui.VRManager} */
  151. this.vr_ = null;
  152. // Configure and create the layout of the controls
  153. this.configure(this.config_);
  154. this.addEventListeners_();
  155. this.setupMediaSession_();
  156. /**
  157. * The pressed keys set is used to record which keys are currently pressed
  158. * down, so we can know what keys are pressed at the same time.
  159. * Used by the focusInsideOverflowMenu_() function.
  160. * @private {!Set<string>}
  161. */
  162. this.pressedKeys_ = new Set();
  163. // We might've missed a caststatuschanged event from the proxy between
  164. // the controls creation and initializing. Run onCastStatusChange_()
  165. // to ensure we have the casting state right.
  166. this.onCastStatusChange_();
  167. // Start this timer after we are finished initializing everything,
  168. this.timeAndSeekRangeTimer_.tickEvery(this.config_.refreshTickInSeconds);
  169. this.eventManager_.listen(this.localization_,
  170. shaka.ui.Localization.LOCALE_CHANGED, (e) => {
  171. const locale = e['locales'][0];
  172. this.adManager_.setLocale(locale);
  173. this.videoContainer_.setAttribute('lang', locale);
  174. });
  175. this.adManager_.initInterstitial(
  176. this.getClientSideAdContainer(), this.localPlayer_, this.localVideo_);
  177. }
  178. /**
  179. * @override
  180. * @export
  181. */
  182. async destroy() {
  183. if (document.pictureInPictureElement == this.localVideo_) {
  184. await document.exitPictureInPicture();
  185. }
  186. if (this.eventManager_) {
  187. this.eventManager_.release();
  188. this.eventManager_ = null;
  189. }
  190. if (this.mouseStillTimer_) {
  191. this.mouseStillTimer_.stop();
  192. this.mouseStillTimer_ = null;
  193. }
  194. if (this.fadeControlsTimer_) {
  195. this.fadeControlsTimer_.stop();
  196. this.fadeControlsTimer_ = null;
  197. }
  198. if (this.hideSettingsMenusTimer_) {
  199. this.hideSettingsMenusTimer_.stop();
  200. this.hideSettingsMenusTimer_ = null;
  201. }
  202. if (this.timeAndSeekRangeTimer_) {
  203. this.timeAndSeekRangeTimer_.stop();
  204. this.timeAndSeekRangeTimer_ = null;
  205. }
  206. if (this.vr_) {
  207. this.vr_.release();
  208. this.vr_ = null;
  209. }
  210. // Important! Release all child elements before destroying the cast proxy
  211. // or player. This makes sure those destructions will not trigger event
  212. // listeners in the UI which would then invoke the cast proxy or player.
  213. this.releaseChildElements_();
  214. if (this.controlsContainer_) {
  215. this.videoContainer_.removeChild(this.controlsContainer_);
  216. this.controlsContainer_ = null;
  217. }
  218. if (this.castProxy_) {
  219. await this.castProxy_.destroy();
  220. this.castProxy_ = null;
  221. }
  222. if (this.spinnerContainer_) {
  223. this.videoContainer_.removeChild(this.spinnerContainer_);
  224. this.spinnerContainer_ = null;
  225. }
  226. if (this.clientAdContainer_) {
  227. this.videoContainer_.removeChild(this.clientAdContainer_);
  228. this.clientAdContainer_ = null;
  229. }
  230. if (this.localPlayer_) {
  231. await this.localPlayer_.destroy();
  232. this.localPlayer_ = null;
  233. }
  234. this.player_ = null;
  235. this.localVideo_ = null;
  236. this.video_ = null;
  237. this.localization_ = null;
  238. this.pressedKeys_.clear();
  239. this.removeMediaSession_();
  240. // FakeEventTarget implements IReleasable
  241. super.release();
  242. }
  243. /** @private */
  244. releaseChildElements_() {
  245. for (const element of this.elements_) {
  246. element.release();
  247. }
  248. this.elements_ = [];
  249. }
  250. /**
  251. * @param {string} name
  252. * @param {!shaka.extern.IUIElement.Factory} factory
  253. * @export
  254. */
  255. static registerElement(name, factory) {
  256. shaka.ui.ControlsPanel.elementNamesToFactories_.set(name, factory);
  257. }
  258. /**
  259. * @param {!shaka.extern.IUISeekBar.Factory} factory
  260. * @export
  261. */
  262. static registerSeekBar(factory) {
  263. shaka.ui.ControlsPanel.seekBarFactory_ = factory;
  264. }
  265. /**
  266. * This allows the application to inhibit casting.
  267. *
  268. * @param {boolean} allow
  269. * @export
  270. */
  271. allowCast(allow) {
  272. this.castAllowed_ = allow;
  273. this.onCastStatusChange_();
  274. }
  275. /**
  276. * Used by the application to notify the controls that a load operation is
  277. * complete. This allows the controls to recalculate play/paused state, which
  278. * is important for platforms like Android where autoplay is disabled.
  279. * @export
  280. */
  281. loadComplete() {
  282. // If we are on Android or if autoplay is false, video.paused should be
  283. // true. Otherwise, video.paused is false and the content is autoplaying.
  284. this.onPlayStateChange_();
  285. }
  286. /**
  287. * @param {!shaka.extern.UIConfiguration} config
  288. * @export
  289. */
  290. configure(config) {
  291. this.config_ = config;
  292. this.castProxy_.changeReceiverId(config.castReceiverAppId,
  293. config.castAndroidReceiverCompatible);
  294. // Deconstruct the old layout if applicable
  295. if (this.seekBar_) {
  296. this.seekBar_ = null;
  297. }
  298. if (this.playButton_) {
  299. this.playButton_ = null;
  300. }
  301. if (this.contextMenu_) {
  302. this.contextMenu_ = null;
  303. }
  304. if (this.vr_) {
  305. this.vr_.configure(config);
  306. }
  307. if (this.controlsContainer_) {
  308. shaka.util.Dom.removeAllChildren(this.controlsContainer_);
  309. this.releaseChildElements_();
  310. } else {
  311. this.addControlsContainer_();
  312. // The client-side ad container is only created once, and is never
  313. // re-created or uprooted in the DOM, even when the DOM is re-created,
  314. // since that seemingly breaks the IMA SDK.
  315. this.addClientAdContainer_();
  316. goog.asserts.assert(
  317. this.controlsContainer_, 'Should have a controlsContainer_!');
  318. goog.asserts.assert(this.localVideo_, 'Should have a localVideo_!');
  319. goog.asserts.assert(this.player_, 'Should have a player_!');
  320. this.vr_ = new shaka.ui.VRManager(this.controlsContainer_, this.vrCanvas_,
  321. this.localVideo_, this.player_, this.config_);
  322. }
  323. // Create the new layout
  324. this.createDOM_();
  325. // Init the play state
  326. this.onPlayStateChange_();
  327. // Elements that should not propagate clicks (controls panel, menus)
  328. const noPropagationElements = this.videoContainer_.getElementsByClassName(
  329. 'shaka-no-propagation');
  330. for (const element of noPropagationElements) {
  331. const cb = (event) => event.stopPropagation();
  332. this.eventManager_.listen(element, 'click', cb);
  333. this.eventManager_.listen(element, 'dblclick', cb);
  334. }
  335. }
  336. /**
  337. * Enable or disable the custom controls. Enabling disables native
  338. * browser controls.
  339. *
  340. * @param {boolean} enabled
  341. * @export
  342. */
  343. setEnabledShakaControls(enabled) {
  344. this.enabled_ = enabled;
  345. if (enabled) {
  346. this.videoContainer_.setAttribute('shaka-controls', 'true');
  347. // If we're hiding native controls, make sure the video element itself is
  348. // not tab-navigable. Our custom controls will still be tab-navigable.
  349. this.localVideo_.tabIndex = -1;
  350. this.localVideo_.controls = false;
  351. } else {
  352. this.videoContainer_.removeAttribute('shaka-controls');
  353. }
  354. // The effects of play state changes are inhibited while showing native
  355. // browser controls. Recalculate that state now.
  356. this.onPlayStateChange_();
  357. }
  358. /**
  359. * Enable or disable native browser controls. Enabling disables shaka
  360. * controls.
  361. *
  362. * @param {boolean} enabled
  363. * @export
  364. */
  365. setEnabledNativeControls(enabled) {
  366. // If we enable the native controls, the element must be tab-navigable.
  367. // If we disable the native controls, we want to make sure that the video
  368. // element itself is not tab-navigable, so that the element is skipped over
  369. // when tabbing through the page.
  370. this.localVideo_.controls = enabled;
  371. this.localVideo_.tabIndex = enabled ? 0 : -1;
  372. if (enabled) {
  373. this.setEnabledShakaControls(false);
  374. }
  375. }
  376. /**
  377. * @export
  378. * @return {?shaka.extern.IAd}
  379. */
  380. getAd() {
  381. return this.ad_;
  382. }
  383. /**
  384. * @export
  385. * @return {shaka.cast.CastProxy}
  386. */
  387. getCastProxy() {
  388. return this.castProxy_;
  389. }
  390. /**
  391. * @return {shaka.ui.Localization}
  392. * @export
  393. */
  394. getLocalization() {
  395. return this.localization_;
  396. }
  397. /**
  398. * @return {!HTMLElement}
  399. * @export
  400. */
  401. getVideoContainer() {
  402. return this.videoContainer_;
  403. }
  404. /**
  405. * @return {HTMLMediaElement}
  406. * @export
  407. */
  408. getVideo() {
  409. return this.video_;
  410. }
  411. /**
  412. * @return {HTMLMediaElement}
  413. * @export
  414. */
  415. getLocalVideo() {
  416. return this.localVideo_;
  417. }
  418. /**
  419. * @return {shaka.Player}
  420. * @export
  421. */
  422. getPlayer() {
  423. return this.player_;
  424. }
  425. /**
  426. * @return {shaka.Player}
  427. * @export
  428. */
  429. getLocalPlayer() {
  430. return this.localPlayer_;
  431. }
  432. /**
  433. * @return {!HTMLElement}
  434. * @export
  435. */
  436. getControlsContainer() {
  437. goog.asserts.assert(
  438. this.controlsContainer_, 'No controls container after destruction!');
  439. return this.controlsContainer_;
  440. }
  441. /**
  442. * @return {!HTMLElement}
  443. * @export
  444. */
  445. getServerSideAdContainer() {
  446. return this.daiAdContainer_;
  447. }
  448. /**
  449. * @return {!HTMLElement}
  450. * @export
  451. */
  452. getClientSideAdContainer() {
  453. goog.asserts.assert(
  454. this.clientAdContainer_, 'No client ad container after destruction!');
  455. return this.clientAdContainer_;
  456. }
  457. /**
  458. * @return {!shaka.extern.UIConfiguration}
  459. * @export
  460. */
  461. getConfig() {
  462. return this.config_;
  463. }
  464. /**
  465. * @return {boolean}
  466. * @export
  467. */
  468. isSeeking() {
  469. return this.isSeeking_;
  470. }
  471. /**
  472. * @param {boolean} seeking
  473. * @export
  474. */
  475. setSeeking(seeking) {
  476. this.isSeeking_ = seeking;
  477. }
  478. /**
  479. * @return {boolean}
  480. * @export
  481. */
  482. isCastAllowed() {
  483. return this.castAllowed_;
  484. }
  485. /**
  486. * @return {number}
  487. * @export
  488. */
  489. getDisplayTime() {
  490. return this.seekBar_ ? this.seekBar_.getValue() : this.video_.currentTime;
  491. }
  492. /**
  493. * @param {?number} time
  494. * @export
  495. */
  496. setLastTouchEventTime(time) {
  497. this.lastTouchEventTime_ = time;
  498. }
  499. /**
  500. * @return {boolean}
  501. * @export
  502. */
  503. anySettingsMenusAreOpen() {
  504. return this.menus_.some(
  505. (menu) => !menu.classList.contains('shaka-hidden'));
  506. }
  507. /** @export */
  508. hideSettingsMenus() {
  509. this.hideSettingsMenusTimer_.tickNow();
  510. }
  511. /**
  512. * @return {boolean}
  513. * @private
  514. */
  515. shouldUseDocumentFullscreen_() {
  516. if (!document.fullscreenEnabled) {
  517. return false;
  518. }
  519. // When the preferVideoFullScreenInVisionOS configuration value applies,
  520. // we avoid using document fullscreen, even if it is available.
  521. const video = /** @type {HTMLVideoElement} */(this.localVideo_);
  522. if (video.webkitSupportsFullscreen) {
  523. if (this.config_.preferVideoFullScreenInVisionOS &&
  524. shaka.util.Platform.isVisionOS()) {
  525. return false;
  526. }
  527. }
  528. return true;
  529. }
  530. /**
  531. * @return {boolean}
  532. * @export
  533. */
  534. isFullScreenSupported() {
  535. if (this.shouldUseDocumentFullscreen_()) {
  536. return true;
  537. }
  538. if (!this.ad_ || !this.ad_.isUsingAnotherMediaElement()) {
  539. const video = /** @type {HTMLVideoElement} */(this.localVideo_);
  540. if (video.webkitSupportsFullscreen) {
  541. return true;
  542. }
  543. }
  544. return false;
  545. }
  546. /**
  547. * @return {boolean}
  548. * @export
  549. */
  550. isFullScreenEnabled() {
  551. if (this.shouldUseDocumentFullscreen_()) {
  552. return !!document.fullscreenElement;
  553. }
  554. const video = /** @type {HTMLVideoElement} */(this.localVideo_);
  555. if (video.webkitSupportsFullscreen) {
  556. return video.webkitDisplayingFullscreen;
  557. }
  558. return false;
  559. }
  560. /** @private */
  561. async enterFullScreen_() {
  562. try {
  563. if (this.shouldUseDocumentFullscreen_()) {
  564. if (document.pictureInPictureElement) {
  565. await document.exitPictureInPicture();
  566. }
  567. const fullScreenElement = this.config_.fullScreenElement;
  568. await fullScreenElement.requestFullscreen({navigationUI: 'hide'});
  569. if (this.config_.forceLandscapeOnFullscreen && screen.orientation) {
  570. // Locking to 'landscape' should let it be either
  571. // 'landscape-primary' or 'landscape-secondary' as appropriate.
  572. // We ignore errors from this specific call, since it creates noise
  573. // on desktop otherwise.
  574. try {
  575. await screen.orientation.lock('landscape');
  576. } catch (error) {}
  577. }
  578. } else {
  579. const video = /** @type {HTMLVideoElement} */(this.localVideo_);
  580. if (video.webkitSupportsFullscreen) {
  581. video.webkitEnterFullscreen();
  582. }
  583. }
  584. } catch (error) {
  585. // Entering fullscreen can fail without user interaction.
  586. this.dispatchEvent(new shaka.util.FakeEvent(
  587. 'error', (new Map()).set('detail', error)));
  588. }
  589. }
  590. /** @private */
  591. async exitFullScreen_() {
  592. if (this.shouldUseDocumentFullscreen_()) {
  593. if (screen.orientation) {
  594. screen.orientation.unlock();
  595. }
  596. await document.exitFullscreen();
  597. } else {
  598. const video = /** @type {HTMLVideoElement} */(this.localVideo_);
  599. if (video.webkitSupportsFullscreen) {
  600. video.webkitExitFullscreen();
  601. }
  602. }
  603. }
  604. /** @export */
  605. async toggleFullScreen() {
  606. if (this.isFullScreenEnabled()) {
  607. await this.exitFullScreen_();
  608. } else {
  609. await this.enterFullScreen_();
  610. }
  611. }
  612. /**
  613. * @return {boolean}
  614. * @export
  615. */
  616. isPiPAllowed() {
  617. if (this.castProxy_.isCasting()) {
  618. return false;
  619. }
  620. if ('documentPictureInPicture' in window &&
  621. this.config_.preferDocumentPictureInPicture) {
  622. const video = /** @type {HTMLVideoElement} */(this.localVideo_);
  623. return !video.disablePictureInPicture;
  624. }
  625. if (document.pictureInPictureEnabled) {
  626. const video = /** @type {HTMLVideoElement} */(this.localVideo_);
  627. return !video.disablePictureInPicture;
  628. }
  629. return false;
  630. }
  631. /**
  632. * @return {boolean}
  633. * @export
  634. */
  635. isPiPEnabled() {
  636. if ('documentPictureInPicture' in window &&
  637. this.config_.preferDocumentPictureInPicture) {
  638. return !!window.documentPictureInPicture.window;
  639. } else {
  640. return !!document.pictureInPictureElement;
  641. }
  642. }
  643. /** @export */
  644. async togglePiP() {
  645. try {
  646. if ('documentPictureInPicture' in window &&
  647. this.config_.preferDocumentPictureInPicture) {
  648. await this.toggleDocumentPictureInPicture_();
  649. } else if (!document.pictureInPictureElement) {
  650. // If you were fullscreen, leave fullscreen first.
  651. if (document.fullscreenElement) {
  652. document.exitFullscreen();
  653. }
  654. const video = /** @type {HTMLVideoElement} */(this.localVideo_);
  655. await video.requestPictureInPicture();
  656. } else {
  657. await document.exitPictureInPicture();
  658. }
  659. } catch (error) {
  660. this.dispatchEvent(new shaka.util.FakeEvent(
  661. 'error', (new Map()).set('detail', error)));
  662. }
  663. }
  664. /**
  665. * The Document Picture-in-Picture API makes it possible to open an
  666. * always-on-top window that can be populated with arbitrary HTML content.
  667. * https://developer.chrome.com/docs/web-platform/document-picture-in-picture
  668. * @private
  669. */
  670. async toggleDocumentPictureInPicture_() {
  671. // Close Picture-in-Picture window if any.
  672. if (window.documentPictureInPicture.window) {
  673. window.documentPictureInPicture.window.close();
  674. return;
  675. }
  676. // Open a Picture-in-Picture window.
  677. const pipPlayer = this.videoContainer_;
  678. const rectPipPlayer = pipPlayer.getBoundingClientRect();
  679. const pipWindow = await window.documentPictureInPicture.requestWindow({
  680. width: rectPipPlayer.width,
  681. height: rectPipPlayer.height,
  682. });
  683. // Copy style sheets to the Picture-in-Picture window.
  684. this.copyStyleSheetsToWindow_(pipWindow);
  685. // Add placeholder for the player.
  686. const parentPlayer = pipPlayer.parentNode || document.body;
  687. const placeholder = this.videoContainer_.cloneNode(true);
  688. placeholder.style.visibility = 'hidden';
  689. placeholder.style.height = getComputedStyle(pipPlayer).height;
  690. parentPlayer.appendChild(placeholder);
  691. // Make sure player fits in the Picture-in-Picture window.
  692. const styles = document.createElement('style');
  693. styles.append(`[data-shaka-player-container] {
  694. width: 100% !important; max-height: 100%}`);
  695. pipWindow.document.head.append(styles);
  696. // Move player to the Picture-in-Picture window.
  697. pipWindow.document.body.append(pipPlayer);
  698. // Listen for the PiP closing event to move the player back.
  699. this.eventManager_.listenOnce(pipWindow, 'pagehide', () => {
  700. placeholder.replaceWith(/** @type {!Node} */(pipPlayer));
  701. });
  702. }
  703. /** @private */
  704. copyStyleSheetsToWindow_(win) {
  705. const styleSheets = /** @type {!Iterable<*>} */(document.styleSheets);
  706. const allCSS = [...styleSheets]
  707. .map((sheet) => {
  708. try {
  709. return [...sheet.cssRules].map((rule) => rule.cssText).join('');
  710. } catch (e) {
  711. const link = /** @type {!HTMLLinkElement} */(
  712. document.createElement('link'));
  713. link.rel = 'stylesheet';
  714. link.type = sheet.type;
  715. link.media = sheet.media;
  716. link.href = sheet.href;
  717. win.document.head.appendChild(link);
  718. }
  719. return '';
  720. })
  721. .filter(Boolean)
  722. .join('\n');
  723. const style = document.createElement('style');
  724. style.textContent = allCSS;
  725. win.document.head.appendChild(style);
  726. }
  727. /** @export */
  728. showAdUI() {
  729. shaka.ui.Utils.setDisplay(this.adPanel_, true);
  730. shaka.ui.Utils.setDisplay(this.clientAdContainer_, true);
  731. if (this.ad_.hasCustomClick()) {
  732. this.controlsContainer_.setAttribute('ad-active', 'true');
  733. } else {
  734. this.controlsContainer_.removeAttribute('ad-active');
  735. }
  736. }
  737. /** @export */
  738. hideAdUI() {
  739. shaka.ui.Utils.setDisplay(this.adPanel_, false);
  740. shaka.ui.Utils.setDisplay(this.clientAdContainer_, false);
  741. this.controlsContainer_.removeAttribute('ad-active');
  742. }
  743. /**
  744. * Play or pause the current presentation.
  745. */
  746. playPausePresentation() {
  747. if (!this.enabled_) {
  748. return;
  749. }
  750. if (this.ad_) {
  751. this.playPauseAd();
  752. if (this.ad_.isLinear()) {
  753. return;
  754. }
  755. }
  756. if (!this.video_.duration) {
  757. // Can't play yet. Ignore.
  758. return;
  759. }
  760. if (this.presentationIsPaused()) {
  761. // If we are at the end, go back to the beginning.
  762. if (this.player_.isEnded()) {
  763. this.video_.currentTime = this.player_.seekRange().start;
  764. }
  765. this.video_.play();
  766. } else {
  767. this.video_.pause();
  768. }
  769. }
  770. /**
  771. * Play or pause the current ad.
  772. */
  773. playPauseAd() {
  774. if (this.ad_ && this.ad_.isPaused()) {
  775. this.ad_.play();
  776. } else if (this.ad_) {
  777. this.ad_.pause();
  778. }
  779. }
  780. /**
  781. * Return true if the presentation is paused.
  782. *
  783. * @return {boolean}
  784. */
  785. presentationIsPaused() {
  786. // The video element is in a paused state while seeking, but we don't count
  787. // that.
  788. return this.video_.paused && !this.isSeeking();
  789. }
  790. /** @private */
  791. createDOM_() {
  792. this.videoContainer_.classList.add('shaka-video-container');
  793. this.localVideo_.classList.add('shaka-video');
  794. this.addScrimContainer_();
  795. if (this.config_.addBigPlayButton) {
  796. this.addPlayButton_();
  797. }
  798. if (this.config_.customContextMenu) {
  799. this.addContextMenu_();
  800. }
  801. if (!this.spinnerContainer_) {
  802. this.addBufferingSpinner_();
  803. }
  804. if (this.config_.seekOnTaps) {
  805. this.addFastForwardButtonOnControlsContainer_();
  806. this.addRewindButtonOnControlsContainer_();
  807. }
  808. this.addDaiAdContainer_();
  809. this.addControlsButtonPanel_();
  810. this.menus_ = Array.from(
  811. this.videoContainer_.getElementsByClassName('shaka-settings-menu'));
  812. this.menus_.push(...Array.from(
  813. this.videoContainer_.getElementsByClassName('shaka-overflow-menu')));
  814. this.addSeekBar_();
  815. this.showOnHoverControls_ = Array.from(
  816. this.videoContainer_.getElementsByClassName(
  817. 'shaka-show-controls-on-mouse-over'));
  818. }
  819. /** @private */
  820. addControlsContainer_() {
  821. /** @private {HTMLElement} */
  822. this.controlsContainer_ = shaka.util.Dom.createHTMLElement('div');
  823. this.controlsContainer_.classList.add('shaka-controls-container');
  824. this.videoContainer_.appendChild(this.controlsContainer_);
  825. // Use our controls by default, without anyone calling
  826. // setEnabledShakaControls:
  827. this.videoContainer_.setAttribute('shaka-controls', 'true');
  828. this.eventManager_.listen(this.controlsContainer_, 'touchend', (e) => {
  829. this.onContainerTouch_(e);
  830. });
  831. this.eventManager_.listen(this.controlsContainer_, 'click', () => {
  832. this.onContainerClick_();
  833. });
  834. this.eventManager_.listen(this.controlsContainer_, 'dblclick', () => {
  835. if (this.config_.doubleClickForFullscreen &&
  836. this.isFullScreenSupported()) {
  837. this.toggleFullScreen();
  838. }
  839. });
  840. }
  841. /** @private */
  842. addPlayButton_() {
  843. const playButtonContainer = shaka.util.Dom.createHTMLElement('div');
  844. playButtonContainer.classList.add('shaka-play-button-container');
  845. this.controlsContainer_.appendChild(playButtonContainer);
  846. /** @private {shaka.ui.BigPlayButton} */
  847. this.playButton_ =
  848. new shaka.ui.BigPlayButton(playButtonContainer, this);
  849. this.elements_.push(this.playButton_);
  850. }
  851. /** @private */
  852. addContextMenu_() {
  853. /** @private {shaka.ui.ContextMenu} */
  854. this.contextMenu_ =
  855. new shaka.ui.ContextMenu(this.controlsButtonPanel_, this);
  856. this.elements_.push(this.contextMenu_);
  857. }
  858. /** @private */
  859. addScrimContainer_() {
  860. // This is the container that gets styled by CSS to have the
  861. // black gradient scrim at the end of the controls.
  862. const scrimContainer = shaka.util.Dom.createHTMLElement('div');
  863. scrimContainer.classList.add('shaka-scrim-container');
  864. this.controlsContainer_.appendChild(scrimContainer);
  865. }
  866. /** @private */
  867. addAdControls_() {
  868. /** @private {!HTMLElement} */
  869. this.adPanel_ = shaka.util.Dom.createHTMLElement('div');
  870. this.adPanel_.classList.add('shaka-ad-controls');
  871. const showAdPanel = this.ad_ != null && this.ad_.isLinear();
  872. shaka.ui.Utils.setDisplay(this.adPanel_, showAdPanel);
  873. this.bottomControls_.appendChild(this.adPanel_);
  874. const adPosition = new shaka.ui.AdPosition(this.adPanel_, this);
  875. this.elements_.push(adPosition);
  876. const adCounter = new shaka.ui.AdCounter(this.adPanel_, this);
  877. this.elements_.push(adCounter);
  878. const skipButton = new shaka.ui.SkipAdButton(this.adPanel_, this);
  879. this.elements_.push(skipButton);
  880. }
  881. /** @private */
  882. addBufferingSpinner_() {
  883. /** @private {HTMLElement} */
  884. this.spinnerContainer_ = shaka.util.Dom.createHTMLElement('div');
  885. this.spinnerContainer_.classList.add('shaka-spinner-container');
  886. this.videoContainer_.appendChild(this.spinnerContainer_);
  887. const spinner = shaka.util.Dom.createHTMLElement('div');
  888. spinner.classList.add('shaka-spinner');
  889. this.spinnerContainer_.appendChild(spinner);
  890. // Svg elements have to be created with the svg xml namespace.
  891. const xmlns = 'http://www.w3.org/2000/svg';
  892. const svg =
  893. /** @type {!HTMLElement} */(document.createElementNS(xmlns, 'svg'));
  894. svg.classList.add('shaka-spinner-svg');
  895. svg.setAttribute('viewBox', '0 0 30 30');
  896. spinner.appendChild(svg);
  897. // These coordinates are relative to the SVG viewBox above. This is
  898. // distinct from the actual display size in the page, since the "S" is for
  899. // "Scalable." The radius of 14.5 is so that the edges of the 1-px-wide
  900. // stroke will touch the edges of the viewBox.
  901. const spinnerCircle = document.createElementNS(xmlns, 'circle');
  902. spinnerCircle.classList.add('shaka-spinner-path');
  903. spinnerCircle.setAttribute('cx', '15');
  904. spinnerCircle.setAttribute('cy', '15');
  905. spinnerCircle.setAttribute('r', '14.5');
  906. spinnerCircle.setAttribute('fill', 'none');
  907. spinnerCircle.setAttribute('stroke-width', '1');
  908. spinnerCircle.setAttribute('stroke-miterlimit', '10');
  909. svg.appendChild(spinnerCircle);
  910. }
  911. /**
  912. * Add fast-forward button on Controls container for moving video some
  913. * seconds ahead when the video is tapped more than once, video seeks ahead
  914. * some seconds for every extra tap.
  915. * @private
  916. */
  917. addFastForwardButtonOnControlsContainer_() {
  918. const hiddenFastForwardContainer = shaka.util.Dom.createHTMLElement('div');
  919. hiddenFastForwardContainer.classList.add(
  920. 'shaka-hidden-fast-forward-container');
  921. this.controlsContainer_.appendChild(hiddenFastForwardContainer);
  922. /** @private {shaka.ui.HiddenFastForwardButton} */
  923. this.hiddenFastForwardButton_ =
  924. new shaka.ui.HiddenFastForwardButton(hiddenFastForwardContainer, this);
  925. this.elements_.push(this.hiddenFastForwardButton_);
  926. }
  927. /**
  928. * Add Rewind button on Controls container for moving video some seconds
  929. * behind when the video is tapped more than once, video seeks behind some
  930. * seconds for every extra tap.
  931. * @private
  932. */
  933. addRewindButtonOnControlsContainer_() {
  934. const hiddenRewindContainer = shaka.util.Dom.createHTMLElement('div');
  935. hiddenRewindContainer.classList.add(
  936. 'shaka-hidden-rewind-container');
  937. this.controlsContainer_.appendChild(hiddenRewindContainer);
  938. /** @private {shaka.ui.HiddenRewindButton} */
  939. this.hiddenRewindButton_ =
  940. new shaka.ui.HiddenRewindButton(hiddenRewindContainer, this);
  941. this.elements_.push(this.hiddenRewindButton_);
  942. }
  943. /** @private */
  944. addControlsButtonPanel_() {
  945. /** @private {!HTMLElement} */
  946. this.bottomControls_ = shaka.util.Dom.createHTMLElement('div');
  947. this.bottomControls_.classList.add('shaka-bottom-controls');
  948. this.bottomControls_.classList.add('shaka-no-propagation');
  949. this.controlsContainer_.appendChild(this.bottomControls_);
  950. // Overflow menus are supposed to hide once you click elsewhere
  951. // on the page. The click event listener on window ensures that.
  952. // However, clicks on the bottom controls don't propagate to the container,
  953. // so we have to explicitly hide the menus onclick here.
  954. this.eventManager_.listen(this.bottomControls_, 'click', (e) => {
  955. // We explicitly deny this measure when clicking on buttons that
  956. // open submenus in the control panel.
  957. if (!e.target['closest']('.shaka-overflow-button')) {
  958. this.hideSettingsMenus();
  959. }
  960. });
  961. this.addAdControls_();
  962. /** @private {!HTMLElement} */
  963. this.controlsButtonPanel_ = shaka.util.Dom.createHTMLElement('div');
  964. this.controlsButtonPanel_.classList.add('shaka-controls-button-panel');
  965. this.controlsButtonPanel_.classList.add(
  966. 'shaka-show-controls-on-mouse-over');
  967. if (this.config_.enableTooltips) {
  968. this.controlsButtonPanel_.classList.add('shaka-tooltips-on');
  969. }
  970. this.bottomControls_.appendChild(this.controlsButtonPanel_);
  971. // Create the elements specified by controlPanelElements
  972. for (const name of this.config_.controlPanelElements) {
  973. if (shaka.ui.ControlsPanel.elementNamesToFactories_.get(name)) {
  974. const factory =
  975. shaka.ui.ControlsPanel.elementNamesToFactories_.get(name);
  976. const element = factory.create(this.controlsButtonPanel_, this);
  977. this.elements_.push(element);
  978. } else {
  979. shaka.log.alwaysWarn('Unrecognized control panel element requested:',
  980. name);
  981. }
  982. }
  983. }
  984. /**
  985. * Adds a container for server side ad UI with IMA SDK.
  986. *
  987. * @private
  988. */
  989. addDaiAdContainer_() {
  990. /** @private {!HTMLElement} */
  991. this.daiAdContainer_ = shaka.util.Dom.createHTMLElement('div');
  992. this.daiAdContainer_.classList.add('shaka-server-side-ad-container');
  993. this.controlsContainer_.appendChild(this.daiAdContainer_);
  994. }
  995. /**
  996. * Adds a seekbar depending on the configuration.
  997. * By default an instance of shaka.ui.SeekBar is created
  998. * This behaviour can be overridden by providing a SeekBar factory using the
  999. * registerSeekBarFactory function.
  1000. *
  1001. * @private
  1002. */
  1003. addSeekBar_() {
  1004. if (this.config_.addSeekBar) {
  1005. this.seekBar_ = shaka.ui.ControlsPanel.seekBarFactory_.create(
  1006. this.bottomControls_, this);
  1007. this.elements_.push(this.seekBar_);
  1008. } else {
  1009. // Settings menus need to be positioned lower if the seekbar is absent.
  1010. for (const menu of this.menus_) {
  1011. menu.classList.add('shaka-low-position');
  1012. }
  1013. }
  1014. }
  1015. /**
  1016. * Adds a container for client side ad UI with IMA SDK.
  1017. *
  1018. * @private
  1019. */
  1020. addClientAdContainer_() {
  1021. /** @private {HTMLElement} */
  1022. this.clientAdContainer_ = shaka.util.Dom.createHTMLElement('div');
  1023. this.clientAdContainer_.classList.add('shaka-client-side-ad-container');
  1024. shaka.ui.Utils.setDisplay(this.clientAdContainer_, false);
  1025. this.eventManager_.listen(this.clientAdContainer_, 'click', () => {
  1026. this.onContainerClick_();
  1027. });
  1028. this.videoContainer_.appendChild(this.clientAdContainer_);
  1029. }
  1030. /**
  1031. * Adds static event listeners. This should only add event listeners to
  1032. * things that don't change (e.g. Player). Dynamic elements (e.g. controls)
  1033. * should have their event listeners added when they are created.
  1034. *
  1035. * @private
  1036. */
  1037. addEventListeners_() {
  1038. this.eventManager_.listen(this.player_, 'buffering', () => {
  1039. this.onBufferingStateChange_();
  1040. });
  1041. // Set the initial state, as well.
  1042. this.onBufferingStateChange_();
  1043. // Listen for key down events to detect tab and enable outline
  1044. // for focused elements.
  1045. this.eventManager_.listen(window, 'keydown', (e) => {
  1046. this.onWindowKeyDown_(/** @type {!KeyboardEvent} */(e));
  1047. });
  1048. // Listen for click events to dismiss the settings menus.
  1049. this.eventManager_.listen(window, 'click', () => this.hideSettingsMenus());
  1050. // Avoid having multiple submenus open at the same time.
  1051. this.eventManager_.listen(
  1052. this, 'submenuopen', () => {
  1053. this.hideSettingsMenus();
  1054. });
  1055. this.eventManager_.listen(this.video_, 'play', () => {
  1056. this.onPlayStateChange_();
  1057. });
  1058. this.eventManager_.listen(this.video_, 'pause', () => {
  1059. this.onPlayStateChange_();
  1060. });
  1061. this.eventManager_.listen(this.videoContainer_, 'mousemove', (e) => {
  1062. this.onMouseMove_(e);
  1063. });
  1064. this.eventManager_.listen(this.videoContainer_, 'touchmove', (e) => {
  1065. this.onMouseMove_(e);
  1066. }, {passive: true});
  1067. this.eventManager_.listen(this.videoContainer_, 'touchend', (e) => {
  1068. this.onMouseMove_(e);
  1069. }, {passive: true});
  1070. this.eventManager_.listen(this.videoContainer_, 'mouseleave', () => {
  1071. this.onMouseLeave_();
  1072. });
  1073. this.eventManager_.listen(this.castProxy_, 'caststatuschanged', () => {
  1074. this.onCastStatusChange_();
  1075. });
  1076. this.eventManager_.listen(this.vr_, 'vrstatuschanged', () => {
  1077. this.dispatchEvent(new shaka.util.FakeEvent('vrstatuschanged'));
  1078. });
  1079. this.eventManager_.listen(this.videoContainer_, 'keydown', (e) => {
  1080. this.onControlsKeyDown_(/** @type {!KeyboardEvent} */(e));
  1081. });
  1082. this.eventManager_.listen(this.videoContainer_, 'keyup', (e) => {
  1083. this.onControlsKeyUp_(/** @type {!KeyboardEvent} */(e));
  1084. });
  1085. this.eventManager_.listen(
  1086. this.adManager_, shaka.ads.Utils.AD_STARTED, (e) => {
  1087. this.ad_ = (/** @type {!Object} */ (e))['ad'];
  1088. this.showAdUI();
  1089. this.onBufferingStateChange_();
  1090. });
  1091. this.eventManager_.listen(
  1092. this.adManager_, shaka.ads.Utils.AD_STOPPED, () => {
  1093. this.ad_ = null;
  1094. this.hideAdUI();
  1095. this.onBufferingStateChange_();
  1096. });
  1097. if (screen.orientation) {
  1098. this.eventManager_.listen(screen.orientation, 'change', async () => {
  1099. await this.onScreenRotation_();
  1100. });
  1101. }
  1102. }
  1103. /**
  1104. * @private
  1105. */
  1106. setupMediaSession_() {
  1107. if (!this.config_.setupMediaSession || !navigator.mediaSession) {
  1108. return;
  1109. }
  1110. const addMediaSessionHandler = (type, callback) => {
  1111. try {
  1112. navigator.mediaSession.setActionHandler(type, (details) => {
  1113. callback(details);
  1114. });
  1115. } catch (error) {
  1116. shaka.log.debug(
  1117. `The "${type}" media session action is not supported.`);
  1118. }
  1119. };
  1120. const updatePositionState = () => {
  1121. if (this.ad_ && this.ad_.isLinear()) {
  1122. clearPositionState();
  1123. return;
  1124. }
  1125. const seekRange = this.player_.seekRange();
  1126. let duration = seekRange.end - seekRange.start;
  1127. const position = parseFloat(
  1128. (this.video_.currentTime - seekRange.start).toFixed(2));
  1129. if (this.player_.isLive() && Math.abs(duration - position) < 1) {
  1130. // Positive infinity indicates media without a defined end, such as a
  1131. // live stream.
  1132. duration = Infinity;
  1133. }
  1134. try {
  1135. navigator.mediaSession.setPositionState({
  1136. duration: Math.max(0, duration),
  1137. playbackRate: this.video_.playbackRate,
  1138. position: Math.max(0, position),
  1139. });
  1140. } catch (error) {
  1141. shaka.log.v2(
  1142. 'setPositionState in media session is not supported.');
  1143. }
  1144. };
  1145. const clearPositionState = () => {
  1146. try {
  1147. navigator.mediaSession.setPositionState();
  1148. } catch (error) {
  1149. shaka.log.v2(
  1150. 'setPositionState in media session is not supported.');
  1151. }
  1152. };
  1153. const commonHandler = (details) => {
  1154. const keyboardSeekDistance = this.config_.keyboardSeekDistance;
  1155. switch (details.action) {
  1156. case 'pause':
  1157. this.playPausePresentation();
  1158. break;
  1159. case 'play':
  1160. this.playPausePresentation();
  1161. break;
  1162. case 'seekbackward':
  1163. if (details.seekOffset && !isFinite(details.seekOffset)) {
  1164. break;
  1165. }
  1166. if (!this.ad_ || !this.ad_.isLinear()) {
  1167. this.seek_(this.seekBar_.getValue() -
  1168. (details.seekOffset || keyboardSeekDistance));
  1169. }
  1170. break;
  1171. case 'seekforward':
  1172. if (details.seekOffset && !isFinite(details.seekOffset)) {
  1173. break;
  1174. }
  1175. if (!this.ad_ || !this.ad_.isLinear()) {
  1176. this.seek_(this.seekBar_.getValue() +
  1177. (details.seekOffset || keyboardSeekDistance));
  1178. }
  1179. break;
  1180. case 'seekto':
  1181. if (details.seekTime && !isFinite(details.seekTime)) {
  1182. break;
  1183. }
  1184. if (!this.ad_ || !this.ad_.isLinear()) {
  1185. this.seek_(this.player_.seekRange().start + details.seekTime);
  1186. }
  1187. break;
  1188. case 'stop':
  1189. this.player_.unload();
  1190. break;
  1191. case 'enterpictureinpicture':
  1192. if (!this.ad_ || !this.ad_.isLinear()) {
  1193. this.togglePiP();
  1194. }
  1195. break;
  1196. }
  1197. };
  1198. addMediaSessionHandler('pause', commonHandler);
  1199. addMediaSessionHandler('play', commonHandler);
  1200. addMediaSessionHandler('seekbackward', commonHandler);
  1201. addMediaSessionHandler('seekforward', commonHandler);
  1202. addMediaSessionHandler('seekto', commonHandler);
  1203. addMediaSessionHandler('stop', commonHandler);
  1204. if ('documentPictureInPicture' in window ||
  1205. document.pictureInPictureEnabled) {
  1206. addMediaSessionHandler('enterpictureinpicture', commonHandler);
  1207. }
  1208. const playerLoaded = () => {
  1209. if (this.player_.isLive() || this.player_.seekRange().start != 0) {
  1210. updatePositionState();
  1211. this.eventManager_.listen(
  1212. this.video_, 'timeupdate', updatePositionState);
  1213. } else {
  1214. clearPositionState();
  1215. }
  1216. };
  1217. const playerUnloading = () => {
  1218. this.eventManager_.unlisten(
  1219. this.video_, 'timeupdate', updatePositionState);
  1220. };
  1221. if (this.player_.isFullyLoaded()) {
  1222. playerLoaded();
  1223. }
  1224. this.eventManager_.listen(this.player_, 'loaded', playerLoaded);
  1225. this.eventManager_.listen(this.player_, 'unloading', playerUnloading);
  1226. this.eventManager_.listen(this.player_, 'metadata', (event) => {
  1227. const payload = event['payload'];
  1228. if (!payload) {
  1229. return;
  1230. }
  1231. let title;
  1232. if (payload['key'] == 'TIT2' && payload['data']) {
  1233. title = payload['data'];
  1234. }
  1235. let imageUrl;
  1236. if (payload['key'] == 'APIC' && payload['mimeType'] == '-->') {
  1237. imageUrl = payload['data'];
  1238. }
  1239. if (title) {
  1240. let metadata = {
  1241. title: title,
  1242. artwork: [],
  1243. };
  1244. if (navigator.mediaSession.metadata) {
  1245. metadata = navigator.mediaSession.metadata;
  1246. metadata.title = title;
  1247. }
  1248. navigator.mediaSession.metadata = new MediaMetadata(metadata);
  1249. }
  1250. if (imageUrl) {
  1251. const video = /** @type {HTMLVideoElement} */ (this.localVideo_);
  1252. if (imageUrl != video.poster) {
  1253. video.poster = imageUrl;
  1254. }
  1255. let metadata = {
  1256. title: '',
  1257. artwork: [{src: imageUrl}],
  1258. };
  1259. if (navigator.mediaSession.metadata) {
  1260. metadata = navigator.mediaSession.metadata;
  1261. metadata.artwork = [{src: imageUrl}];
  1262. }
  1263. navigator.mediaSession.metadata = new MediaMetadata(metadata);
  1264. }
  1265. });
  1266. }
  1267. /**
  1268. * @private
  1269. */
  1270. removeMediaSession_() {
  1271. if (!this.config_.setupMediaSession || !navigator.mediaSession) {
  1272. return;
  1273. }
  1274. try {
  1275. navigator.mediaSession.setPositionState();
  1276. } catch (error) {}
  1277. const disableMediaSessionHandler = (type) => {
  1278. try {
  1279. navigator.mediaSession.setActionHandler(type, null);
  1280. } catch (error) {}
  1281. };
  1282. disableMediaSessionHandler('pause');
  1283. disableMediaSessionHandler('play');
  1284. disableMediaSessionHandler('seekbackward');
  1285. disableMediaSessionHandler('seekforward');
  1286. disableMediaSessionHandler('seekto');
  1287. disableMediaSessionHandler('stop');
  1288. disableMediaSessionHandler('enterpictureinpicture');
  1289. }
  1290. /**
  1291. * When a mobile device is rotated to landscape layout, and the video is
  1292. * loaded, make the demo app go into fullscreen.
  1293. * Similarly, exit fullscreen when the device is rotated to portrait layout.
  1294. * @private
  1295. */
  1296. async onScreenRotation_() {
  1297. if (!this.video_ ||
  1298. this.video_.readyState == 0 ||
  1299. this.castProxy_.isCasting() ||
  1300. !this.config_.enableFullscreenOnRotation ||
  1301. !this.isFullScreenSupported()) {
  1302. return;
  1303. }
  1304. if (screen.orientation.type.includes('landscape') &&
  1305. !this.isFullScreenEnabled()) {
  1306. await this.enterFullScreen_();
  1307. } else if (screen.orientation.type.includes('portrait') &&
  1308. this.isFullScreenEnabled()) {
  1309. await this.exitFullScreen_();
  1310. }
  1311. }
  1312. /**
  1313. * Hiding the cursor when the mouse stops moving seems to be the only
  1314. * decent UX in fullscreen mode. Since we can't use pure CSS for that,
  1315. * we use events both in and out of fullscreen mode.
  1316. * Showing the control bar when a key is pressed, and hiding it after some
  1317. * time.
  1318. * @param {!Event} event
  1319. * @private
  1320. */
  1321. onMouseMove_(event) {
  1322. // Disable blue outline for focused elements for mouse navigation.
  1323. if (event.type == 'mousemove') {
  1324. this.controlsContainer_.classList.remove('shaka-keyboard-navigation');
  1325. this.computeOpacity();
  1326. }
  1327. if (event.type == 'touchstart' || event.type == 'touchmove' ||
  1328. event.type == 'touchend' || event.type == 'keyup') {
  1329. this.lastTouchEventTime_ = Date.now();
  1330. } else if (this.lastTouchEventTime_ + 1000 < Date.now()) {
  1331. // It has been a while since the last touch event, this is probably a real
  1332. // mouse moving, so treat it like a mouse.
  1333. this.lastTouchEventTime_ = null;
  1334. }
  1335. // When there is a touch, we can get a 'mousemove' event after touch events.
  1336. // This should be treated as part of the touch, which has already been
  1337. // handled.
  1338. if (this.lastTouchEventTime_ && event.type == 'mousemove') {
  1339. return;
  1340. }
  1341. // Use the cursor specified in the CSS file.
  1342. this.videoContainer_.classList.remove('no-cursor');
  1343. this.recentMouseMovement_ = true;
  1344. // Make sure we are not about to hide the settings menus and then force them
  1345. // open.
  1346. this.hideSettingsMenusTimer_.stop();
  1347. if (!this.isOpaque()) {
  1348. // Only update the time and seek range on mouse movement if it's the very
  1349. // first movement and we're about to show the controls. Otherwise, the
  1350. // seek bar will be updated much more rapidly during mouse movement. Do
  1351. // this right before making it visible.
  1352. this.updateTimeAndSeekRange_();
  1353. this.computeOpacity();
  1354. }
  1355. // Hide the cursor when the mouse stops moving.
  1356. // Only applies while the cursor is over the video container.
  1357. this.mouseStillTimer_.stop();
  1358. // Only start a timeout on 'touchend' or for 'mousemove' with no touch
  1359. // events.
  1360. if (event.type == 'touchend' ||
  1361. event.type == 'keyup'|| !this.lastTouchEventTime_) {
  1362. this.mouseStillTimer_.tickAfter(/* seconds= */ 3);
  1363. }
  1364. }
  1365. /** @private */
  1366. onMouseLeave_() {
  1367. // We sometimes get 'mouseout' events with touches. Since we can never
  1368. // leave the video element when touching, ignore.
  1369. if (this.lastTouchEventTime_) {
  1370. return;
  1371. }
  1372. // Stop the timer and invoke the callback now to hide the controls. If we
  1373. // don't, the opacity style we set in onMouseMove_ will continue to override
  1374. // the opacity in CSS and force the controls to stay visible.
  1375. this.mouseStillTimer_.tickNow();
  1376. }
  1377. /**
  1378. * This callback is for when we are pretty sure that the mouse has stopped
  1379. * moving (aka the mouse is still). This method should only be called via
  1380. * |mouseStillTimer_|. If this behaviour needs to be invoked directly, use
  1381. * |mouseStillTimer_.tickNow()|.
  1382. *
  1383. * @private
  1384. */
  1385. onMouseStill_() {
  1386. // Hide the cursor.
  1387. this.videoContainer_.classList.add('no-cursor');
  1388. this.recentMouseMovement_ = false;
  1389. this.computeOpacity();
  1390. }
  1391. /**
  1392. * @return {boolean} true if any relevant elements are hovered.
  1393. * @private
  1394. */
  1395. isHovered_() {
  1396. if (!window.matchMedia('hover: hover').matches) {
  1397. // This is primarily a touch-screen device, so the :hover query below
  1398. // doesn't make sense. In spite of this, the :hover query on an element
  1399. // can still return true on such a device after a touch ends.
  1400. // See https://bit.ly/34dBORX for details.
  1401. return false;
  1402. }
  1403. return this.showOnHoverControls_.some((element) => {
  1404. return element.matches(':hover');
  1405. });
  1406. }
  1407. /**
  1408. * Recompute whether the controls should be shown or hidden.
  1409. */
  1410. computeOpacity() {
  1411. const adIsPaused = this.ad_ ? this.ad_.isPaused() : false;
  1412. const videoIsPaused = this.video_.paused && !this.isSeeking_;
  1413. const keyboardNavigationMode = this.controlsContainer_.classList.contains(
  1414. 'shaka-keyboard-navigation');
  1415. // Keep showing the controls if the ad or video is paused, there has been
  1416. // recent mouse movement, we're in keyboard navigation, or one of a special
  1417. // class of elements is hovered.
  1418. if (adIsPaused ||
  1419. ((!this.ad_ || !this.ad_.isLinear()) && videoIsPaused) ||
  1420. this.recentMouseMovement_ ||
  1421. keyboardNavigationMode ||
  1422. this.isHovered_()) {
  1423. // Make sure the state is up-to-date before showing it.
  1424. this.updateTimeAndSeekRange_();
  1425. this.controlsContainer_.setAttribute('shown', 'true');
  1426. this.fadeControlsTimer_.stop();
  1427. } else {
  1428. this.fadeControlsTimer_.tickAfter(/* seconds= */ this.config_.fadeDelay);
  1429. }
  1430. }
  1431. /**
  1432. * @param {!Event} event
  1433. * @private
  1434. */
  1435. onContainerTouch_(event) {
  1436. if (!this.video_.duration) {
  1437. // Can't play yet. Ignore.
  1438. return;
  1439. }
  1440. if (this.isOpaque()) {
  1441. this.lastTouchEventTime_ = Date.now();
  1442. // The controls are showing.
  1443. // Let this event continue and become a click.
  1444. } else {
  1445. // The controls are hidden, so show them.
  1446. this.onMouseMove_(event);
  1447. // Stop this event from becoming a click event.
  1448. event.cancelable && event.preventDefault();
  1449. }
  1450. }
  1451. /** @private */
  1452. onContainerClick_() {
  1453. if (!this.enabled_ || this.isPlayingVR()) {
  1454. return;
  1455. }
  1456. if (this.anySettingsMenusAreOpen()) {
  1457. this.hideSettingsMenusTimer_.tickNow();
  1458. } else if (this.config_.singleClickForPlayAndPause) {
  1459. this.playPausePresentation();
  1460. }
  1461. }
  1462. /** @private */
  1463. onCastStatusChange_() {
  1464. const isCasting = this.castProxy_.isCasting();
  1465. this.dispatchEvent(new shaka.util.FakeEvent(
  1466. 'caststatuschanged', (new Map()).set('newStatus', isCasting)));
  1467. if (isCasting) {
  1468. this.controlsContainer_.setAttribute('casting', 'true');
  1469. } else {
  1470. this.controlsContainer_.removeAttribute('casting');
  1471. }
  1472. }
  1473. /** @private */
  1474. onPlayStateChange_() {
  1475. this.computeOpacity();
  1476. }
  1477. /**
  1478. * Support controls with keyboard inputs.
  1479. * @param {!KeyboardEvent} event
  1480. * @private
  1481. */
  1482. onControlsKeyDown_(event) {
  1483. const activeElement = document.activeElement;
  1484. const isVolumeBar = activeElement && activeElement.classList ?
  1485. activeElement.classList.contains('shaka-volume-bar') : false;
  1486. const isSeekBar = activeElement && activeElement.classList &&
  1487. activeElement.classList.contains('shaka-seek-bar');
  1488. // Show the control panel if it is on focus or any button is pressed.
  1489. if (this.controlsContainer_.contains(activeElement)) {
  1490. this.onMouseMove_(event);
  1491. }
  1492. if (!this.config_.enableKeyboardPlaybackControls) {
  1493. return;
  1494. }
  1495. const keyboardSeekDistance = this.config_.keyboardSeekDistance;
  1496. const keyboardLargeSeekDistance = this.config_.keyboardLargeSeekDistance;
  1497. switch (event.key) {
  1498. case 'ArrowLeft':
  1499. // If it's not focused on the volume bar, move the seek time backward
  1500. // for a few sec. Otherwise, the volume will be adjusted automatically.
  1501. if (this.seekBar_ && isSeekBar && !isVolumeBar &&
  1502. keyboardSeekDistance > 0) {
  1503. event.preventDefault();
  1504. this.seek_(this.seekBar_.getValue() - keyboardSeekDistance);
  1505. }
  1506. break;
  1507. case 'ArrowRight':
  1508. // If it's not focused on the volume bar, move the seek time forward
  1509. // for a few sec. Otherwise, the volume will be adjusted automatically.
  1510. if (this.seekBar_ && isSeekBar && !isVolumeBar &&
  1511. keyboardSeekDistance > 0) {
  1512. event.preventDefault();
  1513. this.seek_(this.seekBar_.getValue() + keyboardSeekDistance);
  1514. }
  1515. break;
  1516. case 'PageDown':
  1517. // PageDown is like ArrowLeft, but has a larger jump distance, and does
  1518. // nothing to volume.
  1519. if (this.seekBar_ && isSeekBar && keyboardSeekDistance > 0) {
  1520. event.preventDefault();
  1521. this.seek_(this.seekBar_.getValue() - keyboardLargeSeekDistance);
  1522. }
  1523. break;
  1524. case 'PageUp':
  1525. // PageDown is like ArrowRight, but has a larger jump distance, and does
  1526. // nothing to volume.
  1527. if (this.seekBar_ && isSeekBar && keyboardSeekDistance > 0) {
  1528. event.preventDefault();
  1529. this.seek_(this.seekBar_.getValue() + keyboardLargeSeekDistance);
  1530. }
  1531. break;
  1532. // Jump to the beginning of the video's seek range.
  1533. case 'Home':
  1534. if (this.seekBar_) {
  1535. this.seek_(this.player_.seekRange().start);
  1536. }
  1537. break;
  1538. // Jump to the end of the video's seek range.
  1539. case 'End':
  1540. if (this.seekBar_) {
  1541. this.seek_(this.player_.seekRange().end);
  1542. }
  1543. break;
  1544. case 'f':
  1545. if (this.isFullScreenSupported()) {
  1546. this.toggleFullScreen();
  1547. }
  1548. break;
  1549. case 'm':
  1550. if (this.ad_ && this.ad_.isLinear()) {
  1551. this.ad_.setMuted(!this.ad_.isMuted());
  1552. } else {
  1553. this.localVideo_.muted = !this.localVideo_.muted;
  1554. }
  1555. break;
  1556. case 'p':
  1557. if (this.isPiPAllowed()) {
  1558. this.togglePiP();
  1559. }
  1560. break;
  1561. // Pause or play by pressing space on the seek bar.
  1562. case ' ':
  1563. if (isSeekBar) {
  1564. this.playPausePresentation();
  1565. }
  1566. break;
  1567. }
  1568. }
  1569. /**
  1570. * Support controls with keyboard inputs.
  1571. * @param {!KeyboardEvent} event
  1572. * @private
  1573. */
  1574. onControlsKeyUp_(event) {
  1575. // When the key is released, remove it from the pressed keys set.
  1576. this.pressedKeys_.delete(event.key);
  1577. }
  1578. /**
  1579. * Called both as an event listener and directly by the controls to initialize
  1580. * the buffering state.
  1581. * @private
  1582. */
  1583. onBufferingStateChange_() {
  1584. if (!this.enabled_) {
  1585. return;
  1586. }
  1587. if (this.ad_ && this.ad_.isClientRendering() && this.ad_.isLinear()) {
  1588. shaka.ui.Utils.setDisplay(this.spinnerContainer_, false);
  1589. return;
  1590. }
  1591. shaka.ui.Utils.setDisplay(
  1592. this.spinnerContainer_, this.player_.isBuffering());
  1593. }
  1594. /**
  1595. * @return {boolean}
  1596. * @export
  1597. */
  1598. isOpaque() {
  1599. if (!this.enabled_) {
  1600. return false;
  1601. }
  1602. return this.controlsContainer_.getAttribute('shown') != null ||
  1603. this.controlsContainer_.getAttribute('casting') != null;
  1604. }
  1605. /**
  1606. * Update the video's current time based on the keyboard operations.
  1607. *
  1608. * @param {number} currentTime
  1609. * @private
  1610. */
  1611. seek_(currentTime) {
  1612. goog.asserts.assert(
  1613. this.seekBar_, 'Caller of seek_ must check for seekBar_ first!');
  1614. this.video_.currentTime = currentTime;
  1615. this.updateTimeAndSeekRange_();
  1616. }
  1617. /**
  1618. * Called when the seek range or current time need to be updated.
  1619. * @private
  1620. */
  1621. updateTimeAndSeekRange_() {
  1622. if (this.seekBar_) {
  1623. this.seekBar_.setValue(this.video_.currentTime);
  1624. this.seekBar_.update();
  1625. if (this.seekBar_.isShowing()) {
  1626. for (const menu of this.menus_) {
  1627. menu.classList.remove('shaka-low-position');
  1628. }
  1629. } else {
  1630. for (const menu of this.menus_) {
  1631. menu.classList.add('shaka-low-position');
  1632. }
  1633. }
  1634. }
  1635. this.dispatchEvent(new shaka.util.FakeEvent('timeandseekrangeupdated'));
  1636. }
  1637. /**
  1638. * Add behaviors for keyboard navigation.
  1639. * 1. Add blue outline for focused elements.
  1640. * 2. Allow exiting overflow settings menus by pressing Esc key.
  1641. * 3. When navigating on overflow settings menu by pressing Tab
  1642. * key or Shift+Tab keys keep the focus inside overflow menu.
  1643. *
  1644. * @param {!KeyboardEvent} event
  1645. * @private
  1646. */
  1647. onWindowKeyDown_(event) {
  1648. // Add the key to the pressed keys set when it's pressed.
  1649. this.pressedKeys_.add(event.key);
  1650. const anySettingsMenusAreOpen = this.anySettingsMenusAreOpen();
  1651. if (event.key == 'Tab') {
  1652. // Enable blue outline for focused elements for keyboard
  1653. // navigation.
  1654. this.controlsContainer_.classList.add('shaka-keyboard-navigation');
  1655. this.computeOpacity();
  1656. this.eventManager_.listen(window, 'mousedown', () => this.onMouseDown_());
  1657. }
  1658. // If escape key was pressed, close any open settings menus.
  1659. if (event.key == 'Escape') {
  1660. this.hideSettingsMenusTimer_.tickNow();
  1661. }
  1662. if (anySettingsMenusAreOpen && this.pressedKeys_.has('Tab')) {
  1663. // If Tab key or Shift+Tab keys are pressed when navigating through
  1664. // an overflow settings menu, keep the focus to loop inside the
  1665. // overflow menu.
  1666. this.keepFocusInMenu_(event);
  1667. }
  1668. }
  1669. /**
  1670. * When the user is using keyboard to navigate inside the overflow settings
  1671. * menu (pressing Tab key to go forward, or pressing Shift + Tab keys to go
  1672. * backward), make sure it's focused only on the elements of the overflow
  1673. * panel.
  1674. *
  1675. * This is called by onWindowKeyDown_() function, when there's a settings
  1676. * overflow menu open, and the Tab key / Shift+Tab keys are pressed.
  1677. *
  1678. * @param {!Event} event
  1679. * @private
  1680. */
  1681. keepFocusInMenu_(event) {
  1682. const openSettingsMenus = this.menus_.filter(
  1683. (menu) => !menu.classList.contains('shaka-hidden'));
  1684. if (!openSettingsMenus.length) {
  1685. // For example, this occurs when you hit escape to close the menu.
  1686. return;
  1687. }
  1688. const settingsMenu = openSettingsMenus[0];
  1689. if (settingsMenu.childNodes.length) {
  1690. // Get the first and the last displaying child element from the overflow
  1691. // menu.
  1692. let firstShownChild = settingsMenu.firstElementChild;
  1693. while (firstShownChild &&
  1694. firstShownChild.classList.contains('shaka-hidden')) {
  1695. firstShownChild = firstShownChild.nextElementSibling;
  1696. }
  1697. let lastShownChild = settingsMenu.lastElementChild;
  1698. while (lastShownChild &&
  1699. lastShownChild.classList.contains('shaka-hidden')) {
  1700. lastShownChild = lastShownChild.previousElementSibling;
  1701. }
  1702. const activeElement = document.activeElement;
  1703. // When only Tab key is pressed, navigate to the next element.
  1704. // If it's currently focused on the last shown child element of the
  1705. // overflow menu, let the focus move to the first child element of the
  1706. // menu.
  1707. // When Tab + Shift keys are pressed at the same time, navigate to the
  1708. // previous element. If it's currently focused on the first shown child
  1709. // element of the overflow menu, let the focus move to the last child
  1710. // element of the menu.
  1711. if (this.pressedKeys_.has('Shift')) {
  1712. if (activeElement == firstShownChild) {
  1713. event.preventDefault();
  1714. lastShownChild.focus();
  1715. }
  1716. } else {
  1717. if (activeElement == lastShownChild) {
  1718. event.preventDefault();
  1719. firstShownChild.focus();
  1720. }
  1721. }
  1722. }
  1723. }
  1724. /**
  1725. * For keyboard navigation, we use blue borders to highlight the active
  1726. * element. If we detect that a mouse is being used, remove the blue border
  1727. * from the active element.
  1728. * @private
  1729. */
  1730. onMouseDown_() {
  1731. this.eventManager_.unlisten(window, 'mousedown');
  1732. }
  1733. /**
  1734. * @export
  1735. */
  1736. showUI() {
  1737. const event = new Event('mousemove', {bubbles: false, cancelable: false});
  1738. this.onMouseMove_(event);
  1739. }
  1740. /**
  1741. * @export
  1742. */
  1743. hideUI() {
  1744. // Stop the timer and invoke the callback now to hide the controls. If we
  1745. // don't, the opacity style we set in onMouseMove_ will continue to override
  1746. // the opacity in CSS and force the controls to stay visible.
  1747. this.mouseStillTimer_.tickNow();
  1748. }
  1749. /**
  1750. * @return {shaka.ui.VRManager}
  1751. */
  1752. getVR() {
  1753. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1754. return this.vr_;
  1755. }
  1756. /**
  1757. * Returns if a VR is capable.
  1758. *
  1759. * @return {boolean}
  1760. * @export
  1761. */
  1762. canPlayVR() {
  1763. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1764. return this.vr_.canPlayVR();
  1765. }
  1766. /**
  1767. * Returns if a VR is supported.
  1768. *
  1769. * @return {boolean}
  1770. * @export
  1771. */
  1772. isPlayingVR() {
  1773. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1774. return this.vr_.isPlayingVR();
  1775. }
  1776. /**
  1777. * Reset VR view.
  1778. */
  1779. resetVR() {
  1780. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1781. this.vr_.reset();
  1782. }
  1783. /**
  1784. * Get the angle of the north.
  1785. *
  1786. * @return {?number}
  1787. * @export
  1788. */
  1789. getVRNorth() {
  1790. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1791. return this.vr_.getNorth();
  1792. }
  1793. /**
  1794. * Returns the angle of the current field of view displayed in degrees.
  1795. *
  1796. * @return {?number}
  1797. * @export
  1798. */
  1799. getVRFieldOfView() {
  1800. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1801. return this.vr_.getFieldOfView();
  1802. }
  1803. /**
  1804. * Changing the field of view increases or decreases the portion of the video
  1805. * that is viewed at one time. If the field of view is decreased, a small
  1806. * part of the video will be seen, but with more detail. If the field of view
  1807. * is increased, a larger part of the video will be seen, but with less
  1808. * detail.
  1809. *
  1810. * @param {number} fieldOfView In degrees
  1811. * @export
  1812. */
  1813. setVRFieldOfView(fieldOfView) {
  1814. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1815. this.vr_.setFieldOfView(fieldOfView);
  1816. }
  1817. /**
  1818. * Toggle stereoscopic mode.
  1819. *
  1820. * @export
  1821. */
  1822. toggleStereoscopicMode() {
  1823. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1824. this.vr_.toggleStereoscopicMode();
  1825. }
  1826. /**
  1827. * Returns true if stereoscopic mode is enabled.
  1828. *
  1829. * @return {boolean}
  1830. */
  1831. isStereoscopicModeEnabled() {
  1832. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1833. return this.vr_.isStereoscopicModeEnabled();
  1834. }
  1835. /**
  1836. * Increment the yaw in X angle in degrees.
  1837. *
  1838. * @param {number} angle In degrees
  1839. * @export
  1840. */
  1841. incrementYaw(angle) {
  1842. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1843. this.vr_.incrementYaw(angle);
  1844. }
  1845. /**
  1846. * Increment the pitch in X angle in degrees.
  1847. *
  1848. * @param {number} angle In degrees
  1849. * @export
  1850. */
  1851. incrementPitch(angle) {
  1852. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1853. this.vr_.incrementPitch(angle);
  1854. }
  1855. /**
  1856. * Increment the roll in X angle in degrees.
  1857. *
  1858. * @param {number} angle In degrees
  1859. * @export
  1860. */
  1861. incrementRoll(angle) {
  1862. goog.asserts.assert(this.vr_ != null, 'Should have a VR manager!');
  1863. this.vr_.incrementRoll(angle);
  1864. }
  1865. /**
  1866. * Create a localization instance already pre-loaded with all the locales that
  1867. * we support.
  1868. *
  1869. * @return {!shaka.ui.Localization}
  1870. * @private
  1871. */
  1872. static createLocalization_() {
  1873. /** @type {string} */
  1874. const fallbackLocale = 'en';
  1875. /** @type {!shaka.ui.Localization} */
  1876. const localization = new shaka.ui.Localization(fallbackLocale);
  1877. shaka.ui.Locales.addTo(localization);
  1878. localization.changeLocale(navigator.languages || []);
  1879. return localization;
  1880. }
  1881. };
  1882. /**
  1883. * @event shaka.ui.Controls#CastStatusChangedEvent
  1884. * @description Fired upon receiving a 'caststatuschanged' event from
  1885. * the cast proxy.
  1886. * @property {string} type
  1887. * 'caststatuschanged'
  1888. * @property {boolean} newStatus
  1889. * The new status of the application. True for 'is casting' and
  1890. * false otherwise.
  1891. * @exportDoc
  1892. */
  1893. /**
  1894. * @event shaka.ui.Controls#VRStatusChangedEvent
  1895. * @description Fired when VR status change
  1896. * @property {string} type
  1897. * 'vrstatuschanged'
  1898. * @exportDoc
  1899. */
  1900. /**
  1901. * @event shaka.ui.Controls#SubMenuOpenEvent
  1902. * @description Fired when one of the overflow submenus is opened
  1903. * (e. g. language/resolution/subtitle selection).
  1904. * @property {string} type
  1905. * 'submenuopen'
  1906. * @exportDoc
  1907. */
  1908. /**
  1909. * @event shaka.ui.Controls#CaptionSelectionUpdatedEvent
  1910. * @description Fired when the captions/subtitles menu has finished updating.
  1911. * @property {string} type
  1912. * 'captionselectionupdated'
  1913. * @exportDoc
  1914. */
  1915. /**
  1916. * @event shaka.ui.Controls#ResolutionSelectionUpdatedEvent
  1917. * @description Fired when the resolution menu has finished updating.
  1918. * @property {string} type
  1919. * 'resolutionselectionupdated'
  1920. * @exportDoc
  1921. */
  1922. /**
  1923. * @event shaka.ui.Controls#LanguageSelectionUpdatedEvent
  1924. * @description Fired when the audio language menu has finished updating.
  1925. * @property {string} type
  1926. * 'languageselectionupdated'
  1927. * @exportDoc
  1928. */
  1929. /**
  1930. * @event shaka.ui.Controls#ErrorEvent
  1931. * @description Fired when something went wrong with the controls.
  1932. * @property {string} type
  1933. * 'error'
  1934. * @property {!shaka.util.Error} detail
  1935. * An object which contains details on the error. The error's 'category'
  1936. * and 'code' properties will identify the specific error that occurred.
  1937. * In an uncompiled build, you can also use the 'message' and 'stack'
  1938. * properties to debug.
  1939. * @exportDoc
  1940. */
  1941. /**
  1942. * @event shaka.ui.Controls#TimeAndSeekRangeUpdatedEvent
  1943. * @description Fired when the time and seek range elements have finished
  1944. * updating.
  1945. * @property {string} type
  1946. * 'timeandseekrangeupdated'
  1947. * @exportDoc
  1948. */
  1949. /**
  1950. * @event shaka.ui.Controls#UIUpdatedEvent
  1951. * @description Fired after a call to ui.configure() once the UI has finished
  1952. * updating.
  1953. * @property {string} type
  1954. * 'uiupdated'
  1955. * @exportDoc
  1956. */
  1957. /** @private {!Map<string, !shaka.extern.IUIElement.Factory>} */
  1958. shaka.ui.ControlsPanel.elementNamesToFactories_ = new Map();
  1959. /** @private {?shaka.extern.IUISeekBar.Factory} */
  1960. shaka.ui.ControlsPanel.seekBarFactory_ = new shaka.ui.SeekBar.Factory();