From fd09d0196f8db2102f9364a56f3075bf2cd93c88 Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 6 Feb 2020 17:40:33 -0400 Subject: more socat stuff : shrug emoji : :) --- players.js | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'players.js') diff --git a/players.js b/players.js index f2cced2..5d332b3 100644 --- a/players.js +++ b/players.js @@ -4,6 +4,10 @@ const { spawn } = require('child_process') const { commandExists, killProcess, getTimeStrings } = require('./general-util') const EventEmitter = require('events') const Socat = require('./socat') +const fs = require('fs') +const util = require('util') + +const unlink = util.promisify(fs.unlink) class Player extends EventEmitter { constructor() { @@ -120,16 +124,19 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { } playFile(file) { - let path + this.removeSocket(this.socketPath) + do { - // path = '/tmp/mtui-socket-' + Math.floor(Math.random() * 10000) - path = './mtui-socket-' + Math.floor(Math.random() * 10000) - } while (this.existsSync(path)) + // this.socketPathpath = '/tmp/mtui-socket-' + Math.floor(Math.random() * 10000) + this.socketPath = __dirname + '/mtui-socket-' + Math.floor(Math.random() * 10000) + } while (this.existsSync(this.socketPath)) - this.socat = new Socat(path) + this.socat = new Socat(this.socketPath) const mpv = super.playFile(file) + mpv.then(() => this.removeSocket(this.socketPath)) + return mpv } @@ -191,11 +198,21 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { this.sendCommand('set', 'loop', this.isLooping) } - kill() { + async kill() { + const path = this.socketPath + delete this.socketPath if (this.socat) { - this.socat.stop() + await this.socat.dispose() + await this.socat.stop() + } + await super.kill() + await this.removeSocket(path) + } + + async removeSocket(path) { + if (path) { + await unlink(path).catch(() => {}) } - return super.kill() } } -- cgit 1.3.0-6-gf8a5