Total packet rate plots for event_log_visualizer.

Bug: b/152399961
Change-Id: I9fcd2e234f229cefc972149ab22ccd845a8e90ba
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/172440
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Commit-Queue: Kristoffer Erlandsson <kerl@google.com>
Cr-Commit-Position: refs/heads/master@{#30963}
diff --git a/rtc_tools/rtc_event_log_visualizer/analyzer.cc b/rtc_tools/rtc_event_log_visualizer/analyzer.cc
index eaf28bf..9fcb510 100644
--- a/rtc_tools/rtc_event_log_visualizer/analyzer.cc
+++ b/rtc_tools/rtc_event_log_visualizer/analyzer.cc
@@ -28,6 +28,7 @@
 #include "call/call.h"
 #include "call/video_receive_stream.h"
 #include "call/video_send_stream.h"
+#include "logging/rtc_event_log/rtc_event_processor.h"
 #include "logging/rtc_event_log/rtc_stream_config.h"
 #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor.h"
 #include "modules/audio_coding/neteq/tools/audio_sink.h"
@@ -654,6 +655,57 @@
                  " RTP/RTCP packets");
 }
 
+void EventLogAnalyzer::CreateTotalPacketRateGraph(PacketDirection direction,
+                                                  Plot* plot) {
+  // Contains a log timestamp to enable counting logged events of different
+  // types using MovingAverage().
+  class LogTime {
+   public:
+    explicit LogTime(int64_t log_time_us) : log_time_us_(log_time_us) {}
+
+    int64_t log_time_us() const { return log_time_us_; }
+
+   private:
+    int64_t log_time_us_;
+  };
+
+  std::vector<LogTime> packet_times;
+  auto handle_rtp = [&](const LoggedRtpPacket& packet) {
+    packet_times.emplace_back(packet.log_time_us());
+  };
+  RtcEventProcessor process;
+  for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
+    process.AddEvents(stream.packet_view, handle_rtp);
+  }
+  if (direction == kIncomingPacket) {
+    auto handle_incoming_rtcp = [&](const LoggedRtcpPacketIncoming& packet) {
+      packet_times.emplace_back(packet.log_time_us());
+    };
+    process.AddEvents(parsed_log_.incoming_rtcp_packets(),
+                      handle_incoming_rtcp);
+  } else {
+    auto handle_outgoing_rtcp = [&](const LoggedRtcpPacketOutgoing& packet) {
+      packet_times.emplace_back(packet.log_time_us());
+    };
+    process.AddEvents(parsed_log_.outgoing_rtcp_packets(),
+                      handle_outgoing_rtcp);
+  }
+  process.ProcessEventsInOrder();
+  TimeSeries time_series(std::string("Total ") + "(" +
+                             GetDirectionAsShortString(direction) + ") packets",
+                         LineStyle::kLine);
+  MovingAverage<LogTime, uint64_t>([](auto packet) { return 1; }, packet_times,
+                                   config_, &time_series);
+  plot->AppendTimeSeries(std::move(time_series));
+
+  plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
+                 "Time (s)", kLeftMargin, kRightMargin);
+  plot->SetSuggestedYAxis(0, 1, "Packet Rate (packets/s)", kBottomMargin,
+                          kTopMargin);
+  plot->SetTitle("Rate of all " + GetDirectionAsString(direction) +
+                 " RTP/RTCP packets");
+}
+
 // For each SSRC, plot the time between the consecutive playouts.
 void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
   for (const auto& playout_stream : parsed_log_.audio_playout_events()) {
diff --git a/rtc_tools/rtc_event_log_visualizer/analyzer.h b/rtc_tools/rtc_event_log_visualizer/analyzer.h
index e59e7b4..1e09109 100644
--- a/rtc_tools/rtc_event_log_visualizer/analyzer.h
+++ b/rtc_tools/rtc_event_log_visualizer/analyzer.h
@@ -65,6 +65,8 @@
 
   void CreatePacketRateGraph(PacketDirection direction, Plot* plot);
 
+  void CreateTotalPacketRateGraph(PacketDirection direction, Plot* plot);
+
   void CreatePlayoutGraph(Plot* plot);
 
   void CreateAudioLevelGraph(PacketDirection direction, Plot* plot);
diff --git a/rtc_tools/rtc_event_log_visualizer/main.cc b/rtc_tools/rtc_event_log_visualizer/main.cc
index 6231068..eb36b26 100644
--- a/rtc_tools/rtc_event_log_visualizer/main.cc
+++ b/rtc_tools/rtc_event_log_visualizer/main.cc
@@ -297,6 +297,12 @@
   plots.RegisterPlot("outgoing_packet_rate", [&](Plot* plot) {
     analyzer.CreatePacketRateGraph(webrtc::kOutgoingPacket, plot);
   });
+  plots.RegisterPlot("total_incoming_packet_rate", [&](Plot* plot) {
+    analyzer.CreateTotalPacketRateGraph(webrtc::kIncomingPacket, plot);
+  });
+  plots.RegisterPlot("total_outgoing_packet_rate", [&](Plot* plot) {
+    analyzer.CreateTotalPacketRateGraph(webrtc::kOutgoingPacket, plot);
+  });
   plots.RegisterPlot("audio_playout",
                      [&](Plot* plot) { analyzer.CreatePlayoutGraph(plot); });
   plots.RegisterPlot("incoming_audio_level", [&](Plot* plot) {