diff --git a/src/audio/manager.cpp b/src/audio/manager.cpp index 33730d9..d32c5e2 100644 --- a/src/audio/manager.cpp +++ b/src/audio/manager.cpp @@ -15,17 +15,6 @@ #include // clang-format on -const uint8_t *StripRTPExtensionHeader(const uint8_t *buf, int num_bytes, size_t &outlen) { - if (buf[0] == 0xbe && buf[1] == 0xde && num_bytes > 4) { - uint64_t offset = 4 + 4 * ((buf[2] << 8) | buf[3]); - - outlen = num_bytes - offset; - return buf + offset; - } - outlen = num_bytes; - return buf; -} - void data_callback(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount) { AudioManager *mgr = reinterpret_cast(pDevice->pUserData); if (mgr == nullptr) return; @@ -233,11 +222,9 @@ void AudioManager::FeedMeOpus(uint32_t ssrc, const std::vector &data) { std::lock_guard _(m_mutex); if (m_muted_ssrcs.find(ssrc) != m_muted_ssrcs.end()) return; - size_t payload_size = 0; - const auto *opus_encoded = StripRTPExtensionHeader(data.data(), static_cast(data.size()), payload_size); static std::array pcm; if (auto it = m_sources.find(ssrc); it != m_sources.end()) { - int decoded = opus_decode(it->second.second, opus_encoded, static_cast(payload_size), pcm.data(), 120 * 48, 0); + int decoded = opus_decode(it->second.second, data.data(), static_cast(data.size()), pcm.data(), 120 * 48, 0); if (decoded <= 0) { } else { UpdateReceiveVolume(ssrc, pcm.data(), decoded); diff --git a/src/discord/voiceclient.cpp b/src/discord/voiceclient.cpp index 212b878..e3e83f1 100644 --- a/src/discord/voiceclient.cpp +++ b/src/discord/voiceclient.cpp @@ -462,6 +462,19 @@ void DiscordVoiceClient::SetState(State state) { m_signal_state_update.emit(state); } +size_t GetPayloadOffset(const uint8_t *buf, size_t num_bytes) { + const bool has_extension_header = (buf[0] & 0b00010000) != 0; + const int csrc_count = buf[0] & 0b00001111; + + size_t offset = 12 + csrc_count * 4; + + if (has_extension_header && num_bytes > 4) { + offset += 4 + 4 * ((buf[offset + 2] << 8) | buf[offset + 3]); + } + + return offset; +} + void DiscordVoiceClient::OnUDPData(std::vector data) { uint8_t *payload = data.data() + 12; uint32_t ssrc = (data[8] << 24) | @@ -473,7 +486,9 @@ void DiscordVoiceClient::OnUDPData(std::vector data) { if (crypto_secretbox_open_easy(payload, payload, data.size() - 12, nonce.data(), m_secret_key.data())) { // spdlog::get("voice")->trace("UDP payload decryption failure"); } else { - Abaddon::Get().GetAudio().FeedMeOpus(ssrc, { payload, payload + data.size() - 12 - crypto_box_MACBYTES }); + size_t opus_offset = GetPayloadOffset(data.data(), data.size()); + payload = data.data() + opus_offset; + Abaddon::Get().GetAudio().FeedMeOpus(ssrc, { payload, payload + data.size() - opus_offset - crypto_box_MACBYTES }); } }