Orca
A bot framework that is easy to reason about, easy to debug, and easy to use.
discord-voice-connections.h
Go to the documentation of this file.
1 
8 #ifndef DISCORD_VOICE_CONNECTIONS_H
9 #define DISCORD_VOICE_CONNECTIONS_H
10 
11 #include <time.h>
12 #include <pthread.h>
13 #include "cee-data-sizes.h"
14 
15 struct discord_voice; //forward
16 
23 typedef void (*voice_idle_cb)(
24  struct discord *client,
25  struct discord_voice *vc,
26  const struct discord_user *bot);
27 
34 typedef void (*voice_speaking_cb)(
35  struct discord *client,
36  struct discord_voice *vc,
37  const struct discord_user *bot,
38  const u64_snowflake_t user_id,
39  const int speaking,
40  const int delay,
41  const int ssrc);
42 
50  struct discord *client,
51  struct discord_voice *vc,
52  const struct discord_user *bot,
53  const u64_snowflake_t user_id);
54 
61 typedef void (*voice_codec_cb)(
62  struct discord *client,
63  struct discord_voice *vc,
64  const struct discord_user *bot,
65  const char audio_codec[],
66  const char video_codec[]);
67 
68 
69 struct discord_voice_cbs { /* CALLBACKS STRUCTURE */
74 
75  void (*on_ready)(struct discord_voice *vc);
76  void (*on_session_descriptor)(struct discord_voice *vc);
77  void (*on_udp_server_connected)(struct discord_voice *vc);
78 };
88 struct discord_voice {
91  char token[128];
93  char new_token[128];
94  char new_url[512];
95 
97  char session_id[128];
99  struct websockets *ws;
100  struct { /* RECONNECT STRUCTURE */
102  bool enable;
103  unsigned char attempt;
104  unsigned char threshold;
105  } reconnect;
107 
108  bool is_redirect;
109  bool is_ready;
110 
114  struct { /* VOICE PAYLOAD STRUCTURE */
115  enum discord_voice_opcodes opcode;
116  struct sized_buffer event_data;
117  } payload;
118 
119  struct { /* HEARTBEAT STRUCTURE */
122  } hbeat;
123 
124  int ping_ms;
125 
126  struct discord *p_client;
127 
128  bool shutdown;
129 
130  struct {
131  int ssrc;
132  int server_port;
133  char server_ip[CEE_MAX_IP_ADDR_STR_LEN];
134  char digest[CEE_SHA1_STR_LEN];
135  char unique_key[128];
136  int audio_udp_pid;
137  uintmax_t start_time;
138  } udp_service;
139 
140  struct discord_voice_cbs *p_voice_cbs;
141 
143 
151 };
152 
159 void discord_voice_set_on_idle(struct discord_voice *vc, voice_idle_cb *callback);
160 
161 enum discord_join_vc_status {
162  DISCORD_JOIN_VC_ERROR = 0,
163  DISCORD_JOIN_VC_JOINED = 1,
164  DISCORD_JOIN_VC_EXHAUST_CAPACITY,
165  DISCORD_JOIN_VC_ALREADY_JOINED
166 };
167 
180 enum discord_join_vc_status discord_join_vc(
181  struct discord *client,
182  struct discord_message *msg,
183  u64_snowflake_t guild_id,
184  u64_snowflake_t channel_id,
185  bool self_mute,
186  bool self_deaf);
187 
197 void discord_send_speaking(struct discord_voice *vc, enum discord_voice_speaking_flags flag, int delay);
198 
206 void _discord_on_voice_state_update(struct discord *client, struct discord_voice_state *vs);
207 
218 void _discord_on_voice_server_update(struct discord *client, u64_snowflake_t guild_id, char token[], char endpoint[]);
219 
227 void discord_voice_shutdown(struct discord_voice *vc);
228 
238 void discord_voice_reconnect(struct discord_voice *vc, bool resume);
239 
247 bool discord_voice_is_alive(struct discord_voice *vc);
248 
254 void discord_voice_connections_init(struct discord *client);
255 
256 #endif // DISCORD_VOICE_CONNECTIONS_H
discord_send_speaking
void discord_send_speaking(struct discord_voice *vc, enum discord_voice_speaking_flags flag, int delay)
Notify clients that you are speaking or have stopped speaking.
discord_voice_shutdown
void discord_voice_shutdown(struct discord_voice *vc)
Gracefully exits a ongoing Discord Voice connection.
discord_voice::message_channel_id
uint64_t message_channel_id
used to communicate the status of the bot state changes
Definition: discord-voice-connections.h:142
discord_voice_set_on_idle
void discord_voice_set_on_idle(struct discord_voice *vc, voice_idle_cb *callback)
Set a callback that triggers at every event-loop iteration.
discord_join_vc
enum discord_join_vc_status discord_join_vc(struct discord *client, struct discord_message *msg, u64_snowflake_t guild_id, u64_snowflake_t channel_id, bool self_mute, bool self_deaf)
Send a Voice State Update to Discord.
discord_voice_cbs::on_codec
voice_codec_cb on_codec
triggers when a codec is received
Definition: discord-voice-connections.h:73
voice_client_disconnect_cb
void(* voice_client_disconnect_cb)(struct discord *client, struct discord_voice *vc, const struct discord_user *bot, const u64_snowflake_t user_id)
Voice Client Disconnect callback.
Definition: discord-voice-connections.h:49
discord_voice_state
Voice State Structure.
Definition: voice.h:17
discord_voice::new_url
char new_url[512]
the new url after a voice region change
Definition: discord-voice-connections.h:94
discord_voice::tstamp
u64_unix_ms_t tstamp
Definition: discord-voice-connections.h:121
discord_voice::event_data
struct sized_buffer event_data
Definition: discord-voice-connections.h:116
voice_speaking_cb
void(* voice_speaking_cb)(struct discord *client, struct discord_voice *vc, const struct discord_user *bot, const u64_snowflake_t user_id, const int speaking, const int delay, const int ssrc)
Voice Speaking callback.
Definition: discord-voice-connections.h:34
discord_voice::guild_id
u64_snowflake_t guild_id
the session guild id
Definition: discord-voice-connections.h:89
discord_voice::is_ready
bool is_ready
can start sending/receiving additional events to discord
Definition: discord-voice-connections.h:109
discord_voice_reconnect
void discord_voice_reconnect(struct discord_voice *vc, bool resume)
Gracefully reconnect a ongoing Discord Voice connection.
discord_voice::session_id
char session_id[128]
the session id
Definition: discord-voice-connections.h:97
discord_voice::ping_ms
int ping_ms
latency between client and websockets server, calculated by the interval between HEARTBEAT and HEARTB...
Definition: discord-voice-connections.h:124
discord_voice::shutdown
bool shutdown
if true shutdown websockets connection as soon as possible
Definition: discord-voice-connections.h:128
discord_voice::is_resumable
bool is_resumable
will attempt to resume session if connection shutsdown
Definition: discord-voice-connections.h:106
discord_voice::enable
bool enable
will attempt reconnecting if true
Definition: discord-voice-connections.h:102
voice_idle_cb
void(* voice_idle_cb)(struct discord *client, struct discord_voice *vc, const struct discord_user *bot)
Idle callback.
Definition: discord-voice-connections.h:23
discord_voice_cbs
Definition: discord-voice-connections.h:69
discord_voice_cbs::on_speaking
voice_speaking_cb on_speaking
triggers when a user start speaking
Definition: discord-voice-connections.h:71
u64_unix_ms_t
uint64_t u64_unix_ms_t
unix time in ms
Definition: discord.h:26
discord
The Discord Client structure.
Definition: discord-internal.h:271
_discord_on_voice_server_update
void _discord_on_voice_server_update(struct discord *client, u64_snowflake_t guild_id, char token[], char endpoint[])
Update the voice session with a new token and url.
u64_snowflake_t
uint64_t u64_snowflake_t
snowflake datatype
Definition: discord.h:32
discord_voice::channel_id
u64_snowflake_t channel_id
Definition: discord-voice-connections.h:90
discord_voice::bot_id
u64_snowflake_t bot_id
the bot user id
Definition: discord-voice-connections.h:98
discord_voice::reconnect
struct discord_voice::@5 reconnect
handle reconnect logic
discord_voice
Discord Voice Connection handle, contain information about its active session.
Definition: discord-voice-connections.h:88
discord_voice::ws
struct websockets * ws
Definition: discord-voice-connections.h:99
discord_message
Message Structure.
Definition: channel.h:465
discord_voice::interval_ms
u64_unix_ms_t interval_ms
Definition: discord-voice-connections.h:120
discord_voice::new_token
char new_token[128]
the new session token after a voice region change
Definition: discord-voice-connections.h:93
discord_voice::recv_interval
int recv_interval
Interval to divide the received packets.
Definition: discord-voice-connections.h:150
discord_voice::threshold
unsigned char threshold
max amount of reconnects before giving up
Definition: discord-voice-connections.h:104
discord_voice::p_client
struct discord * p_client
pointer to client this struct is part of
Definition: discord-voice-connections.h:126
discord_voice_connections_init
void discord_voice_connections_init(struct discord *client)
Initialize the fields of a Discord Voice Connections handle.
discord_voice_is_alive
bool discord_voice_is_alive(struct discord_voice *vc)
Check if a Discord Voice connection is alive.
discord_voice::payload
struct discord_voice::@6 payload
discord_user
User Structure.
Definition: user.h:45
_discord_on_voice_state_update
void _discord_on_voice_state_update(struct discord *client, struct discord_voice_state *vs)
Update the voice session with a new session_id.
discord_voice::is_redirect
bool is_redirect
redirect to a different voice server
Definition: discord-voice-connections.h:108
voice_codec_cb
void(* voice_codec_cb)(struct discord *client, struct discord_voice *vc, const struct discord_user *bot, const char audio_codec[], const char video_codec[])
Voice Codec callback.
Definition: discord-voice-connections.h:61
discord_voice_cbs::on_client_disconnect
voice_client_disconnect_cb on_client_disconnect
triggers when a user has disconnected from the voice channel
Definition: discord-voice-connections.h:72
discord_voice::attempt
unsigned char attempt
current reconnect attempt (resets to 0 when succesful)
Definition: discord-voice-connections.h:103
discord_voice::opcode
enum discord_voice_opcodes opcode
Definition: discord-voice-connections.h:115
discord_voice::token
char token[128]
the session token
Definition: discord-voice-connections.h:92
discord_voice_cbs::on_idle
voice_idle_cb on_idle
triggers on every event loop iteration
Definition: discord-voice-connections.h:70