rtcp::Rrtr block moved into own file and got Parse function

BUG=webrtc:5260
R=asapersson@webrtc.org, åsapersson

Review URL: https://codereview.webrtc.org/1496883002 .

Cr-Commit-Position: refs/heads/master@{#10912}
diff --git a/webrtc/modules/modules.gyp b/webrtc/modules/modules.gyp
index 5d7899c..cac5f40 100644
--- a/webrtc/modules/modules.gyp
+++ b/webrtc/modules/modules.gyp
@@ -310,6 +310,7 @@
                 'rtp_rtcp/source/rtcp_packet/pli_unittest.cc',
                 'rtp_rtcp/source/rtcp_packet/receiver_report_unittest.cc',
                 'rtp_rtcp/source/rtcp_packet/report_block_unittest.cc',
+                'rtp_rtcp/source/rtcp_packet/rrtr_unittest.cc',
                 'rtp_rtcp/source/rtcp_packet/transport_feedback_unittest.cc',
                 'rtp_rtcp/source/rtcp_receiver_unittest.cc',
                 'rtp_rtcp/source/rtcp_sender_unittest.cc',
diff --git a/webrtc/modules/rtp_rtcp/BUILD.gn b/webrtc/modules/rtp_rtcp/BUILD.gn
index 21869b4..20b2075 100644
--- a/webrtc/modules/rtp_rtcp/BUILD.gn
+++ b/webrtc/modules/rtp_rtcp/BUILD.gn
@@ -60,6 +60,8 @@
     "source/rtcp_packet/receiver_report.h",
     "source/rtcp_packet/report_block.cc",
     "source/rtcp_packet/report_block.h",
+    "source/rtcp_packet/rrtr.cc",
+    "source/rtcp_packet/rrtr.h",
     "source/rtcp_packet/transport_feedback.cc",
     "source/rtcp_packet/transport_feedback.h",
     "source/rtcp_receiver.cc",
diff --git a/webrtc/modules/rtp_rtcp/rtp_rtcp.gypi b/webrtc/modules/rtp_rtcp/rtp_rtcp.gypi
index a315b023..9282135 100644
--- a/webrtc/modules/rtp_rtcp/rtp_rtcp.gypi
+++ b/webrtc/modules/rtp_rtcp/rtp_rtcp.gypi
@@ -55,6 +55,8 @@
         'source/rtcp_packet/receiver_report.h',
         'source/rtcp_packet/report_block.cc',
         'source/rtcp_packet/report_block.h',
+        'source/rtcp_packet/rrtr.cc',
+        'source/rtcp_packet/rrtr.h',
         'source/rtcp_packet/transport_feedback.cc',
         'source/rtcp_packet/transport_feedback.h',
         'source/rtcp_receiver.cc',
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_packet.cc b/webrtc/modules/rtp_rtcp/source/rtcp_packet.cc
index 41ebcad..e31f748 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_packet.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_packet.cc
@@ -43,7 +43,6 @@
 using webrtc::RTCPUtility::RTCPPacketRTPFBTMMBRItem;
 using webrtc::RTCPUtility::RTCPPacketSR;
 using webrtc::RTCPUtility::RTCPPacketXRDLRRReportBlockItem;
-using webrtc::RTCPUtility::RTCPPacketXRReceiverReferenceTimeItem;
 using webrtc::RTCPUtility::RTCPPacketXR;
 using webrtc::RTCPUtility::RTCPPacketXRVOIPMetricItem;
 
@@ -416,30 +415,6 @@
   AssignUWord16(buffer, pos, block_length);
 }
 
