Source: ui/airplay_button.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.AirPlayButton');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.Player');
  9. goog.require('shaka.ui.Controls');
  10. goog.require('shaka.ui.Element');
  11. goog.require('shaka.ui.Enums');
  12. goog.require('shaka.ui.Locales');
  13. goog.require('shaka.ui.Localization');
  14. goog.require('shaka.ui.OverflowMenu');
  15. goog.require('shaka.ui.Utils');
  16. goog.require('shaka.util.Dom');
  17. goog.requireType('shaka.ui.Controls');
  18. /**
  19. * @extends {shaka.ui.Element}
  20. * @final
  21. * @export
  22. */
  23. shaka.ui.AirPlayButton = class extends shaka.ui.Element {
  24. /**
  25. * @param {!HTMLElement} parent
  26. * @param {!shaka.ui.Controls} controls
  27. */
  28. constructor(parent, controls) {
  29. super(parent, controls);
  30. /** @private {!HTMLButtonElement} */
  31. this.airplayButton_ = shaka.util.Dom.createButton();
  32. this.airplayButton_.classList.add('shaka-airplay-button');
  33. this.airplayButton_.classList.add('shaka-tooltip');
  34. this.airplayButton_.ariaPressed = 'false';
  35. /** @private {!HTMLElement} */
  36. this.airplayIcon_ = shaka.util.Dom.createHTMLElement('i');
  37. this.airplayIcon_.classList.add('material-icons-round');
  38. this.airplayIcon_.textContent = shaka.ui.Enums.MaterialDesignIcons.AIRPLAY;
  39. this.airplayButton_.appendChild(this.airplayIcon_);
  40. // Don't show the button if AirPlay is not supported.
  41. if (!window.WebKitPlaybackTargetAvailabilityEvent) {
  42. this.airplayButton_.classList.add('shaka-hidden');
  43. }
  44. const label = shaka.util.Dom.createHTMLElement('label');
  45. label.classList.add('shaka-overflow-button-label');
  46. label.classList.add('shaka-overflow-menu-only');
  47. this.airplayNameSpan_ = shaka.util.Dom.createHTMLElement('span');
  48. label.appendChild(this.airplayNameSpan_);
  49. this.airplayCurrentSelectionSpan_ =
  50. shaka.util.Dom.createHTMLElement('span');
  51. this.airplayCurrentSelectionSpan_.classList.add(
  52. 'shaka-current-selection-span');
  53. label.appendChild(this.airplayCurrentSelectionSpan_);
  54. this.airplayButton_.appendChild(label);
  55. this.parent.appendChild(this.airplayButton_);
  56. // Setup strings in the correct language
  57. this.updateLocalizedStrings_();
  58. // Setup button display and state according to the current airplay status
  59. this.onAirPlayStatusChange_();
  60. this.eventManager.listen(
  61. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  62. this.updateLocalizedStrings_();
  63. });
  64. this.eventManager.listen(
  65. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  66. this.updateLocalizedStrings_();
  67. });
  68. this.eventManager.listen(this.airplayButton_, 'click', () => {
  69. this.onAirPlayClick_();
  70. });
  71. const video = this.controls.getVideo();
  72. goog.asserts.assert(video != null, 'Should have a video!');
  73. this.eventManager.listen(video,
  74. 'webkitplaybacktargetavailabilitychanged', (e) => {
  75. const event = /** @type {!AirPlayEvent} */ (e);
  76. this.onAirPlayAvailabilityChange_(event);
  77. });
  78. this.eventManager.listen(video,
  79. 'webkitcurrentplaybacktargetiswirelesschanged', () => {
  80. this.onAirPlayStatusChange_();
  81. });
  82. }
  83. /**
  84. * @private
  85. */
  86. onAirPlayClick_() {
  87. const video = this.controls.getVideo();
  88. goog.asserts.assert(video != null, 'Should have a video!');
  89. video.webkitShowPlaybackTargetPicker();
  90. }
  91. /**
  92. * @param {!AirPlayEvent} e
  93. * @private
  94. */
  95. onAirPlayAvailabilityChange_(e) {
  96. const canCast = e.availability == 'available';
  97. const loadMode = this.player.getLoadMode();
  98. const srcMode = loadMode == shaka.Player.LoadMode.SRC_EQUALS;
  99. shaka.ui.Utils.setDisplay(this.airplayButton_, canCast && srcMode);
  100. }
  101. /**
  102. * @private
  103. */
  104. onAirPlayStatusChange_() {
  105. const video = this.controls.getVideo();
  106. goog.asserts.assert(video != null, 'Should have a video!');
  107. const isCasting = video && video.webkitCurrentPlaybackTargetIsWireless;
  108. // Aria-pressed set to true when casting, set to false otherwise.
  109. if (isCasting) {
  110. this.airplayButton_.ariaPressed = 'true';
  111. } else {
  112. this.airplayButton_.ariaPressed = 'false';
  113. }
  114. }
  115. /**
  116. * @private
  117. */
  118. updateLocalizedStrings_() {
  119. const LocIds = shaka.ui.Locales.Ids;
  120. this.airplayButton_.ariaLabel = this.localization.resolve(LocIds.AIRPLAY);
  121. this.airplayNameSpan_.textContent =
  122. this.localization.resolve(LocIds.AIRPLAY);
  123. }
  124. };
  125. /**
  126. * @implements {shaka.extern.IUIElement.Factory}
  127. * @final
  128. */
  129. shaka.ui.AirPlayButton.Factory = class {
  130. /** @override */
  131. create(rootElement, controls) {
  132. return new shaka.ui.AirPlayButton(rootElement, controls);
  133. }
  134. };
  135. shaka.ui.OverflowMenu.registerElement(
  136. 'airplay', new shaka.ui.AirPlayButton.Factory());
  137. shaka.ui.Controls.registerElement(
  138. 'airplay', new shaka.ui.AirPlayButton.Factory());