提交 e56b4947 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Optimize RtpPacket::SetExtensions() tby filling the RTP extension maps in place

上级 685ef99e
...@@ -154,8 +154,8 @@ namespace RTC ...@@ -154,8 +154,8 @@ namespace RTC
Header* header{ nullptr }; Header* header{ nullptr };
uint8_t* csrcList{ nullptr }; uint8_t* csrcList{ nullptr };
HeaderExtension* headerExtension{ nullptr }; HeaderExtension* headerExtension{ nullptr };
std::map<uint8_t, OneByteExtension*> oneByteExtensions; std::map<uint8_t, OneByteExtension*> mapOneByteExtensions;
std::map<uint8_t, TwoBytesExtension*> twoBytesExtensions; std::map<uint8_t, TwoBytesExtension*> mapTwoBytesExtensions;
uint8_t audioLevelExtensionId{ 0 }; uint8_t audioLevelExtensionId{ 0 };
uint8_t videoOrientationExtensionId{ 0 }; uint8_t videoOrientationExtensionId{ 0 };
uint8_t absSendTimeExtensionId{ 0 }; uint8_t absSendTimeExtensionId{ 0 };
...@@ -451,9 +451,9 @@ namespace RTC ...@@ -451,9 +451,9 @@ namespace RTC
} }
else if (HasOneByteExtensions()) else if (HasOneByteExtensions())
{ {
auto it = this->oneByteExtensions.find(id); auto it = this->mapOneByteExtensions.find(id);
if (it == this->oneByteExtensions.end()) if (it == this->mapOneByteExtensions.end())
return nullptr; return nullptr;
auto* extension = it->second; auto* extension = it->second;
...@@ -464,9 +464,9 @@ namespace RTC ...@@ -464,9 +464,9 @@ namespace RTC
} }
else if (HasTwoBytesExtensions()) else if (HasTwoBytesExtensions())
{ {
auto it = this->twoBytesExtensions.find(id); auto it = this->mapTwoBytesExtensions.find(id);
if (it == this->twoBytesExtensions.end()) if (it == this->mapTwoBytesExtensions.end())
return nullptr; return nullptr;
auto* extension = it->second; auto* extension = it->second;
......
...@@ -177,18 +177,18 @@ namespace RTC ...@@ -177,18 +177,18 @@ namespace RTC
if (HasOneByteExtensions()) if (HasOneByteExtensions())
{ {
extIds.reserve(this->oneByteExtensions.size()); extIds.reserve(this->mapOneByteExtensions.size());
for (auto& kv : this->oneByteExtensions) for (auto& kv : this->mapOneByteExtensions)
{ {
extIds.push_back(std::to_string(kv.first)); extIds.push_back(std::to_string(kv.first));
} }
} }
else else
{ {
extIds.reserve(this->twoBytesExtensions.size()); extIds.reserve(this->mapTwoBytesExtensions.size());
for (auto& kv : this->twoBytesExtensions) for (auto& kv : this->mapTwoBytesExtensions)
{ {
extIds.push_back(std::to_string(kv.first)); extIds.push_back(std::to_string(kv.first));
} }
...@@ -284,6 +284,10 @@ namespace RTC ...@@ -284,6 +284,10 @@ namespace RTC
this->ridExtensionId = 0; this->ridExtensionId = 0;
this->rridExtensionId = 0; this->rridExtensionId = 0;
// Clear the One-Byte and Two-Bytes extension elements maps.
this->mapOneByteExtensions.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,
// keep the header extension id. // keep the header extension id.
if (type == 1u && HasOneByteExtensions()) if (type == 1u && HasOneByteExtensions())
...@@ -392,6 +396,9 @@ namespace RTC ...@@ -392,6 +396,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.
this->mapOneByteExtensions[extension.id] = reinterpret_cast<OneByteExtension*>(ptr);
*ptr = (extension.id << 4) | ((extension.len - 1) & 0x0F); *ptr = (extension.id << 4) | ((extension.len - 1) & 0x0F);
++ptr; ++ptr;
std::memmove(ptr, extension.value, extension.len); std::memmove(ptr, extension.value, extension.len);
...@@ -402,6 +409,9 @@ namespace RTC ...@@ -402,6 +409,9 @@ namespace RTC
if (extension.id == 0) if (extension.id == 0)
continue; continue;
// Store the Two-Bytes extension element in the map.
this->mapTwoBytesExtensions[extension.id] = reinterpret_cast<TwoBytesExtension*>(ptr);
*ptr = extension.id; *ptr = extension.id;
++ptr; ++ptr;
*ptr = extension.len; *ptr = extension.len;
...@@ -418,9 +428,6 @@ namespace RTC ...@@ -418,9 +428,6 @@ namespace RTC
} }
MS_ASSERT(ptr == this->payload, "wrong ptr calculation"); MS_ASSERT(ptr == this->payload, "wrong ptr calculation");
// Parse extensions again.
ParseExtensions();
} }
RtpPacket* RtpPacket::Clone(const uint8_t* buffer) const RtpPacket* RtpPacket::Clone(const uint8_t* buffer) const
...@@ -647,7 +654,7 @@ namespace RTC ...@@ -647,7 +654,7 @@ namespace RTC
if (HasOneByteExtensions()) if (HasOneByteExtensions())
{ {
// Clear the One-Byte extension elements map. // Clear the One-Byte extension elements map.
this->oneByteExtensions.clear(); this->mapOneByteExtensions.clear();
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();
...@@ -674,8 +681,8 @@ namespace RTC ...@@ -674,8 +681,8 @@ namespace RTC
break; break;
} }
// Store the One-Byte extension element in a map. // Store the One-Byte extension element in the map.
this->oneByteExtensions[id] = reinterpret_cast<OneByteExtension*>(ptr); this->mapOneByteExtensions[id] = reinterpret_cast<OneByteExtension*>(ptr);
ptr += (1 + len); ptr += (1 + len);
} }
...@@ -696,7 +703,7 @@ namespace RTC ...@@ -696,7 +703,7 @@ namespace RTC
else if (HasTwoBytesExtensions()) else if (HasTwoBytesExtensions())
{ {
// Clear the Two-Bytes extension elements map. // Clear the Two-Bytes extension elements map.
this->twoBytesExtensions.clear(); this->mapTwoBytesExtensions.clear();
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();
...@@ -722,8 +729,8 @@ namespace RTC ...@@ -722,8 +729,8 @@ namespace RTC
break; break;
} }
// Store the Two-Bytes extension element in a map. // Store the Two-Bytes extension element in the map.
this->twoBytesExtensions[id] = reinterpret_cast<TwoBytesExtension*>(ptr); this->mapTwoBytesExtensions[id] = reinterpret_cast<TwoBytesExtension*>(ptr);
ptr += (2 + len); ptr += (2 + len);
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论