Source: lib/polyfill/mediasource.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.polyfill.MediaSource');
  7. goog.require('shaka.log');
  8. goog.require('shaka.polyfill');
  9. goog.require('shaka.util.MimeUtils');
  10. goog.require('shaka.util.Platform');
  11. /**
  12. * @summary A polyfill to patch MSE bugs.
  13. * @export
  14. */
  15. shaka.polyfill.MediaSource = class {
  16. /**
  17. * Install the polyfill if needed.
  18. * @export
  19. */
  20. static install() {
  21. shaka.log.debug('MediaSource.install');
  22. // MediaSource bugs are difficult to detect without checking for the
  23. // affected platform. SourceBuffer is not always exposed on window, for
  24. // example, and instances are only accessible after setting up MediaSource
  25. // on a video element. Because of this, we use UA detection and other
  26. // platform detection tricks to decide which patches to install.
  27. const safariVersion = shaka.util.Platform.safariVersion();
  28. if (!window.MediaSource) {
  29. shaka.log.info('No MSE implementation available.');
  30. } else if (window.cast && cast.__platform__ &&
  31. cast.__platform__.canDisplayType) {
  32. shaka.log.info('Patching Chromecast MSE bugs.');
  33. // Chromecast cannot make accurate determinations via isTypeSupported.
  34. shaka.polyfill.MediaSource.patchCastIsTypeSupported_();
  35. } else if (safariVersion) {
  36. // NOTE: shaka.Player.isBrowserSupported() has its own restrictions on
  37. // Safari version.
  38. if (safariVersion <= 12) {
  39. shaka.log.info('Patching Safari 11 & 12 MSE bugs.');
  40. // Safari 11 & 12 do not correctly implement abort() on SourceBuffer.
  41. // Calling abort() before appending a segment causes that segment to be
  42. // incomplete in the buffer.
  43. // Bug filed: https://bugs.webkit.org/show_bug.cgi?id=165342
  44. shaka.polyfill.MediaSource.stubAbort_();
  45. // If you remove up to a keyframe, Safari 11 & 12 incorrectly will also
  46. // remove that keyframe and the content up to the next.
  47. // Offsetting the end of the removal range seems to help.
  48. // Bug filed: https://bugs.webkit.org/show_bug.cgi?id=177884
  49. shaka.polyfill.MediaSource.patchRemovalRange_();
  50. } else {
  51. shaka.log.info('Patching Safari 13 MSE bugs.');
  52. // Safari 13 does not correctly implement abort() on SourceBuffer.
  53. // Calling abort() before appending a segment causes that segment to be
  54. // incomplete in the buffer.
  55. // Bug filed: https://bugs.webkit.org/show_bug.cgi?id=165342
  56. shaka.polyfill.MediaSource.stubAbort_();
  57. }
  58. } else if (shaka.util.Platform.isTizen2() ||
  59. shaka.util.Platform.isTizen3() ||
  60. shaka.util.Platform.isTizen4()) {
  61. shaka.log.info('Rejecting Opus.');
  62. // Tizen's implementation of MSE does not work well with opus. To prevent
  63. // the player from trying to play opus on Tizen, we will override media
  64. // source to always reject opus content.
  65. shaka.polyfill.MediaSource.rejectCodec_('opus');
  66. } else {
  67. shaka.log.info('Using native MSE as-is.');
  68. }
  69. if (window.MediaSource &&
  70. MediaSource.isTypeSupported('video/webm; codecs="vp9"') &&
  71. !MediaSource.isTypeSupported('video/webm; codecs="vp09.00.10.08"')) {
  72. shaka.log.info('Patching vp09 support queries.');
  73. // Only the old, deprecated style of VP9 codec strings is supported.
  74. // This occurs on older smart TVs.
  75. // Patch isTypeSupported to translate the new strings into the old one.
  76. shaka.polyfill.MediaSource.patchVp09_();
  77. }
  78. }
  79. /**
  80. * Stub out abort(). On some buggy MSE implementations, calling abort()
  81. * causes various problems.
  82. *
  83. * @private
  84. */
  85. static stubAbort_() {
  86. /* eslint-disable no-restricted-syntax */
  87. const addSourceBuffer = MediaSource.prototype.addSourceBuffer;
  88. MediaSource.prototype.addSourceBuffer = function(...varArgs) {
  89. const sourceBuffer = addSourceBuffer.apply(this, varArgs);
  90. sourceBuffer.abort = function() {}; // Stub out for buggy implementations.
  91. return sourceBuffer;
  92. };
  93. /* eslint-enable no-restricted-syntax */
  94. }
  95. /**
  96. * Patch remove(). On Safari 11, if you call remove() to remove the content
  97. * up to a keyframe, Safari will also remove the keyframe and all of the data
  98. * up to the next one. For example, if the keyframes are at 0s, 5s, and 10s,
  99. * and you tried to remove 0s-5s, it would instead remove 0s-10s.
  100. *
  101. * Offsetting the end of the range seems to be a usable workaround.
  102. *
  103. * @private
  104. */
  105. static patchRemovalRange_() {
  106. // eslint-disable-next-line no-restricted-syntax
  107. const originalRemove = SourceBuffer.prototype.remove;
  108. // eslint-disable-next-line no-restricted-syntax
  109. SourceBuffer.prototype.remove = function(startTime, endTime) {
  110. // eslint-disable-next-line no-restricted-syntax
  111. return originalRemove.call(this, startTime, endTime - 0.001);
  112. };
  113. }
  114. /**
  115. * Patch |MediaSource.isTypeSupported| to always reject |codec|. This is used
  116. * when we know that we are on a platform that does not work well with a given
  117. * codec.
  118. *
  119. * @param {string} codec
  120. * @private
  121. */
  122. static rejectCodec_(codec) {
  123. const isTypeSupported = MediaSource.isTypeSupported;
  124. MediaSource.isTypeSupported = (mimeType) => {
  125. const actualCodec = shaka.util.MimeUtils.getCodecBase(mimeType);
  126. return actualCodec != codec && isTypeSupported(mimeType);
  127. };
  128. }
  129. /**
  130. * Patch isTypeSupported() to chain to a private API on the Chromecast which
  131. * can query for support of detailed content parameters.
  132. *
  133. * @private
  134. */
  135. static patchCastIsTypeSupported_() {
  136. const originalIsTypeSupported = MediaSource.isTypeSupported;
  137. MediaSource.isTypeSupported = (mimeType) => {
  138. // Parse the basic MIME type from its parameters.
  139. const pieces = mimeType.split(/ *; */);
  140. pieces.shift(); // Remove basic MIME type from pieces.
  141. const hasCodecs = pieces.some((piece) => piece.startsWith('codecs='));
  142. if (!hasCodecs) {
  143. // Though the original reason for this special case was not documented,
  144. // it is presumed to be because the platform won't accept a MIME type
  145. // without codecs in canDisplayType. It is valid, however, in
  146. // isTypeSupported.
  147. return originalIsTypeSupported(mimeType);
  148. }
  149. // Only canDisplayType can check extended MIME type parameters on this
  150. // platform (such as frame rate, resolution, etc).
  151. // In previous versions of this polyfill, the MIME type parameters were
  152. // manipulated, filtered, or extended. This is no longer true, so we pass
  153. // the full MIME type to the platform as we received it.
  154. return cast.__platform__.canDisplayType(mimeType);
  155. };
  156. }
  157. /**
  158. * Patch isTypeSupported() to translate vp09 codec strings into vp9, to allow
  159. * such content to play on older smart TVs.
  160. *
  161. * @private
  162. */
  163. static patchVp09_() {
  164. const originalIsTypeSupported = MediaSource.isTypeSupported;
  165. if (shaka.util.Platform.isWebOS()) {
  166. // Don't do this on LG webOS as otherwise it is unable
  167. // to play vp09 at all.
  168. return;
  169. }
  170. MediaSource.isTypeSupported = (mimeType) => {
  171. // Split the MIME type into its various parameters.
  172. const pieces = mimeType.split(/ *; */);
  173. const codecsIndex =
  174. pieces.findIndex((piece) => piece.startsWith('codecs='));
  175. if (codecsIndex < 0) {
  176. // No codec? Call the original without modifying the MIME type.
  177. return originalIsTypeSupported(mimeType);
  178. }
  179. const codecsParam = pieces[codecsIndex];
  180. const codecs = codecsParam
  181. .replace('codecs=', '').replace(/"/g, '').split(/\s*,\s*/);
  182. const vp09Index = codecs.findIndex(
  183. (codecName) => codecName.startsWith('vp09'));
  184. if (vp09Index >= 0) {
  185. // vp09? Replace it with vp9.
  186. codecs[vp09Index] = 'vp9';
  187. pieces[codecsIndex] = 'codecs="' + codecs.join(',') + '"';
  188. mimeType = pieces.join('; ');
  189. }
  190. return originalIsTypeSupported(mimeType);
  191. };
  192. }
  193. };
  194. shaka.polyfill.register(shaka.polyfill.MediaSource.install);