small changes

This commit is contained in:
Fabian Stamm 2017-06-20 11:15:03 +02:00
parent ccddd1abcb
commit 64b94c96db
6 changed files with 441 additions and 160 deletions

BIN
files/t2.mp4 Normal file

Binary file not shown.

BIN
files/testv.mp4 Normal file

Binary file not shown.

213
index.js
View File

@ -1,159 +1,54 @@
//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;
const DEBUG = process.env.debug | false;
var p = require("./vlc_rc");
var player = new p.VLC_RC({verbose:true, debug:false, manual:false});
var http = require("http").createServer();
var io = require("socket.io")(http);
var activeFile = undefined;
// DEBUG
if(DEBUG){
//player.loadFile(__dirname + "/files/testv.mp4", ()=>{
activeFile = "testv.mp4";
player.loadFile(__dirname + "/files/" + activeFile, ()=>{
player.play();
});
}
// END DEBUG
io.on("connection", (socket)=>{
//console.log("client connected:", socket.client.id);
socket.on("play", (data)=>{
activeFile = data.file;
player.loadFile(__dirname + "/files/" + data.file, ()=>{
player.play(()=>{
socket.emit("playing", {file: data.file});
});
});
});
socket.on("stop", (data)=>{
player.stop(()=>{
io.emit("stopped");
});
});
});
setInterval(()=>{
var start
var update_infos = {};
player.getTime((t)=>{
player.getState((playing)=>{
update_infos.file = activeFile;
update_infos.time = t;
update_infos.is_playing = playing;
//update_infos.
io.emit("update", update_infos);
})
});
}, 1000/25)
http.listen(5000);

218
package-lock.json generated
View File

