Remove the ACMResampler class, use PushResampler directly instead.

Bug: chromium:335805780
Change-Id: Ieaf127046fe79a53c2bbde71d6e08f3ce07cbf09
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/391280
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Reviewed-by: Jakob Ivarsson‎ <jakobi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#44698}
diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn
index d279433..dd4500f 100644
--- a/modules/audio_coding/BUILD.gn
+++ b/modules/audio_coding/BUILD.gn
@@ -760,6 +760,7 @@
     "../../api/neteq:default_neteq_factory",
     "../../api/neteq:neteq_api",
     "../../api/units:timestamp",
+    "../../common_audio",
     "../../rtc_base:checks",
     "../../rtc_base:digest",
     "../../rtc_base:stringutils",
diff --git a/modules/audio_coding/acm2/acm_resampler.cc b/modules/audio_coding/acm2/acm_resampler.cc
index 1587ecc..9daa04d 100644
--- a/modules/audio_coding/acm2/acm_resampler.cc
+++ b/modules/audio_coding/acm2/acm_resampler.cc
@@ -10,51 +10,16 @@
 
 #include "modules/audio_coding/acm2/acm_resampler.h"
 
-#include <string.h>
-
+#include <array>
 #include <cstdint>
 
 #include "api/audio/audio_frame.h"
 #include "api/audio/audio_view.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/logging.h"
