Add packet rate plots to event_log_visualizer.

Bug: b/152399961
Change-Id: I8dbc0166ed537c197f26a80275100fb3faa338f5
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/172094
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Commit-Queue: Kristoffer Erlandsson <kerl@google.com>
Cr-Commit-Position: refs/heads/master@{#30942}
diff --git a/rtc_tools/rtc_event_log_visualizer/analyzer.cc b/rtc_tools/rtc_event_log_visualizer/analyzer.cc
index 6a43388..eaf28bf 100644
--- a/rtc_tools/rtc_event_log_visualizer/analyzer.cc
+++ b/rtc_tools/rtc_event_log_visualizer/analyzer.cc
@@ -617,6 +617,43 @@
                  " RTP/RTCP packets");
 }
 
+void EventLogAnalyzer::CreatePacketRateGraph(PacketDirection direction,
+                                             Plot* plot) {
+  auto CountPackets = [](auto packet) { return 1.0; };
+  for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
+    // Filter on SSRC.
+    if (!MatchingSsrc(stream.ssrc, desired_ssrc_)) {
+      continue;
+    }
+    TimeSeries time_series(
+        std::string("RTP ") + GetStreamName(direction, stream.ssrc),
+        LineStyle::kLine);
+    MovingAverage<LoggedRtpPacket, double>(CountPackets, stream.packet_view,
+                                           config_, &time_series);
+    plot->AppendTimeSeries(std::move(time_series));
+  }
+  TimeSeries time_series(
+      std::string("RTCP ") + "(" + GetDirectionAsShortString(direction) + ")",
+      LineStyle::kLine);
+  if (direction == kIncomingPacket) {
+    MovingAverage<LoggedRtcpPacketIncoming, double>(
+        CountPackets, parsed_log_.incoming_rtcp_packets(), config_,
+        &time_series);
+  } else {
+    MovingAverage<LoggedRtcpPacketOutgoing, double>(
+        CountPackets, parsed_log_.outgoing_rtcp_packets(), 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 " + 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 c4f7220..e59e7b4 100644
--- a/rtc_tools/rtc_event_log_visualizer/analyzer.h
+++ b/rtc_tools/rtc_event_log_visualizer/analyzer.h
@@ -63,6 +63,8 @@
 
   void CreateAccumulatedPacketsGraph(PacketDirection direction, Plot* plot);
 
+  void CreatePacketRateGraph(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 cac0cb3..6231068 100644
--- a/rtc_tools/rtc_event_log_visualizer/main.cc
+++ b/rtc_tools/rtc_event_log_visualizer/main.cc
@@ -291,6 +291,12 @@
   plots.RegisterPlot("outgoing_packet_count", [&](Plot* plot) {
     analyzer.CreateAccumulatedPacketsGraph(webrtc::kOutgoingPacket, plot);
   });
+  plots.RegisterPlot("incoming_packet_rate", [&](Plot* plot) {
+    analyzer.CreatePacketRateGraph(webrtc::kIncomingPacket, plot);
+  });
+  plots.RegisterPlot("outgoing_packet_rate", [&](Plot* plot) {
+    analyzer.CreatePacketRateGraph(webrtc::kOutgoingPacket, plot);
+  });
   plots.RegisterPlot("audio_playout",
                      [&](Plot* plot) { analyzer.CreatePlayoutGraph(plot); });
   plots.RegisterPlot("incoming_audio_level", [&](Plot* plot) {