termice
Version:
Simple terminal icecast player
112 lines (111 loc) • 3.8 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const Util = __importStar(require("./util.js"));
class Mplayer {
constructor() {
const config = Util.read_config();
const def_cache = '1024';
const def_wait_io = 100;
this.cache_size = config.mplayer.cache || def_cache;
this.wait_io = config.mplayer.wait_io || def_wait_io;
this.bin_path = config.mplayer.path || 'mplayer';
this.is_init = false;
}
async kill() {
const cmd = 'killall mplayer';
return new Promise((resolve, reject) => {
(0, child_process_1.exec)(cmd, (err) => {
if (err)
reject(err);
else
resolve();
});
});
}
mplayer_stdin(line, call_no_init = false) {
return new Promise((resolve, reject) => {
if (this.is_init) {
this.pipe.stdin.write(line + '\n');
setTimeout(resolve, this.wait_io);
}
else if (call_no_init) {
resolve();
}
else {
reject(Error('mplayer_stdin: Idk why this fails.'));
}
});
}
init_mplayer(url, is_playlist) {
if (this.is_init) {
throw Error('Mplayer already initiated.');
}
else {
const args = ['-cache', this.cache_size, '-slave'];
const options = {
detached: true,
stdio: ['pipe', 'ignore', 'ignore']
};
this.pipe = is_playlist
? (0, child_process_1.spawn)(this.bin_path, args.concat('-playlist', url), options)
: (0, child_process_1.spawn)(this.bin_path, args.concat(url), options);
this.is_init = true;
}
}
async load_file(url, is_playlist) {
const cmd = is_playlist ? 'loadlist' : 'loadfile';
return this.mplayer_stdin(`${cmd} ${url} 0`, true);
}
play(url, is_playlist) {
return new Promise(async (resolve, reject) => {
if (this.is_init) {
try {
await this.load_file(url, is_playlist);
resolve();
}
catch (err) {
reject(err);
}
}
else {
this.init_mplayer(url, is_playlist);
resolve();
}
});
}
async quit() {
return this.mplayer_stdin('quit', true);
}
async pause() {
return this.mplayer_stdin('pause', true);
}
async volume(n) {
return this.mplayer_stdin(`volume ${n} 0`, false);
}
async stop() {
const p = this.mplayer_stdin('stop', true);
this.is_init = false;
return p;
}
}
exports.default = new Mplayer();