andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_ |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 13 | |
| 14 | #include <map> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/constructormagic.h" |
Andrew MacDonald | 469c2c0 | 2015-05-22 17:50:26 -0700 | [diff] [blame] | 17 | |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 20 | // Only add new values to the end of the enumeration and never remove (only |
| 21 | // deprecate) to maintain binary compatibility. |
| 22 | enum class ConfigOptionID { |
| 23 | kMyExperimentForTest, |
| 24 | kAlgo1CostFunctionForTest, |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 25 | kTemporalLayersFactory, // Deprecated |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 26 | kNetEqCapacityConfig, // Deprecated |
| 27 | kNetEqFastAccelerate, // Deprecated |
| 28 | kVoicePacing, // Deprecated |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 29 | kExtendedFilter, |
| 30 | kDelayAgnostic, |
| 31 | kExperimentalAgc, |
| 32 | kExperimentalNs, |
Sam Zackrisson | 9394f6f | 2018-06-14 10:11:35 +0200 | [diff] [blame] | 33 | kBeamforming, // Deprecated |
peah | a332e2d | 2016-02-17 01:11:16 -0800 | [diff] [blame] | 34 | kIntelligibility, |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 35 | kEchoCanceller3, // Deprecated |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 36 | kAecRefinedAdaptiveFilter, |
Sam Zackrisson | ab1aee0 | 2018-03-05 15:59:06 +0100 | [diff] [blame] | 37 | kLevelControl // Deprecated |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 40 | // Class Config is designed to ease passing a set of options across webrtc code. |
| 41 | // Options are identified by typename in order to avoid incorrect casts. |
| 42 | // |
| 43 | // Usage: |
| 44 | // * declaring an option: |
| 45 | // struct Algo1_CostFunction { |
| 46 | // virtual float cost(int x) const { return x; } |
| 47 | // virtual ~Algo1_CostFunction() {} |
| 48 | // }; |
| 49 | // |
| 50 | // * accessing an option: |
| 51 | // config.Get<Algo1_CostFunction>().cost(value); |
| 52 | // |
| 53 | // * setting an option: |
| 54 | // struct SqrCost : Algo1_CostFunction { |
| 55 | // virtual float cost(int x) const { return x*x; } |
| 56 | // }; |
| 57 | // config.Set<Algo1_CostFunction>(new SqrCost()); |
| 58 | // |
| 59 | // Note: This class is thread-compatible (like STL containers). |
| 60 | class Config { |
| 61 | public: |
| 62 | // Returns the option if set or a default constructed one. |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 63 | // Callers that access options too often are encouraged to cache the result. |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 64 | // Returned references are owned by this. |
| 65 | // |
| 66 | // Requires std::is_default_constructible<T> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 67 | template <typename T> |
| 68 | const T& Get() const; |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 69 | |
| 70 | // Set the option, deleting any previous instance of the same. |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 71 | // This instance gets ownership of the newly set value. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 72 | template <typename T> |
| 73 | void Set(T* value); |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 74 | |
kwiberg | 65fc8b9 | 2016-08-29 10:05:24 -0700 | [diff] [blame] | 75 | Config(); |
| 76 | ~Config(); |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 77 | |
| 78 | private: |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 79 | struct BaseOption { |
| 80 | virtual ~BaseOption() {} |
| 81 | }; |
| 82 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 83 | template <typename T> |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 84 | struct Option : BaseOption { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 85 | explicit Option(T* v) : value(v) {} |
| 86 | ~Option() { delete value; } |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 87 | T* value; |
| 88 | }; |
| 89 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 90 | template <typename T> |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 91 | static ConfigOptionID identifier() { |
| 92 | return T::identifier; |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Used to instantiate a default constructed object that doesn't needs to be |
| 96 | // owned. This allows Get<T> to be implemented without requiring explicitly |
| 97 | // locks. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 98 | template <typename T> |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 99 | static const T& default_value() { |
Niels Möller | 14682a3 | 2018-05-24 08:54:25 +0200 | [diff] [blame] | 100 | static const T* const def = new T(); |
| 101 | return *def; |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 102 | } |
| 103 | |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 104 | typedef std::map<ConfigOptionID, BaseOption*> OptionMap; |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 105 | OptionMap options_; |
| 106 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 107 | // RTC_DISALLOW_COPY_AND_ASSIGN |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 108 | Config(const Config&); |
| 109 | void operator=(const Config&); |
| 110 | }; |
| 111 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 112 | template <typename T> |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 113 | const T& Config::Get() const { |
| 114 | OptionMap::const_iterator it = options_.find(identifier<T>()); |
| 115 | if (it != options_.end()) { |
| 116 | const T* t = static_cast<Option<T>*>(it->second)->value; |
| 117 | if (t) { |
| 118 | return *t; |
| 119 | } |
| 120 | } |
| 121 | return default_value<T>(); |
| 122 | } |
| 123 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 124 | template <typename T> |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 125 | void Config::Set(T* value) { |
| 126 | BaseOption*& it = options_[identifier<T>()]; |
| 127 | delete it; |
| 128 | it = new Option<T>(value); |
| 129 | } |
| 130 | } // namespace webrtc |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 131 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 132 | #endif // MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_ |