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 && !window.ManagedMediaSource) {
  29. shaka.log.info('No MSE implementation available.');
  30. } else if (safariVersion && window.MediaSource) {
  31. // NOTE: shaka.Player.isBrowserSupported() has its own restrictions on
  32. // Safari version.
  33. if (safariVersion <= 12) {
  34. shaka.log.info('Patching Safari 8-12 MSE bugs.');
  35. // Safari 8 does not implement appendWindowEnd.
  36. // Safari 10 fires spurious 'updateend' events after endOfStream().
  37. // We do not have patches for these bugs here.
  38. // Safari 8-12 do not correctly implement abort() on SourceBuffer.
  39. // Calling abort() before appending a segment causes that segment to be
  40. // incomplete in the buffer.
  41. // Bug filed: https://bugs.webkit.org/show_bug.cgi?id=165342
  42. shaka.polyfill.MediaSource.stubAbort_();
  43. // If you remove up to a keyframe, Safari 8-12 incorrectly will also
  44. // remove that keyframe and the content up to the next.
  45. // Offsetting the end of the removal range seems to help.
  46. // Bug filed: https://bugs.webkit.org/show_bug.cgi?id=177884
  47. shaka.polyfill.MediaSource.patchRemovalRange_();
  48. } else if (safariVersion <= 15) {
  49. shaka.log.info('Patching Safari 13 & 14 & 15 MSE bugs.');
  50. // Safari 13 does not correctly implement abort() on SourceBuffer.
  51. // Calling abort() before appending a segment causes that segment to be
  52. // incomplete in the buffer.
  53. // Bug filed: https://bugs.webkit.org/show_bug.cgi?id=165342
  54. shaka.polyfill.MediaSource.stubAbort_();
  55. }
  56. } else if (shaka.util.Platform.isZenterio()) {
  57. // Zenterio uses WPE based on Webkit 607.x.x which do not correctly
  58. // implement abort() on SourceBuffer.
  59. // Calling abort() before appending a segment causes that segment to be
  60. // incomplete in the buffer.
  61. // Bug filed: https://bugs.webkit.org/show_bug.cgi?id=165342
  62. shaka.polyfill.MediaSource.stubAbort_();
  63. // If you remove up to a keyframe, Webkit 607.x.x incorrectly will also
  64. // remove that keyframe and the content up to the next.
  65. // Offsetting the end of the removal range seems to help.
  66. // Bug filed: https://bugs.webkit.org/show_bug.cgi?id=177884
  67. shaka.polyfill.MediaSource.patchRemovalRange_();
  68. } else if (shaka.util.Platform.isTizen2() ||
  69. shaka.util.Platform.isTizen3() ||
  70. shaka.util.Platform.isTizen4()) {
  71. shaka.log.info('Rejecting Opus.');
  72. // Tizen's implementation of MSE does not work well with opus. To prevent
  73. // the player from trying to play opus on Tizen, we will override media
  74. // source to always reject opus content.
  75. shaka.polyfill.MediaSource.rejectCodec_('opus');
  76. } else {
  77. shaka.log.info('Using native MSE as-is.');
  78. }
  79. if (window.MediaSource || window.ManagedMediaSource) {
  80. // TS content is broken on all browsers in general.
  81. // See https://github.com/shaka-project/shaka-player/issues/4955
  82. // See https://github.com/shaka-project/shaka-player/issues/5278
  83. // See https://github.com/shaka-project/shaka-player/issues/6334
  84. shaka.polyfill.MediaSource.rejectContainer_('mp2t');
  85. }
  86. if (window.MediaSource &&
  87. MediaSource.isTypeSupported('video/webm; codecs="vp9"') &&
  88. !MediaSource.isTypeSupported('video/webm; codecs="vp09.00.10.08"')) {
  89. shaka.log.info('Patching vp09 support queries.');
  90. // Only the old, deprecated style of VP9 codec strings is supported.
  91. // This occurs on older smart TVs.
  92. // Patch isTypeSupported to translate the new strings into the old one.
  93. shaka.polyfill.MediaSource.patchVp09_();
  94. }
  95. }
  96. /**
  97. * Stub out abort(). On some buggy MSE implementations, calling abort()
  98. * causes various problems.
  99. *
  100. * @private
  101. */
  102. static stubAbort_() {
  103. /* eslint-disable no-restricted-syntax */
  104. const addSourceBuffer = MediaSource.prototype.addSourceBuffer;
  105. MediaSource.prototype.addSourceBuffer = function(...varArgs) {
  106. const sourceBuffer = addSourceBuffer.apply(this, varArgs);
  107. sourceBuffer.abort = function() {}; // Stub out for buggy implementations.
  108. return sourceBuffer;
  109. };
  110. /* eslint-enable no-restricted-syntax */
  111. }
  112. /**
  113. * Patch remove(). On Safari 11, if you call remove() to remove the content
  114. * up to a keyframe, Safari will also remove the keyframe and all of the data
  115. * up to the next one. For example, if the keyframes are at 0s, 5s, and 10s,
  116. * and you tried to remove 0s-5s, it would instead remove 0s-10s.
  117. *
  118. * Offsetting the end of the range seems to be a usable workaround.
  119. *
  120. * @private
  121. */
  122. static patchRemovalRange_() {
  123. // eslint-disable-next-line no-restricted-syntax
  124. const originalRemove = SourceBuffer.prototype.remove;
  125. // eslint-disable-next-line no-restricted-syntax
  126. SourceBuffer.prototype.remove = function(startTime, endTime) {
  127. // eslint-disable-next-line no-restricted-syntax
  128. return originalRemove.call(this, startTime, endTime - 0.001);
  129. };
  130. }
  131. /**
  132. * Patch |MediaSource.isTypeSupported| to always reject |container|. This is
  133. * used when we know that we are on a platform that does not work well with
  134. * a given container.
  135. *
  136. * @param {string} container
  137. * @private
  138. */
  139. static rejectContainer_(container) {
  140. if (window.MediaSource) {
  141. const isTypeSupported =
  142. // eslint-disable-next-line no-restricted-syntax
  143. MediaSource.isTypeSupported.bind(MediaSource);
  144. MediaSource.isTypeSupported = (mimeType) => {
  145. const actualContainer = shaka.util.MimeUtils.getContainerType(mimeType);
  146. return actualContainer != container && isTypeSupported(mimeType);
  147. };
  148. }
  149. if (window.ManagedMediaSource) {
  150. const isTypeSupportedManaged =
  151. // eslint-disable-next-line no-restricted-syntax
  152. ManagedMediaSource.isTypeSupported.bind(ManagedMediaSource);
  153. window.ManagedMediaSource.isTypeSupported = (mimeType) => {
  154. const actualContainer = shaka.util.MimeUtils.getContainerType(mimeType);
  155. return actualContainer != container && isTypeSupportedManaged(mimeType);
  156. };
  157. }
  158. }
  159. /**
  160. * Patch |MediaSource.isTypeSupported| to always reject |codec|. This is used
  161. * when we know that we are on a platform that does not work well with a given
  162. * codec.
  163. *
  164. * @param {string} codec
  165. * @private
  166. */
  167. static rejectCodec_(codec) {
  168. const isTypeSupported =
  169. // eslint-disable-next-line no-restricted-syntax
  170. MediaSource.isTypeSupported.bind(MediaSource);
  171. MediaSource.isTypeSupported = (mimeType) => {
  172. const actualCodec = shaka.util.MimeUtils.getCodecBase(mimeType);
  173. return actualCodec != codec && isTypeSupported(mimeType);
  174. };
  175. if (window.ManagedMediaSource) {
  176. const isTypeSupportedManaged =
  177. // eslint-disable-next-line no-restricted-syntax
  178. ManagedMediaSource.isTypeSupported.bind(ManagedMediaSource);
  179. window.ManagedMediaSource.isTypeSupported = (mimeType) => {
  180. const actualCodec = shaka.util.MimeUtils.getCodecBase(mimeType);
  181. return actualCodec != codec && isTypeSupportedManaged(mimeType);
  182. };
  183. }
  184. }
  185. /**
  186. * Patch isTypeSupported() to translate vp09 codec strings into vp9, to allow
  187. * such content to play on older smart TVs.
  188. *
  189. * @private
  190. */
  191. static patchVp09_() {
  192. const originalIsTypeSupported = MediaSource.isTypeSupported;
  193. if (shaka.util.Platform.isWebOS()) {
  194. // Don't do this on LG webOS as otherwise it is unable
  195. // to play vp09 at all.
  196. return;
  197. }
  198. MediaSource.isTypeSupported = (mimeType) => {
  199. // Split the MIME type into its various parameters.
  200. const pieces = mimeType.split(/ *; */);
  201. const codecsIndex =
  202. pieces.findIndex((piece) => piece.startsWith('codecs='));
  203. if (codecsIndex < 0) {
  204. // No codec? Call the original without modifying the MIME type.
  205. return originalIsTypeSupported(mimeType);
  206. }
  207. const codecsParam = pieces[codecsIndex];
  208. const codecs = codecsParam
  209. .replace('codecs=', '').replace(/"/g, '').split(/\s*,\s*/);
  210. const vp09Index = codecs.findIndex(
  211. (codecName) => codecName.startsWith('vp09'));
  212. if (vp09Index >= 0) {
  213. // vp09? Replace it with vp9.
  214. codecs[vp09Index] = 'vp9';
  215. pieces[codecsIndex] = 'codecs="' + codecs.join(',') + '"';
  216. mimeType = pieces.join('; ');
  217. }
  218. return originalIsTypeSupported(mimeType);
  219. };
  220. }
  221. };
  222. shaka.polyfill.register(shaka.polyfill.MediaSource.install);