flash-media-source.js.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: flash-media-source.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: flash-media-source.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @file flash-media-source.js
  21. */
  22. import document from 'global/document';
  23. import videojs from 'video.js';
  24. import FlashSourceBuffer from './flash-source-buffer';
  25. import FlashConstants from './flash-constants';
  26. import {parseContentType} from './codec-utils';
  27. /**
  28. * A flash implmentation of HTML MediaSources and a polyfill
  29. * for browsers that don't support native or HTML MediaSources..
  30. *
  31. * @link https://developer.mozilla.org/en-US/docs/Web/API/MediaSource
  32. * @class FlashMediaSource
  33. * @extends videojs.EventTarget
  34. */
  35. export default class FlashMediaSource extends videojs.EventTarget {
  36. constructor() {
  37. super();
  38. this.sourceBuffers = [];
  39. this.readyState = 'closed';
  40. this.on(['sourceopen', 'webkitsourceopen'], (event) => {
  41. // find the swf where we will push media data
  42. this.swfObj = document.getElementById(event.swfId);
  43. this.player_ = videojs(this.swfObj.parentNode);
  44. this.tech_ = this.swfObj.tech;
  45. this.readyState = 'open';
  46. this.tech_.on('seeking', () => {
  47. let i = this.sourceBuffers.length;
  48. while (i--) {
  49. this.sourceBuffers[i].abort();
  50. }
  51. });
  52. // trigger load events
  53. if (this.swfObj) {
  54. this.swfObj.vjs_load();
  55. }
  56. });
  57. }
  58. /**
  59. * We have this function so that the html and flash interfaces
  60. * are the same.
  61. *
  62. * @private
  63. */
  64. addSeekableRange_() {
  65. // intentional no-op
  66. }
  67. /**
  68. * Create a new flash source buffer and add it to our flash media source.
  69. *
  70. * @link https://developer.mozilla.org/en-US/docs/Web/API/MediaSource/addSourceBuffer
  71. * @param {String} type the content-type of the source
  72. * @return {Object} the flash source buffer
  73. */
  74. addSourceBuffer(type) {
  75. let parsedType = parseContentType(type);
  76. let sourceBuffer;
  77. // if this is an FLV type, we'll push data to flash
  78. if (parsedType.type === 'video/mp2t' || parsedType.type === 'audio/mp2t') {
  79. // Flash source buffers
  80. sourceBuffer = new FlashSourceBuffer(this);
  81. } else {
  82. throw new Error('NotSupportedError (Video.js)');
  83. }
  84. this.sourceBuffers.push(sourceBuffer);
  85. return sourceBuffer;
  86. }
  87. /**
  88. * Signals the end of the stream.
  89. *
  90. * @link https://w3c.github.io/media-source/#widl-MediaSource-endOfStream-void-EndOfStreamError-error
  91. * @param {String=} error Signals that a playback error
  92. * has occurred. If specified, it must be either "network" or
  93. * "decode".
  94. */
  95. endOfStream(error) {
  96. if (error === 'network') {
  97. // MEDIA_ERR_NETWORK
  98. this.tech_.error(2);
  99. } else if (error === 'decode') {
  100. // MEDIA_ERR_DECODE
  101. this.tech_.error(3);
  102. }
  103. if (this.readyState !== 'ended') {
  104. this.readyState = 'ended';
  105. this.swfObj.vjs_endOfStream();
  106. }
  107. }
  108. }
  109. /**
  110. * Set or return the presentation duration.
  111. *
  112. * @param {Double} value the duration of the media in seconds
  113. * @param {Double} the current presentation duration
  114. * @link http://www.w3.org/TR/media-source/#widl-MediaSource-duration
  115. */
  116. try {
  117. Object.defineProperty(FlashMediaSource.prototype, 'duration', {
  118. /**
  119. * Return the presentation duration.
  120. *
  121. * @return {Double} the duration of the media in seconds
  122. * @link http://www.w3.org/TR/media-source/#widl-MediaSource-duration
  123. */
  124. get() {
  125. if (!this.swfObj) {
  126. return NaN;
  127. }
  128. // get the current duration from the SWF
  129. return this.swfObj.vjs_getProperty('duration');
  130. },
  131. /**
  132. * Set the presentation duration.
  133. *
  134. * @param {Double} value the duration of the media in seconds
  135. * @return {Double} the duration of the media in seconds
  136. * @link http://www.w3.org/TR/media-source/#widl-MediaSource-duration
  137. */
  138. set(value) {
  139. let i;
  140. let oldDuration = this.swfObj.vjs_getProperty('duration');
  141. this.swfObj.vjs_setProperty('duration', value);
  142. if (value &lt; oldDuration) {
  143. // In MSE, this triggers the range removal algorithm which causes
  144. // an update to occur
  145. for (i = 0; i &lt; this.sourceBuffers.length; i++) {
  146. this.sourceBuffers[i].remove(value, oldDuration);
  147. }
  148. }
  149. return value;
  150. }
  151. });
  152. } catch (e) {
  153. // IE8 throws if defineProperty is called on a non-DOM node. We
  154. // don't support IE8 but we shouldn't throw an error if loaded
  155. // there.
  156. FlashMediaSource.prototype.duration = NaN;
  157. }
  158. for (let property in FlashConstants) {
  159. FlashMediaSource[property] = FlashConstants[property];
  160. }
  161. </code></pre>
  162. </article>
  163. </section>
  164. </div>
  165. <nav>
  166. <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>
  167. </nav>
  168. <br class="clear">
  169. <footer>
  170. 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)
  171. </footer>
  172. <script> prettyPrint(); </script>
  173. <script src="scripts/linenumber.js"> </script>
  174. </body>
  175. </html>