hls-reader.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. StreamEach(this.reader, this.process.bind(this), (err) => {
  48. if (err) throw err;
  49. this.buffer.end();
  50. });
  51. // start output if needed
  52. if (!this.sync) {
  53. process.nextTick(() => {
  54. this.hook();
  55. });
  56. }
  57. }
  58. Util.inherits(HlsReader, Readable);
  59. HlsReader.prototype._read = internals.NOOP;
  60. HlsReader.prototype.destroy = function () {
  61. };
  62. HlsReader.prototype.process = async function(segmentInfo, done) {
  63. let result;
  64. try {
  65. this.isReading = true;
  66. let stream;
  67. try {
  68. stream = await this.decrypt(segmentInfo.stream, segmentInfo.segment && segmentInfo.segment.details.keys);
  69. }
  70. catch (err) {
  71. console.error('decrypt failed', err.stack);
  72. stream = segmentInfo.stream;
  73. }
  74. this.emit('segment', segmentInfo);
  75. const dispatcher = new Pati.EventDispatcher(stream);
  76. dispatcher.on('end', Pati.EventDispatcher.end);
  77. if (!this.isHooked) {
  78. // pull data and detect if we need to hook before end
  79. let buffered = 0;
  80. dispatcher.on('data', (chunk) => {
  81. buffered += chunk.length;
  82. if (!this.isHooked && buffered >= this.bufferSize) {
  83. this.hook();
  84. }
  85. });
  86. }
  87. stream.pipe(this.buffer, { end: false });
  88. try {
  89. await dispatcher.finish();
  90. }
  91. catch (err) {
  92. console.error('stream error', err.stack || err);
  93. }
  94. this.isReading = false;
  95. this.hook();
  96. }
  97. catch (err) {
  98. result = err;
  99. }
  100. finally {
  101. done(result);
  102. }
  103. };
  104. // the hook is used to prebuffer
  105. HlsReader.prototype.hook = function hook() {
  106. if (this.isHooked) return;
  107. this.isHooked = true;
  108. let s = this.buffer;
  109. if (this.sync) {
  110. let smooth = TsSmooth();
  111. smooth.on('unpipe', () => {
  112. this.unpipe();
  113. });
  114. smooth.on('warning', (err) => {
  115. console.error('smoothing error', err);
  116. });
  117. s = s.pipe(smooth);
  118. }
  119. internals.pump(s, this, (err) => {
  120. if (err) {
  121. return this.emit('error', err);
  122. }
  123. this.push(null);
  124. });
  125. this.emit('ready');
  126. };
  127. HlsReader.prototype.decrypt = function(stream, keyAttrs, next) {
  128. return SegmentDecrypt.decrypt(stream, keyAttrs, { base: this.reader.baseUrl, key: this.key, cookie: this.cookie }, next);
  129. };
  130. const hlsreader = module.exports = function hlsreader(segmentReader, options) {
  131. return new HlsReader(segmentReader, options);
  132. };
  133. hlsreader.HlsReader = HlsReader;