Restructure neteq_rtpplay into a library with small executable wrapper.

Most of the code in neteq_rtpplay is moved into a factory class for
NetEqTest. The factory method takes the same argc and argv arguments as
neteq_rtpplay.
This CL also adds a small public API for neteq_test to allow easy
integration into external software.

Bug: webrtc:9667
Change-Id: I5241c1f51736cb6fbe47b0ad25f4bc83dabd727d
Reviewed-on: https://webrtc-review.googlesource.com/96100
Commit-Queue: Ivo Creusen <ivoc@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Reviewed-by: Minyue Li <minyue@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24531}
diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn
index 0321c31..4d24aff 100644
--- a/modules/audio_coding/BUILD.gn
+++ b/modules/audio_coding/BUILD.gn
@@ -1089,6 +1089,7 @@
     ":neteq",
     "../..:webrtc_common",
     "../../api:libjingle_peerconnection_api",
+    "../../api:neteq_simulator_api",
     "../../api/audio:audio_frame_api",
     "../../api/audio_codecs:audio_codecs_api",
     "../../api/audio_codecs:builtin_audio_decoder_factory",
@@ -1164,6 +1165,8 @@
     "neteq/tools/neteq_replacement_input.h",
     "neteq/tools/neteq_stats_getter.cc",
     "neteq/tools/neteq_stats_getter.h",
+    "neteq/tools/neteq_stats_plotter.cc",
+    "neteq/tools/neteq_stats_plotter.h",
   ]
 
   if (!build_with_chromium && is_clang) {
@@ -1505,8 +1508,9 @@
       proto_out_dir = "modules/audio_coding/neteq"
     }
 
-    rtc_test("neteq_rtpplay") {
+    rtc_source_set("neteq_test_factory") {
       testonly = true
+      visibility += webrtc_default_visibility
       defines = []
       deps = [
         "../../rtc_base:checks",
@@ -1514,7 +1518,8 @@
         "../../test:fileutils",
       ]
       sources = [
-        "neteq/tools/neteq_rtpplay.cc",
+        "neteq/tools/neteq_test_factory.cc",
+        "neteq/tools/neteq_test_factory.h",
       ]
 
       if (!build_with_chromium && is_clang) {
@@ -1529,6 +1534,19 @@
         "../../rtc_base:rtc_base_approved",
         "../../system_wrappers:system_wrappers_default",
         "../../test:test_support",
+        "//third_party/abseil-cpp/absl/memory",
+      ]
+    }
+
+    rtc_test("neteq_rtpplay") {
+      testonly = true
+      defines = []
+      deps = [
+        ":neteq_test_factory",
+        ":neteq_test_tools",
+      ]
+      sources = [
+        "neteq/tools/neteq_rtpplay.cc",
       ]
     }
   }
diff --git a/modules/audio_coding/neteq/include/neteq.h b/modules/audio_coding/neteq/include/neteq.h
index fdb73c5..f376b05 100644
--- a/modules/audio_coding/neteq/include/neteq.h
+++ b/modules/audio_coding/neteq/include/neteq.h
@@ -21,6 +21,7 @@
 #include "api/audio_codecs/audio_decoder.h"
 #include "api/rtp_headers.h"
 #include "common_types.h"  // NOLINT(build/include)
+#include "modules/audio_coding/neteq/defines.h"
 #include "modules/audio_coding/neteq/neteq_decoder_enum.h"
 #include "rtc_base/constructormagic.h"
 #include "rtc_base/scoped_ref_ptr.h"
@@ -129,9 +130,14 @@
   // If muted state is enabled (through Config::enable_muted_state), |muted|
   // may be set to true after a prolonged expand period. When this happens, the
   // |data_| in |audio_frame| is not written, but should be interpreted as being
-  // all zeros.
+  // all zeros. For testing purposes, an override can be supplied in the
+  // |action_override| argument, which will cause NetEq to take this action
+  // next, instead of the action it would normally choose.
   // Returns kOK on success, or kFail in case of an error.
-  virtual int GetAudio(AudioFrame* audio_frame, bool* muted) = 0;
+  virtual int GetAudio(
+      AudioFrame* audio_frame,
+      bool* muted,
+      absl::optional<Operations> action_override = absl::nullopt) = 0;
 
   // Replaces the current set of decoders with the given one.
   virtual void SetCodecs(const std::map<int, SdpAudioFormat>& codecs) = 0;
diff --git a/modules/audio_coding/neteq/neteq_impl.cc b/modules/audio_coding/neteq/neteq_impl.cc
index ddcd221..3b2bd83 100644
--- a/modules/audio_coding/neteq/neteq_impl.cc
+++ b/modules/audio_coding/neteq/neteq_impl.cc
@@ -199,10 +199,12 @@
 }
 }  // namespace
 
