Introduced the new APM data logging functionality into the AEC echo_cancellation.* API layer.

BUG=webrtc:5298

Review-Url: https://codereview.webrtc.org/1952593002
Cr-Commit-Position: refs/heads/master@{#12635}
diff --git a/webrtc/modules/audio_processing/aec/echo_cancellation.cc b/webrtc/modules/audio_processing/aec/echo_cancellation.cc
index 6b1758a1..e1dba6e 100644
--- a/webrtc/modules/audio_processing/aec/echo_cancellation.cc
+++ b/webrtc/modules/audio_processing/aec/echo_cancellation.cc
@@ -14,9 +14,6 @@
 #include "webrtc/modules/audio_processing/aec/echo_cancellation.h"
 
 #include <math.h>
-#if WEBRTC_AEC_DEBUG_DUMP == 1
-#include <stdio.h>
-#endif
 #include <stdlib.h>
 #include <string.h>
 
@@ -27,14 +24,9 @@
 #include "webrtc/modules/audio_processing/aec/aec_core.h"
 #include "webrtc/modules/audio_processing/aec/aec_resampler.h"
 #include "webrtc/modules/audio_processing/aec/echo_cancellation_internal.h"
+#include "webrtc/modules/audio_processing/logging/apm_data_dumper.h"
 #include "webrtc/typedefs.h"
 
-// Check to verify that the define is properly set.
-#if !defined(WEBRTC_AEC_DEBUG_DUMP) || \
-    (WEBRTC_AEC_DEBUG_DUMP != 0 && WEBRTC_AEC_DEBUG_DUMP != 1)
-#error "Set WEBRTC_AEC_DEBUG_DUMP to either 0 or 1"
-#endif
-
 namespace webrtc {
 
 // Measured delays [ms]
@@ -127,11 +119,12 @@
                             int32_t skew);
 
 void* WebRtcAec_Create() {
-  Aec* aecpc = reinterpret_cast<Aec*>(malloc(sizeof(Aec)));
+  Aec* aecpc = new Aec();
 
   if (!aecpc) {
     return NULL;
   }
+  aecpc->data_dumper.reset(new ApmDataDumper(aecpc->instance_count));
 
   aecpc->aec = WebRtcAec_CreateAec(aecpc->instance_count);
   if (!aecpc->aec) {
@@ -155,18 +148,7 @@
 
   aecpc->initFlag = 0;
 
-#if WEBRTC_AEC_DEBUG_DUMP == 1
-  char filename[64];
-  snprintf(filename, sizeof(filename), "aec_buf%d.dat", aecpc->instance_count);
-  aecpc->bufFile = fopen(filename, "wb");
-  snprintf(filename, sizeof(filename), "aec_skew%d.dat", aecpc->instance_count);
-  aecpc->skewFile = fopen(filename, "wb");
-  snprintf(filename, sizeof(filename), "aec_delay%d.dat",
-           aecpc->instance_count);
-  aecpc->delayFile = fopen(filename, "wb");
-#endif
   aecpc->instance_count++;
-
   return aecpc;
 }
 
@@ -179,19 +161,14 @@
 
   WebRtc_FreeBuffer(aecpc->far_pre_buf);
 
-#if WEBRTC_AEC_DEBUG_DUMP == 1
-  fclose(aecpc->bufFile);
-  fclose(aecpc->skewFile);
-  fclose(aecpc->delayFile);
-#endif
-
   WebRtcAec_FreeAec(aecpc->aec);
   WebRtcAec_FreeResampler(aecpc->resampler);
-  free(aecpc);
+  delete aecpc;
 }
 
 int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq) {
   Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
+  aecpc->data_dumper->InitiateNewSetOfRecordings();
   AecConfig aecConfig;
 
   if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000 &&
@@ -376,15 +353,9 @@
                            msInSndCardBuf, skew);
   }
 
