D++ (DPP)
C++ Discord API Bot Library
dpp::DiscordVoiceClient Class Reference

Implements a discord voice connection. Each DiscordVoiceClient connects to one voice channel and derives from a websocket client. More...

#include <discordvoiceclient.h>

+ Inheritance diagram for dpp::DiscordVoiceClient:
+ Collaboration diagram for dpp::DiscordVoiceClient:

Public Member Functions

virtual void log (dpp::loglevel severity, const std::string &msg)
 
virtual void OneSecondTimer ()
 
bool IsReady ()
 voice client is ready to stream audio. The voice client is considered ready if it has a secret key. More...
 
bool IsConnected ()
 Returns true if the voice client is connected to the websocket. More...
 
dpp::utility::uptime Uptime ()
 Returns the connection time of the voice client. More...
 
 DiscordVoiceClient (dpp::cluster *_cluster, snowflake _channel_id, snowflake _server_id, const std::string &_token, const std::string &_session_id, const std::string &_host)
 
virtual ~DiscordVoiceClient ()
 
virtual bool HandleFrame (const std::string &buffer)
 
virtual void Error (uint32_t errorcode)
 
void Run ()
 
void SendAudio (uint16_t *audio_data, const size_t length, bool use_opus=true)
 Send audio to the voice channel. More...
 
void PauseAudio (bool pause)
 Pause sending of audio. More...
 
void StopAudio ()
 Immediately stop all audio. Clears the packet queue. More...
 
bool IsPlaying ()
 Returns true if we are playing audio. More...
 
float GetSecsRemaining ()
 Get the number of seconds remaining of the audio output buffer. More...
 
uint32_t GetTracksRemaining ()
 Get the number of tracks remaining in the output buffer. This is calculated by the number of track markers plus one. More...
 
dpp::utility::uptime GetRemaining ()
 Get the time remaining to send the audio output buffer in hours:minutes:seconds. More...
 
void InsertMarker (const std::string &metadata="")
 Insert a track marker into the audio output buffer. A track marker is an arbitrary flag in the buffer contents that indictes the end of some block of audio of significance to the sender. This may be a song from a streaming site, or some voice audio/speech, a sound effect, or whatever you choose. You can later skip to the next marker using the dpp::DiscordVoiceClient::SkipToNextMarker function. More...
 
void SkipToNextMarker ()
 Skip tp the next track marker, previously inserted by using the dpp::DiscordVoiceClient::InsertMarker function. If there are no markers in the output buffer, then this skips to the end of the buffer and is equivalent to the dpp::DiscordVoiceClient::StopAudio function. More...
 
const std::vector< std::string > GetMarkerMetadata ()
 Get the metdata string associated with each inserted marker. More...
 
bool IsPaused ()
 Returns true if the audio is paused. You can unpause with dpp::DiscordVoiceClient::PauseAudio. More...
 
std::string DiscoverIP ()
 Discord external IP detection. More...
 
virtual void write (const std::string &data)
 Write to websocket. Encapsulates data in frames if the status is CONNECTED. More...
 
virtual bool HandleBuffer (std::string &buffer)
 Processes incoming frames from the SSL socket input buffer. More...
 
virtual void close ()
 Close websocket. More...
 
uint64_t GetBytesOut ()
 
uint64_t GetBytesIn ()
 
void ReadLoop ()
 Nonblocking I/O loop. More...
 

Public Attributes

class dpp::clustercreator
 
bool terminating
 
uint32_t heartbeat_interval
 
time_t last_heartbeat
 
std::thread::native_handle_type thread_id
 
std::string token
 
std::string sessionid
 
snowflake server_id
 
snowflake channel_id
 
std::function< int()> custom_readable_fd
 Attaching an additional file descriptor to this function will send notifications when there is data to read. More...
 
std::function< int()> custom_writeable_fd
 Attaching an additional file descriptor to this function will send notifications when you are able to write to the socket. More...
 
