Improve buffer compensation

This commit is contained in:
Calvin Montgomery 2013-04-24 08:04:26 +04:00
parent 35fb504847
commit dc5fa70017
1 changed files with 17 additions and 11 deletions

View File

@ -1,7 +1,7 @@
var Media = function(data) { var Media = function(data) {
this.id = data.id; this.id = data.id;
this.type = data.type; this.type = data.type;
this.lastdiff = 0; this.diff = 0;
switch(this.type) { switch(this.type) {
case "yt": case "yt":
@ -279,6 +279,7 @@ Media.prototype.initRTMP = function() {
} }
Media.prototype.update = function(data) { Media.prototype.update = function(data) {
console.log(parseInt(data.currentTime / 60), parseInt(data.currentTime % 60));
if(data.id != this.id) { if(data.id != this.id) {
if(data.currentTime < 0) { if(data.currentTime < 0) {
data.currentTime = 0; data.currentTime = 0;
@ -288,22 +289,27 @@ Media.prototype.update = function(data) {
if(data.paused) { if(data.paused) {
this.pause(); this.pause();
} }
if(LEADER) {
return;
}
this.getTime(function(seconds) { this.getTime(function(seconds) {
var time = data.currentTime; var time = data.currentTime;
if(readCookie("sync_compensate")) { var diff = time - seconds || time;
var diff = time - seconds;
if(diff > 0) { var a = SYNC_THRESHOLD + 1;
diff = diff > 5 ? 5 : diff; // If 2 updates in a row have lag, compensate for buffering
time += diff; if(diff >= a && diff <= 6 && this.diff >= a && this.diff <= 6) {
diff = (diff + this.lastdiff) / 2 || 0; this.seek(time + diff);
this.lastdiff = diff;
} }
else if(diff < -2 || diff >= SYNC_THRESHOLD) {
if(diff < 0) {
this.seek(time + 0.5);
} }
if(Math.abs(data.currentTime - seconds) > SYNC_THRESHOLD) { else {
if(!LEADER) {
this.seek(time); this.seek(time);
} }
} }
this.diff = diff;
}.bind(this)); }.bind(this));
} }