Split NetEq simulation and jitter buffer plot to be able to plot other metrics in the simulation.

Bug: webrtc:9147
Change-Id: Ied37dedd19fc24a48700fb01645cee6288d3efa7
Reviewed-on: https://webrtc-review.googlesource.com/70160
Commit-Queue: Minyue Li <minyue@webrtc.org>
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23125}
diff --git a/rtc_tools/event_log_visualizer/analyzer.cc b/rtc_tools/event_log_visualizer/analyzer.cc
index 89cf890..efb14eb 100644
--- a/rtc_tools/event_log_visualizer/analyzer.cc
+++ b/rtc_tools/event_log_visualizer/analyzer.cc
@@ -1640,7 +1640,7 @@
 // Creates a NetEq test object and all necessary input and output helpers. Runs
 // the test and returns the NetEqDelayAnalyzer object that was used to
 // instrument the test.
-std::unique_ptr<test::NetEqDelayAnalyzer> CreateNetEqTestAndRun(
+std::unique_ptr<test::NetEqStatsGetter> CreateNetEqTestAndRun(
     const std::vector<LoggedRtpPacketIncoming>* packet_stream,
     const std::vector<int64_t>* output_events_us,
     rtc::Optional<int64_t> end_time_us,
@@ -1678,26 +1678,26 @@
 
   std::unique_ptr<test::NetEqDelayAnalyzer> delay_cb(
       new test::NetEqDelayAnalyzer);
+  std::unique_ptr<test::NetEqStatsGetter> neteq_stats_getter(
+      new test::NetEqStatsGetter(std::move(delay_cb)));
   test::DefaultNetEqTestErrorCallback error_cb;
   test::NetEqTest::Callbacks callbacks;
   callbacks.error_callback = &error_cb;
-  callbacks.post_insert_packet = delay_cb.get();
-  callbacks.get_audio_callback = delay_cb.get();
+  callbacks.post_insert_packet = neteq_stats_getter->delay_analyzer();
+  callbacks.get_audio_callback = neteq_stats_getter.get();
 
   test::NetEqTest test(config, codecs, ext_codecs, std::move(input),
                        std::move(output), callbacks);
   test.Run();
-  return delay_cb;
+  return neteq_stats_getter;
 }
 }  // namespace
 
-// Plots the jitter buffer delay profile. This will plot only for the first
-// incoming audio SSRC. If the stream contains more than one incoming audio
-// SSRC, all but the first will be ignored.
-void EventLogAnalyzer::CreateAudioJitterBufferGraph(
+EventLogAnalyzer::NetEqStatsGetterMap EventLogAnalyzer::SimulateNetEq(
     const std::string& replacement_file_name,
-    int file_sample_rate_hz,
-    Plot* plot) {
+    int file_sample_rate_hz) const {
+  NetEqStatsGetterMap neteq_stats;
+
   const std::vector<LoggedRtpPacketIncoming>* audio_packets = nullptr;
   uint32_t ssrc;
   for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
@@ -1709,7 +1709,7 @@
   }
   if (audio_packets == nullptr) {
     // No incoming audio stream found.
-    return;
+    return neteq_stats;
   }
 
   std::map<uint32_t, std::vector<int64_t>>::const_iterator output_events_it =
@@ -1725,18 +1725,32 @@
           ? rtc::nullopt
           : rtc::Optional<int64_t>(log_segments_.front().second);
 
-  auto delay_cb = CreateNetEqTestAndRun(
+  neteq_stats[ssrc] = CreateNetEqTestAndRun(
       audio_packets, &output_events_it->second, end_time_us,
       replacement_file_name, file_sample_rate_hz);
 
+  return neteq_stats;
+}
+
+// Plots the jitter buffer delay profile. This will plot only for the first
+// incoming audio SSRC. If the stream contains more than one incoming audio
+// SSRC, all but the first will be ignored.
+void EventLogAnalyzer::CreateAudioJitterBufferGraph(
+    const NetEqStatsGetterMap& neteq_stats,
+    Plot* plot) const {
+  if (neteq_stats.size() < 1)
+    return;
+
+  const uint32_t ssrc = neteq_stats.begin()->first;
+
   std::vector<float> send_times_s;
   std::vector<float> arrival_delay_ms;
   std::vector<float> corrected_arrival_delay_ms;
   std::vector<rtc::Optional<float>> playout_delay_ms;
   std::vector<rtc::Optional<float>> target_delay_ms;
-  delay_cb->CreateGraphs(&send_times_s, &arrival_delay_ms,
-                         &corrected_arrival_delay_ms, &playout_delay_ms,
-                         &target_delay_ms);
+  neteq_stats.at(ssrc)->delay_analyzer()->CreateGraphs(
+      &send_times_s, &arrival_delay_ms, &corrected_arrival_delay_ms,
+      &playout_delay_ms, &target_delay_ms);
   RTC_DCHECK_EQ(send_times_s.size(), arrival_delay_ms.size());
   RTC_DCHECK_EQ(send_times_s.size(), corrected_arrival_delay_ms.size());
   RTC_DCHECK_EQ(send_times_s.size(), playout_delay_ms.size());
diff --git a/rtc_tools/event_log_visualizer/analyzer.h b/rtc_tools/event_log_visualizer/analyzer.h
index 7ed65a7..572884c 100644
--- a/rtc_tools/event_log_visualizer/analyzer.h
+++ b/rtc_tools/event_log_visualizer/analyzer.h
@@ -19,6 +19,7 @@
 #include <vector>
 
 #include "logging/rtc_event_log/rtc_event_log_parser_new.h"
+#include "modules/audio_coding/neteq/tools/neteq_stats_getter.h"
 #include "rtc_base/strings/string_builder.h"
 #include "rtc_tools/event_log_visualizer/plot_base.h"
 #include "rtc_tools/event_log_visualizer/triage_notifications.h"
@@ -70,9 +71,14 @@
   void CreateAudioEncoderEnableFecGraph(Plot* plot);
   void CreateAudioEncoderEnableDtxGraph(Plot* plot);
   void CreateAudioEncoderNumChannelsGraph(Plot* plot);
-  void CreateAudioJitterBufferGraph(const std::string& replacement_file_name,
-                                    int file_sample_rate_hz,
-                                    Plot* plot);
+
+  using NetEqStatsGetterMap =
+      std::map<uint32_t, std::unique_ptr<test::NetEqStatsGetter>>;
+  NetEqStatsGetterMap SimulateNetEq(const std::string& replacement_file_name,
+                                    int file_sample_rate_hz) const;
+  void CreateAudioJitterBufferGraph(
+      const NetEqStatsGetterMap& neteq_stats_getters,
+      Plot* plot) const;
 
   void CreateIceCandidatePairConfigGraph(Plot* plot);
   void CreateIceConnectivityCheckGraph(Plot* plot);
diff --git a/rtc_tools/event_log_visualizer/main.cc b/rtc_tools/event_log_visualizer/main.cc
index 4d752fd..d12935d 100644
--- a/rtc_tools/event_log_visualizer/main.cc
+++ b/rtc_tools/event_log_visualizer/main.cc
@@ -333,7 +333,8 @@
       wav_path = webrtc::test::ResourcePath(
           "audio_processing/conversational_speech/EN_script2_F_sp2_B1", "wav");
     }
-    analyzer.CreateAudioJitterBufferGraph(wav_path, 48000,
+    auto neteq_stats = analyzer.SimulateNetEq(wav_path, 48000);
+    analyzer.CreateAudioJitterBufferGraph(neteq_stats,
                                           collection->AppendNewPlot());
   }