flash-worker.js.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: flash-worker.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-worker.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @file flash-worker.js
  21. */
  22. /**
  23. * videojs-contrib-media-sources
  24. *
  25. * Copyright (c) 2015 Brightcove
  26. * All rights reserved.
  27. *
  28. * Handles communication between the browser-world and the mux.js
  29. * transmuxer running inside of a WebWorker by exposing a simple
  30. * message-based interface to a Transmuxer object.
  31. */
  32. import window from 'global/window';
  33. import flv from 'mux.js/lib/flv';
  34. const orderTags = function(tags) {
  35. let videoTags = tags.videoTags;
  36. let audioTags = tags.audioTags;
  37. let ordered = [];
  38. let tag;
  39. while (videoTags.length || audioTags.length) {
  40. if (!videoTags.length) {
  41. // only audio tags remain
  42. tag = audioTags.shift();
  43. } else if (!audioTags.length) {
  44. // only video tags remain
  45. tag = videoTags.shift();
  46. } else if (audioTags[0].dts &lt; videoTags[0].dts) {
  47. // audio should be decoded next
  48. tag = audioTags.shift();
  49. } else {
  50. // video should be decoded next
  51. tag = videoTags.shift();
  52. }
  53. ordered.push(tag);
  54. }
  55. return ordered;
  56. }
  57. /**
  58. * Re-emits tranmsuxer events by converting them into messages to the
  59. * world outside the worker.
  60. *
  61. * @param {Object} transmuxer the transmuxer to wire events on
  62. * @private
  63. */
  64. const wireTransmuxerEvents = function(transmuxer) {
  65. transmuxer.on('data', function(segment) {
  66. segment.tags = orderTags(segment.tags);
  67. window.postMessage({
  68. action: 'data',
  69. segment
  70. });
  71. });
  72. transmuxer.on('done', function(data) {
  73. window.postMessage({ action: 'done' });
  74. });
  75. };
  76. /**
  77. * All incoming messages route through this hash. If no function exists
  78. * to handle an incoming message, then we ignore the message.
  79. *
  80. * @class MessageHandlers
  81. * @param {Object} options the options to initialize with
  82. */
  83. class MessageHandlers {
  84. constructor(options) {
  85. this.options = options || {};
  86. this.init();
  87. }
  88. /**
  89. * initialize our web worker and wire all the events.
  90. */
  91. init() {
  92. if (this.transmuxer) {
  93. this.transmuxer.dispose();
  94. }
  95. this.transmuxer = new flv.Transmuxer(this.options);
  96. wireTransmuxerEvents(this.transmuxer);
  97. }
  98. /**
  99. * Adds data (a ts segment) to the start of the transmuxer pipeline for
  100. * processing.
  101. *
  102. * @param {ArrayBuffer} data data to push into the muxer
  103. */
  104. push(data) {
  105. // Cast array buffer to correct type for transmuxer
  106. let segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
  107. this.transmuxer.push(segment);
  108. }
  109. /**
  110. * Recreate the transmuxer so that the next segment added via `push`
  111. * start with a fresh transmuxer.
  112. */
  113. reset() {
  114. this.init();
  115. }
  116. /**
  117. * Forces the pipeline to finish processing the last segment and emit it's
  118. * results.
  119. *
  120. * @param {Object} data event data, not really used
  121. */
  122. flush(data) {
  123. this.transmuxer.flush();
  124. }
  125. }
  126. /**
  127. * Our web wroker interface so that things can talk to mux.js
  128. * that will be running in a web worker. the scope is passed to this by
  129. * webworkify.
  130. *
  131. * @param {Object} self the scope for the web worker
  132. */
  133. const Worker = function(self) {
  134. self.onmessage = function(event) {
  135. if (event.data.action === 'init' &amp;&amp; event.data.options) {
  136. this.messageHandlers = new MessageHandlers(event.data.options);
  137. return;
  138. }
  139. if (!this.messageHandlers) {
  140. this.messageHandlers = new MessageHandlers();
  141. }
  142. if (event.data &amp;&amp; event.data.action &amp;&amp; event.data.action !== 'init') {
  143. if (this.messageHandlers[event.data.action]) {
  144. this.messageHandlers[event.data.action](event.data);
  145. }
  146. }
  147. };
  148. };
  149. export default (self) => {
  150. return new Worker(self);
  151. };
  152. </code></pre>
  153. </article>
  154. </section>
  155. </div>
  156. <nav>
  157. <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#endOfStream">endOfStream</a></li><li><a href="global.html#get">get</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#set">set</a></li><li><a href="global.html#supportsNativeMediaSources">supportsNativeMediaSources</a></li><li><a href="global.html#URL">URL</a></li><li><a href="global.html#Worker">Worker</a></li></ul>
  158. </nav>
  159. <br class="clear">
  160. <footer>
  161. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Dec 02 2016 19:56:59 GMT-0500 (EST)
  162. </footer>
  163. <script> prettyPrint(); </script>
  164. <script src="scripts/linenumber.js"> </script>
  165. </body>
  166. </html>