-int NetEqImpl::GetAudio(AudioFrame* audio_frame, bool* muted) {
+int NetEqImpl::GetAudio(AudioFrame* audio_frame,
+                        bool* muted,
+                        absl::optional<Operations> action_override) {
   TRACE_EVENT0("webrtc", "NetEqImpl::GetAudio");
   rtc::CritScope lock(&crit_sect_);
-  if (GetAudioInternal(audio_frame, muted) != 0) {
+  if (GetAudioInternal(audio_frame, muted, action_override) != 0) {
     return kFail;
   }
   RTC_DCHECK_EQ(
@@ -798,7 +800,9 @@
   return 0;
 }
 
-int NetEqImpl::GetAudioInternal(AudioFrame* audio_frame, bool* muted) {
+int NetEqImpl::GetAudioInternal(AudioFrame* audio_frame,
+                                bool* muted,
+                                absl::optional<Operations> action_override) {
   PacketList packet_list;
   DtmfEvent dtmf_event;
   Operations operation;
@@ -831,9 +835,8 @@
     *muted = true;
     return 0;
   }
-
-  int return_value =
-      GetDecision(&operation, &packet_list, &dtmf_event, &play_dtmf);
+  int return_value = GetDecision(&operation, &packet_list, &dtmf_event,
+                                 &play_dtmf, action_override);
   if (return_value != 0) {
     last_mode_ = kModeError;
     return return_value;
@@ -1021,7 +1024,8 @@
 int NetEqImpl::GetDecision(Operations* operation,
                            PacketList* packet_list,
                            DtmfEvent* dtmf_event,
-                           bool* play_dtmf) {
+                           bool* play_dtmf,
+                           absl::optional<Operations> action_override) {
   // Initialize output variables.
   *play_dtmf = false;
   *operation = kUndefined;
@@ -1093,6 +1097,10 @@
       *sync_buffer_, *expand_, decoder_frame_length_, packet, last_mode_,
       *play_dtmf, generated_noise_samples, &reset_decoder_);
 
+  if (action_override) {
+    // Use the provided action instead of the decision NetEq decided on.
+    *operation = *action_override;
+  }
   // Check if we already have enough samples in the |sync_buffer_|. If so,
   // change decision to normal, unless the decision was merge, accelerate, or
   // preemptive expand.
diff --git a/modules/audio_coding/neteq/neteq_impl.h b/modules/audio_coding/neteq/neteq_impl.h
index 68fdf3c..dcb931e 100644
--- a/modules/audio_coding/neteq/neteq_impl.h
+++ b/modules/audio_coding/neteq/neteq_impl.h
@@ -131,7 +131,10 @@
 
   void InsertEmptyPacket(const RTPHeader& rtp_header) override;
 
-  int GetAudio(AudioFrame* audio_frame, bool* muted) override;
+  int GetAudio(
+      AudioFrame* audio_frame,
+      bool* muted,
+      absl::optional<Operations> action_override = absl::nullopt) override;
 
   void SetCodecs(const std::map<int, SdpAudioFormat>& codecs) override;
 
@@ -230,7 +233,9 @@
 
   // Delivers 10 ms of audio data. The data is written to |audio_frame|.
   // Returns 0 on success, otherwise an error code.
-  int GetAudioInternal(AudioFrame* audio_frame, bool* muted)
+  int GetAudioInternal(AudioFrame* audio_frame,
+                       bool* muted,
+                       absl::optional<Operations> action_override)
       RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
 
   // Provides a decision to the GetAudioInternal method. The decision what to
@@ -241,7 +246,9 @@
   int GetDecision(Operations* operation,
                   PacketList* packet_list,
                   DtmfEvent* dtmf_event,
-                  bool* play_dtmf) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
+                  bool* play_dtmf,
+                  absl::optional<Operations> action_override)
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
 
   // Decodes the speech packets in |packet_list|, and writes the results to
   // |decoded_buffer|, which is allocated to hold |decoded_buffer_length|
diff --git a/modules/audio_coding/neteq/tools/neteq_rtpplay.cc b/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
index bf782bf..2968a22 100644
--- a/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
+++ b/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
@@ -8,544 +8,15 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include <errno.h>
-#include <inttypes.h>
-#include <limits.h>  // For ULONG_MAX returned by strtoul.
 #include <stdio.h>
-#include <stdlib.h>  // For strtoul.
-#include <iostream>
-#include <memory>
-#include <string>
 
-#include "modules/audio_coding/neteq/include/neteq.h"
-#include "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
-#include "modules/audio_coding/neteq/tools/input_audio_file.h"
-#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
-#include "modules/audio_coding/neteq/tools/neteq_event_log_input.h"
-#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
-#include "modules/audio_coding/neteq/tools/neteq_replacement_input.h"
-#include "modules/audio_coding/neteq/tools/neteq_stats_getter.h"
 #include "modules/audio_coding/neteq/tools/neteq_test.h"
-#include "modules/audio_coding/neteq/tools/output_audio_file.h"
-#include "modules/audio_coding/neteq/tools/output_wav_file.h"
-#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/flags.h"
-#include "test/field_trial.h"
-#include "test/testsupport/fileutils.h"
-
-namespace webrtc {
-namespace test {
-namespace {
-
-// Parses the input string for a valid SSRC (at the start of the string). If a
-// valid SSRC is found, it is written to the output variable |ssrc|, and true is
-// returned. Otherwise, false is returned.
-bool ParseSsrc(const std::string& str, uint32_t* ssrc) {
-  if (str.empty())
-    return true;
-  int base = 10;
-  // Look for "0x" or "0X" at the start and change base to 16 if found.
-  if ((str.compare(0, 2, "0x") == 0) || (str.compare(0, 2, "0X") == 0))
-    base = 16;
-  errno = 0;
-  char* end_ptr;
-  unsigned long value = strtoul(str.c_str(), &end_ptr, base);
-  if (value == ULONG_MAX && errno == ERANGE)
-    return false;  // Value out of range for unsigned long.
-  if (sizeof(unsigned long) > sizeof(uint32_t) && value > 0xFFFFFFFF)
-    return false;  // Value out of range for uint32_t.
-  if (end_ptr - str.c_str() < static_cast<ptrdiff_t>(str.length()))
-    return false;  // Part of the string was not parsed.
-  *ssrc = static_cast<uint32_t>(value);
-  return true;
-}
-
-// Flag validators.
-bool ValidatePayloadType(int value) {
-  if (value >= 0 && value <= 127)  // Value is ok.
-    return true;
-  printf("Payload type must be between 0 and 127, not %d\n",
-         static_cast<int>(value));
-  return false;
-}
-
-bool ValidateSsrcValue(const std::string& str) {
-  uint32_t dummy_ssrc;
-  if (ParseSsrc(str, &dummy_ssrc))  // Value is ok.
-    return true;
-  printf("Invalid SSRC: %s\n", str.c_str());
-  return false;
-}
-
-static bool ValidateExtensionId(int value) {
-  if (value > 0 && value <= 255)  // Value is ok.
-    return true;
-  printf("Extension ID must be between 1 and 255, not %d\n",
-         static_cast<int>(value));
-  return false;
-}
-
-// Define command line flags.
-DEFINE_int(pcmu, 0, "RTP payload type for PCM-u");
-DEFINE_int(pcma, 8, "RTP payload type for PCM-a");
-DEFINE_int(ilbc, 102, "RTP payload type for iLBC");
-DEFINE_int(isac, 103, "RTP payload type for iSAC");
-DEFINE_int(isac_swb, 104, "RTP payload type for iSAC-swb (32 kHz)");
-DEFINE_int(opus, 111, "RTP payload type for Opus");
-DEFINE_int(pcm16b, 93, "RTP payload type for PCM16b-nb (8 kHz)");
-DEFINE_int(pcm16b_wb, 94, "RTP payload type for PCM16b-wb (16 kHz)");
-DEFINE_int(pcm16b_swb32, 95, "RTP payload type for PCM16b-swb32 (32 kHz)");
-DEFINE_int(pcm16b_swb48, 96, "RTP payload type for PCM16b-swb48 (48 kHz)");
-DEFINE_int(g722, 9, "RTP payload type for G.722");
-DEFINE_int(avt, 106, "RTP payload type for AVT/DTMF (8 kHz)");
-DEFINE_int(avt_16, 114, "RTP payload type for AVT/DTMF (16 kHz)");
-DEFINE_int(avt_32, 115, "RTP payload type for AVT/DTMF (32 kHz)");
-DEFINE_int(avt_48, 116, "RTP payload type for AVT/DTMF (48 kHz)");
-DEFINE_int(red, 117, "RTP payload type for redundant audio (RED)");
-DEFINE_int(cn_nb, 13, "RTP payload type for comfort noise (8 kHz)");
-DEFINE_int(cn_wb, 98, "RTP payload type for comfort noise (16 kHz)");
-DEFINE_int(cn_swb32, 99, "RTP payload type for comfort noise (32 kHz)");
-DEFINE_int(cn_swb48, 100, "RTP payload type for comfort noise (48 kHz)");
-DEFINE_bool(codec_map,
-            false,
-            "Prints the mapping between RTP payload type and "
-            "codec");
-DEFINE_string(replacement_audio_file,
-              "",
-              "A PCM file that will be used to populate "
-              "dummy"
-              " RTP packets");
-DEFINE_string(ssrc,
-              "",
-              "Only use packets with this SSRC (decimal or hex, the latter "
-              "starting with 0x)");
-DEFINE_int(audio_level, 1, "Extension ID for audio level (RFC 6464)");
-DEFINE_int(abs_send_time, 3, "Extension ID for absolute sender time");
-DEFINE_int(transport_seq_no, 5, "Extension ID for transport sequence number");
-DEFINE_int(video_content_type, 7, "Extension ID for video content type");
-DEFINE_int(video_timing, 8, "Extension ID for video timing");
-DEFINE_bool(matlabplot,
-            false,
-            "Generates a matlab script for plotting the delay profile");
-DEFINE_bool(pythonplot,
-            false,
-            "Generates a python script for plotting the delay profile");
-DEFINE_bool(help, false, "Prints this message");
-DEFINE_bool(concealment_events, false, "Prints concealment events");
-DEFINE_string(
-    force_fieldtrials,
-    "",
-    "Field trials control experimental feature code which can be forced. "
-    "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
-    " will assign the group Enable to field trial WebRTC-FooFeature.");
-
-// Maps a codec type to a printable name string.
-std::string CodecName(NetEqDecoder codec) {
-  switch (codec) {
-    case NetEqDecoder::kDecoderPCMu:
-      return "PCM-u";
-    case NetEqDecoder::kDecoderPCMa:
-      return "PCM-a";
-    case NetEqDecoder::kDecoderILBC:
-      return "iLBC";
-    case NetEqDecoder::kDecoderISAC:
-      return "iSAC";
-    case NetEqDecoder::kDecoderISACswb:
-      return "iSAC-swb (32 kHz)";
-    case NetEqDecoder::kDecoderOpus:
-      return "Opus";
-    case NetEqDecoder::kDecoderPCM16B:
-      return "PCM16b-nb (8 kHz)";
-    case NetEqDecoder::kDecoderPCM16Bwb:
-      return "PCM16b-wb (16 kHz)";
-    case NetEqDecoder::kDecoderPCM16Bswb32kHz:
-      return "PCM16b-swb32 (32 kHz)";
-    case NetEqDecoder::kDecoderPCM16Bswb48kHz:
-      return "PCM16b-swb48 (48 kHz)";
-    case NetEqDecoder::kDecoderG722:
-      return "G.722";
-    case NetEqDecoder::kDecoderRED:
-      return "redundant audio (RED)";
-    case NetEqDecoder::kDecoderAVT:
-      return "AVT/DTMF (8 kHz)";
-    case NetEqDecoder::kDecoderAVT16kHz:
-      return "AVT/DTMF (16 kHz)";
-    case NetEqDecoder::kDecoderAVT32kHz:
-      return "AVT/DTMF (32 kHz)";
-    case NetEqDecoder::kDecoderAVT48kHz:
-      return "AVT/DTMF (48 kHz)";
-    case NetEqDecoder::kDecoderCNGnb:
-      return "comfort noise (8 kHz)";
-    case NetEqDecoder::kDecoderCNGwb:
-      return "comfort noise (16 kHz)";
-    case NetEqDecoder::kDecoderCNGswb32kHz:
-      return "comfort noise (32 kHz)";
-    case NetEqDecoder::kDecoderCNGswb48kHz:
-      return "comfort noise (48 kHz)";
-    default:
-      FATAL();
-      return "undefined";
-  }
-}
-
-void PrintCodecMappingEntry(NetEqDecoder codec, int flag) {
-  std::cout << CodecName(codec) << ": " << flag << std::endl;
-}
-
-void PrintCodecMapping() {
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMu, FLAG_pcmu);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMa, FLAG_pcma);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderILBC, FLAG_ilbc);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderISAC, FLAG_isac);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderISACswb, FLAG_isac_swb);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderOpus, FLAG_opus);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16B, FLAG_pcm16b);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bwb, FLAG_pcm16b_wb);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb32kHz,
-                         FLAG_pcm16b_swb32);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb48kHz,
-                         FLAG_pcm16b_swb48);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderG722, FLAG_g722);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT, FLAG_avt);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT16kHz, FLAG_avt_16);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT32kHz, FLAG_avt_32);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT48kHz, FLAG_avt_48);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderRED, FLAG_red);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGnb, FLAG_cn_nb);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGwb, FLAG_cn_wb);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb32kHz, FLAG_cn_swb32);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb48kHz, FLAG_cn_swb48);
-}
-
-absl::optional<int> CodecSampleRate(uint8_t payload_type) {
-  if (payload_type == FLAG_pcmu || payload_type == FLAG_pcma ||
-      payload_type == FLAG_ilbc || payload_type == FLAG_pcm16b ||
-      payload_type == FLAG_cn_nb || payload_type == FLAG_avt)
-    return 8000;
-  if (payload_type == FLAG_isac || payload_type == FLAG_pcm16b_wb ||
-      payload_type == FLAG_g722 || payload_type == FLAG_cn_wb ||
-      payload_type == FLAG_avt_16)
-    return 16000;
-  if (payload_type == FLAG_isac_swb || payload_type == FLAG_pcm16b_swb32 ||
-      payload_type == FLAG_cn_swb32 || payload_type == FLAG_avt_32)
-    return 32000;
-  if (payload_type == FLAG_opus || payload_type == FLAG_pcm16b_swb48 ||
-      payload_type == FLAG_cn_swb48 || payload_type == FLAG_avt_48)
-    return 48000;
-  if (payload_type == FLAG_red)
-    return 0;
-  return absl::nullopt;
-}
-
-// A callback class which prints whenver the inserted packet stream changes
-// the SSRC.
-class SsrcSwitchDetector : public NetEqPostInsertPacket {
- public:
-  // Takes a pointer to another callback object, which will be invoked after
-  // this object finishes. This does not transfer ownership, and null is a
-  // valid value.
-  explicit SsrcSwitchDetector(NetEqPostInsertPacket* other_callback)
-      : other_callback_(other_callback) {}
-
-  void AfterInsertPacket(const NetEqInput::PacketData& packet,
-                         NetEq* neteq) override {
-    if (last_ssrc_ && packet.header.ssrc != *last_ssrc_) {
-      std::cout << "Changing streams from 0x" << std::hex << *last_ssrc_
-                << " to 0x" << std::hex << packet.header.ssrc << std::dec
-                << " (payload type "
-                << static_cast<int>(packet.header.payloadType) << ")"
-                << std::endl;
-    }
-    last_ssrc_ = packet.header.ssrc;
-    if (other_callback_) {
-      other_callback_->AfterInsertPacket(packet, neteq);
-    }
-  }
-
- private:
-  NetEqPostInsertPacket* other_callback_;
-  absl::optional<uint32_t> last_ssrc_;
-};
-
-int RunTest(int argc, char* argv[]) {
-  std::string program_name = argv[0];
-  std::string usage =
-      "Tool for decoding an RTP dump file using NetEq.\n"
-      "Run " +
-      program_name +
-      " --help for usage.\n"
-      "Example usage:\n" +
-      program_name + " input.rtp output.{pcm, wav}\n";
-  if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
-    return 1;
-  }
-  if (FLAG_help) {
-    std::cout << usage;
-    rtc::FlagList::Print(nullptr, false);
-    return 0;
-  }
-
-  if (FLAG_codec_map) {
-    PrintCodecMapping();
-  }
-
-  if (argc != 3) {
-    if (FLAG_codec_map) {
-      // We have already printed the codec map. Just end the program.
-      return 0;
-    }
-    // Print usage information.
-    std::cout << usage;
-    return 0;
-  }
-
-  ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
-  ScopedFieldTrials field_trials(FLAG_force_fieldtrials);
-
-  RTC_CHECK(ValidatePayloadType(FLAG_pcmu));
-  RTC_CHECK(ValidatePayloadType(FLAG_pcma));
-  RTC_CHECK(ValidatePayloadType(FLAG_ilbc));
-  RTC_CHECK(ValidatePayloadType(FLAG_isac));
-  RTC_CHECK(ValidatePayloadType(FLAG_isac_swb));
-  RTC_CHECK(ValidatePayloadType(FLAG_opus));
-  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b));
-  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_wb));
-  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb32));
-  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb48));
-  RTC_CHECK(ValidatePayloadType(FLAG_g722));
-  RTC_CHECK(ValidatePayloadType(FLAG_avt));
-  RTC_CHECK(ValidatePayloadType(FLAG_avt_16));
-  RTC_CHECK(ValidatePayloadType(FLAG_avt_32));
-  RTC_CHECK(ValidatePayloadType(FLAG_avt_48));
-  RTC_CHECK(ValidatePayloadType(FLAG_red));
-  RTC_CHECK(ValidatePayloadType(FLAG_cn_nb));
-  RTC_CHECK(ValidatePayloadType(FLAG_cn_wb));
-  RTC_CHECK(ValidatePayloadType(FLAG_cn_swb32));
-  RTC_CHECK(ValidatePayloadType(FLAG_cn_swb48));
-  RTC_CHECK(ValidateSsrcValue(FLAG_ssrc));
-  RTC_CHECK(ValidateExtensionId(FLAG_audio_level));
-  RTC_CHECK(ValidateExtensionId(FLAG_abs_send_time));
-  RTC_CHECK(ValidateExtensionId(FLAG_transport_seq_no));
-  RTC_CHECK(ValidateExtensionId(FLAG_video_content_type));
-  RTC_CHECK(ValidateExtensionId(FLAG_video_timing));
-
-  // Gather RTP header extensions in a map.
-  NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
-      {FLAG_audio_level, kRtpExtensionAudioLevel},
-      {FLAG_abs_send_time, kRtpExtensionAbsoluteSendTime},
-      {FLAG_transport_seq_no, kRtpExtensionTransportSequenceNumber},
-      {FLAG_video_content_type, kRtpExtensionVideoContentType},
-      {FLAG_video_timing, kRtpExtensionVideoTiming}};
-
-  const std::string input_file_name = argv[1];
-  std::unique_ptr<NetEqInput> input;
-  if (RtpFileSource::ValidRtpDump(input_file_name) ||
-      RtpFileSource::ValidPcap(input_file_name)) {
-    input.reset(new NetEqRtpDumpInput(input_file_name, rtp_ext_map));
-  } else {
-    input.reset(new NetEqEventLogInput(input_file_name, rtp_ext_map));
-  }
-
-  std::cout << "Input file: " << input_file_name << std::endl;
-  RTC_CHECK(input) << "Cannot open input file";
-  RTC_CHECK(!input->ended()) << "Input file is empty";
-
-  // Check if an SSRC value was provided.
-  if (strlen(FLAG_ssrc) > 0) {
-    uint32_t ssrc;
-    RTC_CHECK(ParseSsrc(FLAG_ssrc, &ssrc)) << "Flag verification has failed.";
-    static_cast<NetEqPacketSourceInput*>(input.get())->SelectSsrc(ssrc);
-  }
-
-  // Check the sample rate.
-  absl::optional<int> sample_rate_hz;
-  std::set<std::pair<int, uint32_t>> discarded_pt_and_ssrc;
-  while (absl::optional<RTPHeader> first_rtp_header = input->NextHeader()) {
-    RTC_DCHECK(first_rtp_header);
-    sample_rate_hz = CodecSampleRate(first_rtp_header->payloadType);
-    if (sample_rate_hz) {
-      std::cout << "Found valid packet with payload type "
-                << static_cast<int>(first_rtp_header->payloadType)
-                << " and SSRC 0x" << std::hex << first_rtp_header->ssrc
-                << std::dec << std::endl;
-      break;
-    }
-    // Discard this packet and move to the next. Keep track of discarded payload
-    // types and SSRCs.
-    discarded_pt_and_ssrc.emplace(first_rtp_header->payloadType,
-                                  first_rtp_header->ssrc);
-    input->PopPacket();
-  }
-  if (!discarded_pt_and_ssrc.empty()) {
-    std::cout << "Discarded initial packets with the following payload types "
-                 "and SSRCs:"
-              << std::endl;
-    for (const auto& d : discarded_pt_and_ssrc) {
-      std::cout << "PT " << d.first << "; SSRC 0x" << std::hex
-                << static_cast<int>(d.second) << std::dec << std::endl;
-    }
-  }
-  if (!sample_rate_hz) {
-    std::cout << "Cannot find any packets with known payload types"
-              << std::endl;
-    RTC_NOTREACHED();
-  }
-
-  // Open the output file now that we know the sample rate. (Rate is only needed
-  // for wav files.)
-  const std::string output_file_name = argv[2];
-  std::unique_ptr<AudioSink> output;
-  if (output_file_name.size() >= 4 &&
-      output_file_name.substr(output_file_name.size() - 4) == ".wav") {
-    // Open a wav file.
-    output.reset(new OutputWavFile(output_file_name, *sample_rate_hz));
-  } else {
-    // Open a pcm file.
-    output.reset(new OutputAudioFile(output_file_name));
-  }
-
-  std::cout << "Output file: " << output_file_name << std::endl;
-
-  NetEqTest::DecoderMap codecs = {
-    {FLAG_pcmu, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")},
-    {FLAG_pcma, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")},
-#ifdef WEBRTC_CODEC_ILBC
-    {FLAG_ilbc, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")},
-#endif
-    {FLAG_isac, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")},
-#if !defined(WEBRTC_ANDROID)
-    {FLAG_isac_swb, std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")},
-#endif
-#ifdef WEBRTC_CODEC_OPUS
-    {FLAG_opus, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")},
-#endif
-    {FLAG_pcm16b, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")},
-    {FLAG_pcm16b_wb,
-     std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")},
-    {FLAG_pcm16b_swb32,
-     std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")},
-    {FLAG_pcm16b_swb48,
-     std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")},
-    {FLAG_g722, std::make_pair(NetEqDecoder::kDecoderG722, "g722")},
-    {FLAG_avt, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")},
-    {FLAG_avt_16, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")},
-    {FLAG_avt_32, std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")},
-    {FLAG_avt_48, std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")},
-    {FLAG_red, std::make_pair(NetEqDecoder::kDecoderRED, "red")},
-    {FLAG_cn_nb, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")},
-    {FLAG_cn_wb, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")},
-    {FLAG_cn_swb32,
-     std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")},
-    {FLAG_cn_swb48,
-     std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")}
-  };
-
-  // Check if a replacement audio file was provided.
-  std::unique_ptr<AudioDecoder> replacement_decoder;
-  NetEqTest::ExtDecoderMap ext_codecs;
-  if (strlen(FLAG_replacement_audio_file) > 0) {
-    // Find largest unused payload type.
-    int replacement_pt = 127;
-    while (!(codecs.find(replacement_pt) == codecs.end() &&
-             ext_codecs.find(replacement_pt) == ext_codecs.end())) {
-      --replacement_pt;
-      RTC_CHECK_GE(replacement_pt, 0);
-    }
-
-    auto std_set_int32_to_uint8 = [](const std::set<int32_t>& a) {
-      std::set<uint8_t> b;
-      for (auto& x : a) {
-        b.insert(static_cast<uint8_t>(x));
-      }
-      return b;
-    };
-
-    std::set<uint8_t> cn_types = std_set_int32_to_uint8(
-        {FLAG_cn_nb, FLAG_cn_wb, FLAG_cn_swb32, FLAG_cn_swb48});
-    std::set<uint8_t> forbidden_types = std_set_int32_to_uint8(
-        {FLAG_g722, FLAG_red, FLAG_avt, FLAG_avt_16, FLAG_avt_32, FLAG_avt_48});
-    input.reset(new NetEqReplacementInput(std::move(input), replacement_pt,
-                                          cn_types, forbidden_types));
-
-    replacement_decoder.reset(new FakeDecodeFromFile(
-        std::unique_ptr<InputAudioFile>(
-            new InputAudioFile(FLAG_replacement_audio_file)),
-        48000, false));
-    NetEqTest::ExternalDecoderInfo ext_dec_info = {
-        replacement_decoder.get(), NetEqDecoder::kDecoderArbitrary,
-        "replacement codec"};
-    ext_codecs[replacement_pt] = ext_dec_info;
-  }
-
-  NetEqTest::Callbacks callbacks;
-  std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer;
-  if (FLAG_matlabplot || FLAG_pythonplot) {
-    delay_analyzer.reset(new NetEqDelayAnalyzer);
-  }
-
-  SsrcSwitchDetector ssrc_switch_detector(delay_analyzer.get());
-  callbacks.post_insert_packet = &ssrc_switch_detector;
-  NetEqStatsGetter stats_getter(std::move(delay_analyzer));
-  callbacks.get_audio_callback = &stats_getter;
-  NetEq::Config config;
-  config.sample_rate_hz = *sample_rate_hz;
-  NetEqTest test(config, codecs, ext_codecs, std::move(input),
-                 std::move(output), callbacks);
-
-  int64_t test_duration_ms = test.Run();
-
-  if (FLAG_matlabplot) {
-    auto matlab_script_name = output_file_name;
-    std::replace(matlab_script_name.begin(), matlab_script_name.end(), '.',
-                 '_');
-    std::cout << "Creating Matlab plot script " << matlab_script_name + ".m"
-              << std::endl;
-    stats_getter.delay_analyzer()->CreateMatlabScript(matlab_script_name +
-                                                      ".m");
-  }
-  if (FLAG_pythonplot) {
-    auto python_script_name = output_file_name;
-    std::replace(python_script_name.begin(), python_script_name.end(), '.',
-                 '_');
-    std::cout << "Creating Python plot script " << python_script_name + ".py"
-              << std::endl;
-    stats_getter.delay_analyzer()->CreatePythonScript(python_script_name +
-                                                      ".py");
-  }
-
-  printf("Simulation statistics:\n");
-  printf("  output duration: %" PRId64 " ms\n", test_duration_ms);
-  auto stats = stats_getter.AverageStats();
-  printf("  packet_loss_rate: %f %%\n", 100.0 * stats.packet_loss_rate);
-  printf("  expand_rate: %f %%\n", 100.0 * stats.expand_rate);
-  printf("  speech_expand_rate: %f %%\n", 100.0 * stats.speech_expand_rate);
-  printf("  preemptive_rate: %f %%\n", 100.0 * stats.preemptive_rate);
-  printf("  accelerate_rate: %f %%\n", 100.0 * stats.accelerate_rate);
-  printf("  secondary_decoded_rate: %f %%\n",
-         100.0 * stats.secondary_decoded_rate);
-  printf("  secondary_discarded_rate: %f %%\n",
-         100.0 * stats.secondary_discarded_rate);
-  printf("  clockdrift_ppm: %f ppm\n", stats.clockdrift_ppm);
-  printf("  mean_waiting_time_ms: %f ms\n", stats.mean_waiting_time_ms);
-  printf("  median_waiting_time_ms: %f ms\n", stats.median_waiting_time_ms);
-  printf("  min_waiting_time_ms: %f ms\n", stats.min_waiting_time_ms);
-  printf("  max_waiting_time_ms: %f ms\n", stats.max_waiting_time_ms);
-  printf("  current_buffer_size_ms: %f ms\n", stats.current_buffer_size_ms);
-  printf("  preferred_buffer_size_ms: %f ms\n", stats.preferred_buffer_size_ms);
-  if (FLAG_concealment_events) {
-    std::cout << " concealment_events_ms:" << std::endl;
-    for (auto concealment_event : stats_getter.concealment_events())
-      std::cout << concealment_event.ToString() << std::endl;
-    std::cout << " end of concealment_events_ms" << std::endl;
-  }
-  return 0;
-}
-
-}  // namespace
-}  // namespace test
-}  // namespace webrtc
+#include "modules/audio_coding/neteq/tools/neteq_test_factory.h"
 
 int main(int argc, char* argv[]) {
-  return webrtc::test::RunTest(argc, argv);
+  webrtc::test::NetEqTestFactory factory;
+  std::unique_ptr<webrtc::test::NetEqTest> test =
+      factory.InitializeTest(argc, argv);
+  test->Run();
+  return 0;
 }
diff --git a/modules/audio_coding/neteq/tools/neteq_stats_plotter.cc b/modules/audio_coding/neteq/tools/neteq_stats_plotter.cc
new file mode 100644
index 0000000..3f86cfd
--- /dev/null
+++ b/modules/audio_coding/neteq/tools/neteq_stats_plotter.cc
@@ -0,0 +1,81 @@
+/*
+ *  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.
+ */
+
+#include "modules/audio_coding/neteq/tools/neteq_stats_plotter.h"
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <utility>
+
+namespace webrtc {
+namespace test {
+
+NetEqStatsPlotter::NetEqStatsPlotter(bool make_matlab_plot,
+                                     bool make_python_plot,
+                                     bool show_concealment_events,
+                                     std::string base_file_name)
+    : make_matlab_plot_(make_matlab_plot),
+      make_python_plot_(make_python_plot),
+      show_concealment_events_(show_concealment_events),
+      base_file_name_(base_file_name) {
+  std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer;
+  if (make_matlab_plot || make_python_plot) {
+    delay_analyzer.reset(new NetEqDelayAnalyzer);
+  }
+  stats_getter_.reset(new NetEqStatsGetter(std::move(delay_analyzer)));
+}
+
+void NetEqStatsPlotter::SimulationEnded(int64_t simulation_time_ms) {
+  if (make_matlab_plot_) {
+    auto matlab_script_name = base_file_name_;
+    std::replace(matlab_script_name.begin(), matlab_script_name.end(), '.',
+                 '_');
+    printf("Creating Matlab plot script %s.m\n", matlab_script_name.c_str());
+    stats_getter_->delay_analyzer()->CreateMatlabScript(matlab_script_name +
+                                                        ".m");
+  }
+  if (make_python_plot_) {
+    auto python_script_name = base_file_name_;
+    std::replace(python_script_name.begin(), python_script_name.end(), '.',
+                 '_');
+    printf("Creating Python plot script %s.py\n", python_script_name.c_str());
+    stats_getter_->delay_analyzer()->CreatePythonScript(python_script_name +
+                                                        ".py");
+  }
+
+  printf("Simulation statistics:\n");
+  printf("  output duration: %" PRId64 " ms\n", simulation_time_ms);
+  auto stats = stats_getter_->AverageStats();
+  printf("  packet_loss_rate: %f %%\n", 100.0 * stats.packet_loss_rate);
+  printf("  expand_rate: %f %%\n", 100.0 * stats.expand_rate);
+  printf("  speech_expand_rate: %f %%\n", 100.0 * stats.speech_expand_rate);
+  printf("  preemptive_rate: %f %%\n", 100.0 * stats.preemptive_rate);
+  printf("  accelerate_rate: %f %%\n", 100.0 * stats.accelerate_rate);
+  printf("  secondary_decoded_rate: %f %%\n",
+         100.0 * stats.secondary_decoded_rate);
+  printf("  secondary_discarded_rate: %f %%\n",
+         100.0 * stats.secondary_discarded_rate);
+  printf("  clockdrift_ppm: %f ppm\n", stats.clockdrift_ppm);
+  printf("  mean_waiting_time_ms: %f ms\n", stats.mean_waiting_time_ms);
+  printf("  median_waiting_time_ms: %f ms\n", stats.median_waiting_time_ms);
+  printf("  min_waiting_time_ms: %f ms\n", stats.min_waiting_time_ms);
+  printf("  max_waiting_time_ms: %f ms\n", stats.max_waiting_time_ms);
+  printf("  current_buffer_size_ms: %f ms\n", stats.current_buffer_size_ms);
+  printf("  preferred_buffer_size_ms: %f ms\n", stats.preferred_buffer_size_ms);
+  if (show_concealment_events_) {
+    printf(" concealment_events_ms:\n");
+    for (auto concealment_event : stats_getter_->concealment_events())
+      printf("%s\n", concealment_event.ToString().c_str());
+    printf(" end of concealment_events_ms\n");
+  }
+}
+
+}  // namespace test
+}  // namespace webrtc
diff --git a/modules/audio_coding/neteq/tools/neteq_stats_plotter.h b/modules/audio_coding/neteq/tools/neteq_stats_plotter.h
new file mode 100644
index 0000000..c4df24e
--- /dev/null
+++ b/modules/audio_coding/neteq/tools/neteq_stats_plotter.h
@@ -0,0 +1,46 @@
+/*
+ *  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 MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_STATS_PLOTTER_H_
+#define MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_STATS_PLOTTER_H_
+
+#include <memory>
+#include <string>
+
+#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
+#include "modules/audio_coding/neteq/tools/neteq_stats_getter.h"
+#include "modules/audio_coding/neteq/tools/neteq_test.h"
+
+namespace webrtc {
+namespace test {
+
+class NetEqStatsPlotter : public NetEqSimulationEndedCallback {
+ public:
+  NetEqStatsPlotter(bool make_matlab_plot,
+                    bool make_python_plot,
+                    bool show_concealment_events,
+                    std::string base_file_name);
+
+  void SimulationEnded(int64_t simulation_time_ms) override;
+
+  NetEqStatsGetter* stats_getter() { return stats_getter_.get(); }
+
+ private:
+  std::unique_ptr<NetEqStatsGetter> stats_getter_;
+  const bool make_matlab_plot_;
+  const bool make_python_plot_;
+  const bool show_concealment_events_;
+  const std::string base_file_name_;
+};
+
+}  // namespace test
+}  // namespace webrtc
+
+#endif  // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_STATS_PLOTTER_H_
diff --git a/modules/audio_coding/neteq/tools/neteq_test.cc b/modules/audio_coding/neteq/tools/neteq_test.cc
index 3448f4e..110819c 100644
--- a/modules/audio_coding/neteq/tools/neteq_test.cc
+++ b/modules/audio_coding/neteq/tools/neteq_test.cc
@@ -16,6 +16,26 @@
 
 namespace webrtc {
 namespace test {
+namespace {
+
+absl::optional<Operations> ActionToOperations(
+    absl::optional<NetEqSimulator::Action> a) {
+  if (!a) {
+    return absl::nullopt;
+  }
+  switch (*a) {
+    case NetEqSimulator::Action::kAccelerate:
+      return absl::make_optional(kAccelerate);
+    case NetEqSimulator::Action::kExpand:
+      return absl::make_optional(kExpand);
+    case NetEqSimulator::Action::kNormal:
+      return absl::make_optional(kNormal);
+    case NetEqSimulator::Action::kPreemptiveExpand:
+      return absl::make_optional(kPreemptiveExpand);
+  }
+}
+
+}  // namespace
 
 void DefaultNetEqTestErrorCallback::OnInsertPacketError(
     const NetEqInput::PacketData& packet) {
@@ -49,6 +69,20 @@
 NetEqTest::~NetEqTest() = default;
 
 int64_t NetEqTest::Run() {
+  int64_t simulation_time = 0;
+  SimulationStepResult step_result;
+  do {
+    step_result = RunToNextGetAudio();
+    simulation_time += step_result.simulation_step_ms;
+  } while (!step_result.is_simulation_finished);
+  if (callbacks_.simulation_ended_callback) {
+    callbacks_.simulation_ended_callback->SimulationEnded(simulation_time);
+  }
+  return simulation_time;
+}
+
+NetEqTest::SimulationStepResult NetEqTest::RunToNextGetAudio() {
+  SimulationStepResult result;
   const int64_t start_time_ms = *input_->NextEventTime();
   int64_t time_now_ms = start_time_ms;
 
@@ -81,7 +115,9 @@
       }
       AudioFrame out_frame;
       bool muted;
-      int error = neteq_->GetAudio(&out_frame, &muted);
+      int error = neteq_->GetAudio(&out_frame, &muted,
+                                   ActionToOperations(next_action_));
+      next_action_ = absl::nullopt;
       RTC_CHECK(!muted) << "The code does not handle enable_muted_state";
       if (error != NetEq::kOK) {
         if (callbacks_.error_callback) {
@@ -102,9 +138,26 @@
       }
 
       input_->AdvanceOutputEvent();
+      result.simulation_step_ms = time_now_ms - start_time_ms;
+      // TODO(ivoc): Set the result.<action>_ms values correctly.
+      result.is_simulation_finished = input_->ended();
+      return result;
     }
   }
-  return time_now_ms - start_time_ms;
+  result.simulation_step_ms = time_now_ms - start_time_ms;
+  result.is_simulation_finished = true;
+  return result;
+}
+
+void NetEqTest::SetNextAction(NetEqTest::Action next_operation) {
+  next_action_ = absl::optional<Action>(next_operation);
+}
+
+NetEqTest::NetEqState NetEqTest::GetNetEqState() {
+  NetEqState state;
+  const auto network_stats = SimulationStats();
+  state.current_delay_ms = network_stats.current_buffer_size_ms;
+  return state;
 }
 
 NetEqNetworkStatistics NetEqTest::SimulationStats() {
diff --git a/modules/audio_coding/neteq/tools/neteq_test.h b/modules/audio_coding/neteq/tools/neteq_test.h
index ada6bab..c558045 100644
--- a/modules/audio_coding/neteq/tools/neteq_test.h
+++ b/modules/audio_coding/neteq/tools/neteq_test.h
@@ -16,6 +16,8 @@
 #include <string>
 #include <utility>
 
+#include "absl/types/optional.h"
+#include "api/test/neteq_simulator.h"
 #include "modules/audio_coding/neteq/include/neteq.h"
 #include "modules/audio_coding/neteq/tools/audio_sink.h"
 #include "modules/audio_coding/neteq/tools/neteq_input.h"
@@ -52,10 +54,16 @@
                              NetEq* neteq) = 0;
 };
 
+class NetEqSimulationEndedCallback {
+ public:
+  virtual ~NetEqSimulationEndedCallback() = default;
+  virtual void SimulationEnded(int64_t simulation_time_ms) = 0;
+};
+
 // Class that provides an input--output test for NetEq. The input (both packets
 // and output events) is provided by a NetEqInput object, while the output is
 // directed to an AudioSink object.
-class NetEqTest {
+class NetEqTest : public NetEqSimulator {
  public:
   using DecoderMap = std::map<int, std::pair<NetEqDecoder, std::string> >;
 
@@ -71,6 +79,7 @@
     NetEqTestErrorCallback* error_callback = nullptr;
     NetEqPostInsertPacket* post_insert_packet = nullptr;
     NetEqGetAudioCallback* get_audio_callback = nullptr;
+    NetEqSimulationEndedCallback* simulation_ended_callback = nullptr;
   };
 
   // Sets up the test with given configuration, codec mappings, input, ouput,
@@ -82,10 +91,17 @@
             std::unique_ptr<AudioSink> output,
             Callbacks callbacks);
 
-  ~NetEqTest();
+  ~NetEqTest() override;
 
   // Runs the test. Returns the duration of the produced audio in ms.
   int64_t Run();
+  // Runs the simulation until we hit the next GetAudio event. If the simulation
+  // is finished, is_simulation_finished will be set to true in the returned
+  // SimulationStepResult.
+  SimulationStepResult RunToNextGetAudio() override;
+
+  void SetNextAction(Action next_operation) override;
+  NetEqState GetNetEqState() override;
 
   // Returns the statistics from NetEq.
   NetEqNetworkStatistics SimulationStats();
@@ -96,7 +112,7 @@
  private:
   void RegisterDecoders(const DecoderMap& codecs);
   void RegisterExternalDecoders(const ExtDecoderMap& codecs);
-
+  absl::optional<Action> next_action_;
   std::unique_ptr<NetEq> neteq_;
   std::unique_ptr<NetEqInput> input_;
   std::unique_ptr<AudioSink> output_;
diff --git a/modules/audio_coding/neteq/tools/neteq_test_factory.cc b/modules/audio_coding/neteq/tools/neteq_test_factory.cc
new file mode 100644
index 0000000..9ab2310
--- /dev/null
+++ b/modules/audio_coding/neteq/tools/neteq_test_factory.cc
@@ -0,0 +1,509 @@
+/*
+ *  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.
+ */
+
+#include "modules/audio_coding/neteq/tools/neteq_test_factory.h"
+
+#include <errno.h>
+#include <limits.h>  // For ULONG_MAX returned by strtoul.
+#include <stdio.h>
+#include <stdlib.h>  // For strtoul.
+#include <iostream>
+#include <memory>
+#include <set>
+#include <string>
+#include <utility>
+
+#include "absl/memory/memory.h"
+#include "modules/audio_coding/neteq/include/neteq.h"
+#include "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
+#include "modules/audio_coding/neteq/tools/input_audio_file.h"
+#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
+#include "modules/audio_coding/neteq/tools/neteq_event_log_input.h"
+#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
+#include "modules/audio_coding/neteq/tools/neteq_replacement_input.h"
+#include "modules/audio_coding/neteq/tools/neteq_stats_getter.h"
+#include "modules/audio_coding/neteq/tools/neteq_stats_plotter.h"
+#include "modules/audio_coding/neteq/tools/neteq_test.h"
+#include "modules/audio_coding/neteq/tools/output_audio_file.h"
+#include "modules/audio_coding/neteq/tools/output_wav_file.h"
+#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/flags.h"
+#include "test/field_trial.h"
+#include "test/testsupport/fileutils.h"
+
+namespace webrtc {
+namespace test {
+namespace {
+
+// Parses the input string for a valid SSRC (at the start of the string). If a
+// valid SSRC is found, it is written to the output variable |ssrc|, and true is
+// returned. Otherwise, false is returned.
+bool ParseSsrc(const std::string& str, uint32_t* ssrc) {
+  if (str.empty())
+    return true;
+  int base = 10;
+  // Look for "0x" or "0X" at the start and change base to 16 if found.
+  if ((str.compare(0, 2, "0x") == 0) || (str.compare(0, 2, "0X") == 0))
+    base = 16;
+  errno = 0;
+  char* end_ptr;
+  unsigned long value = strtoul(str.c_str(), &end_ptr, base);  // NOLINT
+  if (value == ULONG_MAX && errno == ERANGE)
+    return false;  // Value out of range for unsigned long.
+  if (sizeof(unsigned long) > sizeof(uint32_t) && value > 0xFFFFFFFF)  // NOLINT
+    return false;  // Value out of range for uint32_t.
+  if (end_ptr - str.c_str() < static_cast<ptrdiff_t>(str.length()))
+    return false;  // Part of the string was not parsed.
+  *ssrc = static_cast<uint32_t>(value);
+  return true;
+}
+
+// Flag validators.
+bool ValidatePayloadType(int value) {
+  if (value >= 0 && value <= 127)  // Value is ok.
+    return true;
+  printf("Payload type must be between 0 and 127, not %d\n",
+         static_cast<int>(value));
+  return false;
+}
+
+bool ValidateSsrcValue(const std::string& str) {
+  uint32_t dummy_ssrc;
+  if (ParseSsrc(str, &dummy_ssrc))  // Value is ok.
+    return true;
+  printf("Invalid SSRC: %s\n", str.c_str());
+  return false;
+}
+
+static bool ValidateExtensionId(int value) {
+  if (value > 0 && value <= 255)  // Value is ok.
+    return true;
+  printf("Extension ID must be between 1 and 255, not %d\n",
+         static_cast<int>(value));
+  return false;
+}
+
+// Define command line flags.
+DEFINE_int(pcmu, 0, "RTP payload type for PCM-u");
+DEFINE_int(pcma, 8, "RTP payload type for PCM-a");
+DEFINE_int(ilbc, 102, "RTP payload type for iLBC");
+DEFINE_int(isac, 103, "RTP payload type for iSAC");
+DEFINE_int(isac_swb, 104, "RTP payload type for iSAC-swb (32 kHz)");
+DEFINE_int(opus, 111, "RTP payload type for Opus");
+DEFINE_int(pcm16b, 93, "RTP payload type for PCM16b-nb (8 kHz)");
+DEFINE_int(pcm16b_wb, 94, "RTP payload type for PCM16b-wb (16 kHz)");
+DEFINE_int(pcm16b_swb32, 95, "RTP payload type for PCM16b-swb32 (32 kHz)");
+DEFINE_int(pcm16b_swb48, 96, "RTP payload type for PCM16b-swb48 (48 kHz)");
+DEFINE_int(g722, 9, "RTP payload type for G.722");
+DEFINE_int(avt, 106, "RTP payload type for AVT/DTMF (8 kHz)");
+DEFINE_int(avt_16, 114, "RTP payload type for AVT/DTMF (16 kHz)");
+DEFINE_int(avt_32, 115, "RTP payload type for AVT/DTMF (32 kHz)");
+DEFINE_int(avt_48, 116, "RTP payload type for AVT/DTMF (48 kHz)");
+DEFINE_int(red, 117, "RTP payload type for redundant audio (RED)");
+DEFINE_int(cn_nb, 13, "RTP payload type for comfort noise (8 kHz)");
+DEFINE_int(cn_wb, 98, "RTP payload type for comfort noise (16 kHz)");
+DEFINE_int(cn_swb32, 99, "RTP payload type for comfort noise (32 kHz)");
+DEFINE_int(cn_swb48, 100, "RTP payload type for comfort noise (48 kHz)");
+DEFINE_bool(codec_map,
+            false,
+            "Prints the mapping between RTP payload type and "
+            "codec");
+DEFINE_string(replacement_audio_file,
+              "",
+              "A PCM file that will be used to populate "
+              "dummy"
+              " RTP packets");
+DEFINE_string(ssrc,
+              "",
+              "Only use packets with this SSRC (decimal or hex, the latter "
+              "starting with 0x)");
+DEFINE_int(audio_level, 1, "Extension ID for audio level (RFC 6464)");
+DEFINE_int(abs_send_time, 3, "Extension ID for absolute sender time");
+DEFINE_int(transport_seq_no, 5, "Extension ID for transport sequence number");
+DEFINE_int(video_content_type, 7, "Extension ID for video content type");
+DEFINE_int(video_timing, 8, "Extension ID for video timing");
+DEFINE_bool(matlabplot,
+            false,
+            "Generates a matlab script for plotting the delay profile");
+DEFINE_bool(pythonplot,
+            false,
+            "Generates a python script for plotting the delay profile");
+DEFINE_bool(help, false, "Prints this message");
+DEFINE_bool(concealment_events, false, "Prints concealment events");
+DEFINE_string(
+    force_fieldtrials,
+    "",
+    "Field trials control experimental feature code which can be forced. "
+    "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
+    " will assign the group Enable to field trial WebRTC-FooFeature.");
+
+// Maps a codec type to a printable name string.
+std::string CodecName(NetEqDecoder codec) {
+  switch (codec) {
+    case NetEqDecoder::kDecoderPCMu:
+      return "PCM-u";
+    case NetEqDecoder::kDecoderPCMa:
+      return "PCM-a";
+    case NetEqDecoder::kDecoderILBC:
+      return "iLBC";
+    case NetEqDecoder::kDecoderISAC:
+      return "iSAC";
+    case NetEqDecoder::kDecoderISACswb:
+      return "iSAC-swb (32 kHz)";
+    case NetEqDecoder::kDecoderOpus:
+      return "Opus";
+    case NetEqDecoder::kDecoderPCM16B:
+      return "PCM16b-nb (8 kHz)";
+    case NetEqDecoder::kDecoderPCM16Bwb:
+      return "PCM16b-wb (16 kHz)";
+    case NetEqDecoder::kDecoderPCM16Bswb32kHz:
+      return "PCM16b-swb32 (32 kHz)";
+    case NetEqDecoder::kDecoderPCM16Bswb48kHz:
+      return "PCM16b-swb48 (48 kHz)";
+    case NetEqDecoder::kDecoderG722:
+      return "G.722";
+    case NetEqDecoder::kDecoderRED:
+      return "redundant audio (RED)";
+    case NetEqDecoder::kDecoderAVT:
+      return "AVT/DTMF (8 kHz)";
+    case NetEqDecoder::kDecoderAVT16kHz:
+      return "AVT/DTMF (16 kHz)";
+    case NetEqDecoder::kDecoderAVT32kHz:
+      return "AVT/DTMF (32 kHz)";
+    case NetEqDecoder::kDecoderAVT48kHz:
+      return "AVT/DTMF (48 kHz)";
+    case NetEqDecoder::kDecoderCNGnb:
+      return "comfort noise (8 kHz)";
+    case NetEqDecoder::kDecoderCNGwb:
+      return "comfort noise (16 kHz)";
+    case NetEqDecoder::kDecoderCNGswb32kHz:
+      return "comfort noise (32 kHz)";
+    case NetEqDecoder::kDecoderCNGswb48kHz:
+      return "comfort noise (48 kHz)";
+    default:
+      FATAL();
+      return "undefined";
+  }
+}
+
+void PrintCodecMappingEntry(NetEqDecoder codec, int flag) {
+  std::cout << CodecName(codec) << ": " << flag << std::endl;
+}
+
+void PrintCodecMapping() {
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMu, FLAG_pcmu);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMa, FLAG_pcma);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderILBC, FLAG_ilbc);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderISAC, FLAG_isac);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderISACswb, FLAG_isac_swb);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderOpus, FLAG_opus);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16B, FLAG_pcm16b);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bwb, FLAG_pcm16b_wb);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb32kHz,
+                         FLAG_pcm16b_swb32);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb48kHz,
+                         FLAG_pcm16b_swb48);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderG722, FLAG_g722);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT, FLAG_avt);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT16kHz, FLAG_avt_16);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT32kHz, FLAG_avt_32);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT48kHz, FLAG_avt_48);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderRED, FLAG_red);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGnb, FLAG_cn_nb);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGwb, FLAG_cn_wb);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb32kHz, FLAG_cn_swb32);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb48kHz, FLAG_cn_swb48);
+}
+
+absl::optional<int> CodecSampleRate(uint8_t payload_type) {
+  if (payload_type == FLAG_pcmu || payload_type == FLAG_pcma ||
+      payload_type == FLAG_ilbc || payload_type == FLAG_pcm16b ||
+      payload_type == FLAG_cn_nb || payload_type == FLAG_avt)
+    return 8000;
+  if (payload_type == FLAG_isac || payload_type == FLAG_pcm16b_wb ||
+      payload_type == FLAG_g722 || payload_type == FLAG_cn_wb ||
+      payload_type == FLAG_avt_16)
+    return 16000;
+  if (payload_type == FLAG_isac_swb || payload_type == FLAG_pcm16b_swb32 ||
+      payload_type == FLAG_cn_swb32 || payload_type == FLAG_avt_32)
+    return 32000;
+  if (payload_type == FLAG_opus || payload_type == FLAG_pcm16b_swb48 ||
+      payload_type == FLAG_cn_swb48 || payload_type == FLAG_avt_48)
+    return 48000;
+  if (payload_type == FLAG_red)
+    return 0;
+  return absl::nullopt;
+}
+
+}  // namespace
+
+// A callback class which prints whenver the inserted packet stream changes
+// the SSRC.
+class SsrcSwitchDetector : public NetEqPostInsertPacket {
+ public:
+  // Takes a pointer to another callback object, which will be invoked after
+  // this object finishes. This does not transfer ownership, and null is a
+  // valid value.
+  explicit SsrcSwitchDetector(NetEqPostInsertPacket* other_callback)
+      : other_callback_(other_callback) {}
+
+  void AfterInsertPacket(const NetEqInput::PacketData& packet,
+                         NetEq* neteq) override {
+    if (last_ssrc_ && packet.header.ssrc != *last_ssrc_) {
+      std::cout << "Changing streams from 0x" << std::hex << *last_ssrc_
+                << " to 0x" << std::hex << packet.header.ssrc << std::dec
+                << " (payload type "
+                << static_cast<int>(packet.header.payloadType) << ")"
+                << std::endl;
+    }
+    last_ssrc_ = packet.header.ssrc;
+    if (other_callback_) {
+      other_callback_->AfterInsertPacket(packet, neteq);
+    }
+  }
+
+ private:
+  NetEqPostInsertPacket* other_callback_;
+  absl::optional<uint32_t> last_ssrc_;
+};
+
+NetEqTestFactory::NetEqTestFactory() = default;
+
+NetEqTestFactory::~NetEqTestFactory() = default;
+
+std::unique_ptr<NetEqTest> NetEqTestFactory::InitializeTest(int argc,
+                                                            char* argv[]) {
+  std::string program_name = argv[0];
+  std::string usage =
+      "Tool for decoding an RTP dump file using NetEq.\n"
+      "Run " +
+      program_name +
+      " --help for usage.\n"
+      "Example usage:\n" +
+      program_name + " input.rtp output.{pcm, wav}\n";
+  if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
+    exit(1);
+  }
+  if (FLAG_help) {
+    std::cout << usage;
+    rtc::FlagList::Print(nullptr, false);
+    exit(0);
+  }
+
+  if (FLAG_codec_map) {
+    PrintCodecMapping();
+  }
+
+  if (argc != 3) {
+    if (FLAG_codec_map) {
+      // We have already printed the codec map. Just end the program.
+      exit(0);
+    }
+    // Print usage information.
+    std::cout << usage;
+    exit(0);
+  }
+
+  ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
+  ScopedFieldTrials field_trials(FLAG_force_fieldtrials);
+
+  RTC_CHECK(ValidatePayloadType(FLAG_pcmu));
+  RTC_CHECK(ValidatePayloadType(FLAG_pcma));
+  RTC_CHECK(ValidatePayloadType(FLAG_ilbc));
+  RTC_CHECK(ValidatePayloadType(FLAG_isac));
+  RTC_CHECK(ValidatePayloadType(FLAG_isac_swb));
+  RTC_CHECK(ValidatePayloadType(FLAG_opus));
+  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b));
+  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_wb));
+  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb32));
+  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb48));
+  RTC_CHECK(ValidatePayloadType(FLAG_g722));
+  RTC_CHECK(ValidatePayloadType(FLAG_avt));
+  RTC_CHECK(ValidatePayloadType(FLAG_avt_16));
+  RTC_CHECK(ValidatePayloadType(FLAG_avt_32));
+  RTC_CHECK(ValidatePayloadType(FLAG_avt_48));
+  RTC_CHECK(ValidatePayloadType(FLAG_red));
+  RTC_CHECK(ValidatePayloadType(FLAG_cn_nb));
+  RTC_CHECK(ValidatePayloadType(FLAG_cn_wb));
+  RTC_CHECK(ValidatePayloadType(FLAG_cn_swb32));
+  RTC_CHECK(ValidatePayloadType(FLAG_cn_swb48));
+  RTC_CHECK(ValidateSsrcValue(FLAG_ssrc));
+  RTC_CHECK(ValidateExtensionId(FLAG_audio_level));
+  RTC_CHECK(ValidateExtensionId(FLAG_abs_send_time));
+  RTC_CHECK(ValidateExtensionId(FLAG_transport_seq_no));
+  RTC_CHECK(ValidateExtensionId(FLAG_video_content_type));
+  RTC_CHECK(ValidateExtensionId(FLAG_video_timing));
+
+  // Gather RTP header extensions in a map.
+  NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
+      {FLAG_audio_level, kRtpExtensionAudioLevel},
+      {FLAG_abs_send_time, kRtpExtensionAbsoluteSendTime},
+      {FLAG_transport_seq_no, kRtpExtensionTransportSequenceNumber},
+      {FLAG_video_content_type, kRtpExtensionVideoContentType},
+      {FLAG_video_timing, kRtpExtensionVideoTiming}};
+
+  const std::string input_file_name = argv[1];
+  std::unique_ptr<NetEqInput> input;
+  if (RtpFileSource::ValidRtpDump(input_file_name) ||
+      RtpFileSource::ValidPcap(input_file_name)) {
+    input.reset(new NetEqRtpDumpInput(input_file_name, rtp_ext_map));
+  } else {
+    input.reset(new NetEqEventLogInput(input_file_name, rtp_ext_map));
+  }
+
+  std::cout << "Input file: " << input_file_name << std::endl;
+  RTC_CHECK(input) << "Cannot open input file";
+  RTC_CHECK(!input->ended()) << "Input file is empty";
+
+  // Check if an SSRC value was provided.
+  if (strlen(FLAG_ssrc) > 0) {
+    uint32_t ssrc;
+    RTC_CHECK(ParseSsrc(FLAG_ssrc, &ssrc)) << "Flag verification has failed.";
+    static_cast<NetEqPacketSourceInput*>(input.get())->SelectSsrc(ssrc);
+  }
+
+  // Check the sample rate.
+  absl::optional<int> sample_rate_hz;
+  std::set<std::pair<int, uint32_t>> discarded_pt_and_ssrc;
+  while (absl::optional<RTPHeader> first_rtp_header = input->NextHeader()) {
+    RTC_DCHECK(first_rtp_header);
+    sample_rate_hz = CodecSampleRate(first_rtp_header->payloadType);
+    if (sample_rate_hz) {
+      std::cout << "Found valid packet with payload type "
+                << static_cast<int>(first_rtp_header->payloadType)
+                << " and SSRC 0x" << std::hex << first_rtp_header->ssrc
+                << std::dec << std::endl;
+      break;
+    }
+    // Discard this packet and move to the next. Keep track of discarded payload
+    // types and SSRCs.
+    discarded_pt_and_ssrc.emplace(first_rtp_header->payloadType,
+                                  first_rtp_header->ssrc);
+    input->PopPacket();
+  }
+  if (!discarded_pt_and_ssrc.empty()) {
+    std::cout << "Discarded initial packets with the following payload types "
+                 "and SSRCs:"
+              << std::endl;
+    for (const auto& d : discarded_pt_and_ssrc) {
+      std::cout << "PT " << d.first << "; SSRC 0x" << std::hex
+                << static_cast<int>(d.second) << std::dec << std::endl;
+    }
+  }
+  if (!sample_rate_hz) {
+    std::cout << "Cannot find any packets with known payload types"
+              << std::endl;
+    RTC_NOTREACHED();
+  }
+
+  // Open the output file now that we know the sample rate. (Rate is only needed
+  // for wav files.)
+  const std::string output_file_name = argv[2];
+  std::unique_ptr<AudioSink> output;
+  if (output_file_name.size() >= 4 &&
+      output_file_name.substr(output_file_name.size() - 4) == ".wav") {
+    // Open a wav file.
+    output.reset(new OutputWavFile(output_file_name, *sample_rate_hz));
+  } else {
+    // Open a pcm file.
+    output.reset(new OutputAudioFile(output_file_name));
+  }
+
+  std::cout << "Output file: " << output_file_name << std::endl;
+
+  NetEqTest::DecoderMap codecs = {
+    {FLAG_pcmu, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")},
+    {FLAG_pcma, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")},
+#ifdef WEBRTC_CODEC_ILBC
+    {FLAG_ilbc, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")},
+#endif
+    {FLAG_isac, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")},
+#if !defined(WEBRTC_ANDROID)
+    {FLAG_isac_swb, std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")},
+#endif
+#ifdef WEBRTC_CODEC_OPUS
+    {FLAG_opus, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")},
+#endif
+    {FLAG_pcm16b, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")},
+    {FLAG_pcm16b_wb,
+     std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")},
+    {FLAG_pcm16b_swb32,
+     std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")},
+    {FLAG_pcm16b_swb48,
+     std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")},
+    {FLAG_g722, std::make_pair(NetEqDecoder::kDecoderG722, "g722")},
+    {FLAG_avt, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")},
+    {FLAG_avt_16, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")},
+    {FLAG_avt_32, std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")},
+    {FLAG_avt_48, std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")},
+    {FLAG_red, std::make_pair(NetEqDecoder::kDecoderRED, "red")},
+    {FLAG_cn_nb, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")},
+    {FLAG_cn_wb, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")},
+    {FLAG_cn_swb32,
+     std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")},
+    {FLAG_cn_swb48,
+     std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")}
+  };
+
+  // Check if a replacement audio file was provided.
+  if (strlen(FLAG_replacement_audio_file) > 0) {
+    // Find largest unused payload type.
+    int replacement_pt = 127;
+    while (!(codecs.find(replacement_pt) == codecs.end() &&
+             ext_codecs_.find(replacement_pt) == ext_codecs_.end())) {
+      --replacement_pt;
+      RTC_CHECK_GE(replacement_pt, 0);
+    }
+
+    auto std_set_int32_to_uint8 = [](const std::set<int32_t>& a) {
+      std::set<uint8_t> b;
+      for (auto& x : a) {
+        b.insert(static_cast<uint8_t>(x));
+      }
+      return b;
+    };
+
+    std::set<uint8_t> cn_types = std_set_int32_to_uint8(
+        {FLAG_cn_nb, FLAG_cn_wb, FLAG_cn_swb32, FLAG_cn_swb48});
+    std::set<uint8_t> forbidden_types = std_set_int32_to_uint8(
+        {FLAG_g722, FLAG_red, FLAG_avt, FLAG_avt_16, FLAG_avt_32, FLAG_avt_48});
+    input.reset(new NetEqReplacementInput(std::move(input), replacement_pt,
+                                          cn_types, forbidden_types));
+
+    replacement_decoder_.reset(new FakeDecodeFromFile(
+        std::unique_ptr<InputAudioFile>(
+            new InputAudioFile(FLAG_replacement_audio_file)),
+        48000, false));
+    NetEqTest::ExternalDecoderInfo ext_dec_info = {
+        replacement_decoder_.get(), NetEqDecoder::kDecoderArbitrary,
+        "replacement codec"};
+    ext_codecs_[replacement_pt] = ext_dec_info;
+  }
+
+  NetEqTest::Callbacks callbacks;
+  stats_plotter_.reset(new NetEqStatsPlotter(FLAG_matlabplot, FLAG_pythonplot,
+                                             FLAG_concealment_events,
+                                             output_file_name));
+
+  ssrc_switch_detector_.reset(
+      new SsrcSwitchDetector(stats_plotter_->stats_getter()->delay_analyzer()));
+  callbacks.post_insert_packet = ssrc_switch_detector_.get();
+  callbacks.get_audio_callback = stats_plotter_->stats_getter();
+  callbacks.simulation_ended_callback = stats_plotter_.get();
+  NetEq::Config config;
+  config.sample_rate_hz = *sample_rate_hz;
+  return absl::make_unique<NetEqTest>(config, codecs, ext_codecs_,
+                                      std::move(input), std::move(output),
+                                      callbacks);
+}
+
+}  // namespace test
+}  // namespace webrtc
diff --git a/modules/audio_coding/neteq/tools/neteq_test_factory.h b/modules/audio_coding/neteq/tools/neteq_test_factory.h
new file mode 100644
index 0000000..210c3e3
--- /dev/null
+++ b/modules/audio_coding/neteq/tools/neteq_test_factory.h
@@ -0,0 +1,43 @@
+/*
+ *  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 MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_FACTORY_H_
+#define MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_FACTORY_H_
+
+#include <memory>
+
+#include "modules/audio_coding/neteq/tools/neteq_test.h"
+
+namespace webrtc {
+namespace test {
+
+class SsrcSwitchDetector;
+class NetEqStatsGetter;
+class NetEqStatsPlotter;
+
+// Note that the NetEqTestFactory needs to be alive when the NetEqTest object is
+// used for a simulation.
+class NetEqTestFactory {
+ public:
+  NetEqTestFactory();
+  ~NetEqTestFactory();
+  std::unique_ptr<NetEqTest> InitializeTest(int argc, char* argv[]);
+
+ private:
+  std::unique_ptr<AudioDecoder> replacement_decoder_;
+  NetEqTest::ExtDecoderMap ext_codecs_;
+  std::unique_ptr<SsrcSwitchDetector> ssrc_switch_detector_;
+  std::unique_ptr<NetEqStatsPlotter> stats_plotter_;
+};
+
+}  // namespace test
+}  // namespace webrtc
+
+#endif  // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_FACTORY_H_