blob: 5256142b702b779ce7b1e9043035d5dc3f833ea8 [file] [log] [blame]
andresp@webrtc.orga36ad692014-05-14 12:24:04 +00001//
2// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3//
4// Use of this source code is governed by a BSD-style license
5// that can be found in the LICENSE file in the root of the source
6// tree. An additional intellectual property rights grant can be found
7// in the file PATENTS. All contributing project authors may
8// be found in the AUTHORS file in the root of the source tree.
9//
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef SYSTEM_WRAPPERS_INCLUDE_FIELD_TRIAL_H_
12#define SYSTEM_WRAPPERS_INCLUDE_FIELD_TRIAL_H_
andresp@webrtc.orga36ad692014-05-14 12:24:04 +000013
14#include <string>
15
Ali Tofigh04c88162022-03-21 14:47:35 +010016#include "absl/strings/string_view.h"
17
andresp@webrtc.orga36ad692014-05-14 12:24:04 +000018// Field trials allow webrtc clients (such as Chrome) to turn on feature code
19// in binaries out in the field and gather information with that.
20//
Konrad Hofbaueree1e0152019-12-05 16:25:40 +010021// By default WebRTC provides an implementation of field trials that can be
Mirko Bonadei92e00382018-09-15 10:37:11 +020022// found in system_wrappers/source/field_trial.cc. If clients want to provide
23// a custom version, they will have to:
andresp@webrtc.orga36ad692014-05-14 12:24:04 +000024//
Mirko Bonadei92e00382018-09-15 10:37:11 +020025// 1. Compile WebRTC defining the preprocessor macro
26// WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT (if GN is used this can be achieved
27// by setting the GN arg rtc_exclude_field_trial_default to true).
28// 2. Provide an implementation of:
29// std::string webrtc::field_trial::FindFullName(const std::string& trial).
andresp@webrtc.orga36ad692014-05-14 12:24:04 +000030//
31// They are designed to wire up directly to chrome field trials and to speed up
32// developers by reducing the need to wire APIs to control whether a feature is
33// on/off. E.g. to experiment with a new method that could lead to a different
34// trade-off between CPU/bandwidth:
35//
36// 1 - Develop the feature with default behaviour off:
37//
Jonas Olsson5b2eda42019-06-11 14:29:40 +020038// if (FieldTrial::FindFullName("WebRTCExperimentMethod2") == "Enabled")
andresp@webrtc.orga36ad692014-05-14 12:24:04 +000039// method2();
40// else
41// method1();
42//
43// 2 - Once the changes are rolled to chrome, the new code path can be
44// controlled as normal chrome field trials.
45//
46// 3 - Evaluate the new feature and clean the code paths.
47//
48// Notes:
49// - NOT every feature is a candidate to be controlled by this mechanism as
Konrad Hofbaueree1e0152019-12-05 16:25:40 +010050// it may require negotiation between involved parties (e.g. SDP).
andresp@webrtc.orga36ad692014-05-14 12:24:04 +000051//
52// TODO(andresp): since chrome --force-fieldtrials does not marks the trial
Konrad Hofbaueree1e0152019-12-05 16:25:40 +010053// as active it does not get propagated to the renderer process. For now one
andresp@webrtc.orga36ad692014-05-14 12:24:04 +000054// needs to push a config with start_active:true or run a local finch
55// server.
56//
andresp@webrtc.orga36ad692014-05-14 12:24:04 +000057// TODO(andresp): find out how to get bots to run tests with trials enabled.
58
59namespace webrtc {
60namespace field_trial {
61
62// Returns the group name chosen for the named trial, or the empty string
63// if the trial does not exists.
64//
65// Note: To keep things tidy append all the trial names with WebRTC.
Ali Tofigh04c88162022-03-21 14:47:35 +010066std::string FindFullName(absl::string_view name);
andresp@webrtc.orga36ad692014-05-14 12:24:04 +000067
sprangc1b57a12017-02-28 08:50:47 -080068// Convenience method, returns true iff FindFullName(name) return a string that
69// starts with "Enabled".
tommi9751c452017-02-28 09:26:22 -080070// TODO(tommi): Make sure all implementations support this.
71inline bool IsEnabled(const char* name) {
72 return FindFullName(name).find("Enabled") == 0;
73}
sprangc1b57a12017-02-28 08:50:47 -080074
Ilya Nikolaevskiy8cf45e92018-01-15 10:16:54 +010075// Convenience method, returns true iff FindFullName(name) return a string that
76// starts with "Disabled".
77inline bool IsDisabled(const char* name) {
78 return FindFullName(name).find("Disabled") == 0;
79}
80
Mirko Bonadei92e00382018-09-15 10:37:11 +020081// Optionally initialize field trial from a string.
82// This method can be called at most once before any other call into webrtc.
83// E.g. before the peer connection factory is constructed.
84// Note: trials_string must never be destroyed.
Mirko Bonadei51868f52019-11-23 15:10:32 +000085void InitFieldTrialsFromString(const char* trials_string);
Mirko Bonadei92e00382018-09-15 10:37:11 +020086
87const char* GetFieldTrialString();
88
Konrad Hofbaueree1e0152019-12-05 16:25:40 +010089// Validates the given field trial string.
90bool FieldTrialsStringIsValid(const char* trials_string);
91
92// Merges two field trial strings.
93//
94// If a key (trial) exists twice with conflicting values (groups), the value
95// in 'second' takes precedence.
96// Shall only be called with valid FieldTrial strings.
97std::string MergeFieldTrialsStrings(const char* first, const char* second);
Konrad Hofbaueree1e0152019-12-05 16:25:40 +010098
andresp@webrtc.orga36ad692014-05-14 12:24:04 +000099} // namespace field_trial
100} // namespace webrtc
101
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200102#endif // SYSTEM_WRAPPERS_INCLUDE_FIELD_TRIAL_H_