DiscordBufferedVoiceDataSource

open class DiscordBufferedVoiceDataSource : DiscordVoiceDataSource

DiscordBufferedVoiceDataSource is generic data source around a pipe. The only condition is that it expects raw PCM 16-bit-le out of the pipe.

It buffers ~5 minutes worth of voice data

Usage:

func client(_ client: DiscordClient, needsDataSourceForEngine engine: DiscordVoiceEngine) throws -> DiscordVoiceDataSource {
    return DiscordBufferedVoiceDataSource(opusEncoder: try DiscordOpusEncoder(bitrate: 128_000))
    // Somewhere down the line we setup some middleware which will use this source.
}
  • The max number of voice packets to buffer. Roughly equal to (nPackets * 20ms) / 1000 = seconds to buffer.

    Declaration

    Swift

    public let bufferSize: Int
  • The number of packets that must be in the buffer before we start reading more into the buffer.

    Declaration

    Swift

    public let drainThreshold: Int
  • The queue for this encoder.

    Declaration

    Swift

    public let encoderQueue = DispatchQueue(label: "discordVoiceEncoder.encoderQueue")
  • The Opus encoder.

    Declaration

    Swift

    public let opusEncoder: DiscordOpusEncoder
  • The size of a frame in samples per channel. Needed to calculate the maximum size of a frame.

    Declaration

    Swift

    public var frameSize = 960
  • A middleware process that spits out raw PCM for the encoder.

    Declaration

    Swift

    public var middleware: DiscordEncoderMiddleware?
  • The DispatchIO source for this buffered source.

    Declaration

    Swift

    public var source: DispatchIO!
  • What the encoder reads from, and what a consumer writes to to have things encoded into Opus

    Declaration

    Swift

    public private(set) var pipe: Pipe
  • A file handle that can be used to write to the encoder.

    Declaration

    Swift

    public var writeToHandler: FileHandle

    Return Value

    A file handle that can be written to.

  • Sets up a raw encoder. This contains no FFmpeg middleware, so you must write raw PCM data to the encoder.

    Declaration

    Swift

    public init(opusEncoder: DiscordOpusEncoder, bufferSize: Int = 15_000, drainThreshold: Int = 13_500)

    Parameters

    opusEncoder

    The Opus encoder to use.

    bufferSize

    The max number of voice packets to buffer.

    drainThreshold

    The number of packets that must be in the buffer before we start reading more into the buffer.

  • Abrubtly halts encoding and kills the encoder

    Declaration

    Swift

    open func closeEncoder()
  • Called when it is time to setup a DispatchIO for reading.

    Override to attach a custom file descriptor. By default it uses an internal pipe.

    Declaration

    Swift

    open func createDispatchIO()
  • Called when the engine needs voice data. If there is no more data left, a DiscordVoiceEngineDataSourceStatus.done error should be thrown.

    Declaration

    Swift

    open func engineNeedsData(_ engine: DiscordVoiceEngine) throws -> [UInt8]

    Parameters

    engine

    The voice engine that needs data.

    Return Value

    An array of Opus encoded bytes.

  • Call only when you know you’ve finished writing data, but ffmpeg is still encoding, or has data we haven’t read This should cause ffmpeg to get an EOF on input, which will cause it to close once its output buffer is empty

    Declaration

    Swift

    open func finishUpAndClose()
  • Starts listening to the writePipe.

    Declaration

    Swift

    open func startReading()