diff --git a/rtc_base/rate_statistics.cc b/rtc_base/rate_statistics.cc index 85621fa555..5c83796471 100644 --- a/rtc_base/rate_statistics.cc +++ b/rtc_base/rate_statistics.cc @@ -58,7 +58,7 @@ void RateStatistics::Update(int64_t count, int64_t now_ms) { RTC_DCHECK_GE(count, 0); EraseOld(now_ms); - if (first_timestamp_ == -1) { + if (first_timestamp_ == -1 || num_samples_ == 0) { first_timestamp_ = now_ms; } diff --git a/rtc_base/rate_statistics_unittest.cc b/rtc_base/rate_statistics_unittest.cc index 735677082b..8f1a8384cf 100644 --- a/rtc_base/rate_statistics_unittest.cc +++ b/rtc_base/rate_statistics_unittest.cc @@ -148,14 +148,15 @@ TEST_F(RateStatisticsTest, ResetAfterSilence) { now_ms += kWindowMs + 1; EXPECT_FALSE(static_cast(stats_.Rate(now_ms))); + // Silence over window size should trigger auto reset for coming sample. stats_.Update(1000, now_ms); ++now_ms; stats_.Update(1000, now_ms); // We expect two samples of 1000 bytes, and that the bitrate is measured over - // 500 ms, i.e. 2 * 8 * 1000 / 0.500 = 32000. - EXPECT_EQ(32000u, *stats_.Rate(now_ms)); + // active window instead of full window, which is now_ms - first_timestamp + 1 + EXPECT_EQ(kExpectedBitrate, *stats_.Rate(now_ms)); - // Reset, add the same samples again. + // Manual reset, add the same samples again. stats_.Reset(); EXPECT_FALSE(static_cast(stats_.Rate(now_ms))); stats_.Update(1000, now_ms); @@ -272,9 +273,16 @@ TEST_F(RateStatisticsTest, HandlesQuietPeriods) { EXPECT_FALSE(static_cast(stats_.Rate(now_ms))); // Move window a long way out. + // This will cause an automatic reset of the window + // First data point won't give a valid result now_ms += 2 * kWindowMs; stats_.Update(0, now_ms); bitrate = stats_.Rate(now_ms); + EXPECT_FALSE(static_cast(stats_.Rate(now_ms))); + // Second data point gives valid result + ++now_ms; + stats_.Update(0, now_ms); + bitrate = stats_.Rate(now_ms); EXPECT_TRUE(static_cast(bitrate)); EXPECT_EQ(0u, *bitrate); }