mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 04:53:30 +01:00
"yeah we need voice broadcasts cause we make big big music bots" no stop
This commit is contained in:
38
src/client/voice/dispatcher/BroadcastDispatcher.js
Normal file
38
src/client/voice/dispatcher/BroadcastDispatcher.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const Collection = require('../../../util/Collection');
|
||||
const StreamDispatcher = require('./StreamDispatcher');
|
||||
|
||||
/**
|
||||
* The class that sends voice packet data to the voice connection.
|
||||
* @implements {VolumeInterface}
|
||||
*/
|
||||
class BroadcastDispatcher extends StreamDispatcher {
|
||||
constructor(player, options, streams) {
|
||||
super(player, options, streams);
|
||||
this.broadcast = player.broadcast;
|
||||
}
|
||||
|
||||
_write(chunk, enc, done) {
|
||||
if (!this.startTime) this.startTime = Date.now();
|
||||
for (const dispatcher of this.broadcast.dispatchers) {
|
||||
dispatcher._write(chunk, enc);
|
||||
}
|
||||
this._step(done);
|
||||
}
|
||||
|
||||
_destroy(err, cb) {
|
||||
if (this.player.dispatcher === this) this.player.dispatcher = null;
|
||||
const { streams } = this;
|
||||
if (streams.opus) streams.opus.unpipe(this);
|
||||
if (streams.ffmpeg) streams.ffmpeg.destroy();
|
||||
super._destroy(err, cb);
|
||||
}
|
||||
|
||||
setBitrate(value) {
|
||||
if (!value || !this.streams.opus || !this.streams.opus.setBitrate) return false;
|
||||
const bitrate = value === 'auto' ? 48 : value;
|
||||
this.streams.opus.setBitrate(bitrate * 1000);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BroadcastDispatcher;
|
||||
@@ -47,6 +47,12 @@ class StreamDispatcher extends Writable {
|
||||
this.pausedSince = null;
|
||||
this._writeCallback = null;
|
||||
|
||||
/**
|
||||
* The broadcast controlling this dispatcher, if any
|
||||
* @type {?VoiceBroadcast}
|
||||
*/
|
||||
this.broadcast = this.streams.broadcast;
|
||||
|
||||
this._pausedTime = 0;
|
||||
this.count = 0;
|
||||
|
||||
@@ -165,8 +171,10 @@ class StreamDispatcher extends Writable {
|
||||
this._writeCallback = done;
|
||||
return;
|
||||
}
|
||||
const next = FRAME_LENGTH + (this.count * FRAME_LENGTH) - (Date.now() - this.startTime - this.pausedTime);
|
||||
setTimeout(done.bind(this), next);
|
||||
if (!this.streams.broadcast) {
|
||||
const next = FRAME_LENGTH + (this.count * FRAME_LENGTH) - (Date.now() - this.startTime - this.pausedTime);
|
||||
setTimeout(done.bind(this), next);
|
||||
}
|
||||
if (this._sdata.sequence === (2 ** 16) - 1) this._sdata.sequence = -1;
|
||||
if (this._sdata.timestamp === (2 ** 32) - 1) this._sdata.timestamp = -TIMESTAMP_INC;
|
||||
this._sdata.sequence++;
|
||||
@@ -218,6 +226,7 @@ class StreamDispatcher extends Writable {
|
||||
if (this.speaking === value) return;
|
||||
if (this.player.voiceConnection.status !== VoiceStatus.CONNECTED) return;
|
||||
this.speaking = value;
|
||||
this.player.voiceConnection.setSpeaking(value);
|
||||
/**
|
||||
* Emitted when the dispatcher starts/stops speaking.
|
||||
* @event StreamDispatcher#speaking
|
||||
|
||||
Reference in New Issue
Block a user