Using unit classes in BitrateAllocationUpdate struct.

This prepares for moving BitrateAllocationUpdate to API.

Bug: webrtc:9718
Change-Id: Ib2bcedb6b68fde33b6a2466f40829e86438aa973
Reviewed-on: https://webrtc-review.googlesource.com/c/111507
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Oskar Sundbom <ossu@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25737}
diff --git a/audio/audio_send_stream.cc b/audio/audio_send_stream.cc
index b84cc95..cc25ee4 100644
--- a/audio/audio_send_stream.cc
+++ b/audio/audio_send_stream.cc
@@ -465,18 +465,18 @@
   // A send stream may be allocated a bitrate of zero if the allocator decides
   // to disable it. For now we ignore this decision and keep sending on min
   // bitrate.
-  if (update.bitrate_bps == 0) {
-    update.bitrate_bps = config_.min_bitrate_bps;
+  if (update.target_bitrate.IsZero()) {
+    update.target_bitrate = DataRate::bps(config_.min_bitrate_bps);
   }
-  RTC_DCHECK_GE(update.bitrate_bps,
-                static_cast<uint32_t>(config_.min_bitrate_bps));
+  RTC_DCHECK_GE(update.target_bitrate.bps<int>(), config_.min_bitrate_bps);
   // The bitrate allocator might allocate an higher than max configured bitrate
   // if there is room, to allow for, as example, extra FEC. Ignore that for now.
-  const uint32_t max_bitrate_bps = config_.max_bitrate_bps;
-  if (update.bitrate_bps > max_bitrate_bps)
-    update.bitrate_bps = max_bitrate_bps;
+  const DataRate max_bitrate = DataRate::bps(config_.max_bitrate_bps);
+  if (update.target_bitrate > max_bitrate)
+    update.target_bitrate = max_bitrate;
 
-  channel_send_->SetBitrate(update.bitrate_bps, update.bwe_period_ms);
+  channel_send_->SetBitrate(update.target_bitrate.bps(),
+                            update.bwe_period.ms());
 
   // The amount of audio protection is not exposed by the encoder, hence
   // always returning 0.
diff --git a/audio/audio_send_stream_unittest.cc b/audio/audio_send_stream_unittest.cc
index ddd3408..d89acd5 100644
--- a/audio/audio_send_stream_unittest.cc
+++ b/audio/audio_send_stream_unittest.cc
@@ -474,10 +474,10 @@
   EXPECT_CALL(*helper.channel_send(),
               SetBitrate(helper.config().max_bitrate_bps, _));
   BitrateAllocationUpdate update;
-  update.bitrate_bps = helper.config().max_bitrate_bps + 5000;
-  update.fraction_loss = 0;
-  update.rtt = 50;
-  update.bwe_period_ms = 6000;
+  update.target_bitrate = DataRate::bps(helper.config().max_bitrate_bps + 5000);
+  update.packet_loss_ratio = 0;
+  update.round_trip_time = TimeDelta::ms(50);
+  update.bwe_period = TimeDelta::ms(6000);
   send_stream->OnBitrateUpdated(update);
 }
 
@@ -486,10 +486,10 @@
   auto send_stream = helper.CreateAudioSendStream();
   EXPECT_CALL(*helper.channel_send(), SetBitrate(_, 5000));
   BitrateAllocationUpdate update;
-  update.bitrate_bps = helper.config().max_bitrate_bps + 5000;
-  update.fraction_loss = 0;
-  update.rtt = 50;
-  update.bwe_period_ms = 5000;
+  update.target_bitrate = DataRate::bps(helper.config().max_bitrate_bps + 5000);
+  update.packet_loss_ratio = 0;
+  update.round_trip_time = TimeDelta::ms(50);
+  update.bwe_period = TimeDelta::ms(5000);
   send_stream->OnBitrateUpdated(update);
 }
 
