« get me outta code hell

remove mkfifo; use socat instead - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/players.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-02-04 22:03:56 -0400
committerFlorrie <towerofnix@gmail.com>2020-02-04 22:03:56 -0400
commit5a3835184ed6c31bff97e716f172abaeae93f100 (patch)
treeb3ce7017d67980b56f0223ca41c43c2db9855eca /players.js
parentd14770b5f5ec8aaa350d7b235a6c4e5d40dd9a2d (diff)
remove mkfifo; use socat instead
Diffstat (limited to 'players.js')
-rw-r--r--players.js54
1 files changed, 34 insertions, 20 deletions
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()