mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Add passes to streaming voice
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -205,6 +205,7 @@ class VoiceConnection extends EventEmitter {
|
||||
* @typedef {Object} StreamOptions
|
||||
* @property {number} [seek=0] The time to seek to
|
||||
* @property {number} [volume=1] The volume to play at
|
||||
* @property {number} [passes=1] How many times to send the voice packet to reduce packet loss
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -220,8 +221,8 @@ class VoiceConnection extends EventEmitter {
|
||||
* })
|
||||
* .catch(console.log);
|
||||
*/
|
||||
playFile(file, { seek = 0, volume = 1 } = {}) {
|
||||
const options = { seek: seek, volume: volume };
|
||||
playFile(file, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||
const options = { seek, volume, passes };
|
||||
return this.player.playFile(file, options);
|
||||
}
|
||||
|
||||
@@ -241,8 +242,8 @@ class VoiceConnection extends EventEmitter {
|
||||
* })
|
||||
* .catch(console.log);
|
||||
*/
|
||||
playStream(stream, { seek = 0, volume = 1 } = {}) {
|
||||
const options = { seek: seek, volume: volume };
|
||||
playStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||
const options = { seek, volume, passes };
|
||||
return this.player.playStream(stream, options);
|
||||
}
|
||||
|
||||
@@ -252,8 +253,8 @@ class VoiceConnection extends EventEmitter {
|
||||
* @param {StreamOptions} [options] Options for playing the stream
|
||||
* @returns {StreamDispatcher}
|
||||
*/
|
||||
playConvertedStream(stream, { seek = 0, volume = 1 } = {}) {
|
||||
const options = { seek: seek, volume: volume };
|
||||
playConvertedStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||
const options = { seek, volume, passes };
|
||||
this._shutdown();
|
||||
const dispatcher = this.player.playPCMStream(stream, options);
|
||||
return dispatcher;
|
||||
|
||||
@@ -32,6 +32,12 @@ class StreamDispatcher extends EventEmitter {
|
||||
this._startStreaming();
|
||||
this._triggered = false;
|
||||
this._volume = streamOptions.volume;
|
||||
/**
|
||||
* How many passes the dispatcher should take when sending packets to reduce packet loss. Values over 5
|
||||
* aren't recommended, as it means you are using 5x more bandwidth. You _can_ edit this at runtime.
|
||||
* @type {number}
|
||||
*/
|
||||
this.passes = streamOptions.passes || 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,9 +51,11 @@ class StreamDispatcher extends EventEmitter {
|
||||
}
|
||||
|
||||
_sendBuffer(buffer, sequence, timestamp) {
|
||||
this.player.connection.udp.send(
|
||||
this._createPacket(sequence, timestamp, this.player.opusEncoder.encode(buffer))
|
||||
);
|
||||
let repeats = this.passes;
|
||||
const packet = this._createPacket(sequence, timestamp, this.player.opusEncoder.encode(buffer));
|
||||
while (repeats--) {
|
||||
this.player.connection.udp.send(packet);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,8 +24,8 @@ class VoiceConnectionPlayer extends EventEmitter {
|
||||
};
|
||||
}
|
||||
|
||||
convertStream(stream, { seek = 0, volume = 1 } = {}) {
|
||||
const options = { seek: seek, volume: volume };
|
||||
convertStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||
const options = { seek, volume, passes };
|
||||
const encoder = this.converterEngine.createConvertStream(options.seek);
|
||||
const pipe = stream.pipe(encoder.stdin, { end: false });
|
||||
pipe.on('unpipe', () => {
|
||||
@@ -95,8 +95,8 @@ class VoiceConnectionPlayer extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
playPCMStream(pcmStream, { seek = 0, volume = 1 } = {}) {
|
||||
const options = { seek: seek, volume: volume };
|
||||
playPCMStream(pcmStream, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||
const options = { seek, volume, passes };
|
||||
const dispatcher = new StreamDispatcher(this, pcmStream, this._streamingData, options);
|
||||
dispatcher.on('speaking', value => this.setSpeaking(value));
|
||||
dispatcher.on('end', () => this.killStream(pcmStream));
|
||||
|
||||
@@ -7,9 +7,9 @@ class DefaultPlayer extends BasePlayer {
|
||||
return this.playStream(fs.createReadStream(file), options);
|
||||
}
|
||||
|
||||
playStream(stream, { seek = 0, volume = 1 } = {}) {
|
||||
playStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||
this._shutdown();
|
||||
const options = { seek: seek, volume: volume };
|
||||
const options = { seek, volume, passes };
|
||||
const pcmStream = this.convertStream(stream, options);
|
||||
const dispatcher = this.playPCMStream(pcmStream, options);
|
||||
return dispatcher;
|
||||
|
||||
@@ -138,7 +138,7 @@ client.on('message', msg => {
|
||||
msg.channel.guild.channels.get(chan).join()
|
||||
.then(conn => {
|
||||
msg.reply('done');
|
||||
disp = conn.player.playStream(ytdl('https://www.youtube.com/watch?v=dc-nyGo0aC8', {filter : 'audioonly'}));
|
||||
disp = conn.player.playStream(ytdl('https://www.youtube.com/watch?v=oQBiPwklN_Q', {filter : 'audioonly'}), { passes : 3 });
|
||||
conn.player.on('debug', console.log);
|
||||
conn.player.on('error', err => console.log(123, err));
|
||||
disp.on('error', err => console.log(123, err));
|
||||
|
||||
Reference in New Issue
Block a user