Migrate rtc event log away from legacy rtp header parser

Legacy parser is unsupported,
e.g. it doesn't support parsing two-byte header rtp header extension.

Bug: None
Change-Id: I83d683a7fcb9b147dbfe06bc8808fb5731e0e8f8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/226560
Reviewed-by: Björn Terelius <terelius@google.com>
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34572}
diff --git a/logging/BUILD.gn b/logging/BUILD.gn
index 90a05f7..0700403 100644
--- a/logging/BUILD.gn
+++ b/logging/BUILD.gn
@@ -345,7 +345,6 @@
       "../call:video_stream_api",
       "../modules:module_api_public",
       "../modules/audio_coding:audio_network_adaptor",
-      "../modules/rtp_rtcp",
       "../modules/rtp_rtcp:rtp_rtcp_format",
       "../rtc_base:checks",
       "../rtc_base:ignore_wundef",
diff --git a/logging/rtc_event_log/rtc_event_log_parser.cc b/logging/rtc_event_log/rtc_event_log_parser.cc
index 91df18b..cb4be8f 100644
--- a/logging/rtc_event_log/rtc_event_log_parser.cc
+++ b/logging/rtc_event_log/rtc_event_log_parser.cc
@@ -35,7 +35,7 @@
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
 #include "modules/rtp_rtcp/source/byte_io.h"
 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
