diff --git a/media/base/mediachannel.h b/media/base/mediachannel.h index d6205c2154..a5831ff6e7 100644 --- a/media/base/mediachannel.h +++ b/media/base/mediachannel.h @@ -46,7 +46,6 @@ namespace rtc { -class RateLimiter; class Timing; } diff --git a/media/base/rtpdataengine.cc b/media/base/rtpdataengine.cc index 191645b28a..3aebe84584 100644 --- a/media/base/rtpdataengine.cc +++ b/media/base/rtpdataengine.cc @@ -17,9 +17,9 @@ #include "media/base/rtputils.h" #include "media/base/streamparams.h" #include "rtc_base/copyonwritebuffer.h" +#include "rtc_base/data_rate_limiter.h" #include "rtc_base/helpers.h" #include "rtc_base/logging.h" -#include "rtc_base/ratelimiter.h" #include "rtc_base/sanitizer.h" #include "rtc_base/stringutils.h" @@ -64,7 +64,7 @@ RtpDataMediaChannel::RtpDataMediaChannel(const MediaConfig& config) void RtpDataMediaChannel::Construct() { sending_ = false; receiving_ = false; - send_limiter_.reset(new rtc::RateLimiter(kDataMaxBandwidth / 8, 1.0)); + send_limiter_.reset(new rtc::DataRateLimiter(kDataMaxBandwidth / 8, 1.0)); } @@ -248,7 +248,7 @@ bool RtpDataMediaChannel::SetMaxSendBandwidth(int bps) { if (bps <= 0) { bps = kDataMaxBandwidth; } - send_limiter_.reset(new rtc::RateLimiter(bps / 8, 1.0)); + send_limiter_.reset(new rtc::DataRateLimiter(bps / 8, 1.0)); RTC_LOG(LS_INFO) << "RtpDataMediaChannel::SetSendBandwidth to " << bps << "bps."; return true; diff --git a/media/base/rtpdataengine.h b/media/base/rtpdataengine.h index 64e083b0fd..52d43253dc 100644 --- a/media/base/rtpdataengine.h +++ b/media/base/rtpdataengine.h @@ -20,6 +20,10 @@ #include "media/base/mediaconstants.h" #include "media/base/mediaengine.h" +namespace rtc { +class DataRateLimiter; +} + namespace cricket { struct DataCodec; @@ -103,7 +107,7 @@ class RtpDataMediaChannel : public DataMediaChannel { std::vector send_streams_; std::vector recv_streams_; std::map rtp_clock_by_send_ssrc_; - std::unique_ptr send_limiter_; + std::unique_ptr send_limiter_; }; } // namespace cricket diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index df4969fdd9..50c5c68fa1 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -585,6 +585,8 @@ rtc_static_library("rtc_base_generic") { "crc32.h", "cryptstring.cc", "cryptstring.h", + "data_rate_limiter.cc", + "data_rate_limiter.h", "dscp.h", "filerotatingstream.cc", "filerotatingstream.h", @@ -630,7 +632,6 @@ rtc_static_library("rtc_base_generic") { "physicalsocketserver.h", "proxyinfo.cc", "proxyinfo.h", - "ratelimiter.cc", "ratelimiter.h", "rtccertificate.cc", "rtccertificate.h", @@ -1085,6 +1086,7 @@ if (rtc_include_tests) { sources = [ "callback_unittest.cc", "crc32_unittest.cc", + "data_rate_limiter_unittest.cc", "helpers_unittest.cc", "httpbase_unittest.cc", "httpcommon_unittest.cc", @@ -1098,7 +1100,6 @@ if (rtc_include_tests) { "optionsfile_unittest.cc", "proxy_unittest.cc", "ptr_util_unittest.cc", - "ratelimiter_unittest.cc", "rollingaccumulator_unittest.cc", "rtccertificate_unittest.cc", "rtccertificategenerator_unittest.cc", diff --git a/rtc_base/ratelimiter.cc b/rtc_base/data_rate_limiter.cc similarity index 82% rename from rtc_base/ratelimiter.cc rename to rtc_base/data_rate_limiter.cc index 842538d804..7288257d76 100644 --- a/rtc_base/ratelimiter.cc +++ b/rtc_base/data_rate_limiter.cc @@ -8,16 +8,16 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "rtc_base/ratelimiter.h" +#include "rtc_base/data_rate_limiter.h" namespace rtc { -bool RateLimiter::CanUse(size_t desired, double time) { +bool DataRateLimiter::CanUse(size_t desired, double time) { return ((time > period_end_ && desired <= max_per_period_) || (used_in_period_ + desired) <= max_per_period_); } -void RateLimiter::Use(size_t used, double time) { +void DataRateLimiter::Use(size_t used, double time) { if (time > period_end_) { period_start_ = time; period_end_ = time + period_length_; diff --git a/rtc_base/data_rate_limiter.h b/rtc_base/data_rate_limiter.h new file mode 100644 index 0000000000..d290816a2c --- /dev/null +++ b/rtc_base/data_rate_limiter.h @@ -0,0 +1,56 @@ +/* + * Copyright 2012 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef RTC_BASE_DATA_RATE_LIMITER_H_ +#define RTC_BASE_DATA_RATE_LIMITER_H_ + +#include + +namespace rtc { + +// Limits the rate of use to a certain maximum quantity per period of +// time. Use, for example, for simple bandwidth throttling. +// +// It's implemented like a diet plan: You have so many calories per +// day. If you hit the limit, you can't eat any more until the next +// day. +class DataRateLimiter { + public: + // For example, 100kb per second. + DataRateLimiter(size_t max, double period) + : max_per_period_(max), + period_length_(period), + used_in_period_(0), + period_start_(0.0), + period_end_(period) {} + virtual ~DataRateLimiter() {} + + // Returns true if if the desired quantity is available in the + // current period (< (max - used)). Once the given time passes the + // end of the period, used is set to zero and more use is available. + bool CanUse(size_t desired, double time); + // Increment the quantity used this period. If past the end of a + // period, a new period is started. + void Use(size_t used, double time); + + size_t used_in_period() const { return used_in_period_; } + + size_t max_per_period() const { return max_per_period_; } + + private: + size_t max_per_period_; + double period_length_; + size_t used_in_period_; + double period_start_; + double period_end_; +}; +} // namespace rtc + +#endif // RTC_BASE_DATA_RATE_LIMITER_H_ diff --git a/rtc_base/ratelimiter_unittest.cc b/rtc_base/data_rate_limiter_unittest.cc similarity index 94% rename from rtc_base/ratelimiter_unittest.cc rename to rtc_base/data_rate_limiter_unittest.cc index f0931e4466..8c410fe1e5 100644 --- a/rtc_base/ratelimiter_unittest.cc +++ b/rtc_base/data_rate_limiter_unittest.cc @@ -8,14 +8,14 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "rtc_base/ratelimiter.h" +#include "rtc_base/data_rate_limiter.h" #include "rtc_base/gunit.h" namespace rtc { TEST(RateLimiterTest, TestCanUse) { // Diet: Can eat 2,000 calories per day. - RateLimiter limiter = RateLimiter(2000, 1.0); + DataRateLimiter limiter = DataRateLimiter(2000, 1.0); double monday = 1.0; double tuesday = 2.0; diff --git a/rtc_base/ratelimiter.h b/rtc_base/ratelimiter.h index 85706d3dde..8aa84aa042 100644 --- a/rtc_base/ratelimiter.h +++ b/rtc_base/ratelimiter.h @@ -1,5 +1,5 @@ /* - * Copyright 2012 The WebRTC Project Authors. All rights reserved. + * Copyright 2018 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source @@ -11,52 +11,14 @@ #ifndef RTC_BASE_RATELIMITER_H_ #define RTC_BASE_RATELIMITER_H_ -#include +#include "rtc_base/data_rate_limiter.h" namespace rtc { - -// Limits the rate of use to a certain maximum quantity per period of -// time. Use, for example, for simple bandwidth throttling. -// -// It's implemented like a diet plan: You have so many calories per -// day. If you hit the limit, you can't eat any more until the next -// day. -class RateLimiter { +// Deprecated, use DataRateLimiter instead +class RateLimiter : public DataRateLimiter { public: - // For example, 100kb per second. - RateLimiter(size_t max, double period) - : max_per_period_(max), - period_length_(period), - used_in_period_(0), - period_start_(0.0), - period_end_(period) { - } - virtual ~RateLimiter() {} - - // Returns true if if the desired quantity is available in the - // current period (< (max - used)). Once the given time passes the - // end of the period, used is set to zero and more use is available. - bool CanUse(size_t desired, double time); - // Increment the quantity used this period. If past the end of a - // period, a new period is started. - void Use(size_t used, double time); - - size_t used_in_period() const { - return used_in_period_; - } - - size_t max_per_period() const { - return max_per_period_; - } - - private: - size_t max_per_period_; - double period_length_; - size_t used_in_period_; - double period_start_; - double period_end_; + using DataRateLimiter::DataRateLimiter; }; - } // namespace rtc #endif // RTC_BASE_RATELIMITER_H_