-#if WEBRTC_AEC_DEBUG_DUMP == 1
-  {
-    int16_t far_buf_size_ms = (int16_t)(WebRtcAec_system_delay(aecpc->aec) /
-                                        (sampMsNb * aecpc->rate_factor));
-    (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
-    (void)fwrite(&aecpc->knownDelay, sizeof(aecpc->knownDelay), 1,
-                 aecpc->delayFile);
-  }
-#endif
+  int far_buf_size_samples = WebRtcAec_system_delay(aecpc->aec);
+  aecpc->data_dumper->DumpRaw("aec_system_delay", 1, &far_buf_size_samples);
+  aecpc->data_dumper->DumpRaw("aec_known_delay", 1, &aecpc->knownDelay);
 
   return retVal;
 }
@@ -603,9 +574,7 @@
         aecpc->skew = maxSkewEst;
       }
 
-#if WEBRTC_AEC_DEBUG_DUMP == 1
-      (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
-#endif
+      aecpc->data_dumper->DumpRaw("aec_skew", 1, &aecpc->skew);
     }
   }
 
diff --git a/webrtc/modules/audio_processing/aec/echo_cancellation_internal.h b/webrtc/modules/audio_processing/aec/echo_cancellation_internal.h
index 188fb57..5e79626 100644
--- a/webrtc/modules/audio_processing/aec/echo_cancellation_internal.h
+++ b/webrtc/modules/audio_processing/aec/echo_cancellation_internal.h
@@ -11,6 +11,8 @@
 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC_ECHO_CANCELLATION_INTERNAL_H_
 #define WEBRTC_MODULES_AUDIO_PROCESSING_AEC_ECHO_CANCELLATION_INTERNAL_H_
 
+#include <memory>
+
 extern "C" {
 #include "webrtc/common_audio/ring_buffer.h"
 }
@@ -18,7 +20,11 @@
 
 namespace webrtc {
 
+class ApmDataDumper;
+
 typedef struct Aec {
+  std::unique_ptr<ApmDataDumper> data_dumper;
+
   int delayCtr;
   int sampFreq;
   int splitSampFreq;
@@ -45,12 +51,6 @@
   int checkBuffSize;
   short lastDelayDiff;
 
-#if WEBRTC_AEC_DEBUG_DUMP
-  FILE* bufFile;
-  FILE* delayFile;
-  FILE* skewFile;
-#endif
-
   // Structures
   void* resampler;
 
diff --git a/webrtc/modules/audio_processing/logging/apm_data_dumper.h b/webrtc/modules/audio_processing/logging/apm_data_dumper.h
index 18f9e5e..93232b7 100644
--- a/webrtc/modules/audio_processing/logging/apm_data_dumper.h
+++ b/webrtc/modules/audio_processing/logging/apm_data_dumper.h
@@ -73,6 +73,32 @@
 #endif
   }
 
+  void DumpRaw(const char* name, int v_length, const int16_t* v) {
+#if WEBRTC_AEC_DEBUG_DUMP == 1
+    FILE* file = GetRawFile(name);
+    fwrite(v, sizeof(v[0]), v_length, file);
+#endif
+  }
+
+  void DumpRaw(const char* name, rtc::ArrayView<const int16_t> v) {
+#if WEBRTC_AEC_DEBUG_DUMP == 1
+    DumpRaw(name, v.size(), v.data());
+#endif
+  }
+
+  void DumpRaw(const char* name, int v_length, const int32_t* v) {
+#if WEBRTC_AEC_DEBUG_DUMP == 1
+    FILE* file = GetRawFile(name);
+    fwrite(v, sizeof(v[0]), v_length, file);
+#endif
+  }
+
+  void DumpRaw(const char* name, rtc::ArrayView<const int32_t> v) {
+#if WEBRTC_AEC_DEBUG_DUMP == 1
+    DumpRaw(name, v.size(), v.data());
+#endif
+  }
+
   void DumpWav(const char* name,
                int v_length,
                const float* v,