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 <steveanton@webrtc.org>
Commit-Queue: Amit Hilbuch <amithi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26378}
This commit is contained in:
Amit Hilbuch 2019-01-23 14:54:24 -08:00 committed by Commit Bot
parent 6fde78cb2f
commit dbb49dfb27
8 changed files with 27 additions and 30 deletions

View File

@ -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" ]

View File

@ -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:";

View File

@ -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"

View File

@ -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<MediaStreamInterface> 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_;

View File

@ -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 += [

View File

@ -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 <limits>
#include <vector>
@ -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<uint32_t> known_ids)
UniqueRandomIdGenerator::UniqueRandomIdGenerator(ArrayView<uint32_t> 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<uint32_t>::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<std::string> known_ids) {
UniqueStringGenerator::UniqueStringGenerator(ArrayView<std::string> 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<uint32_t> int_value = rtc::StringToNumber<uint32_t>(value);
absl::optional<uint32_t> int_value = StringToNumber<uint32_t>(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

View File

@ -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 <limits>
#include <set>
@ -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<TIntegral> known_ids);
explicit UniqueNumberGenerator(ArrayView<TIntegral> 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<uint32_t> known_ids);
explicit UniqueRandomIdGenerator(ArrayView<uint32_t> 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<std::string> known_ids);
explicit UniqueStringGenerator(ArrayView<std::string> known_ids);
~UniqueStringGenerator();
std::string GenerateString();
@ -105,7 +105,7 @@ UniqueNumberGenerator<TIntegral>::UniqueNumberGenerator() : counter_(0) {}
template <typename TIntegral>
UniqueNumberGenerator<TIntegral>::UniqueNumberGenerator(
rtc::ArrayView<TIntegral> known_ids)
ArrayView<TIntegral> known_ids)
: counter_(0), known_ids_(known_ids.begin(), known_ids.end()) {}
template <typename TIntegral>
@ -126,6 +126,6 @@ template <typename TIntegral>
void UniqueNumberGenerator<TIntegral>::AddKnownId(TIntegral value) {
known_ids_.insert(value);
}
} // namespace webrtc
} // namespace rtc
#endif // PC_UNIQUE_ID_GENERATOR_H_
#endif // RTC_BASE_UNIQUE_ID_GENERATOR_H_

View File

@ -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 <typename Generator>
class UniqueIdGeneratorTest : public Test {};
@ -107,4 +107,4 @@ TYPED_TEST(UniqueIdGeneratorTest, AddedElementsAreNotGenerated) {
EXPECT_THAT(intersection, IsEmpty());
}
} // namespace webrtc
} // namespace rtc