Update RtcpReceiver to use Timesetamp/TimeDelta types instead of raw ints

Bug: webrtc:13757
Change-Id: Ie0317a584406bec3c34403a7bc8059e4272b339f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/311674
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#40429}
diff --git a/modules/rtp_rtcp/source/rtcp_receiver.cc b/modules/rtp_rtcp/source/rtcp_receiver.cc
index dd9d266..ba5b951 100644
--- a/modules/rtp_rtcp/source/rtcp_receiver.cc
+++ b/modules/rtp_rtcp/source/rtcp_receiver.cc
@@ -19,6 +19,8 @@
 #include <utility>
 #include <vector>
 
+#include "api/units/time_delta.h"
+#include "api/units/timestamp.h"
 #include "api/video/video_bitrate_allocation.h"
 #include "api/video/video_bitrate_allocator.h"
 #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
@@ -55,15 +57,14 @@
 using rtcp::ReportBlock;
 
 // The number of RTCP time intervals needed to trigger a timeout.
-const int kRrTimeoutIntervals = 3;
+constexpr int kRrTimeoutIntervals = 3;
 
-const int64_t kTmmbrTimeoutIntervalMs = 5 * 5000;
-
-const int64_t kMaxWarningLogIntervalMs = 10000;
-const int64_t kRtcpMinFrameLengthMs = 17;
+constexpr TimeDelta kTmmbrTimeoutInterval = TimeDelta::Seconds(25);
+constexpr TimeDelta kMaxWarningLogInterval = TimeDelta::Seconds(10);
+constexpr TimeDelta kRtcpMinFrameLength = TimeDelta::Millis(17);
 
 // Maximum number of received RRTRs that will be stored.
-const size_t kMaxNumberOfStoredRrtrs = 300;
+constexpr size_t kMaxNumberOfStoredRrtrs = 300;
 
 constexpr TimeDelta kDefaultVideoReportInterval = TimeDelta::Seconds(1);
 constexpr TimeDelta kDefaultAudioReportInterval = TimeDelta::Seconds(5);
@@ -154,12 +155,12 @@
       // TODO(bugs.webrtc.org/10774): Remove fallback.
       remote_ssrc_(0),
       xr_rrtr_status_(config.non_sender_rtt_measurement),
-      oldest_tmmbr_info_ms_(0),
+      oldest_tmmbr_info_(Timestamp::Zero()),
       cname_callback_(config.rtcp_cname_callback),
       report_block_data_observer_(config.report_block_data_observer),
       packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
       num_skipped_packets_(0),
-      last_skipped_packets_warning_ms_(clock_->TimeInMilliseconds()) {
+      last_skipped_packets_warning_(clock_->CurrentTime()) {
   RTC_DCHECK(owner);
 }
 
@@ -181,12 +182,12 @@
       // TODO(bugs.webrtc.org/10774): Remove fallback.
       remote_ssrc_(0),
       xr_rrtr_status_(config.non_sender_rtt_measurement),
-      oldest_tmmbr_info_ms_(0),
+      oldest_tmmbr_info_(Timestamp::Zero()),
       cname_callback_(config.rtcp_cname_callback),
       report_block_data_observer_(config.report_block_data_observer),
       packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
       num_skipped_packets_(0),
-      last_skipped_packets_warning_ms_(clock_->TimeInMilliseconds()) {
+      last_skipped_packets_warning_(clock_->CurrentTime()) {
   RTC_DCHECK(owner);
   // Dear reader - if you're here because of this log statement and are
   // wondering what this is about, chances are that you are using an instance
@@ -313,7 +314,7 @@
     }
 
     // Check for expired timers and if so, log and reset.
-    auto now = clock_->CurrentTime();
+    Timestamp now = clock_->CurrentTime();
     if (RtcpRrTimeoutLocked(now)) {
       RTC_LOG_F(LS_WARNING) << "Timeout: No RTCP RR received.";
     } else if (RtcpRrSequenceNumberTimeoutLocked(now)) {
@@ -466,14 +467,14 @@
   }
 
   if (num_skipped_packets_ > 0) {
-    const int64_t now_ms = clock_->TimeInMilliseconds();
-    if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs) {
-      last_skipped_packets_warning_ms_ = now_ms;
+    const Timestamp now = clock_->CurrentTime();
+    if (now - last_skipped_packets_warning_ >= kMaxWarningLogInterval) {
+      last_skipped_packets_warning_ = now;
       RTC_LOG(LS_WARNING)
           << num_skipped_packets_
           << " RTCP blocks were skipped due to being malformed or of "
              "unrecognized/unsupported type, during the past "
-          << (kMaxWarningLogIntervalMs / 1000) << " second period.";
+          << kMaxWarningLogInterval << " period.";
     }
   }
 
@@ -622,14 +623,14 @@
   // Create or find receive information.
   TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
   // Update that this remote is alive.
-  tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
+  tmmbr_info->last_time_received = clock_->CurrentTime();
   return tmmbr_info;
 }
 
 void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
   auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
   if (tmmbr_it != tmmbr_infos_.end())
-    tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
+    tmmbr_it->second.last_time_received = clock_->CurrentTime();
 }
 
 RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
