videojs-contrib-media-sources.js.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: videojs-contrib-media-sources.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: videojs-contrib-media-sources.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @file videojs-contrib-media-sources.js
  21. */
  22. import window from 'global/window';
  23. import FlashMediaSource from './flash-media-source';
  24. import HtmlMediaSource from './html-media-source';
  25. import videojs from 'video.js';
  26. let urlCount = 0;
  27. // ------------
  28. // Media Source
  29. // ------------
  30. const defaults = {
  31. // how to determine the MediaSource implementation to use. There
  32. // are three available modes:
  33. // - auto: use native MediaSources where available and Flash
  34. // everywhere else
  35. // - html5: always use native MediaSources
  36. // - flash: always use the Flash MediaSource polyfill
  37. mode: 'auto'
  38. };
  39. // store references to the media sources so they can be connected
  40. // to a video element (a swf object)
  41. // TODO: can we store this somewhere local to this module?
  42. videojs.mediaSources = {};
  43. /**
  44. * Provide a method for a swf object to notify JS that a
  45. * media source is now open.
  46. *
  47. * @param {String} msObjectURL string referencing the MSE Object URL
  48. * @param {String} swfId the swf id
  49. */
  50. const open = function(msObjectURL, swfId) {
  51. let mediaSource = videojs.mediaSources[msObjectURL];
  52. if (mediaSource) {
  53. mediaSource.trigger({type: 'sourceopen', swfId});
  54. } else {
  55. throw new Error('Media Source not found (Video.js)');
  56. }
  57. };
  58. /**
  59. * Check to see if the native MediaSource object exists and supports
  60. * an MP4 container with both H.264 video and AAC-LC audio.
  61. *
  62. * @return {Boolean} if native media sources are supported
  63. */
  64. const supportsNativeMediaSources = function() {
  65. return (!!window.MediaSource &amp;&amp; !!window.MediaSource.isTypeSupported &amp;&amp;
  66. window.MediaSource.isTypeSupported('video/mp4;codecs="avc1.4d400d,mp4a.40.2"'));
  67. };
  68. /**
  69. * An emulation of the MediaSource API so that we can support
  70. * native and non-native functionality such as flash and
  71. * video/mp2t videos. returns an instance of HtmlMediaSource or
  72. * FlashMediaSource depending on what is supported and what options
  73. * are passed in.
  74. *
  75. * @link https://developer.mozilla.org/en-US/docs/Web/API/MediaSource/MediaSource
  76. * @param {Object} options options to use during setup.
  77. */
  78. export const MediaSource = function(options) {
  79. let settings = videojs.mergeOptions(defaults, options);
  80. this.MediaSource = {
  81. open,
  82. supportsNativeMediaSources
  83. };
  84. // determine whether HTML MediaSources should be used
  85. if (settings.mode === 'html5' ||
  86. (settings.mode === 'auto' &amp;&amp; supportsNativeMediaSources())) {
  87. return new HtmlMediaSource();
  88. } else if (videojs.getTech('Flash')) {
  89. return new FlashMediaSource();
  90. }
  91. throw new Error('Cannot use Flash or Html5 to create a MediaSource for this video');
  92. };
  93. MediaSource.open = open;
  94. MediaSource.supportsNativeMediaSources = supportsNativeMediaSources;
  95. /**
  96. * A wrapper around the native URL for our MSE object
  97. * implementation, this object is exposed under videojs.URL
  98. *
  99. * @link https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
  100. */
  101. export const URL = {
  102. /**
  103. * A wrapper around the native createObjectURL for our objects.
  104. * This function maps a native or emulated mediaSource to a blob
  105. * url so that it can be loaded into video.js
  106. *
  107. * @link https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
  108. * @param {MediaSource} object the object to create a blob url to
  109. */
  110. createObjectURL(object) {
  111. let objectUrlPrefix = 'blob:vjs-media-source/';
  112. let url;
  113. // use the native MediaSource to generate an object URL
  114. if (object instanceof HtmlMediaSource) {
  115. url = window.URL.createObjectURL(object.nativeMediaSource_);
  116. object.url_ = url;
  117. return url;
  118. }
  119. // if the object isn't an emulated MediaSource, delegate to the
  120. // native implementation
  121. if (!(object instanceof FlashMediaSource)) {
  122. url = window.URL.createObjectURL(object);
  123. object.url_ = url;
  124. return url;
  125. }
  126. // build a URL that can be used to map back to the emulated
  127. // MediaSource
  128. url = objectUrlPrefix + urlCount;
  129. urlCount++;
  130. // setup the mapping back to object
  131. videojs.mediaSources[url] = object;
  132. return url;
  133. }
  134. };
  135. videojs.MediaSource = MediaSource;
  136. videojs.URL = URL;
  137. </code></pre>
  138. </article>
  139. </section>
  140. </div>
  141. <nav>
  142. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="FlashMediaSource.html">FlashMediaSource</a></li><li><a href="FlashSourceBuffer.html">FlashSourceBuffer</a></li><li><a href="HtmlMediaSource.html">HtmlMediaSource</a></li><li><a href="MessageHandlers.html">MessageHandlers</a></li><li><a href="VirtualSourceBuffer.html">VirtualSourceBuffer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#abort">abort</a></li><li><a href="global.html#addSourceBuffer">addSourceBuffer</a></li><li><a href="global.html#appendBuffer">appendBuffer</a></li><li><a href="global.html#appendGopInfo_">appendGopInfo_</a></li><li><a href="global.html#endOfStream">endOfStream</a></li><li><a href="global.html#FlashTransmuxerWorker">FlashTransmuxerWorker</a></li><li><a href="global.html#get">get</a></li><li><a href="global.html#gopsSafeToAlignWith">gopsSafeToAlignWith</a></li><li><a href="global.html#MediaSource">MediaSource</a></li><li><a href="global.html#open">open</a></li><li><a href="global.html#remove">remove</a></li><li><a href="global.html#removeGopBuffer">removeGopBuffer</a></li><li><a href="global.html#set">set</a></li><li><a href="global.html#supportsNativeMediaSources">supportsNativeMediaSources</a></li><li><a href="global.html#TransmuxerWorker">TransmuxerWorker</a></li><li><a href="global.html#updateGopBuffer">updateGopBuffer</a></li><li><a href="global.html#URL">URL</a></li></ul>
  143. </nav>
  144. <br class="clear">
  145. <footer>
  146. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.4</a> on Thu Nov 02 2017 12:03:25 GMT-0400 (EDT)
  147. </footer>
  148. <script> prettyPrint(); </script>
  149. <script src="scripts/linenumber.js"> </script>
  150. </body>
  151. </html>