-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_manager.js
273 lines (243 loc) · 9.92 KB
/
queue_manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* Getter for now playing
*/
module.exports.getNowPlaying = function() {
return module.vlc.nowPlaying;
}
/**
* Populate our media library into our database
*/
module.exports.populateMediaLibrary = function() {
module.vlc.readLibrary('Media Library', function(libraryData) {
var loaded = 0;
for (x in libraryData) {
loaded++;
MediaLibrary.create({
Name: libraryData[x].name,
Duration: libraryData[x].duration,
Uri: libraryData[x].uri
});
}
console.log("%s songs loaded into the Media Library", loaded);
});
};
/**
* Initialize the scheduled music poller
*/
module.exports.startStatusPoller = function() {
setInterval(module.vlc.fetchStatus, 2000);
}
module.vlc = {
nowPlaying: {
// If a song is currently playing
is_playing: false,
// If a song is currently paused
is_paused: false,
// The file name that is playing
file_name: null,
// What song are we going to play next?
up_next: null,
// The number of seconds into the current song we are
song_position: 0,
// The total number of seconds this song is
song_total: 0,
// Display data about what's playing
metadata: {}
},
queue: {
// Whether or not we've queued up a new song to play
sent: false,
// The file that was last monitored playing
file_name: null
},
/**
* Generic function to read in a library
*
* @param library Either 'Playlist' or 'Media Library'
* @param callback(libraryData)
*/
readLibrary: function(library, cb) {
require('http').get('http://127.0.0.1:8080/requests/playlist.json', function (res) {
var chunkData = '';
res.setEncoding('utf8');
res.on('data', function (data) {
chunkData += data.toString();
}).on('end', function (data) {
var jsonData = JSON.parse(chunkData).children,
libraryData = [];
// Find our media library
for (x in jsonData) {
if (jsonData[x].type == 'node' && jsonData[x].name === library) {
jsonData = jsonData[x].children;
break;
}
}
// Find all the songs recursively
var treeWalker = function(jsonData) {
for (x in jsonData) {
switch (jsonData[x].type) {
case 'node':
treeWalker(jsonData[x].children);
break;
case 'leaf':
libraryData.push(jsonData[x]);
break;
}
}
};
treeWalker(jsonData);
if (typeof cb === 'function') {
cb.call(this, libraryData);
}
});
});
},
/**
* Determine what VLC is currently doing
*/
fetchStatus: function () {
require('http').get('http://localhost:8080/requests/status.json', function (res) {
var chunkData = '';
res.setEncoding('utf8');
res.on('data', function (data) {
chunkData += data.toString();
}).on('end', function (data) {
// Parse response
var statusData = JSON.parse(chunkData);
module.vlc.nowPlaying.is_playing = statusData.state === 'playing';
module.vlc.nowPlaying.is_paused = statusData.state === 'paused';
module.vlc.nowPlaying.song_position = statusData.time;
module.vlc.nowPlaying.song_total = statusData.length;
if (typeof statusData.information !== 'undefined' &&
typeof statusData.information.category !== 'undefined' &&
typeof statusData.information.category.meta !== 'undefined') {
// Information about playing song
module.vlc.nowPlaying.metadata = statusData.information.category.meta;
module.vlc.nowPlaying.file_name = statusData.information.category.meta.filename;
} else {
// No data available (i.e. stopped)
module.vlc.nowPlaying.metadata = {};
module.vlc.nowPlaying.file_name = null;
}
// Check if we need to do anything with this information
module.vlc.monitorQueue();
});
});
},
/**
* Make sure the queue is ready to do something
*/
monitorQueue: function() {
if (module.vlc.nowPlaying.file_name !== module.vlc.queue.file_name) {
if (module.vlc.nowPlaying.file_name === null) {
console.log("VLC stopped playing music");
} else {
console.log("Song [%s] is now playing", module.vlc.nowPlaying.file_name);
}
module.vlc.queue.file_name = module.vlc.nowPlaying.file_name;
module.vlc.queue.sent = false;
module.vlc.nowPlaying.up_next = null;
}
// If we've already queued a new file, stop caring
if (module.vlc.queue.sent) {
return;
}
// If the music was paused, we'll assume admin override
if (module.vlc.nowPlaying.is_paused) {
console.log("VLC is currently paused so we will not manage the queue.");
return;
}
// Load in a new song 30 seconds before the old one ends
var secondsLeft = module.vlc.nowPlaying.song_total - module.vlc.nowPlaying.song_position;
if (module.vlc.nowPlaying.is_playing && secondsLeft <= 40) {
console.log("Preparing queue since only %d seconds are left in the current song.", secondsLeft);
module.vlc.queueNewSong();
} else if (!module.vlc.nowPlaying.is_playing) {
console.log("Preparing queue since no music is currently playing.");
module.vlc.queueNewSong();
}
},
/**
* Determine what song we should play next
*/
queueNewSong: function() {
MediaQueue.all(
{
where: {
PlayTime: null
},
order: 'QueueTime',
limit: 1
},
function (err, libraryData) {
if (libraryData.length > 0) {
// We had a song selected
var queuedSong = libraryData[0];
MediaLibrary.find(queuedSong.MediaLibraryId, function(err, song) {
module.vlc.enqueueSong(song, function() {
// Record to up next
module.vlc.nowPlaying.up_next = song.Name;
module.vlc.queue.sent = true;
// Mark the queued item as played
queuedSong.PlayTime = new Date();
queuedSong.save(function() {});
// Note that the song played already
song.PlayCount++
song.LastPlayed = new Date();
song.save(function() {});
});
});
} else {
console.log("No songs queued");
}
}
);
},
/**
* Push a song to VLC and ensure it will play
*/
enqueueSong: function(song, cb) {
require('http').get('http://localhost:8080/requests/status.json?command=in_enqueue&input=' + escape(song.Uri), function() {
console.log("Song %s enqueued to VLC", song.Name);
if (!module.vlc.nowPlaying.is_playing && !module.vlc.nowPlaying.is_paused) {
// We need to initiate the play from the playlist so further enqueues will auto play/cross fade
module.vlc.readLibrary("Playlist", function(libraryData) {
// Start from the end and work our way forward to find the song we just queued
var songId;
for (var x=libraryData.length-1; x>=0; x--) {
if (libraryData[x].uri === song.Uri) {
songId = libraryData[x].id;
break;
}
}
if (songId !== undefined) {
// Since we found the song, initiate play
require('http').get('http://localhost:8080/requests/status.json?command=pl_play&id=' + escape(songId), function() {
console.log("VLC playlist started");
// Set record of music starting to prevent multiple enqueue attempts during
// the initial delay caused by starting.
module.vlc.nowPlaying.is_playing = true;
module.vlc.nowPlaying.song_position = 0;
module.vlc.nowPlaying.song_total = song.Duration;
// Enqueue worked
if (typeof cb === "function") {
cb.call(this);
}
});
} else {
console.log("VLC playlist could not be started");
// Enqueue worked
if (typeof cb === "function") {
cb.call(this);
}
}
});
} else {
// Enqueue worked
if (typeof cb === "function") {
cb.call(this);
}
}
});
}
};