xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/rtp_header_extension_map.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
12 
13 #include "absl/strings/string_view.h"
14 #include "modules/rtp_rtcp/source/rtp_dependency_descriptor_extension.h"
15 #include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
16 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
17 #include "modules/rtp_rtcp/source/rtp_video_layers_allocation_extension.h"
18 #include "rtc_base/arraysize.h"
19 #include "rtc_base/checks.h"
20 #include "rtc_base/logging.h"
21 
22 namespace webrtc {
23 namespace {
24 
25 struct ExtensionInfo {
26   RTPExtensionType type;
27   absl::string_view uri;
28 };
29 
30 template <typename Extension>
CreateExtensionInfo()31 constexpr ExtensionInfo CreateExtensionInfo() {
32   return {Extension::kId, Extension::Uri()};
33 }
34 
35 constexpr ExtensionInfo kExtensions[] = {
36     CreateExtensionInfo<TransmissionOffset>(),
37     CreateExtensionInfo<AudioLevel>(),
38     CreateExtensionInfo<CsrcAudioLevel>(),
39     CreateExtensionInfo<AbsoluteSendTime>(),
40     CreateExtensionInfo<AbsoluteCaptureTimeExtension>(),
41     CreateExtensionInfo<VideoOrientation>(),
42     CreateExtensionInfo<TransportSequenceNumber>(),
43     CreateExtensionInfo<TransportSequenceNumberV2>(),
44     CreateExtensionInfo<PlayoutDelayLimits>(),
45     CreateExtensionInfo<VideoContentTypeExtension>(),
46     CreateExtensionInfo<RtpVideoLayersAllocationExtension>(),
47     CreateExtensionInfo<VideoTimingExtension>(),
48     CreateExtensionInfo<RtpStreamId>(),
49     CreateExtensionInfo<RepairedRtpStreamId>(),
50     CreateExtensionInfo<RtpMid>(),
51     CreateExtensionInfo<RtpGenericFrameDescriptorExtension00>(),
52     CreateExtensionInfo<RtpDependencyDescriptorExtension>(),
53     CreateExtensionInfo<ColorSpaceExtension>(),
54     CreateExtensionInfo<InbandComfortNoiseExtension>(),
55     CreateExtensionInfo<VideoFrameTrackingIdExtension>(),
56 };
57 
58 // Because of kRtpExtensionNone, NumberOfExtension is 1 bigger than the actual
59 // number of known extensions.
60 static_assert(arraysize(kExtensions) ==
61                   static_cast<int>(kRtpExtensionNumberOfExtensions) - 1,
62               "kExtensions expect to list all known extensions");
63 
64 }  // namespace
65 
66 constexpr RTPExtensionType RtpHeaderExtensionMap::kInvalidType;
67 constexpr int RtpHeaderExtensionMap::kInvalidId;
68 
RtpHeaderExtensionMap()69 RtpHeaderExtensionMap::RtpHeaderExtensionMap() : RtpHeaderExtensionMap(false) {}
70 
RtpHeaderExtensionMap(bool extmap_allow_mixed)71 RtpHeaderExtensionMap::RtpHeaderExtensionMap(bool extmap_allow_mixed)
72     : extmap_allow_mixed_(extmap_allow_mixed) {
73   for (auto& id : ids_)
74     id = kInvalidId;
75 }
76 
RtpHeaderExtensionMap(rtc::ArrayView<const RtpExtension> extensions)77 RtpHeaderExtensionMap::RtpHeaderExtensionMap(
78     rtc::ArrayView<const RtpExtension> extensions)
79     : RtpHeaderExtensionMap(false) {
80   for (const RtpExtension& extension : extensions)
81     RegisterByUri(extension.id, extension.uri);
82 }
83 
Reset(rtc::ArrayView<const RtpExtension> extensions)84 void RtpHeaderExtensionMap::Reset(
85     rtc::ArrayView<const RtpExtension> extensions) {
86   for (auto& id : ids_)
87     id = kInvalidId;
88   for (const RtpExtension& extension : extensions)
89     RegisterByUri(extension.id, extension.uri);
90 }
91 
RegisterByType(int id,RTPExtensionType type)92 bool RtpHeaderExtensionMap::RegisterByType(int id, RTPExtensionType type) {
93   for (const ExtensionInfo& extension : kExtensions)
94     if (type == extension.type)
95       return Register(id, extension.type, extension.uri);
96   RTC_DCHECK_NOTREACHED();
97   return false;
98 }
99 
RegisterByUri(int id,absl::string_view uri)100 bool RtpHeaderExtensionMap::RegisterByUri(int id, absl::string_view uri) {
101   for (const ExtensionInfo& extension : kExtensions)
102     if (uri == extension.uri)
103       return Register(id, extension.type, extension.uri);
104   RTC_LOG(LS_WARNING) << "Unknown extension uri:'" << uri << "', id: " << id
105                       << '.';
106   return false;
107 }
108 
GetType(int id) const109 RTPExtensionType RtpHeaderExtensionMap::GetType(int id) const {
110   RTC_DCHECK_GE(id, RtpExtension::kMinId);
111   RTC_DCHECK_LE(id, RtpExtension::kMaxId);
112   for (int type = kRtpExtensionNone + 1; type < kRtpExtensionNumberOfExtensions;
113        ++type) {
114     if (ids_[type] == id) {
115       return static_cast<RTPExtensionType>(type);
116     }
117   }
118   return kInvalidType;
119 }
120 
Deregister(absl::string_view uri)121 void RtpHeaderExtensionMap::Deregister(absl::string_view uri) {
122   for (const ExtensionInfo& extension : kExtensions) {
123     if (extension.uri == uri) {
124       ids_[extension.type] = kInvalidId;
125       break;
126     }
127   }
128 }
129 
Register(int id,RTPExtensionType type,absl::string_view uri)130 bool RtpHeaderExtensionMap::Register(int id,
131                                      RTPExtensionType type,
132                                      absl::string_view uri) {
133   RTC_DCHECK_GT(type, kRtpExtensionNone);
134   RTC_DCHECK_LT(type, kRtpExtensionNumberOfExtensions);
135 
136   if (id < RtpExtension::kMinId || id > RtpExtension::kMaxId) {
137     RTC_LOG(LS_WARNING) << "Failed to register extension uri:'" << uri
138                         << "' with invalid id:" << id << ".";
139     return false;
140   }
141 
142   RTPExtensionType registered_type = GetType(id);
143   if (registered_type == type) {  // Same type/id pair already registered.
144     RTC_LOG(LS_VERBOSE) << "Reregistering extension uri:'" << uri
145                         << "', id:" << id;
146     return true;
147   }
148 
149   if (registered_type !=
150       kInvalidType) {  // `id` used by another extension type.
151     RTC_LOG(LS_WARNING) << "Failed to register extension uri:'" << uri
152                         << "', id:" << id
153                         << ". Id already in use by extension type "
154                         << static_cast<int>(registered_type);
155     return false;
156   }
157   if (IsRegistered(type)) {
158     RTC_LOG(LS_WARNING) << "Illegal reregistration for uri: " << uri
159                         << " is previously registered with id " << GetId(type)
160                         << " and cannot be reregistered with id " << id;
161     return false;
162   }
163 
164   // There is a run-time check above id fits into uint8_t.
165   ids_[type] = static_cast<uint8_t>(id);
166   return true;
167 }
168 
169 }  // namespace webrtc
170