2013-07-06 15:25:12 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
Copyright (c) 2013 Calvin Montgomery
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-07-03 21:29:49 +00:00
|
|
|
ULList = require("./ullist").ULList;
|
2013-09-29 16:30:36 +00:00
|
|
|
var AsyncQueue = require("./asyncqueue");
|
2013-07-01 22:45:55 +00:00
|
|
|
var Media = require("./media").Media;
|
2013-07-14 02:19:47 +00:00
|
|
|
var AllPlaylists = {};
|
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-27 14:40:49 +00:00
|
|
|
var name = chan.canonical_name;
|
|
|
|
if(name in AllPlaylists && AllPlaylists[name]) {
|
|
|
|
var pl = AllPlaylists[name];
|
2013-07-16 15:46:09 +00:00
|
|
|
if(!pl.dead)
|
2013-07-14 02:19:47 +00:00
|
|
|
pl.die();
|
|
|
|
}
|
2013-07-02 15:38:13 +00:00
|
|
|
this.items = new ULList();
|
2013-07-09 22:29:01 +00:00
|
|
|
this.current = null;
|
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-09-29 16:30:36 +00:00
|
|
|
this.fnqueue = new AsyncQueue();
|
2013-07-27 14:40:49 +00:00
|
|
|
AllPlaylists[name] = this;
|
2013-06-29 22:09:20 +00:00
|
|
|
|
2013-08-03 14:58:21 +00:00
|
|
|
this.channel = chan;
|
|
|
|
this.server = chan.server;
|
|
|
|
var pl = this;
|
|
|
|
this.on("mediaUpdate", function(m) {
|
2013-09-08 22:43:30 +00:00
|
|
|
if (chan.dead) {
|
|
|
|
pl.die();
|
|
|
|
return;
|
|
|
|
}
|
2013-08-03 14:58:21 +00:00
|
|
|
chan.sendAll("mediaUpdate", m.timeupdate());
|
|
|
|
});
|
|
|
|
this.on("changeMedia", function(m) {
|
2013-09-08 22:43:30 +00:00
|
|
|
if (chan.dead) {
|
|
|
|
pl.die();
|
|
|
|
return;
|
|
|
|
}
|
2013-08-03 14:58:21 +00:00
|
|
|
chan.onVideoChange();
|
|
|
|
chan.sendAll("setCurrent", pl.current.uid);
|
|
|
|
chan.sendAll("changeMedia", m.fullupdate());
|
|
|
|
});
|
|
|
|
this.on("remove", function(item) {
|
2013-09-08 22:43:30 +00:00
|
|
|
if (chan.dead) {
|
|
|
|
pl.die();
|
|
|
|
return;
|
|
|
|
}
|
2013-08-03 14:58:21 +00:00
|
|
|
chan.broadcastPlaylistMeta();
|
|
|
|
chan.sendAll("delete", {
|
|
|
|
uid: item.uid
|
2013-06-30 19:33:38 +00:00
|
|
|
});
|
2013-08-03 14:58:21 +00:00
|
|
|
});
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
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-14 21:59:40 +00:00
|
|
|
if(this._qaInterval) {
|
|
|
|
clearInterval(this._qaInterval);
|
|
|
|
this._qaInterval = false;
|
|
|
|
}
|
2013-07-16 15:56:33 +00:00
|
|
|
//for(var key in this)
|
|
|
|
// delete this[key];
|
2013-07-16 15:46:09 +00:00
|
|
|
this.dead = true;
|
2013-07-03 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
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-08-26 21:37:09 +00:00
|
|
|
var self = this;
|
|
|
|
if(this.items.length >= 4000) {
|
2013-08-26 15:36:47 +00:00
|
|
|
return "Playlist limit reached (4,000)";
|
2013-08-26 21:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var it = this.items.findVideoId(item.media.id);
|
|
|
|
if(it) {
|
|
|
|
if(pos === "append" || it == this.current) {
|
|
|
|
return "This item is already on the playlist";
|
|
|
|
}
|
2013-08-26 15:36:47 +00:00
|
|
|
|
2013-08-26 21:37:09 +00:00
|
|
|
self.remove(it.uid, function () {
|
|
|
|
self.channel.sendAll("delete", {
|
|
|
|
uid: it.uid
|
|
|
|
});
|
2013-08-28 15:38:24 +00:00
|
|
|
self.channel.broadcastPlaylistMeta();
|
2013-08-26 21:37:09 +00:00
|
|
|
});
|
|
|
|
}
|
2013-08-26 15:36:47 +00:00
|
|
|
|
|
|
|
if(pos == "append") {
|
2013-08-26 21:37:09 +00:00
|
|
|
if(!this.items.append(item)) {
|
2013-08-26 15:36:47 +00:00
|
|
|
return "Playlist failure";
|
2013-08-26 21:37:09 +00:00
|
|
|
}
|
2013-08-26 15:36:47 +00:00
|
|
|
} else if(pos == "prepend") {
|
2013-08-26 21:37:09 +00:00
|
|
|
if(!this.items.prepend(item)) {
|
2013-08-26 15:36:47 +00:00
|
|
|
return "Playlist failure";
|
2013-08-26 21:37:09 +00:00
|
|
|
}
|
2013-08-26 15:36:47 +00:00
|
|
|
} else {
|
2013-08-26 21:37:09 +00:00
|
|
|
if(!this.items.insertAfter(item, pos)) {
|
2013-08-26 15:36:47 +00:00
|
|
|
return "Playlist failure";
|
2013-08-26 21:37:09 +00:00
|
|
|
}
|
2013-08-26 15:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(this.items.length == 1) {
|
2013-07-03 03:19:17 +00:00
|
|
|
this.current = item;
|
|
|
|
this.startPlayback();
|
|
|
|
}
|
|
|
|
|
2013-08-26 15:36:47 +00:00
|
|
|
return false;
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 00:53:27 +00:00
|
|
|
Playlist.prototype.addMedia = function (data) {
|
|
|
|
var pos = data.pos;
|
|
|
|
if (pos === "next") {
|
|
|
|
if (this.current !== null)
|
|
|
|
pos = this.current.uid;
|
|
|
|
else
|
2013-09-30 03:32:37 +00:00
|
|
|
pos = "append";
|
2013-09-30 00:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var m = new Media(data.id, data.title, data.seconds, data.type);
|
|
|
|
var item = this.makeItem(m);
|
|
|
|
item.queueby = data.queueby;
|
|
|
|
item.temp = data.temp;
|
|
|
|
return {
|
|
|
|
item: item,
|
2013-09-30 03:32:37 +00:00
|
|
|
error: this.add(item, pos)
|
2013-09-30 00:53:27 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-09-30 15:05:09 +00:00
|
|
|
Playlist.prototype.remove = function (uid) {
|
|
|
|
var self = this;
|
|
|
|
var item = self.items.find(uid);
|
|
|
|
if (item && self.items.remove(uid)) {
|
|
|
|
if (item === self.current) {
|
|
|
|
setImmediate(function () { self._next(); });
|
2013-07-03 15:26:10 +00:00
|
|
|
}
|
2013-09-30 15:05:09 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-29 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 15:05:09 +00:00
|
|
|
Playlist.prototype.move = function (from, after) {
|
2013-07-02 15:38:13 +00:00
|
|
|
var it = this.items.find(from);
|
2013-09-30 15:05:09 +00:00
|
|
|
if (!this.items.remove(from))
|
|
|
|
return false;
|
2013-07-01 22:45:55 +00:00
|
|
|
|
2013-09-30 15:05:09 +00:00
|
|
|
if (after === "prepend") {
|
|
|
|
if (!this.items.prepend(it))
|
|
|
|
return false;
|
|
|
|
} else if (after === "append") {
|
|
|
|
if (!this.items.append(it))
|
|
|
|
return false;
|
|
|
|
} else if (!this.items.insertAfter(it, after)) {
|
|
|
|
return false;
|
2013-07-01 22:45:55 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 15:05:09 +00:00
|
|
|
return true;
|
2013-07-01 22:45:55 +00:00
|
|
|
}
|
|
|
|
|
2013-06-29 22:09:20 +00:00
|
|
|
Playlist.prototype.next = function() {
|
|
|
|
if(!this.current)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var it = this.current;
|
|
|
|
|
2013-09-30 15:05:09 +00:00
|
|
|
if (it.temp) {
|
|
|
|
if (this.remove(it.uid)) {
|
|
|
|
this.on("remove")(it);
|
|
|
|
}
|
|
|
|
} else {
|
2013-07-27 14:30:12 +00:00
|
|
|
this._next();
|
|
|
|
}
|
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-09-30 15:05:09 +00:00
|
|
|
if (this.remove(it.uid)) {
|
|
|
|
this.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);
|
|
|
|
}
|
|
|
|
|
2013-08-13 15:18:06 +00:00
|
|
|
Playlist.prototype.count = function (id) {
|
|
|
|
var count = 0;
|
|
|
|
this.items.forEach(function (i) {
|
|
|
|
if(i.media.id === id)
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2013-06-30 00:59:33 +00:00
|
|
|
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) {
|
2013-08-09 00:42:46 +00:00
|
|
|
this._lastUpdate = Date.now();
|
2013-06-30 00:59:33 +00:00
|
|
|
this._leadInterval = setInterval(function() {
|
|
|
|
pl._leadLoop();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-22 04:54:29 +00:00
|
|
|
Playlist.prototype.startPlayback = function (time) {
|
2013-07-03 20:04:50 +00:00
|
|
|
if(!this.current || !this.current.media)
|
|
|
|
return false;
|
2013-09-22 04:54:29 +00:00
|
|
|
if (!this.leading) {
|
|
|
|
this.current.media.paused = false;
|
|
|
|
this.current.media.currentTime = time || 0;
|
|
|
|
this.on("changeMedia")(this.current.media);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
time = time || -3;
|
|
|
|
this.current.media.paused = time < 0;
|
|
|
|
this.current.media.currentTime = time;
|
|
|
|
|
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);
|
2013-09-22 04:54:29 +00:00
|
|
|
if(!isLive(this.current.media.type)) {
|
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
|
2013-08-03 15:12:53 +00:00
|
|
|
|| type == "im" // Imgur album
|
|
|
|
|| type == "cu";// Custom embed
|
2013-06-30 00:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const UPDATE_INTERVAL = 5;
|
|
|
|
|
|
|
|
Playlist.prototype._leadLoop = function() {
|
|
|
|
if(this.current == null)
|
|
|
|
return;
|
|
|
|
|
2013-07-27 14:40:49 +00:00
|
|
|
if(this.channel.name == "") {
|
|
|
|
this.die();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-22 04:54:29 +00:00
|
|
|
var dt = (Date.now() - this._lastUpdate) / 1000.0;
|
|
|
|
var t = this.current.media.currentTime;
|
|
|
|
|
|
|
|
// Transition from lead-in
|
|
|
|
if (t < 0 && (t + dt) >= 0) {
|
|
|
|
this.current.media.currentTime = 0;
|
|
|
|
this.current.media.paused = false;
|
|
|
|
this._counter = 0;
|
|
|
|
this._lastUpdate = Date.now();
|
|
|
|
this.on("mediaUpdate")(this.current.media);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.current.media.currentTime += dt;
|
2013-06-30 00:59:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-21 05:10:22 +00:00
|
|
|
/*
|
|
|
|
Delete items from the playlist for which filter(item) returns
|
|
|
|
a truthy value
|
|
|
|
|
|
|
|
based on code contributed by http://github.com/unbibium
|
|
|
|
*/
|
|
|
|
Playlist.prototype.clean = function (filter) {
|
|
|
|
var self = this;
|
|
|
|
var matches = self.items.findAll(filter);
|
|
|
|
var count = 0;
|
|
|
|
var deleteNext = function () {
|
|
|
|
if (count < matches.length) {
|
|
|
|
var uid = matches[count].uid;
|
|
|
|
count++;
|
2013-10-01 02:34:40 +00:00
|
|
|
if (self.remove(uid)) {
|
|
|
|
self.channel.sendAll("delete", {
|
|
|
|
uid: uid
|
|
|
|
});
|
|
|
|
}
|
|
|
|
deleteNext();
|
2013-09-21 05:10:22 +00:00
|
|
|
} else {
|
|
|
|
// refresh meta only once, at the end
|
|
|
|
self.channel.broadcastPlaylistMeta();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// start initial callback
|
|
|
|
deleteNext();
|
|
|
|
};
|
|
|
|
|
2013-06-29 22:09:20 +00:00
|
|
|
module.exports = Playlist;
|