DiscordPHP Documentation

VoiceClient extends EventEmitter
in package

The Discord voice client.

Tags
since
3.2.0

Table of Contents

DCA_VERSION  = 'DCA1'
The DCA version the client is using.
SILENCE_FRAME  = "\xf8\xff\xfe"
The Opus Silence Frame.
$client  : Socket
The UDP client.
$ssrc  : int
The SSRC value.
$audioApplication  : string
The audio application to encode with.
$bitrate  : int
The bitrate to encode with.
$buffer  : Buffer
Audio Buffer.
$channel  : Channel
The Channel that we are connecting to.
$data  : array<string|int, mixed>
Data from the main WebSocket.
$dca  : string
The DCA binary name that we will use.
$deaf  : bool
Whether we are set as deaf.
$dnsConfig  : string|Config
The Config for DNS Resolver.
$endpoint  : string
The Voice WebSocket endpoint.
$ffmpeg  : string
The FFmpeg binary location.
$frameSize  : int
The size of audio frames, in milliseconds.
$heartbeat  : TimerInterface
The Voice WebSocket heartbeat timer.
$heartbeat_interval  : int
The UDP heartbeat interval.
$heartbeatSeq  : int
The UDP heartbeat sequence.
$logger  : LoggerInterface
The logger.
$loop  : LoopInterface
The ReactPHP event loop.
$mainWebsocket  : WebSocket
The main WebSocket instance.
$mode  : string
The Voice WebSocket mode.
$mute  : bool
Whether we are set as mute.
$paused  : bool
Whether the voice client is currently paused.
$readOpusTimer  : TimerInterface
readopus Timer.
$ready  : bool
Is the voice client ready?
$recieveStreams  : array<string|int, mixed>
Voice audio recieve streams.
$reconnecting  : bool
Is the voice client reconnecting?
$secret_key  : string
The secret key used for encrypting voice.
$sentLoginFrame  : bool
Have we sent the login frame yet?
$seq  : int
The sequence of audio packets being sent.
$silenceRemaining  : int
Silence Frame Remain Count.
$speaking  : bool
Are we currently set as speaking?
$speakingStatus  : Collection
Collection of the status of people speaking.
$startTime  : int
The time we started sending packets.
$streamTime  : int
The stream time of the last packet.
$timestamp  : int
The timestamp of the last packet.
$udpHeartbeat  : TimerInterface
The UDP heartbeat timer.
$udpPort  : int
The port the UDP client will use.
$version  : int
The Discord voice gateway version.
$voiceDecoders  : Collection
Collection of voice decoders.
$voiceWebsocket  : WebSocket
The voice WebSocket instance.
$volume  : int
The volume the audio will be encoded with.
__construct()  : mixed
Constructs the Voice Client instance.
close()  : void
Closes the voice client.
dcaDecode()  : Process
Decodes a file from Opus with DCA.
ffmpegEncode()  : Process
Creates a process that will run FFmpeg and encode `$filename` into Ogg Opus format.
getChannel()  : Channel
Returns the connected channel.
getRecieveStream()  : RecieveStream
Gets a recieve voice stream.
handleVoiceServerChange()  : void
Handles a voice server change.
handleVoiceStateUpdate()  : void
Handles a voice state update.
handleWebSocketClose()  : void
Handles a WebSocket close.
handleWebSocketConnection()  : void
Handles a WebSocket connection.
handleWebSocketError()  : void
Handles a WebSocket error.
initSockets()  : void
Initilizes the WebSocket and UDP socket.
isPaused()  : bool
Checks if we are paused.
isReady()  : bool
Returns whether the voice client is ready.
isSpeaking()  : bool
Checks if the user is speaking.
pause()  : void
Pauses the current sound.
playDCAStream()  : ExtendedPromiseInterface
Plays a DCA stream.
playFile()  : ExtendedPromiseInterface
Plays a file/url on the voice stream.
playOggStream()  : ExtendedPromiseInterface
Plays an Ogg Opus stream.
playRawStream()  : ExtendedPromiseInterface
Plays a raw PCM16 stream.
setAudioApplication()  : void
Sets the audio application.
setBitrate()  : void
Sets the bitrate.
setMuteDeaf()  : void
Changes your mute and deaf value.
setSpeaking()  : void
Sets the speaking value of the client.
setVolume()  : void
Sets the volume.
start()  : void|bool
Starts the voice client.
stop()  : void
Stops the current sound.
switchChannel()  : void
Switches voice channels.
unpause()  : void
Unpauses the current sound.
handleAudioData()  : void
Handles raw opus data from the UDP server.
checkForExecutable()  : string|null
Checks if an executable exists on the system.
checkForFFmpeg()  : bool
Checks if FFmpeg is installed.
checkForLibsodium()  : bool
Checks if libsodium-php is installed.
insertSilence()  : void
Insert 5 frames of silence.
mainSend()  : void
Sends a message to the main websocket.
reset()  : void
Resets the voice client.
send()  : void
Sends a message to the voice websocket.
sendBuffer()  : void
Sends a buffer to the UDP socket.

