flash-transmuxer-worker.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @file flash-transmuxer-worker.js
  3. */
  4. 'use strict';
  5. Object.defineProperty(exports, '__esModule', {
  6. value: true
  7. });
  8. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  10. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  11. var _globalWindow = require('global/window');
  12. var _globalWindow2 = _interopRequireDefault(_globalWindow);
  13. var _muxJsLibFlv = require('mux.js/lib/flv');
  14. var _muxJsLibFlv2 = _interopRequireDefault(_muxJsLibFlv);
  15. /**
  16. * Re-emits transmuxer events by converting them into messages to the
  17. * world outside the worker.
  18. *
  19. * @param {Object} transmuxer the transmuxer to wire events on
  20. * @private
  21. */
  22. var wireTransmuxerEvents = function wireTransmuxerEvents(transmuxer) {
  23. transmuxer.on('data', function (segment) {
  24. _globalWindow2['default'].postMessage({
  25. action: 'data',
  26. segment: segment
  27. });
  28. });
  29. transmuxer.on('done', function (data) {
  30. _globalWindow2['default'].postMessage({ action: 'done' });
  31. });
  32. };
  33. /**
  34. * All incoming messages route through this hash. If no function exists
  35. * to handle an incoming message, then we ignore the message.
  36. *
  37. * @class MessageHandlers
  38. * @param {Object} options the options to initialize with
  39. */
  40. var MessageHandlers = (function () {
  41. function MessageHandlers(options) {
  42. _classCallCheck(this, MessageHandlers);
  43. this.options = options || {};
  44. this.init();
  45. }
  46. /**
  47. * Our web wroker interface so that things can talk to mux.js
  48. * that will be running in a web worker. The scope is passed to this by
  49. * webworkify.
  50. *
  51. * @param {Object} self the scope for the web worker
  52. */
  53. /**
  54. * initialize our web worker and wire all the events.
  55. */
  56. _createClass(MessageHandlers, [{
  57. key: 'init',
  58. value: function init() {
  59. if (this.transmuxer) {
  60. this.transmuxer.dispose();
  61. }
  62. this.transmuxer = new _muxJsLibFlv2['default'].Transmuxer(this.options);
  63. wireTransmuxerEvents(this.transmuxer);
  64. }
  65. /**
  66. * Adds data (a ts segment) to the start of the transmuxer pipeline for
  67. * processing.
  68. *
  69. * @param {ArrayBuffer} data data to push into the muxer
  70. */
  71. }, {
  72. key: 'push',
  73. value: function push(data) {
  74. // Cast array buffer to correct type for transmuxer
  75. var segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
  76. this.transmuxer.push(segment);
  77. }
  78. /**
  79. * Recreate the transmuxer so that the next segment added via `push`
  80. * start with a fresh transmuxer.
  81. */
  82. }, {
  83. key: 'reset',
  84. value: function reset() {
  85. this.init();
  86. }
  87. /**
  88. * Forces the pipeline to finish processing the last segment and emit its
  89. * results.
  90. */
  91. }, {
  92. key: 'flush',
  93. value: function flush() {
  94. this.transmuxer.flush();
  95. }
  96. }, {
  97. key: 'resetCaptions',
  98. value: function resetCaptions() {
  99. this.transmuxer.resetCaptions();
  100. }
  101. }]);
  102. return MessageHandlers;
  103. })();
  104. var FlashTransmuxerWorker = function FlashTransmuxerWorker(self) {
  105. self.onmessage = function (event) {
  106. if (event.data.action === 'init' && event.data.options) {
  107. this.messageHandlers = new MessageHandlers(event.data.options);
  108. return;
  109. }
  110. if (!this.messageHandlers) {
  111. this.messageHandlers = new MessageHandlers();
  112. }
  113. if (event.data && event.data.action && event.data.action !== 'init') {
  114. if (this.messageHandlers[event.data.action]) {
  115. this.messageHandlers[event.data.action](event.data);
  116. }
  117. }
  118. };
  119. };
  120. exports['default'] = function (self) {
  121. return new FlashTransmuxerWorker(self);
  122. };
  123. module.exports = exports['default'];