-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·159 lines (142 loc) · 3.86 KB
/
index.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
//Get Child Process
var spawn = require('child_process').spawn;
class VLC_RC {
constructor(options) {
this.options = options || {};
this.outData = "";
this.commandAnswer = [];
this.activeCommand = {callback:undefined};
this.commandQueue = []; //{command, callback}
this.startTime = undefined;
this.passedTime = undefined;
this.playing = false;
/*if(!this.options.hasOwnProperty('verbose')) {
this.options.verbose=false;
}
if(!this.options.hasOwnProperty('debug')) {
this.options.debug=false;
}*/
var args = [
"-I rc", //Remote Control Interface
"--play-and-pause", //Pause at end of Movie
//"--fullscreen" //Start in Fullscreen
];
this.vlc_process = spawn('cvlc', args);
if(options.verbose) {
console.log("\x1b[37m\x1b[1m","VLC Process created","\x1b[21m");
console.log("Options:");
console.log("\x1b[44m\x1b[37m",this.options,"\x1b[0m");
}
if(options.debug) {
this.debug = true;
if(options.manual) {
process.stdin.on("data", (data)=>{
this.vlc_process.stdin.write(data);
});
}
} else {
this.debug = false;
}
this.vlc_process.stdout.on("data", this.onStdOut.bind(this));
}
onStdOut(data) {
if(this.debug) process.stdout.write(data);
this.outData += data;
var line = "";
for(let i = 0; i < this.outData.length; i++){
if(this.outData[i] == "\n") { //New Line
if(line.length <= 0) continue;
this.commandAnswer.push(line);
line = "";
} else {
line += this.outData[i];
}
}
if(line[0] == ">") { //Neuer Befehr erwartet
if(this.activeCommand){
if(this.activeCommand.callback) this.activeCommand.callback(this.commandAnswer.slice(0));
this.activeCommand = undefined;
}
this.commandAnswer = [];
line = "";
this.nextCommand();
}
this.outData = line;
}
nextCommand(){
if(this.activeCommand != undefined) return;
if(this.commandQueue.length < 1) return;
var c = this.commandQueue[0];
this.activeCommand = c;
this.vlc_process.stdin.write(c.command + "\n");
if(this.debug) process.stdout.write(c.command + "\n");
this.commandQueue.splice(0, 1);
}
runCommand(command, callback){
this.commandQueue.push({command:command, callback:callback});
this.nextCommand();
}
test() {
console.log(this);
}
loadFile(path, callback) {
//ToDo remove all other files
this.runCommand("playlist", (data)=>{
data.forEach(function(element) {
var d = element.replace(/\D/g,'');
if(d.length < 1) return;
this.runCommand("delete " + d[0]);
}, this);
this.runCommand("enqueue " + path, callback);
});
}
play(callback) {
this.runCommand("play", ()=>{
this.playing = true;
if(this.passedTime) {
this.startTime = new Date().getTime() - this.passedTime();
} else {
this.startTime = new Date().getTime();
}
});
}
getTime(callback) {
this.getOrigTime((data)=>{
if(!this.startTime) return data;
var dif = new Date().getTime() - this.startTime;
var difr = Math.floor(dif/1000);
if(difr != data){
this.startTime = new Date().getTime() - data*1000;
return callback(data);
}
return callback(dif);
});
}
getOrigTime(callback) {
this.runCommand("get_time", (data)=>{
callback(Number(data));
});
}
pause(callback) {
this.runCommand("pause", ()=>{
this.passedTime = new Date().getTime() - this.startTime;
this.startTime = undefined;
callback();
});
}
stop(callback) {
this.runCommand("stop", callback);
this.startTime = undefined;
this.passedTime = undefined;
}
getState(callback){
this.getOrigTime((time)=>{
this.runCommand("get_length", (data)=>{
if(time >= Number(data[0])) this.playing = false;
else this.playing = true;
callback(this.playing);
});
});
}
}
module.exports.VLC_RC = VLC_RC;