|
|
@@ -64,44 +64,7 @@ function HlsReader(segmentReader, options) {
|
|
|
this.isHooked = false;
|
|
|
this.buffer = new Passthrough({ highWaterMark: this.bufferSize });
|
|
|
|
|
|
- StreamEach(this.reader, (segmentInfo, done) => {
|
|
|
-
|
|
|
- this.isReading = true;
|
|
|
-
|
|
|
- return this.decrypt(segmentInfo.stream, segmentInfo.details.keys, (err, stream) => {
|
|
|
-
|
|
|
- if (err) {
|
|
|
- console.error('decrypt failed', err.stack);
|
|
|
- stream = segmentInfo.stream;
|
|
|
- }
|
|
|
-
|
|
|
- this.emit('segment', segmentInfo);
|
|
|
-
|
|
|
- stream = Oncemore(stream);
|
|
|
-
|
|
|
- if (!this.isHooked) {
|
|
|
- // pull data and detect if we need to hook before end
|
|
|
- let buffered = 0;
|
|
|
- stream.on('data', (chunk) => {
|
|
|
-
|
|
|
- buffered += chunk.length;
|
|
|
- if (!this.isHooked && buffered >= this.bufferSize)
|
|
|
- this.hook();
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- stream.pipe(this.buffer, { end: false });
|
|
|
- stream.once('end', 'error', (err) => {
|
|
|
-
|
|
|
- this.isReading = false;
|
|
|
- if (err) {
|
|
|
- console.error('stream error', err.stack || err);
|
|
|
- }
|
|
|
- this.hook();
|
|
|
- done();
|
|
|
- });
|
|
|
- });
|
|
|
- }, (err) => {
|
|
|
+ StreamEach(this.reader, this.process.bind(this), (err) => {
|
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
|
@@ -124,6 +87,45 @@ HlsReader.prototype.destroy = function () {
|
|
|
|
|
|
};
|
|
|
|
|
|
+HlsReader.prototype.process = function(segmentInfo, done) {
|
|
|
+
|
|
|
+ this.isReading = true;
|
|
|
+
|
|
|
+ return this.decrypt(segmentInfo.stream, segmentInfo.details.keys, (err, stream) => {
|
|
|
+
|
|
|
+ if (err) {
|
|
|
+ console.error('decrypt failed', err.stack);
|
|
|
+ stream = segmentInfo.stream;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.emit('segment', segmentInfo);
|
|
|
+
|
|
|
+ stream = Oncemore(stream);
|
|
|
+
|
|
|
+ if (!this.isHooked) {
|
|
|
+ // pull data and detect if we need to hook before end
|
|
|
+ let buffered = 0;
|
|
|
+ stream.on('data', (chunk) => {
|
|
|
+
|
|
|
+ buffered += chunk.length;
|
|
|
+ if (!this.isHooked && buffered >= this.bufferSize)
|
|
|
+ this.hook();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ stream.pipe(this.buffer, { end: false });
|
|
|
+ stream.once('end', 'error', (err) => {
|
|
|
+
|
|
|
+ this.isReading = false;
|
|
|
+ if (err) {
|
|
|
+ console.error('stream error', err.stack || err);
|
|
|
+ }
|
|
|
+ this.hook();
|
|
|
+ done();
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
// the hook is used to prebuffer
|
|
|
HlsReader.prototype.hook = function hook() {
|
|
|
|