@@ -657,31 +658,30 @@
 bool RTCPReceiver::UpdateTmmbrTimers() {
   MutexLock lock(&rtcp_receiver_lock_);
 
-  int64_t now_ms = clock_->TimeInMilliseconds();
-  int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
+  Timestamp timeout = clock_->CurrentTime() - kTmmbrTimeoutInterval;
 
-  if (oldest_tmmbr_info_ms_ >= timeout_ms)
+  if (oldest_tmmbr_info_ >= timeout)
     return false;
 
   bool update_bounding_set = false;
-  oldest_tmmbr_info_ms_ = -1;
+  oldest_tmmbr_info_ = Timestamp::MinusInfinity();
   for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
     TmmbrInformation* tmmbr_info = &tmmbr_it->second;
-    if (tmmbr_info->last_time_received_ms > 0) {
-      if (tmmbr_info->last_time_received_ms < timeout_ms) {
+    if (tmmbr_info->last_time_received > Timestamp::Zero()) {
+      if (tmmbr_info->last_time_received < timeout) {
         // No rtcp packet for the last 5 regular intervals, reset limitations.
         tmmbr_info->tmmbr.clear();
         // Prevent that we call this over and over again.
-        tmmbr_info->last_time_received_ms = 0;
+        tmmbr_info->last_time_received = Timestamp::Zero();
         // Send new TMMBN to all channels using the default codec.
         update_bounding_set = true;
-      } else if (oldest_tmmbr_info_ms_ == -1 ||
-                 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
-        oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
+      } else if (oldest_tmmbr_info_ == Timestamp::MinusInfinity() ||
+                 tmmbr_info->last_time_received < oldest_tmmbr_info_) {
+        oldest_tmmbr_info_ = tmmbr_info->last_time_received;
       }
       ++tmmbr_it;
     } else if (tmmbr_info->ready_for_delete) {
-      // When we dont have a last_time_received_ms and the object is marked
+      // When we dont have a `last_time_received` and the object is marked
       // ready_for_delete it's removed from the map.
       tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
     } else {
@@ -926,9 +926,9 @@
     auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
     entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
                                        request.packet_overhead());
-    // FindOrCreateTmmbrInfo always sets `last_time_received_ms` to
-    // `clock_->TimeInMilliseconds()`.
-    entry->last_updated_ms = tmmbr_info->last_time_received_ms;
+    // FindOrCreateTmmbrInfo always sets `last_time_received` to
+    // `clock_->CurrentTime()`.
+    entry->last_updated = tmmbr_info->last_time_received;
 
     packet_information->packet_type_flags |= kRtcpTmmbr;
     break;
@@ -999,7 +999,7 @@
   if (fir.requests().empty())
     return true;
 
-  const int64_t now_ms = clock_->TimeInMilliseconds();
+  const Timestamp now = clock_->CurrentTime();
   for (const rtcp::Fir::Request& fir_request : fir.requests()) {
     // Is it our sender that is requested to generate a new keyframe.
     if (local_media_ssrc() != fir_request.ssrc)
@@ -1007,20 +1007,20 @@
 
     ++packet_type_counter_.fir_packets;
 
-    auto inserted = last_fir_.insert(std::make_pair(
-        fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
-    if (!inserted.second) {  // There was already an entry.
-      LastFirStatus* last_fir = &inserted.first->second;
+    auto [it, inserted] =
+        last_fir_.try_emplace(fir.sender_ssrc(), now, fir_request.seq_nr);
+    if (!inserted) {  // There was already an entry.
+      LastFirStatus* last_fir = &it->second;
 
       // Check if we have reported this FIRSequenceNumber before.
       if (fir_request.seq_nr == last_fir->sequence_number)
         continue;
 
       // Sanity: don't go crazy with the callbacks.
-      if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
+      if (now - last_fir->request < kRtcpMinFrameLength)
         continue;
 
-      last_fir->request_ms = now_ms;
+      last_fir->request = now;
       last_fir->sequence_number = fir_request.seq_nr;
     }
     // Received signal that we need to send a new key frame.
@@ -1173,12 +1173,11 @@
   MutexLock lock(&rtcp_receiver_lock_);
   std::vector<rtcp::TmmbItem> candidates;
 
-  int64_t now_ms = clock_->TimeInMilliseconds();
-  int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
+  Timestamp timeout = clock_->CurrentTime() - kTmmbrTimeoutInterval;
 
   for (auto& kv : tmmbr_infos_) {
     for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
-      if (it->second.last_updated_ms < timeout_ms) {
+      if (it->second.last_updated < timeout) {
         // Erase timeout entries.
         it = kv.second.tmmbr.erase(it);
       } else {
diff --git a/modules/rtp_rtcp/source/rtcp_receiver.h b/modules/rtp_rtcp/source/rtcp_receiver.h
index b097869..a6175d0 100644
--- a/modules/rtp_rtcp/source/rtcp_receiver.h
+++ b/modules/rtp_rtcp/source/rtcp_receiver.h
@@ -20,6 +20,7 @@
 #include "api/array_view.h"
 #include "api/sequence_checker.h"
 #include "api/units/time_delta.h"
+#include "api/units/timestamp.h"
 #include "modules/rtp_rtcp/include/report_block_data.h"
 #include "modules/rtp_rtcp/include/rtcp_statistics.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
@@ -204,10 +205,10 @@
   struct TmmbrInformation {
     struct TimedTmmbrItem {
       rtcp::TmmbItem tmmbr_item;
-      int64_t last_updated_ms;
+      Timestamp last_updated = Timestamp::Zero();
     };
 
-    int64_t last_time_received_ms = 0;
+    Timestamp last_time_received = Timestamp::Zero();
 
     bool ready_for_delete = false;
 
@@ -232,9 +233,9 @@
   };
 
   struct LastFirStatus {
-    LastFirStatus(int64_t now_ms, uint8_t sequence_number)
-        : request_ms(now_ms), sequence_number(sequence_number) {}
-    int64_t request_ms;
+    LastFirStatus(Timestamp now, uint8_t sequence_number)
+        : request(now), sequence_number(sequence_number) {}
+    Timestamp request;
     uint8_t sequence_number;
   };
 
@@ -380,7 +381,7 @@
   bool xr_rrtr_status_ RTC_GUARDED_BY(rtcp_receiver_lock_);
   absl::optional<TimeDelta> xr_rr_rtt_;
 
-  int64_t oldest_tmmbr_info_ms_ RTC_GUARDED_BY(rtcp_receiver_lock_);
+  Timestamp oldest_tmmbr_info_ RTC_GUARDED_BY(rtcp_receiver_lock_);
   // Mapped by remote ssrc.
   flat_map<uint32_t, TmmbrInformation> tmmbr_infos_
       RTC_GUARDED_BY(rtcp_receiver_lock_);
@@ -414,7 +415,7 @@
   RtcpNackStats nack_stats_;
 
   size_t num_skipped_packets_;
-  int64_t last_skipped_packets_warning_ms_;
+  Timestamp last_skipped_packets_warning_;
 };
 }  // namespace webrtc
 #endif  // MODULES_RTP_RTCP_SOURCE_RTCP_RECEIVER_H_