-// Receiver Reference Time Report Block (RFC 3611).
-//
-//   0                   1                   2                   3
-//   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
-//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-//  |     BT=4      |   reserved    |       block length = 2        |
-//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-//  |              NTP timestamp, most significant word             |
-//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-//  |             NTP timestamp, least significant word             |
-//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
-void CreateRrtr(const std::vector<RTCPPacketXRReceiverReferenceTimeItem>& rrtrs,
-                uint8_t* buffer,
-                size_t* pos) {
-  const uint16_t kBlockLength = 2;
-  for (std::vector<RTCPPacketXRReceiverReferenceTimeItem>::const_iterator it =
-       rrtrs.begin(); it != rrtrs.end(); ++it) {
-    CreateXrBlockHeader(kBtReceiverReferenceTime, kBlockLength, buffer, pos);
-    AssignUWord32(buffer, pos, (*it).NTPMostSignificant);
-    AssignUWord32(buffer, pos, (*it).NTPLeastSignificant);
-  }
-}
-
 // DLRR Report Block (RFC 3611).
 //
 //   0                   1                   2                   3
@@ -910,19 +885,22 @@
   }
   CreateHeader(0U, PT_XR, HeaderLength(), packet, index);
   CreateXrHeader(xr_header_, packet, index);
-  CreateRrtr(rrtr_blocks_, packet, index);
+  for (const Rrtr& block : rrtr_blocks_) {
+    block.Create(packet + *index);
+    *index += Rrtr::kLength;
+  }
   CreateDlrr(dlrr_blocks_, packet, index);
   CreateVoipMetric(voip_metric_blocks_, packet, index);
   return true;
 }
 
 bool Xr::WithRrtr(Rrtr* rrtr) {
-  assert(rrtr);
+  RTC_DCHECK(rrtr);
   if (rrtr_blocks_.size() >= kMaxNumberOfRrtrBlocks) {
     LOG(LS_WARNING) << "Max RRTR blocks reached.";
     return false;
   }
-  rrtr_blocks_.push_back(rrtr->rrtr_block_);
+  rrtr_blocks_.push_back(*rrtr);
   return true;
 }
 
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_packet.h b/webrtc/modules/rtp_rtcp/source/rtcp_packet.h
index 1f5e223..7ea05b1 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_packet.h
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_packet.h
@@ -18,6 +18,7 @@
 
 #include "webrtc/base/scoped_ptr.h"
 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/report_block.h"
+#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr.h"
 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
 #include "webrtc/typedefs.h"
@@ -30,7 +31,6 @@
 
 class Dlrr;
 class RawPacket;
-class Rrtr;
 class VoipMetric;
 
 // Class for building RTCP packets.
@@ -670,10 +670,7 @@
     return kXrHeaderLength + RrtrLength() + DlrrLength() + VoipMetricLength();
   }
 
-  size_t RrtrLength() const {
-    const size_t kRrtrBlockLength = 12;
-    return kRrtrBlockLength * rrtr_blocks_.size();
-  }
+  size_t RrtrLength() const { return Rrtr::kLength * rrtr_blocks_.size(); }
 
   size_t DlrrLength() const;
 
@@ -683,46 +680,13 @@
   }
 
   RTCPUtility::RTCPPacketXR xr_header_;
-  std::vector<RTCPUtility::RTCPPacketXRReceiverReferenceTimeItem> rrtr_blocks_;
+  std::vector<Rrtr> rrtr_blocks_;
   std::vector<DlrrBlock> dlrr_blocks_;
   std::vector<RTCPUtility::RTCPPacketXRVOIPMetricItem> voip_metric_blocks_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(Xr);
 };
 
