mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +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
|
* @typedef {Object} StreamOptions
|
||||||
* @property {number} [seek=0] The time to seek to
|
* @property {number} [seek=0] The time to seek to
|
||||||
* @property {number} [volume=1] The volume to play at
|
* @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);
|
* .catch(console.log);
|
||||||
*/
|
*/
|
||||||
playFile(file, { seek = 0, volume = 1 } = {}) {
|
playFile(file, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||||
const options = { seek: seek, volume: volume };
|
const options = { seek, volume, passes };
|
||||||
return this.player.playFile(file, options);
|
return this.player.playFile(file, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,8 +242,8 @@ class VoiceConnection extends EventEmitter {
|
|||||||
* })
|
* })
|
||||||
* .catch(console.log);
|
* .catch(console.log);
|
||||||
*/
|
*/
|
||||||
playStream(stream, { seek = 0, volume = 1 } = {}) {
|
playStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||||
const options = { seek: seek, volume: volume };
|
const options = { seek, volume, passes };
|
||||||
return this.player.playStream(stream, options);
|
return this.player.playStream(stream, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,8 +253,8 @@ class VoiceConnection extends EventEmitter {
|
|||||||
* @param {StreamOptions} [options] Options for playing the stream
|
* @param {StreamOptions} [options] Options for playing the stream
|
||||||
* @returns {StreamDispatcher}
|
* @returns {StreamDispatcher}
|
||||||
*/
|
*/
|
||||||
playConvertedStream(stream, { seek = 0, volume = 1 } = {}) {
|
playConvertedStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||||
const options = { seek: seek, volume: volume };
|
const options = { seek, volume, passes };
|
||||||
this._shutdown();
|
this._shutdown();
|
||||||
const dispatcher = this.player.playPCMStream(stream, options);
|
const dispatcher = this.player.playPCMStream(stream, options);
|
||||||
return dispatcher;
|
return dispatcher;
|
||||||
|
|||||||
@@ -32,6 +32,12 @@ class StreamDispatcher extends EventEmitter {
|
|||||||
this._startStreaming();
|
this._startStreaming();
|
||||||
this._triggered = false;
|
this._triggered = false;
|
||||||
this._volume = streamOptions.volume;
|
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) {
|
_sendBuffer(buffer, sequence, timestamp) {
|
||||||
this.player.connection.udp.send(
|
let repeats = this.passes;
|
||||||
this._createPacket(sequence, timestamp, this.player.opusEncoder.encode(buffer))
|
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 } = {}) {
|
convertStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||||
const options = { seek: seek, volume: volume };
|
const options = { seek, volume, passes };
|
||||||
const encoder = this.converterEngine.createConvertStream(options.seek);
|
const encoder = this.converterEngine.createConvertStream(options.seek);
|
||||||
const pipe = stream.pipe(encoder.stdin, { end: false });
|
const pipe = stream.pipe(encoder.stdin, { end: false });
|
||||||
pipe.on('unpipe', () => {
|
pipe.on('unpipe', () => {
|
||||||
@@ -95,8 +95,8 @@ class VoiceConnectionPlayer extends EventEmitter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
playPCMStream(pcmStream, { seek = 0, volume = 1 } = {}) {
|
playPCMStream(pcmStream, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||||
const options = { seek: seek, volume: volume };
|
const options = { seek, volume, passes };
|
||||||
const dispatcher = new StreamDispatcher(this, pcmStream, this._streamingData, options);
|
const dispatcher = new StreamDispatcher(this, pcmStream, this._streamingData, options);
|
||||||
dispatcher.on('speaking', value => this.setSpeaking(value));
|
dispatcher.on('speaking', value => this.setSpeaking(value));
|
||||||
dispatcher.on('end', () => this.killStream(pcmStream));
|
dispatcher.on('end', () => this.killStream(pcmStream));
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ class DefaultPlayer extends BasePlayer {
|
|||||||
return this.playStream(fs.createReadStream(file), options);
|
return this.playStream(fs.createReadStream(file), options);
|
||||||
}
|
}
|
||||||
|
|
||||||
playStream(stream, { seek = 0, volume = 1 } = {}) {
|
playStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
|
||||||
this._shutdown();
|
this._shutdown();
|
||||||
const options = { seek: seek, volume: volume };
|
const options = { seek, volume, passes };
|
||||||
const pcmStream = this.convertStream(stream, options);
|
const pcmStream = this.convertStream(stream, options);
|
||||||
const dispatcher = this.playPCMStream(pcmStream, options);
|
const dispatcher = this.playPCMStream(pcmStream, options);
|
||||||
return dispatcher;
|
return dispatcher;
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ client.on('message', msg => {
|
|||||||
msg.channel.guild.channels.get(chan).join()
|
msg.channel.guild.channels.get(chan).join()
|
||||||
.then(conn => {
|
.then(conn => {
|
||||||
msg.reply('done');
|
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('debug', console.log);
|
||||||
conn.player.on('error', err => console.log(123, err));
|
conn.player.on('error', err => console.log(123, err));
|
||||||
disp.on('error', err => console.log(123, err));
|
disp.on('error', err => console.log(123, err));
|
||||||
|
|||||||
Reference in New Issue
Block a user