diff --git a/.gitignore b/.gitignore index 00cbbdf..5bec922 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ logs npm-debug.log* yarn-debug.log* yarn-error.log* +.vscode/ # Runtime data pids diff --git a/index.js b/index.js index 3918f73..c47f249 100755 --- a/index.js +++ b/index.js @@ -1,32 +1,159 @@ //Get Child Process var spawn = require('child_process').spawn; -var VLC_RC = function(options) { - this.options = options || {}; - if(!this.options.hasOwnProperty('verbose')) { - this.options.verbose=false; + +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)); } - if(!this.options.hasOwnProperty('debug')) { - this.options.debug=false; + + 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; } - var args = [ - "-I rc", //Remote Control Interface - "--play-and-pause", //Pause at end of Movie - "--fullscreen" //Start in Fullscreen - ]; - this.vlc_process = spawn('vlc', 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"); + + 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); } - if(options.debug) { - this.vlc_process.stdout.pipe(process.stdout); - this.vlc_process.stderr.pipe(process.stderr); + + 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); + }); + }); } } -VLC_RC.prototype.hello = function() { - //console.log(this.options); -} -module.exports.VLC_RC = VLC_RC; - +module.exports.VLC_RC = VLC_RC; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..1766560 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,12 @@ +{ + "name": "vlc_rc", + "version": "0.0.1", + "lockfileVersion": 1, + "dependencies": { + "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=" + } + } +} diff --git a/package.json b/package.json index ab12b15..e504c5a 100755 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "NodeJS Binding for the VLC Media Player remote control", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node test.js", + "start": "node index.js" }, "repository": { "type": "git", @@ -23,5 +24,8 @@ "bugs": { "url": "https://github.com/margau/node_vlc_rc/issues" }, - "homepage": "https://github.com/margau/node_vlc_rc#readme" + "homepage": "https://github.com/margau/node_vlc_rc#readme", + "dependencies": { + "another-telnet-client": "^0.1.3" + } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..8563b24 --- /dev/null +++ b/test.js @@ -0,0 +1,6 @@ +var i = require("./index.js"); +var vlc = new i.VLC_RC({verbose:false, debug:true, manual:true}); +vlc.loadFile("/home/fabian/Downloads/testv.mp4"); +vlc.loadFile("/home/fabian/Downloads/t2.mp4"); +//vlc.pause(); +//vlc.play(); \ No newline at end of file