xref: /aosp_15_r20/external/webrtc/api/field_trials.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "api/field_trials.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <atomic>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
16*d9f75844SAndroid Build Coastguard Worker #include "system_wrappers/include/field_trial.h"
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker namespace {
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker // This part is copied from system_wrappers/field_trial.cc.
InsertIntoMap(const std::string & s)21*d9f75844SAndroid Build Coastguard Worker webrtc::flat_map<std::string, std::string> InsertIntoMap(const std::string& s) {
22*d9f75844SAndroid Build Coastguard Worker   std::string::size_type field_start = 0;
23*d9f75844SAndroid Build Coastguard Worker   webrtc::flat_map<std::string, std::string> key_value_map;
24*d9f75844SAndroid Build Coastguard Worker   while (field_start < s.size()) {
25*d9f75844SAndroid Build Coastguard Worker     std::string::size_type separator_pos = s.find('/', field_start);
26*d9f75844SAndroid Build Coastguard Worker     RTC_CHECK_NE(separator_pos, std::string::npos)
27*d9f75844SAndroid Build Coastguard Worker         << "Missing separator '/' after field trial key.";
28*d9f75844SAndroid Build Coastguard Worker     RTC_CHECK_GT(separator_pos, field_start)
29*d9f75844SAndroid Build Coastguard Worker         << "Field trial key cannot be empty.";
30*d9f75844SAndroid Build Coastguard Worker     std::string key = s.substr(field_start, separator_pos - field_start);
31*d9f75844SAndroid Build Coastguard Worker     field_start = separator_pos + 1;
32*d9f75844SAndroid Build Coastguard Worker 
33*d9f75844SAndroid Build Coastguard Worker     RTC_CHECK_LT(field_start, s.size())
34*d9f75844SAndroid Build Coastguard Worker         << "Missing value after field trial key. String ended.";
35*d9f75844SAndroid Build Coastguard Worker     separator_pos = s.find('/', field_start);
36*d9f75844SAndroid Build Coastguard Worker     RTC_CHECK_NE(separator_pos, std::string::npos)
37*d9f75844SAndroid Build Coastguard Worker         << "Missing terminating '/' in field trial string.";
38*d9f75844SAndroid Build Coastguard Worker     RTC_CHECK_GT(separator_pos, field_start)
39*d9f75844SAndroid Build Coastguard Worker         << "Field trial value cannot be empty.";
40*d9f75844SAndroid Build Coastguard Worker     std::string value = s.substr(field_start, separator_pos - field_start);
41*d9f75844SAndroid Build Coastguard Worker     field_start = separator_pos + 1;
42*d9f75844SAndroid Build Coastguard Worker 
43*d9f75844SAndroid Build Coastguard Worker     // If a key is specified multiple times, only the value linked to the first
44*d9f75844SAndroid Build Coastguard Worker     // key is stored. note: This will crash in debug build when calling
45*d9f75844SAndroid Build Coastguard Worker     // InitFieldTrialsFromString().
46*d9f75844SAndroid Build Coastguard Worker     key_value_map.emplace(key, value);
47*d9f75844SAndroid Build Coastguard Worker   }
48*d9f75844SAndroid Build Coastguard Worker   // This check is technically redundant due to earlier checks.
49*d9f75844SAndroid Build Coastguard Worker   // We nevertheless keep the check to make it clear that the entire
50*d9f75844SAndroid Build Coastguard Worker   // string has been processed, and without indexing past the end.
51*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK_EQ(field_start, s.size());
52*d9f75844SAndroid Build Coastguard Worker 
53*d9f75844SAndroid Build Coastguard Worker   return key_value_map;
54*d9f75844SAndroid Build Coastguard Worker }
55*d9f75844SAndroid Build Coastguard Worker 
56*d9f75844SAndroid Build Coastguard Worker // Makes sure that only one instance is created, since the usage
57*d9f75844SAndroid Build Coastguard Worker // of global string makes behaviour unpredicatable otherwise.
58*d9f75844SAndroid Build Coastguard Worker // TODO(bugs.webrtc.org/10335): Remove once global string is gone.
59*d9f75844SAndroid Build Coastguard Worker std::atomic<bool> instance_created_{false};
60*d9f75844SAndroid Build Coastguard Worker 
61*d9f75844SAndroid Build Coastguard Worker }  // namespace
62*d9f75844SAndroid Build Coastguard Worker 
63*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
64*d9f75844SAndroid Build Coastguard Worker 
FieldTrials(const std::string & s)65*d9f75844SAndroid Build Coastguard Worker FieldTrials::FieldTrials(const std::string& s)
66*d9f75844SAndroid Build Coastguard Worker     : uses_global_(true),
67*d9f75844SAndroid Build Coastguard Worker       field_trial_string_(s),
68*d9f75844SAndroid Build Coastguard Worker       previous_field_trial_string_(webrtc::field_trial::GetFieldTrialString()),
69*d9f75844SAndroid Build Coastguard Worker       key_value_map_(InsertIntoMap(s)) {
70*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/10335): Remove the global string!
71*d9f75844SAndroid Build Coastguard Worker   field_trial::InitFieldTrialsFromString(field_trial_string_.c_str());
72*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(!instance_created_.exchange(true))
73*d9f75844SAndroid Build Coastguard Worker       << "Only one instance may be instanciated at any given time!";
74*d9f75844SAndroid Build Coastguard Worker }
75*d9f75844SAndroid Build Coastguard Worker 
CreateNoGlobal(const std::string & s)76*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<FieldTrials> FieldTrials::CreateNoGlobal(const std::string& s) {
77*d9f75844SAndroid Build Coastguard Worker   return std::unique_ptr<FieldTrials>(new FieldTrials(s, true));
78*d9f75844SAndroid Build Coastguard Worker }
79*d9f75844SAndroid Build Coastguard Worker 
FieldTrials(const std::string & s,bool)80*d9f75844SAndroid Build Coastguard Worker FieldTrials::FieldTrials(const std::string& s, bool)
81*d9f75844SAndroid Build Coastguard Worker     : uses_global_(false),
82*d9f75844SAndroid Build Coastguard Worker       previous_field_trial_string_(nullptr),
83*d9f75844SAndroid Build Coastguard Worker       key_value_map_(InsertIntoMap(s)) {}
84*d9f75844SAndroid Build Coastguard Worker 
~FieldTrials()85*d9f75844SAndroid Build Coastguard Worker FieldTrials::~FieldTrials() {
86*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/10335): Remove the global string!
87*d9f75844SAndroid Build Coastguard Worker   if (uses_global_) {
88*d9f75844SAndroid Build Coastguard Worker     field_trial::InitFieldTrialsFromString(previous_field_trial_string_);
89*d9f75844SAndroid Build Coastguard Worker     RTC_CHECK(instance_created_.exchange(false));
90*d9f75844SAndroid Build Coastguard Worker   }
91*d9f75844SAndroid Build Coastguard Worker }
92*d9f75844SAndroid Build Coastguard Worker 
GetValue(absl::string_view key) const93*d9f75844SAndroid Build Coastguard Worker std::string FieldTrials::GetValue(absl::string_view key) const {
94*d9f75844SAndroid Build Coastguard Worker   auto it = key_value_map_.find(std::string(key));
95*d9f75844SAndroid Build Coastguard Worker   if (it != key_value_map_.end())
96*d9f75844SAndroid Build Coastguard Worker     return it->second;
97*d9f75844SAndroid Build Coastguard Worker 
98*d9f75844SAndroid Build Coastguard Worker   // Check the global string so that programs using
99*d9f75844SAndroid Build Coastguard Worker   // a mix between FieldTrials and the global string continue to work
100*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/10335): Remove the global string!
101*d9f75844SAndroid Build Coastguard Worker   if (uses_global_) {
102*d9f75844SAndroid Build Coastguard Worker     return field_trial::FindFullName(std::string(key));
103*d9f75844SAndroid Build Coastguard Worker   }
104*d9f75844SAndroid Build Coastguard Worker   return "";
105*d9f75844SAndroid Build Coastguard Worker }
106*d9f75844SAndroid Build Coastguard Worker 
107*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
108