Move SampleCounter from ReceiveStatisticsProxy to rtc_base/numerics

This class will be used in upcoming VideoQualiyObserver.

Bug: none
Change-Id: I7d79a6caf3040a3f707ed8700842dea1de81e0a6
Reviewed-on: https://webrtc-review.googlesource.com/77724
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23343}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index aaf974e..43172a9 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -417,6 +417,8 @@
     "numerics/histogram_percentile_counter.h",
     "numerics/mod_ops.h",
     "numerics/moving_max_counter.h",
+    "numerics/sample_counter.cc",
+    "numerics/sample_counter.h",
     "onetimeevent.h",
     "pathutils.cc",
     "pathutils.h",
@@ -1140,6 +1142,7 @@
       "numerics/moving_max_counter_unittest.cc",
       "numerics/safe_compare_unittest.cc",
       "numerics/safe_minmax_unittest.cc",
+      "numerics/sample_counter_unittest.cc",
       "onetimeevent_unittest.cc",
       "pathutils_unittest.cc",
       "platform_file_unittest.cc",
diff --git a/rtc_base/numerics/sample_counter.cc b/rtc_base/numerics/sample_counter.cc
new file mode 100644
index 0000000..037fddc
--- /dev/null
+++ b/rtc_base/numerics/sample_counter.cc
@@ -0,0 +1,82 @@
+/*
+ *  Copyright (c) 2018 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 <limits>
+
+#include "rtc_base/checks.h"
+#include "rtc_base/numerics/safe_conversions.h"
+#include "rtc_base/numerics/sample_counter.h"
+
+namespace rtc {
+
+SampleCounter::SampleCounter() = default;
+SampleCounter::~SampleCounter() = default;
+
+void SampleCounter::Add(int sample) {
+  if (sum_ > 0) {
+    RTC_DCHECK_LE(sample, std::numeric_limits<int64_t>::max() - sum_);
+  } else {
+    RTC_DCHECK_GE(sample, std::numeric_limits<int64_t>::min() - sum_);
+  }
+  sum_ += sample;
+  // Prevent overflow in squaring.
+  RTC_DCHECK_GT(sample, std::numeric_limits<int32_t>::min());
+  RTC_DCHECK_LE(int64_t{sample} * sample,
+                std::numeric_limits<int64_t>::max() - sum_squared_);
+  sum_squared_ += int64_t{sample} * sample;
+  ++num_samples_;
+  if (!max_ || sample > *max_) {
+    max_ = sample;
+  }
+}
+
+void SampleCounter::Add(const SampleCounter& other) {
+  if (sum_ > 0) {
+    RTC_DCHECK_LE(other.sum_, std::numeric_limits<int64_t>::max() - sum_);
+  } else {
+    RTC_DCHECK_GE(other.sum_, std::numeric_limits<int64_t>::min() - sum_);
+  }
+  sum_ += other.sum_;
+  RTC_DCHECK_LE(other.sum_squared_,
+                std::numeric_limits<int64_t>::max() - sum_squared_);
+  sum_squared_ += other.sum_squared_;
+  RTC_DCHECK_LE(other.num_samples_,
+                std::numeric_limits<int64_t>::max() - num_samples_);
+  num_samples_ += other.num_samples_;
+  if (other.max_ && (!max_ || *max_ < *other.max_))
+    max_ = other.max_;
+}
+
+rtc::Optional<int> SampleCounter::Avg(int64_t min_required_samples) const {
+  RTC_DCHECK_GT(min_required_samples, 0);
+  if (num_samples_ < min_required_samples)
+    return rtc::nullopt;
+  return rtc::dchecked_cast<int>(sum_ / num_samples_);
+}
+
+rtc::Optional<int64_t> SampleCounter::Variance(
+    int64_t min_required_samples) const {
+  RTC_DCHECK_GT(min_required_samples, 0);
+  if (num_samples_ < min_required_samples)
+    return rtc::nullopt;
+  // E[(x-mean)^2] = E[x^2] - mean^2
+  int64_t mean = sum_ / num_samples_;
+  return sum_squared_ / num_samples_ - mean * mean;
+}
+
+rtc::Optional<int> SampleCounter::Max() const {
+  return max_;
+}
+
+void SampleCounter::Reset() {
+  *this = {};
+}
+
+}  // namespace rtc
diff --git a/rtc_base/numerics/sample_counter.h b/rtc_base/numerics/sample_counter.h
new file mode 100644
index 0000000..0de6f25
--- /dev/null
+++ b/rtc_base/numerics/sample_counter.h
@@ -0,0 +1,41 @@
+/*
+ *  Copyright (c) 2018 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.
+ */
+
+#ifndef RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_
+#define RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_
+
+#include "api/optional.h"
+
+namespace rtc {
+
+// Simple utility class for counting basic statistics (max./avg./variance) on
+// stream of samples.
+class SampleCounter {
+ public:
+  SampleCounter();
+  ~SampleCounter();
+  void Add(int sample);
+  rtc::Optional<int> Avg(int64_t min_required_samples) const;
+  rtc::Optional<int64_t> Variance(int64_t min_required_samples) const;
+  rtc::Optional<int> Max() const;
+  void Reset();
+  // Adds all the samples from the |other| SampleCounter as if they were all
+  // individually added using |Add(int)| method.
+  void Add(const SampleCounter& other);
+
+ private:
+  int64_t sum_ = 0;
+  int64_t sum_squared_ = 0;
+  int64_t num_samples_ = 0;
+  rtc::Optional<int> max_;
+};
+
+}  // namespace rtc
+#endif  // RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_
diff --git a/rtc_base/numerics/sample_counter_unittest.cc b/rtc_base/numerics/sample_counter_unittest.cc
new file mode 100644
index 0000000..844ea4a
--- /dev/null
+++ b/rtc_base/numerics/sample_counter_unittest.cc
@@ -0,0 +1,53 @@
+/*
+ *  Copyright 2018 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 "rtc_base/numerics/sample_counter.h"
+
+#include <utility>
+#include <vector>
+
+#include "test/gmock.h"
+#include "test/gtest.h"
+
+using testing::Eq;
+
+namespace rtc {
+
+TEST(SampleCounterTest, ProcessesNoSamples) {
+  constexpr int kMinSamples = 1;
+  SampleCounter counter;
+  EXPECT_THAT(counter.Avg(kMinSamples), Eq(rtc::nullopt));
+  EXPECT_THAT(counter.Avg(kMinSamples), Eq(rtc::nullopt));
+  EXPECT_THAT(counter.Max(), Eq(rtc::nullopt));
+}
+
+TEST(SampleCounterTest, NotEnoughSamples) {
+  constexpr int kMinSamples = 6;
+  SampleCounter counter;
+  for (int value : {1, 2, 3, 4, 5}) {
+    counter.Add(value);
+  }
+  EXPECT_THAT(counter.Avg(kMinSamples), Eq(rtc::nullopt));
+  EXPECT_THAT(counter.Avg(kMinSamples), Eq(rtc::nullopt));
+  EXPECT_THAT(counter.Max(), Eq(5));
+}
+
+TEST(SampleCounterTest, EnoughSamples) {
+  constexpr int kMinSamples = 5;
+  SampleCounter counter;
+  for (int value : {1, 2, 3, 4, 5}) {
+    counter.Add(value);
+  }
+  EXPECT_THAT(counter.Avg(kMinSamples), Eq(3));
+  EXPECT_THAT(counter.Variance(kMinSamples), Eq(2));
+  EXPECT_THAT(counter.Max(), Eq(5));
+}
+
+}  // namespace rtc
diff --git a/video/receive_statistics_proxy.cc b/video/receive_statistics_proxy.cc
index 62a68ed..2cccd5a 100644
--- a/video/receive_statistics_proxy.cc
+++ b/video/receive_statistics_proxy.cc
@@ -190,10 +190,12 @@
         round(render_pixel_tracker_.ComputeTotalRate()));
   }
 
-  int sync_offset_ms = sync_offset_counter_.Avg(kMinRequiredSamples);
-  if (sync_offset_ms != -1) {
-    RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs", sync_offset_ms);
-    log_stream << "WebRTC.Video.AVSyncOffsetInMs " << sync_offset_ms << '\n';
+  rtc::Optional<int> sync_offset_ms =
+      sync_offset_counter_.Avg(kMinRequiredSamples);
+  if (sync_offset_ms) {
+    RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs",
+                               *sync_offset_ms);
+    log_stream << "WebRTC.Video.AVSyncOffsetInMs " << *sync_offset_ms << '\n';
   }
   AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
   if (freq_offset_stats.num_samples > 0) {
@@ -215,37 +217,41 @@
                << key_frames_permille << '\n';
   }
 
-  int qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
-  if (qp != -1) {
-    RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", qp);
-    log_stream << "WebRTC.Video.Decoded.Vp8.Qp " << qp << '\n';
+  rtc::Optional<int> qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
+  if (qp) {
+    RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", *qp);
+    log_stream << "WebRTC.Video.Decoded.Vp8.Qp " << *qp << '\n';
   }
-  int decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
-  if (decode_ms != -1) {
-    RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", decode_ms);
-    log_stream << "WebRTC.Video.DecodeTimeInMs " << decode_ms << '\n';
+  rtc::Optional<int> decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
+  if (decode_ms) {
+    RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", *decode_ms);
+    log_stream << "WebRTC.Video.DecodeTimeInMs " << *decode_ms << '\n';
   }
-  int jb_delay_ms = jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
-  if (jb_delay_ms != -1) {
+  rtc::Optional<int> jb_delay_ms =
+      jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
+  if (jb_delay_ms) {
     RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
-                               jb_delay_ms);
-    log_stream << "WebRTC.Video.JitterBufferDelayInMs " << jb_delay_ms << '\n';
+                               *jb_delay_ms);
+    log_stream << "WebRTC.Video.JitterBufferDelayInMs " << *jb_delay_ms << '\n';
   }
 
-  int target_delay_ms = target_delay_counter_.Avg(kMinRequiredSamples);
-  if (target_delay_ms != -1) {
-    RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs", target_delay_ms);
-    log_stream << "WebRTC.Video.TargetDelayInMs " << target_delay_ms << '\n';
+  rtc::Optional<int> target_delay_ms =
+      target_delay_counter_.Avg(kMinRequiredSamples);
+  if (target_delay_ms) {
+    RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs",
+                               *target_delay_ms);
+    log_stream << "WebRTC.Video.TargetDelayInMs " << *target_delay_ms << '\n';
   }
-  int current_delay_ms = current_delay_counter_.Avg(kMinRequiredSamples);
-  if (current_delay_ms != -1) {
+  rtc::Optional<int> current_delay_ms =
+      current_delay_counter_.Avg(kMinRequiredSamples);
+  if (current_delay_ms) {
     RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
-                               current_delay_ms);
-    log_stream << "WebRTC.Video.CurrentDelayInMs " << current_delay_ms << '\n';
+                               *current_delay_ms);
+    log_stream << "WebRTC.Video.CurrentDelayInMs " << *current_delay_ms << '\n';
   }
-  int delay_ms = delay_counter_.Avg(kMinRequiredSamples);
-  if (delay_ms != -1)
-    RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", delay_ms);
+  rtc::Optional<int> delay_ms = delay_counter_.Avg(kMinRequiredSamples);
+  if (delay_ms)
+    RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", *delay_ms);
 
   // Aggregate content_specific_stats_ by removing experiment or simulcast
   // information;
@@ -289,36 +295,38 @@
     RTC_DCHECK(videocontenttypehelpers::GetExperimentId(content_type) == 0 ||
                videocontenttypehelpers::GetSimulcastId(content_type) == 0);
 
-    int e2e_delay_ms = stats.e2e_delay_counter.Avg(kMinRequiredSamples);
-    if (e2e_delay_ms != -1) {
+    rtc::Optional<int> e2e_delay_ms =
+        stats.e2e_delay_counter.Avg(kMinRequiredSamples);
+    if (e2e_delay_ms) {
       RTC_HISTOGRAM_COUNTS_SPARSE_10000(
-          uma_prefix + ".EndToEndDelayInMs" + uma_suffix, e2e_delay_ms);
+          uma_prefix + ".EndToEndDelayInMs" + uma_suffix, *e2e_delay_ms);
       log_stream << uma_prefix << ".EndToEndDelayInMs" << uma_suffix << " "
-                 << e2e_delay_ms << '\n';
+                 << *e2e_delay_ms << '\n';
     }
-    int e2e_delay_max_ms = stats.e2e_delay_counter.Max();
-    if (e2e_delay_max_ms != -1 && e2e_delay_ms != -1) {
+    rtc::Optional<int> e2e_delay_max_ms = stats.e2e_delay_counter.Max();
+    if (e2e_delay_max_ms && e2e_delay_ms) {
       RTC_HISTOGRAM_COUNTS_SPARSE_100000(
-          uma_prefix + ".EndToEndDelayMaxInMs" + uma_suffix, e2e_delay_max_ms);
+          uma_prefix + ".EndToEndDelayMaxInMs" + uma_suffix, *e2e_delay_max_ms);
       log_stream << uma_prefix << ".EndToEndDelayMaxInMs" << uma_suffix << " "
-                 << e2e_delay_max_ms << '\n';
+                 << *e2e_delay_max_ms << '\n';
     }
-    int interframe_delay_ms =
+    rtc::Optional<int> interframe_delay_ms =
         stats.interframe_delay_counter.Avg(kMinRequiredSamples);
-    if (interframe_delay_ms != -1) {
+    if (interframe_delay_ms) {
       RTC_HISTOGRAM_COUNTS_SPARSE_10000(
           uma_prefix + ".InterframeDelayInMs" + uma_suffix,
-          interframe_delay_ms);
+          *interframe_delay_ms);
       log_stream << uma_prefix << ".InterframeDelayInMs" << uma_suffix << " "
-                 << interframe_delay_ms << '\n';
+                 << *interframe_delay_ms << '\n';
     }
-    int interframe_delay_max_ms = stats.interframe_delay_counter.Max();
-    if (interframe_delay_max_ms != -1 && interframe_delay_ms != -1) {
+    rtc::Optional<int> interframe_delay_max_ms =
+        stats.interframe_delay_counter.Max();
+    if (interframe_delay_max_ms && interframe_delay_ms) {
       RTC_HISTOGRAM_COUNTS_SPARSE_10000(
           uma_prefix + ".InterframeDelayMaxInMs" + uma_suffix,
-          interframe_delay_max_ms);
+          *interframe_delay_max_ms);
       log_stream << uma_prefix << ".InterframeDelayMaxInMs" << uma_suffix << " "
-                 << interframe_delay_max_ms << '\n';
+                 << *interframe_delay_max_ms << '\n';
     }
 
     rtc::Optional<uint32_t> interframe_delay_95p_ms =
@@ -331,20 +339,20 @@
                  << uma_suffix << " " << *interframe_delay_95p_ms << '\n';
     }
 
-    int width = stats.received_width.Avg(kMinRequiredSamples);
-    if (width != -1) {
+    rtc::Optional<int> width = stats.received_width.Avg(kMinRequiredSamples);
+    if (width) {
       RTC_HISTOGRAM_COUNTS_SPARSE_10000(
-          uma_prefix + ".ReceivedWidthInPixels" + uma_suffix, width);
+          uma_prefix + ".ReceivedWidthInPixels" + uma_suffix, *width);
       log_stream << uma_prefix << ".ReceivedWidthInPixels" << uma_suffix << " "
-                 << width << '\n';
+                 << *width << '\n';
     }
 
-    int height = stats.received_height.Avg(kMinRequiredSamples);
-    if (height != -1) {
+    rtc::Optional<int> height = stats.received_height.Avg(kMinRequiredSamples);
+    if (height) {
       RTC_HISTOGRAM_COUNTS_SPARSE_10000(
-          uma_prefix + ".ReceivedHeightInPixels" + uma_suffix, height);
+          uma_prefix + ".ReceivedHeightInPixels" + uma_suffix, *height);
       log_stream << uma_prefix << ".ReceivedHeightInPixels" << uma_suffix << " "
-                 << height << '\n';
+                 << *height << '\n';
     }
 
     if (content_type != VideoContentType::UNSPECIFIED) {
@@ -374,12 +382,12 @@
                    << " " << key_frames_permille << '\n';
       }
 
-      int qp = stats.qp_counter.Avg(kMinRequiredSamples);
-      if (qp != -1) {
+      rtc::Optional<int> qp = stats.qp_counter.Avg(kMinRequiredSamples);
+      if (qp) {
         RTC_HISTOGRAM_COUNTS_SPARSE_200(
-            uma_prefix + ".Decoded.Vp8.Qp" + uma_suffix, qp);
-        log_stream << uma_prefix << ".Decoded.Vp8.Qp" << uma_suffix << " " << qp
-                   << '\n';
+            uma_prefix + ".Decoded.Vp8.Qp" + uma_suffix, *qp);
+        log_stream << uma_prefix << ".Decoded.Vp8.Qp" << uma_suffix << " "
+                   << *qp << '\n';
       }
     }
   }
@@ -469,7 +477,7 @@
 
   double fps =
       render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
-  int qp = qp_sample_.Avg(1);
+  rtc::Optional<int> qp = qp_sample_.Avg(1);
 
   bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
   bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
@@ -477,8 +485,8 @@
   bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
 
   fps_threshold_.AddMeasurement(static_cast<int>(fps));
-  if (qp != -1)
-    qp_threshold_.AddMeasurement(qp);
+  if (qp)
+    qp_threshold_.AddMeasurement(*qp);
   rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
   double fps_variance = fps_variance_opt.value_or(0);
   if (fps_variance_opt) {
@@ -516,7 +524,7 @@
 
   RTC_LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
                       << " fps: " << fps << " fps_bad: " << fps_bad
-                      << " qp: " << qp << " qp_bad: " << qp_bad
+                      << " qp: " << qp.value_or(-1) << " qp_bad: " << qp_bad
                       << " variance_bad: " << variance_bad
                       << " fps_variance: " << fps_variance;
 
@@ -816,38 +824,6 @@
   last_decoded_frame_time_ms_.reset();
 }
 
-void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
-  sum += sample;
-  ++num_samples;
-  if (!max || sample > *max) {
-    max.emplace(sample);
-  }
-}
-
-void ReceiveStatisticsProxy::SampleCounter::Add(const SampleCounter& other) {
-  sum += other.sum;
-  num_samples += other.num_samples;
-  if (other.max && (!max || *max < *other.max))
-    max = other.max;
-}
-
-int ReceiveStatisticsProxy::SampleCounter::Avg(
-    int64_t min_required_samples) const {
-  if (num_samples < min_required_samples || num_samples == 0)
-    return -1;
-  return static_cast<int>(sum / num_samples);
-}
-
-int ReceiveStatisticsProxy::SampleCounter::Max() const {
-  return max.value_or(-1);
-}
-
-void ReceiveStatisticsProxy::SampleCounter::Reset() {
-  num_samples = 0;
-  sum = 0;
-  max.reset();
-}
-
 void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
                                          int64_t max_rtt_ms) {
   rtc::CritScope lock(&crit_);
diff --git a/video/receive_statistics_proxy.h b/video/receive_statistics_proxy.h
index 73768a4..9a2185e 100644
--- a/video/receive_statistics_proxy.h
+++ b/video/receive_statistics_proxy.h
@@ -23,6 +23,7 @@
 #include "rtc_base/criticalsection.h"
 #include "rtc_base/numerics/histogram_percentile_counter.h"
 #include "rtc_base/numerics/moving_max_counter.h"
+#include "rtc_base/numerics/sample_counter.h"
 #include "rtc_base/rate_statistics.h"
 #include "rtc_base/ratetracker.h"
 #include "rtc_base/thread_annotations.h"
@@ -103,22 +104,8 @@
   void DecoderThreadStopped();
 
  private:
-  struct SampleCounter {
-    SampleCounter() : sum(0), num_samples(0) {}
-    void Add(int sample);
-    int Avg(int64_t min_required_samples) const;
-    int Max() const;
-    void Reset();
-    void Add(const SampleCounter& other);
-
-   private:
-    int64_t sum;
-    int64_t num_samples;
-    rtc::Optional<int> max;
-  };
-
   struct QpCounters {
-    SampleCounter vp8;
+    rtc::SampleCounter vp8;
   };
 
   struct ContentSpecificStats {
@@ -126,13 +113,13 @@
 
     void Add(const ContentSpecificStats& other);
 
-    SampleCounter e2e_delay_counter;
-    SampleCounter interframe_delay_counter;
+    rtc::SampleCounter e2e_delay_counter;
+    rtc::SampleCounter interframe_delay_counter;
     int64_t flow_duration_ms = 0;
     int64_t total_media_bytes = 0;
-    SampleCounter received_width;
-    SampleCounter received_height;
-    SampleCounter qp_counter;
+    rtc::SampleCounter received_width;
+    rtc::SampleCounter received_height;
+    rtc::SampleCounter qp_counter;
     FrameCounts frame_counts;
     rtc::HistogramPercentileCounter interframe_delay_percentiles;
   };
@@ -161,7 +148,7 @@
   QualityThreshold fps_threshold_ RTC_GUARDED_BY(crit_);
   QualityThreshold qp_threshold_ RTC_GUARDED_BY(crit_);
   QualityThreshold variance_threshold_ RTC_GUARDED_BY(crit_);
-  SampleCounter qp_sample_ RTC_GUARDED_BY(crit_);
+  rtc::SampleCounter qp_sample_ RTC_GUARDED_BY(crit_);
   int num_bad_states_ RTC_GUARDED_BY(crit_);
   int num_certain_states_ RTC_GUARDED_BY(crit_);
   mutable VideoReceiveStream::Stats stats_ RTC_GUARDED_BY(crit_);
@@ -170,12 +157,12 @@
   rtc::RateTracker render_fps_tracker_ RTC_GUARDED_BY(crit_);
   rtc::RateTracker render_pixel_tracker_ RTC_GUARDED_BY(crit_);
   rtc::RateTracker total_byte_tracker_ RTC_GUARDED_BY(crit_);
-  SampleCounter sync_offset_counter_ RTC_GUARDED_BY(crit_);
-  SampleCounter decode_time_counter_ RTC_GUARDED_BY(crit_);
-  SampleCounter jitter_buffer_delay_counter_ RTC_GUARDED_BY(crit_);
-  SampleCounter target_delay_counter_ RTC_GUARDED_BY(crit_);
-  SampleCounter current_delay_counter_ RTC_GUARDED_BY(crit_);
-  SampleCounter delay_counter_ RTC_GUARDED_BY(crit_);
+  rtc::SampleCounter sync_offset_counter_ RTC_GUARDED_BY(crit_);
+  rtc::SampleCounter decode_time_counter_ RTC_GUARDED_BY(crit_);
+  rtc::SampleCounter jitter_buffer_delay_counter_ RTC_GUARDED_BY(crit_);
+  rtc::SampleCounter target_delay_counter_ RTC_GUARDED_BY(crit_);
+  rtc::SampleCounter current_delay_counter_ RTC_GUARDED_BY(crit_);
+  rtc::SampleCounter delay_counter_ RTC_GUARDED_BY(crit_);
   mutable rtc::MovingMaxCounter<int> interframe_delay_max_moving_
       RTC_GUARDED_BY(crit_);
   std::map<VideoContentType, ContentSpecificStats> content_specific_stats_