flash-transmuxer-worker.js.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: flash-transmuxer-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-transmuxer-worker.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @file flash-transmuxer-worker.js
  21. */
  22. import window from 'global/window';
  23. import flv from 'mux.js/lib/flv';
  24. /**
  25. * Re-emits transmuxer events by converting them into messages to the
  26. * world outside the worker.
  27. *
  28. * @param {Object} transmuxer the transmuxer to wire events on
  29. * @private
  30. */
  31. const wireTransmuxerEvents = function(transmuxer) {
  32. transmuxer.on('data', function(segment) {
  33. window.postMessage({
  34. action: 'data',
  35. segment
  36. });
  37. });
  38. transmuxer.on('done', function(data) {
  39. window.postMessage({ action: 'done' });
  40. });
  41. };
  42. /**
  43. * All incoming messages route through this hash. If no function exists
  44. * to handle an incoming message, then we ignore the message.
  45. *
  46. * @class MessageHandlers
  47. * @param {Object} options the options to initialize with
  48. */
  49. class MessageHandlers {
  50. constructor(options) {
  51. this.options = options || {};
  52. this.init();
  53. }
  54. /**
  55. * initialize our web worker and wire all the events.
  56. */
  57. init() {
  58. if (this.transmuxer) {
  59. this.transmuxer.dispose();
  60. }
  61. this.transmuxer = new flv.Transmuxer(this.options);
  62. wireTransmuxerEvents(this.transmuxer);
  63. }
  64. /**
  65. * Adds data (a ts segment) to the start of the transmuxer pipeline for
  66. * processing.
  67. *
  68. * @param {ArrayBuffer} data data to push into the muxer
  69. */
  70. push(data) {
  71. // Cast array buffer to correct type for transmuxer
  72. let segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
  73. this.transmuxer.push(segment);
  74. }
  75. /**
  76. * Recreate the transmuxer so that the next segment added via `push`
  77. * start with a fresh transmuxer.
  78. */
  79. reset() {
  80. this.init();
  81. }
  82. /**
  83. * Forces the pipeline to finish processing the last segment and emit its
  84. * results.
  85. */
  86. flush() {
  87. this.transmuxer.flush();
  88. }
  89. resetCaptions() {
  90. this.transmuxer.resetCaptions();
  91. }
  92. }
  93. /**
  94. * Our web wroker interface so that things can talk to mux.js
  95. * that will be running in a web worker. The scope is passed to this by
  96. * webworkify.
  97. *
  98. * @param {Object} self the scope for the web worker
  99. */
  100. const FlashTransmuxerWorker = function(self) {
  101. self.onmessage = function(event) {
  102. if (event.data.action === 'init' &amp;&amp; event.data.options) {
  103. this.messageHandlers = new MessageHandlers(event.data.options);
  104. return;
  105. }
  106. if (!this.messageHandlers) {
  107. this.messageHandlers = new MessageHandlers();
  108. }
  109. if (event.data &amp;&amp; event.data.action &amp;&amp; event.data.action !== 'init') {
  110. if (this.messageHandlers[event.data.action]) {
  111. this.messageHandlers[event.data.action](event.data);
  112. }
  113. }
  114. };
  115. };
  116. export default (self) => {
  117. return new FlashTransmuxerWorker(self);
  118. };
  119. </code></pre>
  120. </article>
  121. </section>
  122. </div>
  123. <nav>
  124. <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>
  125. </nav>
  126. <br class="clear">
  127. <footer>
  128. 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)
  129. </footer>
  130. <script> prettyPrint(); </script>
  131. <script src="scripts/linenumber.js"> </script>
  132. </body>
  133. </html>