hls-reader.js 3.6 KB

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