blob: 930cf7e85ed3f15d3f00ff209d70137eaaebe9f7 [file] [log] [blame]
andresp@webrtc.org6b68c282013-05-13 08:06:36 +00001/*
2 * Copyright (c) 2013 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 MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_
12#define MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000013
14#include <map>
15
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/constructor_magic.h"
Mirko Bonadei3d255302018-10-11 10:50:45 +020017#include "rtc_base/system/rtc_export.h"
Andrew MacDonald469c2c02015-05-22 17:50:26 -070018
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000019namespace webrtc {
20
aluebs688e3082016-01-14 04:32:46 -080021// Only add new values to the end of the enumeration and never remove (only
22// deprecate) to maintain binary compatibility.
23enum class ConfigOptionID {
24 kMyExperimentForTest,
25 kAlgo1CostFunctionForTest,
solenberg88499ec2016-09-07 07:34:41 -070026 kTemporalLayersFactory, // Deprecated
peahe0eae3c2016-12-14 01:16:23 -080027 kNetEqCapacityConfig, // Deprecated
28 kNetEqFastAccelerate, // Deprecated
29 kVoicePacing, // Deprecated
aluebs688e3082016-01-14 04:32:46 -080030 kExtendedFilter,
31 kDelayAgnostic,
32 kExperimentalAgc,
33 kExperimentalNs,
Alessio Bazzicacc22f512018-08-30 13:01:34 +020034 kBeamforming, // Deprecated
35 kIntelligibility, // Deprecated
36 kEchoCanceller3, // Deprecated
peahca4cac72016-06-29 15:26:12 -070037 kAecRefinedAdaptiveFilter,
Sam Zackrissonab1aee02018-03-05 15:59:06 +010038 kLevelControl // Deprecated
aluebs688e3082016-01-14 04:32:46 -080039};
40
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000041// Class Config is designed to ease passing a set of options across webrtc code.
42// Options are identified by typename in order to avoid incorrect casts.
43//
44// Usage:
45// * declaring an option:
46// struct Algo1_CostFunction {
47// virtual float cost(int x) const { return x; }
48// virtual ~Algo1_CostFunction() {}
49// };
50//
51// * accessing an option:
52// config.Get<Algo1_CostFunction>().cost(value);
53//
54// * setting an option:
55// struct SqrCost : Algo1_CostFunction {
56// virtual float cost(int x) const { return x*x; }
57// };
58// config.Set<Algo1_CostFunction>(new SqrCost());
59//
60// Note: This class is thread-compatible (like STL containers).
Mirko Bonadei3d255302018-10-11 10:50:45 +020061class RTC_EXPORT Config {
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000062 public:
63 // Returns the option if set or a default constructed one.
andrew@webrtc.org61e596f2013-07-25 18:28:29 +000064 // Callers that access options too often are encouraged to cache the result.
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000065 // Returned references are owned by this.
66 //
67 // Requires std::is_default_constructible<T>
Yves Gerey665174f2018-06-19 15:03:05 +020068 template <typename T>
69 const T& Get() const;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000070
71 // Set the option, deleting any previous instance of the same.
andrew@webrtc.org61e596f2013-07-25 18:28:29 +000072 // This instance gets ownership of the newly set value.
Yves Gerey665174f2018-06-19 15:03:05 +020073 template <typename T>
74 void Set(T* value);
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000075
kwiberg65fc8b92016-08-29 10:05:24 -070076 Config();
77 ~Config();
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000078
79 private:
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000080 struct BaseOption {
81 virtual ~BaseOption() {}
82 };
83
Yves Gerey665174f2018-06-19 15:03:05 +020084 template <typename T>
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000085 struct Option : BaseOption {
Yves Gerey665174f2018-06-19 15:03:05 +020086 explicit Option(T* v) : value(v) {}
87 ~Option() { delete value; }
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000088 T* value;
89 };
90
Yves Gerey665174f2018-06-19 15:03:05 +020091 template <typename T>
aluebs688e3082016-01-14 04:32:46 -080092 static ConfigOptionID identifier() {
93 return T::identifier;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000094 }
95
96 // Used to instantiate a default constructed object that doesn't needs to be
97 // owned. This allows Get<T> to be implemented without requiring explicitly
98 // locks.
Yves Gerey665174f2018-06-19 15:03:05 +020099 template <typename T>
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000100 static const T& default_value() {
Niels Möller14682a32018-05-24 08:54:25 +0200101 static const T* const def = new T();
102 return *def;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000103 }
104
aluebs688e3082016-01-14 04:32:46 -0800105 typedef std::map<ConfigOptionID, BaseOption*> OptionMap;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000106 OptionMap options_;
107
henrikg3c089d72015-09-16 05:37:44 -0700108 // RTC_DISALLOW_COPY_AND_ASSIGN
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000109 Config(const Config&);
110 void operator=(const Config&);
111};
112
Yves Gerey665174f2018-06-19 15:03:05 +0200113template <typename T>
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000114const T& Config::Get() const {
115 OptionMap::const_iterator it = options_.find(identifier<T>());
116 if (it != options_.end()) {
117 const T* t = static_cast<Option<T>*>(it->second)->value;
118 if (t) {
119 return *t;
120 }
121 }
122 return default_value<T>();
123}
124
Yves Gerey665174f2018-06-19 15:03:05 +0200125template <typename T>
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000126void Config::Set(T* value) {
127 BaseOption*& it = options_[identifier<T>()];
128 delete it;
129 it = new Option<T>(value);
130}
131} // namespace webrtc
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000132
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200133#endif // MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_