@ -3,10 +3,228 @@
"version": "0.0.1",
"lockfileVersion": 1,
"dependencies": {
"accepts": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.3.tgz",
"integrity": "sha1-w8p0NJOGSMPg2cHjKN1otiLChMo="
},
"after": {
"version": "0.8.2",
"resolved": "https://registry.npmjs.org/after/-/after-0.8.2.tgz",
"integrity": "sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8="
},
"another-telnet-client": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/another-telnet-client/-/another-telnet-client-0.1.3.tgz",
"integrity": "sha1-3me4Ehr7R39e0OAlkByyIqoONS0="
},
"arraybuffer.slice": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/arraybuffer.slice/-/arraybuffer.slice-0.0.6.tgz",
"integrity": "sha1-8zshWfBTKj8xB6JywMz70a0peco="
},
"backo2": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz",
"integrity": "sha1-MasayLEpNjRj41s+u2n038+6eUc="
},
"base64-arraybuffer": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz",
"integrity": "sha1-c5JncZI7Whl0etZmqlzUv5xunOg="
},
"base64id": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/base64id/-/base64id-1.0.0.tgz",
"integrity": "sha1-R2iMuZu2gE8OBtPnY7HDLlfY5rY="
},
"better-assert": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/better-assert/-/better-assert-1.0.2.tgz",
"integrity": "sha1-QIZrnhueC1W0gYlDEeaPr/rrxSI="
},
"blob": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/blob/-/blob-0.0.4.tgz",
"integrity": "sha1-vPEwUspURj8w+fx+lbmkdjCpSSE="
},
"callsite": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/callsite/-/callsite-1.0.0.tgz",
"integrity": "sha1-KAOY5dZkvXQDi28JBRU+borxvCA="
},
"component-bind": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/component-bind/-/component-bind-1.0.0.tgz",
"integrity": "sha1-AMYIq33Nk4l8AAllGx06jh5zu9E="
},
"component-emitter": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.2.1.tgz",
"integrity": "sha1-E3kY1teCg/ffemt8WmPhQOaUJeY="
},
"component-inherit": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/component-inherit/-/component-inherit-0.0.3.tgz",
"integrity": "sha1-ZF/ErfWLcrZJ1crmUTVhnbJv8UM="
},
"cookie": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz",
"integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s="
},
"debug": {
"version": "2.6.8",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.8.tgz",
"integrity": "sha1-5zFTHKLt4n0YgiJCfaF4IdaP9Pw="
},
"engine.io": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-3.1.0.tgz",
"integrity": "sha1-XKQ4486f28kVxKIcjdnhJmcG5X4="
},
"engine.io-client": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-3.1.1.tgz",
"integrity": "sha1-QVqYUrrbFPoAj6PvHjFgjbZ2EyU="
},
"engine.io-parser": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-2.1.1.tgz",
"integrity": "sha1-4Ps/DgRi9/WLt3waUun1p+JuRmg="
},
"has-binary2": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/has-binary2/-/has-binary2-1.0.2.tgz",
"integrity": "sha1-6D26SfC5vk0CbSc2U1DZ8D9Uvpg="
},
"has-cors": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/has-cors/-/has-cors-1.1.0.tgz",
"integrity": "sha1-XkdHk/fqmEPRu5nCPu9J/xJv/zk="
},
"indexof": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/indexof/-/indexof-0.0.1.tgz",
"integrity": "sha1-gtwzbSMrkGIXnQWrMpOmYFn9Q10="
},
"isarray": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-2.0.1.tgz",
"integrity": "sha1-o32U7ZzaLVmGXJ92/llu4fM4dB4="
},
"mime-db": {
"version": "1.27.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.27.0.tgz",
"integrity": "sha1-gg9XIpa70g7CXtVeW13oaeVDbrE="
},
"mime-types": {
"version": "2.1.15",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.15.tgz",
"integrity": "sha1-pOv1BkCUVpI3uM9wBGd20J/JKu0="
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"negotiator": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz",
"integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk="
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
},
"object-component": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/object-component/-/object-component-0.0.3.tgz",
"integrity": "sha1-8MaapQ78lbhmwYb0AKM3acsvEpE="
},
"parsejson": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/parsejson/-/parsejson-0.0.3.tgz",
"integrity": "sha1-q343WfIJ7OmUN5c/fQ8fZK4OZKs="
},
"parseqs": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/parseqs/-/parseqs-0.0.5.tgz",
"integrity": "sha1-1SCKNzjkZ2bikbouoXNoSSGouJ0="
},
"parseuri": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/parseuri/-/parseuri-0.0.5.tgz",
"integrity": "sha1-gCBKUNTbt3m/3G6+J3jZDkvOMgo="
},
"safe-buffer": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.0.1.tgz",
"integrity": "sha1-0mPKVGls2KMGtcplUekt5XkY++c="
},
"socket.io": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-2.0.3.tgz",
"integrity": "sha1-Q1nwaiSTOua9CHeYr3jGgOrjReM="
},
"socket.io-adapter": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-1.1.0.tgz",
"integrity": "sha1-x6pGUB3VVsLLiiivj/lcC14dqkw=",
"dependencies": {
"debug": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.3.3.tgz",
"integrity": "sha1-QMRT5n5uE8kB3ewxeviYbNqe/4w="
},
"ms": {
"version": "0.7.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-0.7.2.tgz",
"integrity": "sha1-riXPJRKziFodldfwN4aNhDESR2U="
}
}
},
"socket.io-client": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-2.0.3.tgz",
"integrity": "sha1-bK9K/5+FsZ/ZG2zhPWmttWT4hzs="
},
"socket.io-parser": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-3.1.2.tgz",
"integrity": "sha1-28IoIVH8T6675Aru3Ady66YZ9/I="
},
"to-array": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/to-array/-/to-array-0.1.4.tgz",
"integrity": "sha1-F+bBH3PdTz10zaek/zI46a2b+JA="
},
"ultron": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.0.tgz",
"integrity": "sha1-sHoualQagV/Go0zNRTO67DB8qGQ="
},
"uws": {
"version": "0.14.5",
"resolved": "https://registry.npmjs.org/uws/-/uws-0.14.5.tgz",
"integrity": "sha1-Z6rzPEaypYel9mZtAPdpEyjxSdw=",
"optional": true
},
"ws": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/ws/-/ws-2.3.1.tgz",
"integrity": "sha1-a5Sz5EfLajY/eF6vlK9jWejoHIA="
},
"xmlhttprequest-ssl": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.3.tgz",
"integrity": "sha1-GFqIjATspGw+QHDZn3tJ3jUomS0="
},
"yeast": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/yeast/-/yeast-0.1.2.tgz",
"integrity": "sha1-AI4G2AlDIMNy28L47XagymyKxBk="
}
}
}

View File

@ -26,6 +26,7 @@
},
"homepage": "https://github.com/margau/node_vlc_rc#readme",
"dependencies": {
"another-telnet-client": "^0.1.3"
"another-telnet-client": "^0.1.3",
"socket.io": "^2.0.3"
}
}

167
vlc_rc.js Normal file
View File

@ -0,0 +1,167 @@
//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]);
var elements = element.split(" ");
for(var l = 0; l < elements.length; l++){
var n = Number(elements[l]);
if(Number.isInteger(n)) {
this.runCommand("delete "+ n);
l = Number.MAX_VALUE;
}
}
}, 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;