Constants

DCA_VERSION

The DCA version the client is using.

public string DCA_VERSION = 'DCA1'

The DCA version.

SILENCE_FRAME

The Opus Silence Frame.

public string SILENCE_FRAME = "\xf8\xff\xfe"

The silence frame.

Properties

$client

The UDP client.

public Socket $client

The voiceUDP client.

$ssrc

The SSRC value.

public int $ssrc

The SSRC value used for RTP.

$audioApplication

The audio application to encode with.

protected string $audioApplication = 'audio'

Available: voip, audio (default), lowdelay

The audio application.

$bitrate

The bitrate to encode with.

protected int $bitrate = 128000

Encoding bitrate.

$channel

The Channel that we are connecting to.

protected Channel $channel

The channel that we are going to connect to.

$data

Data from the main WebSocket.

protected array<string|int, mixed> $data

Information required for the voice WebSocket.

$dca

The DCA binary name that we will use.

protected string $dca

The DCA binary name that will be run.

$deaf

Whether we are set as deaf.

protected bool $deaf = false

Whether we are set as deaf.

$dnsConfig

The Config for DNS Resolver.

protected string|Config $dnsConfig

$endpoint

The Voice WebSocket endpoint.

protected string $endpoint

The endpoint the Voice WebSocket and UDP client will connect to.

$ffmpeg

The FFmpeg binary location.

protected string $ffmpeg

$frameSize

The size of audio frames, in milliseconds.

protected int $frameSize = 20

The size of audio frames.

$heartbeat

The Voice WebSocket heartbeat timer.

protected TimerInterface $heartbeat

The heartbeat periodic timer.

$heartbeat_interval

The UDP heartbeat interval.

protected int $heartbeat_interval

How often we send a heartbeat packet.

$heartbeatSeq

The UDP heartbeat sequence.

protected int $heartbeatSeq = 0

The heartbeat sequence.

$logger

The logger.

protected LoggerInterface $logger

Logger.

$loop

The ReactPHP event loop.

protected LoopInterface $loop

The ReactPHP event loop that will run everything.

$mainWebsocket

The main WebSocket instance.

protected WebSocket $mainWebsocket

The main WebSocket client.

$mode

The Voice WebSocket mode.

protected string $mode = 'xsalsa20_poly1305'

The voice mode.

$mute

Whether we are set as mute.

protected bool $mute = false

Whether we are set as mute.

$paused

Whether the voice client is currently paused.

protected bool $paused = false

Whether the voice client is currently paused.

$readOpusTimer

readopus Timer.

protected TimerInterface $readOpusTimer

Timer

$ready

Is the voice client ready?

protected bool $ready = false

Whether the voice client is ready.

$recieveStreams

Voice audio recieve streams.

protected array<string|int, mixed> $recieveStreams

Voice audio recieve streams.

$reconnecting

Is the voice client reconnecting?

protected bool $reconnecting = false

Whether the voice client is reconnecting.

$secret_key

The secret key used for encrypting voice.

protected string $secret_key

The secret key.

$sentLoginFrame

Have we sent the login frame yet?

protected bool $sentLoginFrame = false

Whether we have sent the login frame.

$seq

The sequence of audio packets being sent.

protected int $seq = 0

The sequence of audio packets.

$silenceRemaining

Silence Frame Remain Count.

protected int $silenceRemaining = 5

Amount of silence frames remaining.

$speaking

Are we currently set as speaking?

protected bool $speaking = false

Whether we are speaking or not.

$speakingStatus

Collection of the status of people speaking.

protected Collection $speakingStatus

Status of people speaking.

$startTime

The time we started sending packets.

protected int $startTime

The time we started sending packets.

$streamTime

The stream time of the last packet.

protected int $streamTime = 0

The time we sent the last packet.

$timestamp