-// Receiver Reference Time Report Block (RFC 3611).
-//
-//   0                   1                   2                   3
-//   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
-//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-//  |     BT=4      |   reserved    |       block length = 2        |
-//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-//  |              NTP timestamp, most significant word             |
-//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-//  |             NTP timestamp, least significant word             |
-//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
-class Rrtr {
- public:
-  Rrtr() {
-    memset(&rrtr_block_, 0, sizeof(rrtr_block_));
-  }
-  ~Rrtr() {}
-
-  void WithNtpSec(uint32_t sec) {
-    rrtr_block_.NTPMostSignificant = sec;
-  }
-  void WithNtpFrac(uint32_t frac) {
-    rrtr_block_.NTPLeastSignificant = frac;
-  }
-
- private:
-  friend class Xr;
-  RTCPUtility::RTCPPacketXRReceiverReferenceTimeItem rrtr_block_;
-
-  RTC_DISALLOW_COPY_AND_ASSIGN(Rrtr);
-};
-
 // DLRR Report Block (RFC 3611).
 //
 //   0                   1                   2                   3
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr.cc b/webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr.cc
new file mode 100644
index 0000000..db4ae67
--- /dev/null
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr.cc
@@ -0,0 +1,49 @@
+/*
+ *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr.h"
+
+#include "webrtc/base/checks.h"
+#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
+
+namespace webrtc {
+namespace rtcp {
+// Receiver Reference Time Report Block (RFC 3611).
+//
+//   0                   1                   2                   3
+//   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+//  |     BT=4      |   reserved    |       block length = 2        |
+//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+//  |              NTP timestamp, most significant word             |
+//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+//  |             NTP timestamp, least significant word             |
+//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+
+void Rrtr::Parse(const uint8_t* buffer) {
+  RTC_DCHECK(buffer[0] == kBlockType);
+  // reserved = buffer[1];
+  RTC_DCHECK(ByteReader<uint16_t>::ReadBigEndian(&buffer[2]) == kBlockLength);
+  uint32_t seconds = ByteReader<uint32_t>::ReadBigEndian(&buffer[4]);
+  uint32_t fraction = ByteReader<uint32_t>::ReadBigEndian(&buffer[8]);
+  ntp_.Set(seconds, fraction);
+}
+
+void Rrtr::Create(uint8_t* buffer) const {
+  const uint8_t kReserved = 0;
+  buffer[0] = kBlockType;
+  buffer[1] = kReserved;
+  ByteWriter<uint16_t>::WriteBigEndian(&buffer[2], kBlockLength);
+  ByteWriter<uint32_t>::WriteBigEndian(&buffer[4], ntp_.seconds());
+  ByteWriter<uint32_t>::WriteBigEndian(&buffer[8], ntp_.fractions());
+}
+
+}  // namespace rtcp
+}  // namespace webrtc
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr.h b/webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr.h
new file mode 100644
index 0000000..3354f61
--- /dev/null
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr.h
@@ -0,0 +1,49 @@
+/*
+ *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ *
+ */
+
+#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_RRTR_H_
+#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_RRTR_H_
+
+#include "webrtc/base/basictypes.h"
+#include "webrtc/system_wrappers/include/ntp_time.h"
+
+namespace webrtc {
+namespace rtcp {
+
+class Rrtr {
+ public:
+  static const uint8_t kBlockType = 4;
+  static const uint16_t kBlockLength = 2;
+  static const size_t kLength = 4 * (kBlockLength + 1);  // 12
+
+  Rrtr() {}
+  Rrtr(const Rrtr&) = default;
+  ~Rrtr() {}
+
+  Rrtr& operator=(const Rrtr&) = default;
+
+  void Parse(const uint8_t* buffer);
+
+  // Fills buffer with the Rrtr.
+  // Consumes Rrtr::kLength bytes.
+  void Create(uint8_t* buffer) const;
+
+  void WithNtp(const NtpTime& ntp) { ntp_ = ntp; }
+
+  NtpTime ntp() const { return ntp_; }
+
+ private:
+  NtpTime ntp_;
+};
+
+}  // namespace rtcp
+}  // namespace webrtc
+#endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_RRTR_H_
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr_unittest.cc
new file mode 100644
index 0000000..6536e06
--- /dev/null
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr_unittest.cc
@@ -0,0 +1,51 @@
+/*
+ *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rrtr.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+using webrtc::rtcp::Rrtr;
+
+namespace webrtc {
+namespace {
+
+const uint32_t kNtpSec = 0x12345678;
+const uint32_t kNtpFrac = 0x23456789;
+const uint8_t kBlock[] = {0x04, 0x00, 0x00, 0x02,
+                          0x12, 0x34, 0x56, 0x78,
+                          0x23, 0x45, 0x67, 0x89};
+const size_t kBlockSizeBytes = sizeof(kBlock);
+static_assert(
+    kBlockSizeBytes == Rrtr::kLength,
+    "Size of manually created Rrtr block should match class constant");
+
+TEST(RtcpPacketRrtrTest, Create) {
+  uint8_t buffer[Rrtr::kLength];
+  Rrtr rrtr;
+  rrtr.WithNtp(NtpTime(kNtpSec, kNtpFrac));
+
+  rrtr.Create(buffer);
+  EXPECT_EQ(0, memcmp(buffer, kBlock, kBlockSizeBytes));
+}
+
+TEST(RtcpPacketRrtrTest, Parse) {
+  Rrtr read_rrtr;
+  read_rrtr.Parse(kBlock);
+
+  // Run checks on const object to ensure all accessors have const modifier.
+  const Rrtr& parsed = read_rrtr;
+
+  EXPECT_EQ(kNtpSec, parsed.ntp().seconds());
+  EXPECT_EQ(kNtpFrac, parsed.ntp().fractions());
+}
+
+}  // namespace
+}  // namespace webrtc
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_packet_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtcp_packet_unittest.cc
index dc2b113..eed56e2 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_packet_unittest.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_packet_unittest.cc
@@ -684,8 +684,7 @@
 
 TEST(RtcpPacketTest, XrWithRrtr) {
   Rrtr rrtr;
-  rrtr.WithNtpSec(0x11111111);
-  rrtr.WithNtpFrac(0x22222222);
+  rrtr.WithNtp(NtpTime(0x11111111, 0x22222222));
   Xr xr;
   xr.From(kSenderSsrc);
   EXPECT_TRUE(xr.WithRrtr(&rrtr));
@@ -702,11 +701,9 @@
 
 TEST(RtcpPacketTest, XrWithTwoRrtrBlocks) {
   Rrtr rrtr1;
-  rrtr1.WithNtpSec(0x11111111);
-  rrtr1.WithNtpFrac(0x22222222);
+  rrtr1.WithNtp(NtpTime(0x11111111, 0x22222222));
   Rrtr rrtr2;
-  rrtr2.WithNtpSec(0x33333333);
-  rrtr2.WithNtpFrac(0x44444444);
+  rrtr2.WithNtp(NtpTime(0x33333333, 0x44444444));
   Xr xr;
   xr.From(kSenderSsrc);
   EXPECT_TRUE(xr.WithRrtr(&rrtr1));
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
index b75fb73..c27dfa5 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
@@ -620,8 +620,7 @@
 
 TEST_F(RtcpReceiverTest, InjectXrReceiverReferenceTimePacket) {
   rtcp::Rrtr rrtr;
-  rrtr.WithNtpSec(0x10203);
-  rrtr.WithNtpFrac(0x40506);
+  rrtr.WithNtp(NtpTime(0x10203, 0x40506));
   rtcp::Xr xr;
   xr.From(0x2345);
   xr.WithRrtr(&rrtr);
@@ -756,13 +755,12 @@
 
 TEST_F(RtcpReceiverTest, GetLastReceivedXrReferenceTimeInfo) {
   const uint32_t kSenderSsrc = 0x123456;
-  const uint32_t kNtpSec = 0x10203;
-  const uint32_t kNtpFrac = 0x40506;
-  const uint32_t kNtpMid = RTCPUtility::MidNtp(kNtpSec, kNtpFrac);
+  const NtpTime kNtp(0x10203, 0x40506);
+  const uint32_t kNtpMid =
+      RTCPUtility::MidNtp(kNtp.seconds(), kNtp.fractions());
 
   rtcp::Rrtr rrtr;
-  rrtr.WithNtpSec(kNtpSec);
-  rrtr.WithNtpFrac(kNtpFrac);
+  rrtr.WithNtp(kNtp);
   rtcp::Xr xr;
   xr.From(kSenderSsrc);
   xr.WithRrtr(&rrtr);
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc b/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc
index ac3f337..dc022bf 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc
@@ -764,8 +764,7 @@
   xr->From(ssrc_);
 
   rtcp::Rrtr rrtr;
-  rrtr.WithNtpSec(ctx.ntp_sec_);
-  rrtr.WithNtpFrac(ctx.ntp_frac_);
+  rrtr.WithNtp(NtpTime(ctx.ntp_sec_, ctx.ntp_frac_));
 
   xr->WithRrtr(&rrtr);