Add VoiceReceiver.destroy()/recreate(), addresses #693

This commit is contained in:
Amish Shah
2016-09-12 17:00:55 +01:00
parent b39d959cdf
commit 95369b0a89
2 changed files with 37 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@@ -25,12 +25,17 @@ class VoiceReceiver extends EventEmitter {
this.queues = new Map();
this.pcmStreams = new Map();
this.opusStreams = new Map();
/**
* Whether or not this receiver has been destroyed.
* @type {boolean}
*/
this.destroyed = false;
/**
* The VoiceConnection that instantiated this
* @type {VoiceConnection}
*/
this.connection = connection;
this.connection.udp.udpSocket.on('message', msg => {
this._listener = (msg => {
const ssrc = +msg.readUInt32BE(8).toString(10);
const user = this.connection.ssrcMap.get(ssrc);
if (!user) {
@@ -45,7 +50,36 @@ class VoiceReceiver extends EventEmitter {
}
this.handlePacket(msg, user);
}
});
}).bind(this);
this.connection.udp.udpSocket.on('message', this._listener);
}
/**
* If this VoiceReceiver has been destroyed, running `recreate()` will recreate the listener.
* This avoids you having to create a new receiver.
* <info>Any streams that you had prior to destroying the receiver will not be recreated.</info>
*/
recreate() {
if (this.destroyed) return;
this.connection.udp.udpSocket.on('message', this._listener);
this.destroyed = false;
return;
}
/**
* Destroy this VoiceReceiver, also ending any streams that it may be controlling.
*/
destroy() {
this.connection.udp.udpSocket.removeListener('message', this._listener);
for (const stream of this.pcmStreams) {
stream[1]._push(null);
this.pcmStreams.delete(stream[0]);
}
for (const stream of this.opusStreams) {
stream[1]._push(null);
this.opusStreams.delete(stream[0]);
}
this.destroyed = true;
}
/**