sync/lib/playlist.js

466 lines
12 KiB
JavaScript
Raw Normal View History

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.
*/
var 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;
var util = require("./utilities");
var vimeoWorkaround = require("./get-info").vimeoWorkaround;
var Config = require("./config");
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) {
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)
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();
AllPlaylists[name] = this;
2013-06-29 22:09:20 +00:00
this.channel = chan;
this.server = chan.server;
var pl = this;
this.on("mediaUpdate", function(m) {
if (chan.dead) {
pl.die();
return;
}
chan.sendAll("mediaUpdate", m.timeupdate());
});
this.on("changeMedia", function(m) {
if (chan.dead) {
pl.die();
return;
}
2014-01-06 15:55:12 +00:00
chan.resetVideo();
chan.sendAll("setCurrent", pl.current.uid);
chan.sendAll("changeMedia", m.fullupdate());
2014-02-08 18:45:07 +00:00
chan.logger.log("[playlist] Now playing: " + m.title + " (" + m.type + ":" + m.id +
")");
});
this.on("remove", function(item) {
if (chan.dead) {
pl.die();
return;
}
2014-01-06 15:55:12 +00:00
chan.updatePlaylistMeta();
chan.sendPlaylistMeta(chan.users);
chan.sendAll("delete", {
uid: item.uid
2013-06-30 19:33:38 +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();
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) {
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);
2013-11-07 23:19:36 +00:00
m.object = e.object;
m.params = e.params;
2013-07-01 22:45:55 +00:00
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) {
var self = this;
if(this.items.length >= 4000) {
2013-08-26 15:36:47 +00:00
return "Playlist limit reached (4,000)";
}
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-10-04 03:07:44 +00:00
self.remove(it.uid);
self.channel.sendAll("delete", {
uid: it.uid
});
2014-02-10 02:10:11 +00:00
self.channel.updatePlaylistMeta();
self.channel.sendPlaylistMeta(self.channel.users);
}
2013-08-26 15:36:47 +00:00
if(pos == "append") {
if(!this.items.append(item)) {
2013-08-26 15:36:47 +00:00
return "Playlist failure";
}
2013-08-26 15:36:47 +00:00
} else if(pos == "prepend") {
if(!this.items.prepend(item)) {
2013-08-26 15:36:47 +00:00
return "Playlist failure";
}
2013-08-26 15:36:47 +00:00
} else {
if(!this.items.insertAfter(item, pos)) {
2013-08-26 15:36:47 +00:00
return "Playlist failure";
}
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
pos = "append";
2013-09-30 00:53:27 +00:00
}
var m = new Media(data.id, data.title, data.seconds, data.type);
2013-11-07 23:19:36 +00:00
m.object = data.object;
m.params = data.params;
2013-09-30 00:53:27 +00:00
var item = this.makeItem(m);
item.queueby = data.queueby;
item.temp = data.temp;
return {
item: item,
error: this.add(item, pos)
2013-09-30 00:53:27 +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) {
2013-10-07 05:10:16 +00:00
self._next();
2013-07-03 15:26:10 +00:00
}
return true;
} else {
return false;
}
2013-06-29 22:09:20 +00:00
}
Playlist.prototype.move = function (from, after) {
2013-07-02 15:38:13 +00:00
var it = this.items.find(from);
if (!this.items.remove(from))
return false;
2013-07-01 22:45:55 +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
}
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;
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) {
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);
}
Playlist.prototype.count = function (id) {
var count = 0;
this.items.forEach(function (i) {
if(i.media.id === id)
count++;
});
return count;
}
Playlist.prototype.getFullUpdate = function () {
if (!this.current) {
return null;
} else if (!this.current.media) {
return null;
} else {
return this.current.media.fullupdate();
}
};
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) {
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) {
var self = this;
if(!self.current || !self.current.media)
2013-07-03 20:04:50 +00:00
return false;
if (self.current.media.type === "vi" &&
!self.current.media.direct &&
Config.get("vimeo-workaround")) {
vimeoWorkaround(self.current.media.id, function (direct) {
if (self.current != null && self.current.media != null) {
self.current.media.direct = direct;
self.startPlayback(time);
}
});
return;
}
if (!self.leading) {
self.current.media.paused = false;
self.current.media.currentTime = time || 0;
self.on("changeMedia")(self.current.media);
2013-09-22 04:54:29 +00:00
return;
}
time = time || -3;
self.current.media.paused = time < 0;
self.current.media.currentTime = time;
2013-09-22 04:54:29 +00:00
if(self._leadInterval) {
clearInterval(self._leadInterval);
self._leadInterval = false;
2013-07-03 19:51:35 +00:00
}
self.on("changeMedia")(self.current.media);
2014-02-10 02:10:11 +00:00
if(!self.current.media.live) {
self._lastUpdate = Date.now();
self._leadInterval = setInterval(function() {
self._leadLoop();
2013-06-30 00:59:33 +00:00
}, 1000);
}
}
const UPDATE_INTERVAL = 5;
Playlist.prototype._leadLoop = function() {
if(this.current == null)
return;
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
}
/*
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();
} else {
// refresh meta only once, at the end
2014-02-10 02:10:11 +00:00
self.channel.updatePlaylistMeta();
self.channel.sendPlaylistMeta(self.channel.users);
}
};
// start initial callback
deleteNext();
};
2013-06-29 22:09:20 +00:00
module.exports = Playlist;