| 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 | #ifndef PC_USED_IDS_H_ |
| 11 | #define PC_USED_IDS_H_ |
| 12 |
| 13 | #include <set> |
| 14 | #include <vector> |
| 15 |
| 16 | #include "api/rtp_parameters.h" |
| 17 | #include "media/base/codec.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
| 20 |
| 21 | namespace cricket { |
| 22 | template <typename IdStruct> |
| 23 | class UsedIds { |
| 24 | public: |
| 25 | UsedIds(int min_allowed_id, int max_allowed_id) |
| 26 | : min_allowed_id_(min_allowed_id), |
| 27 | max_allowed_id_(max_allowed_id), |
| 28 | next_id_(max_allowed_id) {} |
| 29 | virtual ~UsedIds() {} |
| 30 |
| 31 | // Loops through all Id in `ids` and changes its id if it is |
| 32 | // already in use by another IdStruct. Call this methods with all Id |
| 33 | // in a session description to make sure no duplicate ids exists. |
| 34 | // Note that typename Id must be a type of IdStruct. |
| 35 | template <typename Id> |
| 36 | void FindAndSetIdUsed(std::vector<Id>* ids) { |
| 37 | for (const Id& id : *ids) { |
| 38 | FindAndSetIdUsed(&id); |
| 39 | } |
| 40 | } |
| 41 |
| 42 | // Finds and sets an unused id if the `idstruct` id is already in use. |
| 43 | void FindAndSetIdUsed(IdStruct* idstruct) { |
| 44 | const int original_id = idstruct->id; |
| 45 | int new_id = idstruct->id; |
| 46 |
| 47 | if (original_id > max_allowed_id_ || original_id < min_allowed_id_) { |
| 48 | // If the original id is not in range - this is an id that can't be |
| 49 | // dynamically changed. |
| 50 | return; |
| 51 | } |
| 52 |
| 53 | if (IsIdUsed(original_id)) { |
| 54 | new_id = FindUnusedId(); |
| 55 | // Duplicate id found. Reassign from the original id to the new. |
| 56 | idstruct->id = new_id; |
| 57 | } |
| 58 | SetIdUsed(new_id); |
| 59 | } |
| 60 |
| 61 | protected: |
| 62 | virtual bool IsIdUsed(int new_id) { |
| 63 | return id_set_.find(new_id) != id_set_.end(); |
| 64 | } |
| 65 | const int min_allowed_id_; |
| 66 | const int max_allowed_id_; |
| 67 |
| 68 | private: |
| 69 | // Returns the first unused id in reverse order. |
| 70 | // This hopefully reduces the risk of more collisions. We want to change the |
| 71 | // default ids as little as possible. This function is virtual and can be |
| 72 | // overriden if the search for unused IDs should follow a specific pattern. |
| 73 | virtual int FindUnusedId() { |
| 74 | while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) { |
| 75 | --next_id_; |
| 76 | } |
| 77 | RTC_DCHECK(next_id_ >= min_allowed_id_); |
| 78 | return next_id_; |
| 79 | } |
| 80 |
| 81 | void SetIdUsed(int new_id) { |
| 82 | RTC_DCHECK(new_id >= min_allowed_id_); |
| 83 | RTC_DCHECK(new_id <= max_allowed_id_); |
| 84 | RTC_DCHECK(!IsIdUsed(new_id)); |
| 85 | id_set_.insert(new_id); |
| 86 | } |
| 87 | int next_id_; |
| 88 | std::set<int> id_set_; |
| 89 | }; |
| 90 |
| 91 | // Helper class used for finding duplicate RTP payload types among audio, video |
| 92 | // and data codecs. When bundle is used the payload types may not collide. |
| 93 | class UsedPayloadTypes : public UsedIds<Codec> { |
| 94 | public: |
| 95 | UsedPayloadTypes() |
| 96 | : UsedIds<Codec>(kFirstDynamicPayloadTypeLowerRange, |
| 97 | kLastDynamicPayloadTypeUpperRange) {} |
| 98 |
| 99 | // Check if a payload type is valid. The range [64-95] is forbidden |
| 100 | // when rtcp-mux is used. |
| 101 | static bool IsIdValid(Codec codec, bool rtcp_mux) { |
| 102 | if (rtcp_mux && (codec.id > kLastDynamicPayloadTypeLowerRange && |
| 103 | codec.id < kFirstDynamicPayloadTypeUpperRange)) { |
| 104 | return false; |
| 105 | } |
| 106 | return codec.id >= 0 && codec.id <= kLastDynamicPayloadTypeUpperRange; |
| 107 | } |
| 108 |
| 109 | protected: |
| 110 | bool IsIdUsed(int new_id) override { |
| 111 | // Range marked for RTCP avoidance is "used". |
| 112 | if (new_id > kLastDynamicPayloadTypeLowerRange && |
| 113 | new_id < kFirstDynamicPayloadTypeUpperRange) |
| 114 | return true; |
| 115 | return UsedIds<Codec>::IsIdUsed(new_id); |
| 116 | } |
| 117 |
| 118 | private: |
| 119 | static const int kFirstDynamicPayloadTypeLowerRange = 35; |
| 120 | static const int kLastDynamicPayloadTypeLowerRange = 63; |
| 121 |
| 122 | static const int kFirstDynamicPayloadTypeUpperRange = 96; |
| 123 | static const int kLastDynamicPayloadTypeUpperRange = 127; |
| 124 | }; |
| 125 |
| 126 | // Helper class used for finding duplicate RTP Header extension ids among |
| 127 | // audio and video extensions. |
| 128 | class UsedRtpHeaderExtensionIds : public UsedIds<webrtc::RtpExtension> { |
| 129 | public: |
| 130 | enum class IdDomain { |
| 131 | // Only allocate IDs that fit in one-byte header extensions. |
| 132 | kOneByteOnly, |
| 133 | // Prefer to allocate one-byte header extension IDs, but overflow to |
| 134 | // two-byte if none are left. |
| 135 | kTwoByteAllowed, |
| 136 | }; |
| 137 |
| 138 | explicit UsedRtpHeaderExtensionIds(IdDomain id_domain) |
| 139 | : UsedIds<webrtc::RtpExtension>( |
| 140 | webrtc::RtpExtension::kMinId, |
| 141 | id_domain == IdDomain::kTwoByteAllowed |
| 142 | ? webrtc::RtpExtension::kMaxId |
| 143 | : webrtc::RtpExtension::kOneByteHeaderExtensionMaxId), |
| 144 | id_domain_(id_domain), |
| 145 | next_extension_id_(webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) { |
| 146 | } |
| 147 |
| 148 | private: |
| 149 | // Returns the first unused id in reverse order from the max id of one byte |
| 150 | // header extensions. This hopefully reduces the risk of more collisions. We |
| 151 | // want to change the default ids as little as possible. If no unused id is |
| 152 | // found and two byte header extensions are enabled (i.e., |
| 153 | // `extmap_allow_mixed_` is true), search for unused ids from 16 to 255. |
| 154 | int FindUnusedId() override { |
| 155 | if (next_extension_id_ <= |
| 156 | webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) { |
| 157 | // First search in reverse order from the max id of one byte header |
| 158 | // extensions (14). |
| 159 | while (IsIdUsed(next_extension_id_) && |
| 160 | next_extension_id_ >= min_allowed_id_) { |
| 161 | --next_extension_id_; |
| 162 | } |
| 163 | } |
| 164 |
| 165 | if (id_domain_ == IdDomain::kTwoByteAllowed) { |
| 166 | if (next_extension_id_ < min_allowed_id_) { |
| 167 | // We have searched among all one-byte IDs without finding an unused ID, |
| 168 | // continue at the first two-byte ID (16; avoid 15 since it is somewhat |
| 169 | // special per https://www.rfc-editor.org/rfc/rfc8285#section-4.2 |
| 170 | next_extension_id_ = |
| 171 | webrtc::RtpExtension::kOneByteHeaderExtensionMaxId + 2; |
| 172 | } |
| 173 |
| 174 | if (next_extension_id_ > |
| 175 | webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) { |
| 176 | while (IsIdUsed(next_extension_id_) && |
| 177 | next_extension_id_ <= max_allowed_id_) { |
| 178 | ++next_extension_id_; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | RTC_DCHECK(next_extension_id_ >= min_allowed_id_); |
| 183 | RTC_DCHECK(next_extension_id_ <= max_allowed_id_); |
| 184 | return next_extension_id_; |
| 185 | } |
| 186 |
| 187 | const IdDomain id_domain_; |
| 188 | int next_extension_id_; |
| 189 | }; |
| 190 |
| 191 | } // namespace cricket |
| 192 |
| 193 | #endif // PC_USED_IDS_H_ |
| 194 |