Reset loss based BWE on route change.

The change is under field trial use_in_start_phase.

Bug: webrtc:12707
Change-Id: I2ba8245c5d126b3c8a2e54b826853d98aad6e4f9
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/325184
Commit-Queue: Diep Bui <diepbp@webrtc.org>
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41047}
diff --git a/modules/congestion_controller/goog_cc/loss_based_bwe_v2.cc b/modules/congestion_controller/goog_cc/loss_based_bwe_v2.cc
index ab8fbc2..1b112e9 100644
--- a/modules/congestion_controller/goog_cc/loss_based_bwe_v2.cc
+++ b/modules/congestion_controller/goog_cc/loss_based_bwe_v2.cc
@@ -149,6 +149,10 @@
   return IsReady() && config_->use_in_start_phase;
 }
 
+bool LossBasedBweV2::UseInStartPhase() const {
+  return config_->use_in_start_phase;
+}
+
 LossBasedBweV2::Result LossBasedBweV2::GetLossBasedResult() const {
   if (!IsReady()) {
     if (!IsEnabled()) {
diff --git a/modules/congestion_controller/goog_cc/loss_based_bwe_v2.h b/modules/congestion_controller/goog_cc/loss_based_bwe_v2.h
index 425ca2a..70da76d 100644
--- a/modules/congestion_controller/goog_cc/loss_based_bwe_v2.h
+++ b/modules/congestion_controller/goog_cc/loss_based_bwe_v2.h
@@ -62,6 +62,9 @@
   // Returns true if loss based BWE is ready to be used in the start phase.
   bool ReadyToUseInStartPhase() const;
 
+  // Returns true if loss based BWE can be used in the start phase.
+  bool UseInStartPhase() const;
+
   // Returns `DataRate::PlusInfinity` if no BWE can be calculated.
   Result GetLossBasedResult() const;
 
diff --git a/modules/congestion_controller/goog_cc/send_side_bandwidth_estimation.cc b/modules/congestion_controller/goog_cc/send_side_bandwidth_estimation.cc
index b09cb22..22693d6 100644
--- a/modules/congestion_controller/goog_cc/send_side_bandwidth_estimation.cc
+++ b/modules/congestion_controller/goog_cc/send_side_bandwidth_estimation.cc
@@ -203,9 +203,9 @@
 RttBasedBackoff::~RttBasedBackoff() = default;
 
 SendSideBandwidthEstimation::SendSideBandwidthEstimation(
-    const FieldTrialsView* key_value_config,
-    RtcEventLog* event_log)
-    : rtt_backoff_(key_value_config),
+    const FieldTrialsView* key_value_config, RtcEventLog* event_log)
+    : key_value_config_(key_value_config),
+      rtt_backoff_(key_value_config),
       lost_packets_since_last_loss_update_(0),
       expected_packets_since_last_loss_update_(0),
       current_target_(DataRate::Zero()),
@@ -234,7 +234,7 @@
       high_loss_threshold_(kDefaultHighLossThreshold),
       bitrate_threshold_(kDefaultBitrateThreshold),
       loss_based_bandwidth_estimator_v1_(key_value_config),
-      loss_based_bandwidth_estimator_v2_(key_value_config),
+      loss_based_bandwidth_estimator_v2_(new LossBasedBweV2(key_value_config)),
       loss_based_state_(LossBasedState::kDelayBasedEstimate),
       disable_receiver_limit_caps_only_("Disabled") {
   RTC_DCHECK(event_log);
@@ -252,7 +252,7 @@
   ParseFieldTrial({&disable_receiver_limit_caps_only_},
                   key_value_config->Lookup("WebRTC-Bwe-ReceiverLimitCapsOnly"));
   if (LossBasedBandwidthEstimatorV2Enabled()) {
-    loss_based_bandwidth_estimator_v2_.SetMinMaxBitrate(
+    loss_based_bandwidth_estimator_v2_->SetMinMaxBitrate(
         min_bitrate_configured_, max_bitrate_configured_);
   }
 }
@@ -281,6 +281,10 @@
   uma_update_state_ = kNoUpdate;
   uma_rtt_state_ = kNoUpdate;
   last_rtc_event_log_ = Timestamp::MinusInfinity();
+  if (loss_based_bandwidth_estimator_v2_->UseInStartPhase()) {
+    loss_based_bandwidth_estimator_v2_.reset(
+        new LossBasedBweV2(key_value_config_));
+  }
 }
 
 void SendSideBandwidthEstimation::SetBitrates(
@@ -315,8 +319,8 @@
   } else {
     max_bitrate_configured_ = kDefaultMaxBitrate;
   }
-  loss_based_bandwidth_estimator_v2_.SetMinMaxBitrate(min_bitrate_configured_,
-                                                      max_bitrate_configured_);
+  loss_based_bandwidth_estimator_v2_->SetMinMaxBitrate(min_bitrate_configured_,
+                                                       max_bitrate_configured_);
 }
 
 int SendSideBandwidthEstimation::GetMinBitrate() const {
@@ -371,7 +375,7 @@
         *acknowledged_rate, at_time);
   }
   if (LossBasedBandwidthEstimatorV2Enabled()) {
-    loss_based_bandwidth_estimator_v2_.SetAcknowledgedBitrate(
+    loss_based_bandwidth_estimator_v2_->SetAcknowledgedBitrate(
         *acknowledged_rate);
   }
 }
