Remove old stuff

This commit is contained in:
Amish Shah
2016-12-28 18:16:26 +00:00
parent 8e75b47a7b
commit 0a47d0e1d6
6 changed files with 18 additions and 552 deletions

View File

@@ -32,14 +32,20 @@ class AudioPlayer extends EventEmitter {
return this.streams.last().transcoder;
}
destroyAllStreams(exceptLatest) {
destroyStream(stream) {
const data = this.streams.get(stream);
if (!data) return;
const transcoder = data.transcoder;
const dispatcher = data.dispatcher;
if (transcoder) transcoder.kill();
if (dispatcher) dispatcher.destroy('end');
}
destroyAllStreams(except) {
for (const stream of this.streams.keys()) {
const data = this.streams.get(stream);
const transcoder = data.transcoder;
const dispatcher = data.dispatcher;
if (exceptLatest && transcoder === this.currentTranscoder) continue;
if (transcoder) transcoder.kill();
if (dispatcher) dispatcher.destroy('end');
if (except === stream) continue;
if (except === true && this.streams.get(stream) === this.streams.last()) continue;
this.destroyStream(stream);
}
}
@@ -51,6 +57,11 @@ class AudioPlayer extends EventEmitter {
ffmpegArguments: ffmpegArguments.concat(['-ss', String(seek)]),
});
this.streams.set(stream, { transcoder });
transcoder.on('error', e => {
this.destroyStream(stream);
if (this.listenerCount('error') > 0) this.emit('error', e);
else this.emit('warn', e);
});
this.playPCMStream(transcoder.output, options);
}

View File

@@ -1,100 +0,0 @@
const OpusEncoders = require('../opus/OpusEngineList');
const EventEmitter = require('events').EventEmitter;
const StreamDispatcher = require('../dispatcher/StreamDispatcher');
const ffmpegArguments = [
'-analyzeduration', '0',
'-loglevel', '0',
'-f', 's16le',
'-ar', '48000',
'-ac', '2',
];
/**
* Represents the Audio Player of a Voice Connection
* @extends {EventEmitter}
* @private
*/
class AudioPlayer extends EventEmitter {
constructor(voiceConnection) {
super();
/**
* The voice connection the player belongs to
* @type {VoiceConnection}
*/
this.voiceConnection = voiceConnection;
this.opusEncoder = OpusEncoders.fetch();
this.currentConverter = null;
/**
* The current stream dispatcher, if a stream is being played
* @type {StreamDispatcher}
*/
this.dispatcher = null;
// this.prism.on('error', e => this.emit('error', e));
this.streamingData = {
channels: 2,
count: 0,
sequence: 0,
timestamp: 0,
pausedTime: 0,
};
this.voiceConnection.on('closing', () => this.cleanup(null, 'voice connection closing'));
}
get prism() {
return this.voiceConnection.prism;
}
playUnknownStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
const options = { seek, volume, passes };
const transcoder = this.prism.transcode({
type: 'ffmpeg',
media: stream,
ffmpegArguments,
});
this.playPCMStream(transcoder.output, options);
}
/*playUnknownStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
const options = { seek, volume, passes };
stream.on('end', () => {
this.emit('debug', 'Input stream to converter has ended');
});
stream.on('error', e => this.emit('error', e));
const conversionProcess = this.audioToPCM.createConvertStream(options.seek);
conversionProcess.on('error', e => this.emit('error', e));
conversionProcess.setInput(stream);
return this.playPCMStream(conversionProcess.process.stdout, conversionProcess, options);
}*/
cleanup(checkStream, reason) {
// cleanup is a lot less aggressive than v9 because it doesn't try to kill every single stream it is aware of
this.emit('debug', `Clean up triggered due to ${reason}`);
const filter = checkStream && this.dispatcher && this.dispatcher.stream === checkStream;
if (this.currentConverter && (checkStream ? filter : true)) {
this.currentConverter.destroy();
this.currentConverter = null;
}
}
playPCMStream(stream, converter, { seek = 0, volume = 1, passes = 1 } = {}) {
const options = { seek, volume, passes };
stream.on('end', () => this.emit('debug', 'PCM input stream ended'));
this.cleanup(null, 'outstanding play stream');
this.currentConverter = converter;
if (this.dispatcher) {
this.streamingData = this.dispatcher.streamingData;
}
stream.on('error', e => this.emit('error', e));
const dispatcher = new StreamDispatcher(this, stream, this.streamingData, options);
dispatcher.on('error', e => this.emit('error', e));
dispatcher.on('end', () => this.cleanup(dispatcher.stream, 'dispatcher ended'));
dispatcher.on('speaking', value => this.voiceConnection.setSpeaking(value));
this.dispatcher = dispatcher;
dispatcher.on('debug', m => this.emit('debug', `Stream dispatch - ${m}`));
return dispatcher;
}
}
module.exports = AudioPlayer;

