2022-04-14 14:25:48 +02:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
const { spawn } = require('child_process');
|
2022-04-14 15:26:46 +02:00
|
|
|
const createWriteStream = require('fs').createWriteStream;
|
|
|
|
const unlink = require('fs/promises').unlink;
|
|
|
|
const resolve = require('path').resolve;
|
2022-04-14 14:25:48 +02:00
|
|
|
|
|
|
|
class Player extends EventEmitter {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2022-04-21 13:40:00 +02:00
|
|
|
this.timestamp = Date.now();
|
2022-04-14 14:25:48 +02:00
|
|
|
this.position = 0;
|
|
|
|
this.events = [];
|
2022-04-14 15:26:46 +02:00
|
|
|
this.tmp = {
|
2022-04-20 16:15:51 +02:00
|
|
|
file: resolve(global.config?.tmp || '/tmp/kannon.tmp')
|
|
|
|
};
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async prepare(size, threshold) {
|
|
|
|
logger.debug('preparing audio player...');
|
2022-04-20 16:15:51 +02:00
|
|
|
await this.#reset();
|
|
|
|
await this.#removeTemporaryFile();
|
2022-04-14 14:25:48 +02:00
|
|
|
this.size = size;
|
|
|
|
this.threshold = threshold;
|
2022-04-14 15:26:46 +02:00
|
|
|
this.tmp.stream = createWriteStream(this.tmp.file);
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
2022-04-21 13:40:00 +02:00
|
|
|
async feed(buffer) {
|
|
|
|
this.tmp.stream.write(buffer);
|
|
|
|
if (this.tmp.announced === undefined && this.tmp.stream.bytesWritten >= this.threshold) {
|
|
|
|
this.tmp.announced = true;
|
|
|
|
this.#setState(constants.STATE_READY);
|
|
|
|
logger.debug('threshold of ' + this.threshold + ' bytes reached after ' + (Date.now() - this.timestamp) + 'ms');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stopFeed() {
|
|
|
|
logger.debug('finished writing of ' + this.tmp.stream.bytesWritten + ' bytes after ' + (Date.now() - this.timestamp) + 'ms');
|
|
|
|
this.tmp.stream.end();
|
|
|
|
this.tmp.stream.close();
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 16:15:51 +02:00
|
|
|
async play(position) {
|
2022-04-14 14:25:48 +02:00
|
|
|
if (this.isPlaying()) {
|
2022-04-21 13:40:00 +02:00
|
|
|
await this.stop();
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
2022-04-20 16:15:51 +02:00
|
|
|
await this.#spawnProcess(position);
|
2022-04-14 14:25:48 +02:00
|
|
|
this.process.stderr.on('data', (data) => {
|
|
|
|
data = data.toString();
|
|
|
|
const position = data.toString().trim().split(' ')[0];
|
|
|
|
if (position.length === 0 || isNaN(position)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.position = position;
|
|
|
|
});
|
|
|
|
this.process.stdin.on('error', (error) => {
|
2022-04-21 13:40:00 +02:00
|
|
|
this.#setState(constants.STATE_ERROR, error);
|
2022-04-14 14:25:48 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async pause() {
|
2022-04-20 16:15:51 +02:00
|
|
|
await this.#reset(true);
|
2022-04-21 13:40:00 +02:00
|
|
|
this.#setState(constants.STATE_PAUSED);
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async stop() {
|
2022-04-20 16:15:51 +02:00
|
|
|
await this.#reset();
|
2022-04-21 13:40:00 +02:00
|
|
|
this.#setState(constants.STATE_STOPPED);
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isReady() {
|
2022-04-21 13:40:00 +02:00
|
|
|
return this.state === constants.STATE_READY;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isPlaying() {
|
2022-04-21 13:40:00 +02:00
|
|
|
return this.state === constants.STATE_PLAYING;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isPaused() {
|
2022-04-21 13:40:00 +02:00
|
|
|
return this.state === constants.STATE_PAUSED;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isFinished() {
|
2022-04-21 13:40:00 +02:00
|
|
|
return this.state === constants.STATE_STOPPED;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hasError() {
|
2022-04-21 13:40:00 +02:00
|
|
|
return this.state === constants.STATE_ERROR;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 16:15:51 +02:00
|
|
|
getPosition() {
|
|
|
|
return this.position;
|
|
|
|
}
|
|
|
|
|
|
|
|
async #spawnProcess(position) {
|
2022-04-14 14:25:48 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-04-14 15:26:46 +02:00
|
|
|
const args = [
|
|
|
|
'-vn',
|
|
|
|
'-nodisp'
|
|
|
|
];
|
2022-04-20 16:15:51 +02:00
|
|
|
if (this.isPaused() && !isNaN(position)) {
|
|
|
|
args.unshift('-ss', position);
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
2022-04-20 16:15:51 +02:00
|
|
|
args.push(this.tmp.file);
|
2022-04-14 15:26:46 +02:00
|
|
|
this.process = spawn("ffplay", args);
|
2022-04-14 14:25:48 +02:00
|
|
|
this.process.on('error', (error) => {
|
|
|
|
this.#reset();
|
|
|
|
// TODO: try/catch error
|
|
|
|
reject('error spawning process \'ffplay\': ' + error);
|
|
|
|
});
|
|
|
|
this.process.on('spawn', () => {
|
|
|
|
logger.info('spawned process \'ffplay\' (pid: ' + this.process.pid + ')...');
|
2022-04-21 13:40:00 +02:00
|
|
|
this.#setState(constants.STATE_PLAYING);
|
2022-04-14 14:25:48 +02:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#setState(state, data) {
|
|
|
|
if (this.state === state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.state = state;
|
|
|
|
logger.debug('setting state of audio player to \'' + state + '\'...');
|
|
|
|
if (this.events.includes(state)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-14 15:26:46 +02:00
|
|
|
logger.debug('emitting state \'' + state + '\' of audio player...');
|
2022-04-20 16:15:51 +02:00
|
|
|
this.emit(this.state, { data: data });
|
|
|
|
this.emit('statechange', { state: this.state });
|
2022-04-14 14:25:48 +02:00
|
|
|
this.events.push(state);
|
|
|
|
}
|
|
|
|
|
2022-04-20 16:15:51 +02:00
|
|
|
async #killProcess() {
|
|
|
|
if (this.process === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const pid = this.process.pid;
|
2022-04-14 14:25:48 +02:00
|
|
|
this.#closeStdIO();
|
|
|
|
if (this.process?.killed === false) {
|
|
|
|
this.process.kill('SIGTERM');
|
|
|
|
}
|
2022-04-20 16:15:51 +02:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
this.process.on('close', (code, signal) => {
|
|
|
|
let msg = 'process \'ffplay\' (pid: ' + pid + ') closed with';
|
|
|
|
if (code !== undefined) {
|
|
|
|
msg += ' code \'' + code + '\'';
|
|
|
|
} else {
|
|
|
|
msg += ' signal \'' + signal + '\'';
|
|
|
|
}
|
|
|
|
logger.debug(msg);
|
|
|
|
this.process = undefined;
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#closeStdIO() {
|
|
|
|
if (this.process?.stdio === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logger.debug('closing all stdio streams of process \'ffplay\' (pid: ' + this.process.pid + ')...');
|
|
|
|
for (let index = 0; index < this.process.stdio.length; index++) {
|
|
|
|
this.process.stdio[index].destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 16:15:51 +02:00
|
|
|
async #removeTemporaryFile() {
|
|
|
|
try {
|
|
|
|
await unlink(this.tmp.file);
|
|
|
|
} catch (error) {
|
|
|
|
if (error?.code === 'ENOENT') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logger.error('error removing temporary file \'' + this.tmp.file + '\': ' + error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async #reset(paused) {
|
|
|
|
await this.#killProcess();
|
|
|
|
this.timestamp = Date.now();
|
2022-04-14 14:25:48 +02:00
|
|
|
this.events = [];
|
2022-04-20 16:15:51 +02:00
|
|
|
if (paused === true) {
|
|
|
|
return;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
2022-04-20 16:15:51 +02:00
|
|
|
this.position = 0;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Player;
|