The timestamp of the last packet.

protected int $timestamp = 0

The timestamp the last packet was constructed.

$udpHeartbeat

The UDP heartbeat timer.

protected TimerInterface $udpHeartbeat

The heartbeat periodic timer.

$udpPort

The port the UDP client will use.

protected int $udpPort

The port that the UDP client will connect to.

$version

The Discord voice gateway version.

protected int $version = 4

Voice version.

$voiceWebsocket

The voice WebSocket instance.

protected WebSocket $voiceWebsocket

The voice WebSocket client.

$volume

The volume the audio will be encoded with.

protected int $volume = 100

The volume that the audio will be encoded in.

Methods

__construct()

Constructs the Voice Client instance.

public __construct(WebSocket $websocket, LoopInterface $loop, Channel $channel, LoggerInterface $logger, array<string|int, mixed> $data) : mixed
Parameters
$websocket : WebSocket

The main WebSocket client.

$loop : LoopInterface

The ReactPHP event loop.

$channel : Channel

The channel we are connecting to.

$logger : LoggerInterface

The logger.

$data : array<string|int, mixed>

More information related to the voice client.

Return values
mixed

close()

Closes the voice client.

public close() : void
Tags
throws
RuntimeException
Return values
void

dcaDecode()

Decodes a file from Opus with DCA.

public dcaDecode([int $channels = 2 ][, int|null $frameSize = null ]) : Process
Parameters
$channels : int = 2

How many audio channels to decode with.

$frameSize : int|null = null

The Opus packet frame size.

Return values
Process

A ReactPHP Child Process

ffmpegEncode()

Creates a process that will run FFmpeg and encode `$filename` into Ogg Opus format.

public ffmpegEncode([string|null $filename = null ][, array<string|int, mixed>|null $preArgs = null ]) : Process

If $filename is null, the process will expect some sort of audio data to be piped in via stdin. It is highly recommended to set $preArgs to contain the format of the piped data when using a pipe as an input. You may also want to provide some arguments to FFmpeg via $preArgs, which will be appended to the FFmpeg command before setting the input arguments.

Parameters
$filename : string|null = null

Path to file to be converted into Ogg Opus, or null for pipe via stdin.

$preArgs : array<string|int, mixed>|null = null

A list of arguments to be appended before the input filename.

Return values
Process

A ReactPHP child process.

getChannel()

Returns the connected channel.

public getChannel() : Channel
Return values
Channel

The connected channel.

getRecieveStream()

Gets a recieve voice stream.

public getRecieveStream(int|string $id) : RecieveStream
Parameters
$id : int|string

Either a SSRC or User ID.

Return values
RecieveStream

handleVoiceServerChange()

Handles a voice server change.

public handleVoiceServerChange([array<string|int, mixed> $data = [] ]) : void
Parameters
$data : array<string|int, mixed> = []

New voice server information.

Return values
void

handleVoiceStateUpdate()

Handles a voice state update.

public handleVoiceStateUpdate(object $data) : void
Parameters
$data : object

The WebSocket data.

Return values
void

handleWebSocketClose()

Handles a WebSocket close.

public handleWebSocketClose(int $op, string $reason) : void
Parameters
$op : int
$reason : string
Return values
void

handleWebSocketConnection()

Handles a WebSocket connection.

public handleWebSocketConnection(WebSocket $ws) : void
Parameters
$ws : WebSocket

The WebSocket instance.

Return values
void

handleWebSocketError()

Handles a WebSocket error.

public handleWebSocketError(Exception $e) : void
Parameters
$e : Exception

The error.

Return values
void

initSockets()

Initilizes the WebSocket and UDP socket.

public initSockets() : void
Return values
void

isPaused()

Checks if we are paused.

public isPaused() : bool
Return values
bool

Whether we are paused.

isReady()

Returns whether the voice client is ready.

public isReady() : bool
Return values
bool

Whether the voice client is ready.

isSpeaking()

Checks if the user is speaking.

public isSpeaking([string|int|null $id = null ]) : bool
Parameters
$id : string|int|null = null

Either the User ID or SSRC (if null, return bots speaking status).

Return values
bool

Whether the user is speaking.

pause()

Pauses the current sound.

public pause() : void
Tags
throws
RuntimeException
Return values
void

playDCAStream()

Plays a DCA stream.

public playDCAStream(resource|Process|ReadableResourceStream $stream) : ExtendedPromiseInterface
Parameters
$stream : resource|Process|ReadableResourceStream

