78 lines
1.7 KiB
JavaScript
78 lines
1.7 KiB
JavaScript
"use strict";
|
|
|
|
const tags = require('./library/tags');
|
|
|
|
const throttle = require('throttle-debounce/throttle');
|
|
|
|
const ChildProcessEmitter = require('./child-process').ChildProcessEmitter;
|
|
|
|
const DEFAULT_PAUSE_THROTTLE = 2000;
|
|
const DEFAULT_PLAY_THROTTLE = 5000;
|
|
const DEFAULT_UNKNOWN_THROTTLE = 2000;
|
|
|
|
class MediaPlayer extends ChildProcessEmitter {
|
|
constructor(config, logger) {
|
|
super(config.mpg321, logger);
|
|
|
|
this.stderrFilters.push(line => {
|
|
return line.substr(0, 3) == '@F '
|
|
});
|
|
|
|
this.pauseThrottled = throttle(config.pause_throttle || DEFAULT_PAUSE_THROTTLE, () => {
|
|
this._pause();
|
|
});
|
|
|
|
this.playFileThrottled = throttle(config.play_throttle || DEFAULT_PLAY_THROTTLE, (path) => {
|
|
this._playFile(path);
|
|
});
|
|
|
|
this.unknown = throttle(config.unknown_throttle || DEFAULT_UNKNOWN_THROTTLE, this._unknown);
|
|
|
|
// default to unthrottled
|
|
this.pause = this._pauseFile;
|
|
this.playFile = this._playFile;
|
|
}
|
|
|
|
throttle() {
|
|
this.pause = this.pauseThrottled;
|
|
this.playFile = this.playFileThrottled;
|
|
}
|
|
|
|
stop(tag) {
|
|
this.emit('command', tag);
|
|
this.send('STOP');
|
|
}
|
|
|
|
_pause(tag) {
|
|
this.emit('command', tag);
|
|
this.send('PAUSE');
|
|
}
|
|
|
|
_playFile(tag) {
|
|
this.emit('command', tag);
|
|
this.send("LOAD media/" + tag.path);
|
|
}
|
|
|
|
_unknown(tag) {
|
|
this.emit('command', tag);
|
|
}
|
|
|
|
handleTag(tag) {
|
|
if (tag instanceof tags.StopCommand) {
|
|
this.stop(tag);
|
|
} else if (tag instanceof tags.PauseCommand) {
|
|
this.pause(tag);
|
|
} else if (tag instanceof tags.FileTag) {
|
|
this.playFile(tag);
|
|
} else {
|
|
this.unknown(tag);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = function(config, logger) {
|
|
return new MediaPlayer(config, logger);
|
|
};
|
|
|
|
// vim:set ts=2 sw=2 et:
|