Replace RegisterExternalDecoder with decoder factory in NetEqImplTest120ms

Change-Id: I86b5f748f556be186f020a97fcc1211f953fd219

Bug: webrtc:10080
Change-Id: I86b5f748f556be186f020a97fcc1211f953fd219
Reviewed-on: https://webrtc-review.googlesource.com/c/112600
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25869}
diff --git a/modules/audio_coding/neteq/neteq_impl_unittest.cc b/modules/audio_coding/neteq/neteq_impl_unittest.cc
index 0e087c8..784c63c 100644
--- a/modules/audio_coding/neteq/neteq_impl_unittest.cc
+++ b/modules/audio_coding/neteq/neteq_impl_unittest.cc
@@ -10,6 +10,7 @@
 
 #include <memory>
 
+#include "absl/memory/memory.h"
 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "modules/audio_coding/neteq/accelerate.h"
@@ -28,6 +29,7 @@
 #include "modules/audio_coding/neteq/sync_buffer.h"
 #include "modules/audio_coding/neteq/timestamp_scaler.h"
 #include "rtc_base/numerics/safe_conversions.h"
+#include "test/function_audio_decoder_factory.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/mock_audio_decoder.h"
@@ -59,8 +61,10 @@
  protected:
   NetEqImplTest() { config_.sample_rate_hz = 8000; }
 
