Unverified 提交 169b05b6 authored 作者: José Luis Millán's avatar José Luis Millán 提交者: GitHub

Optimize RTP header extension handling (#786)

* Optimize single-type RTP header extensions handling that avoids allocations, us better container for two-bytes header extension

* use std::array instead of static array
Co-authored-by: 's avatarNazar Mokrynskyi <nazar@mokrynskyi.com>
上级 5cbf2061
......@@ -4,7 +4,8 @@
#include "common.hpp"
#include "Utils.hpp"
#include "RTC/Codecs/PayloadDescriptorHandler.hpp"
#include <absl/container/btree_map.h>
#include <absl/container/flat_hash_map.h>
#include <array>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
......@@ -473,9 +474,11 @@ namespace RTC
}
else if (HasOneByteExtensions())
{
auto it = this->mapOneByteExtensions.find(id);
if (id > 14)
return false;
return it != this->mapOneByteExtensions.end();
// `-1` because we have 14 elements total 0..13 and `id` is in the range 1..14.
return this->oneByteExtensions[id - 1] != nullptr;
}
else if (HasTwoBytesExtensions())
{
......@@ -508,12 +511,14 @@ namespace RTC
}
else if (HasOneByteExtensions())
{
auto it = this->mapOneByteExtensions.find(id);
if (it == this->mapOneByteExtensions.end())
if (id > 14)
return nullptr;
auto* extension = it->second;
// `-1` because we have 14 elements total 0..13 and `id` is in the range 1..14.
auto* extension = this->oneByteExtensions[id - 1];
if (!extension)
return nullptr;
// In One-Byte extensions value length 0 means 1.
len = extension->len + 1;
......@@ -611,8 +616,10 @@ namespace RTC
Header* header{ nullptr };
uint8_t* csrcList{ nullptr };
HeaderExtension* headerExtension{ nullptr };
absl::btree_map<uint8_t, OneByteExtension*> mapOneByteExtensions;
absl::btree_map<uint8_t, TwoBytesExtension*> mapTwoBytesExtensions;
// There might be up to 14 one-byte header extensions
// (https://datatracker.ietf.org/doc/html/rfc5285#section-4.2), use std::array.
std::array<OneByteExtension*, 14> oneByteExtensions;
absl::flat_hash_map<uint8_t, TwoBytesExtension*> mapTwoBytesExtensions;
uint8_t midExtensionId{ 0u };
uint8_t ridExtensionId{ 0u };
uint8_t rridExtensionId{ 0u };
......
......@@ -6,6 +6,7 @@
#include "RTC/RateCalculator.hpp"
#include "RTC/SeqManager.hpp"
#include "RTC/TrendCalculator.hpp"
#include <absl/container/btree_map.h>
#include <map>
namespace RTC
......
......@@ -177,11 +177,9 @@ namespace RTC
if (HasOneByteExtensions())
{
extIds.reserve(this->mapOneByteExtensions.size());
for (const auto& kv : this->mapOneByteExtensions)
for (const auto& extension : this->oneByteExtensions)
{
extIds.push_back(std::to_string(kv.first));
extIds.push_back(std::to_string(extension->id));
}
}
else
......@@ -380,7 +378,7 @@ namespace RTC
this->videoOrientationExtensionId = 0u;
// Clear the One-Byte and Two-Bytes extension elements maps.
this->mapOneByteExtensions.clear();
std::fill(std::begin(this->oneByteExtensions), std::end(this->oneByteExtensions), nullptr);
this->mapTwoBytesExtensions.clear();
// If One-Byte is requested and the packet already has One-Byte extensions,
......@@ -491,8 +489,9 @@ namespace RTC
if (extension.id == 0 || extension.id > 14 || extension.len == 0 || extension.len > 16)
continue;
// Store the One-Byte extension element in the map.
this->mapOneByteExtensions[extension.id] = reinterpret_cast<OneByteExtension*>(ptr);
// Store the One-Byte extension element in an array.
// `-1` because we have 14 elements total 0..13 and `id` is in the range 1..14.
this->oneByteExtensions[extension.id - 1] = reinterpret_cast<OneByteExtension*>(ptr);
*ptr = (extension.id << 4) | ((extension.len - 1) & 0x0F);
++ptr;
......@@ -575,12 +574,12 @@ namespace RTC
}
else if (HasOneByteExtensions())
{
auto it = this->mapOneByteExtensions.find(id);
// `-1` because we have 14 elements total 0..13 and `id` is in the range 1..14.
auto* extension = this->oneByteExtensions[id - 1];
if (it == this->mapOneByteExtensions.end())
if (!extension)
return false;
auto* extension = it->second;
auto currentLen = extension->len + 1;
// Fill with 0's if new length is minor.
......@@ -858,7 +857,7 @@ namespace RTC
if (HasOneByteExtensions())
{
// Clear the One-Byte extension elements map.
this->mapOneByteExtensions.clear();
std::fill(std::begin(this->oneByteExtensions), std::end(this->oneByteExtensions), nullptr);
uint8_t* extensionStart = reinterpret_cast<uint8_t*>(this->headerExtension) + 4;
uint8_t* extensionEnd = extensionStart + GetHeaderExtensionLength();
......@@ -885,8 +884,9 @@ namespace RTC
break;
}
// Store the One-Byte extension element in the map.
this->mapOneByteExtensions[id] = reinterpret_cast<OneByteExtension*>(ptr);
// Store the One-Byte extension element in an array.
// `-1` because we have 14 elements total 0..13 and `id` is in the range 1..14.
this->oneByteExtensions[id - 1] = reinterpret_cast<OneByteExtension*>(ptr);
ptr += (1 + len);
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论