2013-07-03 21:29:49 +00:00
|
|
|
ULList = require("./ullist").ULList;
|
2013-07-01 22:45:55 +00:00
|
|
|
var Media = require("./media").Media;
|
2013-07-02 15:38:13 +00:00
|
|
|
var InfoGetter = require("./get-info");
|
2013-07-01 22:45:55 +00:00
|
|
|
|
2013-06-29 22:09:20 +00:00
|
|
|
function PlaylistItem(media, uid) {
|
|
|
|
this.media = media;
|
|
|
|
this.uid = uid;
|
|
|
|
this.temp = false;
|
|
|
|
this.queueby = "";
|
|
|
|
this.prev = null;
|
|
|
|
this.next = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
PlaylistItem.prototype.pack = function() {
|
|
|
|
return {
|
2013-07-01 22:45:55 +00:00
|
|
|
media: this.media.pack(),
|
2013-06-29 22:09:20 +00:00
|
|
|
uid: this.uid,
|
|
|
|
temp: this.temp,
|
|
|
|
queueby: this.queueby
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-06-30 00:59:33 +00:00
|
|
|
function Playlist(chan) {
|
2013-07-02 15:38:13 +00:00
|
|
|
this.items = new ULList();
|
2013-06-29 22:09:20 +00:00
|
|
|
this.next_uid = 0;
|
2013-06-30 00:59:33 +00:00
|
|
|
this._leadInterval = false;
|
|
|
|
this._lastUpdate = 0;
|
|
|
|
this._counter = 0;
|
|
|
|
this.leading = true;
|
|
|
|
this.callbacks = {
|
|
|
|
"changeMedia": [],
|
2013-06-30 19:33:38 +00:00
|
|
|
"mediaUpdate": [],
|
|
|
|
"remove": [],
|
2013-06-30 00:59:33 +00:00
|
|
|
};
|
2013-07-02 15:38:13 +00:00
|
|
|
this.lock = false;
|
2013-07-03 21:29:49 +00:00
|
|
|
this.action_queue = [];
|
2013-07-02 15:38:13 +00:00
|
|
|
this._qaInterval = false;
|
2013-06-29 22:09:20 +00:00
|
|
|
|
2013-06-30 00:59:33 +00:00
|
|
|
if(chan) {
|
2013-06-30 19:33:38 +00:00
|
|
|
var pl = this;
|
2013-06-30 00:59:33 +00:00
|
|
|
this.on("mediaUpdate", function(m) {
|
|
|
|
chan.sendAll("mediaUpdate", m.timeupdate());
|
|
|
|
});
|
|
|
|
this.on("changeMedia", function(m) {
|
2013-07-06 03:34:04 +00:00
|
|
|
chan.onVideoChange();
|
2013-06-30 19:33:38 +00:00
|
|
|
chan.sendAll("setCurrent", pl.current.uid);
|
2013-06-30 00:59:33 +00:00
|
|
|
chan.sendAll("changeMedia", m.fullupdate());
|
2013-06-29 22:09:20 +00:00
|
|
|
});
|
2013-06-30 19:33:38 +00:00
|
|
|
this.on("remove", function(item) {
|
|
|
|
chan.sendAll("delete", {
|
|
|
|
uid: item.uid
|
|
|
|
});
|
|
|
|
});
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:38:13 +00:00
|
|
|
Playlist.prototype.queueAction = function(data) {
|
2013-07-03 21:29:49 +00:00
|
|
|
this.action_queue.push(data);
|
2013-07-02 15:38:13 +00:00
|
|
|
if(this._qaInterval)
|
|
|
|
return;
|
|
|
|
var pl = this;
|
|
|
|
this._qaInterval = setInterval(function() {
|
2013-07-03 21:29:49 +00:00
|
|
|
var data = pl.action_queue.shift();
|
|
|
|
if(data.waiting) {
|
|
|
|
if(!("expire" in data))
|
|
|
|
data.expire = Date.now() + 10000;
|
|
|
|
if(Date.now() < data.expire)
|
|
|
|
pl.action_queue.unshift(data);
|
|
|
|
}
|
|
|
|
else
|
2013-07-03 03:19:17 +00:00
|
|
|
data.fn();
|
2013-07-03 21:29:49 +00:00
|
|
|
if(pl.action_queue.length == 0) {
|
|
|
|
clearInterval(pl._qaInterval);
|
|
|
|
pl._qaInterval = false;
|
2013-07-02 15:38:13 +00:00
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
2013-07-01 22:45:55 +00:00
|
|
|
Playlist.prototype.dump = function() {
|
2013-07-02 15:38:13 +00:00
|
|
|
var arr = this.items.toArray();
|
2013-07-02 19:01:12 +00:00
|
|
|
var pos = 0;
|
|
|
|
for(var i in arr) {
|
2013-07-03 03:19:17 +00:00
|
|
|
if(this.current && arr[i].uid == this.current.uid) {
|
2013-07-02 19:01:12 +00:00
|
|
|
pos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-01 22:45:55 +00:00
|
|
|
|
|
|
|
var time = 0;
|
|
|
|
if(this.current)
|
|
|
|
time = this.current.media.currentTime;
|
|
|
|
|
|
|
|
return {
|
|
|
|
pl: arr,
|
|
|
|
pos: pos,
|
|
|
|
time: time
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-07-03 19:51:35 +00:00
|
|
|
Playlist.prototype.die = function () {
|
|
|
|
this.clear();
|
|
|
|
if(this._leadInterval) {
|
|
|
|
clearInterval(this._leadInterval);
|
|
|
|
this._leadInterval = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 22:45:55 +00:00
|
|
|
Playlist.prototype.load = function(data, callback) {
|
|
|
|
this.clear();
|
|
|
|
for(var i in data.pl) {
|
|
|
|
var e = data.pl[i].media;
|
|
|
|
var m = new Media(e.id, e.title, e.seconds, e.type);
|
|
|
|
var it = this.makeItem(m);
|
|
|
|
it.temp = data.pl[i].temp;
|
|
|
|
it.queueby = data.pl[i].queueby;
|
2013-07-02 15:38:13 +00:00
|
|
|
this.items.append(it);
|
2013-07-01 22:45:55 +00:00
|
|
|
if(i == parseInt(data.pos)) {
|
|
|
|
this.current = it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(callback)
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2013-06-30 00:59:33 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-29 22:09:20 +00:00
|
|
|
Playlist.prototype.makeItem = function(media) {
|
|
|
|
return new PlaylistItem(media, this.next_uid++);
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:38:13 +00:00
|
|
|
Playlist.prototype.add = function(item, pos) {
|
2013-07-03 03:19:17 +00:00
|
|
|
var success;
|
2013-07-02 15:38:13 +00:00
|
|
|
if(pos == "append")
|
2013-07-03 03:19:17 +00:00
|
|
|
success = this.items.append(item);
|
2013-07-02 15:38:13 +00:00
|
|
|
else if(pos == "prepend")
|
2013-07-03 03:19:17 +00:00
|
|
|
success = this.items.prepend(item);
|
2013-06-29 22:09:20 +00:00
|
|
|
else
|
2013-07-03 03:19:17 +00:00
|
|
|
success = this.items.insertAfter(item, pos);
|
|
|
|
|
|
|
|
if(success && this.items.length == 1) {
|
|
|
|
this.current = item;
|
|
|
|
this.startPlayback();
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 21:29:49 +00:00
|
|
|
Playlist.prototype.addCachedMedia = function(data, callback) {
|
|
|
|
var pos = "append";
|
|
|
|
if(data.pos == "next") {
|
|
|
|
if(!this.current)
|
|
|
|
pos = "prepend";
|
|
|
|
else
|
|
|
|
pos = this.current.uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
var it = this.makeItem(data.media);
|
|
|
|
it.temp = data.temp;
|
|
|
|
it.queueby = data.queueby;
|
|
|
|
|
|
|
|
var pl = this;
|
|
|
|
|
|
|
|
var action = {
|
|
|
|
fn: function() {
|
|
|
|
if(pl.add(it, pos))
|
|
|
|
callback(false, it);
|
|
|
|
},
|
|
|
|
waiting: false
|
|
|
|
};
|
|
|
|
this.queueAction(action);
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:38:13 +00:00
|
|
|
Playlist.prototype.addMedia = function(data, callback) {
|
2013-07-03 15:26:10 +00:00
|
|
|
|
|
|
|
if(data.type == "yp") {
|
|
|
|
this.addYouTubePlaylist(data, callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:38:13 +00:00
|
|
|
var pos = "append";
|
|
|
|
if(data.pos == "next") {
|
|
|
|
if(!this.current)
|
|
|
|
pos = "prepend";
|
|
|
|
else
|
|
|
|
pos = this.current.uid;
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 03:19:17 +00:00
|
|
|
var it = this.makeItem(null);
|
2013-07-02 15:38:13 +00:00
|
|
|
var pl = this;
|
2013-07-03 03:19:17 +00:00
|
|
|
var action = {
|
|
|
|
fn: function() {
|
2013-07-03 21:29:49 +00:00
|
|
|
if(pl.add(it, pos)) {
|
2013-07-03 03:19:17 +00:00
|
|
|
callback(false, it);
|
2013-07-03 21:29:49 +00:00
|
|
|
}
|
2013-07-03 03:19:17 +00:00
|
|
|
},
|
|
|
|
waiting: true
|
|
|
|
};
|
|
|
|
this.queueAction(action);
|
|
|
|
|
2013-07-02 15:38:13 +00:00
|
|
|
InfoGetter.getMedia(data.id, data.type, function(err, media) {
|
|
|
|
if(err) {
|
2013-07-03 03:19:17 +00:00
|
|
|
action.expire = 0;
|
2013-07-02 15:38:13 +00:00
|
|
|
callback(err, null);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-29 22:09:20 +00:00
|
|
|
|
2013-07-04 23:11:13 +00:00
|
|
|
if(data.maxlength && media.seconds > data.maxlength) {
|
|
|
|
action.expire = 0;
|
|
|
|
callback("Media is too long!", null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-03 03:19:17 +00:00
|
|
|
it.media = media;
|
2013-07-02 15:38:13 +00:00
|
|
|
it.temp = data.temp;
|
|
|
|
it.queueby = data.queueby;
|
2013-07-03 03:19:17 +00:00
|
|
|
action.waiting = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Playlist.prototype.addMediaList = function(data, callback) {
|
2013-07-03 21:29:49 +00:00
|
|
|
var start = false;
|
|
|
|
if(data.pos == "next") {
|
2013-07-03 03:19:17 +00:00
|
|
|
data.list = data.list.reverse();
|
2013-07-03 21:29:49 +00:00
|
|
|
start = data.list[data.list.length - 1];
|
|
|
|
}
|
2013-07-03 03:19:17 +00:00
|
|
|
|
|
|
|
var pl = this;
|
|
|
|
data.list.forEach(function(x) {
|
|
|
|
x.pos = data.pos;
|
2013-07-03 21:29:49 +00:00
|
|
|
if(start && x == start) {
|
|
|
|
pl.addMedia(x, function (err, item) {
|
|
|
|
if(err) {
|
|
|
|
callback(err, item);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(err, item);
|
|
|
|
pl.current = item;
|
|
|
|
pl.startPlayback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pl.addMedia(x, callback);
|
|
|
|
}
|
2013-07-02 15:38:13 +00:00
|
|
|
});
|
2013-07-01 22:45:55 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 15:26:10 +00:00
|
|
|
Playlist.prototype.addYouTubePlaylist = function(data, callback) {
|
|
|
|
var pos = "append";
|
|
|
|
if(data.pos == "next") {
|
|
|
|
if(!this.current)
|
|
|
|
pos = "prepend";
|
|
|
|
else
|
|
|
|
pos = this.current.uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pl = this;
|
|
|
|
InfoGetter.getMedia(data.id, data.type, function(err, vids) {
|
|
|
|
if(err) {
|
|
|
|
callback(err, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vids.forEach(function(media) {
|
|
|
|
var it = pl.makeItem(media);
|
|
|
|
it.temp = data.temp;
|
|
|
|
it.queueby = data.queueby;
|
|
|
|
pl.queueAction({
|
|
|
|
fn: function() {
|
|
|
|
if(pl.add(it, pos))
|
|
|
|
callback(false, it);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:38:13 +00:00
|
|
|
Playlist.prototype.remove = function(uid, callback) {
|
2013-07-03 03:19:17 +00:00
|
|
|
var pl = this;
|
|
|
|
this.queueAction({
|
|
|
|
fn: function() {
|
|
|
|
var item = pl.items.find(uid);
|
|
|
|
if(pl.items.remove(uid)) {
|
|
|
|
if(item == pl.current)
|
|
|
|
pl._next();
|
|
|
|
if(callback)
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
waiting: false
|
|
|
|
});
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
2013-07-02 15:38:13 +00:00
|
|
|
Playlist.prototype.move = function(from, after, callback) {
|
2013-07-03 03:19:17 +00:00
|
|
|
var pl = this;
|
|
|
|
this.queueAction({
|
|
|
|
fn: function() {
|
|
|
|
pl._move(from, after, callback);
|
|
|
|
},
|
|
|
|
waiting: false
|
|
|
|
});
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
2013-07-02 15:38:13 +00:00
|
|
|
Playlist.prototype._move = function(from, after, callback) {
|
|
|
|
var it = this.items.find(from);
|
|
|
|
if(!this.items.remove(from))
|
2013-07-01 22:45:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(after === "prepend") {
|
2013-07-02 15:38:13 +00:00
|
|
|
if(!this.items.prepend(it))
|
2013-07-01 22:45:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(after === "append") {
|
2013-07-02 15:38:13 +00:00
|
|
|
if(!this.items.append(it))
|
2013-07-01 22:45:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:38:13 +00:00
|
|
|
else if(!this.items.insertAfter(it, after))
|
2013-07-01 22:45:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2013-06-29 22:09:20 +00:00
|
|
|
Playlist.prototype.next = function() {
|
|
|
|
if(!this.current)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var it = this.current;
|
|
|
|
this._next();
|
|
|
|
|
|
|
|
if(it.temp) {
|
2013-07-02 15:38:13 +00:00
|
|
|
var pl = this;
|
|
|
|
this.remove(it.uid, function() {
|
|
|
|
pl.on("remove")(it);
|
|
|
|
});
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.current;
|
|
|
|
}
|
|
|
|
|
|
|
|
Playlist.prototype._next = function() {
|
|
|
|
if(!this.current)
|
|
|
|
return;
|
|
|
|
this.current = this.current.next;
|
2013-07-02 15:38:13 +00:00
|
|
|
if(this.current === null && this.items.first !== null)
|
|
|
|
this.current = this.items.first;
|
2013-06-29 22:09:20 +00:00
|
|
|
|
|
|
|
if(this.current) {
|
2013-06-30 00:59:33 +00:00
|
|
|
this.startPlayback();
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Playlist.prototype.jump = function(uid) {
|
|
|
|
if(!this.current)
|
|
|
|
return false;
|
|
|
|
|
2013-07-02 15:38:13 +00:00
|
|
|
var jmp = this.items.find(uid);
|
2013-06-29 22:09:20 +00:00
|
|
|
if(!jmp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var it = this.current;
|
|
|
|
|
|
|
|
this.current = jmp;
|
|
|
|
|
|
|
|
if(this.current) {
|
2013-06-30 00:59:33 +00:00
|
|
|
this.startPlayback();
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(it.temp) {
|
2013-07-03 21:29:49 +00:00
|
|
|
var pl = this;
|
|
|
|
this.remove(it.uid, function () {
|
|
|
|
pl.on("remove")(it);
|
|
|
|
});
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.current;
|
|
|
|
}
|
|
|
|
|
|
|
|
Playlist.prototype.clear = function() {
|
2013-07-02 15:38:13 +00:00
|
|
|
this.items.clear();
|
2013-06-29 22:09:20 +00:00
|
|
|
this.next_uid = 0;
|
2013-07-03 21:29:49 +00:00
|
|
|
this.current = null;
|
2013-06-30 00:59:33 +00:00
|
|
|
clearInterval(this._leadInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
Playlist.prototype.lead = function(lead) {
|
|
|
|
this.leading = lead;
|
2013-06-30 19:33:38 +00:00
|
|
|
var pl = this;
|
2013-06-30 00:59:33 +00:00
|
|
|
if(!this.leading && this._leadInterval) {
|
|
|
|
clearInterval(this._leadInterval);
|
|
|
|
this._leadInterval = false;
|
|
|
|
}
|
|
|
|
else if(this.leading && !this._leadInterval) {
|
|
|
|
this._leadInterval = setInterval(function() {
|
|
|
|
pl._leadLoop();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 22:45:55 +00:00
|
|
|
Playlist.prototype.startPlayback = function(time) {
|
2013-07-03 20:04:50 +00:00
|
|
|
if(!this.current || !this.current.media)
|
|
|
|
return false;
|
2013-07-02 15:38:13 +00:00
|
|
|
this.current.media.paused = false;
|
|
|
|
this.current.media.currentTime = time || -1;
|
2013-06-30 00:59:33 +00:00
|
|
|
var pl = this;
|
2013-07-03 19:51:35 +00:00
|
|
|
if(this._leadInterval) {
|
|
|
|
clearInterval(this._leadInterval);
|
|
|
|
this._leadInterval = false;
|
|
|
|
}
|
|
|
|
this.on("changeMedia")(this.current.media);
|
|
|
|
if(this.leading && !isLive(this.current.media.type)) {
|
|
|
|
this.on("changeMedia")(this.current.media);
|
2013-07-01 22:45:55 +00:00
|
|
|
this._lastUpdate = Date.now();
|
2013-06-30 00:59:33 +00:00
|
|
|
this._leadInterval = setInterval(function() {
|
|
|
|
pl._leadLoop();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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++;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Playlist;
|