Packet
in package
An RTP voice packet: builds outbound frames and decodes inbound frames.
Decrypt flow order for inbound packets:
- Transport decrypt with the negotiated AEAD mode (libsodium) —
EncryptionMode::decrypt(), AES-256-GCM or XChaCha20-Poly1305 - Strip RTP header extension payload —
RtpHeader::stripExtensionPayload() - Optional DAVE inbound callback —
inboundFrameDecryptor(injected by VoiceClient); callsDaveRuntime::decryptWithDecryptor()whenpassthroughMode = false.
Huge thanks to Austin and Michael from JDA for the constants and audio packets. Check out their repo: https://github.com/DV8FromTheWorld/JDA
Tags
Table of Contents
Properties
- $decryptedAudio : null|false|string
- The decrypted audio.
- $encryptedAudio : string|null
- The encrypted audio.
- $payloadType : int|null
- The payload type (second byte of RTP header: M+PT bits).
- $secretKey : string|null
- The secret key.
- $seq : int|null
- $ssrc : int|null
- $timestamp : int|null
- $versionPlusFlags : int|null
- The version and flags (first byte of RTP header: V+P+X+CC bits).
- $buffer : Buffer
- The buffer containing the voice packet.
- $header : string
- The audio header, in binary, containing the version, flags, sequence, timestamp, and SSRC.
- $headerSize : int
- Current packet header size. May differ depending on the RTP header.
- $inboundFrameDecryptor : mixed
- $key : string|null
- $mode : EncryptionMode
- $nonce : int|null
- $outboundFrameEncryptor : mixed
- $rawData : string
- The raw data.
Methods
- __construct() : mixed
- Constructs the voice packet.
- decrypt() : string|false|null
- Decrypts the voice message.
- encrypt() : mixed
- Encrypts the voice message.
- getAudioData() : string|false|null
- Retrieves the decrypted audio data.
- getData() : string
- Returns the data.
- getEncryptedMessage() : string|null
- Retrieves the encrypted audio data with header, ready for sending.
- getHeader() : string|null
- Returns the header.
- getSequence() : int
- Returns the sequence.
- getSSRC() : int
- Returns the SSRC.
- getTimestamp() : int
- Returns the timestamp.
- make() : self
- Creates a voice packet from data sent from Discord.
- setBuffer() : self
- Sets the buffer.
- setHeader() : string|null
- Sets the header.
- unpack() : self
- Unpacks the voice message into an array.
- buildHeader() : string
- Builds the 12-byte RTP header: `V/P/X/CC | M/PT | seq(BE16) | timestamp(BE32) | SSRC(BE32)`.
Properties
$decryptedAudio
The decrypted audio.
public
null|false|string
$decryptedAudio
$encryptedAudio
The encrypted audio.
public
string|null
$encryptedAudio
$payloadType
The payload type (second byte of RTP header: M+PT bits).
public
int|null
$payloadType
= null
$secretKey
The secret key.
public
string|null
$secretKey
$seq
public
int|null
$seq
= null
$ssrc
public
int|null
$ssrc
= null
$timestamp
public
int|null
$timestamp
= null
$versionPlusFlags
The version and flags (first byte of RTP header: V+P+X+CC bits).
public
int|null
$versionPlusFlags
= null
$buffer
The buffer containing the voice packet.
protected
Buffer
$buffer
$header
The audio header, in binary, containing the version, flags, sequence, timestamp, and SSRC.
protected
string
$header
$headerSize
Current packet header size. May differ depending on the RTP header.
protected
int
$headerSize
$inboundFrameDecryptor
protected
mixed
$inboundFrameDecryptor
= null
$key
protected
string|null
$key
= null
$mode
protected
EncryptionMode
$mode
= EncryptionMode::AEAD_AES256_GCM_RTPSIZE
$nonce
protected
int|null
$nonce
= null
$outboundFrameEncryptor
protected
mixed
$outboundFrameEncryptor
= null
$rawData
The raw data.
protected
string
$rawData
Methods
__construct()
Constructs the voice packet.
public
__construct([string $data = null ][, int $ssrc = null ][, int $seq = null ][, int $timestamp = null ][, bool $decrypt = true ][, string|null $key = null ][, null|callable(string): string $outboundFrameEncryptor = null ][, null|callable(string, self): false|string $inboundFrameDecryptor = null ][, int|null $nonce = null ][, EncryptionMode $mode = EncryptionMode::AEAD_AES256_GCM_RTPSIZE ]) : mixed
Parameters
- $data : string = null
-
The Opus data to encode.
- $ssrc : int = null
-
The client SSRC value.
- $seq : int = null
-
The packet sequence.
- $timestamp : int = null
-
The packet timestamp.
- $decrypt : bool = true
- $key : string|null = null
-
The encryption key.
- $outboundFrameEncryptor : null|callable(string): string = null
-
Optional callback to transform outgoing decrypted frame data.
- $inboundFrameDecryptor : null|callable(string, self): false|string = null
-
Optional callback to transform incoming decrypted frame data.
- $nonce : int|null = null
-
32-bit nonce counter for the transport cipher. Required for encryption; null is only valid on the decrypt path.
- $mode : EncryptionMode = EncryptionMode::AEAD_AES256_GCM_RTPSIZE
-
The transport encryption mode negotiated with the voice server.
decrypt()
Decrypts the voice message.
public
decrypt([string|null $message = null ]) : string|false|null
Parameters
- $message : string|null = null
Return values
string|false|nullencrypt()
Encrypts the voice message.
public
encrypt() : mixed
Applies the optional outbound DAVE frame transform, then the negotiated AEAD cipher
(AES-256-GCM or XChaCha20-Poly1305) with the RTP header as additional data, and
assembles rawData as [header][ciphertext+tag][nonce].
Tags
getAudioData()
Retrieves the decrypted audio data.
public
getAudioData() : string|false|null
Will return null if the audio data is not decrypted and false on error.
Return values
string|false|nullgetData()
Returns the data.
public
getData() : string
Return values
stringgetEncryptedMessage()
Retrieves the encrypted audio data with header, ready for sending.
public
getEncryptedMessage() : string|null
Will return null if the audio data is not encrypted.
Return values
string|nullgetHeader()
Returns the header.
public
getHeader() : string|null
Return values
string|nullgetSequence()
Returns the sequence.
public
getSequence() : int
Return values
intgetSSRC()
Returns the SSRC.
public
getSSRC() : int
Return values
intgetTimestamp()
Returns the timestamp.
public
getTimestamp() : int
Return values
intmake()
Creates a voice packet from data sent from Discord.
public
static make(string $data) : self
Parameters
- $data : string
Return values
selfsetBuffer()
Sets the buffer.
public
setBuffer(Buffer $buffer) : self
Parameters
- $buffer : Buffer
Return values
selfsetHeader()
Sets the header.
public
setHeader([string|null $message = null ]) : string|null
If no message is provided, it will use the raw data of the packet.
Parameters
- $message : string|null = null
Return values
string|nullunpack()
Unpacks the voice message into an array.
public
unpack(string $message) : self
C1 (unsigned char) | Version + Flags | 1 bytes | Single byte value of 0x80 C1 (unsigned char) | Payload Type | 1 bytes | Single byte value of 0x78 n (Unsigned short (big endian)) | Sequence | 2 bytes I (Unsigned integer (big endian)) | Timestamp | 4 bytes I (Unsigned integer (big endian)) | SSRC | 4 bytes a* (string) | Encrypted audio | n bytes | Binary data of the encrypted audio.
Parameters
- $message : string
Tags
Return values
selfbuildHeader()
Builds the 12-byte RTP header: `V/P/X/CC | M/PT | seq(BE16) | timestamp(BE32) | SSRC(BE32)`.
protected
buildHeader() : string
Emitted once per outbound packet (~50/s per stream), so it packs the fixed layout in a single pack() call rather than routing bytes through a Buffer.