The DCA stream to be sent.

Tags
throws
Exception
deprecated
10.0.0

DCA is now deprecated in DiscordPHP, switch to using playOggStream with raw Ogg Opus.

Return values
ExtendedPromiseInterface

playFile()

Plays a file/url on the voice stream.

public playFile(string $file[, int $channels = 2 ]) : ExtendedPromiseInterface
Parameters
$file : string

The file/url to play.

$channels : int = 2

Deprecated, Discord only supports 2 channels.

Tags
throws
FileNotFoundException
throws
RuntimeException
Return values
ExtendedPromiseInterface

playOggStream()

Plays an Ogg Opus stream.

public playOggStream(resource|Process|ReadableResourceStream $stream) : ExtendedPromiseInterface
Parameters
$stream : resource|Process|ReadableResourceStream

The Ogg Opus stream to be sent.

Tags
throws
RuntimeException
throws
InvalidArgumentException
Return values
ExtendedPromiseInterface

playRawStream()

Plays a raw PCM16 stream.

public playRawStream(resource|ReadableResourceStream $stream[, int $channels = 2 ][, int $audioRate = 48000 ]) : ExtendedPromiseInterface
Parameters
$stream : resource|ReadableResourceStream

The stream to be encoded and sent.

$channels : int = 2

How many audio channels the PCM16 was encoded with.

$audioRate : int = 48000

Audio sampling rate the PCM16 was encoded with.

Tags
throws
RuntimeException
throws
InvalidArgumentException

Thrown when the stream passed to playRawStream is not a valid resource.

Return values
ExtendedPromiseInterface

setAudioApplication()

Sets the audio application.

public setAudioApplication(string $app) : void
Parameters
$app : string

The audio application to set.

Tags
throws
DomainException
throws
RuntimeException
Return values
void

setBitrate()

Sets the bitrate.

public setBitrate(int $bitrate) : void
Parameters
$bitrate : int

The bitrate to set.

Tags
throws
DomainException
throws
RuntimeException
Return values
void

setMuteDeaf()

Changes your mute and deaf value.

public setMuteDeaf(bool $mute, bool $deaf) : void
Parameters
$mute : bool

Whether you should be muted.

$deaf : bool

Whether you should be deaf.

Tags
throws
RuntimeException
Return values
void

setSpeaking()

Sets the speaking value of the client.

public setSpeaking([bool $speaking = true ]) : void
Parameters
$speaking : bool = true

Whether the client is speaking or not.

Tags
throws
RuntimeException
Return values
void

setVolume()

Sets the volume.

public setVolume(int $volume) : void
Parameters
$volume : int

The volume to set.

Tags
throws
DomainException
throws
RuntimeException
Return values
void

start()

Starts the voice client.

public start() : void|bool
Return values
void|bool

stop()

Stops the current sound.

public stop() : void
Tags
throws
RuntimeException
Return values
void

switchChannel()

Switches voice channels.

public switchChannel(Channel $channel) : void
Parameters
$channel : Channel

The channel to switch to.

Tags
throws
InvalidArgumentException
Return values
void

unpause()

Unpauses the current sound.

public unpause() : void
Tags
throws
RuntimeException
Return values
void

handleAudioData()

Handles raw opus data from the UDP server.

protected handleAudioData(string $message) : void
Parameters
$message : string

The data from the UDP server.

Return values
void

checkForExecutable()

Checks if an executable exists on the system.

private static checkForExecutable(string $executable) : string|null
Parameters
$executable : string
Return values
string|null

checkForFFmpeg()

Checks if FFmpeg is installed.

private checkForFFmpeg() : bool
Return values
bool

Whether FFmpeg is installed or not.

checkForLibsodium()

Checks if libsodium-php is installed.

private checkForLibsodium() : bool
Return values
bool

mainSend()

Sends a message to the main websocket.

private mainSend(array<string|int, mixed> $data) : void
Parameters
$data : array<string|int, mixed>

The data to send to the main WebSocket.

Return values
void

reset()

Resets the voice client.

private reset() : void
Return values
void

send()

Sends a message to the voice websocket.

private send(array<string|int, mixed> $data) : void
Parameters
$data : array<string|int, mixed>

The data to send to the voice WebSocket.

Return values
void

sendBuffer()

Sends a buffer to the UDP socket.

private sendBuffer(string $data) : void
Parameters
$data : string

The data to send to the UDP server.

Return values
void

Search results