hls-reader.js 3.6 KB

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