Revert "Replace the ExperimentalAgc config with the new config format"

This reverts commit f3aa6326b8e21f627b9fba72040122723251999b.

Reason for revert: Breaks downstream project.

Original change's description:
> Replace the ExperimentalAgc config with the new config format
> 
> This CL replaces the use of the ExperimentalAgc config with
> using the new config format.
> 
> Beyond that, some further changes were made to how the analog
> and digital AGCs are initialized/called. While these can be
> made in a separate CL, I believe the code changes becomes more
> clear by bundling those with the replacement of the
> ExperimentalAgc config.
> 
> TBR: saza@webrtc.org
> Bug: webrtc:5298
> Change-Id: Ia19940f3abae048541e6716d0184b4caafc7d53e
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/163986
> Reviewed-by: Per Åhgren <peah@webrtc.org>
> Commit-Queue: Per Åhgren <peah@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#30149}

TBR=saza@webrtc.org,peah@webrtc.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: webrtc:5298
Change-Id: I794d2ab4b8caa5330c5ad490ba604646a249a1c1
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/164530
Reviewed-by: Yves Gerey <yvesg@google.com>
Commit-Queue: Yves Gerey <yvesg@google.com>
Cr-Commit-Position: refs/heads/master@{#30153}
diff --git a/modules/audio_processing/agc/agc_manager_direct_unittest.cc b/modules/audio_processing/agc/agc_manager_direct_unittest.cc
index c5e65ad..b7c569b 100644
--- a/modules/audio_processing/agc/agc_manager_direct_unittest.cc
+++ b/modules/audio_processing/agc/agc_manager_direct_unittest.cc
@@ -37,7 +37,7 @@
 class MockGainControl : public GainControl {
  public:
   virtual ~MockGainControl() {}
-  MOCK_METHOD0(Initialize, void());
+  MOCK_METHOD1(Enable, int(bool enable));
   MOCK_CONST_METHOD0(is_enabled, bool());
   MOCK_METHOD1(set_stream_analog_level, int(int level));
   MOCK_CONST_METHOD0(stream_analog_level, int());
diff --git a/modules/audio_processing/agc/gain_control.h b/modules/audio_processing/agc/gain_control.h
index f8c706b..f31cbec 100644
--- a/modules/audio_processing/agc/gain_control.h
+++ b/modules/audio_processing/agc/gain_control.h
@@ -20,6 +20,9 @@
 // Recommended to be enabled on the client-side.
 class GainControl {
  public:
+  virtual int Enable(bool enable) = 0;
+  virtual bool is_enabled() const = 0;
+
   // When an analog mode is set, this must be called prior to |ProcessStream()|
   // to pass the current analog level from the audio HAL. Must be within the
   // range provided to |set_analog_level_limits()|.
diff --git a/modules/audio_processing/audio_processing_impl.cc b/modules/audio_processing/audio_processing_impl.cc
index 2844311..1c88581 100644
--- a/modules/audio_processing/audio_processing_impl.cc
+++ b/modules/audio_processing/audio_processing_impl.cc
@@ -334,7 +334,18 @@
                   std::move(render_pre_processor),
                   std::move(echo_detector),
                   std::move(capture_analyzer)),
-      constants_(!field_trial::IsEnabled(
+      constants_(config.Get<ExperimentalAgc>().startup_min_volume,
+                 config.Get<ExperimentalAgc>().clipped_level_min,
+#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
+                 /* enabled= */ false,
+                 /* enabled_agc2_level_estimator= */ false,
+                 /* digital_adaptive_disabled= */ false,
+#else
+                 config.Get<ExperimentalAgc>().enabled,
+                 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
+                 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
+#endif
+                 !field_trial::IsEnabled(
                      "WebRTC-ApmExperimentalMultiChannelRenderKillSwitch"),
                  !field_trial::IsEnabled(
                      "WebRTC-ApmExperimentalMultiChannelCaptureKillSwitch"),
@@ -353,29 +364,18 @@
   capture_nonlocked_.echo_controller_enabled =
       static_cast<bool>(echo_control_factory_);
 
+  submodules_.gain_control.reset(new GainControlImpl());
+
   // If no echo detector is injected, use the ResidualEchoDetector.
   if (!submodules_.echo_detector) {
     submodules_.echo_detector =
         new rtc::RefCountedObject<ResidualEchoDetector>();
   }
 
-#if !(defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS))
   // TODO(webrtc:5298): Remove once the use of ExperimentalNs has been
   // deprecated.
+#if !(defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS))
   config_.transient_suppression.enabled = config.Get<ExperimentalNs>().enabled;
-
-  // TODO(webrtc:5298): Remove once the use of ExperimentalAgc has been
-  // deprecated.
-  config_.gain_controller1.analog_gain_controller.enabled =
-      config.Get<ExperimentalAgc>().enabled;
-  config_.gain_controller1.analog_gain_controller.startup_min_volume =
-      config.Get<ExperimentalAgc>().startup_min_volume;
-  config_.gain_controller1.analog_gain_controller.clipped_level_min =
-      config.Get<ExperimentalAgc>().clipped_level_min;
-  config_.gain_controller1.analog_gain_controller.enable_agc2_level_estimator =
-      config.Get<ExperimentalAgc>().enabled_agc2_level_estimator;
-  config_.gain_controller1.analog_gain_controller.enable_digital_adaptive =
-      !config.Get<ExperimentalAgc>().digital_adaptive_disabled;
 #endif
 }
 
@@ -480,7 +480,34 @@
 
   AllocateRenderQueue();
 
-  InitializeGainController1();
+  submodules_.gain_control->Initialize(num_proc_channels(),
+                                       proc_sample_rate_hz());
+  if (constants_.use_experimental_agc) {
+    if (!submodules_.agc_manager.get() ||
+        submodules_.agc_manager->num_channels() !=
+            static_cast<int>(num_proc_channels()) ||
+        submodules_.agc_manager->sample_rate_hz() !=
+            capture_nonlocked_.split_rate) {
+      int stream_analog_level = -1;
+      const bool re_creation = !!submodules_.agc_manager;
+      if (re_creation) {
+        stream_analog_level = submodules_.agc_manager->stream_analog_level();
+      }
+      submodules_.agc_manager.reset(new AgcManagerDirect(
+          num_proc_channels(), constants_.agc_startup_min_volume,
+          constants_.agc_clipped_level_min,
+          constants_.use_experimental_agc_agc2_level_estimation,
+          constants_.use_experimental_agc_agc2_digital_adaptive,
+          capture_nonlocked_.split_rate));
+      if (re_creation) {
+        submodules_.agc_manager->set_stream_analog_level(stream_analog_level);
+      }
+    }
+    submodules_.agc_manager->Initialize();
+    submodules_.agc_manager->SetupDigitalGainControl(
+        submodules_.gain_control.get());
+    submodules_.agc_manager->SetCaptureMuted(capture_.output_will_be_muted);
+  }
   InitializeTransientSuppressor();
   InitializeHighPassFilter(true);
   InitializeVoiceDetector();
@@ -623,20 +650,7 @@
       config_.gain_controller1.analog_level_minimum !=
           config.gain_controller1.analog_level_minimum ||
       config_.gain_controller1.analog_level_maximum !=
-          config.gain_controller1.analog_level_maximum ||
-      config_.gain_controller1.analog_gain_controller.enabled !=
-          config.gain_controller1.analog_gain_controller.enabled ||
-      config_.gain_controller1.analog_gain_controller.startup_min_volume !=
-          config.gain_controller1.analog_gain_controller.startup_min_volume ||
-      config_.gain_controller1.analog_gain_controller.clipped_level_min !=
-          config.gain_controller1.analog_gain_controller.clipped_level_min ||
-      config_.gain_controller1.analog_gain_controller
-              .enable_agc2_level_estimator !=
-          config.gain_controller1.analog_gain_controller
-              .enable_agc2_level_estimator ||
-      config_.gain_controller1.analog_gain_controller.enable_digital_adaptive !=
-          config.gain_controller1.analog_gain_controller
-              .enable_digital_adaptive;
+          config.gain_controller1.analog_level_maximum;
 
   const bool agc2_config_changed =
       config_.gain_controller2.enabled != config.gain_controller2.enabled;
@@ -673,7 +687,7 @@
   InitializeHighPassFilter(false);
 
   if (agc1_config_changed) {
-    InitializeGainController1();
+    ApplyAgc1Config(config_.gain_controller1);
   }
 
   const bool config_ok = GainController2::Validate(config_.gain_controller2);
@@ -708,6 +722,29 @@
   }
 }
 
+void AudioProcessingImpl::ApplyAgc1Config(
+    const Config::GainController1& config) {
+  int error = submodules_.gain_control->Enable(config.enabled);
+  RTC_DCHECK_EQ(kNoError, error);
+
+  if (!submodules_.agc_manager) {
+    error = submodules_.gain_control->set_mode(
+        Agc1ConfigModeToInterfaceMode(config.mode));
+    RTC_DCHECK_EQ(kNoError, error);
+    error = submodules_.gain_control->set_target_level_dbfs(
+        config.target_level_dbfs);
+    RTC_DCHECK_EQ(kNoError, error);
+    error = submodules_.gain_control->set_compression_gain_db(
+        config.compression_gain_db);
+    RTC_DCHECK_EQ(kNoError, error);
+    error = submodules_.gain_control->enable_limiter(config.enable_limiter);
+    RTC_DCHECK_EQ(kNoError, error);
+    error = submodules_.gain_control->set_analog_level_limits(
+        config.analog_level_minimum, config.analog_level_maximum);
+    RTC_DCHECK_EQ(kNoError, error);
+  }
+}
+
 // TODO(webrtc:5298): Remove.
 void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {}
 
@@ -897,11 +934,9 @@
           setting.GetFloat(&value);
           int int_value = static_cast<int>(value + .5f);
           config_.gain_controller1.compression_gain_db = int_value;
-          if (submodules_.gain_control) {
-            int error =
-                submodules_.gain_control->set_compression_gain_db(int_value);
-            RTC_DCHECK_EQ(kNoError, error);
-          }
+          int error =
+              submodules_.gain_control->set_compression_gain_db(int_value);
+          RTC_DCHECK_EQ(kNoError, error);
         }
         break;
       }
