Return nullptr instead of crashing in NetEqTestFactory

Currently the code in NetEqTestFactory will crash when something
unexpected happens. It would be better to return a nullptr instead and
let the caller decide how to proceed.

Bug: webrtc:10337
Change-Id: I3cfdffa7e6f2016eeaa5d6e80c5dd6c954ef8485
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/127894
Reviewed-by: Pablo Barrera González <barrerap@webrtc.org>
Commit-Queue: Ivo Creusen <ivoc@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27226}
diff --git a/modules/audio_coding/neteq/tools/neteq_rtpplay.cc b/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
index 1d313f5..2b89597 100644
--- a/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
+++ b/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
@@ -379,6 +379,7 @@
 
   std::unique_ptr<webrtc::test::NetEqTest> test =
       factory.InitializeTestFromFile(/*input_filename=*/argv[1], config);
+  RTC_CHECK(test) << "ERROR: Unable to run test";
   test->Run();
   return 0;
 }
diff --git a/modules/audio_coding/neteq/tools/neteq_test_factory.cc b/modules/audio_coding/neteq/tools/neteq_test_factory.cc
index 832d678..9b68c5c 100644
--- a/modules/audio_coding/neteq/tools/neteq_test_factory.cc
+++ b/modules/audio_coding/neteq/tools/neteq_test_factory.cc
@@ -113,7 +113,10 @@
     const Config& config) {
   std::unique_ptr<NetEqInput> input(
       NetEqEventLogInput::CreateFromString(input_string, config.ssrc_filter));
-  RTC_CHECK(input) << "Cannot parse input string";
+  if (!input) {
+    std::cerr << "Error: Cannot parse input string" << std::endl;
+    return nullptr;
+  }
   return InitializeTest(std::move(input), config);
 }
 
@@ -139,14 +142,20 @@
   }
 
   std::cout << "Input file: " << input_file_name << std::endl;
-  RTC_CHECK(input) << "Cannot open input file";
+  if (!input) {
+    std::cerr << "Error: Cannot open input file" << std::endl;
+    return nullptr;
+  }
   return InitializeTest(std::move(input), config);
 }
 
 std::unique_ptr<NetEqTest> NetEqTestFactory::InitializeTest(
     std::unique_ptr<NetEqInput> input,
     const Config& config) {
-  RTC_CHECK(!input->ended()) << "Input is empty";
+  if (input->ended()) {
+    std::cerr << "Error: Input is empty" << std::endl;
+    return nullptr;
+  }
 
   // Check the sample rate.
   absl::optional<int> sample_rate_hz;
@@ -177,9 +186,9 @@
     }
   }
   if (!sample_rate_hz) {
-    std::cout << "Cannot find any packets with known payload types"
+    std::cerr << "Cannot find any packets with known payload types"
               << std::endl;
-    RTC_NOTREACHED();
+    return nullptr;
   }
 
   // If an output file is requested, open it.
@@ -213,7 +222,11 @@
     int replacement_pt = 127;
     while (codecs.find(replacement_pt) != codecs.end()) {
       --replacement_pt;
-      RTC_CHECK_GE(replacement_pt, 0);
+      if (replacement_pt <= 0) {
+        std::cerr << "Error: Unable to find available replacement payload type"
+                  << std::endl;
+        return nullptr;
+      }
     }
 
     auto std_set_int32_to_uint8 = [](const std::set<int32_t>& a) {
@@ -249,9 +262,13 @@
           return decoder;
         });
 
-    RTC_CHECK(
-        codecs.insert({replacement_pt, SdpAudioFormat("replacement", 48000, 1)})
-            .second);
+    if (!codecs
+             .insert({replacement_pt, SdpAudioFormat("replacement", 48000, 1)})
+             .second) {
+      std::cerr << "Error: Unable to insert replacement audio codec"
+                << std::endl;
+      return nullptr;
+    }
   }
 
   // Create a text log file if needed.
@@ -265,7 +282,6 @@
       config.matlabplot, config.pythonplot, config.concealment_events,
       config.plot_scripts_basename.value_or(""));
 
-  RTC_CHECK(stats_plotter_);
   ssrc_switch_detector_.reset(
       new SsrcSwitchDetector(stats_plotter_->stats_getter()->delay_analyzer()));
   callbacks.post_insert_packet = ssrc_switch_detector_.get();