Calculate JitterBufferDelayInMs in the new jitter buffer.

JitterBufferDelayInMs is used for the WebRTC-NewVideoJitterBuffer finch
experiment, and therefore needs to be calculated.

BUG=webrtc:5514

Review-Url: https://codereview.webrtc.org/2534093003
Cr-Commit-Position: refs/heads/master@{#15313}
diff --git a/webrtc/modules/video_coding/frame_buffer2.cc b/webrtc/modules/video_coding/frame_buffer2.cc
index bf1760e..279c613 100644
--- a/webrtc/modules/video_coding/frame_buffer2.cc
+++ b/webrtc/modules/video_coding/frame_buffer2.cc
@@ -45,9 +45,7 @@
       num_frames_history_(0),
       num_frames_buffered_(0),
       stopped_(false),
-      protection_mode_(kProtectionNack),
-      num_total_frames_(0),
-      num_key_frames_(0) {}
+      protection_mode_(kProtectionNack) {}
 
 FrameBuffer::~FrameBuffer() {
   UpdateHistograms();
@@ -133,6 +131,8 @@
     timing_->UpdateCurrentDelay(frame->RenderTime(),
                                 clock_->TimeInMilliseconds());
 
+    UpdateJitterDelay();
+
     PropagateDecodability(next_frame_it->second);
     AdvanceLastDecodedFrame(next_frame_it);
     *frame_out = std::move(frame);
@@ -364,6 +364,16 @@
   return true;
 }
 
+void FrameBuffer::UpdateJitterDelay() {
+  int unused;
+  int delay;
+  timing_->GetTimings(&unused, &unused, &unused, &unused, &delay, &unused,
+                      &unused);
+
+  accumulated_delay_ += delay;
+  ++accumulated_delay_samples_;
+}
+
 void FrameBuffer::UpdateHistograms() const {
   rtc::CritScope lock(&crit_);
   if (num_total_frames_ > 0) {
@@ -373,6 +383,11 @@
     RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
                               key_frames_permille);
   }
+
+  if (accumulated_delay_samples_ > 0) {
+    RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
+                               accumulated_delay_ / accumulated_delay_samples_);
+  }
 }
 
 }  // namespace video_coding
diff --git a/webrtc/modules/video_coding/frame_buffer2.h b/webrtc/modules/video_coding/frame_buffer2.h
index 7a5326b..b41ef2f 100644
--- a/webrtc/modules/video_coding/frame_buffer2.h
+++ b/webrtc/modules/video_coding/frame_buffer2.h
@@ -139,6 +139,8 @@
                                         FrameMap::iterator info)
       EXCLUSIVE_LOCKS_REQUIRED(crit_);
 
+  void UpdateJitterDelay() EXCLUSIVE_LOCKS_REQUIRED(crit_);
+
   void UpdateHistograms() const;
 
   FrameMap frames_ GUARDED_BY(crit_);
@@ -155,10 +157,16 @@
   int num_frames_buffered_ GUARDED_BY(crit_);
   bool stopped_ GUARDED_BY(crit_);
   VCMVideoProtection protection_mode_ GUARDED_BY(crit_);
-  int num_total_frames_ GUARDED_BY(crit_);
-  int num_key_frames_ GUARDED_BY(crit_);
 
   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
+
+  // For WebRTC.Video.JitterBufferDelayInMs metric.
+  int64_t accumulated_delay_ = 0;
+  int64_t accumulated_delay_samples_ = 0;
+
+  // For WebRTC.Video.KeyFramesReceivedInPermille metric.
+  int64_t num_total_frames_ = 0;
+  int64_t num_key_frames_ = 0;
 };
 
 }  // namespace video_coding
diff --git a/webrtc/video/receive_statistics_proxy.cc b/webrtc/video/receive_statistics_proxy.cc
index b15ef68..7d5fbc0 100644
--- a/webrtc/video/receive_statistics_proxy.cc
+++ b/webrtc/video/receive_statistics_proxy.cc
@@ -15,6 +15,7 @@
 #include "webrtc/base/checks.h"
 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
 #include "webrtc/system_wrappers/include/clock.h"
+#include "webrtc/system_wrappers/include/field_trial.h"
 #include "webrtc/system_wrappers/include/metrics.h"
 
 namespace webrtc {
@@ -92,10 +93,14 @@
   if (decode_ms != -1)
     RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", decode_ms);
 
-  int jb_delay_ms = jitter_buffer_delay_counter_.Avg(kMinRequiredDecodeSamples);
-  if (jb_delay_ms != -1) {
-    RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
-                               jb_delay_ms);
+  if (field_trial::FindFullName("WebRTC-NewVideoJitterBuffer") !=
+        "Enabled") {
+    int jb_delay_ms =
+        jitter_buffer_delay_counter_.Avg(kMinRequiredDecodeSamples);
+    if (jb_delay_ms != -1) {
+      RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
+                                 jb_delay_ms);
+    }
   }
   int target_delay_ms = target_delay_counter_.Avg(kMinRequiredDecodeSamples);
   if (target_delay_ms != -1) {