diff --git a/call/BUILD.gn b/call/BUILD.gn
index 577cab8..c1dc650 100644
--- a/call/BUILD.gn
+++ b/call/BUILD.gn
@@ -164,6 +164,8 @@
     "bitrate_allocator.h",
   ]
   deps = [
+    "../api/units:data_rate",
+    "../api/units:time_delta",
     "../modules/bitrate_controller",
     "../rtc_base:checks",
     "../rtc_base:rtc_base_approved",
diff --git a/call/bitrate_allocator.cc b/call/bitrate_allocator.cc
index 0fb1bf0..d3cb227 100644
--- a/call/bitrate_allocator.cc
+++ b/call/bitrate_allocator.cc
@@ -55,6 +55,7 @@
       last_non_zero_bitrate_bps_(kDefaultBitrateBps),
       last_fraction_loss_(0),
       last_rtt_(0),
+      last_bwe_period_ms_(1000),
       num_pause_events_(0),
       clock_(Clock::GetRealTimeClock()),
       last_bwe_log_time_(0),
@@ -115,10 +116,13 @@
   for (auto& config : bitrate_observer_configs_) {
     uint32_t allocated_bitrate = allocation[config.observer];
     uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
-    uint32_t protection_bitrate =
-        config.observer->OnBitrateUpdated(BitrateAllocationUpdate{
-            allocated_bitrate, allocated_bandwidth, last_fraction_loss_,
-            last_rtt_, last_bwe_period_ms_});
+    BitrateAllocationUpdate update;
+    update.target_bitrate = DataRate::bps(allocated_bitrate);
+    update.link_capacity = DataRate::bps(allocated_bandwidth);
+    update.packet_loss_ratio = last_fraction_loss_ / 256.0;
+    update.round_trip_time = TimeDelta::ms(last_rtt_);
+    update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
+    uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
 
     if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
       if (target_bitrate_bps > 0)
@@ -179,10 +183,13 @@
     for (auto& config : bitrate_observer_configs_) {
       uint32_t allocated_bitrate = allocation[config.observer];
       uint32_t bandwidth = bandwidth_allocation[config.observer];
-      uint32_t protection_bitrate =
-          config.observer->OnBitrateUpdated(BitrateAllocationUpdate{
-              allocated_bitrate, bandwidth, last_fraction_loss_, last_rtt_,
-              last_bwe_period_ms_});
+      BitrateAllocationUpdate update;
+      update.target_bitrate = DataRate::bps(allocated_bitrate);
+      update.link_capacity = DataRate::bps(bandwidth);
+      update.packet_loss_ratio = last_fraction_loss_ / 256.0;
+      update.round_trip_time = TimeDelta::ms(last_rtt_);
+      update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
+      uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
       config.allocated_bitrate_bps = allocated_bitrate;
       if (allocated_bitrate > 0)
         config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
@@ -191,8 +198,14 @@
     // Currently, an encoder is not allowed to produce frames.
     // But we still have to return the initial config bitrate + let the
     // observer know that it can not produce frames.
-    observer->OnBitrateUpdated(BitrateAllocationUpdate{
-        0, 0, last_fraction_loss_, last_rtt_, last_bwe_period_ms_});
+
+    BitrateAllocationUpdate update;
+    update.target_bitrate = DataRate::Zero();
+    update.link_capacity = DataRate::Zero();
+    update.packet_loss_ratio = last_fraction_loss_ / 256.0;
+    update.round_trip_time = TimeDelta::ms(last_rtt_);
+    update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
+    observer->OnBitrateUpdated(update);
   }
   UpdateAllocationLimits();
 }
diff --git a/call/bitrate_allocator.h b/call/bitrate_allocator.h
index 060bfbf..dbdd14f 100644
--- a/call/bitrate_allocator.h
+++ b/call/bitrate_allocator.h
@@ -19,6 +19,8 @@
 #include <utility>
 #include <vector>
 
+#include "api/units/data_rate.h"
+#include "api/units/time_delta.h"
 #include "rtc_base/bitrateallocationstrategy.h"
 #include "rtc_base/sequenced_task_checker.h"
 
@@ -27,12 +29,11 @@
 class Clock;
 
 struct BitrateAllocationUpdate {
-  // TODO(srte): Rename to target_bitrate.
-  uint32_t bitrate_bps;
-  uint32_t link_capacity_bps;
-  uint8_t fraction_loss;
-  int64_t rtt;
-  int64_t bwe_period_ms;
+  DataRate target_bitrate = DataRate::Zero();
+  DataRate link_capacity = DataRate::Zero();
+  double packet_loss_ratio = 0;
+  TimeDelta round_trip_time = TimeDelta::PlusInfinity();
+  TimeDelta bwe_period = TimeDelta::PlusInfinity();
 };
 // Used by all send streams with adaptive bitrate, to get the currently
 // allocated bitrate for the send stream. The current network properties are
diff --git a/call/bitrate_allocator_unittest.cc b/call/bitrate_allocator_unittest.cc
index e0aae7b..0771f79 100644
--- a/call/bitrate_allocator_unittest.cc
+++ b/call/bitrate_allocator_unittest.cc
@@ -60,11 +60,12 @@
   }
 
   uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override {
-    last_bitrate_bps_ = update.bitrate_bps;
-    last_fraction_loss_ = update.fraction_loss;
-    last_rtt_ms_ = update.rtt;
-    last_probing_interval_ms_ = update.bwe_period_ms;
-    return update.bitrate_bps * protection_ratio_;
+    last_bitrate_bps_ = update.target_bitrate.bps();
+    last_fraction_loss_ =
+        rtc::dchecked_cast<uint8_t>(update.packet_loss_ratio * 256);
+    last_rtt_ms_ = update.round_trip_time.ms();
+    last_probing_interval_ms_ = update.bwe_period.ms();
+    return update.target_bitrate.bps() * protection_ratio_;
   }
   uint32_t last_bitrate_bps_;
   uint8_t last_fraction_loss_;
