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
This commit is contained in:
perkj@webrtc.org 2015-03-17 12:45:15 +00:00
parent e155dbeae9
commit a78a94e838
3 changed files with 14 additions and 17 deletions

View File

@ -844,17 +844,6 @@ class VideoMediaChannelTest : public testing::Test,
EXPECT_TRUE(WaitAndSendFrame(1000 / fps));
EXPECT_FRAME_WAIT(frame + i * fps, codec.width, codec.height, kTimeout);
}
cricket::VideoMediaInfo info;
EXPECT_TRUE(channel_->GetStats(&info));
// For webrtc, |framerate_sent| and |framerate_rcvd| depend on periodic
// callbacks (1 sec).
// Received |fraction_lost| and |packets_lost| are from sent RTCP packet.
// One sent packet needed (sent about once per second).
// |framerate_input|, |framerate_decoded| and |framerate_output| are using
// RateTracker. RateTracker needs to be called twice (with >1 second in
// b/w calls) before a framerate is calculated.
// Therefore insert frames (and call GetStats each sec) for a few seconds
// before testing stats.
}
rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
EXPECT_EQ(codec.id, GetPayloadType(p.get()));

View File

@ -15,7 +15,7 @@ namespace rtc {
RateTracker::RateTracker()
: total_units_(0), units_second_(0),
last_units_second_time_(static_cast<uint32>(-1)),
last_units_second_time_(~0u),
last_units_second_calc_(0) {
}
@ -29,7 +29,10 @@ size_t RateTracker::units_second() {
// 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_ != static_cast<uint32>(-1)) {
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;
@ -44,15 +47,13 @@ size_t RateTracker::units_second() {
last_units_second_calc_ = total_units_ - fraction_units;
}
}
if (last_units_second_time_ == static_cast<uint32>(-1)) {
last_units_second_time_ = current_time;
last_units_second_calc_ = total_units_;
}
return units_second_;
}
void RateTracker::Update(size_t units) {
if (last_units_second_time_ == ~0u)
last_units_second_time_ = Time();
total_units_ += units;
}

View File

@ -71,4 +71,11 @@ TEST(RateTrackerTest, TestBasics) {
EXPECT_EQ(9876U * 5, tracker.units_second());
}
TEST(RateTrackerTest, TestGetUnitSecondsAfterInitialValue) {
RateTrackerForTest tracker;
tracker.Update(1234);
tracker.AdvanceTime(1000);
EXPECT_EQ(1234u, tracker.units_second());
}
} // namespace rtc