xref: /aosp_15_r20/external/webrtc/system_wrappers/include/field_trial.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker //
2*d9f75844SAndroid Build Coastguard Worker // Copyright (c) 2014 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 #ifndef SYSTEM_WRAPPERS_INCLUDE_FIELD_TRIAL_H_
12*d9f75844SAndroid Build Coastguard Worker #define SYSTEM_WRAPPERS_INCLUDE_FIELD_TRIAL_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <string>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
17*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/containers/flat_set.h"
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker // Field trials allow webrtc clients (such as Chrome) to turn on feature code
20*d9f75844SAndroid Build Coastguard Worker // in binaries out in the field and gather information with that.
21*d9f75844SAndroid Build Coastguard Worker //
22*d9f75844SAndroid Build Coastguard Worker // By default WebRTC provides an implementation of field trials that can be
23*d9f75844SAndroid Build Coastguard Worker // found in system_wrappers/source/field_trial.cc. If clients want to provide
24*d9f75844SAndroid Build Coastguard Worker // a custom version, they will have to:
25*d9f75844SAndroid Build Coastguard Worker //
26*d9f75844SAndroid Build Coastguard Worker // 1. Compile WebRTC defining the preprocessor macro
27*d9f75844SAndroid Build Coastguard Worker //    WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT (if GN is used this can be achieved
28*d9f75844SAndroid Build Coastguard Worker //    by setting the GN arg rtc_exclude_field_trial_default to true).
29*d9f75844SAndroid Build Coastguard Worker // 2. Provide an implementation of:
30*d9f75844SAndroid Build Coastguard Worker //    std::string webrtc::field_trial::FindFullName(absl::string_view trial).
31*d9f75844SAndroid Build Coastguard Worker //
32*d9f75844SAndroid Build Coastguard Worker // They are designed to wire up directly to chrome field trials and to speed up
33*d9f75844SAndroid Build Coastguard Worker // developers by reducing the need to wire APIs to control whether a feature is
34*d9f75844SAndroid Build Coastguard Worker // on/off. E.g. to experiment with a new method that could lead to a different
35*d9f75844SAndroid Build Coastguard Worker // trade-off between CPU/bandwidth:
36*d9f75844SAndroid Build Coastguard Worker //
37*d9f75844SAndroid Build Coastguard Worker // 1 - Develop the feature with default behaviour off:
38*d9f75844SAndroid Build Coastguard Worker //
39*d9f75844SAndroid Build Coastguard Worker //   if (FieldTrial::FindFullName("WebRTCExperimentMethod2") == "Enabled")
40*d9f75844SAndroid Build Coastguard Worker //     method2();
41*d9f75844SAndroid Build Coastguard Worker //   else
42*d9f75844SAndroid Build Coastguard Worker //     method1();
43*d9f75844SAndroid Build Coastguard Worker //
44*d9f75844SAndroid Build Coastguard Worker // 2 - Once the changes are rolled to chrome, the new code path can be
45*d9f75844SAndroid Build Coastguard Worker //     controlled as normal chrome field trials.
46*d9f75844SAndroid Build Coastguard Worker //
47*d9f75844SAndroid Build Coastguard Worker // 3 - Evaluate the new feature and clean the code paths.
48*d9f75844SAndroid Build Coastguard Worker //
49*d9f75844SAndroid Build Coastguard Worker // Notes:
50*d9f75844SAndroid Build Coastguard Worker //   - NOT every feature is a candidate to be controlled by this mechanism as
51*d9f75844SAndroid Build Coastguard Worker //     it may require negotiation between involved parties (e.g. SDP).
52*d9f75844SAndroid Build Coastguard Worker //
53*d9f75844SAndroid Build Coastguard Worker // TODO(andresp): since chrome --force-fieldtrials does not marks the trial
54*d9f75844SAndroid Build Coastguard Worker //     as active it does not get propagated to the renderer process. For now one
55*d9f75844SAndroid Build Coastguard Worker //     needs to push a config with start_active:true or run a local finch
56*d9f75844SAndroid Build Coastguard Worker //     server.
57*d9f75844SAndroid Build Coastguard Worker //
58*d9f75844SAndroid Build Coastguard Worker // TODO(andresp): find out how to get bots to run tests with trials enabled.
59*d9f75844SAndroid Build Coastguard Worker 
60*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
61*d9f75844SAndroid Build Coastguard Worker namespace field_trial {
62*d9f75844SAndroid Build Coastguard Worker 
63*d9f75844SAndroid Build Coastguard Worker // Returns the group name chosen for the named trial, or the empty string
64*d9f75844SAndroid Build Coastguard Worker // if the trial does not exists.
65*d9f75844SAndroid Build Coastguard Worker //
66*d9f75844SAndroid Build Coastguard Worker // Note: To keep things tidy append all the trial names with WebRTC.
67*d9f75844SAndroid Build Coastguard Worker std::string FindFullName(absl::string_view name);
68*d9f75844SAndroid Build Coastguard Worker 
69*d9f75844SAndroid Build Coastguard Worker // Convenience method, returns true iff FindFullName(name) return a string that
70*d9f75844SAndroid Build Coastguard Worker // starts with "Enabled".
71*d9f75844SAndroid Build Coastguard Worker // TODO(tommi): Make sure all implementations support this.
IsEnabled(absl::string_view name)72*d9f75844SAndroid Build Coastguard Worker inline bool IsEnabled(absl::string_view name) {
73*d9f75844SAndroid Build Coastguard Worker   return FindFullName(name).find("Enabled") == 0;
74*d9f75844SAndroid Build Coastguard Worker }
75*d9f75844SAndroid Build Coastguard Worker 
76*d9f75844SAndroid Build Coastguard Worker // Convenience method, returns true iff FindFullName(name) return a string that
77*d9f75844SAndroid Build Coastguard Worker // starts with "Disabled".
IsDisabled(absl::string_view name)78*d9f75844SAndroid Build Coastguard Worker inline bool IsDisabled(absl::string_view name) {
79*d9f75844SAndroid Build Coastguard Worker   return FindFullName(name).find("Disabled") == 0;
80*d9f75844SAndroid Build Coastguard Worker }
81*d9f75844SAndroid Build Coastguard Worker 
82*d9f75844SAndroid Build Coastguard Worker // Optionally initialize field trial from a string.
83*d9f75844SAndroid Build Coastguard Worker // This method can be called at most once before any other call into webrtc.
84*d9f75844SAndroid Build Coastguard Worker // E.g. before the peer connection factory is constructed.
85*d9f75844SAndroid Build Coastguard Worker // Note: trials_string must never be destroyed.
86*d9f75844SAndroid Build Coastguard Worker void InitFieldTrialsFromString(const char* trials_string);
87*d9f75844SAndroid Build Coastguard Worker 
88*d9f75844SAndroid Build Coastguard Worker const char* GetFieldTrialString();
89*d9f75844SAndroid Build Coastguard Worker 
90*d9f75844SAndroid Build Coastguard Worker // Validates the given field trial string.
91*d9f75844SAndroid Build Coastguard Worker bool FieldTrialsStringIsValid(absl::string_view trials_string);
92*d9f75844SAndroid Build Coastguard Worker 
93*d9f75844SAndroid Build Coastguard Worker // Merges two field trial strings.
94*d9f75844SAndroid Build Coastguard Worker //
95*d9f75844SAndroid Build Coastguard Worker // If a key (trial) exists twice with conflicting values (groups), the value
96*d9f75844SAndroid Build Coastguard Worker // in 'second' takes precedence.
97*d9f75844SAndroid Build Coastguard Worker // Shall only be called with valid FieldTrial strings.
98*d9f75844SAndroid Build Coastguard Worker std::string MergeFieldTrialsStrings(absl::string_view first,
99*d9f75844SAndroid Build Coastguard Worker                                     absl::string_view second);
100*d9f75844SAndroid Build Coastguard Worker 
101*d9f75844SAndroid Build Coastguard Worker // This helper allows to temporary "register" a field trial within the current
102*d9f75844SAndroid Build Coastguard Worker // scope. This is only useful for tests that use the global field trial string,
103*d9f75844SAndroid Build Coastguard Worker // otherwise you can use `webrtc::FieldTrialsRegistry`.
104*d9f75844SAndroid Build Coastguard Worker //
105*d9f75844SAndroid Build Coastguard Worker // If you want to isolate changes to the global field trial string itself within
106*d9f75844SAndroid Build Coastguard Worker // the current scope you should use `webrtc::test::ScopedFieldTrials`.
107*d9f75844SAndroid Build Coastguard Worker class FieldTrialsAllowedInScopeForTesting {
108*d9f75844SAndroid Build Coastguard Worker  public:
109*d9f75844SAndroid Build Coastguard Worker   explicit FieldTrialsAllowedInScopeForTesting(flat_set<std::string> keys);
110*d9f75844SAndroid Build Coastguard Worker   ~FieldTrialsAllowedInScopeForTesting();
111*d9f75844SAndroid Build Coastguard Worker };
112*d9f75844SAndroid Build Coastguard Worker 
113*d9f75844SAndroid Build Coastguard Worker }  // namespace field_trial
114*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
115*d9f75844SAndroid Build Coastguard Worker 
116*d9f75844SAndroid Build Coastguard Worker #endif  // SYSTEM_WRAPPERS_INCLUDE_FIELD_TRIAL_H_
117