Remove testonly from unpack_aecdump.

This CL duplicates a few lines of utility code from
//modules/audio_processing:audioproc_test_utils (which contains more
testonly things) and allows the possibility to remove testonly from
the unpack_aecdump tool.

Bug: b/237526033
Change-Id: If2e1dd4cc825429c496091cf8640c67069fb6e6f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/267701
Reviewed-by: Per Ã…hgren <peah@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37437}
diff --git a/modules/audio_processing/test/test_utils.cc b/modules/audio_processing/test/test_utils.cc
index dcd5869..c041a68 100644
--- a/modules/audio_processing/test/test_utils.cc
+++ b/modules/audio_processing/test/test_utils.cc
@@ -17,24 +17,6 @@
 
 namespace webrtc {
 
-RawFile::RawFile(const std::string& filename)
-    : file_handle_(fopen(filename.c_str(), "wb")) {}
-
-RawFile::~RawFile() {
-  fclose(file_handle_);
-}
-
-void RawFile::WriteSamples(const int16_t* samples, size_t num_samples) {
-#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
-#error "Need to convert samples to little-endian when writing to PCM file"
-#endif
-  fwrite(samples, sizeof(*samples), num_samples, file_handle_);
-}
-
-void RawFile::WriteSamples(const float* samples, size_t num_samples) {
-  fwrite(samples, sizeof(*samples), num_samples, file_handle_);
-}
-
 ChannelBufferWavReader::ChannelBufferWavReader(std::unique_ptr<WavReader> file)
     : file_(std::move(file)) {}
 
@@ -86,40 +68,6 @@
                   output_->data() + old_size);
 }
 
