From 9a3563ca450225dbfa5d3c01c60c2ec74a7c78ad Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sat, 29 Jun 2013 20:59:33 -0400 Subject: [PATCH] Continue work on playlist changes --- channel.js | 12 +++-- playlist.js | 104 ++++++++++++++++++++++++++++++++++--- www/assets/js/callbacks.js | 12 +++-- 3 files changed, 111 insertions(+), 17 deletions(-) diff --git a/channel.js b/channel.js index db8f145c..9b71b5cd 100644 --- a/channel.js +++ b/channel.js @@ -35,7 +35,7 @@ var Channel = function(name) { // Initialize defaults this.registered = false; this.users = []; - this.playlist = new Playlist(); + this.playlist = new Playlist(this); this.library = {}; this.position = -1; this.drinks = 0; @@ -170,7 +170,7 @@ Channel.prototype.loadDump = function() { } } else if(data.playlist) { - this.playlist = new Playlist(data.playlist); + //TODO fix this.playlist.current = this.playlist.find(data.currentUID) || null; } this.sendAll("playlist", this.playlist.toArray()); @@ -1334,7 +1334,7 @@ Channel.prototype.jumpTo = function(uid) { if(this.leader == null && !isLive(this.playlist.current.media.type)) { this.time = new Date().getTime(); if(this.playlist.current.media.uid != oid) { - mediaUpdate(this, this.playlist.current.media); + //mediaUpdate(this, this.playlist.current.media); } } } @@ -1373,7 +1373,7 @@ Channel.prototype.shufflequeue = function() { n.push(pl[i]); pl.splice(i, 1); } - this.playlist = new Playlist(n); + // TODO fix this.playlist.current = this.playlist.last; this.playNext(); this.sendAll("playlist", this.playlist.toArray()); @@ -1920,17 +1920,21 @@ Channel.prototype.changeLeader = function(name) { } if(name == "") { this.logger.log("*** Resuming autolead"); + /* if(this.playlist.current != null && !isLive(this.playlist.current.media.type)) { this.playlist.current.media.paused = false; this.time = new Date().getTime(); this.i = 0; mediaUpdate(this, this.playlist.current.media.id); } + */ + this.playlist.lead(true); return; } for(var i = 0; i < this.users.length; i++) { if(this.users[i].name == name) { this.logger.log("*** Assigned leader: " + name); + this.playlist.lead(false); this.leader = this.users[i]; if(this.users[i].rank < 1.5) { this.users[i].oldrank = this.users[i].rank; diff --git a/playlist.js b/playlist.js index 8ec5c620..2beeae03 100644 --- a/playlist.js +++ b/playlist.js @@ -16,17 +16,42 @@ PlaylistItem.prototype.pack = function() { }; } -function Playlist(items) { +function Playlist(chan) { this.next_uid = 0; this.first = null; this.last = null; this.current = null; this.length = 0; + this._leadInterval = false; + this._lastUpdate = 0; + this._counter = 0; + this.leading = true; + this.callbacks = { + "changeMedia": [], + "mediaUpdate": [] + }; - if(items !== undefined) { - items.forEach(function(it) { - this.append(it); + if(chan) { + this.on("mediaUpdate", function(m) { + chan.sendAll("mediaUpdate", m.timeupdate()); }); + this.on("changeMedia", function(m) { + chan.sendAll("changeMedia", m.fullupdate()); + }); + } +} + +Playlist.prototype.on = function(ev, fn) { + if(typeof fn === "undefined") { + var pl = this; + return function() { + for(var i = 0; i < pl.callbacks[ev].length; i++) { + pl.callbacks[ev][i].apply(this, arguments); + } + } + } + else if(typeof fn === "function") { + this.callbacks[ev].push(fn); } } @@ -59,6 +84,7 @@ Playlist.prototype.prepend = function(plitem) { else { this.current = plitem; this.last = plitem; + this.startPlayback(); } this.first = plitem; this.first.prev = null; @@ -75,6 +101,7 @@ Playlist.prototype.append = function(plitem) { else { this.first = plitem; this.current = plitem; + this.startPlayback(); } this.last = plitem; this.last.next = null; @@ -143,8 +170,7 @@ Playlist.prototype._next = function() { this.current = this.first; if(this.current) { - this.current.media.paused = false; - this.current.media.currentTime = -1; + this.startPlayback(); } } @@ -161,8 +187,7 @@ Playlist.prototype.jump = function(uid) { this.current = jmp; if(this.current) { - this.current.media.paused = false; - this.current.media.currentTime = -1; + this.startPlayback(); } if(it.temp) { @@ -188,6 +213,69 @@ Playlist.prototype.clear = function() { this.current = null; this.length = 0; this.next_uid = 0; + clearInterval(this._leadInterval); +} + +Playlist.prototype.lead = function(lead) { + this.leading = lead; + if(!this.leading && this._leadInterval) { + clearInterval(this._leadInterval); + this._leadInterval = false; + } + else if(this.leading && !this._leadInterval) { + this._leadInterval = setInterval(function() { + pl._leadLoop(); + }, 1000); + } +} + +Playlist.prototype.startPlayback = function() { + this.current.media.paused = true; + this.current.media.currentTime = -2; + var pl = this; + setTimeout(function() { + pl.current.media.paused = false; + pl.on("mediaUpdate")(pl.current.media); + }, 2000); + if(this.leading && !this._leadInterval && !isLive(this.current.media.type)) { + this._leadInterval = setInterval(function() { + pl._leadLoop(); + }, 1000); + } + else if(!this.leading && this._leadInterval) { + clearInterval(this._leadInterval); + this._leadInterval = false; + } + this.on("changeMedia")(this.current.media); +} + +function isLive(type) { + return type == "li" // Livestream.com + || type == "tw" // Twitch.tv + || type == "jt" // Justin.tv + || type == "rt" // RTMP + || type == "jw" // JWPlayer + || type == "us" // Ustream.tv + || type == "im";// Imgur album +} + +const UPDATE_INTERVAL = 5; + +Playlist.prototype._leadLoop = function() { + if(this.current == null) + return; + + this.current.media.currentTime += (Date.now() - this._lastUpdate) / 1000.0; + this._lastUpdate = Date.now(); + this._counter++; + console.log("lead", this._counter); + + if(this.current.media.currentTime >= this.current.media.seconds + 2) { + this.next(); + } + else if(this._counter % UPDATE_INTERVAL == 0) { + this.on("mediaUpdate")(this.current.media); + } } module.exports = Playlist; diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index b6bf1b67..14d53014 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -723,13 +723,15 @@ Callbacks = { var qli = $("#queue li"); $("#queue li").removeClass("queue_active"); var li = $(".pluid-" + data.uid); - if(li.length == 0) - return false; + if(li.length == 0) { + console.log("couldn't find uid" + data.uid); + } + // TODO send separate frame for highlight - li.addClass("queue_active"); + //li.addClass("queue_active"); $("#queue").scrollTop(0); - var scroll = li.position().top - $("#queue").position().top; - $("#queue").scrollTop(scroll); + //var scroll = li.position().top - $("#queue").position().top; + //$("#queue").scrollTop(scroll); if(CHANNEL.opts.allow_voteskip) $("#voteskip").attr("disabled", false);