std::function< void()> custom_readable_ready
 This event will be called when you can read from the custom fd. More...
 
std::function< void()> custom_writeable_ready
 This event will be called when you can write to a custom fd. More...
 

Static Public Attributes

static bool sodium_initialised
 

Protected Member Functions

virtual void Connect ()
 
WSState GetState ()
 

Protected Attributes

std::string buffer
 
std::string obuffer
 
bool nonblocking
 
int sfd
 
SSL * ssl
 
SSL_CTX * ctx
 
std::string cipher
 
time_t last_tick
 
std::string hostname
 
uint64_t bytes_out
 
uint64_t bytes_in
 

Detailed Description

Implements a discord voice connection. Each DiscordVoiceClient connects to one voice channel and derives from a websocket client.

Constructor & Destructor Documentation

◆ DiscordVoiceClient()

dpp::DiscordVoiceClient::DiscordVoiceClient ( dpp::cluster _cluster,
snowflake  _channel_id,
snowflake  _server_id,
const std::string &  _token,
const std::string &  _session_id,
const std::string &  _host 
)

Constructor takes shard id, max shards and token.

Parameters
_clusterThe owning cluster for this shard
_server_idThe server id to identify voice connection as
_tokenThe voice session token to use for identifying to the websocket
_session_idThe voice session id to identify with
_hostThe voice server hostname to connect to (hostname:port format)

◆ ~DiscordVoiceClient()

virtual dpp::DiscordVoiceClient::~DiscordVoiceClient ( )
virtual

Destructor

Member Function Documentation

◆ close()

virtual void dpp::WSClient::close ( )
virtualinherited

Close websocket.

Reimplemented from dpp::SSLClient.

◆ Connect()

virtual void dpp::WSClient::Connect ( )
protectedvirtualinherited

(Re)connect

Reimplemented from dpp::SSLClient.

◆ DiscoverIP()

std::string dpp::DiscordVoiceClient::DiscoverIP ( )

Discord external IP detection.

Returns
std::string Your external IP address
Note
This is a blocking operation that waits for a single packet from Discord's voice servers.

◆ Error()

virtual void dpp::DiscordVoiceClient::Error ( uint32_t  errorcode)
virtual

Handle a websocket error.

Parameters
errorcodeThe error returned from the websocket

Reimplemented from dpp::WSClient.

◆ GetBytesIn()

uint64_t dpp::SSLClient::GetBytesIn ( )
inherited

Get total bytes received

◆ GetBytesOut()

uint64_t dpp::SSLClient::GetBytesOut ( )
inherited

Get total bytes sent

◆ GetMarkerMetadata()

const std::vector< std::string > dpp::DiscordVoiceClient::GetMarkerMetadata ( )

Get the metdata string associated with each inserted marker.

Returns
const std::vector<std::string>& list of metadata strings

◆ GetRemaining()

dpp::utility::uptime dpp::DiscordVoiceClient::GetRemaining ( )

Get the time remaining to send the audio output buffer in hours:minutes:seconds.

Returns
dpp::utility::uptime length of buffer

◆ GetSecsRemaining()

float dpp::DiscordVoiceClient::GetSecsRemaining ( )

Get the number of seconds remaining of the audio output buffer.

Returns
float number of seconds remaining

◆ GetState()

WSState dpp::WSClient::GetState ( )
protectedinherited

Get websocket state

Returns
websocket state

◆ GetTracksRemaining()

uint32_t dpp::DiscordVoiceClient::GetTracksRemaining ( )

Get the number of tracks remaining in the output buffer. This is calculated by the number of track markers plus one.

Returns
uint32_t Number of tracks in the buffer

◆ HandleBuffer()

virtual bool dpp::WSClient::HandleBuffer ( std::string &  buffer)
virtualinherited

Processes incoming frames from the SSL socket input buffer.

Parameters
bufferThe buffer contents. Can modify this value removing the head elements when processed.

Reimplemented from dpp::SSLClient.

◆ HandleFrame()