-void WriteIntData(const int16_t* data,
-                  size_t length,
-                  WavWriter* wav_file,
-                  RawFile* raw_file) {
-  if (wav_file) {
-    wav_file->WriteSamples(data, length);
-  }
-  if (raw_file) {
-    raw_file->WriteSamples(data, length);
-  }
-}
-
-void WriteFloatData(const float* const* data,
-                    size_t samples_per_channel,
-                    size_t num_channels,
-                    WavWriter* wav_file,
-                    RawFile* raw_file) {
-  size_t length = num_channels * samples_per_channel;
-  std::unique_ptr<float[]> buffer(new float[length]);
-  Interleave(data, samples_per_channel, num_channels, buffer.get());
-  if (raw_file) {
-    raw_file->WriteSamples(buffer.get(), length);
-  }
-  // TODO(aluebs): Use ScaleToInt16Range() from audio_util
-  for (size_t i = 0; i < length; ++i) {
-    buffer[i] = buffer[i] > 0
-                    ? buffer[i] * std::numeric_limits<int16_t>::max()
-                    : -buffer[i] * std::numeric_limits<int16_t>::min();
-  }
-  if (wav_file) {
-    wav_file->WriteSamples(buffer.get(), length);
-  }
-}
-
 FILE* OpenFile(const std::string& filename, const char* mode) {
   FILE* file = fopen(filename.c_str(), mode);
   if (!file) {
diff --git a/modules/audio_processing/test/test_utils.h b/modules/audio_processing/test/test_utils.h
index de0fc11..218052f 100644
--- a/modules/audio_processing/test/test_utils.h
+++ b/modules/audio_processing/test/test_utils.h
@@ -29,21 +29,6 @@
 static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
 #define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
 
-class RawFile final {
- public:
-  explicit RawFile(const std::string& filename);
-  ~RawFile();
-
-  RawFile(const RawFile&) = delete;
-  RawFile& operator=(const RawFile&) = delete;
-
-  void WriteSamples(const int16_t* samples, size_t num_samples);
-  void WriteSamples(const float* samples, size_t num_samples);
-
- private:
-  FILE* file_handle_;
-};
-
 // Encapsulates samples and metadata for an integer frame.
 struct Int16FrameData {
   // Max data size that matches the data size of the AudioFrame class, providing
@@ -126,17 +111,6 @@
   std::vector<float>* output_;
 };
 
-void WriteIntData(const int16_t* data,
-                  size_t length,
-                  WavWriter* wav_file,
-                  RawFile* raw_file);
-
-void WriteFloatData(const float* const* data,
-                    size_t samples_per_channel,
-                    size_t num_channels,
-                    WavWriter* wav_file,
-                    RawFile* raw_file);
-
 // Exits on failure; do not use in unit tests.
 FILE* OpenFile(const std::string& filename, const char* mode);
 
diff --git a/rtc_tools/BUILD.gn b/rtc_tools/BUILD.gn
index c7ce8d1..fa22c81 100644
--- a/rtc_tools/BUILD.gn
+++ b/rtc_tools/BUILD.gn
@@ -574,7 +574,6 @@
 
       rtc_executable("unpack_aecdump") {
         visibility = [ "*" ]
-        testonly = true
         sources = [ "unpack_aecdump/unpack.cc" ]
 
         deps = [
@@ -584,11 +583,12 @@
           "../modules/audio_processing:audioproc_debug_proto",
           "../modules/audio_processing:audioproc_debug_proto",
           "../modules/audio_processing:audioproc_protobuf_utils",
-          "../modules/audio_processing:audioproc_test_utils",
+          "../rtc_base:checks",
           "../rtc_base:ignore_wundef",
           "../rtc_base:macromagic",
           "../rtc_base:protobuf_utils",
           "../rtc_base:stringutils",
+          "../rtc_base/system:arch",
           "//third_party/abseil-cpp/absl/flags:flag",
           "//third_party/abseil-cpp/absl/flags:parse",
         ]
diff --git a/rtc_tools/unpack_aecdump/unpack.cc b/rtc_tools/unpack_aecdump/unpack.cc
index 49b62d2..642aa5d 100644
--- a/rtc_tools/unpack_aecdump/unpack.cc
+++ b/rtc_tools/unpack_aecdump/unpack.cc
@@ -25,11 +25,13 @@
 #include "absl/flags/flag.h"
 #include "absl/flags/parse.h"
 #include "api/function_view.h"
+#include "common_audio/include/audio_util.h"
 #include "common_audio/wav_file.h"
 #include "modules/audio_processing/test/protobuf_utils.h"
-#include "modules/audio_processing/test/test_utils.h"
+#include "rtc_base/checks.h"
 #include "rtc_base/ignore_wundef.h"
 #include "rtc_base/strings/string_builder.h"
+#include "rtc_base/system/arch.h"
 
 RTC_PUSH_IGNORING_WUNDEF()
 #include "modules/audio_processing/debug.pb.h"
@@ -103,15 +105,77 @@
 using audioproc::Stream;
 
 namespace {
+class RawFile final {
+ public:
+  explicit RawFile(const std::string& filename)
+      : file_handle_(fopen(filename.c_str(), "wb")) {}
+  ~RawFile() { fclose(file_handle_); }
+
+  RawFile(const RawFile&) = delete;
+  RawFile& operator=(const RawFile&) = delete;
+
+  void WriteSamples(const int16_t* samples, size_t num_samples) {
+#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
+#error "Need to convert samples to little-endian when writing to PCM file"
+#endif
+    fwrite(samples, sizeof(*samples), num_samples, file_handle_);
+  }
+
+  void WriteSamples(const float* samples, size_t num_samples) {
+    fwrite(samples, sizeof(*samples), num_samples, file_handle_);
+  }
+
+ private:
+  FILE* file_handle_;
+};
+
+void WriteIntData(const int16_t* data,
+                  size_t length,
+                  WavWriter* wav_file,
+                  RawFile* raw_file) {
+  if (wav_file) {
+    wav_file->WriteSamples(data, length);
+  }
+  if (raw_file) {
+    raw_file->WriteSamples(data, length);
+  }
+}
+
+void WriteFloatData(const float* const* data,
+                    size_t samples_per_channel,
+                    size_t num_channels,
+                    WavWriter* wav_file,
+                    RawFile* raw_file) {
+  size_t length = num_channels * samples_per_channel;
+  std::unique_ptr<float[]> buffer(new float[length]);
+  Interleave(data, samples_per_channel, num_channels, buffer.get());
+  if (raw_file) {
+    raw_file->WriteSamples(buffer.get(), length);
+  }
+  // TODO(aluebs): Use ScaleToInt16Range() from audio_util
+  for (size_t i = 0; i < length; ++i) {
+    buffer[i] = buffer[i] > 0
+                    ? buffer[i] * std::numeric_limits<int16_t>::max()
+                    : -buffer[i] * std::numeric_limits<int16_t>::min();
+  }
+  if (wav_file) {
+    wav_file->WriteSamples(buffer.get(), length);
+  }
+}
+
+// Exits on failure; do not use in unit tests.
+FILE* OpenFile(const std::string& filename, const char* mode) {
+  FILE* file = fopen(filename.c_str(), mode);
+  RTC_CHECK(file) << "Unable to open file " << filename;
+  return file;
+}
 
 void WriteData(const void* data,
                size_t size,
                FILE* file,
                const std::string& filename) {
-  if (fwrite(data, size, 1, file) != 1) {
-    printf("Error when writing to %s\n", filename.c_str());
-    exit(1);
-  }
+  RTC_CHECK_EQ(fwrite(data, size, 1, file), 1)
+      << "Error when writing to " << filename.c_str();
 }
 
 void WriteCallOrderData(const bool render_call,