Fixed memory leaks

This commit is contained in:
hydrabolt
2015-11-08 10:09:49 +00:00
parent 99a63db142
commit 04c3dbedac
7 changed files with 121 additions and 113 deletions

View File

@@ -44,26 +44,21 @@ var InternalClient = (function () {
this.channels = new Cache();
this.servers = new Cache();
this.private_channels = new Cache();
this.voiceConnections = new Cache();
this.voiceConnection = null;
this.resolver = new Resolver(this);
}
//def leaveVoiceChannel
InternalClient.prototype.leaveVoiceChannel = function leaveVoiceChannel(chann) {
InternalClient.prototype.leaveVoiceChannel = function leaveVoiceChannel() {
var self = this;
return new Promise(function (resolve, reject) {
var channel = self.resolver.resolveVoiceChannel(chann);
if (channel) {
if (self.voiceConnections[channel]) {
var chan = self.voiceConnections[channel];
chan.stopPlaying();
self.voiceConnections.remove(chan);
resolve();
}
if (self.voiceConnection) {
self.voiceConnection.destroy();
self.voiceConnection = null;
resolve();
} else {
reject(new Error("voice channel does not exist"));
resolve();
}
});
};
@@ -77,8 +72,7 @@ var InternalClient = (function () {
var channel = self.resolver.resolveVoiceChannel(chann);
if (channel) {
if (!self.voiceConnections.get("id", channel.id)) {
var next = function next() {
var session,
token,
server = channel.server,
@@ -90,16 +84,18 @@ var InternalClient = (function () {
if (data.t === "VOICE_STATE_UPDATE") {
session = data.d.session_id;
fired++;
} else if (data.t === "VOICE_SERVER_UPDATE") {
token = data.d.token;
endpoint = data.d.endpoint;
var chan = self.voiceConnections.add(new VoiceConnection(channel, self.client, session, token, server, endpoint));
fired++;
var chan = self.voiceConnection = new VoiceConnection(channel, self.client, session, token, server, endpoint);
chan.on("ready", resolve);
chan.on("error", reject);
}
if (fired >= 2) {
self.client.emit("debug", "removed temporary voice websocket listeners");
self.websocket.removeListener('message', check);
}
};
@@ -114,9 +110,9 @@ var InternalClient = (function () {
"self_deaf": false
}
});
} else {
reject(new Error("voice channel connection exists"));
}
};
self.leaveVoiceChannel().then(next);
} else {
reject(new Error("voice channel does not exist"));
}

View File

@@ -23,9 +23,10 @@ var AudioEncoder = (function () {
var self = this;
return new Promise(function (resolve, reject) {
var enc = cpoc.spawn("ffmpeg", ["-i", file, "-f", "s16le", "-ar", "48000", "-ac", "1", "-af", "volume=1", "pipe:1"]);
var enc = cpoc.spawn("ffmpeg", ["-i", file, "-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth.
"-af", "volume=1", "pipe:1"]);
enc.stdout.on("readable", function () {
enc.stdout.once("readable", function () {
callback(null, {
proc: enc,
stream: enc.stdout

View File

@@ -1,4 +1,11 @@
"use strict";
/*
Major credit to izy521 who is the creator of
https://github.com/izy521/discord.io,
without his help voice chat in discord.js would not have
been possible!
*/
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -42,9 +49,17 @@ var VoiceConnection = (function (_EventEmitter) {
this.playing = false;
this.streamTime = 0;
this.streamProc = null;
this.KAI = null;
this.init();
}
VoiceConnection.prototype.destroy = function destroy() {
this.stopPlaying();
if (this.KAI) clearInterval(this.KAI);
this.vWS.close();
this.udp.close();
};
VoiceConnection.prototype.stopPlaying = function stopPlaying() {
this.playing = false;
this.playingIntent = null;
@@ -123,7 +138,7 @@ var VoiceConnection = (function (_EventEmitter) {
VoiceConnection.prototype.setSpeaking = function setSpeaking(value) {
this.playing = value;
this.vWS.send(JSON.stringify({
if (this.vWS.readyState === WebSocket.OPEN) this.vWS.send(JSON.stringify({
op: 5,
d: {
speaking: value,
@@ -138,7 +153,7 @@ var VoiceConnection = (function (_EventEmitter) {
var self = this;
self.playing = true;
try {
self.udp.send(packet, 0, packet.length, self.vWSData.port, self.endpoint, callback);
if (self.vWS.readyState === WebSocket.OPEN) self.udp.send(packet, 0, packet.length, self.vWSData.port, self.endpoint, callback);
} catch (e) {
self.playing = false;
callback(e);
@@ -252,11 +267,12 @@ var VoiceConnection = (function (_EventEmitter) {
self.vWSData = data.d;
KAI = setInterval(function () {
vWS.send(JSON.stringify({
if (vWS.readyState === WebSocket.OPEN) vWS.send(JSON.stringify({
op: 3,
d: null
}));
}, data.d.heartbeat_interval);
self.KAI = KAI;
var udpPacket = new Buffer(70);
udpPacket.writeUIntBE(data.d.ssrc, 0, 4);