-#include "modules/rtp_rtcp/source/rtp_utility.h"
+#include "modules/rtp_rtcp/source/rtp_packet_received.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/safe_conversions.h"
@@ -1368,7 +1368,6 @@
 ParsedRtcEventLog::ParseStatus ParsedRtcEventLog::StoreParsedLegacyEvent(
     const rtclog::Event& event) {
   RTC_PARSE_CHECK_OR_RETURN(event.has_type());
-  RTC_PARSE_CHECK_OR_RETURN(event.has_type());
   switch (event.type()) {
     case rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT: {
       auto config = GetVideoReceiveConfig(event);
@@ -1426,40 +1425,45 @@
       break;
     }
     case rtclog::Event::RTP_EVENT: {
-      PacketDirection direction;
-      uint8_t header[IP_PACKET_SIZE];
-      size_t header_length;
-      size_t total_length;
-      ParseStatus status = GetRtpHeader(event, &direction, header,
-                                        &header_length, &total_length, nullptr);
-      RTC_RETURN_IF_ERROR(status);
+      RTC_PARSE_CHECK_OR_RETURN(event.has_rtp_packet());
+      const rtclog::RtpPacket& rtp_packet = event.rtp_packet();
+      RTC_PARSE_CHECK_OR_RETURN(rtp_packet.has_header());
+      RTC_PARSE_CHECK_OR_RETURN(rtp_packet.has_incoming());
+      RTC_PARSE_CHECK_OR_RETURN(rtp_packet.has_packet_length());
+      size_t total_length = rtp_packet.packet_length();
 
-      uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(header + 8);
-      const RtpHeaderExtensionMap* extension_map =
-          GetRtpHeaderExtensionMap(direction, ssrc);
-      RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
+      // Use RtpPacketReceived instead of more generic RtpPacket because former
+      // has a buildin convertion to RTPHeader.
+      RtpPacketReceived rtp_header;
+      RTC_PARSE_CHECK_OR_RETURN(rtp_header.Parse(rtp_packet.header()));
+
+      if (const RtpHeaderExtensionMap* extension_map = GetRtpHeaderExtensionMap(
+              rtp_packet.incoming(), rtp_header.Ssrc())) {
+        rtp_header.IdentifyExtensions(*extension_map);
+      }
+
       RTPHeader parsed_header;
-      rtp_parser.Parse(&parsed_header, extension_map, /*header_only*/ true);
+      rtp_header.GetHeader(&parsed_header);
 
       // Since we give the parser only a header, there is no way for it to know
       // the padding length. The best solution would be to log the padding
       // length in RTC event log. In absence of it, we assume the RTP packet to
       // contain only padding, if the padding bit is set.
       // TODO(webrtc:9730): Use a generic way to obtain padding length.
-      if ((header[0] & 0x20) != 0)
-        parsed_header.paddingLength = total_length - header_length;
+      if (rtp_header.has_padding())
+        parsed_header.paddingLength = total_length - rtp_header.size();
 
       RTC_PARSE_CHECK_OR_RETURN(event.has_timestamp_us());
       int64_t timestamp_us = event.timestamp_us();
-      if (direction == kIncomingPacket) {
+      if (rtp_packet.incoming()) {
         incoming_rtp_packets_map_[parsed_header.ssrc].push_back(
             LoggedRtpPacketIncoming(Timestamp::Micros(timestamp_us),
-                                    parsed_header, header_length,
+                                    parsed_header, rtp_header.size(),
                                     total_length));
       } else {
         outgoing_rtp_packets_map_[parsed_header.ssrc].push_back(
             LoggedRtpPacketOutgoing(Timestamp::Micros(timestamp_us),
-                                    parsed_header, header_length,
+                                    parsed_header, rtp_header.size(),
                                     total_length));
       }
       break;
@@ -1574,59 +1578,11 @@
   return ParseStatus::Success();
 }
 
-// The header must have space for at least IP_PACKET_SIZE bytes.
-ParsedRtcEventLog::ParseStatus ParsedRtcEventLog::GetRtpHeader(
-    const rtclog::Event& event,
-    PacketDirection* incoming,
-    uint8_t* header,
-    size_t* header_length,
-    size_t* total_length,
-    int* probe_cluster_id) const {
-  RTC_PARSE_CHECK_OR_RETURN(event.has_type());
-  RTC_PARSE_CHECK_OR_RETURN_EQ(event.type(), rtclog::Event::RTP_EVENT);
-  RTC_PARSE_CHECK_OR_RETURN(event.has_rtp_packet());
-  const rtclog::RtpPacket& rtp_packet = event.rtp_packet();
-  // Get direction of packet.
-  RTC_PARSE_CHECK_OR_RETURN(rtp_packet.has_incoming());
-  if (incoming != nullptr) {
-    *incoming = rtp_packet.incoming() ? kIncomingPacket : kOutgoingPacket;
-  }
-  // Get packet length.
-  RTC_PARSE_CHECK_OR_RETURN(rtp_packet.has_packet_length());
-  if (total_length != nullptr) {
-    *total_length = rtp_packet.packet_length();
-  }
-  // Get header length.
-  RTC_PARSE_CHECK_OR_RETURN(rtp_packet.has_header());
-  if (header_length != nullptr) {
-    *header_length = rtp_packet.header().size();
-  }
-  if (probe_cluster_id != nullptr) {
-    if (rtp_packet.has_probe_cluster_id()) {
-      *probe_cluster_id = rtp_packet.probe_cluster_id();
-      RTC_PARSE_CHECK_OR_RETURN_NE(*probe_cluster_id,
-                                   PacedPacketInfo::kNotAProbe);
-    } else {
-      *probe_cluster_id = PacedPacketInfo::kNotAProbe;
-    }
-  }
-  // Get header contents.
-  if (header != nullptr) {
-    const size_t kMinRtpHeaderSize = 12;
-    RTC_PARSE_CHECK_OR_RETURN_GE(rtp_packet.header().size(), kMinRtpHeaderSize);
-    RTC_PARSE_CHECK_OR_RETURN_LE(rtp_packet.header().size(),
-                                 static_cast<size_t>(IP_PACKET_SIZE));
-    memcpy(header, rtp_packet.header().data(), rtp_packet.header().size());
-  }
-  return ParseStatus::Success();
-}
-
 const RtpHeaderExtensionMap* ParsedRtcEventLog::GetRtpHeaderExtensionMap(
-    PacketDirection direction,
+    bool incoming,
     uint32_t ssrc) {
-  auto& extensions_maps = direction == PacketDirection::kIncomingPacket
-                              ? incoming_rtp_extensions_maps_
-                              : outgoing_rtp_extensions_maps_;
+  auto& extensions_maps =
+      incoming ? incoming_rtp_extensions_maps_ : outgoing_rtp_extensions_maps_;
   auto it = extensions_maps.find(ssrc);
   if (it != extensions_maps.end()) {
     return &(it->second);
diff --git a/logging/rtc_event_log/rtc_event_log_parser.h b/logging/rtc_event_log/rtc_event_log_parser.h
index 4898022..d18a238 100644
--- a/logging/rtc_event_log/rtc_event_log_parser.h
+++ b/logging/rtc_event_log/rtc_event_log_parser.h
@@ -627,25 +627,12 @@
   template <typename T>
   void StoreFirstAndLastTimestamp(const std::vector<T>& v);
 
-  // Reads the header, direction, header length and packet length from the RTP
-  // event at |index|, and stores the values in the corresponding output
-  // parameters. Each output parameter can be set to nullptr if that value
-  // isn't needed.
-  // NB: The header must have space for at least IP_PACKET_SIZE bytes.
-  ParseStatus GetRtpHeader(const rtclog::Event& event,
-                           PacketDirection* incoming,
-                           uint8_t* header,
-                           size_t* header_length,
-                           size_t* total_length,
-                           int* probe_cluster_id) const;
-
   // Returns: a pointer to a header extensions map acquired from parsing
   // corresponding Audio/Video Sender/Receiver config events.
   // Warning: if the same SSRC is reused by both video and audio streams during
   // call, extensions maps may be incorrect (the last one would be returned).
-  const RtpHeaderExtensionMap* GetRtpHeaderExtensionMap(
-      PacketDirection direction,
-      uint32_t ssrc);
+  const RtpHeaderExtensionMap* GetRtpHeaderExtensionMap(bool incoming,
+                                                        uint32_t ssrc);
 
   // Reads packet, direction and packet length from the RTCP event at |index|,
   // and stores the values in the corresponding output parameters.