Add IncreaseInputAudioBufferSize feature on Windows.

Bug: 826664, 830624
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win_optional_gpu_tests_rel
Change-Id: I61b09bfbc5c71d0f8573da8d315475b32727f289
Reviewed-on: https://chromium-review.googlesource.com/1002851
Commit-Queue: Henrik Grunell <[email protected]>
Reviewed-by: Alexei Svitkine <[email protected]>
Reviewed-by: Max Morin <[email protected]>
Cr-Commit-Position: refs/heads/master@{#549516}
diff --git a/chrome/browser/about_flags.cc b/chrome/browser/about_flags.cc
index 597bd46..d89c684 100644
--- a/chrome/browser/about_flags.cc
+++ b/chrome/browser/about_flags.cc
@@ -3826,6 +3826,13 @@
      FEATURE_VALUE_TYPE(app_list::features::kEnableHomeLauncher)},
 #endif  // OS_CHROMEOS
 
+#if defined(OS_WIN)
+    {"increase-input-audio-buffer-size",
+     flag_descriptions::kIncreaseInputAudioBufferSize,
+     flag_descriptions::kIncreaseInputAudioBufferSizeDescription, kOsWin,
+     FEATURE_VALUE_TYPE(features::kIncreaseInputAudioBufferSize)},
+#endif  // OS_WIN
+
     // NOTE: Adding a new flag requires adding a corresponding entry to enum
     // "LoginCustomFlags" in tools/metrics/histograms/enums.xml. See "Flag
     // Histograms" in tools/metrics/histograms/README.md (run the
diff --git a/chrome/browser/flag_descriptions.cc b/chrome/browser/flag_descriptions.cc
index 2a5a8a4..af77c42c 100644
--- a/chrome/browser/flag_descriptions.cc
+++ b/chrome/browser/flag_descriptions.cc
@@ -2474,6 +2474,10 @@
 const char kGdiTextPrintingDescription[] =
     "Use GDI to print text as simply text";
 
+const char kIncreaseInputAudioBufferSize[] = "Increase input audio buffer size";
+const char kIncreaseInputAudioBufferSizeDescription[] =
+    "Increases the input audio endpoint buffer to 100 ms.";
+
 const char kTraceExportEventsToEtwName[] =
     "Enable exporting of tracing events to ETW.";
 const char kTraceExportEventsToEtwDesription[] =
diff --git a/chrome/browser/flag_descriptions.h b/chrome/browser/flag_descriptions.h
index ecb61df..a70e42ad 100644
--- a/chrome/browser/flag_descriptions.h
+++ b/chrome/browser/flag_descriptions.h
@@ -1506,6 +1506,9 @@
 extern const char kGdiTextPrinting[];
 extern const char kGdiTextPrintingDescription[];
 
+extern const char kIncreaseInputAudioBufferSize[];
+extern const char kIncreaseInputAudioBufferSizeDescription[];
+
 extern const char kTraceExportEventsToEtwName[];
 extern const char kTraceExportEventsToEtwDesription[];
 
diff --git a/media/audio/audio_features.cc b/media/audio/audio_features.cc
index a468e2f84b..f5b297b 100644
--- a/media/audio/audio_features.cc
+++ b/media/audio/audio_features.cc
@@ -13,4 +13,10 @@
                                            base::FEATURE_ENABLED_BY_DEFAULT};
 #endif
 
+#if defined(OS_WIN)
+// Increases the input audio endpoint buffer size. http://crbug.com/830624.
+const base::Feature kIncreaseInputAudioBufferSize{
+    "IncreaseInputAudioBufferSize", base::FEATURE_DISABLED_BY_DEFAULT};
+#endif
+
 }  // namespace features
diff --git a/media/audio/audio_features.h b/media/audio/audio_features.h
index 6fc3946..ae3c12d4 100644
--- a/media/audio/audio_features.h
+++ b/media/audio/audio_features.h
@@ -13,8 +13,12 @@
 
 #if defined(OS_CHROMEOS)
 MEDIA_EXPORT extern const base::Feature kEnumerateAudioDevices;