View File

@@ -1,121 +0,0 @@
const OpusEngines = require('../opus/OpusEngineList');
const ConverterEngines = require('../pcm/ConverterEngineList');
const Constants = require('../../../util/Constants');
const StreamDispatcher = require('../dispatcher/StreamDispatcher');
const EventEmitter = require('events').EventEmitter;
class VoiceConnectionPlayer extends EventEmitter {
constructor(connection) {
super();
this.connection = connection;
this.opusEncoder = OpusEngines.fetch();
const Engine = ConverterEngines.fetch();
this.converterEngine = new Engine(this);
this.converterEngine.on('error', err => {
this._shutdown();
this.emit('error', err);
});
this.speaking = false;
this.processMap = new Map();
this.dispatcher = null;
this._streamingData = {
sequence: 0,
timestamp: 0,
};
}
convertStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
const options = { seek, volume, passes };
const encoder = this.converterEngine.createConvertStream(options.seek);
const pipe = stream.pipe(encoder.stdin, { end: false });
pipe.on('unpipe', () => {
this.killStream(encoder.stdout);
pipe.destroy();
});
this.processMap.set(encoder.stdout, {
pcmConverter: encoder,
inputStream: stream,
});
return encoder.stdout;
}
_shutdown() {
this.speaking = false;
if (this.dispatcher) this.dispatcher._triggerTerminalState('end', 'ended by parent player shutdown');
for (const stream of this.processMap.keys()) this.killStream(stream);
}
killStream(stream) {
const streams = this.processMap.get(stream);
this._streamingData = this.dispatcher.streamingData;
this.emit(Constants.Events.DEBUG, 'Cleaning up player after audio stream ended or encountered an error');
const dummyHandler = () => null;
if (streams) {
this.processMap.delete(stream);
if (streams.inputStream && streams.pcmConverter) {
try {
streams.inputStream.once('error', dummyHandler);
streams.pcmConverter.once('error', dummyHandler);
streams.pcmConverter.stdin.once('error', dummyHandler);
streams.pcmConverter.stdout.once('error', dummyHandler);
if (streams.inputStream.unpipe) {
streams.inputStream.unpipe(streams.pcmConverter.stdin);
this.emit(Constants.Events.DEBUG, '- Unpiped input stream');
} else if (streams.inputStream.destroy) {
streams.inputStream.destroy();
this.emit(Constants.Events.DEBUG, '- Couldn\'t unpipe input stream, so destroyed input stream');
}
if (streams.pcmConverter.stdin) {
streams.pcmConverter.stdin.end();
this.emit(Constants.Events.DEBUG, '- Ended input stream to PCM converter');
}
if (streams.pcmConverter && streams.pcmConverter.kill) {
streams.pcmConverter.kill('SIGINT');
this.emit(Constants.Events.DEBUG, '- Killed the PCM converter');
}
} catch (err) {
// if an error happened make sure the pcm converter is killed anyway
try {
if (streams.pcmConverter && streams.pcmConverter.kill) {
streams.pcmConverter.kill('SIGINT');
this.emit(Constants.Events.DEBUG, '- Killed the PCM converter after previous error (abnormal)');
}
} catch (e) {
return e;
}
return err;
}
}
}
return null;
}
setSpeaking(value) {
if (this.speaking === value) return;
this.speaking = value;
this.connection.websocket.send({
op: Constants.VoiceOPCodes.SPEAKING,
d: {
speaking: true,
delay: 0,
},
}).catch(e => {
this.emit('debug', e);
});
}
playPCMStream(pcmStream, { seek = 0, volume = 1, passes = 1 } = {}) {
const options = { seek, volume, passes };
const dispatcher = new StreamDispatcher(this, pcmStream, this._streamingData, options);
dispatcher.on('speaking', value => this.setSpeaking(value));
dispatcher.on('end', () => this.killStream(pcmStream));
dispatcher.on('error', () => this.killStream(pcmStream));
dispatcher.setVolume(options.volume);
this.dispatcher = dispatcher;
return dispatcher;
}
}
module.exports = VoiceConnectionPlayer;

View File

@@ -1,19 +0,0 @@
const BasePlayer = require('./BasePlayer');
const fs = require('fs');
class DefaultPlayer extends BasePlayer {
playFile(file, { seek = 0, volume = 1 } = {}) {
const options = { seek: seek, volume: volume };
return this.playStream(fs.createReadStream(file), options);
}
playStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
this._shutdown();
const options = { seek, volume, passes };
const pcmStream = this.convertStream(stream, options);
const dispatcher = this.playPCMStream(pcmStream, options);
return dispatcher;
}
}
module.exports = DefaultPlayer;