From 27179f0a7d5fa638dc1b7b4e34c4f916f81a104e Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sat, 13 Apr 2024 20:23:51 +0700 Subject: [PATCH 1/8] fix: clear track meta and reset tracjs count on stop audio --- src/dpp/discordvoiceclient.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 5e514d7331..032b1e3acd 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -692,6 +692,8 @@ dpp::utility::uptime discord_voice_client::get_remaining() { discord_voice_client& discord_voice_client::stop_audio() { std::lock_guard lock(this->stream_mutex); outbuf.clear(); + track_meta.clear(); + tracks = 0; return *this; } From cff55d9e9c1842be937201c88c77cb03e31ca07c Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sat, 13 Apr 2024 20:29:11 +0700 Subject: [PATCH 2/8] refactor: avoid hardcoded suze --- src/dpp/discordvoiceclient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 032b1e3acd..a0fac04321 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -849,7 +849,7 @@ void discord_voice_client::write_ready() std::lock_guard lock(this->stream_mutex); if (!this->paused && outbuf.size()) { type = send_audio_type; - if (outbuf[0].packet.size() == 2 && (*((uint16_t*)(outbuf[0].packet.data()))) == AUDIO_TRACK_MARKER) { + if (outbuf[0].packet.size() == sizeof(uint16_t) && (*((uint16_t*)(outbuf[0].packet.data()))) == AUDIO_TRACK_MARKER) { outbuf.erase(outbuf.begin()); track_marker_found = true; if (tracks > 0) { From 5d87563412964d64320b17b2fbe2294533fb8ab5 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sat, 13 Apr 2024 20:30:03 +0700 Subject: [PATCH 3/8] refactor: faster erase --- src/dpp/discordvoiceclient.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index a0fac04321..a50ddf4fe4 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -1167,20 +1167,29 @@ uint32_t discord_voice_client::get_tracks_remaining() { discord_voice_client& discord_voice_client::skip_to_next_marker() { std::lock_guard lock(this->stream_mutex); - /* Keep popping the first entry off the outbuf until the first entry is a track marker */ - while (!outbuf.empty() && outbuf[0].packet.size() != sizeof(uint16_t) && (*((uint16_t*)(outbuf[0].packet.data()))) != AUDIO_TRACK_MARKER) { - outbuf.erase(outbuf.begin()); - } - if (outbuf.size()) { - /* Remove the actual track marker out of the buffer */ - outbuf.erase(outbuf.begin()); + if (!outbuf.empty()) { + /* Find the first marker to skip to */ + auto i = std::find_if(outbuf.begin(), outbuf.end(), [](const voice_out_packet &v){ + return v.packet.size() == sizeof(uint16_t) && (*((uint16_t*)(v.packet.data()))) == AUDIO_TRACK_MARKER; + }); + + if (i != outbuf.end()) { + /* Skip queued packets until including found marker */ + outbuf.erase(outbuf.begin(), i); + } else { + /* No market found, skip the whole queue */ + outbuf.clear(); + } } + if (tracks > 0) { tracks--; } + if (!track_meta.empty()) { track_meta.erase(track_meta.begin()); } + return *this; } From 9f08f7f96fe6f563e9c218a79b7c36ce05127b1c Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sat, 13 Apr 2024 21:27:14 +0700 Subject: [PATCH 4/8] fix: let the marker be handled by sending thread --- src/dpp/discordvoiceclient.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index a50ddf4fe4..8c7248a2ee 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -1174,8 +1174,10 @@ discord_voice_client& discord_voice_client::skip_to_next_marker() { }); if (i != outbuf.end()) { - /* Skip queued packets until including found marker */ - outbuf.erase(outbuf.begin(), i); + if (i != outbuf.begin()) { + /* Skip queued packets up to found marker */ + outbuf.erase(outbuf.begin(), i-1); + } } else { /* No market found, skip the whole queue */ outbuf.clear(); From dd68a1f14bfffea2180a0e950a54731b1e7a23da Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sat, 13 Apr 2024 21:37:37 +0700 Subject: [PATCH 5/8] fix: forgot to remove this one --- src/dpp/discordvoiceclient.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 8c7248a2ee..689d477777 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -1184,14 +1184,6 @@ discord_voice_client& discord_voice_client::skip_to_next_marker() { } } - if (tracks > 0) { - tracks--; - } - - if (!track_meta.empty()) { - track_meta.erase(track_meta.begin()); - } - return *this; } From b725d78350edb3d04eebfec198eef5938948068d Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sat, 13 Apr 2024 21:49:14 +0700 Subject: [PATCH 6/8] fix: revert to erasing found marker, fix previous impl bug: should +1 to inlcude erasing the marker --- src/dpp/discordvoiceclient.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 689d477777..6fa7efaad4 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -1174,16 +1174,22 @@ discord_voice_client& discord_voice_client::skip_to_next_marker() { }); if (i != outbuf.end()) { - if (i != outbuf.begin()) { - /* Skip queued packets up to found marker */ - outbuf.erase(outbuf.begin(), i-1); - } + /* Skip queued packets until including found marker */ + outbuf.erase(outbuf.begin(), i+1); } else { /* No market found, skip the whole queue */ outbuf.clear(); } } + if (tracks > 0) { + tracks--; + } + + if (!track_meta.empty()) { + track_meta.erase(track_meta.begin()); + } + return *this; } From 4574a78b7db6369b84ee7e9d9477de6fcf7e5647 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 28 Apr 2024 15:26:37 +0700 Subject: [PATCH 7/8] fix indentation --- src/dpp/discordvoiceclient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 6fa7efaad4..d8819d017f 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -1171,7 +1171,7 @@ discord_voice_client& discord_voice_client::skip_to_next_marker() { /* Find the first marker to skip to */ auto i = std::find_if(outbuf.begin(), outbuf.end(), [](const voice_out_packet &v){ return v.packet.size() == sizeof(uint16_t) && (*((uint16_t*)(v.packet.data()))) == AUDIO_TRACK_MARKER; - }); + }); if (i != outbuf.end()) { /* Skip queued packets until including found marker */ From 6da82e28fbe7ded68e5850f7bca7b4aa29db2547 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Wed, 1 May 2024 21:59:53 +0700 Subject: [PATCH 8/8] fix indentation 2 --- src/dpp/discordvoiceclient.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index d8819d017f..8727992a1c 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -1170,8 +1170,8 @@ discord_voice_client& discord_voice_client::skip_to_next_marker() { if (!outbuf.empty()) { /* Find the first marker to skip to */ auto i = std::find_if(outbuf.begin(), outbuf.end(), [](const voice_out_packet &v){ - return v.packet.size() == sizeof(uint16_t) && (*((uint16_t*)(v.packet.data()))) == AUDIO_TRACK_MARKER; - }); + return v.packet.size() == sizeof(uint16_t) && (*((uint16_t*)(v.packet.data()))) == AUDIO_TRACK_MARKER; + }); if (i != outbuf.end()) { /* Skip queued packets until including found marker */