Adds UMA histogram for system delay jumps

Sudden platform system delay jumps can hurt AEC and we have no stats that monitor these jumps. How often do they occur, and when they are reported are they accurate?

This CL logs all jumps in both the reported and actual delay.

The histogram has been tested with a chromium build where a fake jump of 200 ms was applied after 5 seconds and it was registered correctly in chrome://histograms

BUG=488124
R=henrik.lundin@webrtc.org, peah@webrtc.org

Review URL: https://codereview.webrtc.org/1213733004.

Cr-Commit-Position: refs/heads/master@{#9513}
diff --git a/webrtc/modules/audio_processing/audio_processing_impl.cc b/webrtc/modules/audio_processing/audio_processing_impl.cc
index 3813a9a..bf5b5eb 100644
--- a/webrtc/modules/audio_processing/audio_processing_impl.cc
+++ b/webrtc/modules/audio_processing/audio_processing_impl.cc
@@ -12,10 +12,14 @@
 
 #include <assert.h>
 
+#include "webrtc/base/checks.h"
 #include "webrtc/base/platform_file.h"
 #include "webrtc/common_audio/include/audio_util.h"
 #include "webrtc/common_audio/channel_buffer.h"
 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
+extern "C" {
+#include "webrtc/modules/audio_processing/aec/aec_core.h"
+}
 #include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
 #include "webrtc/modules/audio_processing/audio_buffer.h"
 #include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h"
@@ -33,6 +37,7 @@
 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
 #include "webrtc/system_wrappers/interface/file_wrapper.h"
 #include "webrtc/system_wrappers/interface/logging.h"
+#include "webrtc/system_wrappers/interface/metrics.h"
 
 #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
 // Files generated at build-time by the protobuf compiler.
@@ -170,6 +175,8 @@
       stream_delay_ms_(0),
       delay_offset_ms_(0),
       was_stream_delay_set_(false),
+      last_stream_delay_ms_(0),
+      last_aec_system_delay_ms_(0),
       output_will_be_muted_(false),
       key_pressed_(false),
 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
@@ -587,6 +594,8 @@
   }
 #endif
 
+  MaybeUpdateHistograms();
+
   AudioBuffer* ca = capture_audio_.get();  // For brevity.
   if (use_new_agc_ && gain_control_->is_enabled()) {
     agc_manager_->AnalyzePreProcess(ca->channels()[0],
@@ -990,6 +999,34 @@
   }
 }
 
+void AudioProcessingImpl::MaybeUpdateHistograms() {
+  static const int kMinDiffDelayMs = 50;
+
+  if (echo_cancellation()->is_enabled()) {
+    // Detect a jump in platform reported system delay and log the difference.
+    const int diff_stream_delay_ms = stream_delay_ms_ - last_stream_delay_ms_;
+    if (diff_stream_delay_ms > kMinDiffDelayMs && last_stream_delay_ms_ != 0) {
+      RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
+                           diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
+    }
+    last_stream_delay_ms_ = stream_delay_ms_;
+
+    // Detect a jump in AEC system delay and log the difference.
+    const int frames_per_ms = rtc::CheckedDivExact(split_rate_, 1000);
+    const int aec_system_delay_ms =
+        WebRtcAec_system_delay(echo_cancellation()->aec_core()) / frames_per_ms;
+    const int diff_aec_system_delay_ms = aec_system_delay_ms -
+        last_aec_system_delay_ms_;
+    if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
+        last_aec_system_delay_ms_ != 0) {
+      RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
+                           diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
+                           100);
+    }
+    last_aec_system_delay_ms_ = aec_system_delay_ms;
+  }
+}
+
 #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
 int AudioProcessingImpl::WriteMessageToDebugFile() {
   int32_t size = event_msg_->ByteSize();
diff --git a/webrtc/modules/audio_processing/audio_processing_impl.h b/webrtc/modules/audio_processing/audio_processing_impl.h
index 895294d..6f237ae 100644
--- a/webrtc/modules/audio_processing/audio_processing_impl.h
+++ b/webrtc/modules/audio_processing/audio_processing_impl.h
@@ -171,6 +171,7 @@
   void InitializeExperimentalAgc() EXCLUSIVE_LOCKS_REQUIRED(crit_);
   void InitializeTransient() EXCLUSIVE_LOCKS_REQUIRED(crit_);
   void InitializeBeamformer() EXCLUSIVE_LOCKS_REQUIRED(crit_);
+  void MaybeUpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_);
 
   EchoCancellationImpl* echo_cancellation_;
   EchoControlMobileImpl* echo_control_mobile_;
@@ -207,6 +208,8 @@
   int stream_delay_ms_;
   int delay_offset_ms_;
   bool was_stream_delay_set_;
+  int last_stream_delay_ms_;
+  int last_aec_system_delay_ms_;
 
   bool output_will_be_muted_ GUARDED_BY(crit_);
 
diff --git a/webrtc/modules/audio_processing/audio_processing_tests.gypi b/webrtc/modules/audio_processing/audio_processing_tests.gypi
index a05c67b..19b9ddf 100644
--- a/webrtc/modules/audio_processing/audio_processing_tests.gypi
+++ b/webrtc/modules/audio_processing/audio_processing_tests.gypi
@@ -111,6 +111,7 @@
             'audioproc_protobuf_utils',
             '<(DEPTH)/testing/gtest.gyp:gtest',
             '<(webrtc_root)/system_wrappers/system_wrappers.gyp:system_wrappers',
+            '<(webrtc_root)/system_wrappers/system_wrappers.gyp:system_wrappers_default',
             '<(webrtc_root)/test/test.gyp:test_support',
           ],
           'sources': [ 'test/process_test.cc', ],
@@ -123,6 +124,7 @@
             'audioproc_debug_proto',
             'audioproc_test_utils',
             'audioproc_protobuf_utils',
+            '<(webrtc_root)/system_wrappers/system_wrappers.gyp:system_wrappers_default',
             '<(webrtc_root)/test/test.gyp:test_support',
             '<(DEPTH)/third_party/gflags/gflags.gyp:gflags',
           ],