-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplaylist.js
134 lines (124 loc) · 3.32 KB
/
playlist.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
var events = require('events'),
media = require('./media.js'),
fs = require('fs'),
exec = require('child_process').exec,
util = require('util'),
playlist;
playlist = function() {
this.temp_directory = './tmp';
this.sequence = 0;
this.segments_until_next = 0;
this.target_duration = 10;
this.window_length = 10;
this.media = [];
this.paths = {};
this.queue = [];
this.segments = [];
this.begun = false;
}
util.inherits(playlist, events.EventEmitter);
playlist.prototype.addMedia = function(path) {
var m,
index;
if (typeof this.paths[path] === 'undefined') {
m = new media(path);
index = this.media.push(m)
m.index = this.paths[path] = index;
} else {
index = this.paths[path];
}
return this.media[index];
}
playlist.prototype.getRandomMedia = function() {
var random_N = Math.floor(Math.random()*this.media.length);
return (this.queue.indexOf(random_N) === -1) ? random_N : this.getRandomMedia();
}
playlist.prototype.enqueueOne = function(cb) {
var self = this,
media_n = this.getRandomMedia(),
media = this.media[media_n];
if (this.queue.length > 3) return this;
this.queue.push(media_n);
return media
.once('play', function() {
self.enqueueOne();
})
.once('ready', function() {
for (var i = 0; i < this.segments.length; i++) {
self.segments.push({
owner : media_n,
segment : media.segments[i]
});
};
})
.once('ended', function() {
self.playNext();
})
.encode();
}
playlist.prototype.playNext = function(cb) {
var media_n, media, self = this;
if (this.begun === true) {
this.queue.shift();
}
media_n = this.queue[0];
media = this.media[media_n];
console.log("queue length of " + this.queue.length + " [seinfeld-tools]")
media.ready(function() {
self.segments_until_next = media.segments.length;
media.play();
if (self.begun === false) {
self.beginHeartbeat();
}
if (cb) cb.call(self);
})
}
playlist.prototype.formattedSegments = function() {
return this.segments.slice(0, this.window_length);
}
playlist.prototype.currentlyPlaying = function() {
return this.media[this.queue[0]];
}
playlist.prototype.heartbeat = function() {
var old_segment,
new_segment,
self = this;
if (this.segments.length >= 1) {
self.sequence++;
self.segments_until_next--;
old_segment = this.segments.shift();
new_segment = this.segments[0];
if (this.segments.length < 1 || old_segment.owner !== new_segment.owner) {
this.media[old_segment.owner].end();
}
if (new_segment) {
setTimeout(function() {
self.heartbeat();
}, new_segment.segment.duration * 1000)
console.log('next ❤ in ' + new_segment.segment.duration + ' seconds, '+this.segments_until_next+ ' remaining of current episode')
}
}
}
playlist.prototype.beginHeartbeat = function() {
var self = this;
console.log("beginning heartbeat [seinfeld-tools]")
this.emit('started')
this.begun = true;
setTimeout(function() {
self.heartbeat();
}, this.segments[0].segment.duration * 1000);
console.log('next ❤ in ' + this.segments[0].segment.duration + ' seconds')
}
playlist.prototype.start = function() {
var self = this;
if (this.media.length) {
console.log("begin ts folder cleanup [seinfeld-tools]")
exec('rm -rf ./tmp/*', function() {
console.log("cleanup complete [seinfeld-tools]")
self.enqueueOne();
self.playNext();
});
}
return this;
}
module.exports = playlist;