hls-reader.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. "use strict";
  2. var Util = require('util');
  3. var StreamEach = require('stream-each'),
  4. oncemore = require('oncemore');
  5. var Readable = require('readable-stream/readable'),
  6. Passthrough = require('readable-stream/passthrough');
  7. var tssmooth = require('./tssmooth');
  8. var SegmentDecrypt = require('./segment-decrypt');
  9. var internals = {
  10. NOOP: function(){},
  11. };
  12. // 'pipe' stream to a Readable
  13. function pump(src, dst, done) {
  14. src.on('data', function(chunk) {
  15. if (!dst.push(chunk)) {
  16. src.pause();
  17. }
  18. });
  19. oncemore(src).once('end', 'error', function(err) {
  20. // TODO: flush source buffer on error?
  21. dst._read = internals.NOOP;
  22. done(err);
  23. });
  24. dst._read = function() {
  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. options = options || {};
  33. Readable.call(this, { lowWaterMark: options.lowWaterMark, highWaterMark: options.highWaterMark });
  34. var self = this;
  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, function (segmentInfo, done) {
  47. self.isReading = true;
  48. return self.decrypt(segmentInfo.stream, segmentInfo.details.key, function (err, stream) {
  49. if (err) {
  50. console.error('decrypt failed', err.stack);
  51. stream = segmentInfo.stream;
  52. }
  53. self.emit('segment', segmentInfo);
  54. stream = oncemore(stream);
  55. if (!self.isHooked) {
  56. // pull data and detect if we need to hook before end
  57. var buffered = 0;
  58. stream.on('data', function(chunk) {
  59. buffered += chunk.length;
  60. if (!self.isHooked && buffered >= self.bufferSize)
  61. self.hook();
  62. });
  63. }
  64. stream.pipe(self.buffer, { end: false });
  65. stream.once('end', 'error', function(err) {
  66. self.isReading = false;
  67. if (err) {
  68. console.error('stream error', err.stack || err);
  69. }
  70. self.hook();
  71. done();
  72. });
  73. });
  74. }, function (err) {
  75. if (err) throw err;
  76. self.buffer.end();
  77. });
  78. // start output if needed
  79. if (!this.sync) {
  80. process.nextTick(function() {
  81. self.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. var self = this;
  92. if (this.isHooked) return;
  93. self.isHooked = true;
  94. var s = this.buffer;
  95. if (this.sync) {
  96. var smooth = tssmooth();
  97. smooth.on('unpipe', function() {
  98. this.unpipe();
  99. });
  100. smooth.on('warning', function(err) {
  101. console.error('smoothing error', err);
  102. });
  103. s = s.pipe(smooth);
  104. }
  105. pump(s, this, function(err) {
  106. if (err) {
  107. return self.emit('error', err);
  108. }
  109. self.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. var hlsreader = module.exports = function hlsreader(segmentReader, options) {
  117. return new HlsReader(segmentReader, options);
  118. };
  119. hlsreader.HlsReader = HlsReader;