Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: webrtc/modules/rtp_rtcp/source/rtp_header_extension.cc

Issue 2867713003: Remove hardcoded kValueSizeBytes values from variable-length header extensions. (Closed)
Patch Set: Patch 2 Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h" 11 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
12 12
13 #include "webrtc/base/arraysize.h" 13 #include "webrtc/base/arraysize.h"
14 #include "webrtc/base/checks.h" 14 #include "webrtc/base/checks.h"
15 #include "webrtc/base/logging.h" 15 #include "webrtc/base/logging.h"
16 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" 16 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h"
17 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" 17 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
18 18
19 namespace webrtc { 19 namespace webrtc {
20 namespace { 20 namespace {
21 21
22 using RtpUtility::Word32Align; 22 using RtpUtility::Word32Align;
23 23
24 struct ExtensionInfo { 24 struct ExtensionInfo {
25 RTPExtensionType type; 25 RTPExtensionType type;
26 size_t value_size;
27 const char* uri; 26 const char* uri;
28 }; 27 };
29 28
30 template <typename Extension> 29 template <typename Extension>
31 constexpr ExtensionInfo CreateExtensionInfo() { 30 constexpr ExtensionInfo CreateExtensionInfo() {
32 return {Extension::kId, Extension::kValueSizeBytes, Extension::kUri}; 31 return {Extension::kId, Extension::kUri};
33 } 32 }
34 33
35 constexpr ExtensionInfo kExtensions[] = { 34 constexpr ExtensionInfo kExtensions[] = {
36 CreateExtensionInfo<TransmissionOffset>(), 35 CreateExtensionInfo<TransmissionOffset>(),
37 CreateExtensionInfo<AudioLevel>(), 36 CreateExtensionInfo<AudioLevel>(),
38 CreateExtensionInfo<AbsoluteSendTime>(), 37 CreateExtensionInfo<AbsoluteSendTime>(),
39 CreateExtensionInfo<VideoOrientation>(), 38 CreateExtensionInfo<VideoOrientation>(),
40 CreateExtensionInfo<TransportSequenceNumber>(), 39 CreateExtensionInfo<TransportSequenceNumber>(),
41 CreateExtensionInfo<PlayoutDelayLimits>(), 40 CreateExtensionInfo<PlayoutDelayLimits>(),
42 CreateExtensionInfo<VideoContentTypeExtension>(), 41 CreateExtensionInfo<VideoContentTypeExtension>(),
43 CreateExtensionInfo<RtpStreamId>(), 42 CreateExtensionInfo<RtpStreamId>(),
44 CreateExtensionInfo<RepairedRtpStreamId>(), 43 CreateExtensionInfo<RepairedRtpStreamId>(),
45 }; 44 };
46 45
47 // Because of kRtpExtensionNone, NumberOfExtension is 1 bigger than the actual 46 // Because of kRtpExtensionNone, NumberOfExtension is 1 bigger than the actual
48 // number of known extensions. 47 // number of known extensions.
49 static_assert(arraysize(kExtensions) == 48 static_assert(arraysize(kExtensions) ==
50 static_cast<int>(kRtpExtensionNumberOfExtensions) - 1, 49 static_cast<int>(kRtpExtensionNumberOfExtensions) - 1,
51 "kExtensions expect to list all known extensions"); 50 "kExtensions expect to list all known extensions");
52 51
53 size_t ValueSize(RTPExtensionType type) {
54 for (const ExtensionInfo& extension : kExtensions)
55 if (type == extension.type)
56 return extension.value_size;
57
58 RTC_NOTREACHED();
59 return 0;
60 }
61
62 } // namespace 52 } // namespace
63 53
64 constexpr RTPExtensionType RtpHeaderExtensionMap::kInvalidType; 54 constexpr RTPExtensionType RtpHeaderExtensionMap::kInvalidType;
65 constexpr uint8_t RtpHeaderExtensionMap::kInvalidId; 55 constexpr uint8_t RtpHeaderExtensionMap::kInvalidId;
66 constexpr uint8_t RtpHeaderExtensionMap::kMinId; 56 constexpr uint8_t RtpHeaderExtensionMap::kMinId;
67 constexpr uint8_t RtpHeaderExtensionMap::kMaxId; 57 constexpr uint8_t RtpHeaderExtensionMap::kMaxId;
68 58
69 RtpHeaderExtensionMap::RtpHeaderExtensionMap() { 59 RtpHeaderExtensionMap::RtpHeaderExtensionMap() {
70 total_values_size_bytes_ = 0;
71 for (auto& type : types_) 60 for (auto& type : types_)
72 type = kInvalidType; 61 type = kInvalidType;
73 for (auto& id : ids_) 62 for (auto& id : ids_)
74 id = kInvalidId; 63 id = kInvalidId;
75 } 64 }
76 65
77 RtpHeaderExtensionMap::RtpHeaderExtensionMap( 66 RtpHeaderExtensionMap::RtpHeaderExtensionMap(
78 rtc::ArrayView<const RtpExtension> extensions) 67 rtc::ArrayView<const RtpExtension> extensions)
79 : RtpHeaderExtensionMap() { 68 : RtpHeaderExtensionMap() {
80 for (const RtpExtension& extension : extensions) 69 for (const RtpExtension& extension : extensions)
81 RegisterByUri(extension.id, extension.uri); 70 RegisterByUri(extension.id, extension.uri);
82 } 71 }
83 72
84 bool RtpHeaderExtensionMap::RegisterByType(uint8_t id, RTPExtensionType type) { 73 bool RtpHeaderExtensionMap::RegisterByType(uint8_t id, RTPExtensionType type) {
85 for (const ExtensionInfo& extension : kExtensions) 74 for (const ExtensionInfo& extension : kExtensions)
86 if (type == extension.type) 75 if (type == extension.type)
87 return Register(id, extension.type, extension.value_size, extension.uri); 76 return Register(id, extension.type, extension.uri);
88 RTC_NOTREACHED(); 77 RTC_NOTREACHED();
89 return false; 78 return false;
90 } 79 }
91 80
92 bool RtpHeaderExtensionMap::RegisterByUri(uint8_t id, const std::string& uri) { 81 bool RtpHeaderExtensionMap::RegisterByUri(uint8_t id, const std::string& uri) {
93 for (const ExtensionInfo& extension : kExtensions) 82 for (const ExtensionInfo& extension : kExtensions)
94 if (uri == extension.uri) 83 if (uri == extension.uri)
95 return Register(id, extension.type, extension.value_size, extension.uri); 84 return Register(id, extension.type, extension.uri);
96 LOG(LS_WARNING) << "Unknown extension uri:'" << uri 85 LOG(LS_WARNING) << "Unknown extension uri:'" << uri
97 << "', id: " << static_cast<int>(id) << '.'; 86 << "', id: " << static_cast<int>(id) << '.';
98 return false; 87 return false;
99 } 88 }
100 89
101 size_t RtpHeaderExtensionMap::GetTotalLengthInBytes() const { 90 size_t RtpHeaderExtensionMap::GetTotalLengthInBytes(
91 rtc::ArrayView<const RtpExtensionSize> extensions) const {
102 static constexpr size_t kRtpOneByteHeaderLength = 4; 92 static constexpr size_t kRtpOneByteHeaderLength = 4;
103 if (total_values_size_bytes_ == 0) 93 static constexpr size_t kExtensionSizeOverhead = 1;
danilchap 2017/05/09 14:45:22 may be kExtensionHeaderLength = 1 May be with so m
erikvarga1 2017/05/09 15:19:41 Oh, right, that one byte is not only the size but
94 size_t value_size = 0;
danilchap 2017/05/09 14:45:22 values_size (since it represent size of several va
erikvarga1 2017/05/09 15:19:41 Ah, I missed that 's' in the previous comment. Don
95 for (const RtpExtensionSize& extension : extensions) {
96 if (IsRegistered(extension.type))
97 value_size += extension.value_size + kExtensionSizeOverhead;
98 }
99 if (value_size == 0)
104 return 0; 100 return 0;
105 return Word32Align(kRtpOneByteHeaderLength + total_values_size_bytes_); 101 return Word32Align(kRtpOneByteHeaderLength + value_size);
106 } 102 }
107 103
108 int32_t RtpHeaderExtensionMap::Deregister(RTPExtensionType type) { 104 int32_t RtpHeaderExtensionMap::Deregister(RTPExtensionType type) {
109 if (IsRegistered(type)) { 105 if (IsRegistered(type)) {
110 uint8_t id = GetId(type); 106 uint8_t id = GetId(type);
111 total_values_size_bytes_ -= (ValueSize(type) + 1);
112 types_[id] = kInvalidType; 107 types_[id] = kInvalidType;
113 ids_[type] = kInvalidId; 108 ids_[type] = kInvalidId;
114 } 109 }
115 return 0; 110 return 0;
116 } 111 }
117 112
118 bool RtpHeaderExtensionMap::Register(uint8_t id, 113 bool RtpHeaderExtensionMap::Register(uint8_t id,
119 RTPExtensionType type, 114 RTPExtensionType type,
120 size_t value_size,
121 const char* uri) { 115 const char* uri) {
122 RTC_DCHECK_GT(type, kRtpExtensionNone); 116 RTC_DCHECK_GT(type, kRtpExtensionNone);
123 RTC_DCHECK_LT(type, kRtpExtensionNumberOfExtensions); 117 RTC_DCHECK_LT(type, kRtpExtensionNumberOfExtensions);
124 RTC_DCHECK_GE(value_size, 1U);
125 RTC_DCHECK_LE(value_size, 16U);
126 118
127 if (id < kMinId || id > kMaxId) { 119 if (id < kMinId || id > kMaxId) {
128 LOG(LS_WARNING) << "Failed to register extension uri:'" << uri 120 LOG(LS_WARNING) << "Failed to register extension uri:'" << uri
129 << "' with invalid id:" << static_cast<int>(id) << "."; 121 << "' with invalid id:" << static_cast<int>(id) << ".";
130 return false; 122 return false;
131 } 123 }
132 124
133 if (GetType(id) == type) { // Same type/id pair already registered. 125 if (GetType(id) == type) { // Same type/id pair already registered.
134 LOG(LS_VERBOSE) << "Reregistering extension uri:'" << uri 126 LOG(LS_VERBOSE) << "Reregistering extension uri:'" << uri
135 << "', id:" << static_cast<int>(id); 127 << "', id:" << static_cast<int>(id);
136 return true; 128 return true;
137 } 129 }
138 130
139 if (GetType(id) != kInvalidType) { // |id| used by another extension type. 131 if (GetType(id) != kInvalidType) { // |id| used by another extension type.
140 LOG(LS_WARNING) << "Failed to register extension uri:'" << uri 132 LOG(LS_WARNING) << "Failed to register extension uri:'" << uri
141 << "', id:" << static_cast<int>(id) 133 << "', id:" << static_cast<int>(id)
142 << ". Id already in use by extension type " 134 << ". Id already in use by extension type "
143 << static_cast<int>(GetType(id)); 135 << static_cast<int>(GetType(id));
144 return false; 136 return false;
145 } 137 }
146 RTC_DCHECK(!IsRegistered(type)); 138 RTC_DCHECK(!IsRegistered(type));
147 139
148 types_[id] = type; 140 types_[id] = type;
149 ids_[type] = id; 141 ids_[type] = id;
150 total_values_size_bytes_ += (value_size + 1);
151 return true; 142 return true;
152 } 143 }
153 144
154 } // namespace webrtc 145 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698