Use struct parser for AlrDetector config.

Bug: webrtc:9883
Change-Id: Ib58fa5ba87607a268f4960898625b1a5adcab69a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/148596
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28862}
diff --git a/modules/congestion_controller/goog_cc/alr_detector.cc b/modules/congestion_controller/goog_cc/alr_detector.cc
index 8dfcb1a..2ee77b0 100644
--- a/modules/congestion_controller/goog_cc/alr_detector.cc
+++ b/modules/congestion_controller/goog_cc/alr_detector.cc
@@ -24,7 +24,7 @@
 namespace webrtc {
 
 namespace {
-absl::optional<AlrExperimentSettings> GetExperimentSettings(
+AlrDetectorConfig GetConfigFromTrials(
     const WebRtcKeyValueConfig* key_value_config) {
   RTC_CHECK(AlrExperimentSettings::MaxOneFieldTrialEnabled(*key_value_config));
   absl::optional<AlrExperimentSettings> experiment_settings =
@@ -36,47 +36,37 @@
         *key_value_config,
         AlrExperimentSettings::kStrictPacingAndProbingExperimentName);
   }
-  return experiment_settings;
+  AlrDetectorConfig conf;
+  if (experiment_settings) {
+    conf.bandwidth_usage_ratio =
+        experiment_settings->alr_bandwidth_usage_percent / 100.0;
+    conf.start_budget_level_ratio =
+        experiment_settings->alr_start_budget_level_percent / 100.0;
+    conf.stop_budget_level_ratio =
+        experiment_settings->alr_stop_budget_level_percent / 100.0;
+  }
+  conf.Parser()->Parse(
+      key_value_config->Lookup("WebRTC-AlrDetectorParameters"));
+  return conf;
 }
 }  //  namespace
 
+std::unique_ptr<StructParametersParser> AlrDetectorConfig::Parser() {
+  return StructParametersParser::Create(   //
+      "bw_usage", &bandwidth_usage_ratio,  //
+      "start", &start_budget_level_ratio,  //
+      "stop", &stop_budget_level_ratio);
+}
+
+AlrDetector::AlrDetector(AlrDetectorConfig config, RtcEventLog* event_log)
+    : conf_(config), alr_budget_(0, true), event_log_(event_log) {}
+
 AlrDetector::AlrDetector(const WebRtcKeyValueConfig* key_value_config)
-    : AlrDetector(key_value_config,
-                  nullptr,
-                  GetExperimentSettings(key_value_config)) {}
+    : AlrDetector(GetConfigFromTrials(key_value_config), nullptr) {}
 
 AlrDetector::AlrDetector(const WebRtcKeyValueConfig* key_value_config,
                          RtcEventLog* event_log)
