Change VideoReceiveStream::Stats total_bitrate_bps to include all received packets.

The total_bitrate_bps is based on complete frames (from jitter buffer callback:
ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe, size_t size_bytes)) (since M58).

Receive total_bitrate_bps can be incorrectly reported.
Example log of stats_.total_bitrate_bps in video_loopback call (target bitrate 600kbps):
SEND: stats_.total_bitrate_bps 682832
RECV: stats_.total_bitrate_bps 1078104 (new: 677384)
SEND: stats_.total_bitrate_bps 694280
RECV: stats_.total_bitrate_bps 1091768 (new: 663152)
SEND: stats_.total_bitrate_bps 626248
RECV: stats_.total_bitrate_bps 7683776 (new: 636080)

Changed total_bitrate_bps to be based on incoming packets (reported via callback from ReceiveStatisticsImpl: ReceiveStatisticsProxy::DataCountersUpdated(const webrtc::StreamDataCounters& counters, uint32_t ssrc)).

BUG=webrtc:7400

Review-Url: https://codereview.webrtc.org/2775813002
Cr-Commit-Position: refs/heads/master@{#17411}
diff --git a/webrtc/video/receive_statistics_proxy.cc b/webrtc/video/receive_statistics_proxy.cc
index 590bd03..2ed2fae 100644
--- a/webrtc/video/receive_statistics_proxy.cc
+++ b/webrtc/video/receive_statistics_proxy.cc
@@ -73,10 +73,10 @@
       renders_fps_estimator_(1000, 1000),
       render_fps_tracker_(100, 10u),
       render_pixel_tracker_(100, 10u),
+      total_byte_tracker_(100, 10u),  // bucket_interval_ms, bucket_count
       freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs),
       first_report_block_time_ms_(-1),
-      avg_rtt_ms_(0),
-      frame_window_accumulated_bytes_(0) {
+      avg_rtt_ms_(0) {
   stats_.ssrc = config_.rtp.remote_ssrc;
   // TODO(brandtr): Replace |rtx_stats_| with a single instance of
   // StreamDataCounters.
@@ -312,25 +312,23 @@
   }
 }
 
-void ReceiveStatisticsProxy::UpdateFrameAndBitrate(int64_t now_ms) const {
+void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const {
   int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
   while (!frame_window_.empty() &&
          frame_window_.begin()->first < old_frames_ms) {
-    frame_window_accumulated_bytes_ -= frame_window_.begin()->second;
     frame_window_.erase(frame_window_.begin());
   }
 
   size_t framerate =
       (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
-  size_t bitrate_bps =
-      frame_window_accumulated_bytes_ * 8000 / kRateStatisticsWindowSizeMs;
   stats_.network_frame_rate = static_cast<int>(framerate);
-  stats_.total_bitrate_bps = static_cast<int>(bitrate_bps);
 }
 
 VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
   rtc::CritScope lock(&crit_);
-  UpdateFrameAndBitrate(clock_->TimeInMilliseconds());
+  UpdateFramerate(clock_->TimeInMilliseconds());
+  stats_.total_bitrate_bps =
+      static_cast<int>(total_byte_tracker_.ComputeRate() * 8);
   return stats_;
 }
 
@@ -412,17 +410,25 @@
 void ReceiveStatisticsProxy::DataCountersUpdated(
     const webrtc::StreamDataCounters& counters,
     uint32_t ssrc) {
+  size_t last_total_bytes = 0;
+  size_t total_bytes = 0;
   rtc::CritScope lock(&crit_);
   if (ssrc == stats_.ssrc) {
+    last_total_bytes = stats_.rtp_stats.transmitted.TotalBytes();
+    total_bytes = counters.transmitted.TotalBytes();
     stats_.rtp_stats = counters;
   } else {
     auto it = rtx_stats_.find(ssrc);
     if (it != rtx_stats_.end()) {
+      last_total_bytes = it->second.transmitted.TotalBytes();
+      total_bytes = counters.transmitted.TotalBytes();
       it->second = counters;
     } else {
       RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
     }
   }
+  if (total_bytes > last_total_bytes)
+    total_byte_tracker_.AddSamples(total_bytes - last_total_bytes);
 }
 
 void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp) {
@@ -502,9 +508,8 @@
     ++stats_.frame_counts.delta_frames;
 
   int64_t now_ms = clock_->TimeInMilliseconds();
-  frame_window_accumulated_bytes_ += size_bytes;
   frame_window_.insert(std::make_pair(now_ms, size_bytes));
-  UpdateFrameAndBitrate(now_ms);
+  UpdateFramerate(now_ms);
 }
 
 void ReceiveStatisticsProxy::OnFrameCountsUpdated(
diff --git a/webrtc/video/receive_statistics_proxy.h b/webrtc/video/receive_statistics_proxy.h
index 30d5d00..07e59b4 100644
--- a/webrtc/video/receive_statistics_proxy.h
+++ b/webrtc/video/receive_statistics_proxy.h
@@ -104,9 +104,8 @@
 
   void QualitySample() EXCLUSIVE_LOCKS_REQUIRED(crit_);
 
-  // Removes info about old frames and then updates the framerate/bitrate.
-  void UpdateFrameAndBitrate(int64_t now_ms) const
-      EXCLUSIVE_LOCKS_REQUIRED(crit_);
+  // Removes info about old frames and then updates the framerate.
+  void UpdateFramerate(int64_t now_ms) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
 
   Clock* const clock_;
   // Ownership of this object lies with the owner of the ReceiveStatisticsProxy
@@ -132,6 +131,7 @@
   RateStatistics renders_fps_estimator_ GUARDED_BY(crit_);
   rtc::RateTracker render_fps_tracker_ GUARDED_BY(crit_);
   rtc::RateTracker render_pixel_tracker_ GUARDED_BY(crit_);
+  rtc::RateTracker total_byte_tracker_ GUARDED_BY(crit_);
   SampleCounter render_width_counter_ GUARDED_BY(crit_);
   SampleCounter render_height_counter_ GUARDED_BY(crit_);
   SampleCounter sync_offset_counter_ GUARDED_BY(crit_);
@@ -148,7 +148,6 @@
   std::map<uint32_t, StreamDataCounters> rtx_stats_ GUARDED_BY(crit_);
   int64_t avg_rtt_ms_ GUARDED_BY(crit_);
   mutable std::map<int64_t, size_t> frame_window_ GUARDED_BY(&crit_);
-  mutable size_t frame_window_accumulated_bytes_ GUARDED_BY(&crit_);
 };
 
 }  // namespace webrtc
diff --git a/webrtc/video/receive_statistics_proxy_unittest.cc b/webrtc/video/receive_statistics_proxy_unittest.cc
index ba4ad3a..af7ae68 100644
--- a/webrtc/video/receive_statistics_proxy_unittest.cc
+++ b/webrtc/video/receive_statistics_proxy_unittest.cc
@@ -124,7 +124,6 @@
   statistics_proxy_->OnCompleteFrame(true, kFrameSizeBytes);
   VideoReceiveStream::Stats stats = statistics_proxy_->GetStats();
   EXPECT_EQ(1, stats.network_frame_rate);
-  EXPECT_EQ(kFrameSizeBytes * 8, stats.total_bitrate_bps);
   EXPECT_EQ(1, stats.frame_counts.key_frames);
   EXPECT_EQ(0, stats.frame_counts.delta_frames);
 }