webrtc_m130/webrtc/base/ratetracker.cc
perkj@webrtc.org a78a94e838 Fix RateTracker to set an initial reference time when first updated.
BUG=4442
R=mflodman@webrtc.org, pbos@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/43829004

Cr-Commit-Position: refs/heads/master@{#8751}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8751 4adac7df-926f-26a2-2b94-8c16560cd09d
2015-03-17 12:45:41 +00:00

65 lines
2.0 KiB
C++

/*
* Copyright 2004 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.
*/
#include "webrtc/base/ratetracker.h"
#include "webrtc/base/timeutils.h"
namespace rtc {
RateTracker::RateTracker()
: total_units_(0), units_second_(0),
last_units_second_time_(~0u),
last_units_second_calc_(0) {
}
size_t RateTracker::total_units() const {
return total_units_;
}
size_t RateTracker::units_second() {
// Snapshot units / second calculator. Determine how many seconds have
// elapsed since our last reference point. If over 1 second, establish
// a new reference point that is an integer number of seconds since the
// last one, and compute the units over that interval.
uint32 current_time = Time();
if (last_units_second_time_ == ~0u) {
last_units_second_time_ = current_time;
last_units_second_calc_ = total_units_;
} else {
int delta = rtc::TimeDiff(current_time, last_units_second_time_);
if (delta >= 1000) {
int fraction_time = delta % 1000;
int seconds = delta / 1000;
int fraction_units =
static_cast<int>(total_units_ - last_units_second_calc_) *
fraction_time / delta;
// Compute "units received during the interval" / "seconds in interval"
units_second_ =
(total_units_ - last_units_second_calc_ - fraction_units) / seconds;
last_units_second_time_ = current_time - fraction_time;
last_units_second_calc_ = total_units_ - fraction_units;
}
}
return units_second_;
}
void RateTracker::Update(size_t units) {
if (last_units_second_time_ == ~0u)
last_units_second_time_ = Time();
total_units_ += units;
}
uint32 RateTracker::Time() const {
return rtc::Time();
}
} // namespace rtc