blob: 4e318c986897ec48fc102552971e9a2fe1249711 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/constructormagic.h"
Andrew MacDonald469c2c02015-05-22 17:50:26 -070017
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000018namespace webrtc {
19
aluebs688e3082016-01-14 04:32:46 -080020// Only add new values to the end of the enumeration and never remove (only
21// deprecate) to maintain binary compatibility.
22enum class ConfigOptionID {
23 kMyExperimentForTest,
24 kAlgo1CostFunctionForTest,
solenberg88499ec2016-09-07 07:34:41 -070025 kTemporalLayersFactory, // Deprecated
peahe0eae3c2016-12-14 01:16:23 -080026 kNetEqCapacityConfig, // Deprecated
27 kNetEqFastAccelerate, // Deprecated
28 kVoicePacing, // Deprecated
aluebs688e3082016-01-14 04:32:46 -080029 kExtendedFilter,
30 kDelayAgnostic,
31 kExperimentalAgc,
32 kExperimentalNs,
Sam Zackrisson9394f6f2018-06-14 10:11:35 +020033 kBeamforming, // Deprecated
peaha332e2d2016-02-17 01:11:16 -080034 kIntelligibility,
peahe0eae3c2016-12-14 01:16:23 -080035 kEchoCanceller3, // Deprecated
peahca4cac72016-06-29 15:26:12 -070036 kAecRefinedAdaptiveFilter,
Sam Zackrissonab1aee02018-03-05 15:59:06 +010037 kLevelControl // Deprecated
aluebs688e3082016-01-14 04:32:46 -080038};
39
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000040// 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).
60class Config {
61 public:
62 // Returns the option if set or a default constructed one.
andrew@webrtc.org61e596f2013-07-25 18:28:29 +000063 // Callers that access options too often are encouraged to cache the result.
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000064 // Returned references are owned by this.
65 //
66 // Requires std::is_default_constructible<T>
67 template<typename T> const T& Get() const;
68
69 // Set the option, deleting any previous instance of the same.
andrew@webrtc.org61e596f2013-07-25 18:28:29 +000070 // This instance gets ownership of the newly set value.
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000071 template<typename T> void Set(T* value);
72
kwiberg65fc8b92016-08-29 10:05:24 -070073 Config();
74 ~Config();
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000075
76 private:
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000077 struct BaseOption {
78 virtual ~BaseOption() {}
79 };
80
81 template<typename T>
82 struct Option : BaseOption {
83 explicit Option(T* v): value(v) {}
84 ~Option() {
85 delete value;
86 }
87 T* value;
88 };
89
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000090 template<typename T>
aluebs688e3082016-01-14 04:32:46 -080091 static ConfigOptionID identifier() {
92 return T::identifier;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000093 }
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.
98 template<typename T>
99 static const T& default_value() {
Niels Möller14682a32018-05-24 08:54:25 +0200100 static const T* const def = new T();
101 return *def;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000102 }
103
aluebs688e3082016-01-14 04:32:46 -0800104 typedef std::map<ConfigOptionID, BaseOption*> OptionMap;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000105 OptionMap options_;
106
henrikg3c089d72015-09-16 05:37:44 -0700107 // RTC_DISALLOW_COPY_AND_ASSIGN
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000108 Config(const Config&);
109 void operator=(const Config&);
110};
111
112template<typename T>
113const 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
124template<typename T>
125void 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.org61e596f2013-07-25 18:28:29 +0000131
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200132#endif // MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_