Change the way that AecDumps are created in APM

This CL changes the way that AecDumps are created in APM. Instead
of being injected, they are now created via the API.

This removes the AecDumpFactory from the API surface of APM and
makes the API more explicit.

The CL will be followed by one more CL that deprecates the usage
of the AttachAecDump API also within the audio_processing
and the fuzzer folders.

The CL also moves the aec_dump.* files from the include folder
to the aec_dump folder and changes the build files. The reasons
for this are that
1) The content of aec_dump.h is not really part of the API
   surface of APM.
2) Those files anyway needed to be moved to a separate build-
   target to avoid a circular build-file dependency caused by
   the other changes in this CL

Bug: webrtc:5298
Change-Id: I7dd6b49de76eb44158472874e1d4ae17dca9be54
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/174750
Commit-Queue: Per Åhgren <peah@webrtc.org>
Reviewed-by: Sam Zackrisson <saza@webrtc.org>
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31207}
diff --git a/media/engine/webrtc_voice_engine.cc b/media/engine/webrtc_voice_engine.cc
index 47bfa7d..85c7280 100644
--- a/media/engine/webrtc_voice_engine.cc
+++ b/media/engine/webrtc_voice_engine.cc
@@ -587,14 +587,8 @@
     return false;
   }
 
-  auto aec_dump = webrtc::AecDumpFactory::Create(
-      std::move(file), max_size_bytes, low_priority_worker_queue_.get());
-  if (!aec_dump) {
-    return false;
-  }
-
-  ap->AttachAecDump(std::move(aec_dump));
-  return true;
+  return ap->CreateAndAttachAecDump(file.Release(), max_size_bytes,
+                                    low_priority_worker_queue_.get());
 }
 
 void WebRtcVoiceEngine::StopAecDump() {
diff --git a/modules/audio_processing/BUILD.gn b/modules/audio_processing/BUILD.gn
index 86ecbff..7ca78e2 100644
--- a/modules/audio_processing/BUILD.gn
+++ b/modules/audio_processing/BUILD.gn
@@ -50,6 +50,7 @@
     "../../rtc_base:macromagic",
     "../../rtc_base:rtc_base_approved",
     "../../rtc_base/system:arch",
+    "../../rtc_base/system:file_wrapper",
     "../../rtc_base/system:rtc_export",
     "agc:gain_control_interface",
     "//third_party/abseil-cpp/absl/types:optional",
@@ -112,6 +113,20 @@
   ]
 }
 
