Skip to content

Commit

Permalink
feat: receive_sequence for v8 voice gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
Neko-Life committed Sep 26, 2024
1 parent d26a7be commit 47bdf18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 4 additions & 2 deletions include/dpp/discordvoiceclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,11 @@ class DPP_EXPORT discord_voice_client : public websocket_client
uint16_t sequence;

/**
* @brief Last received sequence from gateway
* @brief Last received sequence from gateway.
*
* Needed for heartbeat and resume payload.
*/
uint16_t receive_sequence;
int32_t receive_sequence;

/**
* @brief Timestamp value used in outbound audio. Each packet
Expand Down
20 changes: 15 additions & 5 deletions src/dpp/discordvoiceclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ discord_voice_client::discord_voice_client(dpp::cluster* _cluster, snowflake _ch
repacketizer(nullptr),
fd(INVALID_SOCKET),
sequence(0),
receive_sequence(0),
receive_sequence(-1),
timestamp(0),
packet_nonce(1),
last_timestamp(std::chrono::high_resolution_clock::now()),
Expand Down Expand Up @@ -531,6 +531,16 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod
return true;
}

if (j.find("seq") != j.end() && j["seq"].is_number()) {
/**
* Save the sequence number needed for heartbeat and resume payload.
*
* NOTE: Contrary to the documentation, discord does not seem to send messages with sequence number
* in order, should we only save the sequence if it's larger number?
*/
receive_sequence = j["seq"].get<int32_t>();
}

if (j.find("op") != j.end()) {
uint32_t op = j["op"];

Expand Down Expand Up @@ -608,6 +618,9 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod
this->heartbeat_interval = j["d"]["heartbeat_interval"].get<uint32_t>();
}

/* Reset receive_sequence on HELLO */
receive_sequence = -1;

if (!modes.empty()) {
log(dpp::ll_debug, "Resuming voice session " + this->sessionid + "...");
json obj = {
Expand All @@ -618,7 +631,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod
{ "server_id", std::to_string(this->server_id) },
{ "session_id", this->sessionid },
{ "token", this->token },
{ "seq_ack", this->sequence },
{ "seq_ack", this->receive_sequence },
}
}
};
Expand Down Expand Up @@ -843,9 +856,6 @@ void discord_voice_client::read_ready()
std::memcpy(&vp.seq, &buffer[2], sizeof(rtp_seq_t));
vp.seq = ntohs(vp.seq);

/* Used for buffered resume of receive */
receive_sequence = vp.seq;

/* Get the timestamp of the voice UDP packet */
std::memcpy(&vp.timestamp, &buffer[4], sizeof(rtp_timestamp_t));
vp.timestamp = ntohl(vp.timestamp);
Expand Down

0 comments on commit 47bdf18

Please sign in to comment.