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