diff --git a/channel.js b/channel.js index d42f0aa9..4fd1a184 100644 --- a/channel.js +++ b/channel.js @@ -782,7 +782,8 @@ Channel.prototype.broadcastPlaylistMeta = function() { var total = 0; var iter = this.playlist.items.first; while(iter !== null) { - total += iter.media.seconds; + if(iter.media !== null) + total += iter.media.seconds; iter = iter.next; } var timestr = formatTime(total); @@ -1225,29 +1226,23 @@ Channel.prototype.addMedia = function(data, user, callback) { Channel.prototype.enqueueList = function(data, user) { var pl = data.list; var chan = this; - // Queue in reverse order for qnext - if(data.pos == "next") { - var i = pl.length; - var cback = function() { - i--; - if(i > 0) { - pl[i].pos = "next"; - chan.enqueue(pl[i], user, cback); - } + this.playlist.addMediaList(data, function(err, item) { + if(err) { + if(err === true) + err = false; + if(user) + user.socket.emit("queueFail", err); + return; } - this.enqueue(pl[0], user, cback); - } - else { - var i = 0; - var cback = function() { - i++; - if(i < pl.length) { - pl[i].pos = "end"; - chan.enqueue(pl[i], user, cback); - } + else { + chan.sendAll("queue", { + item: item.pack(), + after: item.prev ? item.prev.uid : "prepend" + }); + chan.broadcastPlaylistMeta(); + chan.cacheMedia(item.media); } - this.enqueue(pl[i], user, cback); - } + }); } Channel.prototype.tryQueuePlaylist = function(user, data) { diff --git a/playlist.js b/playlist.js index d13d8f01..1b0de13e 100644 --- a/playlist.js +++ b/playlist.js @@ -71,9 +71,13 @@ Playlist.prototype.queueAction = function(data) { Playlist.prototype.dump = function() { var arr = this.items.toArray(); - var pos = arr.indexOf(this.current); - if(pos < 0) - pos = 0; + var pos = 0; + for(var i in arr) { + if(arr[i].uid == this.current.uid) { + pos = i; + break; + } + } var time = 0; if(this.current) @@ -133,6 +137,14 @@ Playlist.prototype.add = function(item, pos) { } Playlist.prototype.addMedia = function(data, callback) { + if(this.lock) { + this.queueAction({ + fn: "addMedia", + args: arguments + }); + return; + } + this.lock = true; var pos = "append"; if(data.pos == "next") { if(!this.current) @@ -145,6 +157,7 @@ Playlist.prototype.addMedia = function(data, callback) { InfoGetter.getMedia(data.id, data.type, function(err, media) { if(err) { callback(err, null); + pl.lock = false; return; } @@ -155,10 +168,19 @@ Playlist.prototype.addMedia = function(data, callback) { callback(true, null); else callback(false, it); + pl.lock = false; }); } Playlist.prototype.remove = function(uid, callback) { + if(this.lock) { + this.queueAction({ + fn: "remove", + args: arguments + }); + return; + } + this.lock = true; var item = this.items.find(uid); if(this.items.remove(uid)) { if(item == this.current) @@ -166,6 +188,7 @@ Playlist.prototype.remove = function(uid, callback) { if(callback) callback(); } + this.lock = false; } Playlist.prototype.move = function(from, after, callback) { @@ -179,7 +202,6 @@ Playlist.prototype.move = function(from, after, callback) { this.lock = true; this._move(from, after, callback); this.lock = false; - this.lock = false; } Playlist.prototype._move = function(from, after, callback) {