playing stream

This commit is contained in:
hydrabolt
2015-11-08 12:34:59 +00:00
parent a1e92dff5c
commit 30f02e77c4
5 changed files with 171 additions and 26 deletions

View File

@@ -26,13 +26,48 @@ var AudioEncoder = (function () {
return this.opus.encode(buffer, 1920); return this.opus.encode(buffer, 1920);
}; };
AudioEncoder.prototype.encodeStream = function encodeStream(stream) {
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1];
var self = this;
return new Promise(function (resolve, reject) {
var enc = cpoc.spawn("ffmpeg", ["-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", "-i", "-"]);
stream.pipe(enc.stdin);
enc.stdout.once("readable", function () {
callback(null, {
proc: enc,
stream: enc.stdout,
instream: stream
});
resolve({
proc: enc,
stream: enc.stdout,
instream: stream
});
});
enc.stdout.on("end", function () {
callback("end");
reject("end");
});
enc.stdout.on("close", function () {
callback("close");
reject("close");
});
});
};
AudioEncoder.prototype.encodeFile = function encodeFile(file) { AudioEncoder.prototype.encodeFile = function encodeFile(file) {
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1]; var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1];
var self = this; var self = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
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. var enc = cpoc.spawn("ffmpeg", ["-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"]); "-af", "volume=1", "pipe:1", "-i", file]);
enc.stdout.once("readable", function () { enc.stdout.once("readable", function () {
callback(null, { callback(null, {

View File

@@ -57,7 +57,10 @@ var VoiceConnection = (function (_EventEmitter) {
VoiceConnection.prototype.stopPlaying = function stopPlaying() { VoiceConnection.prototype.stopPlaying = function stopPlaying() {
this.playing = false; this.playing = false;
this.playingIntent = null; this.playingIntent = null;
if (this.streamProc) this.streamProc.kill(); if (this.instream) {
console.log(this.instream);
this.instream.unpipe(this.streamProc);
}if (this.streamProc) this.streamProc.destroy();
}; };
VoiceConnection.prototype.playRawStream = function playRawStream(stream) { VoiceConnection.prototype.playRawStream = function playRawStream(stream) {
@@ -80,14 +83,15 @@ var VoiceConnection = (function (_EventEmitter) {
self.playingIntent = retStream; self.playingIntent = retStream;
function send() { function send() {
if (!self.playingIntent || !self.playing) { if (!self.playingIntent || !self.playing) {
self.setSpeaking(false); self.setSpeaking(false);
retStream.emit("end"); retStream.emit("end");
self;
return; return;
} }
try { try {
var buffer = stream.read(1920); var buffer = stream.read(1920);
if (!buffer) { if (!buffer) {
setTimeout(send, length * 10); // give chance for some data in 200ms to appear setTimeout(send, length * 10); // give chance for some data in 200ms to appear
return; return;
@@ -162,7 +166,7 @@ var VoiceConnection = (function (_EventEmitter) {
if (!self.encoder.opus) { if (!self.encoder.opus) {
self.playing = false; self.playing = false;
self.emit("error", "No Opus!"); self.emit("error", "No Opus!");
self.emit("debug", "Tried to use node-opus, but opus not available - install it!"); self.client.emit("debug", "Tried to use node-opus, but opus not available - install it!");
return; return;
} }
var buffer = self.encoder.opusBuffer(rawbuffer); var buffer = self.encoder.opusBuffer(rawbuffer);
@@ -199,6 +203,31 @@ var VoiceConnection = (function (_EventEmitter) {
function error() { function error() {
var e = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; var e = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
console.log(e);
reject(e);
callback(e);
}
});
};
VoiceConnection.prototype.playStream = function playStream(stream) {
var _this2 = this;
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, str) {} : arguments[1];
var self = this;
return new Promise(function (resolve, reject) {
_this2.encoder.encodeStream(stream)["catch"](error).then(function (data) {
self.streamProc = data.proc;
self.instream = data.instream;
var intent = self.playRawStream(data.stream);
resolve(intent);
callback(null, intent);
});
function error() {
var e = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
console.log(e);
reject(e); reject(e);
callback(e); callback(e);
} }
@@ -206,12 +235,12 @@ var VoiceConnection = (function (_EventEmitter) {
}; };
VoiceConnection.prototype.init = function init() { VoiceConnection.prototype.init = function init() {
var _this2 = this; var _this3 = this;
var self = this; var self = this;
dns.lookup(this.endpoint, function (err, address, family) { dns.lookup(this.endpoint, function (err, address, family) {
self.endpoint = address; self.endpoint = address;
var vWS = self.vWS = new WebSocket("wss://" + _this2.endpoint, null, { rejectUnauthorized: false }); var vWS = self.vWS = new WebSocket("wss://" + _this3.endpoint, null, { rejectUnauthorized: false });
var udpClient = self.udp = udp.createSocket("udp4"); var udpClient = self.udp = udp.createSocket("udp4");
var firstPacket = true; var firstPacket = true;

View File

@@ -23,16 +23,55 @@ class AudioEncoder{
} }
encodeFile(file, callback=function(err, buffer){}){ encodeStream(stream, callback=function(err, buffer){}){
var self = this; var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var enc = cpoc.spawn("ffmpeg" , [ var enc = cpoc.spawn("ffmpeg" , [
"-i", file,
"-f", "s16le", "-f", "s16le",
"-ar", "48000", "-ar", "48000",
"-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth.
"-af", "volume=1", "-af", "volume=1",
"pipe:1" "pipe:1",
"-i", "-"
]);
stream.pipe(enc.stdin);
enc.stdout.once("readable", function() {
callback(null, {
proc : enc,
stream : enc.stdout,
instream : stream
});
resolve({
proc : enc,
stream : enc.stdout,
instream : stream
});
});
enc.stdout.on("end", function() {
callback("end");
reject("end");
});
enc.stdout.on("close", function() {
callback("close");
reject("close");
});
});
}
encodeFile(file, callback=function(err, buffer){}){
var self = this;
return new Promise((resolve, reject) => {
var enc = cpoc.spawn("ffmpeg" , [
"-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",
"-i", file
]); ]);
enc.stdout.once("readable", function() { enc.stdout.once("readable", function() {
@@ -45,7 +84,7 @@ class AudioEncoder{
stream : enc.stdout stream : enc.stdout
}); });
}); });
enc.stdout.on("end", function() { enc.stdout.on("end", function() {
callback("end"); callback("end");
reject("end"); reject("end");

View File

@@ -50,8 +50,11 @@ class VoiceConnection extends EventEmitter {
stopPlaying() { stopPlaying() {
this.playing = false; this.playing = false;
this.playingIntent = null; this.playingIntent = null;
if (this.streamProc) if(this.instream){
this.streamProc.kill(); console.log(this.instream);
this.instream.unpipe(this.streamProc);
}if (this.streamProc)
this.streamProc.destroy();
} }
playRawStream(stream) { playRawStream(stream) {
@@ -74,14 +77,15 @@ class VoiceConnection extends EventEmitter {
self.playingIntent = retStream; self.playingIntent = retStream;
function send() { function send() {
if (!self.playingIntent || !self.playing) { if (!self.playingIntent || !self.playing) {
self.setSpeaking(false); self.setSpeaking(false);
retStream.emit("end"); retStream.emit("end");
self
return; return;
} }
try { try {
var buffer = stream.read(1920); var buffer = stream.read(1920);
if (!buffer) { if (!buffer) {
setTimeout(send, length * 10); // give chance for some data in 200ms to appear setTimeout(send, length * 10); // give chance for some data in 200ms to appear
return; return;
@@ -159,7 +163,7 @@ class VoiceConnection extends EventEmitter {
if(!self.encoder.opus){ if(!self.encoder.opus){
self.playing=false; self.playing=false;
self.emit("error", "No Opus!"); self.emit("error", "No Opus!");
self.emit("debug", "Tried to use node-opus, but opus not available - install it!"); self.client.emit("debug", "Tried to use node-opus, but opus not available - install it!");
return; return;
} }
var buffer = self.encoder.opusBuffer(rawbuffer); var buffer = self.encoder.opusBuffer(rawbuffer);
@@ -196,6 +200,29 @@ class VoiceConnection extends EventEmitter {
}); });
function error(e = true) { function error(e = true) {
console.log(e);
reject(e);
callback(e);
}
})
}
playStream(stream, callback = function (err, str) { }) {
var self = this;
return new Promise((resolve, reject) => {
this.encoder
.encodeStream(stream)
.catch(error)
.then(data => {
self.streamProc = data.proc;
self.instream = data.instream;
var intent = self.playRawStream(data.stream);
resolve(intent);
callback(null, intent);
});
function error(e = true) {
console.log(e);
reject(e); reject(e);
callback(e); callback(e);
} }

View File

@@ -1,40 +1,55 @@
var Discord = require("../"); var Discord = require("../");
var client = new Discord.Client(); var client = new Discord.Client();
client.on("debug", (m) => console.log("[debug]",m)); var request = require("superagent");
client.on("debug", (m) => console.log("[debug]", m));
client.on("warn", (m) => console.log("[warn]", m)); client.on("warn", (m) => console.log("[warn]", m));
var start = Date.now(); var start = Date.now();
client.on("message", m => { client.on("message", m => {
if(m.content === "&init"){ if (m.content === "&init") {
for(var channel of m.channel.server.channels){ for (var channel of m.channel.server.channels) {
if(channel instanceof Discord.VoiceChannel){ if (channel instanceof Discord.VoiceChannel) {
client.joinVoiceChannel(channel).catch(error); client.joinVoiceChannel(channel).catch(error);
break; break;
} }
} }
} }
if(m.content.startsWith("$$$ stop")){ if (m.content.startsWith("$$$ stop")) {
if(client.internal.voiceConnection){ if (client.internal.voiceConnection) {
client.internal.voiceConnection.stopPlaying(); client.internal.voiceConnection.stopPlaying();
} }
return; return;
} }
if(m.content.startsWith("$$$")){ if (m.content.startsWith("$$$")) {
var chan; var chan;
var rest = m.content.split(" "); var rest = m.content.split(" ");
rest.splice(0, 1); rest.splice(0, 1);
rest = rest.join(" "); rest = rest.join(" ");
if(client.internal.voiceConnection){ if (client.internal.voiceConnection) {
client.reply(m, "ok, I'll play that for you"); client.reply(m, "ok, I'll play that for you");
var connection = client.internal.voiceConnection; var connection = client.internal.voiceConnection;
connection.playFile("C:/users/amish/desktop/"+rest); connection.playFile("C:/users/amish/desktop/" + rest);
}
} if (m.content.startsWith("$pipebitch")) {
var chan;
var rest = m.content.split(" ");
rest.splice(0, 1);
rest = rest.join(" ");
if (client.internal.voiceConnection) {
client.reply(m, "ok, I'll play that for you " + rest);
var connection = client.internal.voiceConnection;
var request = require("request");
connection.playStream(request(rest));
} }
} }
}); });
function error(e){ function error(e) {
console.log(e.stack); console.log(e.stack);
process.exit(0); process.exit(0);
} }
client.login(process.env["discordEmail"], process.env["discordPass"]).catch((e)=>console.log(e)); client.login(process.env["discordEmail"], process.env["discordPass"]).catch((e) => console.log(e));