-    : AlrDetector(key_value_config,
-                  event_log,
-                  GetExperimentSettings(key_value_config)) {}
-
-AlrDetector::AlrDetector(
-    const WebRtcKeyValueConfig* key_value_config,
-    RtcEventLog* event_log,
-    absl::optional<AlrExperimentSettings> experiment_settings)
-    : bandwidth_usage_ratio_(
-          "bw_usage",
-          experiment_settings
-              ? experiment_settings->alr_bandwidth_usage_percent / 100.0
-              : kDefaultBandwidthUsageRatio),
-      start_budget_level_ratio_(
-          "start",
-          experiment_settings
-              ? experiment_settings->alr_start_budget_level_percent / 100.0
-              : kDefaultStartBudgetLevelRatio),
-      stop_budget_level_ratio_(
-          "stop",
-          experiment_settings
-              ? experiment_settings->alr_stop_budget_level_percent / 100.0
-              : kDefaultStopBudgetLevelRatio),
-      alr_budget_(0, true),
-      event_log_(event_log) {
-  ParseFieldTrial({&bandwidth_usage_ratio_, &start_budget_level_ratio_,
-                   &stop_budget_level_ratio_},
-                  key_value_config->Lookup("WebRTC-AlrDetectorParameters"));
-}
-
+    : AlrDetector(GetConfigFromTrials(key_value_config), event_log) {}
 AlrDetector::~AlrDetector() {}
 
 void AlrDetector::OnBytesSent(size_t bytes_sent, int64_t send_time_ms) {
@@ -92,11 +82,11 @@
   alr_budget_.UseBudget(bytes_sent);
   alr_budget_.IncreaseBudget(delta_time_ms);
   bool state_changed = false;
-  if (alr_budget_.budget_ratio() > start_budget_level_ratio_ &&
+  if (alr_budget_.budget_ratio() > conf_.start_budget_level_ratio &&
       !alr_started_time_ms_) {
     alr_started_time_ms_.emplace(rtc::TimeMillis());
     state_changed = true;
-  } else if (alr_budget_.budget_ratio() < stop_budget_level_ratio_ &&
+  } else if (alr_budget_.budget_ratio() < conf_.stop_budget_level_ratio &&
              alr_started_time_ms_) {
     state_changed = true;
     alr_started_time_ms_.reset();
@@ -110,7 +100,7 @@
 void AlrDetector::SetEstimatedBitrate(int bitrate_bps) {
   RTC_DCHECK(bitrate_bps);
   int target_rate_kbps =
-      static_cast<double>(bitrate_bps) * bandwidth_usage_ratio_ / 1000;
+      static_cast<double>(bitrate_bps) * conf_.bandwidth_usage_ratio / 1000;
   alr_budget_.set_target_rate_kbps(target_rate_kbps);
 }
 
diff --git a/modules/congestion_controller/goog_cc/alr_detector.h b/modules/congestion_controller/goog_cc/alr_detector.h
index 364294f..d83ed76 100644
--- a/modules/congestion_controller/goog_cc/alr_detector.h
+++ b/modules/congestion_controller/goog_cc/alr_detector.h
@@ -13,17 +13,29 @@
 
 #include <stddef.h>
 #include <stdint.h>
+#include <memory>
 
 #include "absl/types/optional.h"
 #include "api/transport/webrtc_key_value_config.h"
 #include "modules/pacing/interval_budget.h"
 #include "rtc_base/experiments/alr_experiment.h"
-#include "rtc_base/experiments/field_trial_units.h"
+#include "rtc_base/experiments/struct_parameters_parser.h"
 
 namespace webrtc {
 
 class RtcEventLog;
 
+struct AlrDetectorConfig {
+  // Sent traffic ratio as a function of network capacity used to determine
+  // application-limited region. ALR region start when bandwidth usage drops
+  // below kAlrStartUsageRatio and ends when it raises above
+  // kAlrEndUsageRatio. NOTE: This is intentionally conservative at the moment
+  // until BW adjustments of application limited region is fine tuned.
+  double bandwidth_usage_ratio = 0.65;
+  double start_budget_level_ratio = 0.80;
+  double stop_budget_level_ratio = 0.50;
+  std::unique_ptr<StructParametersParser> Parser();
+};
 // Application limited region detector is a class that utilizes signals of
 // elapsed time and bytes sent to estimate whether network traffic is
 // currently limited by the application's ability to generate traffic.
@@ -33,6 +45,7 @@
 // Note: This class is not thread-safe.
 class AlrDetector {
  public:
+  AlrDetector(AlrDetectorConfig config, RtcEventLog* event_log);
   explicit AlrDetector(const WebRtcKeyValueConfig* key_value_config);
   AlrDetector(const WebRtcKeyValueConfig* key_value_config,
               RtcEventLog* event_log);
@@ -51,23 +64,8 @@
   void UpdateBudgetWithBytesSent(size_t bytes_sent);
 
  private:
-  // Sent traffic ratio as a function of network capacity used to determine
-  // application-limited region. ALR region start when bandwidth usage drops
-  // below kAlrStartUsageRatio and ends when it raises above
-  // kAlrEndUsageRatio. NOTE: This is intentionally conservative at the moment
-  // until BW adjustments of application limited region is fine tuned.
-  static constexpr double kDefaultBandwidthUsageRatio = 0.65;
-  static constexpr double kDefaultStartBudgetLevelRatio = 0.80;
-  static constexpr double kDefaultStopBudgetLevelRatio = 0.50;
-
-  AlrDetector(const WebRtcKeyValueConfig* key_value_config,
-              RtcEventLog* event_log,
-              absl::optional<AlrExperimentSettings> experiment_settings);
-
   friend class GoogCcStatePrinter;
-  FieldTrialParameter<double> bandwidth_usage_ratio_;
-  FieldTrialParameter<double> start_budget_level_ratio_;
-  FieldTrialParameter<double> stop_budget_level_ratio_;
+  const AlrDetectorConfig conf_;
 
   absl::optional<int64_t> last_send_time_ms_;