Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jmrunge/out of sync timeout #148

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions heartbeats.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ heartbeats.prototype.syncMelted = function() {
logger.info("[syncMelted Expected and playing medias are the same");
if (Math.abs(meltedClip.currentFrame - expected.frame) > expected.media.get('fps')) {
logger.info("[syncMelted] I'm over 1 second off")
result = result.then(self.fixMelted.bind(self, expected));
result = result.then(self.fixMelted.bind(self, expected, expected.media.get('live')));
}
}
if (meltedStatus.status !== "playing") {
Expand Down Expand Up @@ -267,10 +267,12 @@ heartbeats.prototype.handleNoMedias = function() {
logger.error("No medias loaded!");
};

heartbeats.prototype.fixMelted = function(expected) {
heartbeats.prototype.fixMelted = function(expected, live) {
var self = this;
logger.warn("Melted is out of sync!");
logger.warn("Melted is out of sync! (Live: %s)", live ? true : false);
self.emit("outOfSync", expected);
if (live)
return;
return self.server.goto(expected.media.get('actual_order'), expected.frame);
};

Expand Down
3 changes: 2 additions & 1 deletion models/Mosto.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Mosto.Media = Backbone.Model.extend({
// end: undefined,
in: undefined,
out: undefined,
blank: false
blank: false,
live: false
},

constructor: function(attributes, options) {
Expand Down
49 changes: 49 additions & 0 deletions mosto.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,56 @@ function mosto(customConfig) {
this.meltedInterval = undefined;
/* TODO: This should be in config */
this.restartMelted = true;

/* OUT OF SYNC TIMEOUT */
this.checkOutOfSyncTimeout = true;
this.outOfSyncTimeout = 5; /* seconds */
this.outOfSyncTolerance = 5; /* times */
this.outOfSyncCount = 0;
this.outOfSyncStarted = false;

events.EventEmitter.call(this);
}

util.inherits(mosto, events.EventEmitter);

mosto.prototype.checkSyncTimeout = function() {
logger.debug("Checking sync timeout");
if (!this.checkOutOfSyncTimeout) {
logger.debug("Reloading playlists, aborting check...");
return;
}
var now = moment().valueOf();
if (this.outOfSyncStarted) {
logger.debug("Sync timeout already started");
var limit = this.outOfSyncStarted + (this.outOfSyncTimeout * 1000);
if (now >= limit) {
logger.debug("Sync timeout exceeded limit, reseting counters");
this.outOfSyncCount = 0;
this.outOfSyncStarted = false;
} else {
this.outOfSyncCount++;
logger.debug("Sync timeout #%d received", this.outOfSyncCount);
if (this.outOfSyncCount === this.outOfSyncTolerance) {
logger.debug("Sync timeout tolerance reached, reloading playlists");
this.checkOutOfSyncTimeout = false;
this.playlists.save();
var self = this;
this.playlists.get("melted_medias").write.take(function() {
self.outOfSyncCount = 0;
self.outOfSyncStarted = false;
self.checkOutOfSyncTimeout = true;
self.playlists.get("melted_medias").write.leave();
});
}
}
} else {
logger.debug("Starting sync timeout check");
this.outOfSyncCount++;
this.outOfSyncStarted = moment().valueOf();
}
};

mosto.prototype.getTimeWindow = function(from, to) {
var window = {
from: from || moment(),
Expand Down Expand Up @@ -182,6 +226,11 @@ mosto.prototype.initHeartbeats = function() {
self.emit('playing');
});

self.heartbeats.on('outOfSync', function(expected) {
if (!expected.media.get('live'))
self.checkSyncTimeout();
});

self.heartbeats.init();
};

Expand Down