buddy

node MVC discord bot
Log | Files | Refs | README

BroadcastDispatcher.js (1388B)


      1 'use strict';
      2 
      3 const StreamDispatcher = require('./StreamDispatcher');
      4 
      5 /**
      6  * The class that sends voice packet data to the voice connection.
      7  * @implements {VolumeInterface}
      8  * @extends {StreamDispatcher}
      9  */
     10 class BroadcastDispatcher extends StreamDispatcher {
     11   constructor(player, options, streams) {
     12     super(player, options, streams);
     13     this.broadcast = player.broadcast;
     14   }
     15 
     16   _write(chunk, enc, done) {
     17     if (!this.startTime) this.startTime = Date.now();
     18     for (const dispatcher of this.broadcast.subscribers) {
     19       dispatcher._write(chunk, enc);
     20     }
     21     this._step(done);
     22   }
     23 
     24   _destroy(err, cb) {
     25     if (this.player.dispatcher === this) this.player.dispatcher = null;
     26     const { streams } = this;
     27     if (streams.opus) streams.opus.unpipe(this);
     28     if (streams.ffmpeg) streams.ffmpeg.destroy();
     29     super._destroy(err, cb);
     30   }
     31 
     32   /**
     33    * Set the bitrate of the current Opus encoder if using a compatible Opus stream.
     34    * @param {number} value New bitrate, in kbps
     35    * If set to 'auto', 48kbps will be used
     36    * @returns {boolean} true if the bitrate has been successfully changed.
     37    */
     38   setBitrate(value) {
     39     if (!value || !this.streams.opus || !this.streams.opus.setBitrate) return false;
     40     const bitrate = value === 'auto' ? 48 : value;
     41     this.streams.opus.setBitrate(bitrate * 1000);
     42     return true;
     43   }
     44 }
     45 
     46 module.exports = BroadcastDispatcher;