diff --git a/webrtc/video/stats_counter.cc b/webrtc/video/stats_counter.cc index 918e998c4d..42d23adcfc 100644 --- a/webrtc/video/stats_counter.cc +++ b/webrtc/video/stats_counter.cc @@ -195,10 +195,10 @@ bool PermilleCounter::GetMetric(int* metric) const { return true; } -RateCounter::RateCounter(Clock* clock, StatsCounterObserver* observer) - : StatsCounter(clock, - true, // |include_empty_intervals| - observer) {} +RateCounter::RateCounter(Clock* clock, + StatsCounterObserver* observer, + bool include_empty_intervals) + : StatsCounter(clock, include_empty_intervals, observer) {} void RateCounter::Add(int sample) { StatsCounter::Add(sample); @@ -211,10 +211,10 @@ bool RateCounter::GetMetric(int* metric) const { return true; } -RateAccCounter::RateAccCounter(Clock* clock, StatsCounterObserver* observer) - : StatsCounter(clock, - true, // |include_empty_intervals| - observer) {} +RateAccCounter::RateAccCounter(Clock* clock, + StatsCounterObserver* observer, + bool include_empty_intervals) + : StatsCounter(clock, include_empty_intervals, observer) {} void RateAccCounter::Set(int sample) { StatsCounter::Set(sample); diff --git a/webrtc/video/stats_counter.h b/webrtc/video/stats_counter.h index 477e38e900..c272b6278d 100644 --- a/webrtc/video/stats_counter.h +++ b/webrtc/video/stats_counter.h @@ -191,7 +191,9 @@ class PermilleCounter : public StatsCounter { // class RateCounter : public StatsCounter { public: - RateCounter(Clock* clock, StatsCounterObserver* observer); + RateCounter(Clock* clock, + StatsCounterObserver* observer, + bool include_empty_intervals); ~RateCounter() override {} void Add(int sample); @@ -211,7 +213,9 @@ class RateCounter : public StatsCounter { // class RateAccCounter : public StatsCounter { public: - RateAccCounter(Clock* clock, StatsCounterObserver* observer); + RateAccCounter(Clock* clock, + StatsCounterObserver* observer, + bool include_empty_intervals); ~RateAccCounter() override {} void Set(int sample); diff --git a/webrtc/video/stats_counter_unittest.cc b/webrtc/video/stats_counter_unittest.cc index 7a352e6416..5dd8b72cd6 100644 --- a/webrtc/video/stats_counter_unittest.cc +++ b/webrtc/video/stats_counter_unittest.cc @@ -170,7 +170,7 @@ TEST_F(StatsCounterTest, TestMetric_PermilleCounter) { TEST_F(StatsCounterTest, TestMetric_RateCounter) { StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); - RateCounter counter(&clock_, observer); + RateCounter counter(&clock_, observer, true); counter.Add(186); counter.Add(350); counter.Add(22); @@ -189,7 +189,7 @@ TEST_F(StatsCounterTest, TestMetric_RateCounter) { TEST_F(StatsCounterTest, TestMetric_RateAccCounter) { StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); - RateAccCounter counter(&clock_, observer); + RateAccCounter counter(&clock_, observer, true); counter.Set(175); counter.Set(188); clock_.AdvanceTimeMilliseconds(kProcessIntervalMs); @@ -252,7 +252,7 @@ TEST_F(StatsCounterTest, TestRateAccCounter_NegativeRateIgnored) { const int kSample1 = 200; // 200 / 2 sec const int kSample2 = 100; // -100 / 2 sec - negative ignored const int kSample3 = 700; // 600 / 2 sec - RateAccCounter counter(&clock_, observer); + RateAccCounter counter(&clock_, observer, true); SetSampleAndAdvance(kSample1, kProcessIntervalMs, &counter); SetSampleAndAdvance(kSample2, kProcessIntervalMs, &counter); SetSampleAndAdvance(kSample3, kProcessIntervalMs, &counter); @@ -292,11 +292,38 @@ TEST_F(StatsCounterTest, TestAvgCounter_IntervalsWithoutSamplesIgnored) { EXPECT_EQ(8, stats.max); } -TEST_F(StatsCounterTest, TestRateCounter_IntervalsWithoutSamplesIncluded) { +TEST_F(StatsCounterTest, TestRateCounter_IntervalsWithoutSamplesIgnored) { + const bool kIncludeEmptyIntervals = false; StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); const int kSample1 = 50; // 50 / 2 sec const int kSample2 = 20; // 20 / 2 sec - RateCounter counter(&clock_, observer); + RateCounter counter(&clock_, observer, kIncludeEmptyIntervals); + counter.Add(kSample1); + clock_.AdvanceTimeMilliseconds(kProcessIntervalMs * 3 - 1); + // Trigger process (sample included in next interval). + counter.Add(kSample2); + // [25:1], one interval without samples passed. + EXPECT_EQ(1, observer->num_calls_); + EXPECT_EQ(25, observer->last_sample_); + // Make last interval pass. + clock_.AdvanceTimeMilliseconds(1); + counter.Add(111); // Trigger process (sample included in next interval). + // [10:1],[25:1] + EXPECT_EQ(2, observer->num_calls_); + EXPECT_EQ(10, observer->last_sample_); + // Aggregated stats. + AggregatedStats stats = counter.GetStats(); + EXPECT_EQ(2, stats.num_samples); + EXPECT_EQ(10, stats.min); + EXPECT_EQ(25, stats.max); +} + +TEST_F(StatsCounterTest, TestRateCounter_IntervalsWithoutSamplesIncluded) { + const bool kIncludeEmptyIntervals = true; + StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); + const int kSample1 = 50; // 50 / 2 sec + const int kSample2 = 20; // 20 / 2 sec + RateCounter counter(&clock_, observer, kIncludeEmptyIntervals); counter.Add(kSample1); clock_.AdvanceTimeMilliseconds(kProcessIntervalMs * 3 - 1); // Trigger process (sample included in next interval).