From dbb49dfb270647ef141e094d348b3d32a345818e Mon Sep 17 00:00:00 2001 From: Amit Hilbuch Date: Wed, 23 Jan 2019 14:54:24 -0800 Subject: [PATCH] Moving UniqueIdGenerator into rtc_base. UniqueIdGenerator classes are useful outside the pc directory. This change moves them to the rtc_base directory to enable code in all directories to reference them. Bug: None Change-Id: I1c77da87ea26d9611f37dc1d4d2c16006a6589c6 Reviewed-on: https://webrtc-review.googlesource.com/c/119460 Reviewed-by: Steve Anton Commit-Queue: Amit Hilbuch Cr-Commit-Position: refs/heads/master@{#26378} --- pc/BUILD.gn | 3 --- pc/media_session.cc | 4 ++-- pc/peer_connection.cc | 1 - pc/peer_connection.h | 4 ++-- rtc_base/BUILD.gn | 3 +++ {pc => rtc_base}/unique_id_generator.cc | 18 ++++++++---------- {pc => rtc_base}/unique_id_generator.h | 18 +++++++++--------- .../unique_id_generator_unittest.cc | 6 +++--- 8 files changed, 27 insertions(+), 30 deletions(-) rename {pc => rtc_base}/unique_id_generator.cc (77%) rename {pc => rtc_base}/unique_id_generator.h (91%) rename {pc => rtc_base}/unique_id_generator_unittest.cc (97%) diff --git a/pc/BUILD.gn b/pc/BUILD.gn index f8fd591aa4..ce4e6dc5e9 100644 --- a/pc/BUILD.gn +++ b/pc/BUILD.gn @@ -85,8 +85,6 @@ rtc_static_library("rtc_pc_base") { "transport_stats.cc", "transport_stats.h", "transportstats.h", - "unique_id_generator.cc", - "unique_id_generator.h", ] deps = [ @@ -305,7 +303,6 @@ if (rtc_include_tests) { "srtptestutil.h", "test/rtp_transport_test_util.h", "test/srtp_test_util.h", - "unique_id_generator_unittest.cc", ] include_dirs = [ "//third_party/libsrtp/srtp" ] diff --git a/pc/media_session.cc b/pc/media_session.cc index 9f3a7df1e1..3c98117d71 100644 --- a/pc/media_session.cc +++ b/pc/media_session.cc @@ -28,16 +28,16 @@ #include "pc/channel_manager.h" #include "pc/rtp_media_utils.h" #include "pc/srtp_filter.h" -#include "pc/unique_id_generator.h" #include "rtc_base/checks.h" #include "rtc_base/helpers.h" #include "rtc_base/logging.h" #include "rtc_base/third_party/base64/base64.h" +#include "rtc_base/unique_id_generator.h" namespace { +using rtc::UniqueRandomIdGenerator; using webrtc::RtpTransceiverDirection; -using webrtc::UniqueRandomIdGenerator; const char kInline[] = "inline:"; diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc index 0cbd3153f7..4cdab68474 100644 --- a/pc/peer_connection.cc +++ b/pc/peer_connection.cc @@ -42,7 +42,6 @@ #include "pc/sctp_utils.h" #include "pc/sdp_utils.h" #include "pc/stream_collection.h" -#include "pc/unique_id_generator.h" #include "pc/video_capturer_track_source.h" #include "pc/video_track.h" #include "rtc_base/bind.h" diff --git a/pc/peer_connection.h b/pc/peer_connection.h index ca92d76e11..dde19bacfa 100644 --- a/pc/peer_connection.h +++ b/pc/peer_connection.h @@ -28,8 +28,8 @@ #include "pc/rtp_transceiver.h" #include "pc/stats_collector.h" #include "pc/stream_collection.h" -#include "pc/unique_id_generator.h" #include "pc/webrtc_session_description_factory.h" +#include "rtc_base/unique_id_generator.h" namespace webrtc { @@ -1062,7 +1062,7 @@ class PeerConnection : public PeerConnectionInternal, rtc::scoped_refptr missing_msid_default_stream_; // MIDs will be generated using this generator which will keep track of // all the MIDs that have been seen over the life of the PeerConnection. - UniqueStringGenerator mid_generator_; + rtc::UniqueStringGenerator mid_generator_; SessionError session_error_ = SessionError::kNone; std::string session_error_desc_; diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index ceb856cddd..e38da1f1d8 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -1028,6 +1028,8 @@ rtc_static_library("rtc_base") { "stream.h", "thread.cc", "thread.h", + "unique_id_generator.cc", + "unique_id_generator.h", ] if (build_with_chromium) { @@ -1519,6 +1521,7 @@ if (rtc_include_tests) { "stream_unittest.cc", "test_client_unittest.cc", "thread_unittest.cc", + "unique_id_generator_unittest.cc", ] if (is_win) { sources += [ diff --git a/pc/unique_id_generator.cc b/rtc_base/unique_id_generator.cc similarity index 77% rename from pc/unique_id_generator.cc rename to rtc_base/unique_id_generator.cc index 6b508f1f4b..62cd068f11 100644 --- a/pc/unique_id_generator.cc +++ b/rtc_base/unique_id_generator.cc @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "pc/unique_id_generator.h" +#include "rtc_base/unique_id_generator.h" #include #include @@ -17,11 +17,10 @@ #include "rtc_base/string_encode.h" #include "rtc_base/string_to_number.h" -namespace webrtc { +namespace rtc { UniqueRandomIdGenerator::UniqueRandomIdGenerator() : known_ids_() {} -UniqueRandomIdGenerator::UniqueRandomIdGenerator( - rtc::ArrayView known_ids) +UniqueRandomIdGenerator::UniqueRandomIdGenerator(ArrayView known_ids) : known_ids_(known_ids.begin(), known_ids.end()) {} UniqueRandomIdGenerator::~UniqueRandomIdGenerator() = default; @@ -29,7 +28,7 @@ UniqueRandomIdGenerator::~UniqueRandomIdGenerator() = default; uint32_t UniqueRandomIdGenerator::GenerateId() { while (true) { RTC_CHECK_LT(known_ids_.size(), std::numeric_limits::max()); - auto pair = known_ids_.insert(rtc::CreateRandomNonZeroId()); + auto pair = known_ids_.insert(CreateRandomNonZeroId()); if (pair.second) { return *pair.first; } @@ -41,8 +40,7 @@ void UniqueRandomIdGenerator::AddKnownId(uint32_t value) { } UniqueStringGenerator::UniqueStringGenerator() : unique_number_generator_() {} -UniqueStringGenerator::UniqueStringGenerator( - rtc::ArrayView known_ids) { +UniqueStringGenerator::UniqueStringGenerator(ArrayView known_ids) { for (const std::string& str : known_ids) { AddKnownId(str); } @@ -51,11 +49,11 @@ UniqueStringGenerator::UniqueStringGenerator( UniqueStringGenerator::~UniqueStringGenerator() = default; std::string UniqueStringGenerator::GenerateString() { - return rtc::ToString(unique_number_generator_.GenerateNumber()); + return ToString(unique_number_generator_.GenerateNumber()); } void UniqueStringGenerator::AddKnownId(const std::string& value) { - absl::optional int_value = rtc::StringToNumber(value); + absl::optional int_value = StringToNumber(value); // The underlying generator works for uint32_t values, so if the provided // value is not a uint32_t it will never be generated anyway. if (int_value.has_value()) { @@ -63,4 +61,4 @@ void UniqueStringGenerator::AddKnownId(const std::string& value) { } } -} // namespace webrtc +} // namespace rtc diff --git a/pc/unique_id_generator.h b/rtc_base/unique_id_generator.h similarity index 91% rename from pc/unique_id_generator.h rename to rtc_base/unique_id_generator.h index cd6e085db0..849daa9758 100644 --- a/pc/unique_id_generator.h +++ b/rtc_base/unique_id_generator.h @@ -8,8 +8,8 @@ * be found in the AUTHORS file in the root of the source tree. */ -#ifndef PC_UNIQUE_ID_GENERATOR_H_ -#define PC_UNIQUE_ID_GENERATOR_H_ +#ifndef RTC_BASE_UNIQUE_ID_GENERATOR_H_ +#define RTC_BASE_UNIQUE_ID_GENERATOR_H_ #include #include @@ -17,7 +17,7 @@ #include "api/array_view.h" -namespace webrtc { +namespace rtc { // This class will generate numbers. A common use case is for identifiers. // The generated numbers will be unique, in the local scope of the generator. @@ -33,7 +33,7 @@ class UniqueNumberGenerator { typedef TIntegral value_type; UniqueNumberGenerator(); // Creates a generator that will never return any value from the given list. - explicit UniqueNumberGenerator(rtc::ArrayView known_ids); + explicit UniqueNumberGenerator(ArrayView known_ids); ~UniqueNumberGenerator(); // Generates a number that this generator has never produced before. @@ -61,7 +61,7 @@ class UniqueRandomIdGenerator { typedef uint32_t value_type; UniqueRandomIdGenerator(); // Create a generator that will never return any value from the given list. - explicit UniqueRandomIdGenerator(rtc::ArrayView known_ids); + explicit UniqueRandomIdGenerator(ArrayView known_ids); ~UniqueRandomIdGenerator(); // Generates a random id that this generator has never produced before. @@ -86,7 +86,7 @@ class UniqueStringGenerator { public: typedef std::string value_type; UniqueStringGenerator(); - explicit UniqueStringGenerator(rtc::ArrayView known_ids); + explicit UniqueStringGenerator(ArrayView known_ids); ~UniqueStringGenerator(); std::string GenerateString(); @@ -105,7 +105,7 @@ UniqueNumberGenerator::UniqueNumberGenerator() : counter_(0) {} template UniqueNumberGenerator::UniqueNumberGenerator( - rtc::ArrayView known_ids) + ArrayView known_ids) : counter_(0), known_ids_(known_ids.begin(), known_ids.end()) {} template @@ -126,6 +126,6 @@ template void UniqueNumberGenerator::AddKnownId(TIntegral value) { known_ids_.insert(value); } -} // namespace webrtc +} // namespace rtc -#endif // PC_UNIQUE_ID_GENERATOR_H_ +#endif // RTC_BASE_UNIQUE_ID_GENERATOR_H_ diff --git a/pc/unique_id_generator_unittest.cc b/rtc_base/unique_id_generator_unittest.cc similarity index 97% rename from pc/unique_id_generator_unittest.cc rename to rtc_base/unique_id_generator_unittest.cc index f3cba7675b..5b6ce4d42a 100644 --- a/pc/unique_id_generator_unittest.cc +++ b/rtc_base/unique_id_generator_unittest.cc @@ -13,15 +13,15 @@ #include "vector" #include "api/array_view.h" -#include "pc/unique_id_generator.h" #include "rtc_base/gunit.h" #include "rtc_base/helpers.h" +#include "rtc_base/unique_id_generator.h" #include "test/gmock.h" using ::testing::IsEmpty; using ::testing::Test; -namespace webrtc { +namespace rtc { template class UniqueIdGeneratorTest : public Test {}; @@ -107,4 +107,4 @@ TYPED_TEST(UniqueIdGeneratorTest, AddedElementsAreNotGenerated) { EXPECT_THAT(intersection, IsEmpty()); } -} // namespace webrtc +} // namespace rtc