|
|
@@ -40,6 +40,9 @@ function TsLimit() {
|
|
|
this.pcr = -1;
|
|
|
this.last = null;
|
|
|
|
|
|
+ this.bitrate = 10E06;
|
|
|
+ this.time = null;
|
|
|
+
|
|
|
this.pcr2time = function(pcr) {
|
|
|
if (self.pcr === -1) {
|
|
|
self.pcr = pcr;
|
|
|
@@ -52,6 +55,10 @@ function TsLimit() {
|
|
|
return self.last + pcr_delta;
|
|
|
}
|
|
|
|
|
|
+ this.bytes2delta = function(bytes) {
|
|
|
+ return bytes*8*1E6 / self.bitrate; /* useconds */
|
|
|
+ }
|
|
|
+
|
|
|
return this;
|
|
|
}
|
|
|
util.inherits(TsLimit, Transform);
|
|
|
@@ -69,13 +76,16 @@ TsLimit.prototype._transform = function(chunk, output, cb) {
|
|
|
var buf = self.buffer;
|
|
|
var end = buf.length-188-1;
|
|
|
var now = utime();
|
|
|
+ if (!self.time) self.time = now;
|
|
|
+
|
|
|
+ var packet_time = self.bytes2delta(188);
|
|
|
for (var i=0; i<end; i+=188) {
|
|
|
+ self.time += packet_time;
|
|
|
var pcr = parsePCR(buf.slice(i, i+188));
|
|
|
- if (pcr !== -1) {
|
|
|
- var pcrtime = self.pcr2time(pcr);
|
|
|
- if (pcrtime > now)
|
|
|
- break;
|
|
|
- }
|
|
|
+ if (pcr !== -1)
|
|
|
+ self.time = self.pcr2time(pcr);
|
|
|
+ if (self.time > now)
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
// TODO: limit output speed
|
|
|
@@ -83,8 +93,7 @@ TsLimit.prototype._transform = function(chunk, output, cb) {
|
|
|
self.buffer = buf.slice(i);
|
|
|
|
|
|
if (i < end) {
|
|
|
- // TODO: calculate timeout?
|
|
|
- return setTimeout(process, 5);
|
|
|
+ return setTimeout(process, (self.time - now) / 1000);
|
|
|
}
|
|
|
cb();
|
|
|
}
|