mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 18:13:29 +01:00
playing stream
This commit is contained in:
@@ -26,13 +26,48 @@ var AudioEncoder = (function () {
|
||||
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) {
|
||||
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", ["-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"]);
|
||||
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 () {
|
||||
callback(null, {
|
||||
|
||||
@@ -57,7 +57,10 @@ var VoiceConnection = (function (_EventEmitter) {
|
||||
VoiceConnection.prototype.stopPlaying = function stopPlaying() {
|
||||
this.playing = false;
|
||||
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) {
|
||||
@@ -80,14 +83,15 @@ var VoiceConnection = (function (_EventEmitter) {
|
||||
self.playingIntent = retStream;
|
||||
|
||||
function send() {
|
||||
|
||||
if (!self.playingIntent || !self.playing) {
|
||||
self.setSpeaking(false);
|
||||
retStream.emit("end");
|
||||
self;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
var buffer = stream.read(1920);
|
||||
|
||||
if (!buffer) {
|
||||
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
|
||||
return;
|
||||
@@ -162,7 +166,7 @@ var VoiceConnection = (function (_EventEmitter) {
|
||||
if (!self.encoder.opus) {
|
||||
self.playing = false;
|
||||
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;
|
||||
}
|
||||
var buffer = self.encoder.opusBuffer(rawbuffer);
|
||||
@@ -199,6 +203,31 @@ var VoiceConnection = (function (_EventEmitter) {
|
||||
function error() {
|
||||
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);
|
||||
callback(e);
|
||||
}
|
||||
@@ -206,12 +235,12 @@ var VoiceConnection = (function (_EventEmitter) {
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.init = function init() {
|
||||
var _this2 = this;
|
||||
var _this3 = this;
|
||||
|
||||
var self = this;
|
||||
dns.lookup(this.endpoint, function (err, address, family) {
|
||||
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 firstPacket = true;
|
||||
|
||||
Reference in New Issue
Block a user