Source: lib/polyfill/patchedmediakeys_nop.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.polyfill.PatchedMediaKeysNop');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.log');
  9. goog.require('shaka.polyfill');
  10. /**
  11. * @summary A polyfill to stub out
  12. * {@link https://bit.ly/EmeMar15 EME draft 12 March 2015} on browsers without
  13. * EME.
  14. * All methods will fail.
  15. * @export
  16. */
  17. shaka.polyfill.PatchedMediaKeysNop = class {
  18. /**
  19. * Installs the polyfill if needed.
  20. * @export
  21. */
  22. static install() {
  23. if (!window.HTMLVideoElement ||
  24. (navigator.requestMediaKeySystemAccess &&
  25. // eslint-disable-next-line no-restricted-syntax
  26. MediaKeySystemAccess.prototype.getConfiguration)) {
  27. return;
  28. }
  29. shaka.log.info('EME not available.');
  30. // Alias.
  31. const PatchedMediaKeysNop = shaka.polyfill.PatchedMediaKeysNop;
  32. // Install patches.
  33. navigator.requestMediaKeySystemAccess =
  34. PatchedMediaKeysNop.requestMediaKeySystemAccess;
  35. // Delete mediaKeys to work around strict mode compatibility issues.
  36. // eslint-disable-next-line no-restricted-syntax
  37. delete HTMLMediaElement.prototype['mediaKeys'];
  38. // Work around read-only declaration for mediaKeys by using a string.
  39. // eslint-disable-next-line no-restricted-syntax
  40. HTMLMediaElement.prototype['mediaKeys'] = null;
  41. // eslint-disable-next-line no-restricted-syntax
  42. HTMLMediaElement.prototype.setMediaKeys = PatchedMediaKeysNop.setMediaKeys;
  43. // These are not usable, but allow Player.isBrowserSupported to pass.
  44. window.MediaKeys = PatchedMediaKeysNop.MediaKeys;
  45. window.MediaKeySystemAccess = PatchedMediaKeysNop.MediaKeySystemAccess;
  46. }
  47. /**
  48. * An implementation of navigator.requestMediaKeySystemAccess.
  49. * Retrieves a MediaKeySystemAccess object.
  50. *
  51. * @this {!Navigator}
  52. * @param {string} keySystem
  53. * @param {!Array.<!MediaKeySystemConfiguration>} supportedConfigurations
  54. * @return {!Promise.<!MediaKeySystemAccess>}
  55. */
  56. static requestMediaKeySystemAccess(keySystem, supportedConfigurations) {
  57. shaka.log.debug('PatchedMediaKeysNop.requestMediaKeySystemAccess');
  58. goog.asserts.assert(this == navigator,
  59. 'bad "this" for requestMediaKeySystemAccess');
  60. return Promise.reject(new Error(
  61. 'The key system specified is not supported.'));
  62. }
  63. /**
  64. * An implementation of HTMLMediaElement.prototype.setMediaKeys.
  65. * Attaches a MediaKeys object to the media element.
  66. *
  67. * @this {!HTMLMediaElement}
  68. * @param {MediaKeys} mediaKeys
  69. * @return {!Promise}
  70. */
  71. static setMediaKeys(mediaKeys) {
  72. shaka.log.debug('PatchedMediaKeysNop.setMediaKeys');
  73. goog.asserts.assert(this instanceof HTMLMediaElement,
  74. 'bad "this" for setMediaKeys');
  75. if (mediaKeys == null) {
  76. return Promise.resolve();
  77. }
  78. return Promise.reject(new Error('MediaKeys not supported.'));
  79. }
  80. };
  81. /**
  82. * An unusable constructor for MediaKeys.
  83. * @implements {MediaKeys}
  84. */
  85. shaka.polyfill.PatchedMediaKeysNop.MediaKeys = class {
  86. /** */
  87. constructor() {
  88. throw new TypeError('Illegal constructor.');
  89. }
  90. /** @override */
  91. createSession() {}
  92. /** @override */
  93. setServerCertificate() {}
  94. };
  95. /**
  96. * An unusable constructor for MediaKeySystemAccess.
  97. * @implements {MediaKeySystemAccess}
  98. */
  99. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess = class {
  100. /** */
  101. constructor() {
  102. /** @override */
  103. this.keySystem = ''; // For the compiler.
  104. throw new TypeError('Illegal constructor.');
  105. }
  106. /** @override */
  107. getConfiguration() {}
  108. /** @override */
  109. createMediaKeys() {}
  110. };
  111. // A low priority ensures this is the last and acts as a fallback.
  112. shaka.polyfill.register(shaka.polyfill.PatchedMediaKeysNop.install, -10);