virtual bool dpp::DiscordVoiceClient::HandleFrame ( const std::string &  buffer)
virtual

Handle JSON from the websocket.

Parameters
bufferThe entire buffer content from the websocket client
Returns
True if a frame has been handled

Reimplemented from dpp::WSClient.

◆ InsertMarker()

void dpp::DiscordVoiceClient::InsertMarker ( const std::string &  metadata = "")

Insert a track marker into the audio output buffer. A track marker is an arbitrary flag in the buffer contents that indictes the end of some block of audio of significance to the sender. This may be a song from a streaming site, or some voice audio/speech, a sound effect, or whatever you choose. You can later skip to the next marker using the dpp::DiscordVoiceClient::SkipToNextMarker function.

Parameters
metadataArbitrary information related to this track

◆ IsConnected()

bool dpp::DiscordVoiceClient::IsConnected ( )

Returns true if the voice client is connected to the websocket.

Returns
True if connected

◆ IsPaused()

bool dpp::DiscordVoiceClient::IsPaused ( )

Returns true if the audio is paused. You can unpause with dpp::DiscordVoiceClient::PauseAudio.

Returns
true if paused

◆ IsPlaying()

bool dpp::DiscordVoiceClient::IsPlaying ( )

Returns true if we are playing audio.

Returns
true if audio is playing

◆ IsReady()

bool dpp::DiscordVoiceClient::IsReady ( )

voice client is ready to stream audio. The voice client is considered ready if it has a secret key.

Returns
true if ready to stream audio

◆ log()

virtual void dpp::DiscordVoiceClient::log ( dpp::loglevel  severity,
const std::string &  msg 
)
virtual

Log a message to whatever log the user is using. The logged message is passed up the chain to the on_log event in user code which can then do whatever it wants to do with it.

Parameters
severityThe log level from dpp::loglevel
msgThe log message to output

Reimplemented from dpp::SSLClient.

◆ OneSecondTimer()

virtual void dpp::DiscordVoiceClient::OneSecondTimer ( )
virtual

Fires every second from the underlying socket I/O loop, used for sending heartbeats

Reimplemented from dpp::SSLClient.

◆ PauseAudio()

void dpp::DiscordVoiceClient::PauseAudio ( bool  pause)

Pause sending of audio.

Parameters
pauseTrue to pause, false to resume

◆ ReadLoop()

void dpp::SSLClient::ReadLoop ( )
inherited

Nonblocking I/O loop.

◆ Run()

void dpp::DiscordVoiceClient::Run ( )

Start and monitor I/O loop

◆ SendAudio()

void dpp::DiscordVoiceClient::SendAudio ( uint16_t *  audio_data,
const size_t  length,
bool  use_opus = true 
)

Send audio to the voice channel.

You should send an audio packet of n11520 bytes. Note that this function can be costly as it has to opus encode the PCM audio on the fly, and also encrypt it with libsodium.

Note
Because this function encrypts and encodes packets before pushing them onto the output queue, if you have a complete stream ready to send and know its length it is advisable to call this method multiple times to enqueue the entire stream audio so that it is all encoded at once. Constantly calling this from the dpp::on_voice_buffer_send callback can and will eat a TON of cpu!
Parameters
audio_dataRaw PCM audio data. Channels are interleaved, with each channel's amplitude being a 16 bit value.
lengthThe length of the audio data. The length should be a multiple of 4 (2x 16 bit stero channels) with a maximum length of 11520, which is a complete opus frame at highest quality.
use_opusSome containers such as .ogg may contain OPUS encoded data already. In this case, we don't need to encode the frames using opus here. We can set use_opus to false and bypass the codec, only applying libsodium to the stream.

◆ SkipToNextMarker()

void dpp::DiscordVoiceClient::SkipToNextMarker ( )

Skip tp the next track marker, previously inserted by using the dpp::DiscordVoiceClient::InsertMarker function. If there are no markers in the output buffer, then this skips to the end of the buffer and is equivalent to the dpp::DiscordVoiceClient::StopAudio function.