-#include "rtc_base/numerics/safe_conversions.h"
 
 namespace webrtc {
 namespace acm2 {
 
-ACMResampler::ACMResampler() {}
-
-ACMResampler::~ACMResampler() {}
-
-int ACMResampler::Resample10Msec(const int16_t* in_audio,
-                                 int in_freq_hz,
-                                 int out_freq_hz,
-                                 size_t num_audio_channels,
-                                 size_t out_capacity_samples,
-                                 int16_t* out_audio) {
-  InterleavedView<const int16_t> src(
-      in_audio, SampleRateToDefaultChannelSize(in_freq_hz), num_audio_channels);
-  InterleavedView<int16_t> dst(out_audio,
-                               SampleRateToDefaultChannelSize(out_freq_hz),
-                               num_audio_channels);
-  RTC_DCHECK_GE(out_capacity_samples, dst.size());
-  Resample10Msec(src, in_freq_hz, dst, out_freq_hz);
-  return dst.samples_per_channel();
-}
-
-void ACMResampler::Resample10Msec(InterleavedView<const int16_t> src,
-                                  int in_freq_hz,
-                                  InterleavedView<int16_t> dst,
-                                  int out_freq_hz) {
-  RTC_DCHECK_EQ(src.num_channels(), dst.num_channels());
-  if (in_freq_hz == out_freq_hz) {
-    CopySamples(dst, src);
-  } else {
-    resampler_.Resample(src, dst);
-  }
-}
-
 ResamplerHelper::ResamplerHelper() {
   ClearSamples(last_audio_buffer_);
 }
@@ -63,44 +28,38 @@
                                     AudioFrame* audio_frame) {
   const int current_sample_rate_hz = audio_frame->sample_rate_hz_;
   RTC_DCHECK_NE(current_sample_rate_hz, 0);
+  RTC_DCHECK_GT(desired_sample_rate_hz, 0);
 
   // Update if resampling is required.
+  // TODO(tommi): `desired_sample_rate_hz` should never be -1.
+  // Remove the first check.
   const bool need_resampling =
       (desired_sample_rate_hz != -1) &&
       (current_sample_rate_hz != desired_sample_rate_hz);
 
   if (need_resampling && !resampled_last_output_frame_) {
     // Prime the resampler with the last frame.
-    int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
-    int samples_per_channel_int = resampler_.Resample10Msec(
-        last_audio_buffer_.data(), current_sample_rate_hz,
-        desired_sample_rate_hz, audio_frame->num_channels_,
-        AudioFrame::kMaxDataSizeSamples, temp_output);
-    if (samples_per_channel_int < 0) {
-      RTC_LOG(LS_ERROR) << "AcmReceiver::GetAudio - "
-                           "Resampling last_audio_buffer_ failed.";
-      return false;
-    }
+    InterleavedView<const int16_t> src(last_audio_buffer_.data(),
+                                       audio_frame->samples_per_channel(),
+                                       audio_frame->num_channels());
+    std::array<int16_t, AudioFrame::kMaxDataSizeSamples> temp_output;
+    InterleavedView<int16_t> dst(
+        temp_output.data(),
+        SampleRateToDefaultChannelSize(desired_sample_rate_hz),
+        audio_frame->num_channels_);
+    resampler_.Resample(src, dst);
   }
 
   // TODO(bugs.webrtc.org/3923) Glitches in the output may appear if the output
   // rate from NetEq changes.
   if (need_resampling) {
-    // TODO(yujo): handle this more efficiently for muted frames.
-    int samples_per_channel_int = resampler_.Resample10Msec(
-        audio_frame->data(), current_sample_rate_hz, desired_sample_rate_hz,
-        audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
-        audio_frame->mutable_data());
-    if (samples_per_channel_int < 0) {
-      RTC_LOG(LS_ERROR)
-          << "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
-      return false;
-    }
-    audio_frame->samples_per_channel_ =
-        static_cast<size_t>(samples_per_channel_int);
-    audio_frame->sample_rate_hz_ = desired_sample_rate_hz;
-    RTC_DCHECK_EQ(audio_frame->sample_rate_hz_,
-                  dchecked_cast<int>(audio_frame->samples_per_channel_ * 100));
+    // Grab the source view of the current layout before changing properties.
+    InterleavedView<const int16_t> src = audio_frame->data_view();
+    audio_frame->SetSampleRateAndChannelSize(desired_sample_rate_hz);
+    InterleavedView<int16_t> dst = audio_frame->mutable_data(
+        audio_frame->samples_per_channel(), audio_frame->num_channels());
+    // TODO(tommi): Don't resample muted audio frames.
+    resampler_.Resample(src, dst);
     resampled_last_output_frame_ = true;
   } else {
     resampled_last_output_frame_ = false;
@@ -108,10 +67,10 @@
   }
 
   // Store current audio in `last_audio_buffer_` for next time.
-  // TODO: b/335805780 - Use CopySamples().
-  memcpy(last_audio_buffer_.data(), audio_frame->data(),
-         sizeof(int16_t) * audio_frame->samples_per_channel_ *
-             audio_frame->num_channels_);
+  InterleavedView<int16_t> dst(last_audio_buffer_.data(),
+                               audio_frame->samples_per_channel(),
+                               audio_frame->num_channels());
+  CopySamples(dst, audio_frame->data_view());
 
   return true;
 }
diff --git a/modules/audio_coding/acm2/acm_resampler.h b/modules/audio_coding/acm2/acm_resampler.h
index 256884b..8b6cee0 100644
--- a/modules/audio_coding/acm2/acm_resampler.h
+++ b/modules/audio_coding/acm2/acm_resampler.h
@@ -17,34 +17,11 @@
 #include <array>
 
 #include "api/audio/audio_frame.h"
-#include "api/audio/audio_view.h"
 #include "common_audio/resampler/include/push_resampler.h"
 
 namespace webrtc {
 namespace acm2 {
 
-class ACMResampler {
- public:
-  ACMResampler();
-  ~ACMResampler();
-
-  // TODO: b/335805780 - Remove and use only the InterleavedView<> version
-  int Resample10Msec(const int16_t* in_audio,
-                     int in_freq_hz,
-                     int out_freq_hz,
-                     size_t num_audio_channels,
-                     size_t out_capacity_samples,
-                     int16_t* out_audio);
-
-  void Resample10Msec(InterleavedView<const int16_t> src,
-                      int src_freq_hz,
-                      InterleavedView<int16_t> dst,
-                      int dst_freq_hz);
-
- private:
-  PushResampler<int16_t> resampler_;
-};
-
 // Helper class to perform resampling if needed, meant to be used after
 // receiving the audio_frame from NetEq. Provides reasonably glitch free
 // transitions between different output sample rates from NetEq.
@@ -56,7 +33,7 @@
   bool MaybeResample(int desired_sample_rate_hz, AudioFrame* audio_frame);
 
  private:
-  ACMResampler resampler_;
+  PushResampler<int16_t> resampler_;
   bool resampled_last_output_frame_ = true;
   std::array<int16_t, AudioFrame::kMaxDataSizeSamples> last_audio_buffer_;
 };
diff --git a/modules/audio_coding/acm2/audio_coding_module.cc b/modules/audio_coding/acm2/audio_coding_module.cc
index 32a36a7..41d022e 100644
--- a/modules/audio_coding/acm2/audio_coding_module.cc
+++ b/modules/audio_coding/acm2/audio_coding_module.cc
@@ -23,8 +23,8 @@
 #include "api/audio/audio_view.h"
 #include "api/audio_codecs/audio_encoder.h"
 #include "api/function_view.h"
+#include "common_audio/resampler/include/push_resampler.h"
 #include "modules/audio_coding/acm2/acm_remixing.h"
-#include "modules/audio_coding/acm2/acm_resampler.h"
 #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
 #include "modules/include/module_common_types_public.h"
 #include "rtc_base/buffer.h"
@@ -161,7 +161,7 @@
   Buffer encode_buffer_ RTC_GUARDED_BY(acm_mutex_);
   uint32_t expected_codec_ts_ RTC_GUARDED_BY(acm_mutex_);
   uint32_t expected_in_ts_ RTC_GUARDED_BY(acm_mutex_);
-  acm2::ACMResampler resampler_ RTC_GUARDED_BY(acm_mutex_);
+  PushResampler<int16_t> resampler_ RTC_GUARDED_BY(acm_mutex_);
   ChangeLogger bitrate_logger_ RTC_GUARDED_BY(acm_mutex_);
 
   // Current encoder stack, provided by a call to RegisterEncoder.
@@ -506,11 +506,10 @@
   preprocess_frame_.SetSampleRateAndChannelSize(encoder_stack_->SampleRateHz());
 
   if (resample) {
-    resampler_.Resample10Msec(
-        resample_src_audio, in_frame.sample_rate_hz(),
+    resampler_.Resample(
+        resample_src_audio,
         preprocess_frame_.mutable_data(preprocess_frame_.samples_per_channel(),
-                                       preprocess_frame_.num_channels()),
-        preprocess_frame_.sample_rate_hz());
+                                       preprocess_frame_.num_channels()));
   }
 
   expected_codec_ts_ +=
diff --git a/modules/audio_coding/test/opus_test.cc b/modules/audio_coding/test/opus_test.cc
index da44f26..6b511c2 100644
--- a/modules/audio_coding/test/opus_test.cc
+++ b/modules/audio_coding/test/opus_test.cc
@@ -267,9 +267,8 @@
 
     // If input audio is sampled at 32 kHz, resampling to 48 kHz is required.
     InterleavedView<int16_t> dst(&audio[written_samples], 480, channels);
-    resampler_.Resample10Msec(audio_frame.data_view(),
-                              audio_frame.sample_rate_hz_, dst, 48000);
-    written_samples += 480 * channels;
+    resampler_.Resample(audio_frame.data_view(), dst);
+    written_samples += dst.size();
 
     // Sometimes we need to loop over the audio vector to produce the right
     // number of packets.
diff --git a/modules/audio_coding/test/opus_test.h b/modules/audio_coding/test/opus_test.h
index d6f0e7c..4d2586e 100644
--- a/modules/audio_coding/test/opus_test.h
+++ b/modules/audio_coding/test/opus_test.h
@@ -18,6 +18,7 @@
 #include <memory>
 
 #include "api/neteq/neteq.h"
+#include "common_audio/resampler/include/push_resampler.h"
 #include "modules/audio_coding/acm2/acm_resampler.h"
 #include "modules/audio_coding/codecs/opus/opus_inst.h"
 #include "modules/audio_coding/test/PCMFile.h"
@@ -51,7 +52,7 @@
   int counter_;
   uint8_t payload_type_;
   uint32_t rtp_timestamp_;
-  acm2::ACMResampler resampler_;
+  PushResampler<int16_t> resampler_;
   WebRtcOpusEncInst* opus_mono_encoder_;
   WebRtcOpusEncInst* opus_stereo_encoder_;
   WebRtcOpusDecInst* opus_mono_decoder_;