@@ -386,7 +390,7 @@
         report.packet_feedbacks, report.feedback_time);
   }
   if (LossBasedBandwidthEstimatorV2Enabled()) {
-    loss_based_bandwidth_estimator_v2_.UpdateBandwidthEstimate(
+    loss_based_bandwidth_estimator_v2_->UpdateBandwidthEstimate(
         report.packet_feedbacks, delay_based_limit_, in_alr);
     UpdateEstimate(report.feedback_time);
   }
@@ -492,7 +496,7 @@
   // We trust the REMB and/or delay-based estimate during the first 2 seconds if
   // we haven't had any packet loss reported, to allow startup bitrate probing.
   if (last_fraction_loss_ == 0 && IsInStartPhase(at_time) &&
-      !loss_based_bandwidth_estimator_v2_.ReadyToUseInStartPhase()) {
+      !loss_based_bandwidth_estimator_v2_->ReadyToUseInStartPhase()) {
     DataRate new_bitrate = current_target_;
     // TODO(srte): We should not allow the new_bitrate to be larger than the
     // receiver limit here.
@@ -534,7 +538,7 @@
 
   if (LossBasedBandwidthEstimatorV2ReadyForUse()) {
     LossBasedBweV2::Result result =
-        loss_based_bandwidth_estimator_v2_.GetLossBasedResult();
+        loss_based_bandwidth_estimator_v2_->GetLossBasedResult();
     loss_based_state_ = result.state;
     UpdateTargetBitrate(result.bandwidth_estimate, at_time);
     return;
@@ -690,13 +694,13 @@
 }
 
 bool SendSideBandwidthEstimation::LossBasedBandwidthEstimatorV2Enabled() const {
-  return loss_based_bandwidth_estimator_v2_.IsEnabled();
+  return loss_based_bandwidth_estimator_v2_->IsEnabled();
 }
 
 bool SendSideBandwidthEstimation::LossBasedBandwidthEstimatorV2ReadyForUse()
     const {
   return LossBasedBandwidthEstimatorV2Enabled() &&
-         loss_based_bandwidth_estimator_v2_.IsReady();
+         loss_based_bandwidth_estimator_v2_->IsReady();
 }
 
 }  // namespace webrtc
diff --git a/modules/congestion_controller/goog_cc/send_side_bandwidth_estimation.h b/modules/congestion_controller/goog_cc/send_side_bandwidth_estimation.h
index 3a4efc4..dd4d25a 100644
--- a/modules/congestion_controller/goog_cc/send_side_bandwidth_estimation.h
+++ b/modules/congestion_controller/goog_cc/send_side_bandwidth_estimation.h
@@ -16,6 +16,7 @@
 #include <stdint.h>
 
 #include <deque>
+#include <memory>
 #include <utility>
 #include <vector>
 
@@ -167,6 +168,7 @@
   bool LossBasedBandwidthEstimatorV1ReadyForUse() const;
   bool LossBasedBandwidthEstimatorV2ReadyForUse() const;
 
+  const FieldTrialsView* key_value_config_;
   RttBasedBackoff rtt_backoff_;
   LinkCapacityTracker link_capacity_;
 
@@ -208,7 +210,7 @@
   float high_loss_threshold_;
   DataRate bitrate_threshold_;
   LossBasedBandwidthEstimation loss_based_bandwidth_estimator_v1_;
-  LossBasedBweV2 loss_based_bandwidth_estimator_v2_;
+  std::unique_ptr<LossBasedBweV2> loss_based_bandwidth_estimator_v2_;
   LossBasedState loss_based_state_;
   FieldTrialFlag disable_receiver_limit_caps_only_;
 };