Chromium Code Reviews| Index: webrtc/base/ratetracker.cc |
| diff --git a/webrtc/base/ratetracker.cc b/webrtc/base/ratetracker.cc |
| index c1ad2d5e844a00b4b3842bdbcd9e31af83a36a3f..a5e287f2b81e9e12432de0986e867e1295d66843 100644 |
| --- a/webrtc/base/ratetracker.cc |
| +++ b/webrtc/base/ratetracker.cc |
| @@ -19,48 +19,46 @@ |
| namespace rtc { |
| -RateTracker::RateTracker(uint32_t bucket_milliseconds, size_t bucket_count) |
| +RateTracker::RateTracker(int bucket_milliseconds, size_t bucket_count) |
| : bucket_milliseconds_(bucket_milliseconds), |
| bucket_count_(bucket_count), |
| sample_buckets_(new size_t[bucket_count + 1]), |
| total_sample_count_(0u), |
| - bucket_start_time_milliseconds_(~0u) { |
| - RTC_CHECK(bucket_milliseconds > 0u); |
| - RTC_CHECK(bucket_count > 0u); |
| + bucket_start_time_milliseconds_(~0) { |
|
Taylor Brandstetter
2016/04/05 01:08:14
It would be more clear if you replaced "~0" with "
pthatcher1
2016/04/11 20:56:59
Or, better yet, make this use rtc::optional.
Or i
honghaiz3
2016/04/18 23:39:03
Used a named constant kTimeUnset.
|
| + RTC_CHECK(bucket_milliseconds > 0); |
| + RTC_CHECK(bucket_count > 0); |
| } |
| RateTracker::~RateTracker() { |
| delete[] sample_buckets_; |
| } |
| -double RateTracker::ComputeRateForInterval( |
| - uint32_t interval_milliseconds) const { |
| - if (bucket_start_time_milliseconds_ == ~0u) { |
| +double RateTracker::ComputeRateForInterval(int interval_milliseconds) const { |
| + if (bucket_start_time_milliseconds_ == ~0) { |
| return 0.0; |
| } |
| - uint32_t current_time = Time(); |
| + int64_t current_time = Time(); |
| // Calculate which buckets to sum up given the current time. If the time |
| // has passed to a new bucket then we have to skip some of the oldest buckets. |
| - uint32_t available_interval_milliseconds = std::min<uint32_t>( |
| - interval_milliseconds, |
| - bucket_milliseconds_ * static_cast<uint32_t>(bucket_count_)); |
| + int available_interval_milliseconds = |
| + std::min(interval_milliseconds, |
| + bucket_milliseconds_ * static_cast<int>(bucket_count_)); |
| // number of old buckets (i.e. after the current bucket in the ring buffer) |
| // that are expired given our current time interval. |
| size_t buckets_to_skip; |
| // Number of milliseconds of the first bucket that are not a portion of the |
| // current interval. |
| - uint32_t milliseconds_to_skip; |
| + int milliseconds_to_skip; |
| if (current_time > |
| initialization_time_milliseconds_ + available_interval_milliseconds) { |
| - uint32_t time_to_skip = |
| - current_time - bucket_start_time_milliseconds_ + |
| - static_cast<uint32_t>(bucket_count_) * bucket_milliseconds_ - |
| - available_interval_milliseconds; |
| + int time_to_skip = current_time - bucket_start_time_milliseconds_ + |
| + static_cast<int>(bucket_count_) * bucket_milliseconds_ - |
| + available_interval_milliseconds; |
|
pthatcher1
2016/04/11 20:56:59
Why not just use uint64_t for all of these?
honghaiz3
2016/04/18 23:39:03
Done.
|
| buckets_to_skip = time_to_skip / bucket_milliseconds_; |
| milliseconds_to_skip = time_to_skip % bucket_milliseconds_; |
| } else { |
| buckets_to_skip = bucket_count_ - current_bucket_; |
| - milliseconds_to_skip = 0u; |
| + milliseconds_to_skip = 0; |
| available_interval_milliseconds = |
| TimeDiff(current_time, initialization_time_milliseconds_); |
| // Let one bucket interval pass after initialization before reporting. |
| @@ -70,8 +68,7 @@ double RateTracker::ComputeRateForInterval( |
| } |
| // If we're skipping all buckets that means that there have been no samples |
| // within the sampling interval so report 0. |
| - if (buckets_to_skip > bucket_count_ || |
| - available_interval_milliseconds == 0u) { |
| + if (buckets_to_skip > bucket_count_ || available_interval_milliseconds == 0) { |
| return 0.0; |
| } |
| size_t start_bucket = NextBucketIndex(current_bucket_ + buckets_to_skip); |
| @@ -88,21 +85,21 @@ double RateTracker::ComputeRateForInterval( |
| total_samples += sample_buckets_[i]; |
| } |
| // Convert to samples per second. |
| - return static_cast<double>(total_samples * 1000u) / |
| - static_cast<double>(available_interval_milliseconds); |
| + return static_cast<double>(total_samples * 1000) / |
| + static_cast<double>(available_interval_milliseconds); |
| } |
| double RateTracker::ComputeTotalRate() const { |
| - if (bucket_start_time_milliseconds_ == ~0u) { |
| + if (bucket_start_time_milliseconds_ == ~0) { |
| return 0.0; |
| } |
| - uint32_t current_time = Time(); |
| - if (TimeIsLaterOrEqual(current_time, initialization_time_milliseconds_)) { |
| + int64_t current_time = Time(); |
| + if (current_time <= initialization_time_milliseconds_) { |
| return 0.0; |
| } |
| - return static_cast<double>(total_sample_count_ * 1000u) / |
| - static_cast<double>( |
| - TimeDiff(current_time, initialization_time_milliseconds_)); |
| + return static_cast<double>(total_sample_count_ * 1000) / |
| + static_cast<double>( |
| + TimeDiff(current_time, initialization_time_milliseconds_)); |
| } |
| size_t RateTracker::TotalSampleCount() const { |
| @@ -111,15 +108,16 @@ size_t RateTracker::TotalSampleCount() const { |
| void RateTracker::AddSamples(size_t sample_count) { |
| EnsureInitialized(); |
| - uint32_t current_time = Time(); |
| + int64_t current_time = Time(); |
| // Advance the current bucket as needed for the current time, and reset |
| // bucket counts as we advance. |
| - for (size_t i = 0u; i <= bucket_count_ && |
| - current_time >= bucket_start_time_milliseconds_ + bucket_milliseconds_; |
| - ++i) { |
| + for (size_t i = 0; |
|
Taylor Brandstetter
2016/04/05 01:08:14
Why was this changed from 0u? size_t is unsigned.
honghaiz3
2016/04/18 23:39:03
I do not think the "u" here is necessary. I would
|
| + i <= bucket_count_ && |
| + current_time >= bucket_start_time_milliseconds_ + bucket_milliseconds_; |
| + ++i) { |
| bucket_start_time_milliseconds_ += bucket_milliseconds_; |
| current_bucket_ = NextBucketIndex(current_bucket_); |
| - sample_buckets_[current_bucket_] = 0u; |
| + sample_buckets_[current_bucket_] = 0; |
| } |
| // Ensure that bucket_start_time_milliseconds_ is updated appropriately if |
| // the entire buffer of samples has been expired. |
| @@ -130,18 +128,18 @@ void RateTracker::AddSamples(size_t sample_count) { |
| total_sample_count_ += sample_count; |
| } |
| -uint32_t RateTracker::Time() const { |
| +int64_t RateTracker::Time() const { |
| return rtc::Time(); |
| } |
| void RateTracker::EnsureInitialized() { |
| - if (bucket_start_time_milliseconds_ == ~0u) { |
| + if (bucket_start_time_milliseconds_ == ~0) { |
| initialization_time_milliseconds_ = Time(); |
| bucket_start_time_milliseconds_ = initialization_time_milliseconds_; |
| - current_bucket_ = 0u; |
| + current_bucket_ = 0; |
| // We only need to initialize the first bucket because we reset buckets when |
| // current_bucket_ increments. |
| - sample_buckets_[current_bucket_] = 0u; |
| + sample_buckets_[current_bucket_] = 0; |
| } |
| } |