transmuxer-worker.js.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: 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: transmuxer-worker.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @file transmuxer-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 mp4 from 'mux.js/lib/mp4';
  34. /**
  35. * Re-emits transmuxer events by converting them into messages to the
  36. * world outside the worker.
  37. *
  38. * @param {Object} transmuxer the transmuxer to wire events on
  39. * @private
  40. */
  41. const wireTransmuxerEvents = function(transmuxer) {
  42. transmuxer.on('data', function(segment) {
  43. // transfer ownership of the underlying ArrayBuffer
  44. // instead of doing a copy to save memory
  45. // ArrayBuffers are transferable but generic TypedArrays are not
  46. // @link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Passing_data_by_transferring_ownership_(transferable_objects)
  47. let initArray = segment.initSegment;
  48. segment.initSegment = {
  49. data: initArray.buffer,
  50. byteOffset: initArray.byteOffset,
  51. byteLength: initArray.byteLength
  52. };
  53. let typedArray = segment.data;
  54. segment.data = typedArray.buffer;
  55. window.postMessage({
  56. action: 'data',
  57. segment,
  58. byteOffset: typedArray.byteOffset,
  59. byteLength: typedArray.byteLength
  60. }, [segment.data]);
  61. });
  62. if (transmuxer.captionStream) {
  63. transmuxer.captionStream.on('data', function(caption) {
  64. window.postMessage({
  65. action: 'caption',
  66. data: caption
  67. });
  68. });
  69. }
  70. transmuxer.on('done', function(data) {
  71. window.postMessage({ action: 'done' });
  72. });
  73. transmuxer.on('gopInfo', function(gopInfo) {
  74. window.postMessage({
  75. action: 'gopInfo',
  76. gopInfo
  77. });
  78. });
  79. };
  80. /**
  81. * All incoming messages route through this hash. If no function exists
  82. * to handle an incoming message, then we ignore the message.
  83. *
  84. * @class MessageHandlers
  85. * @param {Object} options the options to initialize with
  86. */
  87. class MessageHandlers {
  88. constructor(options) {
  89. this.options = options || {};
  90. this.init();
  91. }
  92. /**
  93. * initialize our web worker and wire all the events.
  94. */
  95. init() {
  96. if (this.transmuxer) {
  97. this.transmuxer.dispose();
  98. }
  99. this.transmuxer = new mp4.Transmuxer(this.options);
  100. wireTransmuxerEvents(this.transmuxer);
  101. }
  102. /**
  103. * Adds data (a ts segment) to the start of the transmuxer pipeline for
  104. * processing.
  105. *
  106. * @param {ArrayBuffer} data data to push into the muxer
  107. */
  108. push(data) {
  109. // Cast array buffer to correct type for transmuxer
  110. let segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
  111. this.transmuxer.push(segment);
  112. }
  113. /**
  114. * Recreate the transmuxer so that the next segment added via `push`
  115. * start with a fresh transmuxer.
  116. */
  117. reset() {
  118. this.init();
  119. }
  120. /**
  121. * Set the value that will be used as the `baseMediaDecodeTime` time for the
  122. * next segment pushed in. Subsequent segments will have their `baseMediaDecodeTime`
  123. * set relative to the first based on the PTS values.
  124. *
  125. * @param {Object} data used to set the timestamp offset in the muxer
  126. */
  127. setTimestampOffset(data) {
  128. let timestampOffset = data.timestampOffset || 0;
  129. this.transmuxer.setBaseMediaDecodeTime(Math.round(timestampOffset * 90000));
  130. }
  131. setAudioAppendStart(data) {
  132. this.transmuxer.setAudioAppendStart(Math.ceil(data.appendStart * 90000));
  133. }
  134. /**
  135. * Forces the pipeline to finish processing the last segment and emit it's
  136. * results.
  137. *
  138. * @param {Object} data event data, not really used
  139. */
  140. flush(data) {
  141. this.transmuxer.flush();
  142. }
  143. resetCaptions() {
  144. this.transmuxer.resetCaptions();
  145. }
  146. alignGopsWith(data) {
  147. this.transmuxer.alignGopsWith(data.gopsToAlignWith.slice());
  148. }
  149. }
  150. /**
  151. * Our web wroker interface so that things can talk to mux.js
  152. * that will be running in a web worker. the scope is passed to this by
  153. * webworkify.
  154. *
  155. * @param {Object} self the scope for the web worker
  156. */
  157. const TransmuxerWorker = function(self) {
  158. self.onmessage = function(event) {
  159. if (event.data.action === 'init' &amp;&amp; event.data.options) {
  160. this.messageHandlers = new MessageHandlers(event.data.options);
  161. return;
  162. }
  163. if (!this.messageHandlers) {
  164. this.messageHandlers = new MessageHandlers();
  165. }
  166. if (event.data &amp;&amp; event.data.action &amp;&amp; event.data.action !== 'init') {
  167. if (this.messageHandlers[event.data.action]) {
  168. this.messageHandlers[event.data.action](event.data);
  169. }
  170. }
  171. };
  172. };
  173. export default (self) => {
  174. return new TransmuxerWorker(self);
  175. };
  176. </code></pre>
  177. </article>
  178. </section>
  179. </div>
  180. <nav>
  181. <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>
  182. </nav>
  183. <br class="clear">
  184. <footer>
  185. 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)
  186. </footer>
  187. <script> prettyPrint(); </script>
  188. <script src="scripts/linenumber.js"> </script>
  189. </body>
  190. </html>