DiscordVoiceEngine
public final class DiscordVoiceEngine : DiscordVoiceEngineSpec
A subclass of DiscordEngine
that provides functionality for voice communication.
Discord uses encrypted OPUS encoded voice packets. The engine is responsible for encyrptingdecrypting voice packets that are sent and received.
-
The configuration for this engine.
Declaration
Swift
public let config: DiscordVoiceEngineConfiguration
-
The heartbeat queue.
Declaration
Swift
public let heartbeatQueue = DispatchQueue(label: "discordVoiceEngine.heartbeatQueue")
-
The parse queue.
Declaration
Swift
public let parseQueue = DispatchQueue(label: "discordVoiceEngine.parseQueue")
-
The voice url
Declaration
Swift
public var connectURL: String
-
The connect UUID of this WebSocketable.
Declaration
Swift
public var connectUUID = UUID()
-
The type of
DiscordEngine
this is. Used to correctly fire engine events.Declaration
Swift
public var description: String
-
The id of the guild this voice engine is for.
Declaration
Swift
public var guildId: GuildID
-
Creates the handshake object that Discord expects.
Declaration
Swift
public var handshakeObject: [String: Any]
-
Not used in voice gateways
Declaration
Swift
public var resumeObject: [String: Any]
-
The underlying websocket.
Declaration
Swift
public var websocket: WebSocket?
-
The voice engine’s delegate.
Declaration
Swift
public private(set) weak var voiceDelegate: DiscordVoiceEngineDelegate?
-
The heartbeat interval for this engine.
Declaration
Swift
public private(set) var heartbeatInterval = -1
-
The data source for this engine. This source is responsible for giving us Opus data that is ready to send.
Declaration
Swift
public private(set) var source: DiscordVoiceDataSource?
-
The modes that are available for communication. Only xsalsa20_poly1305 is supported currently
Declaration
Swift
public private(set) var modes = [String]()
-
The secret key used for encryption
Declaration
Swift
public private(set) var secret: [UInt8]!
-
Our SSRC
Declaration
Swift
public private(set) var ssrc: UInt32 = 0
-
The UDP socket that is used to send/receive voice data
Declaration
Swift
public private(set) var udpSocket: UDPInternetSocket?
-
Our UDP port
Declaration
Swift
public private(set) var udpPort = -1
-
Information about the voice server we are connected to
Declaration
Swift
public private(set) var voiceServerInformation: DiscordVoiceServerInformation!
-
The voice state for this engine.
Declaration
Swift
public private(set) var voiceState: DiscordVoiceState!
-
Constructs a new VoiceEngine
Declaration
Swift
public init(delegate: DiscordVoiceEngineDelegate, config: DiscordVoiceEngineConfiguration, voiceServerInformation: DiscordVoiceServerInformation, voiceState: DiscordVoiceState, source: DiscordVoiceDataSource?, secret: [UInt8]?)
Parameters
delegate
The client this engine should be associated with
voiceServerInformation
The voice server information
encoder
A DiscordVoiceEncoder that from a previous engine. Send if you are still encoding i.e moved channels
secret
The secret from a previous engine.
-
Disconnects the voice engine.
Declaration
Swift
public func disconnect()
-
Handles a close from the WebSocket.
Declaration
Swift
public func handleClose(reason: Error? = nil)
Parameters
reason
The reason the socket closed.
-
Currently unused in VoiceEngines.
Declaration
Swift
public func handleDispatch(_ payload: DiscordGatewayPayload)
-
Handles the hello event.
Declaration
Swift
public func handleHello(_ payload: DiscordGatewayPayload)
Parameters
payload
The dispatch payload
-
Handles a DiscordGatewayPayload. You shouldn’t need to call this directly.
Override this method if you need to customize payload handling.
Declaration
Swift
public func handleGatewayPayload(_ payload: DiscordGatewayPayload)
Parameters
payload
The payload object
-
Handles the resumed event.
Declaration
Swift
public func handleResumed(_ payload: DiscordGatewayPayload)
Parameters
payload
The payload for the event.
-
Parses a raw message from the WebSocket. This is the entry point for voice events. You shouldn’t call this directly.
Declaration
Swift
public func parseGatewayMessage(_ string: String)
-
Stops encoding and requests a new encoder. The
isReadyToSendVoiceWithEngine
delegate method is called when the new encoder is ready.Declaration
Swift
public func requestNewDataSource()
-
Sends a voice heartbeat to Discord. You shouldn’t need to call this directly.
Declaration
Swift
public func sendHeartbeat()
-
Takes a process that outputs random audio data, and sends it to a hidden FFmpeg process that turns the data into raw PCM.
Example setting up youtube-dl to play music.
youtube = EncoderProcess() youtube.launchPath = "usrlocalbinyoutube-dl" youtube.arguments = ["-f", "bestaudio", "-q", "-o", "-", link] voiceEngine.setupMiddleware(youtube) { print("youtube died") }
Currently only works if using the default
DiscordBufferedVoiceDataSource
Declaration
Swift
public func setupMiddleware(_ middleware: Process, terminationHandler: (() -> ())?)
Parameters
middleware
The process that will output audio data.
terminationHandler
Called when the middleware is done. Does not mean that all encoding is done.
-
Starts the handshake with the Discord voice server. You shouldn’t need to call this directly.
Declaration
Swift
public func startHandshake()
-
Starts the engine’s heartbeat. You should call this method when you know the interval that Discord expects.
Declaration
Swift
public func startHeartbeat(milliseconds: Int)
Parameters
milliseconds
The heartbeat interval