mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +01:00
StreamDispatcher documentation
This commit is contained in:
@@ -19,7 +19,15 @@ nonce.fill(0);
|
|||||||
class StreamDispatcher extends EventEmitter {
|
class StreamDispatcher extends EventEmitter {
|
||||||
constructor(player, stream, streamOptions) {
|
constructor(player, stream, streamOptions) {
|
||||||
super();
|
super();
|
||||||
|
/**
|
||||||
|
* The Audio Player that controls this dispatcher
|
||||||
|
* @type {AudioPlayer}
|
||||||
|
*/
|
||||||
this.player = player;
|
this.player = player;
|
||||||
|
/**
|
||||||
|
* The stream that the dispatcher plays
|
||||||
|
* @type {ReadableStream|VoiceBroadcast}
|
||||||
|
*/
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
if (!(this.stream instanceof VoiceBroadcast)) this.startStreaming();
|
if (!(this.stream instanceof VoiceBroadcast)) this.startStreaming();
|
||||||
this.streamOptions = streamOptions;
|
this.streamOptions = streamOptions;
|
||||||
@@ -34,16 +42,28 @@ class StreamDispatcher extends EventEmitter {
|
|||||||
* @type {boolean}
|
* @type {boolean}
|
||||||
*/
|
*/
|
||||||
this.paused = false;
|
this.paused = false;
|
||||||
|
/**
|
||||||
|
* Whether this dispatcher has been destroyed
|
||||||
|
* @type {boolean}
|
||||||
|
*/
|
||||||
this.destroyed = false;
|
this.destroyed = false;
|
||||||
|
|
||||||
this.setVolume(streamOptions.volume || 1);
|
this.setVolume(streamOptions.volume || 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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}
|
||||||
|
*/
|
||||||
get passes() {
|
get passes() {
|
||||||
return this.streamOptions.passes || 1;
|
return this.streamOptions.passes || 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set passes(n) {
|
||||||
|
this.streamOptions.passes = n;
|
||||||
|
}
|
||||||
|
|
||||||
get streamingData() {
|
get streamingData() {
|
||||||
return this.player.streamingData;
|
return this.player.streamingData;
|
||||||
}
|
}
|
||||||
@@ -80,6 +100,12 @@ class StreamDispatcher extends EventEmitter {
|
|||||||
* @param {number} volume The volume that you want to set
|
* @param {number} volume The volume that you want to set
|
||||||
*/
|
*/
|
||||||
setVolume(volume) {
|
setVolume(volume) {
|
||||||
|
/**
|
||||||
|
* Emitted when the volume of this dispatcher changes
|
||||||
|
* @param {number} oldVolume the old volume
|
||||||
|
* @param {number} newVolume the new volume
|
||||||
|
* @event StreamDispatcher#volumeChange
|
||||||
|
*/
|
||||||
this.emit('volumeChange', this.streamOptions.volume, volume);
|
this.emit('volumeChange', this.streamOptions.volume, volume);
|
||||||
this.streamOptions.volume = volume;
|
this.streamOptions.volume = volume;
|
||||||
}
|
}
|
||||||
@@ -134,6 +160,11 @@ class StreamDispatcher extends EventEmitter {
|
|||||||
opusPacket = opusPacket || this.player.opusEncoder.encode(buffer);
|
opusPacket = opusPacket || this.player.opusEncoder.encode(buffer);
|
||||||
let repeats = this.passes;
|
let repeats = this.passes;
|
||||||
const packet = this.createPacket(sequence, timestamp, opusPacket);
|
const packet = this.createPacket(sequence, timestamp, opusPacket);
|
||||||
|
/**
|
||||||
|
* Emitted whenever the dispatcher has debug information
|
||||||
|
* @event StreamDispatcher#debug
|
||||||
|
* @param {string} info the debug info
|
||||||
|
*/
|
||||||
while (repeats--) {
|
while (repeats--) {
|
||||||
this.player.voiceConnection.sockets.udp.send(packet)
|
this.player.voiceConnection.sockets.udp.send(packet)
|
||||||
.catch(e => this.emit('debug', `Failed to send a packet ${e}`));
|
.catch(e => this.emit('debug', `Failed to send a packet ${e}`));
|
||||||
@@ -258,11 +289,20 @@ class StreamDispatcher extends EventEmitter {
|
|||||||
this.destroyed = true;
|
this.destroyed = true;
|
||||||
this.setSpeaking(false);
|
this.setSpeaking(false);
|
||||||
this.emit(type, reason);
|
this.emit(type, reason);
|
||||||
|
/**
|
||||||
|
* Emitted once the dispatcher ends
|
||||||
|
* @param {string} [reason] the reason the dispatcher ended
|
||||||
|
* @event StreamDispatcher#end
|
||||||
|
*/
|
||||||
if (type !== 'end') this.emit('end', `destroyed due to ${type} - ${reason}`);
|
if (type !== 'end') this.emit('end', `destroyed due to ${type} - ${reason}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
startStreaming() {
|
startStreaming() {
|
||||||
if (!this.stream) {
|
if (!this.stream) {
|
||||||
|
/**
|
||||||
|
* Emitted if the dispatcher encounters an error
|
||||||
|
* @param {string} error the error message
|
||||||
|
*/
|
||||||
this.emit('error', 'No stream');
|
this.emit('error', 'No stream');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user