-  void CreateInstance() {
-    NetEqImpl::Dependencies deps(config_, CreateBuiltinAudioDecoderFactory());
+  void CreateInstance(
+      const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
+    ASSERT_TRUE(decoder_factory);
+    NetEqImpl::Dependencies deps(config_, decoder_factory);
 
     // Get a local pointer to NetEq's TickTimer object.
     tick_timer_ = deps.tick_timer.get();
@@ -137,6 +141,8 @@
     ASSERT_TRUE(neteq_ != NULL);
   }
 
+  void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
+
   void UseNoMocks() {
     ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
     use_mock_buffer_level_filter_ = false;
@@ -1328,13 +1334,17 @@
 
   void CreateInstanceNoMocks() {
     UseNoMocks();
-    CreateInstance();
+    CreateInstance(decoder_factory_);
+    EXPECT_TRUE(neteq_->RegisterPayloadType(
+        kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
   }
 
   void CreateInstanceWithDelayManagerMock() {
     UseNoMocks();
     use_mock_delay_manager_ = true;
-    CreateInstance();
+    CreateInstance(decoder_factory_);
+    EXPECT_TRUE(neteq_->RegisterPayloadType(
+        kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
   }
 
   uint32_t timestamp_diff_between_packets() const {
@@ -1364,14 +1374,18 @@
   }
 
   void Register120msCodec(AudioDecoder::SpeechType speech_type) {
-    decoder_.reset(new Decoder120ms(kSamplingFreq_, speech_type));
-    ASSERT_EQ(2u, decoder_->Channels());
-    EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
-                              decoder_.get(), NetEqDecoder::kDecoderOpus_2ch,
-                              "120ms codec", kPayloadType));
+    const uint32_t sampling_freq = kSamplingFreq_;
+    decoder_factory_ =
+        new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
+            [sampling_freq, speech_type]() {
+              std::unique_ptr<AudioDecoder> decoder =
+                  absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
+              RTC_CHECK_EQ(2, decoder->Channels());
+              return decoder;
+            });
   }
 
-  std::unique_ptr<Decoder120ms> decoder_;
+  rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
   AudioFrame output_;
   const uint32_t kPayloadType = 17;
   const uint32_t kSamplingFreq_ = 48000;
@@ -1379,8 +1393,8 @@
 };
 
 TEST_F(NetEqImplTest120ms, CodecInternalCng) {
-  CreateInstanceNoMocks();
   Register120msCodec(AudioDecoder::kComfortNoise);
+  CreateInstanceNoMocks();
 
   InsertPacket(first_timestamp());
   GetFirstPacket();
@@ -1391,8 +1405,8 @@
 }
 
 TEST_F(NetEqImplTest120ms, Normal) {
-  CreateInstanceNoMocks();
   Register120msCodec(AudioDecoder::kSpeech);
+  CreateInstanceNoMocks();
 
   InsertPacket(first_timestamp());
   GetFirstPacket();
@@ -1401,9 +1415,9 @@
 }
 
 TEST_F(NetEqImplTest120ms, Merge) {
+  Register120msCodec(AudioDecoder::kSpeech);
   CreateInstanceWithDelayManagerMock();
 
-  Register120msCodec(AudioDecoder::kSpeech);
   InsertPacket(first_timestamp());
 
   GetFirstPacket();
@@ -1420,8 +1434,8 @@
 }
 
 TEST_F(NetEqImplTest120ms, Expand) {
-  CreateInstanceNoMocks();
   Register120msCodec(AudioDecoder::kSpeech);
+  CreateInstanceNoMocks();
 
   InsertPacket(first_timestamp());
   GetFirstPacket();
@@ -1432,8 +1446,8 @@
 }
 
 TEST_F(NetEqImplTest120ms, FastAccelerate) {
-  CreateInstanceWithDelayManagerMock();
   Register120msCodec(AudioDecoder::kSpeech);
+  CreateInstanceWithDelayManagerMock();
 
   InsertPacket(first_timestamp());
   GetFirstPacket();
@@ -1450,8 +1464,8 @@
 }
 
 TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
-  CreateInstanceWithDelayManagerMock();
   Register120msCodec(AudioDecoder::kSpeech);
+  CreateInstanceWithDelayManagerMock();
 
   InsertPacket(first_timestamp());
   GetFirstPacket();
@@ -1469,8 +1483,8 @@
 }
 
 TEST_F(NetEqImplTest120ms, Accelerate) {
-  CreateInstanceWithDelayManagerMock();
   Register120msCodec(AudioDecoder::kSpeech);
+  CreateInstanceWithDelayManagerMock();
 
   InsertPacket(first_timestamp());
   GetFirstPacket();
diff --git a/test/BUILD.gn b/test/BUILD.gn
index 70e53f5..1557af2 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -830,6 +830,7 @@
 rtc_source_set("audio_codec_mocks") {
   testonly = true
   sources = [
+    "function_audio_decoder_factory.h",
     "mock_audio_decoder.cc",
     "mock_audio_decoder.h",
     "mock_audio_decoder_factory.h",
@@ -843,7 +844,9 @@
     "../api:array_view",
     "../api/audio_codecs:audio_codecs_api",
     "../api/audio_codecs:builtin_audio_decoder_factory",
+    "../rtc_base:checks",
     "../rtc_base:rtc_base_approved",
+    "//third_party/abseil-cpp/absl/memory",
   ]
 }
 
diff --git a/test/function_audio_decoder_factory.h b/test/function_audio_decoder_factory.h
new file mode 100644
index 0000000..a5ee593
--- /dev/null
+++ b/test/function_audio_decoder_factory.h
@@ -0,0 +1,68 @@
+/*
+ *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef TEST_FUNCTION_AUDIO_DECODER_FACTORY_H_
+#define TEST_FUNCTION_AUDIO_DECODER_FACTORY_H_
+
+#include <functional>
+#include <memory>
+#include <utility>
+#include <vector>
+
+#include "absl/memory/memory.h"
+#include "api/audio_codecs/audio_decoder_factory.h"
+#include "api/audio_codecs/audio_format.h"
+#include "rtc_base/checks.h"
+
+namespace webrtc {
+namespace test {
+
+// A decoder factory producing decoders by calling a supplied create function.
+class FunctionAudioDecoderFactory : public AudioDecoderFactory {
+ public:
+  explicit FunctionAudioDecoderFactory(
+      std::function<std::unique_ptr<AudioDecoder>()> create)
+      : create_([create](const SdpAudioFormat&,
+                         absl::optional<AudioCodecPairId> codec_pair_id) {
+          return create();
+        }) {}
+  explicit FunctionAudioDecoderFactory(
+      std::function<std::unique_ptr<AudioDecoder>(
+          const SdpAudioFormat&,
+          absl::optional<AudioCodecPairId> codec_pair_id)> create)
+      : create_(std::move(create)) {}
+
+  // Unused by tests.
+  std::vector<AudioCodecSpec> GetSupportedDecoders() override {
+    RTC_NOTREACHED();
+    return {};
+  }
+
+  bool IsSupportedDecoder(const SdpAudioFormat& format) override {
+    return true;
+  }
+
+  std::unique_ptr<AudioDecoder> MakeAudioDecoder(
+      const SdpAudioFormat& format,
+      absl::optional<AudioCodecPairId> codec_pair_id) override {
+    return create_(format, codec_pair_id);
+  }
+
+ private:
+  const std::function<std::unique_ptr<AudioDecoder>(
+      const SdpAudioFormat&,
+      absl::optional<AudioCodecPairId> codec_pair_id)>
+      create_;
+};
+
+}  // namespace test
+}  // namespace webrtc
+
+#endif  // TEST_FUNCTION_AUDIO_DECODER_FACTORY_H_