+rtc_source_set("aec_dump_interface") {
+  visibility = [ "*" ]
+  sources = [
+    "include/aec_dump.cc",
+    "include/aec_dump.h",
+  ]
+
+  deps = [
+    ":api",
+    ":audio_frame_view",
+    "../../rtc_base:deprecation",
+  ]
+}
+
 rtc_library("audio_processing") {
   visibility = [ "*" ]
   configs += [ ":apm_debug_dump" ]
@@ -134,8 +149,6 @@
     "gain_control_impl.h",
     "gain_controller2.cc",
     "gain_controller2.h",
-    "include/aec_dump.cc",
-    "include/aec_dump.h",
     "level_estimator.cc",
     "level_estimator.h",
     "render_queue_item_verifier.h",
@@ -147,6 +160,7 @@
 
   defines = []
   deps = [
+    ":aec_dump_interface",
     ":api",
     ":apm_logging",
     ":audio_buffer",
@@ -178,6 +192,7 @@
     "../../system_wrappers:field_trial",
     "../../system_wrappers:metrics",
     "aec3",
+    "aec_dump:aec_dump",
     "aecm:aecm_core",
     "agc",
     "agc:gain_control_interface",
@@ -198,6 +213,12 @@
     "../../rtc_base:rtc_base_approved",
     "../../system_wrappers",
   ]
+
+  if (rtc_enable_protobuf) {
+    deps += [ "aec_dump:aec_dump_impl" ]
+  } else {
+    deps += [ "aec_dump:null_aec_dump_factory" ]
+  }
 }
 
 rtc_library("voice_detection") {
@@ -283,6 +304,7 @@
     testonly = true
     sources = [ "include/mock_audio_processing.h" ]
     deps = [
+      ":aec_dump_interface",
       ":api",
       ":audio_buffer",
       ":audio_processing",
diff --git a/modules/audio_processing/aec_dump/BUILD.gn b/modules/audio_processing/aec_dump/BUILD.gn
index 7ba3bc0..9887f7d 100644
--- a/modules/audio_processing/aec_dump/BUILD.gn
+++ b/modules/audio_processing/aec_dump/BUILD.gn
@@ -13,7 +13,7 @@
   sources = [ "aec_dump_factory.h" ]
 
   deps = [
-    "../",
+    "..:aec_dump_interface",
     "../../../rtc_base:rtc_base_approved",
     "../../../rtc_base/system:file_wrapper",
     "../../../rtc_base/system:rtc_export",
@@ -29,6 +29,7 @@
     ]
 
     deps = [
+      "..:aec_dump_interface",
       "..:audioproc_test_utils",
       "../",
       "../../../test:test_support",
@@ -64,7 +65,7 @@
 
     deps = [
       ":aec_dump",
-      "../",
+      "..:aec_dump_interface",
       "../../../api/audio:audio_frame_api",
       "../../../api/task_queue",
       "../../../rtc_base:checks",
@@ -104,6 +105,6 @@
 
   deps = [
     ":aec_dump",
-    "../",
+    "..:aec_dump_interface",
   ]
 }
diff --git a/modules/audio_processing/audio_processing_impl.cc b/modules/audio_processing/audio_processing_impl.cc
index 7751bac..6abebd2 100644
--- a/modules/audio_processing/audio_processing_impl.cc
+++ b/modules/audio_processing/audio_processing_impl.cc
@@ -22,6 +22,7 @@
 #include "api/audio/audio_frame.h"
 #include "common_audio/audio_converter.h"
 #include "common_audio/include/audio_util.h"
+#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
 #include "modules/audio_processing/agc2/gain_applier.h"
 #include "modules/audio_processing/audio_buffer.h"
 #include "modules/audio_processing/common.h"
@@ -1532,6 +1533,32 @@
   }
 }
 
+bool AudioProcessingImpl::CreateAndAttachAecDump(const std::string& file_name,
+                                                 int64_t max_log_size_bytes,
+                                                 rtc::TaskQueue* worker_queue) {
+  std::unique_ptr<AecDump> aec_dump =
+      AecDumpFactory::Create(file_name, max_log_size_bytes, worker_queue);
+  if (!aec_dump) {
+    return false;
+  }
+
+  AttachAecDump(std::move(aec_dump));
+  return true;
+}
+
+bool AudioProcessingImpl::CreateAndAttachAecDump(FILE* handle,
+                                                 int64_t max_log_size_bytes,
+                                                 rtc::TaskQueue* worker_queue) {
+  std::unique_ptr<AecDump> aec_dump =
+      AecDumpFactory::Create(handle, max_log_size_bytes, worker_queue);
+  if (!aec_dump) {
+    return false;
+  }
+
+  AttachAecDump(std::move(aec_dump));
+  return true;
+}
+
 void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
   RTC_DCHECK(aec_dump);
   rtc::CritScope cs_render(&crit_render_);
diff --git a/modules/audio_processing/audio_processing_impl.h b/modules/audio_processing/audio_processing_impl.h
index 188777e..3aa86ac 100644
--- a/modules/audio_processing/audio_processing_impl.h
+++ b/modules/audio_processing/audio_processing_impl.h
@@ -11,8 +11,11 @@
 #ifndef MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_
 #define MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_
 
+#include <stdio.h>
+
 #include <list>
 #include <memory>
+#include <string>
 #include <vector>
 
 #include "api/function_view.h"
@@ -70,6 +73,13 @@
   int Initialize(const ProcessingConfig& processing_config) override;
   void ApplyConfig(const AudioProcessing::Config& config) override;
   void SetExtraOptions(const webrtc::Config& config) override;
+  bool CreateAndAttachAecDump(const std::string& file_name,
+                              int64_t max_log_size_bytes,
+                              rtc::TaskQueue* worker_queue) override;
+  bool CreateAndAttachAecDump(FILE* handle,
+                              int64_t max_log_size_bytes,
+                              rtc::TaskQueue* worker_queue) override;
+  // TODO(webrtc:5298) Deprecated variant.
   void AttachAecDump(std::unique_ptr<AecDump> aec_dump) override;
   void DetachAecDump() override;
   void SetRuntimeSetting(RuntimeSetting setting) override;
diff --git a/modules/audio_processing/include/audio_processing.h b/modules/audio_processing/include/audio_processing.h
index 25b08c6..d84318f 100644
--- a/modules/audio_processing/include/audio_processing.h
+++ b/modules/audio_processing/include/audio_processing.h
@@ -33,8 +33,13 @@
 #include "rtc_base/arraysize.h"
 #include "rtc_base/deprecation.h"
 #include "rtc_base/ref_count.h"
+#include "rtc_base/system/file_wrapper.h"
 #include "rtc_base/system/rtc_export.h"
 
+namespace rtc {
+class TaskQueue;
+}  // namespace rtc
+
 namespace webrtc {
 
 class AecDump;
@@ -600,6 +605,23 @@
   // with this chunk of audio.
   virtual void set_stream_key_pressed(bool key_pressed) = 0;
 
+  // Creates and attaches an webrtc::AecDump for recording debugging
+  // information.
+  // The |worker_queue| may not be null and must outlive the created
+  // AecDump instance. |max_log_size_bytes == -1| means the log size
+  // will be unlimited. |handle| may not be null. The AecDump takes
+  // responsibility for |handle| and closes it in the destructor. A
+  // return value of true indicates that the file has been
+  // sucessfully opened, while a value of false indicates that
+  // opening the file failed.
+  virtual bool CreateAndAttachAecDump(const std::string& file_name,
+                                      int64_t max_log_size_bytes,
+                                      rtc::TaskQueue* worker_queue) = 0;
+  virtual bool CreateAndAttachAecDump(FILE* handle,
+                                      int64_t max_log_size_bytes,
+                                      rtc::TaskQueue* worker_queue) = 0;
+
+  // TODO(webrtc:5298) Deprecated variant.
   // Attaches provided webrtc::AecDump for recording debugging
   // information. Log file and maximum file size logic is supposed to
   // be handled by implementing instance of AecDump. Calling this
diff --git a/modules/audio_processing/include/mock_audio_processing.h b/modules/audio_processing/include/mock_audio_processing.h
index 9492a38..bdae99a 100644
--- a/modules/audio_processing/include/mock_audio_processing.h
+++ b/modules/audio_processing/include/mock_audio_processing.h
@@ -13,7 +13,6 @@
 
 #include <memory>
 
-#include "modules/audio_processing/audio_buffer.h"
 #include "modules/audio_processing/include/aec_dump.h"
 #include "modules/audio_processing/include/audio_processing.h"
 #include "modules/audio_processing/include/audio_processing_statistics.h"
@@ -128,8 +127,15 @@
   MOCK_CONST_METHOD0(delay_offset_ms, int());
   MOCK_METHOD1(set_stream_analog_level, void(int));
   MOCK_CONST_METHOD0(recommended_stream_analog_level, int());
-
-  virtual void AttachAecDump(std::unique_ptr<AecDump> aec_dump) {}
+  MOCK_METHOD3(CreateAndAttachAecDump,
+               bool(const std::string& file_name,
+                    int64_t max_log_size_bytes,
+                    rtc::TaskQueue* worker_queue));
+  MOCK_METHOD3(CreateAndAttachAecDump,
+               bool(FILE* handle,
+                    int64_t max_log_size_bytes,
+                    rtc::TaskQueue* worker_queue));
+  MOCK_METHOD1(AttachAecDump, void(std::unique_ptr<AecDump>));
   MOCK_METHOD0(DetachAecDump, void());
 
   MOCK_METHOD0(GetStatistics, AudioProcessingStats());
diff --git a/test/pc/e2e/test_peer_factory.cc b/test/pc/e2e/test_peer_factory.cc
index 2b01d8d..0d08f8e 100644
--- a/test/pc/e2e/test_peer_factory.cc
+++ b/test/pc/e2e/test_peer_factory.cc
@@ -292,8 +292,8 @@
   rtc::scoped_refptr<AudioProcessing> audio_processing =
       webrtc::AudioProcessingBuilder().Create();
   if (params->aec_dump_path && audio_processing) {
-    audio_processing->AttachAecDump(
-        AecDumpFactory::Create(*params->aec_dump_path, -1, task_queue));
+    audio_processing->CreateAndAttachAecDump(*params->aec_dump_path, -1,
+                                             task_queue);
   }
   rtc::scoped_refptr<AudioDeviceModule> audio_device_module =
       CreateAudioDeviceModule(