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._sendWSJoin(channel);
this.connections.get(channel.guild.id).channel = channel; this.connections.get(channel.guild.id).channel = channel;
} }
resolve(existingConn); return resolve(existingConn);
} }
this.pending.set(channel.guild.id, { this.pending.set(channel.guild.id, {
channel, channel,

View File

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

View File

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

View File

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

View File

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

View File

@@ -12,6 +12,10 @@ class VoiceConnectionPlayer extends EventEmitter {
this.opusEncoder = OpusEngines.fetch(); this.opusEncoder = OpusEngines.fetch();
const Engine = ConverterEngines.fetch(); const Engine = ConverterEngines.fetch();
this.converterEngine = new Engine(this); this.converterEngine = new Engine(this);
this.converterEngine.on('error', err => {
this._shutdown();
this.emit('error', err);
});
this.speaking = false; this.speaking = false;
this.processMap = new Map(); this.processMap = new Map();
} }
@@ -38,25 +42,21 @@ class VoiceConnectionPlayer extends EventEmitter {
if (streams) { if (streams) {
if (streams.inputStream && streams.pcmConverter) { if (streams.inputStream && streams.pcmConverter) {
try { try {
streams.pcmConverter.on('error', console.log); if (streams.inputStream.unpipe) {
streams.pcmConverter.stdin.on('error', console.log); streams.inputStream.unpipe(streams.pcmConverter.stdin);
streams.pcmConverter.stdout.on('error', console.log); this.emit('debug', 'stream kill part 4/5 pass');
streams.inputStream.stdout.on('error', console.log); }
if (streams.pcmConverter.stdout.destroy) { if (streams.pcmConverter.stdout.destroy) {
streams.pcmConverter.stdout.destroy(); streams.pcmConverter.stdout.destroy();
this.emit('debug', 'stream kill part 2/5 pass'); 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) { if (streams.pcmConverter && streams.pcmConverter.kill) {
streams.pcmConverter.kill('SIGINT'); streams.pcmConverter.kill('SIGINT');
this.emit('debug', 'stream kill part 3/5 pass'); this.emit('debug', 'stream kill part 3/5 pass');
} }
if (streams.inputStream.unpipe) { if (streams.pcmConverter.stdin) {
streams.inputStream.unpipe(streams.pcmConverter.stdin); streams.pcmConverter.stdin.end();
this.emit('debug', 'stream kill part 4/5 pass'); this.emit('debug', 'stream kill part 1/5 pass');
} }
if (streams.inputStream.destroy) { if (streams.inputStream.destroy) {
streams.inputStream.destroy(); streams.inputStream.destroy();

View File

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

View File

@@ -194,10 +194,10 @@ class Guild {
_memberSpeakUpdate(user, speaking) { _memberSpeakUpdate(user, speaking) {
const member = this.members.get(user); const member = this.members.get(user);
if (member) { if (member && member.speaking !== speaking) {
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() msg.channel.guild.channels.get(chan).join()
.then(conn => { .then(conn => {
msg.reply('done'); 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); conn.player.on('debug', console.log);
}) })
.catch(console.log); .catch(console.log);