From d09c26c7c1b1eaebdd6bed9d27649a5ca36c8e57 Mon Sep 17 00:00:00 2001 From: meew0 Date: Wed, 23 Dec 2015 23:20:19 +0100 Subject: [PATCH 1/2] Add method to play back the output from an arbitrary ffmpeg cmd --- src/Voice/AudioEncoder.js | 33 +++++++++++++++++++++++++++++++++ src/Voice/VoiceConnection.js | 27 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/Voice/AudioEncoder.js b/src/Voice/AudioEncoder.js index e9b71bd70..171a3a10e 100644 --- a/src/Voice/AudioEncoder.js +++ b/src/Voice/AudioEncoder.js @@ -107,4 +107,37 @@ export default class AudioEncoder { }); }); } + + encodeArbitraryFFmpeg(ffmpegOptions) { + var self = this; + return new Promise((resolve, reject) => { + // add options discord.js needs + var options = ffmpegOptions.concat([ + '-loglevel', '0', + '-f', 's16le', + '-ar', '48000', + '-ac', 2, + 'pipe:1' + ]); + var enc = cpoc.spawn(self.getCommand(), options, { stdio: ['pipe', 'pipe', 'ignore'] }); + + enc.stdout.once("readable", function () { + resolve({ + proc: enc, + stream: enc.stdout, + channels : 2 + }); + }); + + enc.stdout.on("end", function () { + console.log("end"); + reject("end"); + }); + + enc.stdout.on("close", function () { + console.log("close"); + reject("close"); + }); + }); + } } diff --git a/src/Voice/VoiceConnection.js b/src/Voice/VoiceConnection.js index 04f81629d..9932b39ff 100644 --- a/src/Voice/VoiceConnection.js +++ b/src/Voice/VoiceConnection.js @@ -253,6 +253,33 @@ export default class VoiceConnection extends EventEmitter { }) } + playArbitraryFFmpeg(ffmpegOptions, callback = function (err, str) { }) { + var self = this; + self.stopPlaying(); + if (typeof options === "function") { + // options is the callback + callback = options; + options = {}; + } + return new Promise((resolve, reject) => { + this.encoder + .encodeArbitraryFFmpeg(ffmpegOptions) + .catch(error) + .then(data => { + self.streamProc = data.proc; + self.instream = data.instream; + var intent = self.playStream(data.stream); + resolve(intent); + callback(null, intent); + + }); + function error(e = true) { + reject(e); + callback(e); + } + }) + } + init() { var self = this; console.log("\n\nendpoint:", this.endpoint, "\n\n"); From 14682ba2bf2d28b682503a36e42f35c2e058294a Mon Sep 17 00:00:00 2001 From: meew0 Date: Wed, 23 Dec 2015 23:22:18 +0100 Subject: [PATCH 2/2] Lib output for arbitrary ffmpeg playback --- lib/Voice/AudioEncoder.js | 27 +++++++++++++++++++++++++++ lib/Voice/VoiceConnection.js | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/lib/Voice/AudioEncoder.js b/lib/Voice/AudioEncoder.js index f2048afc0..39c0d6a36 100644 --- a/lib/Voice/AudioEncoder.js +++ b/lib/Voice/AudioEncoder.js @@ -113,6 +113,33 @@ var AudioEncoder = (function () { }); }; + AudioEncoder.prototype.encodeArbitraryFFmpeg = function encodeArbitraryFFmpeg(ffmpegOptions) { + var self = this; + return new Promise(function (resolve, reject) { + // add options discord.js needs + var options = ffmpegOptions.concat(['-loglevel', '0', '-f', 's16le', '-ar', '48000', '-ac', 2, 'pipe:1']); + var enc = _child_process2["default"].spawn(self.getCommand(), options, { stdio: ['pipe', 'pipe', 'ignore'] }); + + enc.stdout.once("readable", function () { + resolve({ + proc: enc, + stream: enc.stdout, + channels: 2 + }); + }); + + enc.stdout.on("end", function () { + console.log("end"); + reject("end"); + }); + + enc.stdout.on("close", function () { + console.log("close"); + reject("close"); + }); + }); + }; + return AudioEncoder; })(); diff --git a/lib/Voice/VoiceConnection.js b/lib/Voice/VoiceConnection.js index 6d53aa209..9e4b55af8 100644 --- a/lib/Voice/VoiceConnection.js +++ b/lib/Voice/VoiceConnection.js @@ -285,14 +285,43 @@ var VoiceConnection = (function (_EventEmitter) { }); }; - VoiceConnection.prototype.init = function init() { + VoiceConnection.prototype.playArbitraryFFmpeg = function playArbitraryFFmpeg(ffmpegOptions) { var _this3 = this; + var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, str) {} : arguments[1]; + + var self = this; + self.stopPlaying(); + if (typeof options === "function") { + // options is the callback + callback = options; + options = {}; + } + return new Promise(function (resolve, reject) { + _this3.encoder.encodeArbitraryFFmpeg(ffmpegOptions)["catch"](error).then(function (data) { + self.streamProc = data.proc; + self.instream = data.instream; + var intent = self.playStream(data.stream); + resolve(intent); + callback(null, intent); + }); + function error() { + var e = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; + + reject(e); + callback(e); + } + }); + }; + + VoiceConnection.prototype.init = function init() { + var _this4 = this; + var self = this; console.log("\n\nendpoint:", this.endpoint, "\n\n"); _dns2["default"].lookup(this.endpoint, function (err, address, family) { - var vWS = self.vWS = new _ws2["default"]("wss://" + _this3.endpoint, null, { rejectUnauthorized: false }); - _this3.endpoint = address; + var vWS = self.vWS = new _ws2["default"]("wss://" + _this4.endpoint, null, { rejectUnauthorized: false }); + _this4.endpoint = address; var udpClient = self.udp = _dgram2["default"].createSocket("udp4"); var firstPacket = true;