Note
It is possible to use this function while the output stream is paused.

◆ StopAudio()

void dpp::DiscordVoiceClient::StopAudio ( )

Immediately stop all audio. Clears the packet queue.

◆ Uptime()

dpp::utility::uptime dpp::DiscordVoiceClient::Uptime ( )

Returns the connection time of the voice client.

Returns
dpp::utility::uptime Detail of how long the voice client has been connected for

◆ write()

virtual void dpp::WSClient::write ( const std::string &  data)
virtualinherited

Write to websocket. Encapsulates data in frames if the status is CONNECTED.

Parameters
dataThe data to send.

Reimplemented from dpp::SSLClient.

Member Data Documentation

◆ buffer

std::string dpp::SSLClient::buffer
protectedinherited

Input buffer received from openssl

◆ bytes_in

uint64_t dpp::SSLClient::bytes_in
protectedinherited

Bytes in

◆ bytes_out

uint64_t dpp::SSLClient::bytes_out
protectedinherited

Bytes out

◆ channel_id

snowflake dpp::DiscordVoiceClient::channel_id

Channel ID

◆ cipher

std::string dpp::SSLClient::cipher
protectedinherited

SSL cipher in use

◆ creator

class dpp::cluster* dpp::DiscordVoiceClient::creator

Owning cluster

◆ ctx

SSL_CTX* dpp::SSLClient::ctx
protectedinherited

OpenSSL context

◆ custom_readable_fd

std::function<int()> dpp::SSLClient::custom_readable_fd
inherited

Attaching an additional file descriptor to this function will send notifications when there is data to read.

NOTE: Only hook this if you NEED it as it can increase CPU usage of the thread! Returning -1 means that you don't want to be notified.

◆ custom_readable_ready

std::function<void()> dpp::SSLClient::custom_readable_ready
inherited

This event will be called when you can read from the custom fd.

◆ custom_writeable_fd

std::function<int()> dpp::SSLClient::custom_writeable_fd
inherited

Attaching an additional file descriptor to this function will send notifications when you are able to write to the socket.

NOTE: Only hook this if you NEED it as it can increase CPU usage of the thread! You should toggle this to -1 when you do not have anything to write otherwise it'll keep triggering repeatedly (it is level triggered).

◆ custom_writeable_ready

std::function<void()> dpp::SSLClient::custom_writeable_ready
inherited

This event will be called when you can write to a custom fd.

◆ heartbeat_interval

uint32_t dpp::DiscordVoiceClient::heartbeat_interval

Heartbeat interval for sending heartbeat keepalive

◆ hostname

std::string dpp::SSLClient::hostname
protectedinherited

Hostname connected to

◆ last_heartbeat

time_t dpp::DiscordVoiceClient::last_heartbeat

Last heartbeat

◆ last_tick

time_t dpp::SSLClient::last_tick
protectedinherited

For timers

◆ nonblocking

bool dpp::SSLClient::nonblocking
protectedinherited

True if in nonblocking mode. The socket switches to nonblocking mode once ReadLoop is called.

◆ obuffer

std::string dpp::SSLClient::obuffer
protectedinherited

Output buffer for sending to openssl

◆ server_id

snowflake dpp::DiscordVoiceClient::server_id

Server ID

◆ sessionid

std::string dpp::DiscordVoiceClient::sessionid

Discord voice session id

◆ sfd

int dpp::SSLClient::sfd
protectedinherited

Raw file descriptor of connection

◆ sodium_initialised

bool dpp::DiscordVoiceClient::sodium_initialised
static

◆ ssl

SSL* dpp::SSLClient::ssl
protectedinherited

OpenSSL session

◆ terminating

bool dpp::DiscordVoiceClient::terminating

True when the thread is shutting down

◆ thread_id

std::thread::native_handle_type dpp::DiscordVoiceClient::thread_id

Thread ID

◆ token

std::string dpp::DiscordVoiceClient::token

Discord voice session token

D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0