update rtp extension stripping

This commit is contained in:
ouwou 2024-07-05 03:48:06 -04:00
parent ebce49925c
commit e6191d9534
2 changed files with 17 additions and 15 deletions

View File

@ -15,17 +15,6 @@
#include <cstring>
// 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<AudioManager *>(pDevice->pUserData);
if (mgr == nullptr) return;
@ -233,11 +222,9 @@ void AudioManager::FeedMeOpus(uint32_t ssrc, const std::vector<uint8_t> &data) {
std::lock_guard<std::mutex> _(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<int>(data.size()), payload_size);
static std::array<opus_int16, 120 * 48 * 2> pcm;
if (auto it = m_sources.find(ssrc); it != m_sources.end()) {
int decoded = opus_decode(it->second.second, opus_encoded, static_cast<opus_int32>(payload_size), pcm.data(), 120 * 48, 0);
int decoded = opus_decode(it->second.second, data.data(), static_cast<opus_int32>(data.size()), pcm.data(), 120 * 48, 0);
if (decoded <= 0) {
} else {
UpdateReceiveVolume(ssrc, pcm.data(), decoded);

View File

@ -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<uint8_t> data) {
uint8_t *payload = data.data() + 12;
uint32_t ssrc = (data[8] << 24) |
@ -473,7 +486,9 @@ void DiscordVoiceClient::OnUDPData(std::vector<uint8_t> 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 });
}
}