@@ -977,7 +1012,7 @@
     }
   }
 
-  if (!submodules_.agc_manager && submodules_.gain_control) {
+  if (!submodules_.agc_manager) {
     GainControlImpl::PackRenderAudioBuffer(*audio, &agc_render_queue_buffer_);
     // Insert the samples into the queue.
     if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
@@ -1064,10 +1099,8 @@
     }
   }
 
-  if (submodules_.gain_control) {
-    while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
-      submodules_.gain_control->ProcessRenderAudio(agc_capture_queue_buffer_);
-    }
+  while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
+    submodules_.gain_control->ProcessRenderAudio(agc_capture_queue_buffer_);
   }
 
   while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
@@ -1188,7 +1221,8 @@
     submodules_.echo_controller->AnalyzeCapture(capture_buffer);
   }
 
-  if (submodules_.agc_manager) {
+  if (constants_.use_experimental_agc &&
+      submodules_.gain_control->is_enabled()) {
     submodules_.agc_manager->AnalyzePreProcess(capture_buffer);
   }
 
@@ -1215,10 +1249,7 @@
                                           /*use_split_band_data=*/true);
   }
 
-  if (submodules_.gain_control) {
-    RETURN_ON_ERR(
-        submodules_.gain_control->AnalyzeCaptureAudio(*capture_buffer));
-  }
+  RETURN_ON_ERR(submodules_.gain_control->AnalyzeCaptureAudio(*capture_buffer));
   RTC_DCHECK(
       !(submodules_.legacy_noise_suppressor && submodules_.noise_suppressor));
 
@@ -1283,21 +1314,19 @@
     capture_.stats.voice_detected = absl::nullopt;
   }
 
-  if (submodules_.agc_manager) {
+  if (constants_.use_experimental_agc &&
+      submodules_.gain_control->is_enabled()) {
     submodules_.agc_manager->Process(capture_buffer);
 
     absl::optional<int> new_digital_gain =
         submodules_.agc_manager->GetDigitalComressionGain();
-    if (new_digital_gain && submodules_.gain_control) {
+    if (new_digital_gain) {
       submodules_.gain_control->set_compression_gain_db(*new_digital_gain);
     }
   }
-
-  if (submodules_.gain_control) {
-    // TODO(peah): Add reporting from AEC3 whether there is echo.
-    RETURN_ON_ERR(submodules_.gain_control->ProcessCaptureAudio(
-        capture_buffer, /*stream_has_echo*/ false));
-  }
+  // TODO(peah): Add reporting from AEC3 whether there is echo.
+  RETURN_ON_ERR(submodules_.gain_control->ProcessCaptureAudio(
+      capture_buffer, /*stream_has_echo*/ false));
 
   if (submodule_states_.CaptureMultiBandProcessingPresent() &&
       SampleRateSupportsMultiBand(
@@ -1626,11 +1655,9 @@
     submodules_.agc_manager->set_stream_analog_level(level);
     data_dumper_->DumpRaw("experimental_gain_control_set_stream_analog_level",
                           1, &level);
-  } else if (submodules_.gain_control) {
+  } else {
     int error = submodules_.gain_control->set_stream_analog_level(level);
     RTC_DCHECK_EQ(kNoError, error);
-  } else {
-    capture_.cached_stream_analog_level_ = level;
   }
 }
 
@@ -1638,11 +1665,8 @@
   rtc::CritScope cs_capture(&crit_capture_);
   if (submodules_.agc_manager) {
     return submodules_.agc_manager->stream_analog_level();
-  } else if (submodules_.gain_control) {
-    return submodules_.gain_control->stream_analog_level();
-  } else {
-    return capture_.cached_stream_analog_level_;
   }
+  return submodules_.gain_control->stream_analog_level();
 }
 
 void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
@@ -1699,7 +1723,7 @@
       config_.high_pass_filter.enabled, !!submodules_.echo_control_mobile,
       config_.residual_echo_detector.enabled,
       !!submodules_.legacy_noise_suppressor || !!submodules_.noise_suppressor,
-      !!submodules_.gain_control, !!submodules_.gain_controller2,
+      submodules_.gain_control->is_enabled(), !!submodules_.gain_controller2,
       config_.pre_amplifier.enabled, capture_nonlocked_.echo_controller_enabled,
       config_.voice_detection.enabled, !!submodules_.transient_suppressor);
 }
@@ -1830,71 +1854,6 @@
   aecm_render_signal_queue_.reset();
 }
 
-void AudioProcessingImpl::InitializeGainController1() {
-  if (!config_.gain_controller1.enabled) {
-    submodules_.agc_manager.reset();
-    submodules_.gain_control.reset();
-    return;
-  }
-
-  if (!submodules_.gain_control) {
-    submodules_.gain_control.reset(new GainControlImpl());
-  }
-
-  submodules_.gain_control->Initialize(num_proc_channels(),
-                                       proc_sample_rate_hz());
-
-  if (!config_.gain_controller1.analog_gain_controller.enabled) {
-    int error = submodules_.gain_control->set_mode(
-        Agc1ConfigModeToInterfaceMode(config_.gain_controller1.mode));
-    RTC_DCHECK_EQ(kNoError, error);
-    error = submodules_.gain_control->set_target_level_dbfs(
-        config_.gain_controller1.target_level_dbfs);
-    RTC_DCHECK_EQ(kNoError, error);
-    error = submodules_.gain_control->set_compression_gain_db(
-        config_.gain_controller1.compression_gain_db);
-    RTC_DCHECK_EQ(kNoError, error);
-    error = submodules_.gain_control->enable_limiter(
-        config_.gain_controller1.enable_limiter);
-    RTC_DCHECK_EQ(kNoError, error);
-    error = submodules_.gain_control->set_analog_level_limits(
-        config_.gain_controller1.analog_level_minimum,
-        config_.gain_controller1.analog_level_maximum);
-    RTC_DCHECK_EQ(kNoError, error);
-
-    submodules_.agc_manager.reset();
-    return;
-  }
-
-  if (!submodules_.agc_manager.get() ||
-      submodules_.agc_manager->num_channels() !=
-          static_cast<int>(num_proc_channels()) ||
-      submodules_.agc_manager->sample_rate_hz() !=
-          capture_nonlocked_.split_rate) {
-    int stream_analog_level = -1;
-    const bool re_creation = !!submodules_.agc_manager;
-    if (re_creation) {
-      stream_analog_level = submodules_.agc_manager->stream_analog_level();
-    }
-    submodules_.agc_manager.reset(new AgcManagerDirect(
-        num_proc_channels(),
-        config_.gain_controller1.analog_gain_controller.startup_min_volume,
-        config_.gain_controller1.analog_gain_controller.clipped_level_min,
-        config_.gain_controller1.analog_gain_controller
-            .enable_agc2_level_estimator,
-        !config_.gain_controller1.analog_gain_controller
-             .enable_digital_adaptive,
-        capture_nonlocked_.split_rate));
-    if (re_creation) {
-      submodules_.agc_manager->set_stream_analog_level(stream_analog_level);
-    }
-  }
-  submodules_.agc_manager->Initialize();
-  submodules_.agc_manager->SetupDigitalGainControl(
-      submodules_.gain_control.get());
-  submodules_.agc_manager->SetCaptureMuted(capture_.output_will_be_muted);
-}
-
 void AudioProcessingImpl::InitializeGainController2() {
   if (config_.gain_controller2.enabled) {
     if (!submodules_.gain_controller2) {
@@ -1998,8 +1957,7 @@
   std::string experiments_description = "";
   // TODO(peah): Add semicolon-separated concatenations of experiment
   // descriptions for other submodules.
-  if (config_.gain_controller1.analog_gain_controller.clipped_level_min !=
-      kClippedLevelMin) {
+  if (constants_.agc_clipped_level_min != kClippedLevelMin) {
     experiments_description += "AgcClippingLevelExperiment;";
   }
   if (capture_nonlocked_.echo_controller_enabled) {
@@ -2025,14 +1983,10 @@
           ? static_cast<int>(submodules_.echo_control_mobile->routing_mode())
           : 0;
 
-  apm_config.agc_enabled = !!submodules_.gain_control;
-
-  apm_config.agc_mode = submodules_.gain_control
-                            ? static_cast<int>(submodules_.gain_control->mode())
-                            : GainControl::kAdaptiveAnalog;
+  apm_config.agc_enabled = submodules_.gain_control->is_enabled();
+  apm_config.agc_mode = static_cast<int>(submodules_.gain_control->mode());
   apm_config.agc_limiter_enabled =
-      submodules_.gain_control ? submodules_.gain_control->is_limiter_enabled()
-                               : false;
+      submodules_.gain_control->is_limiter_enabled();
   apm_config.noise_robust_agc_enabled = !!submodules_.agc_manager;
 
   apm_config.hpf_enabled = config_.high_pass_filter.enabled;
diff --git a/modules/audio_processing/audio_processing_impl.h b/modules/audio_processing/audio_processing_impl.h
index af5a0f6..ee3fb4d 100644
--- a/modules/audio_processing/audio_processing_impl.h
+++ b/modules/audio_processing/audio_processing_impl.h
@@ -243,7 +243,6 @@
   void InitializeHighPassFilter(bool forced_reset)
       RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
   void InitializeVoiceDetector() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
-  void InitializeGainController1() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
   void InitializeTransientSuppressor()
       RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
   void InitializeGainController2() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
@@ -264,6 +263,8 @@
   void HandleCaptureRuntimeSettings()
       RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
   void HandleRenderRuntimeSettings() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
+  void ApplyAgc1Config(const Config::GainController1& agc_config)
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
 
   void EmptyQueuedRenderAudio();
   void AllocateRenderQueue()
@@ -380,12 +381,29 @@
 
   // APM constants.
   const struct ApmConstants {
-    ApmConstants(bool multi_channel_render_support,
+    ApmConstants(int agc_startup_min_volume,
+                 int agc_clipped_level_min,
+                 bool use_experimental_agc,
+                 bool use_experimental_agc_agc2_level_estimation,
+                 bool use_experimental_agc_agc2_digital_adaptive,
+                 bool multi_channel_render_support,
                  bool multi_channel_capture_support,
                  bool enforce_split_band_hpf)
-        : multi_channel_render_support(multi_channel_render_support),
+        : agc_startup_min_volume(agc_startup_min_volume),
+          agc_clipped_level_min(agc_clipped_level_min),
+          use_experimental_agc(use_experimental_agc),
+          use_experimental_agc_agc2_level_estimation(
+              use_experimental_agc_agc2_level_estimation),
+          use_experimental_agc_agc2_digital_adaptive(
+              use_experimental_agc_agc2_digital_adaptive),
+          multi_channel_render_support(multi_channel_render_support),
           multi_channel_capture_support(multi_channel_capture_support),
           enforce_split_band_hpf(enforce_split_band_hpf) {}
+    int agc_startup_min_volume;
+    int agc_clipped_level_min;
+    bool use_experimental_agc;
+    bool use_experimental_agc_agc2_level_estimation;
+    bool use_experimental_agc_agc2_digital_adaptive;
     bool multi_channel_render_support;
     bool multi_channel_capture_support;
     bool enforce_split_band_hpf;
@@ -417,7 +435,6 @@
       size_t num_keyboard_frames = 0;
       const float* keyboard_data = nullptr;
     } keyboard_info;
-    int cached_stream_analog_level_ = 0;
   } capture_ RTC_GUARDED_BY(crit_capture_);
 
   struct ApmCaptureNonLockedState {
diff --git a/modules/audio_processing/audio_processing_unittest.cc b/modules/audio_processing/audio_processing_unittest.cc
index ca05f71..8f9e535 100644
--- a/modules/audio_processing/audio_processing_unittest.cc
+++ b/modules/audio_processing/audio_processing_unittest.cc
@@ -430,9 +430,10 @@
       far_file_(NULL),
       near_file_(NULL),
       out_file_(NULL) {
-  apm_.reset(AudioProcessingBuilder().Create());
+  Config config;
+  config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
+  apm_.reset(AudioProcessingBuilder().Create(config));
   AudioProcessing::Config apm_config = apm_->GetConfig();
-  apm_config.gain_controller1.analog_gain_controller.enabled = false;
   apm_config.pipeline.maximum_internal_processing_rate = 48000;
   apm_->ApplyConfig(apm_config);
 }
@@ -966,49 +967,42 @@
 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
 TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
   auto config = apm_->GetConfig();
-  config.gain_controller1.enabled = true;
   config.gain_controller1.target_level_dbfs = -1;
   EXPECT_DEATH(apm_->ApplyConfig(config), "");
 }
 
 TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
   auto config = apm_->GetConfig();
-  config.gain_controller1.enabled = true;
   config.gain_controller1.target_level_dbfs = 32;
   EXPECT_DEATH(apm_->ApplyConfig(config), "");
 }
 
 TEST_F(ApmTest, GainControlDiesOnTooLowCompressionGainDb) {
   auto config = apm_->GetConfig();
-  config.gain_controller1.enabled = true;
   config.gain_controller1.compression_gain_db = -1;
   EXPECT_DEATH(apm_->ApplyConfig(config), "");
 }
 
 TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
   auto config = apm_->GetConfig();
-  config.gain_controller1.enabled = true;
   config.gain_controller1.compression_gain_db = 91;
   EXPECT_DEATH(apm_->ApplyConfig(config), "");
 }
 
 TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
   auto config = apm_->GetConfig();
-  config.gain_controller1.enabled = true;
   config.gain_controller1.analog_level_minimum = -1;
   EXPECT_DEATH(apm_->ApplyConfig(config), "");
 }
 
 TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
   auto config = apm_->GetConfig();
-  config.gain_controller1.enabled = true;
   config.gain_controller1.analog_level_maximum = 65536;
   EXPECT_DEATH(apm_->ApplyConfig(config), "");
 }
 
 TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
   auto config = apm_->GetConfig();
-  config.gain_controller1.enabled = true;
   config.gain_controller1.analog_level_minimum = 512;
   config.gain_controller1.analog_level_maximum = 255;
   EXPECT_DEATH(apm_->ApplyConfig(config), "");
@@ -1016,7 +1010,6 @@
 
 TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
   auto config = apm_->GetConfig();
-  config.gain_controller1.enabled = true;
   config.gain_controller1.analog_level_minimum = 255;
   config.gain_controller1.analog_level_maximum = 512;
   apm_->ApplyConfig(config);
@@ -1025,7 +1018,6 @@
 
 TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
   auto config = apm_->GetConfig();
-  config.gain_controller1.enabled = true;
   config.gain_controller1.analog_level_minimum = 255;
   config.gain_controller1.analog_level_maximum = 512;
   apm_->ApplyConfig(config);
@@ -1541,10 +1533,9 @@
     if (test->num_input_channels() != test->num_output_channels())
       continue;
 
-    apm_.reset(AudioProcessingBuilder().Create());
-    AudioProcessing::Config apm_config = apm_->GetConfig();
-    apm_config.gain_controller1.analog_gain_controller.enabled = false;
-    apm_->ApplyConfig(apm_config);
+    Config config;
+    config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
+    apm_.reset(AudioProcessingBuilder().Create(config));
 
     EnableAllComponents();
 
@@ -1827,11 +1818,10 @@
                             size_t num_reverse_input_channels,
                             size_t num_reverse_output_channels,
                             const std::string& output_file_prefix) {
-    std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
-    AudioProcessing::Config apm_config = ap->GetConfig();
-    apm_config.gain_controller1.analog_gain_controller.enabled = false;
-    ap->ApplyConfig(apm_config);
-
+    Config config;
+    config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
+    std::unique_ptr<AudioProcessing> ap(
+        AudioProcessingBuilder().Create(config));
     EnableAllAPComponents(ap.get());
 
     ProcessingConfig processing_config = {
diff --git a/modules/audio_processing/gain_control_impl.cc b/modules/audio_processing/gain_control_impl.cc
index b5454c0..841d901 100644
--- a/modules/audio_processing/gain_control_impl.cc
+++ b/modules/audio_processing/gain_control_impl.cc
@@ -112,6 +112,10 @@
 
 void GainControlImpl::ProcessRenderAudio(
     rtc::ArrayView<const int16_t> packed_render_audio) {
+  if (!enabled_) {
+    return;
+  }
+
   for (size_t ch = 0; ch < mono_agcs_.size(); ++ch) {
     WebRtcAgc_AddFarend(mono_agcs_[ch]->state, packed_render_audio.data(),
                         packed_render_audio.size());
@@ -147,6 +151,10 @@
 }
 
 int GainControlImpl::AnalyzeCaptureAudio(const AudioBuffer& audio) {
+  if (!enabled_) {
+    return AudioProcessing::kNoError;
+  }
+
   RTC_DCHECK(num_proc_channels_);
   RTC_DCHECK_GE(AudioBuffer::kMaxSplitFrameLength, audio.num_frames_per_band());
   RTC_DCHECK_EQ(audio.num_channels(), *num_proc_channels_);
@@ -195,6 +203,10 @@
 
 int GainControlImpl::ProcessCaptureAudio(AudioBuffer* audio,
                                          bool stream_has_echo) {
+  if (!enabled_) {
+    return AudioProcessing::kNoError;
+  }
+
   if (mode_ == kAdaptiveAnalog && !was_analog_level_set_) {
     return AudioProcessing::kStreamParameterNotSetError;
   }
@@ -297,6 +309,19 @@
   return analog_capture_level_;
 }
 
+int GainControlImpl::Enable(bool enable) {
+  if (enable && !enabled_) {
+    enabled_ = enable;  // Must be set before Initialize() is called.
+
+    RTC_DCHECK(num_proc_channels_);
+    RTC_DCHECK(sample_rate_hz_);
+    Initialize(*num_proc_channels_, *sample_rate_hz_);
+  } else {
+    enabled_ = enable;
+  }
+  return AudioProcessing::kNoError;
+}
+
 int GainControlImpl::set_mode(Mode mode) {
   if (MapSetting(mode) == -1) {
     return AudioProcessing::kBadParameterError;
@@ -356,6 +381,10 @@
   num_proc_channels_ = num_proc_channels;
   sample_rate_hz_ = sample_rate_hz;
 
+  if (!enabled_) {
+    return;
+  }
+
   mono_agcs_.resize(*num_proc_channels_);
   capture_levels_.resize(*num_proc_channels_);
   for (size_t ch = 0; ch < mono_agcs_.size(); ++ch) {
diff --git a/modules/audio_processing/gain_control_impl.h b/modules/audio_processing/gain_control_impl.h
index b65d697..5ddf5ec 100644
--- a/modules/audio_processing/gain_control_impl.h
+++ b/modules/audio_processing/gain_control_impl.h
@@ -44,9 +44,11 @@
                                     std::vector<int16_t>* packed_buffer);
 
   // GainControl implementation.
+  bool is_enabled() const override { return enabled_; }
   int stream_analog_level() const override;
   bool is_limiter_enabled() const override { return limiter_enabled_; }
   Mode mode() const override { return mode_; }
+  int Enable(bool enable) override;
   int set_mode(Mode mode) override;
   int compression_gain_db() const override { return compression_gain_db_; }
   int set_analog_level_limits(int minimum, int maximum) override;
@@ -68,6 +70,8 @@
 
   std::unique_ptr<ApmDataDumper> data_dumper_;
 
+  bool enabled_ = false;
+
   const bool use_legacy_gain_applier_;
   Mode mode_;
   int minimum_capture_level_;
@@ -75,7 +79,7 @@
   bool limiter_enabled_;
   int target_level_dbfs_;
   int compression_gain_db_;
-  int analog_capture_level_ = 0;
+  int analog_capture_level_;
   bool was_analog_level_set_;
   bool stream_is_saturated_;
 
diff --git a/modules/audio_processing/gain_control_unittest.cc b/modules/audio_processing/gain_control_unittest.cc
index 6e01499..c1078b4 100644
--- a/modules/audio_processing/gain_control_unittest.cc
+++ b/modules/audio_processing/gain_control_unittest.cc
@@ -52,6 +52,7 @@
                     GainControlImpl* gain_controller) {
   gain_controller->Initialize(1, sample_rate_hz);
   GainControl* gc = static_cast<GainControl*>(gain_controller);
+  gc->Enable(true);
   gc->set_mode(mode);
   gc->set_stream_analog_level(stream_analog_level);
   gc->set_target_level_dbfs(target_level_dbfs);
diff --git a/modules/audio_processing/include/audio_processing.h b/modules/audio_processing/include/audio_processing.h
index d76d1a8..fe4b0dc 100644
--- a/modules/audio_processing/include/audio_processing.h
+++ b/modules/audio_processing/include/audio_processing.h
@@ -60,10 +60,6 @@
 static const int kAgcStartupMinVolume = 0;
 #endif  // defined(WEBRTC_CHROMIUM_BUILD)
 static constexpr int kClippedLevelMin = 70;
-
-// To be deprecated: Please instead use the flag in the
-// AudioProcessing::Config::AnalogGainController.
-// TODO(webrtc:5298): Remove.
 struct ExperimentalAgc {
   ExperimentalAgc() = default;
   explicit ExperimentalAgc(bool enabled) : enabled(enabled) {}
@@ -318,17 +314,6 @@
       // Must be set if an analog mode is used. Limited to [0, 65535].
       int analog_level_minimum = 0;
       int analog_level_maximum = 255;
-
-      // Enables the analog gain controller functionality.
-      struct AnalogGainController {
-        bool enabled = false;
-        int startup_min_volume = kAgcStartupMinVolume;
-        // Lowest analog microphone level that will be applied in response to
-        // clipping.
-        int clipped_level_min = kClippedLevelMin;
-        bool enable_agc2_level_estimator = false;
-        bool enable_digital_adaptive = true;
-      } analog_gain_controller;
     } gain_controller1;
 
     // Enables the next generation AGC functionality. This feature replaces the
diff --git a/modules/audio_processing/test/aec_dump_based_simulator.cc b/modules/audio_processing/test/aec_dump_based_simulator.cc
index 142e707..95a3e37 100644
--- a/modules/audio_processing/test/aec_dump_based_simulator.cc
+++ b/modules/audio_processing/test/aec_dump_based_simulator.cc
@@ -364,10 +364,11 @@
       }
     }
 
+    // TODO(peah): Add support for controlling the Experimental AGC from the
+    // command line.
     if (msg.has_noise_robust_agc_enabled()) {
-      apm_config.gain_controller1.analog_gain_controller.enabled =
-          settings_.use_analog_agc ? *settings_.use_analog_agc
-                                   : msg.noise_robust_agc_enabled();
+      config.Set<ExperimentalAgc>(
+          new ExperimentalAgc(msg.noise_robust_agc_enabled()));
       if (settings_.use_verbose_logging) {
         std::cout << " noise_robust_agc_enabled: "
                   << (msg.noise_robust_agc_enabled() ? "true" : "false")
diff --git a/modules/audio_processing/test/audio_processing_simulator.cc b/modules/audio_processing/test/audio_processing_simulator.cc
index 84cd9a0..f314732 100644
--- a/modules/audio_processing/test/audio_processing_simulator.cc
+++ b/modules/audio_processing/test/audio_processing_simulator.cc
@@ -494,20 +494,15 @@
     apm_config.gain_controller1.compression_gain_db =
         *settings_.agc_compression_gain;
   }
-  if (settings_.use_analog_agc) {
-    apm_config.gain_controller1.analog_gain_controller.enabled =
-        *settings_.use_analog_agc;
-  }
-  if (settings_.use_analog_agc_agc2_level_estimator) {
-    apm_config.gain_controller1.analog_gain_controller
-        .enable_agc2_level_estimator =
-        *settings_.use_analog_agc_agc2_level_estimator;
-  }
-  if (settings_.analog_agc_disable_digital_adaptive) {
-    apm_config.gain_controller1.analog_gain_controller.enable_digital_adaptive =
-        *settings_.analog_agc_disable_digital_adaptive;
-  }
 
+  config.Set<ExperimentalAgc>(new ExperimentalAgc(
+      !settings_.use_experimental_agc || *settings_.use_experimental_agc,
+      !!settings_.use_experimental_agc_agc2_level_estimator &&
+          *settings_.use_experimental_agc_agc2_level_estimator,
+      !!settings_.experimental_agc_disable_digital_adaptive &&
+          *settings_.experimental_agc_disable_digital_adaptive,
+      !!settings_.experimental_agc_analyze_before_aec &&
+          *settings_.experimental_agc_analyze_before_aec));
   if (settings_.use_ed) {
     apm_config.residual_echo_detector.enabled = *settings_.use_ed;
   }
diff --git a/modules/audio_processing/test/audio_processing_simulator.h b/modules/audio_processing/test/audio_processing_simulator.h
index c28dd6d..c902d7c 100644
--- a/modules/audio_processing/test/audio_processing_simulator.h
+++ b/modules/audio_processing/test/audio_processing_simulator.h
@@ -57,13 +57,14 @@
   absl::optional<bool> use_hpf;
   absl::optional<bool> use_ns;
   absl::optional<bool> use_ts;
-  absl::optional<bool> use_analog_agc;
   absl::optional<bool> use_vad;
   absl::optional<bool> use_le;
   absl::optional<bool> use_all;
   absl::optional<bool> use_legacy_ns;
-  absl::optional<bool> use_analog_agc_agc2_level_estimator;
-  absl::optional<bool> analog_agc_disable_digital_adaptive;
+  absl::optional<bool> use_experimental_agc;
+  absl::optional<bool> use_experimental_agc_agc2_level_estimator;
+  absl::optional<bool> experimental_agc_disable_digital_adaptive;
+  absl::optional<bool> experimental_agc_analyze_before_aec;
   absl::optional<int> agc_mode;
   absl::optional<int> agc_target_level;
   absl::optional<bool> use_agc_limiter;
diff --git a/modules/audio_processing/test/audioproc_float_impl.cc b/modules/audio_processing/test/audioproc_float_impl.cc
index ec637c1..c4d2ec2 100644
--- a/modules/audio_processing/test/audioproc_float_impl.cc
+++ b/modules/audio_processing/test/audioproc_float_impl.cc
@@ -102,10 +102,6 @@
           kParameterNotSpecifiedValue,
           "Activate (1) or deactivate(0) the transient suppressor");
 ABSL_FLAG(int,
-          analog_agc,
-          kParameterNotSpecifiedValue,
-          "Activate (1) or deactivate(0) the transient suppressor");
-ABSL_FLAG(int,
           vad,
           kParameterNotSpecifiedValue,
           "Activate (1) or deactivate(0) the voice activity detector");
@@ -123,12 +119,21 @@
           kParameterNotSpecifiedValue,
           "Activate (1) or deactivate(0) the legacy NS");
 ABSL_FLAG(int,
-          analog_agc_disable_digital_adaptive,
+          experimental_agc,
+          kParameterNotSpecifiedValue,
+          "Activate (1) or deactivate(0) the experimental AGC");
+ABSL_FLAG(int,
+          experimental_agc_disable_digital_adaptive,
           kParameterNotSpecifiedValue,
           "Force-deactivate (1) digital adaptation in "
           "experimental AGC. Digital adaptation is active by default (0).");
 ABSL_FLAG(int,
-          analog_agc_agc2_level_estimator,
+          experimental_agc_analyze_before_aec,
+          kParameterNotSpecifiedValue,
+          "Make level estimation happen before AEC"
+          " in the experimental AGC. After AEC is the default (0)");
+ABSL_FLAG(int,
+          experimental_agc_agc2_level_estimator,
           kParameterNotSpecifiedValue,
           "AGC2 level estimation"
           " in the experimental AGC. AGC1 level estimation is the default (0)");
@@ -329,7 +334,6 @@
     settings.use_le = true;
     settings.use_vad = true;
     settings.use_ts = true;
-    settings.use_analog_agc = true;
     settings.use_ns = true;
     settings.use_hpf = true;
     settings.use_agc = true;
@@ -373,16 +377,20 @@
   SetSettingIfFlagSet(absl::GetFlag(FLAGS_hpf), &settings.use_hpf);
   SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns), &settings.use_ns);
   SetSettingIfFlagSet(absl::GetFlag(FLAGS_ts), &settings.use_ts);
-  SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc),
-                      &settings.use_analog_agc);
   SetSettingIfFlagSet(absl::GetFlag(FLAGS_vad), &settings.use_vad);
   SetSettingIfFlagSet(absl::GetFlag(FLAGS_le), &settings.use_le);
   SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_legacy_ns),
                       &settings.use_legacy_ns);
-  SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc_disable_digital_adaptive),
-                      &settings.analog_agc_disable_digital_adaptive);
-  SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc_agc2_level_estimator),
-                      &settings.use_analog_agc_agc2_level_estimator);
+  SetSettingIfFlagSet(absl::GetFlag(FLAGS_experimental_agc),
+                      &settings.use_experimental_agc);
+  SetSettingIfFlagSet(
+      absl::GetFlag(FLAGS_experimental_agc_disable_digital_adaptive),
+      &settings.experimental_agc_disable_digital_adaptive);
+  SetSettingIfFlagSet(absl::GetFlag(FLAGS_experimental_agc_analyze_before_aec),
+                      &settings.experimental_agc_analyze_before_aec);
+  SetSettingIfFlagSet(
+      absl::GetFlag(FLAGS_experimental_agc_agc2_level_estimator),
+      &settings.use_experimental_agc_agc2_level_estimator);
   SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_mode), &settings.agc_mode);
   SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_target_level),
                         &settings.agc_target_level);
diff --git a/modules/audio_processing/test/debug_dump_replayer.cc b/modules/audio_processing/test/debug_dump_replayer.cc
index 26ca429..d5cf673 100644
--- a/modules/audio_processing/test/debug_dump_replayer.cc
+++ b/modules/audio_processing/test/debug_dump_replayer.cc
@@ -180,6 +180,11 @@
   // These configurations cannot be changed on the fly.
   Config config;
   RTC_CHECK(msg.has_aec_delay_agnostic_enabled());
+
+  RTC_CHECK(msg.has_noise_robust_agc_enabled());
+  config.Set<ExperimentalAgc>(
+      new ExperimentalAgc(msg.noise_robust_agc_enabled()));
+
   RTC_CHECK(msg.has_aec_extended_filter_enabled());
 
   // We only create APM once, since changes on these fields should not
@@ -230,9 +235,6 @@
       static_cast<AudioProcessing::Config::GainController1::Mode>(
           msg.agc_mode());
   apm_config.gain_controller1.enable_limiter = msg.agc_limiter_enabled();
-  RTC_CHECK(msg.has_noise_robust_agc_enabled());
-  apm_config.gain_controller1.analog_gain_controller.enabled =
-      msg.noise_robust_agc_enabled();
 
   apm_->ApplyConfig(apm_config);
 }
diff --git a/modules/audio_processing/test/debug_dump_test.cc b/modules/audio_processing/test/debug_dump_test.cc
index 71478a9..21458aa 100644
--- a/modules/audio_processing/test/debug_dump_test.cc
+++ b/modules/audio_processing/test/debug_dump_test.cc
@@ -210,7 +210,6 @@
     ReadAndDeinterleave(&input_audio_, input_file_channels_, input_config_,
                         input_->channels());
     RTC_CHECK_EQ(AudioProcessing::kNoError, apm_->set_stream_delay_ms(100));
-    apm_->set_stream_analog_level(100);
     if (enable_pre_amplifier_) {
       apm_->SetRuntimeSetting(
           AudioProcessing::RuntimeSetting::CreateCapturePreGain(1 + i % 10));
@@ -359,10 +358,8 @@
   Config config;
   AudioProcessing::Config apm_config;
   apm_config.echo_canceller.enabled = true;
-  apm_config.gain_controller1.analog_gain_controller.enabled = true;
-  apm_config.gain_controller1.analog_gain_controller.startup_min_volume = 0;
   // Arbitrarily set clipping gain to 17, which will never be the default.
-  apm_config.gain_controller1.analog_gain_controller.clipped_level_min = 17;
+  config.Set<ExperimentalAgc>(new ExperimentalAgc(true, 0, 17));
   DebugDumpGenerator generator(config, apm_config);
   generator.StartRecording();
   generator.Process(100);
@@ -439,12 +436,9 @@
 
 TEST_F(DebugDumpTest, VerifyAgcClippingLevelExperimentalString) {
   Config config;
-  AudioProcessing::Config apm_config;
-  apm_config.gain_controller1.analog_gain_controller.enabled = true;
-  apm_config.gain_controller1.analog_gain_controller.startup_min_volume = 0;
   // Arbitrarily set clipping gain to 17, which will never be the default.
-  apm_config.gain_controller1.analog_gain_controller.clipped_level_min = 17;
-  DebugDumpGenerator generator(config, apm_config);
+  config.Set<ExperimentalAgc>(new ExperimentalAgc(true, 0, 17));
+  DebugDumpGenerator generator(config, AudioProcessing::Config());
   generator.StartRecording();
   generator.Process(100);
   generator.StopRecording();
diff --git a/test/fuzzers/agc_fuzzer.cc b/test/fuzzers/agc_fuzzer.cc
index 890649a..ac3f83b 100644
--- a/test/fuzzers/agc_fuzzer.cc
+++ b/test/fuzzers/agc_fuzzer.cc
@@ -67,7 +67,9 @@
   }
   gc->set_compression_gain_db(gain);
   gc->set_target_level_dbfs(target_level_dbfs);
+  gc->Enable(true);
 
+  static_cast<void>(gc->is_enabled());
   static_cast<void>(gc->mode());
   static_cast<void>(gc->analog_level_minimum());
   static_cast<void>(gc->analog_level_maximum());