Change rtp_event_log2text to ignore webrtc::MediaType from proto.

BUG=webrtc:7538

Review-Url: https://codereview.webrtc.org/2894833003
Cr-Original-Commit-Position: refs/heads/master@{#18210}
Cr-Mirrored-From: https://chromium.googlesource.com/external/webrtc
Cr-Mirrored-Commit: c52bd61f65d04b364e90a672691963d089ca5141
diff --git a/logging/rtc_event_log/rtc_event_log2text.cc b/logging/rtc_event_log/rtc_event_log2text.cc
index b2d537e..1fa694d 100644
--- a/logging/rtc_event_log/rtc_event_log2text.cc
+++ b/logging/rtc_event_log/rtc_event_log2text.cc
@@ -73,6 +73,32 @@
   return str.empty() || (!ss.fail() && ss.eof());
 }
 
+// Struct used for storing SSRCs used in a Stream.
+struct Stream {
+  Stream(uint32_t ssrc,
+         webrtc::MediaType media_type,
+         webrtc::PacketDirection direction)
+      : ssrc(ssrc), media_type(media_type), direction(direction) {}
+  uint32_t ssrc;
+  webrtc::MediaType media_type;
+  webrtc::PacketDirection direction;
+};
+
+// All configured streams found in the event log.
+std::vector<Stream> global_streams;
+
+// Returns the MediaType for registered SSRCs. Search from the end to use last
+// registered types first.
+webrtc::MediaType GetMediaType(uint32_t ssrc,
+                               webrtc::PacketDirection direction) {
+  for (auto rit = global_streams.rbegin(); rit != global_streams.rend();
+       ++rit) {
+    if (rit->ssrc == ssrc && rit->direction == direction)
+      return rit->media_type;
+  }
+  return webrtc::MediaType::ANY;
+}
+
 bool ExcludePacket(webrtc::PacketDirection direction,
                    webrtc::MediaType media_type,
                    uint32_t packet_ssrc) {
@@ -118,11 +144,11 @@
 
 void PrintSenderReport(const webrtc::rtcp::CommonHeader& rtcp_block,
                        uint64_t log_timestamp,
-                       webrtc::PacketDirection direction,
-                       webrtc::MediaType media_type) {
+                       webrtc::PacketDirection direction) {
   webrtc::rtcp::SenderReport sr;
   if (!sr.Parse(rtcp_block))
     return;
+  webrtc::MediaType media_type = GetMediaType(sr.sender_ssrc(), direction);
   if (ExcludePacket(direction, media_type, sr.sender_ssrc()))
     return;
   std::cout << log_timestamp << "\t"
@@ -133,11 +159,11 @@
 
 void PrintReceiverReport(const webrtc::rtcp::CommonHeader& rtcp_block,
                          uint64_t log_timestamp,
-                         webrtc::PacketDirection direction,
-                         webrtc::MediaType media_type) {
+                         webrtc::PacketDirection direction) {
   webrtc::rtcp::ReceiverReport rr;
   if (!rr.Parse(rtcp_block))
     return;
+  webrtc::MediaType media_type = GetMediaType(rr.sender_ssrc(), direction);
   if (ExcludePacket(direction, media_type, rr.sender_ssrc()))
     return;
   std::cout << log_timestamp << "\t"
@@ -147,11 +173,11 @@
 
 void PrintXr(const webrtc::rtcp::CommonHeader& rtcp_block,
              uint64_t log_timestamp,
-             webrtc::PacketDirection direction,
-             webrtc::MediaType media_type) {
+             webrtc::PacketDirection direction) {
   webrtc::rtcp::ExtendedReports xr;
   if (!xr.Parse(rtcp_block))
     return;
+  webrtc::MediaType media_type = GetMediaType(xr.sender_ssrc(), direction);
   if (ExcludePacket(direction, media_type, xr.sender_ssrc()))
     return;
   std::cout << log_timestamp << "\t"
@@ -161,20 +187,20 @@
 
 void PrintSdes(const webrtc::rtcp::CommonHeader& rtcp_block,
                uint64_t log_timestamp,
-               webrtc::PacketDirection direction,
-               webrtc::MediaType media_type) {
+               webrtc::PacketDirection direction) {
   std::cout << log_timestamp << "\t"
-            << "RTCP_SDES" << StreamInfo(direction, media_type) << std::endl;
+            << "RTCP_SDES" << StreamInfo(direction, webrtc::MediaType::ANY)
+            << std::endl;
   RTC_NOTREACHED() << "SDES should have been redacted when writing the log";
 }
 
 void PrintBye(const webrtc::rtcp::CommonHeader& rtcp_block,
               uint64_t log_timestamp,
-              webrtc::PacketDirection direction,
-              webrtc::MediaType media_type) {
+              webrtc::PacketDirection direction) {
   webrtc::rtcp::Bye bye;
   if (!bye.Parse(rtcp_block))
     return;
+  webrtc::MediaType media_type = GetMediaType(bye.sender_ssrc(), direction);
   if (ExcludePacket(direction, media_type, bye.sender_ssrc()))
     return;
   std::cout << log_timestamp << "\t"
@@ -184,13 +210,14 @@
 
 void PrintRtpFeedback(const webrtc::rtcp::CommonHeader& rtcp_block,
                       uint64_t log_timestamp,
-                      webrtc::PacketDirection direction,
-                      webrtc::MediaType media_type) {
+                      webrtc::PacketDirection direction) {
   switch (rtcp_block.fmt()) {
     case webrtc::rtcp::Nack::kFeedbackMessageType: {
       webrtc::rtcp::Nack nack;
       if (!nack.Parse(rtcp_block))
         return;
+      webrtc::MediaType media_type =
+          GetMediaType(nack.sender_ssrc(), direction);
       if (ExcludePacket(direction, media_type, nack.sender_ssrc()))
         return;
       std::cout << log_timestamp << "\t"
@@ -202,6 +229,8 @@
       webrtc::rtcp::Tmmbr tmmbr;
       if (!tmmbr.Parse(rtcp_block))
         return;
+      webrtc::MediaType media_type =
+          GetMediaType(tmmbr.sender_ssrc(), direction);
       if (ExcludePacket(direction, media_type, tmmbr.sender_ssrc()))
         return;
       std::cout << log_timestamp << "\t"
@@ -213,6 +242,8 @@
       webrtc::rtcp::Tmmbn tmmbn;
       if (!tmmbn.Parse(rtcp_block))
         return;
+      webrtc::MediaType media_type =
+          GetMediaType(tmmbn.sender_ssrc(), direction);
       if (ExcludePacket(direction, media_type, tmmbn.sender_ssrc()))
         return;
       std::cout << log_timestamp << "\t"
@@ -224,6 +255,8 @@
       webrtc::rtcp::RapidResyncRequest sr_req;
       if (!sr_req.Parse(rtcp_block))
         return;
+      webrtc::MediaType media_type =
+          GetMediaType(sr_req.sender_ssrc(), direction);
       if (ExcludePacket(direction, media_type, sr_req.sender_ssrc()))
         return;
       std::cout << log_timestamp << "\t"
@@ -235,6 +268,8 @@
       webrtc::rtcp::TransportFeedback transport_feedback;
       if (!transport_feedback.Parse(rtcp_block))
         return;
+      webrtc::MediaType media_type =
+          GetMediaType(transport_feedback.sender_ssrc(), direction);
       if (ExcludePacket(direction, media_type,
                         transport_feedback.sender_ssrc()))
         return;
@@ -250,13 +285,13 @@
 
 void PrintPsFeedback(const webrtc::rtcp::CommonHeader& rtcp_block,
                      uint64_t log_timestamp,
-                     webrtc::PacketDirection direction,
-                     webrtc::MediaType media_type) {
+                     webrtc::PacketDirection direction) {
   switch (rtcp_block.fmt()) {
     case webrtc::rtcp::Pli::kFeedbackMessageType: {
       webrtc::rtcp::Pli pli;
       if (!pli.Parse(rtcp_block))
         return;
+      webrtc::MediaType media_type = GetMediaType(pli.sender_ssrc(), direction);
       if (ExcludePacket(direction, media_type, pli.sender_ssrc()))
         return;
       std::cout << log_timestamp << "\t"
@@ -268,6 +303,7 @@
       webrtc::rtcp::Fir fir;
       if (!fir.Parse(rtcp_block))
         return;
+      webrtc::MediaType media_type = GetMediaType(fir.sender_ssrc(), direction);
       if (ExcludePacket(direction, media_type, fir.sender_ssrc()))
         return;
       std::cout << log_timestamp << "\t"
@@ -279,6 +315,8 @@
       webrtc::rtcp::Remb remb;
       if (!remb.Parse(rtcp_block))
         return;
+      webrtc::MediaType media_type =
+          GetMediaType(remb.sender_ssrc(), direction);
       if (ExcludePacket(direction, media_type, remb.sender_ssrc()))
         return;
       std::cout << log_timestamp << "\t"
@@ -324,45 +362,75 @@
   }
 
   for (size_t i = 0; i < parsed_stream.GetNumberOfEvents(); i++) {
-    if (!FLAGS_noconfig && !FLAGS_novideo && !FLAGS_noincoming &&
-        parsed_stream.GetEventType(i) ==
-            webrtc::ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT) {
+    if (parsed_stream.GetEventType(i) ==
+        webrtc::ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT) {
       webrtc::VideoReceiveStream::Config config(nullptr);
       parsed_stream.GetVideoReceiveConfig(i, &config);
-      std::cout << parsed_stream.GetTimestamp(i) << "\tVIDEO_RECV_CONFIG"
-                << "\tssrc=" << config.rtp.remote_ssrc
-                << "\tfeedback_ssrc=" << config.rtp.local_ssrc << std::endl;
+
+      global_streams.emplace_back(config.rtp.remote_ssrc,
+                                  webrtc::MediaType::VIDEO,
+                                  webrtc::kIncomingPacket);
+      global_streams.emplace_back(config.rtp.local_ssrc,
+                                  webrtc::MediaType::VIDEO,
+                                  webrtc::kOutgoingPacket);
+
+      if (!FLAGS_noconfig && !FLAGS_novideo && !FLAGS_noincoming) {
+        std::cout << parsed_stream.GetTimestamp(i) << "\tVIDEO_RECV_CONFIG"
+                  << "\tssrc=" << config.rtp.remote_ssrc
+                  << "\tfeedback_ssrc=" << config.rtp.local_ssrc << std::endl;
+      }
     }
-    if (!FLAGS_noconfig && !FLAGS_novideo && !FLAGS_nooutgoing &&
-        parsed_stream.GetEventType(i) ==
-            webrtc::ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT) {
+    if (parsed_stream.GetEventType(i) ==
+        webrtc::ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT) {
       webrtc::VideoSendStream::Config config(nullptr);
       parsed_stream.GetVideoSendConfig(i, &config);
-      std::cout << parsed_stream.GetTimestamp(i) << "\tVIDEO_SEND_CONFIG";
-      std::cout << "\tssrcs=";
-      for (const auto& ssrc : config.rtp.ssrcs)
-        std::cout << ssrc << ',';
-      std::cout << "\trtx_ssrcs=";
-      for (const auto& ssrc : config.rtp.rtx.ssrcs)
-        std::cout << ssrc << ',';
-      std::cout << std::endl;
+
+      for (uint32_t ssrc : config.rtp.ssrcs) {
+        global_streams.emplace_back(ssrc, webrtc::MediaType::VIDEO,
+                                    webrtc::kOutgoingPacket);
+      }
+      for (uint32_t ssrc : config.rtp.rtx.ssrcs) {
+        global_streams.emplace_back(ssrc, webrtc::MediaType::VIDEO,
+                                    webrtc::kOutgoingPacket);
+      }
+
+      if (!FLAGS_noconfig && !FLAGS_novideo && !FLAGS_nooutgoing) {
+        std::cout << parsed_stream.GetTimestamp(i) << "\tVIDEO_SEND_CONFIG";
+        std::cout << "\tssrcs=";
+        for (const auto& ssrc : config.rtp.ssrcs)
+          std::cout << ssrc << ',';
+        std::cout << "\trtx_ssrcs=";
+        for (const auto& ssrc : config.rtp.rtx.ssrcs)
+          std::cout << ssrc << ',';
+        std::cout << std::endl;
+      }
     }
-    if (!FLAGS_noconfig && !FLAGS_noaudio && !FLAGS_noincoming &&
-        parsed_stream.GetEventType(i) ==
-            webrtc::ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT) {
+    if (parsed_stream.GetEventType(i) ==
+        webrtc::ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT) {
       webrtc::AudioReceiveStream::Config config;
       parsed_stream.GetAudioReceiveConfig(i, &config);
-      std::cout << parsed_stream.GetTimestamp(i) << "\tAUDIO_RECV_CONFIG"
-                << "\tssrc=" << config.rtp.remote_ssrc
-                << "\tfeedback_ssrc=" << config.rtp.local_ssrc << std::endl;
+      global_streams.emplace_back(config.rtp.remote_ssrc,
+                                  webrtc::MediaType::AUDIO,
+                                  webrtc::kIncomingPacket);
+      global_streams.emplace_back(config.rtp.local_ssrc,
+                                  webrtc::MediaType::AUDIO,
+                                  webrtc::kOutgoingPacket);
+      if (!FLAGS_noconfig && !FLAGS_noaudio && !FLAGS_noincoming) {
+        std::cout << parsed_stream.GetTimestamp(i) << "\tAUDIO_RECV_CONFIG"
+                  << "\tssrc=" << config.rtp.remote_ssrc
+                  << "\tfeedback_ssrc=" << config.rtp.local_ssrc << std::endl;
+      }
     }
-    if (!FLAGS_noconfig && !FLAGS_noaudio && !FLAGS_nooutgoing &&
-        parsed_stream.GetEventType(i) ==
-            webrtc::ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT) {
+    if (parsed_stream.GetEventType(i) ==
+        webrtc::ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT) {
       webrtc::AudioSendStream::Config config(nullptr);
       parsed_stream.GetAudioSendConfig(i, &config);
-      std::cout << parsed_stream.GetTimestamp(i) << "\tAUDIO_SEND_CONFIG"
-                << "\tssrc=" << config.rtp.ssrc << std::endl;
+      global_streams.emplace_back(config.rtp.ssrc, webrtc::MediaType::AUDIO,
+                                  webrtc::kOutgoingPacket);
+      if (!FLAGS_noconfig && !FLAGS_noaudio && !FLAGS_nooutgoing) {
+        std::cout << parsed_stream.GetTimestamp(i) << "\tAUDIO_SEND_CONFIG"
+                  << "\tssrc=" << config.rtp.ssrc << std::endl;
+      }
     }
     if (!FLAGS_nortp &&
         parsed_stream.GetEventType(i) == webrtc::ParsedRtcEventLog::RTP_EVENT) {
@@ -378,6 +446,7 @@
       webrtc::RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
       webrtc::RTPHeader parsed_header;
       rtp_parser.Parse(&parsed_header);
+      media_type = GetMediaType(parsed_header.ssrc, direction);
 
       if (ExcludePacket(direction, media_type, parsed_header.ssrc))
         continue;
@@ -409,26 +478,25 @@
         uint64_t log_timestamp = parsed_stream.GetTimestamp(i);
         switch (rtcp_block.type()) {
           case webrtc::rtcp::SenderReport::kPacketType:
-            PrintSenderReport(rtcp_block, log_timestamp, direction, media_type);
+            PrintSenderReport(rtcp_block, log_timestamp, direction);
             break;
           case webrtc::rtcp::ReceiverReport::kPacketType:
-            PrintReceiverReport(rtcp_block, log_timestamp, direction,
-                                media_type);
+            PrintReceiverReport(rtcp_block, log_timestamp, direction);
             break;
           case webrtc::rtcp::Sdes::kPacketType:
-            PrintSdes(rtcp_block, log_timestamp, direction, media_type);
+            PrintSdes(rtcp_block, log_timestamp, direction);
             break;
           case webrtc::rtcp::ExtendedReports::kPacketType:
-            PrintXr(rtcp_block, log_timestamp, direction, media_type);
+            PrintXr(rtcp_block, log_timestamp, direction);
             break;
           case webrtc::rtcp::Bye::kPacketType:
-            PrintBye(rtcp_block, log_timestamp, direction, media_type);
+            PrintBye(rtcp_block, log_timestamp, direction);
             break;
           case webrtc::rtcp::Rtpfb::kPacketType:
-            PrintRtpFeedback(rtcp_block, log_timestamp, direction, media_type);
+            PrintRtpFeedback(rtcp_block, log_timestamp, direction);
             break;
           case webrtc::rtcp::Psfb::kPacketType:
-            PrintPsFeedback(rtcp_block, log_timestamp, direction, media_type);
+            PrintPsFeedback(rtcp_block, log_timestamp, direction);
             break;
           default:
             break;