123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- #ifndef PC_USED_IDS_H_
- #define PC_USED_IDS_H_
- #include <set>
- #include <vector>
- #include "api/rtp_parameters.h"
- #include "media/base/codec.h"
- #include "rtc_base/checks.h"
- #include "rtc_base/logging.h"
- namespace cricket {
- template <typename IdStruct>
- class UsedIds {
- public:
- UsedIds(int min_allowed_id, int max_allowed_id)
- : min_allowed_id_(min_allowed_id),
- max_allowed_id_(max_allowed_id),
- next_id_(max_allowed_id) {}
- virtual ~UsedIds() {}
-
-
-
-
- template <typename Id>
- void FindAndSetIdUsed(std::vector<Id>* ids) {
- for (const Id& id : *ids) {
- FindAndSetIdUsed(&id);
- }
- }
-
- void FindAndSetIdUsed(IdStruct* idstruct) {
- const int original_id = idstruct->id;
- int new_id = idstruct->id;
- if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
-
-
- return;
- }
- if (IsIdUsed(original_id)) {
- new_id = FindUnusedId();
- RTC_LOG(LS_WARNING) << "Duplicate id found. Reassigning from "
- << original_id << " to " << new_id;
- idstruct->id = new_id;
- }
- SetIdUsed(new_id);
- }
- protected:
- bool IsIdUsed(int new_id) { return id_set_.find(new_id) != id_set_.end(); }
- const int min_allowed_id_;
- const int max_allowed_id_;
- private:
-
-
-
-
- virtual int FindUnusedId() {
- while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) {
- --next_id_;
- }
- RTC_DCHECK(next_id_ >= min_allowed_id_);
- return next_id_;
- }
- void SetIdUsed(int new_id) {
- RTC_DCHECK(new_id >= min_allowed_id_);
- RTC_DCHECK(new_id <= max_allowed_id_);
- RTC_DCHECK(!IsIdUsed(new_id));
- id_set_.insert(new_id);
- }
- int next_id_;
- std::set<int> id_set_;
- };
- class UsedPayloadTypes : public UsedIds<Codec> {
- public:
- UsedPayloadTypes()
- : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {}
- private:
- static const int kDynamicPayloadTypeMin = 96;
- static const int kDynamicPayloadTypeMax = 127;
- };
- class UsedRtpHeaderExtensionIds : public UsedIds<webrtc::RtpExtension> {
- public:
- enum class IdDomain {
-
- kOneByteOnly,
-
-
- kTwoByteAllowed,
- };
- explicit UsedRtpHeaderExtensionIds(IdDomain id_domain)
- : UsedIds<webrtc::RtpExtension>(
- webrtc::RtpExtension::kMinId,
- id_domain == IdDomain::kTwoByteAllowed
- ? webrtc::RtpExtension::kMaxId
- : webrtc::RtpExtension::kOneByteHeaderExtensionMaxId),
- id_domain_(id_domain),
- next_extension_id_(webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) {
- }
- private:
-
-
-
-
-
- int FindUnusedId() override {
- if (next_extension_id_ <=
- webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) {
-
-
- while (IsIdUsed(next_extension_id_) &&
- next_extension_id_ >= min_allowed_id_) {
- --next_extension_id_;
- }
- }
- if (id_domain_ == IdDomain::kTwoByteAllowed) {
- if (next_extension_id_ < min_allowed_id_) {
-
-
- next_extension_id_ =
- webrtc::RtpExtension::kOneByteHeaderExtensionMaxId + 1;
- }
- if (next_extension_id_ >
- webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) {
- while (IsIdUsed(next_extension_id_) &&
- next_extension_id_ <= max_allowed_id_) {
- ++next_extension_id_;
- }
- }
- }
- RTC_DCHECK(next_extension_id_ >= min_allowed_id_);
- RTC_DCHECK(next_extension_id_ <= max_allowed_id_);
- return next_extension_id_;
- }
- const IdDomain id_domain_;
- int next_extension_id_;
- };
- }
- #endif
|