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; }