Skip to content

Commit 59179f4

Browse files
authored
fix: 100% busy main thread while bot is not playing audio in vc (#1578)
1 parent c080532 commit 59179f4

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

src/dpp/voice/enabled/read_write.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@ void discord_voice_client::send(const char* packet, size_t len, uint64_t duratio
3434
frame.packet.assign(packet, packet + len);
3535
frame.duration = duration;
3636

37-
std::lock_guard<std::mutex> lock(this->stream_mutex);
38-
outbuf.emplace_back(frame);
37+
bool was_empty = false;
38+
{
39+
std::lock_guard<std::mutex> lock(this->stream_mutex);
40+
was_empty = outbuf.empty();
41+
outbuf.emplace_back(frame);
42+
}
43+
44+
if (was_empty) {
45+
udp_events.flags = WANT_READ | WANT_WRITE | WANT_ERROR;
46+
owner->socketengine->update_socket(udp_events);
47+
}
3948
} else [[unlikely]] {
4049
this->udp_send(packet, len);
4150
}

src/dpp/voice/enabled/write_ready.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@
3131
namespace dpp {
3232

3333
void discord_voice_client::write_ready() {
34-
/*
35-
* WANT_WRITE has been reset everytime this method is being called,
36-
* ALWAYS set it again no matter what we're gonna do.
37-
*/
38-
udp_events.flags = WANT_READ | WANT_WRITE | WANT_ERROR;
39-
owner->socketengine->update_socket(udp_events);
34+
bool needs_write = false;
35+
{
36+
std::lock_guard<std::mutex> lock(this->stream_mutex);
37+
const bool needs_stop_frames = this->paused && !this->sent_stop_frames;
38+
const bool has_audio = !outbuf.empty();
39+
needs_write = needs_stop_frames || has_audio;
40+
}
41+
42+
if (needs_write) {
43+
udp_events.flags = WANT_READ | WANT_WRITE | WANT_ERROR;
44+
owner->socketengine->update_socket(udp_events);
45+
}
4046

4147
uint64_t duration = 0;
4248
bool track_marker_found = false;

0 commit comments

Comments
 (0)