voice stuff

This commit is contained in:
Amish Shah
2016-08-25 14:24:06 +01:00
parent 253789f398
commit d6f5df39fc
10 changed files with 48 additions and 30 deletions

File diff suppressed because one or more lines are too long

View File

@@ -112,7 +112,7 @@ class ClientVoiceManager {
this._sendWSJoin(channel);
this.connections.get(channel.guild.id).channel = channel;
}
resolve(existingConn);
return resolve(existingConn);
}
this.pending.set(channel.guild.id, {
channel,

View File

@@ -74,7 +74,6 @@ class VoiceConnectionUDPClient extends EventEmitter {
});
this.udpSocket.on('error', (error, message) => {
console.log(error);
this.emit('error', { error, message });
});

View File

@@ -9,22 +9,25 @@ class VoiceConnectionWebSocket extends EventEmitter {
this.token = token;
this.sessionID = sessionID;
this.serverID = serverID;
this.ws = new WebSocket(`wss://${endpoint}`, null, { rejectUnauthorized: false });
this.heartbeat = null;
this.opened = false;
this.endpoint = endpoint;
this.attempts = 6;
this.setupWS();
}
setupWS() {
this.attempts--;
this.ws = new WebSocket(`wss://${this.endpoint}`, null, { rejectUnauthorized: false });
this.ws.onopen = () => this._onOpen();
this.ws.onmessage = e => this._onMessage(e);
this.ws.onclose = e => this._onClose(e);
this.ws.onerror = e => this._onError(e);
this.ws.on('error', console.log);
this.heartbeat = null;
}
send(data) {
if (this.ws.readyState === WebSocket.OPEN) {
console.log('sending');
this.ws.send(JSON.stringify(data), function ack(error) {
if (error)
console.log(error);
});
this.ws.send(JSON.stringify(data));
}
}
@@ -36,6 +39,7 @@ class VoiceConnectionWebSocket extends EventEmitter {
}
_onOpen() {
this.opened = true;
this.send({
op: Constants.OPCodes.DISPATCH,
d: {
@@ -48,10 +52,16 @@ class VoiceConnectionWebSocket extends EventEmitter {
}
_onClose(e) {
if (!this.opened && this.attempts >= 0) {
return this.setupWS();
}
this.emit('close', e);
}
_onError(e) {
if (!this.opened && this.attempts >= 0) {
return this.setupWS();
}
this.emit('error', e);
}

View File

@@ -1,6 +1,9 @@
class ConverterEngine {
const EventEmitter = require('events').EventEmitter;
class ConverterEngine extends EventEmitter {
constructor(player) {
super();
this.player = player;
}

View File

@@ -15,6 +15,13 @@ class FfmpegConverterEngine extends ConverterEngine {
this.command = chooseCommand();
}
handleError(encoder, err) {
if (encoder.destroy) {
encoder.destroy();
}
this.emit('error', err);
}
createConvertStream() {
super.createConvertStream();
const encoder = ChildProcess.spawn(this.command, [
@@ -26,9 +33,9 @@ class FfmpegConverterEngine extends ConverterEngine {
'-ss', '0',
'pipe:1',
], { stdio: ['pipe', 'pipe', 'ignore'] });
encoder.on('error', console.log);
encoder.stdin.on('error', console.log);
encoder.stdin.on('error', console.log);
encoder.on('error', e => this.handleError(encoder, e));
encoder.stdin.on('error', e => this.handleError(encoder, e));
encoder.stdout.on('error', e => this.handleError(encoder, e));
return encoder;
}
}

View File

@@ -12,6 +12,10 @@ class VoiceConnectionPlayer extends EventEmitter {
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();
}
@@ -38,25 +42,21 @@ class VoiceConnectionPlayer extends EventEmitter {
if (streams) {
if (streams.inputStream && streams.pcmConverter) {
try {
streams.pcmConverter.on('error', console.log);
streams.pcmConverter.stdin.on('error', console.log);
streams.pcmConverter.stdout.on('error', console.log);
streams.inputStream.stdout.on('error', console.log);
if (streams.inputStream.unpipe) {
streams.inputStream.unpipe(streams.pcmConverter.stdin);
this.emit('debug', 'stream kill part 4/5 pass');
}
if (streams.pcmConverter.stdout.destroy) {
streams.pcmConverter.stdout.destroy();
this.emit('debug', 'stream kill part 2/5 pass');
}
if (streams.pcmConverter.stdin) {
streams.pcmConverter.stdin.end();
this.emit('debug', 'stream kill part 1/5 pass');
}
if (streams.pcmConverter && streams.pcmConverter.kill) {
streams.pcmConverter.kill('SIGINT');
this.emit('debug', 'stream kill part 3/5 pass');
}
if (streams.inputStream.unpipe) {
streams.inputStream.unpipe(streams.pcmConverter.stdin);
this.emit('debug', 'stream kill part 4/5 pass');
if (streams.pcmConverter.stdin) {
streams.pcmConverter.stdin.end();
this.emit('debug', 'stream kill part 1/5 pass');
}
if (streams.inputStream.destroy) {
streams.inputStream.destroy();

View File

@@ -4,6 +4,7 @@ const fs = require('fs-extra');
class DefaultPlayer extends BasePlayer {
playFile(file) {
this._shutdown();
const fileStream = fs.createReadStream(file).on('error', console.log);
const pcmStream = this.convertStream(fileStream).on('error', console.log);
const dispatcher = this.playPCMStream(pcmStream);

View File

@@ -194,10 +194,10 @@ class Guild {
_memberSpeakUpdate(user, speaking) {
const member = this.members.get(user);
if (member) {
if (member && member.speaking !== speaking) {
member.speaking = speaking;
this.client.emit(Constants.Events.GUILD_MEMBER_SPEAKING, member, speaking);
}
this.client.emit(Constants.Events.GUILD_MEMBER_SPEAKING, member, speaking);
}
/**

View File

@@ -205,8 +205,6 @@ client.on('message', msg => {
msg.channel.guild.channels.get(chan).join()
.then(conn => {
msg.reply('done');
const f = '04 Out of the Woods.m4a';
conn.player.playFile(`C:/Users/amish/Desktop/${f}`);
conn.player.on('debug', console.log);
})
.catch(console.log);