Selaa lähdekoodia

Move processing to process function

Gil Pedersen 9 vuotta sitten
vanhempi
commit
7f781ce92e
1 muutettua tiedostoa jossa 40 lisäystä ja 38 poistoa
  1. 40 38
      lib/hls-reader.js

+ 40 - 38
lib/hls-reader.js

@@ -64,44 +64,7 @@ function HlsReader(segmentReader, options) {
   this.isHooked = false;
   this.isHooked = false;
   this.buffer = new Passthrough({ highWaterMark: this.bufferSize });
   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;
     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
 // the hook is used to prebuffer
 HlsReader.prototype.hook = function hook() {
 HlsReader.prototype.hook = function hook() {