Просмотр исходного кода

add index stall detection to prevent infinite reading on stopped streams

Gil Pedersen 12 лет назад
Родитель
Сommit
1ef078ee0e
2 измененных файлов с 19 добавлено и 4 удалено
  1. 1 0
      bin/hlsrecord
  2. 18 4
      lib/reader.js

+ 1 - 0
bin/hlsrecord

@@ -56,6 +56,7 @@ if (hlsrecord.endDate)
 var options = {
   startDate: hlsrecord.beginDate,
   stopDate: hlsrecord.endDate,
+  maxStallTime: 5*60*1000,
   fullStream:true,
   highWaterMark:0,
 };

+ 18 - 4
lib/reader.js

@@ -127,8 +127,10 @@ function HlsStreamReader(src, options) {
   this.noData = !!options.noData;
 
   // dates are inclusive
-  this.startDate = options.startDate;
-  this.stopDate = options.stopDate; 
+  this.startDate = new Date(options.startDate);
+  this.stopDate = new Date(options.stopDate);
+
+  this.maxStallTime = options.maxStallTime || Infinity;
 
   this.index = null;
   this.readState = {
@@ -138,11 +140,21 @@ function HlsStreamReader(src, options) {
   };
   this.watch = {}; // used to stop buffering on expired segments
 
+  this.indexStallSince = null;
+
   function getUpdateInterval(updated) {
-    if (updated && self.index.segments.length)
+    if (updated && self.index.segments.length) {
+      self.indexStallSince = null;
       return Math.min(self.index.target_duration, self.index.segments[self.index.segments.length-1].duration);
-    else
+    } else {
+      if (self.indexStallSince) {
+        if ((Date.now() - +self.indexStallSince) > self.maxStallTime)
+          return -1;
+      } else {
+        self.indexStallSince = new Date();
+      }
       return self.index.target_duration / 2;
+    }
   }
 
   function initialSeqNo() {
@@ -200,6 +212,8 @@ function HlsStreamReader(src, options) {
 
     if (self.index && !self.index.ended && self.readable) {
       var updateInterval = getUpdateInterval(updated);
+      if (updateInterval <= 0)
+        return self.emit('error', new Error('index stall'));
       debug('scheduling index refresh', updateInterval);
       setTimeout(updateindex, Math.max(1, updateInterval)*1000);
     }