diff --git a/video/video_send_stream_impl.cc b/video/video_send_stream_impl.cc
index 1cb4dbc..bc35ab9 100644
--- a/video/video_send_stream_impl.cc
+++ b/video/video_send_stream_impl.cc
@@ -616,14 +616,17 @@
   RTC_DCHECK(rtp_video_sender_->IsActive())
       << "VideoSendStream::Start has not been called.";
 
-  rtp_video_sender_->OnBitrateUpdated(update.bitrate_bps, update.fraction_loss,
-                                      update.rtt,
-                                      stats_proxy_->GetSendFrameRate());
+  rtp_video_sender_->OnBitrateUpdated(
+      update.target_bitrate.bps(),
+      rtc::dchecked_cast<uint8_t>(update.packet_loss_ratio * 256),
+      update.round_trip_time.ms(), stats_proxy_->GetSendFrameRate());
   encoder_target_rate_bps_ = rtp_video_sender_->GetPayloadBitrateBps();
   encoder_target_rate_bps_ =
       std::min(encoder_max_bitrate_bps_, encoder_target_rate_bps_);
-  video_stream_encoder_->OnBitrateUpdated(encoder_target_rate_bps_,
-                                          update.fraction_loss, update.rtt);
+  video_stream_encoder_->OnBitrateUpdated(
+      encoder_target_rate_bps_,
+      rtc::dchecked_cast<uint8_t>(update.packet_loss_ratio * 256),
+      update.round_trip_time.ms());
   stats_proxy_->OnSetEncoderTargetRate(encoder_target_rate_bps_);
   return rtp_video_sender_->GetProtectionBitrateBps();
 }
diff --git a/video/video_send_stream_impl_unittest.cc b/video/video_send_stream_impl_unittest.cc
index c93f8ff..32d0df1 100644
--- a/video/video_send_stream_impl_unittest.cc
+++ b/video/video_send_stream_impl_unittest.cc
@@ -72,9 +72,9 @@
 
 BitrateAllocationUpdate CreateAllocation(int bitrate_bps) {
   BitrateAllocationUpdate update;
-  update.bitrate_bps = bitrate_bps;
-  update.fraction_loss = 0;
-  update.rtt = 0;
+  update.target_bitrate = DataRate::bps(bitrate_bps);
+  update.packet_loss_ratio = 0;
+  update.round_trip_time = TimeDelta::Zero();
   return update;
 }
 }  // namespace