mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
VoiceReceiver: multiple streams fix (#1132)
* VoiceReceiver: multiple streams fix silly hydar... you can't have one Opus engine instance for every stream * Better creation of opus engine
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const NaCl = require('tweetnacl');
|
const NaCl = require('tweetnacl');
|
||||||
const Readable = require('./VoiceReadable');
|
const Readable = require('./VoiceReadable');
|
||||||
|
const OpusEncoders = require('../opus/OpusEngineList');
|
||||||
|
|
||||||
const nonce = Buffer.alloc(24);
|
const nonce = Buffer.alloc(24);
|
||||||
nonce.fill(0);
|
nonce.fill(0);
|
||||||
@@ -25,6 +26,7 @@ class VoiceReceiver extends EventEmitter {
|
|||||||
this.queues = new Map();
|
this.queues = new Map();
|
||||||
this.pcmStreams = new Map();
|
this.pcmStreams = new Map();
|
||||||
this.opusStreams = new Map();
|
this.opusStreams = new Map();
|
||||||
|
this.opusEncoders = new Map();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether or not this receiver has been destroyed.
|
* Whether or not this receiver has been destroyed.
|
||||||
@@ -74,13 +76,16 @@ class VoiceReceiver extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
destroy() {
|
destroy() {
|
||||||
this.voiceConnection.sockets.udp.socket.removeListener('message', this._listener);
|
this.voiceConnection.sockets.udp.socket.removeListener('message', this._listener);
|
||||||
for (const stream of this.pcmStreams) {
|
for (const [id, stream] of this.pcmStreams) {
|
||||||
stream[1]._push(null);
|
stream._push(null);
|
||||||
this.pcmStreams.delete(stream[0]);
|
this.pcmStreams.delete(id);
|
||||||
}
|
}
|
||||||
for (const stream of this.opusStreams) {
|
for (const [id, stream] of this.opusStreams) {
|
||||||
stream[1]._push(null);
|
stream._push(null);
|
||||||
this.opusStreams.delete(stream[0]);
|
this.opusStreams.delete(id);
|
||||||
|
}
|
||||||
|
for (const [id] of this.opusEncoders) {
|
||||||
|
this.opusEncoders.delete(id);
|
||||||
}
|
}
|
||||||
this.destroyed = true;
|
this.destroyed = true;
|
||||||
}
|
}
|
||||||
@@ -137,6 +142,10 @@ class VoiceReceiver extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
this.emit('opus', user, data);
|
this.emit('opus', user, data);
|
||||||
if (this.listenerCount('pcm') > 0 || this.pcmStreams.size > 0) {
|
if (this.listenerCount('pcm') > 0 || this.pcmStreams.size > 0) {
|
||||||
|
if (!this.opusEncoders.get(user.id)) this.opusEncoders.set(user.id, OpusEncoders.fetch());
|
||||||
|
const pcm = this.opusEncoders.get(user.id).decode(data);
|
||||||
|
if (this.pcmStreams.get(user.id)) this.pcmStreams.get(user.id)._push(pcm);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
@@ -144,8 +153,6 @@ class VoiceReceiver extends EventEmitter {
|
|||||||
* @param {User} user The user that is sending the buffer (is speaking)
|
* @param {User} user The user that is sending the buffer (is speaking)
|
||||||
* @param {Buffer} buffer The decoded buffer
|
* @param {Buffer} buffer The decoded buffer
|
||||||
*/
|
*/
|
||||||
const pcm = this.voiceConnection.player.opusEncoder.decode(data);
|
|
||||||
if (this.pcmStreams.get(user.id)) this.pcmStreams.get(user.id)._push(pcm);
|
|
||||||
this.emit('pcm', user, pcm);
|
this.emit('pcm', user, pcm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user