-#endif  // defined(OS_CHROMEOS)
+#endif
 
-}  // features
+#if defined(OS_WIN)
+MEDIA_EXPORT extern const base::Feature kIncreaseInputAudioBufferSize;
+#endif
+
+}  // namespace features
 
 #endif  // MEDIA_AUDIO_AUDIO_FEATURES_H_
diff --git a/media/audio/win/audio_low_latency_input_win.cc b/media/audio/win/audio_low_latency_input_win.cc
index 5f74aa9..3edb5de6 100644
--- a/media/audio/win/audio_low_latency_input_win.cc
+++ b/media/audio/win/audio_low_latency_input_win.cc
@@ -17,6 +17,7 @@
 #include "base/strings/utf_string_conversions.h"
 #include "base/trace_event/trace_event.h"
 #include "media/audio/audio_device_description.h"
+#include "media/audio/audio_features.h"
 #include "media/audio/win/audio_manager_win.h"
 #include "media/audio/win/avrt_wrapper_win.h"
 #include "media/audio/win/core_audio_util_win.h"
@@ -795,14 +796,20 @@
 
   // Initialize the audio stream between the client and the device.
   // We connect indirectly through the audio engine by using shared mode.
-  // Note that, |hnsBufferDuration| is set of 0, which ensures that the
-  // buffer is never smaller than the minimum buffer size needed to ensure
-  // that glitches do not occur between the periodic processing passes.
-  // This setting should lead to lowest possible latency.
+  // The buffer duration is normally set to 0, which ensures that the buffer
+  // size is the minimum buffer size needed to ensure that glitches do not occur
+  // between the periodic processing passes. It can be set to 100 ms via a
+  // feature.
+  // Note: if the value is changed, update the description in
+  // chrome/browser/flag_descriptions.cc.
+  REFERENCE_TIME buffer_duration =
+      base::FeatureList::IsEnabled(features::kIncreaseInputAudioBufferSize)
+          ? 100 * 1000 * 10  // 100 ms expressed in 100-ns units.
+          : 0;
   HRESULT hr = audio_client_->Initialize(
-      AUDCLNT_SHAREMODE_SHARED, flags,
-      0,  // hnsBufferDuration
-      0, &input_format_,
+      AUDCLNT_SHAREMODE_SHARED, flags, buffer_duration,
+      0,  // device period, n/a for shared mode.
+      &input_format_,
       device_id_ == AudioDeviceDescription::kCommunicationsDeviceId
           ? &kCommunicationsSessionId
           : nullptr);
diff --git a/tools/metrics/histograms/enums.xml b/tools/metrics/histograms/enums.xml
index d2df41e0..9a66953 100644
--- a/tools/metrics/histograms/enums.xml
+++ b/tools/metrics/histograms/enums.xml
@@ -26738,6 +26738,7 @@
   <int value="-195029497" label="MediaRemoting:disabled"/>
   <int value="-192919826" label="ViewsSimplifiedFullscreenUI:enabled"/>
   <int value="-192389983" label="NoStatePrefetch:enabled"/>
+  <int value="-185162926" label="IncreaseInputAudioBufferSize:enabled"/>
   <int value="-183246373" label="enable-multilingual-spellchecker"/>
   <int value="-181093956" label="ScrollAnchoring:enabled"/>
   <int value="-174319545" label="BulkPrinters:enabled"/>
@@ -27691,6 +27692,7 @@
   <int value="2101151142" label="disable-direct-write"/>
   <int value="2104788328" label="use-winrt-midi-api"/>
   <int value="2119964154" label="enable-download-resumption"/>
+  <int value="2121056855" label="IncreaseInputAudioBufferSize:disabled"/>
   <int value="2121550859" label="PreferHtmlOverPlugins:enabled"/>
   <int value="2121776031" label="auto-virtual-keyboard"/>
   <int value="2122023503" label="enable-win32k-lockdown-mimetypes"/>