OLD | NEW |
---|---|
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 size_t total = 0; |
danilchap
2017/05/09 12:33:49
may be values_size better describes this variable
erikvarga1
2017/05/09 13:30:30
Done.
| |
94 for (const RtpExtensionSize& extension : extensions) { | |
95 if (ids_[extension.type] != kInvalidId) | |
danilchap
2017/05/09 12:33:49
if (IsRegistred(extension.type))
looks more readab
erikvarga1
2017/05/09 13:30:30
Done.
| |
96 total += extension.value_size + 1; | |
danilchap
2017/05/09 12:33:49
add a comment or constant for + 1
erikvarga1
2017/05/09 13:30:30
Done.
| |
97 } | |
98 if (total == 0) | |
104 return 0; | 99 return 0; |
105 return Word32Align(kRtpOneByteHeaderLength + total_values_size_bytes_); | 100 return Word32Align(kRtpOneByteHeaderLength + total); |
106 } | 101 } |
107 | 102 |
108 int32_t RtpHeaderExtensionMap::Deregister(RTPExtensionType type) { | 103 int32_t RtpHeaderExtensionMap::Deregister(RTPExtensionType type) { |
109 if (IsRegistered(type)) { | 104 if (IsRegistered(type)) { |
110 uint8_t id = GetId(type); | 105 uint8_t id = GetId(type); |
111 total_values_size_bytes_ -= (ValueSize(type) + 1); | |
112 types_[id] = kInvalidType; | 106 types_[id] = kInvalidType; |
113 ids_[type] = kInvalidId; | 107 ids_[type] = kInvalidId; |
114 } | 108 } |
115 return 0; | 109 return 0; |
116 } | 110 } |
117 | 111 |
118 bool RtpHeaderExtensionMap::Register(uint8_t id, | 112 bool RtpHeaderExtensionMap::Register(uint8_t id, |
119 RTPExtensionType type, | 113 RTPExtensionType type, |
120 size_t value_size, | |
121 const char* uri) { | 114 const char* uri) { |
122 RTC_DCHECK_GT(type, kRtpExtensionNone); | 115 RTC_DCHECK_GT(type, kRtpExtensionNone); |
123 RTC_DCHECK_LT(type, kRtpExtensionNumberOfExtensions); | 116 RTC_DCHECK_LT(type, kRtpExtensionNumberOfExtensions); |
124 RTC_DCHECK_GE(value_size, 1U); | |
125 RTC_DCHECK_LE(value_size, 16U); | |
126 | 117 |
127 if (id < kMinId || id > kMaxId) { | 118 if (id < kMinId || id > kMaxId) { |
128 LOG(LS_WARNING) << "Failed to register extension uri:'" << uri | 119 LOG(LS_WARNING) << "Failed to register extension uri:'" << uri |
129 << "' with invalid id:" << static_cast<int>(id) << "."; | 120 << "' with invalid id:" << static_cast<int>(id) << "."; |
130 return false; | 121 return false; |
131 } | 122 } |
132 | 123 |
133 if (GetType(id) == type) { // Same type/id pair already registered. | 124 if (GetType(id) == type) { // Same type/id pair already registered. |
134 LOG(LS_VERBOSE) << "Reregistering extension uri:'" << uri | 125 LOG(LS_VERBOSE) << "Reregistering extension uri:'" << uri |
135 << "', id:" << static_cast<int>(id); | 126 << "', id:" << static_cast<int>(id); |
136 return true; | 127 return true; |
137 } | 128 } |
138 | 129 |
139 if (GetType(id) != kInvalidType) { // |id| used by another extension type. | 130 if (GetType(id) != kInvalidType) { // |id| used by another extension type. |
140 LOG(LS_WARNING) << "Failed to register extension uri:'" << uri | 131 LOG(LS_WARNING) << "Failed to register extension uri:'" << uri |
141 << "', id:" << static_cast<int>(id) | 132 << "', id:" << static_cast<int>(id) |
142 << ". Id already in use by extension type " | 133 << ". Id already in use by extension type " |
143 << static_cast<int>(GetType(id)); | 134 << static_cast<int>(GetType(id)); |
144 return false; | 135 return false; |
145 } | 136 } |
146 RTC_DCHECK(!IsRegistered(type)); | 137 RTC_DCHECK(!IsRegistered(type)); |
147 | 138 |
148 types_[id] = type; | 139 types_[id] = type; |
149 ids_[type] = id; | 140 ids_[type] = id; |
150 total_values_size_bytes_ += (value_size + 1); | |
151 return true; | 141 return true; |
152 } | 142 } |
153 | 143 |
154 } // namespace webrtc | 144 } // namespace webrtc |
OLD | NEW |