Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: receive_sequence for v8 voice gateway #1249

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading