Activate the pre-amplifier in AudioProcessing.

It's a module for applying a gain to the capture signal.
The gain is the first processing step in APM.

After this CL, these two features work:
* The PreAmplifier can be activated with
  AudioProcessing::Config::pre_amplifier
* The PreApmlifier can be controlled after APM creation by
  AudioProcessing::SetRuntimeSetting.

What's left is a change to AecDumps and to AecDump-replay.

NOTRY=True # 1-line change, tests just passed.

Bug: webrtc:9138
Change-Id: I85b3af511695b0a9cec2eed6fee7f05080305e1d
Reviewed-on: https://webrtc-review.googlesource.com/69811
Commit-Queue: Alex Loiko <aleloi@webrtc.org>
Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22881}
diff --git a/modules/audio_processing/audio_processing_impl_unittest.cc b/modules/audio_processing/audio_processing_impl_unittest.cc
index cd2bba6..2ea0159 100644
--- a/modules/audio_processing/audio_processing_impl_unittest.cc
+++ b/modules/audio_processing/audio_processing_impl_unittest.cc
@@ -33,6 +33,24 @@
   MOCK_CONST_METHOD0(Release, rtc::RefCountReleaseStatus());
 };
 
+void GenerateFixedFrame(int16_t audio_level,
+                        size_t input_rate,
+                        size_t num_channels,
+                        AudioFrame* fixed_frame) {
+  const size_t samples_per_input_channel = rtc::CheckedDivExact(
+      input_rate, static_cast<size_t>(rtc::CheckedDivExact(
+                      1000, AudioProcessing::kChunkSizeMs)));
+  fixed_frame->samples_per_channel_ = samples_per_input_channel;
+  fixed_frame->sample_rate_hz_ = input_rate;
+  fixed_frame->num_channels_ = num_channels;
+
+  RTC_DCHECK_LE(samples_per_input_channel * num_channels,
+                AudioFrame::kMaxDataSizeSamples);
+  for (size_t i = 0; i < samples_per_input_channel * num_channels; ++i) {
+    fixed_frame->mutable_data()[i] = audio_level;
+  }
+}
+
 }  // namespace
 
 TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) {
@@ -75,9 +93,33 @@
 }
 
 TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) {
-  // TODO(bugs.chromium.org/9138): Implement this test as soon as the pre-gain
-  // sub-module is implemented and it is notified by HandleRuntimeSettings()
-  // when the gain changes.
+  std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
+  webrtc::AudioProcessing::Config apm_config;
+  apm_config.pre_amplifier.enabled = true;
+  apm_config.pre_amplifier.fixed_gain_factor = 1.f;
+  apm->ApplyConfig(apm_config);
+
+  AudioFrame frame;
+  constexpr int16_t audio_level = 10000;
+  constexpr size_t input_rate = 48000;
+  constexpr size_t num_channels = 2;
+
+  GenerateFixedFrame(audio_level, input_rate, num_channels, &frame);
+  apm->ProcessStream(&frame);
+  EXPECT_EQ(frame.data()[100], audio_level)
+      << "With factor 1, frame shouldn't be modified.";
+
+  constexpr float gain_factor = 2.f;
+  apm->SetRuntimeSetting(
+      AudioProcessing::RuntimeSetting::CreateCapturePreGain(gain_factor));
+
+  // Process for two frames to have time to ramp up gain.
+  for (int i = 0; i < 2; ++i) {
+    GenerateFixedFrame(audio_level, input_rate, num_channels, &frame);
+    apm->ProcessStream(&frame);
+  }
+  EXPECT_EQ(frame.data()[100], gain_factor * audio_level)
+      << "Frame should be amplified.";
 }
 
 }  // namespace webrtc