mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +01:00
Make voice receiving better
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -56,6 +56,7 @@ class VoiceConnection extends EventEmitter {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this._reject = reject;
|
this._reject = reject;
|
||||||
|
this.ssrcMap = new Map();
|
||||||
this.bindListeners();
|
this.bindListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,6 +144,7 @@ class VoiceConnection extends EventEmitter {
|
|||||||
});
|
});
|
||||||
this.websocket.on('speaking', data => {
|
this.websocket.on('speaking', data => {
|
||||||
const guild = this.channel.guild;
|
const guild = this.channel.guild;
|
||||||
|
this.ssrcMap.set(+data.ssrc, this.manager.client.users.get(data.user_id));
|
||||||
guild._memberSpeakUpdate(data.user_id, data.speaking);
|
guild._memberSpeakUpdate(data.user_id, data.speaking);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,34 +11,64 @@ nonce.fill(0);
|
|||||||
class VoiceReceiver extends EventEmitter {
|
class VoiceReceiver extends EventEmitter {
|
||||||
constructor(connection) {
|
constructor(connection) {
|
||||||
super();
|
super();
|
||||||
|
/*
|
||||||
|
need a queue because we don't get the ssrc of the user speaking until after the first few packets,
|
||||||
|
so we queue up unknown SSRCs until they become known, then empty the queue.
|
||||||
|
*/
|
||||||
|
this.queues = new Map();
|
||||||
/**
|
/**
|
||||||
* The VoiceConnection that instantiated this
|
* The VoiceConnection that instantiated this
|
||||||
* @type {VoiceConnection}
|
* @type {VoiceConnection}
|
||||||
*/
|
*/
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
this.connection.udp.udpSocket.on('message', msg => {
|
this.connection.udp.udpSocket.on('message', msg => {
|
||||||
msg.copy(nonce, 0, 0, 12);
|
const ssrc = +msg.readUInt32BE(8).toString(10);
|
||||||
let data = NaCl.secretbox.open(msg.slice(12), nonce, this.connection.data.secret);
|
const user = this.connection.ssrcMap.get(ssrc);
|
||||||
if (!data) {
|
if (!user) {
|
||||||
return this.emit('warn', 'failed to decrypt voice packet');
|
if (!this.queues.has(ssrc)) {
|
||||||
|
this.queues.set(ssrc, []);
|
||||||
|
}
|
||||||
|
this.queues.get(ssrc).push(msg);
|
||||||
}
|
}
|
||||||
data = new Buffer(data);
|
if (user) {
|
||||||
|
if (this.queues.get(ssrc)) {
|
||||||
|
this.queues.get(ssrc).push(msg);
|
||||||
|
this.queues.get(ssrc).map(m => this.handlePacket(m, user));
|
||||||
|
this.queues.delete(ssrc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
handlePacket(msg, user) {
|
||||||
|
msg.copy(nonce, 0, 0, 12);
|
||||||
|
let data = NaCl.secretbox.open(msg.slice(12), nonce, this.connection.data.secret);
|
||||||
|
if (!data) {
|
||||||
|
/**
|
||||||
|
* Emitted whenever a voice packet cannot be decrypted
|
||||||
|
* @event VoiceReceiver#warn
|
||||||
|
* @param {String} message the warning message
|
||||||
|
*/
|
||||||
|
return this.emit('warn', 'failed to decrypt voice packet');
|
||||||
|
}
|
||||||
|
data = new Buffer(data);
|
||||||
/**
|
/**
|
||||||
* Emitted whenever voice data is received from the voice connection. This is _always_ emitted (unlike PCM).
|
* Emitted whenever voice data is received from the voice connection. This is _always_ emitted (unlike PCM).
|
||||||
* @event VoiceReceiver#opus
|
* @event VoiceReceiver#opus
|
||||||
|
* @param {User} user the user that is sending the buffer (is speaking)
|
||||||
* @param {Buffer} buffer the opus buffer
|
* @param {Buffer} buffer the opus buffer
|
||||||
*/
|
*/
|
||||||
this.emit('opus', data);
|
this.emit('opus', user, data);
|
||||||
if (this.listenerCount('pcm') > 0) {
|
if (this.listenerCount('pcm') > 0) {
|
||||||
/**
|
/**
|
||||||
* Emits decoded voice data when it's received. For performance reasons, the decoding will only
|
* Emits decoded voice data when it's received. For performance reasons, the decoding will only
|
||||||
* happen if there is at least one `pcm` listener on this receiver.
|
* happen if there is at least one `pcm` listener on this receiver.
|
||||||
* @event VoiceReceiver#pcm
|
* @event VoiceReceiver#pcm
|
||||||
|
* @param {User} user the user that is sending the buffer (is speaking)
|
||||||
* @param {Buffer} buffer the decoded buffer
|
* @param {Buffer} buffer the decoded buffer
|
||||||
*/
|
*/
|
||||||
this.emit('pcm', this.connection.player.opusEncoder.decode(data));
|
this.emit('pcm', user, this.connection.player.opusEncoder.decode(data));
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -145,7 +145,9 @@ client.on('message', msg => {
|
|||||||
conn.player.on('debug', console.log);
|
conn.player.on('debug', console.log);
|
||||||
conn.player.on('error', err => console.log(123, err));
|
conn.player.on('error', err => console.log(123, err));
|
||||||
const receiver = conn.createReceiver();
|
const receiver = conn.createReceiver();
|
||||||
receiver.on('pcm', console.log);
|
receiver.on('pcm', u => {
|
||||||
|
console.log(u.username);
|
||||||
|
});
|
||||||
disp.on('error', err => console.log(123, err));
|
disp.on('error', err => console.log(123, err));
|
||||||
})
|
})
|
||||||
.catch(console.log);
|
.catch(console.log);
|
||||||
|
|||||||
Reference in New Issue
Block a user