Replacing bandwidth adaptation trial with stable target in Opus encoder.

This also means that the NetworkEstimate::bandwidth can be deprecated
as it's currently just a copy of the target_rate.

Bug: webrtc:10981
Change-Id: I1bc57b98480bd77ce052736b19d630c775428546
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/153669
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Oskar Sundbom <ossu@webrtc.org>
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29288}
diff --git a/api/call/bitrate_allocation.h b/api/call/bitrate_allocation.h
index c52969b..24530c9 100644
--- a/api/call/bitrate_allocation.h
+++ b/api/call/bitrate_allocation.h
@@ -32,8 +32,6 @@
   double packet_loss_ratio = 0;
   // Predicted round trip time.
   TimeDelta round_trip_time = TimeDelta::PlusInfinity();
-  // |link_capacity| is deprecated, use |stable_target_bitrate| instead.
-  DataRate link_capacity = DataRate::Zero();
   // |bwe_period| is deprecated, use |stable_target_bitrate| allocation instead.
   TimeDelta bwe_period = TimeDelta::PlusInfinity();
 };
diff --git a/api/transport/BUILD.gn b/api/transport/BUILD.gn
index 9723407..365e5ae 100644
--- a/api/transport/BUILD.gn
+++ b/api/transport/BUILD.gn
@@ -37,6 +37,7 @@
 
   deps = [
     ":webrtc_key_value_config",
+    "../../rtc_base:deprecation",
     "../units:data_rate",
     "../units:data_size",
     "../units:time_delta",
diff --git a/api/transport/network_types.h b/api/transport/network_types.h
index c8c6d3c..320a7c0 100644
--- a/api/transport/network_types.h
+++ b/api/transport/network_types.h
@@ -19,6 +19,7 @@
 #include "api/units/data_size.h"
 #include "api/units/time_delta.h"
 #include "api/units/timestamp.h"
+#include "rtc_base/deprecation.h"
 
 namespace webrtc {
 
@@ -179,6 +180,7 @@
 
 struct NetworkEstimate {
   Timestamp at_time = Timestamp::PlusInfinity();
+  // Deprecated, use TargetTransferRate::target_rate instead.
   DataRate bandwidth = DataRate::Infinity();
   TimeDelta round_trip_time = TimeDelta::PlusInfinity();
   TimeDelta bwe_period = TimeDelta::PlusInfinity();
diff --git a/call/bitrate_allocator.cc b/call/bitrate_allocator.cc
index 5362cee..989f70c 100644
--- a/call/bitrate_allocator.cc
+++ b/call/bitrate_allocator.cc
@@ -56,7 +56,6 @@
     : limit_observer_(limit_observer),
       last_target_bps_(0),
       last_stable_target_bps_(0),
-      last_bandwidth_bps_(0),
       last_non_zero_bitrate_bps_(kDefaultBitrateBps),
       last_fraction_loss_(0),
       last_rtt_(0),
@@ -95,7 +94,6 @@
 void BitrateAllocator::OnNetworkEstimateChanged(TargetTransferRate msg) {
   RTC_DCHECK_RUN_ON(&sequenced_checker_);
   last_target_bps_ = msg.target_rate.bps();
-  last_bandwidth_bps_ = msg.network_estimate.bandwidth.bps();
   last_stable_target_bps_ = msg.stable_target_rate.bps();
   last_non_zero_bitrate_bps_ =
       last_target_bps_ > 0 ? last_target_bps_ : last_non_zero_bitrate_bps_;
@@ -114,20 +112,16 @@
   }
 
   ObserverAllocation allocation = AllocateBitrates(last_target_bps_);
-  ObserverAllocation bandwidth_allocation =
-      AllocateBitrates(last_bandwidth_bps_);
   ObserverAllocation stable_bitrate_allocation =
       AllocateBitrates(last_stable_target_bps_);
 
   for (auto& config : allocatable_tracks_) {
     uint32_t allocated_bitrate = allocation[config.observer];
-    uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
     uint32_t allocated_stable_target_rate =
         stable_bitrate_allocation[config.observer];
     BitrateAllocationUpdate update;
     update.target_bitrate = DataRate::bps(allocated_bitrate);
     update.stable_target_bitrate = DataRate::bps(allocated_stable_target_rate);
-    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_);
@@ -183,19 +177,15 @@
     // Calculate a new allocation and update all observers.
 
     ObserverAllocation allocation = AllocateBitrates(last_target_bps_);
-    ObserverAllocation bandwidth_allocation =
-        AllocateBitrates(last_bandwidth_bps_);
     ObserverAllocation stable_bitrate_allocation =
         AllocateBitrates(last_stable_target_bps_);
     for (auto& config : allocatable_tracks_) {
       uint32_t allocated_bitrate = allocation[config.observer];
       uint32_t allocated_stable_bitrate =
           stable_bitrate_allocation[config.observer];
-      uint32_t bandwidth = bandwidth_allocation[config.observer];
       BitrateAllocationUpdate update;
       update.target_bitrate = DataRate::bps(allocated_bitrate);
       update.stable_target_bitrate = DataRate::bps(allocated_stable_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_);
@@ -212,7 +202,6 @@
     BitrateAllocationUpdate update;
     update.target_bitrate = DataRate::Zero();
     update.stable_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_);
diff --git a/call/bitrate_allocator.h b/call/bitrate_allocator.h
index bad6016..b7d77d9 100644
--- a/call/bitrate_allocator.h
+++ b/call/bitrate_allocator.h
@@ -199,7 +199,6 @@
       RTC_GUARDED_BY(&sequenced_checker_);
   uint32_t last_target_bps_ RTC_GUARDED_BY(&sequenced_checker_);
   uint32_t last_stable_target_bps_ RTC_GUARDED_BY(&sequenced_checker_);
-  uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&sequenced_checker_);
   uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
   uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
   int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
diff --git a/call/call.cc b/call/call.cc
index f816cb5..971ebbd 100644
--- a/call/call.cc
+++ b/call/call.cc
@@ -1065,7 +1065,7 @@
   RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
   {
     rtc::CritScope cs(&last_bandwidth_bps_crit_);
-    last_bandwidth_bps_ = msg.network_estimate.bandwidth.bps();
+    last_bandwidth_bps_ = msg.target_rate.bps();
   }
 
   uint32_t target_bitrate_bps = msg.target_rate.bps();
diff --git a/call/rtp_transport_controller_send.cc b/call/rtp_transport_controller_send.cc
index 3b3394d..4e8d021 100644
--- a/call/rtp_transport_controller_send.cc
+++ b/call/rtp_transport_controller_send.cc
@@ -141,8 +141,7 @@
   absl::optional<TargetTransferRate> update = control_handler_->GetUpdate();
   if (!update)
     return;
-  retransmission_rate_limiter_.SetMaxRate(
-      update->network_estimate.bandwidth.bps());
+  retransmission_rate_limiter_.SetMaxRate(update->target_rate.bps());
   // We won't create control_handler_ until we have an observers.
   RTC_DCHECK(observer_ != nullptr);
   observer_->OnTargetTransferRate(*update);
diff --git a/modules/audio_coding/codecs/opus/audio_encoder_opus.cc b/modules/audio_coding/codecs/opus/audio_encoder_opus.cc
index 70081d7..60af607 100644
--- a/modules/audio_coding/codecs/opus/audio_encoder_opus.cc
+++ b/modules/audio_coding/codecs/opus/audio_encoder_opus.cc
@@ -425,8 +425,8 @@
     : payload_type_(payload_type),
       send_side_bwe_with_overhead_(
           webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
-      use_link_capacity_for_adaptation_(webrtc::field_trial::IsEnabled(
-          "WebRTC-Audio-LinkCapacityAdaptation")),
+      use_stable_target_for_adaptation_(webrtc::field_trial::IsEnabled(
+          "WebRTC-Audio-StableTargetAdaptation")),
       adjust_bandwidth_(
           webrtc::field_trial::IsEnabled("WebRTC-AdjustOpusBandwidth")),
       bitrate_changed_(true),
@@ -563,26 +563,28 @@
 void AudioEncoderOpusImpl::OnReceivedUplinkBandwidth(
     int target_audio_bitrate_bps,
     absl::optional<int64_t> bwe_period_ms,
-    absl::optional<int64_t> link_capacity_allocation_bps) {
+    absl::optional<int64_t> stable_target_bitrate_bps) {
   if (audio_network_adaptor_) {
     audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps);
-    // We give smoothed bitrate allocation to audio network adaptor as
-    // the uplink bandwidth.
-    // The BWE spikes should not affect the bitrate smoother more than 25%.
-    // To simplify the calculations we use a step response as input signal.
-    // The step response of an exponential filter is
-    // u(t) = 1 - e^(-t / time_constant).
-    // In order to limit the affect of a BWE spike within 25% of its value
-    // before
-    // the next BWE update, we would choose a time constant that fulfills
-    // 1 - e^(-bwe_period_ms / time_constant) < 0.25
-    // Then 4 * bwe_period_ms is a good choice.
-    if (bwe_period_ms)
-      bitrate_smoother_->SetTimeConstantMs(*bwe_period_ms * 4);
-    bitrate_smoother_->AddSample(target_audio_bitrate_bps);
-
-    if (link_capacity_allocation_bps)
-      link_capacity_allocation_bps_ = link_capacity_allocation_bps;
+    if (use_stable_target_for_adaptation_) {
+      if (stable_target_bitrate_bps)
+        audio_network_adaptor_->SetUplinkBandwidth(*stable_target_bitrate_bps);
+    } else {
+      // We give smoothed bitrate allocation to audio network adaptor as
+      // the uplink bandwidth.
+      // The BWE spikes should not affect the bitrate smoother more than 25%.
+      // To simplify the calculations we use a step response as input signal.
+      // The step response of an exponential filter is
+      // u(t) = 1 - e^(-t / time_constant).
+      // In order to limit the affect of a BWE spike within 25% of its value
+      // before
+      // the next BWE update, we would choose a time constant that fulfills
+      // 1 - e^(-bwe_period_ms / time_constant) < 0.25
+      // Then 4 * bwe_period_ms is a good choice.
+      if (bwe_period_ms)
+        bitrate_smoother_->SetTimeConstantMs(*bwe_period_ms * 4);
+      bitrate_smoother_->AddSample(target_audio_bitrate_bps);
+    }
 
     ApplyAudioNetworkAdaptor();
   } else if (send_side_bwe_with_overhead_) {
@@ -612,7 +614,7 @@
 void AudioEncoderOpusImpl::OnReceivedUplinkAllocation(
     BitrateAllocationUpdate update) {
   OnReceivedUplinkBandwidth(update.target_bitrate.bps(), update.bwe_period.ms(),
-                            update.link_capacity.bps());
+                            update.stable_target_bitrate.bps());
 }
 
 void AudioEncoderOpusImpl::OnReceivedRtt(int rtt_ms) {
@@ -857,21 +859,15 @@
 }
 
 void AudioEncoderOpusImpl::MaybeUpdateUplinkBandwidth() {
-  if (audio_network_adaptor_) {
-    if (use_link_capacity_for_adaptation_ && link_capacity_allocation_bps_) {
-      audio_network_adaptor_->SetUplinkBandwidth(
-          *link_capacity_allocation_bps_);
-    } else {
-      int64_t now_ms = rtc::TimeMillis();
-      if (!bitrate_smoother_last_update_time_ ||
-          now_ms - *bitrate_smoother_last_update_time_ >=
-              config_.uplink_bandwidth_update_interval_ms) {
-        absl::optional<float> smoothed_bitrate =
-            bitrate_smoother_->GetAverage();
-        if (smoothed_bitrate)
-          audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate);
-        bitrate_smoother_last_update_time_ = now_ms;
-      }
+  if (audio_network_adaptor_ && !use_stable_target_for_adaptation_) {
+    int64_t now_ms = rtc::TimeMillis();
+    if (!bitrate_smoother_last_update_time_ ||
+        now_ms - *bitrate_smoother_last_update_time_ >=
+            config_.uplink_bandwidth_update_interval_ms) {
+      absl::optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage();
+      if (smoothed_bitrate)
+        audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate);
+      bitrate_smoother_last_update_time_ = now_ms;
     }
   }
 }
diff --git a/modules/audio_coding/codecs/opus/audio_encoder_opus.h b/modules/audio_coding/codecs/opus/audio_encoder_opus.h
index 51db661..1f785a4 100644
--- a/modules/audio_coding/codecs/opus/audio_encoder_opus.h
+++ b/modules/audio_coding/codecs/opus/audio_encoder_opus.h
@@ -174,7 +174,7 @@
   AudioEncoderOpusConfig config_;
   const int payload_type_;
   const bool send_side_bwe_with_overhead_;
-  const bool use_link_capacity_for_adaptation_;
+  const bool use_stable_target_for_adaptation_;
   const bool adjust_bandwidth_;
   bool bitrate_changed_;
   float packet_loss_rate_;
@@ -192,7 +192,6 @@
   absl::optional<size_t> overhead_bytes_per_packet_;
   const std::unique_ptr<SmoothingFilter> bitrate_smoother_;
   absl::optional<int64_t> bitrate_smoother_last_update_time_;
-  absl::optional<int64_t> link_capacity_allocation_bps_;
   int consecutive_dtx_frames_;
 
   friend struct AudioEncoderOpus;
diff --git a/modules/congestion_controller/bbr/bbr_network_controller.cc b/modules/congestion_controller/bbr/bbr_network_controller.cc
index c64152c..6d66af12 100644
--- a/modules/congestion_controller/bbr/bbr_network_controller.cc
+++ b/modules/congestion_controller/bbr/bbr_network_controller.cc
@@ -267,7 +267,6 @@
 
   TargetTransferRate target_rate_msg;
   target_rate_msg.network_estimate.at_time = at_time;
-  target_rate_msg.network_estimate.bandwidth = bandwidth;
   target_rate_msg.network_estimate.round_trip_time = rtt;
 
   // TODO(srte): Fill in field below with proper value.
diff --git a/modules/congestion_controller/goog_cc/goog_cc_network_control.cc b/modules/congestion_controller/goog_cc/goog_cc_network_control.cc
index fea7fc3..78b1236 100644
--- a/modules/congestion_controller/goog_cc/goog_cc_network_control.cc
+++ b/modules/congestion_controller/goog_cc/goog_cc_network_control.cc
@@ -572,7 +572,6 @@
   NetworkControlUpdate update;
   update.target_rate = TargetTransferRate();
   update.target_rate->network_estimate.at_time = at_time;
-  update.target_rate->network_estimate.bandwidth = last_raw_target_rate_;
   update.target_rate->network_estimate.loss_rate_ratio =
       last_estimated_fraction_loss_ / 255.0;
   update.target_rate->network_estimate.round_trip_time = rtt;
@@ -635,7 +634,6 @@
         bandwidth_estimation_->GetEstimatedLinkCapacity(), target_rate);
     target_rate_msg.network_estimate.at_time = at_time;
     target_rate_msg.network_estimate.round_trip_time = TimeDelta::ms(rtt_ms);
-    target_rate_msg.network_estimate.bandwidth = last_raw_target_rate_;
     target_rate_msg.network_estimate.loss_rate_ratio = fraction_loss / 255.0f;
     target_rate_msg.network_estimate.bwe_period = bwe_period;
 
diff --git a/modules/congestion_controller/goog_cc/goog_cc_network_control_unittest.cc b/modules/congestion_controller/goog_cc/goog_cc_network_control_unittest.cc
index 0da3410..4404ae8 100644
--- a/modules/congestion_controller/goog_cc/goog_cc_network_control_unittest.cc
+++ b/modules/congestion_controller/goog_cc/goog_cc_network_control_unittest.cc
@@ -509,7 +509,7 @@
   // Measure variation in steady state.
   for (int i = 0; i < 20; ++i) {
     auto stable_target_rate = client->stable_target_rate();
-    auto target_rate = client->link_capacity();
+    auto target_rate = client->target_rate();
     EXPECT_LE(stable_target_rate, target_rate);
 
     min_stable_target = std::min(min_stable_target, stable_target_rate);
diff --git a/modules/congestion_controller/goog_cc/test/goog_cc_printer.cc b/modules/congestion_controller/goog_cc/test/goog_cc_printer.cc
index f8f984c..a0b3f37 100644
--- a/modules/congestion_controller/goog_cc/test/goog_cc_printer.cc
+++ b/modules/congestion_controller/goog_cc/test/goog_cc_printer.cc
@@ -91,7 +91,6 @@
   };
   std::deque<FieldLogger*> loggers({
       Log("time", [=] { return target_.at_time; }),
-      Log("bandwidth", [=] { return target_.network_estimate.bandwidth; }),
       Log("rtt", [=] { return target_.network_estimate.round_trip_time; }),
       Log("target", [=] { return target_.target_rate; }),
       Log("pacing", [=] { return pacing_.data_rate(); }),
diff --git a/modules/congestion_controller/pcc/pcc_network_controller.cc b/modules/congestion_controller/pcc/pcc_network_controller.cc
index 169b147..9f074af 100644
--- a/modules/congestion_controller/pcc/pcc_network_controller.cc
+++ b/modules/congestion_controller/pcc/pcc_network_controller.cc
@@ -104,7 +104,6 @@
   target_rate_msg.at_time = at_time;
   target_rate_msg.network_estimate.at_time = at_time;
   target_rate_msg.network_estimate.round_trip_time = rtt_tracker_.GetRtt();
-  target_rate_msg.network_estimate.bandwidth = bandwidth_estimate_;
   // TODO(koloskova): Add correct estimate.
   target_rate_msg.network_estimate.loss_rate_ratio = 0;
   target_rate_msg.network_estimate.bwe_period =
diff --git a/test/scenario/call_client.cc b/test/scenario/call_client.cc
index 7118e2d..9293d01 100644
--- a/test/scenario/call_client.cc
+++ b/test/scenario/call_client.cc
@@ -251,11 +251,6 @@
   return network_controller_factory_.GetUpdate().target_rate->target_rate;
 }
 
-DataRate CallClient::link_capacity() const {
-  return network_controller_factory_.GetUpdate()
-      .target_rate->network_estimate.bandwidth;
-}
-
 DataRate CallClient::stable_target_rate() const {
   return network_controller_factory_.GetUpdate()
       .target_rate->stable_target_rate;
diff --git a/test/scenario/call_client.h b/test/scenario/call_client.h
index 78c302d..19cafe8 100644
--- a/test/scenario/call_client.h
+++ b/test/scenario/call_client.h
@@ -108,7 +108,6 @@
   }
   DataRate target_rate() const;
   DataRate stable_target_rate() const;
-  DataRate link_capacity() const;
   DataRate padding_rate() const;
 
   void OnPacketReceived(EmulatedIpPacket packet) override;