Refactor to remove direct memory dependency on kMaxId

When two-byte header extensions are enabled, kMaxId will change from 15
to 255. This CL is a refactor to remove the direct dependency between
memory allocation and kMaxId.

Bug: webrtc:7990
Change-Id: I38974a9c705eb6a0fdba9038a7d909861587d98d
Reviewed-on: https://webrtc-review.googlesource.com/101580
Commit-Queue: Johannes Kron <kron@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24801}
This commit is contained in:
Johannes Kron
2018-09-24 14:50:48 +02:00
committed by Commit Bot
parent 3a9731ff2f
commit c5744b8b21
5 changed files with 89 additions and 58 deletions

View File

@ -59,8 +59,6 @@ constexpr int RtpHeaderExtensionMap::kMinId;
constexpr int RtpHeaderExtensionMap::kMaxId;
RtpHeaderExtensionMap::RtpHeaderExtensionMap() {
for (auto& type : types_)
type = kInvalidType;
for (auto& id : ids_)
id = kInvalidId;
}
@ -89,6 +87,18 @@ bool RtpHeaderExtensionMap::RegisterByUri(int id, const std::string& uri) {
return false;
}
RTPExtensionType RtpHeaderExtensionMap::GetType(int id) const {
RTC_DCHECK_GE(id, kMinId);
RTC_DCHECK_LE(id, kMaxId);
for (int type = kRtpExtensionNone + 1; type < kRtpExtensionNumberOfExtensions;
++type) {
if (ids_[type] == id) {
return static_cast<RTPExtensionType>(type);
}
}
return kInvalidType;
}
size_t RtpHeaderExtensionMap::GetTotalLengthInBytes(
rtc::ArrayView<const RtpExtensionSize> extensions) const {
// Header size of the extension block, see RFC3550 Section 5.3.1
@ -110,8 +120,6 @@ size_t RtpHeaderExtensionMap::GetTotalLengthInBytes(
int32_t RtpHeaderExtensionMap::Deregister(RTPExtensionType type) {
if (IsRegistered(type)) {
uint8_t id = GetId(type);
types_[id] = kInvalidType;
ids_[type] = kInvalidId;
}
return 0;
@ -129,22 +137,23 @@ bool RtpHeaderExtensionMap::Register(int id,
return false;
}
if (GetType(id) == type) { // Same type/id pair already registered.
RTPExtensionType registered_type = GetType(id);
if (registered_type == type) { // Same type/id pair already registered.
RTC_LOG(LS_VERBOSE) << "Reregistering extension uri:'" << uri
<< "', id:" << id;
return true;
}
if (GetType(id) != kInvalidType) { // |id| used by another extension type.
if (registered_type !=
kInvalidType) { // |id| used by another extension type.
RTC_LOG(LS_WARNING) << "Failed to register extension uri:'" << uri
<< "', id:" << id
<< ". Id already in use by extension type "
<< static_cast<int>(GetType(id));
<< static_cast<int>(registered_type);
return false;
}
RTC_DCHECK(!IsRegistered(type));
types_[id] = type;
// There is a run-time check above id fits into uint8_t.
ids_[type] = static_cast<uint8_t>(id);
return true;