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