From 5a3835184ed6c31bff97e716f172abaeae93f100 Mon Sep 17 00:00:00 2001 From: Florrie Date: Tue, 4 Feb 2020 22:03:56 -0400 Subject: remove mkfifo; use socat instead --- players.js | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) (limited to 'players.js') diff --git a/players.js b/players.js index 651b519..07b711c 100644 --- a/players.js +++ b/players.js @@ -1,9 +1,9 @@ // stolen from http-music const { spawn } = require('child_process') -const FIFO = require('fifo-js') -const EventEmitter = require('events') const { commandExists, killProcess, getTimeStrings } = require('./general-util') +const EventEmitter = require('events') +const Socat = require('./socat') class Player extends EventEmitter { constructor() { @@ -116,27 +116,43 @@ module.exports.MPVPlayer = class extends Player { module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { getMPVOptions(file) { - return ['--input-file=' + this.fifo.path, ...super.getMPVOptions(file)] + return ['--input-ipc-server=' + this.socat.path, ...super.getMPVOptions(file)] } playFile(file) { - this.fifo = new FIFO() + let path + do { + path = '/tmp/mtui-socket-' + Math.floor(Math.random() * 10000) + } while (this.existsSync(path)) + + this.socat = new Socat(path) + + const mpv = super.playFile(file) - return super.playFile(file) + return mpv } - sendCommand(command) { - if (this.fifo) { - this.fifo.write(command) + existsSync(path) { + try { + fs.statSync(path) + return true + } catch (error) { + return false + } + } + + sendCommand(...command) { + if (this.socat) { + this.socat.send(JSON.stringify({command})) } } seekAhead(secs) { - this.sendCommand(`seek +${parseFloat(secs)}`) + this.sendCommand('seek', secs) } seekBack(secs) { - this.sendCommand(`seek -${parseFloat(secs)}`) + this.sendCommand('seek', -secs) } volUp(amount) { @@ -151,35 +167,33 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { this.volume = value this.volume = Math.max(0, this.volume) this.volume = Math.min(100, this.volume) - this.sendCommand(`set volume ${this.volume}`) + this.sendCommand('set', 'volume', this.volue) } togglePause() { this.isPaused = !this.isPaused - this.sendCommand('cycle pause') + this.sendCommand('cycle', 'pause') } toggleLoop() { this.isLooping = !this.isLooping - this.sendCommand('cycle loop') + this.sendCommand('cycle', 'loop') } setPause(val) { this.isPaused = !!val - this.sendCommand('set pause ' + (val ? 'yes' : 'no')) + this.sendCommand('set', 'pause', this.isPaused) } setLoop(val) { this.isLooping = !!val - this.sendCommand('set loop ' + (val ? 'yes' : 'no')) + this.sendCommand('set', 'loop', this.isLooping) } kill() { - if (this.fifo) { - this.fifo.close() - delete this.fifo + if (this.socat) { + this.socat.stop() } - return super.kill() } } @@ -246,7 +260,7 @@ module.exports.SoXPlayer = class extends Player { module.exports.getPlayer = async function() { if (await commandExists('mpv')) { - if (await commandExists('mkfifo')) { + if (await commandExists('socat')) { return new module.exports.ControllableMPVPlayer() } else { return new module.exports.MPVPlayer() -- cgit 1.3.0-6-gf8a5