hls-reader.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. 'use strict';
  2. const Util = require('util');
  3. const Readable = require('readable-stream/readable');
  4. const Passthrough = require('readable-stream/passthrough');
  5. const StreamEach = require('stream-each');
  6. const Oncemore = require('oncemore');
  7. const Pati = require('pati');
  8. const TsSmooth = require('./tssmooth');
  9. const SegmentDecrypt = require('./segment-decrypt');
  10. const internals = {
  11. NOOP: function(){},
  12. };
  13. // 'pipe' stream to a Readable
  14. internals.pump = function(src, dst, done) {
  15. src.on('data', (chunk) => {
  16. if (!dst.push(chunk)) {
  17. src.pause();
  18. }
  19. });
  20. Oncemore(src).once('end', 'error', (err) => {
  21. // TODO: flush source buffer on error?
  22. dst._read = internals.NOOP;
  23. done(err);
  24. });
  25. dst._read = () => {
  26. src.resume();
  27. };
  28. }
  29. // TODO: use pipe as interface to segment-reader?
  30. function HlsReader(segmentReader, options) {
  31. if (!(this instanceof HlsReader)) {
  32. return new HlsReader(segmentReader, options);
  33. }
  34. options = options || {};
  35. Readable.call(this, { lowWaterMark: options.lowWaterMark, highWaterMark: options.highWaterMark });
  36. this.reader = segmentReader;
  37. this.sync = !!options.sync; // output in real-time
  38. this.bufferSize = ~~options.bufferSize;
  39. this.cookie = options.cookie;
  40. this.key = options.key;
  41. if (options.key && !Buffer.isBuffer(options.key) && options.key.length !== 32) {
  42. throw new TypeError('key must be a 32 byte Buffer');
  43. }
  44. this.isReading = false;
  45. this.isHooked = false;
  46. this.buffer = new Passthrough({ highWaterMark: this.bufferSize });
  47. const processStreams = () => {
  48. StreamEach(this.reader, this.process.bind(this), (err) => {
  49. if (err) {
  50. return processStreams(); // retry
  51. }
  52. this.buffer.end();
  53. });
  54. };
  55. if (this.reader.index) {
  56. processStreams();
  57. } else {
  58. this.reader.once('index', processStreams);
  59. }
  60. // start output if needed
  61. if (!this.sync) {
  62. process.nextTick(() => {
  63. this.hook();
  64. });
  65. }
  66. }
  67. Util.inherits(HlsReader, Readable);
  68. HlsReader.prototype._read = internals.NOOP;
  69. HlsReader.prototype.destroy = function () {
  70. };
  71. HlsReader.prototype.process = async function(segmentInfo, done) {
  72. let result;
  73. try {
  74. this.isReading = true;
  75. let stream;
  76. try {
  77. stream = await this.decrypt(segmentInfo.stream, segmentInfo.segment && segmentInfo.segment.details.keys);
  78. }
  79. catch (err) {
  80. console.error('decrypt failed', err.stack);
  81. stream = segmentInfo.stream;
  82. }
  83. this.emit('segment', segmentInfo);
  84. const dispatcher = new Pati.EventDispatcher(stream);
  85. dispatcher.on('end', Pati.EventDispatcher.end);
  86. if (!this.isHooked) {
  87. // pull data and detect if we need to hook before end
  88. let buffered = 0;
  89. dispatcher.on('data', (chunk) => {
  90. buffered += chunk.length;
  91. if (!this.isHooked && buffered >= this.bufferSize) {
  92. this.hook();
  93. }
  94. });
  95. }
  96. stream.pipe(this.buffer, { end: false });
  97. try {
  98. await dispatcher.finish();
  99. }
  100. catch (err) {
  101. console.error('stream error', err.stack || err);
  102. }
  103. this.isReading = false;
  104. this.hook();
  105. }
  106. catch (err) {
  107. console.error('process error', err.stack || err);
  108. result = err;
  109. }
  110. finally {
  111. done(result);
  112. }
  113. };
  114. // the hook is used to prebuffer
  115. HlsReader.prototype.hook = function hook() {
  116. if (this.isHooked) return;
  117. this.isHooked = true;
  118. let s = this.buffer;
  119. if (this.sync) {
  120. let smooth = TsSmooth();
  121. smooth.on('unpipe', () => {
  122. this.unpipe();
  123. });
  124. smooth.on('warning', (err) => {
  125. console.error('smoothing error', err);
  126. });
  127. s = s.pipe(smooth);
  128. }
  129. internals.pump(s, this, (err) => {
  130. if (err) {
  131. return this.emit('error', err);
  132. }
  133. this.push(null);
  134. });
  135. this.emit('ready');
  136. };
  137. HlsReader.prototype.decrypt = function(stream, keyAttrs, next) {
  138. return SegmentDecrypt.decrypt(stream, keyAttrs, { base: this.reader.baseUrl, key: this.key, cookie: this.cookie }, next);
  139. };
  140. const hlsreader = module.exports = function hlsreader(segmentReader, options) {
  141. return new HlsReader(segmentReader, options);
  142. };
  143. hlsreader.HlsReader = HlsReader;