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