blob: 81c728df8df9d13d40e178a52ed6d3b84c34e4a1 [file] [log] [blame]
bryant_liuba2b4392014-06-11 16:49:30 +08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_AUDIOPOLICYEFFECTS_H
18#define ANDROID_AUDIOPOLICYEFFECTS_H
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <cutils/misc.h>
24#include <media/AudioEffect.h>
25#include <system/audio.h>
bryant_liuba2b4392014-06-11 16:49:30 +080026#include <utils/Vector.h>
27#include <utils/SortedVector.h>
François Gaffiee8e0e7a2020-01-07 15:16:14 +010028#include <android-base/thread_annotations.h>
29
30#include <future>
bryant_liuba2b4392014-06-11 16:49:30 +080031
32namespace android {
33
34// ----------------------------------------------------------------------------
35
36// AudioPolicyEffects class
37// This class will manage all effects attached to input and output streams in
38// AudioPolicyService as configured in audio_effects.conf.
39class AudioPolicyEffects : public RefBase
40{
41
42public:
43
44 // The constructor will parse audio_effects.conf
45 // First it will look whether vendor specific file exists,
46 // otherwise it will parse the system default file.
47 AudioPolicyEffects();
48 virtual ~AudioPolicyEffects();
49
Eric Laurent8b1e80b2014-10-07 09:08:47 -070050 // NOTE: methods on AudioPolicyEffects should never be called with the AudioPolicyService
51 // main mutex (mLock) held as they will indirectly call back into AudioPolicyService when
52 // managing audio effects.
53
bryant_liuba2b4392014-06-11 16:49:30 +080054 // Return a list of effect descriptors for default input effects
55 // associated with audioSession
Eric Laurentfb66dd92016-01-28 18:32:03 -080056 status_t queryDefaultInputEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +080057 effect_descriptor_t *descriptors,
58 uint32_t *count);
59
60 // Add all input effects associated with this input
61 // Effects are attached depending on the audio_source_t
62 status_t addInputEffects(audio_io_handle_t input,
63 audio_source_t inputSource,
Eric Laurentfb66dd92016-01-28 18:32:03 -080064 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080065
66 // Add all input effects associated to this input
Eric Laurentfb66dd92016-01-28 18:32:03 -080067 status_t releaseInputEffects(audio_io_handle_t input,
68 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080069
bryant_liuba2b4392014-06-11 16:49:30 +080070 // Return a list of effect descriptors for default output effects
71 // associated with audioSession
Eric Laurentfb66dd92016-01-28 18:32:03 -080072 status_t queryDefaultOutputSessionEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +080073 effect_descriptor_t *descriptors,
74 uint32_t *count);
75
76 // Add all output effects associated to this output
77 // Effects are attached depending on the audio_stream_type_t
78 status_t addOutputSessionEffects(audio_io_handle_t output,
79 audio_stream_type_t stream,
Eric Laurentfb66dd92016-01-28 18:32:03 -080080 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080081
82 // release all output effects associated with this output stream and audiosession
83 status_t releaseOutputSessionEffects(audio_io_handle_t output,
84 audio_stream_type_t stream,
Eric Laurentfb66dd92016-01-28 18:32:03 -080085 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080086
Ari Hausman-Cohen24628312018-08-13 15:01:09 -070087 // Add the effect to the list of default effects for sources of type |source|.
88 status_t addSourceDefaultEffect(const effect_uuid_t *type,
89 const String16& opPackageName,
90 const effect_uuid_t *uuid,
91 int32_t priority,
92 audio_source_t source,
93 audio_unique_id_t* id);
94
95 // Add the effect to the list of default effects for streams of a given usage.
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -070096 status_t addStreamDefaultEffect(const effect_uuid_t *type,
97 const String16& opPackageName,
98 const effect_uuid_t *uuid,
99 int32_t priority,
100 audio_usage_t usage,
101 audio_unique_id_t* id);
102
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700103 // Remove the default source effect from wherever it's attached.
104 status_t removeSourceDefaultEffect(audio_unique_id_t id);
105
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700106 // Remove the default stream effect from wherever it's attached.
107 status_t removeStreamDefaultEffect(audio_unique_id_t id);
108
bryant_liuba2b4392014-06-11 16:49:30 +0800109private:
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100110 void initDefaultDeviceEffects();
bryant_liuba2b4392014-06-11 16:49:30 +0800111
112 // class to store the description of an effects and its parameters
113 // as defined in audio_effects.conf
114 class EffectDesc {
115 public:
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700116 EffectDesc(const char *name,
117 const effect_uuid_t& typeUuid,
118 const String16& opPackageName,
119 const effect_uuid_t& uuid,
120 uint32_t priority,
121 audio_unique_id_t id) :
bryant_liuba2b4392014-06-11 16:49:30 +0800122 mName(strdup(name)),
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700123 mTypeUuid(typeUuid),
124 mOpPackageName(opPackageName),
125 mUuid(uuid),
126 mPriority(priority),
127 mId(id) { }
128 EffectDesc(const char *name, const effect_uuid_t& uuid) :
129 EffectDesc(name,
130 *EFFECT_UUID_NULL,
131 String16(""),
132 uuid,
133 0,
134 AUDIO_UNIQUE_ID_ALLOCATE) { }
bryant_liuba2b4392014-06-11 16:49:30 +0800135 EffectDesc(const EffectDesc& orig) :
136 mName(strdup(orig.mName)),
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700137 mTypeUuid(orig.mTypeUuid),
138 mOpPackageName(orig.mOpPackageName),
139 mUuid(orig.mUuid),
140 mPriority(orig.mPriority),
141 mId(orig.mId) {
bryant_liuba2b4392014-06-11 16:49:30 +0800142 // deep copy mParams
143 for (size_t k = 0; k < orig.mParams.size(); k++) {
144 effect_param_t *origParam = orig.mParams[k];
145 // psize and vsize are rounded up to an int boundary for allocation
146 size_t origSize = sizeof(effect_param_t) +
147 ((origParam->psize + 3) & ~3) +
148 ((origParam->vsize + 3) & ~3);
149 effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
150 memcpy(dupParam, origParam, origSize);
151 // This works because the param buffer allocation is also done by
152 // multiples of 4 bytes originally. In theory we should memcpy only
153 // the actual param size, that is without rounding vsize.
154 mParams.add(dupParam);
155 }
156 }
157 /*virtual*/ ~EffectDesc() {
158 free(mName);
159 for (size_t k = 0; k < mParams.size(); k++) {
160 free(mParams[k]);
161 }
162 }
163 char *mName;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700164 effect_uuid_t mTypeUuid;
165 String16 mOpPackageName;
bryant_liuba2b4392014-06-11 16:49:30 +0800166 effect_uuid_t mUuid;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700167 int32_t mPriority;
168 audio_unique_id_t mId;
bryant_liuba2b4392014-06-11 16:49:30 +0800169 Vector <effect_param_t *> mParams;
170 };
171
172 // class to store voctor of EffectDesc
173 class EffectDescVector {
174 public:
175 EffectDescVector() {}
176 /*virtual*/ ~EffectDescVector() {
177 for (size_t j = 0; j < mEffects.size(); j++) {
178 delete mEffects[j];
179 }
180 }
181 Vector <EffectDesc *> mEffects;
182 };
183
184 // class to store voctor of AudioEffects
185 class EffectVector {
186 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700187 explicit EffectVector(audio_session_t session) : mSessionId(session), mRefCount(0) {}
bryant_liuba2b4392014-06-11 16:49:30 +0800188 /*virtual*/ ~EffectVector() {}
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700189
190 // Enable or disable all effects in effect vector
191 void setProcessorEnabled(bool enabled);
192
Glenn Kastend848eb42016-03-08 13:42:11 -0800193 const audio_session_t mSessionId;
bryant_liu890a5632014-08-20 18:06:13 +0800194 // AudioPolicyManager keeps mLock, no need for lock on reference count here
195 int mRefCount;
bryant_liuba2b4392014-06-11 16:49:30 +0800196 Vector< sp<AudioEffect> >mEffects;
197 };
198
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100199 /**
200 * @brief The DeviceEffects class stores the effects associated to a given Device Port.
201 */
202 class DeviceEffects {
203 public:
204 DeviceEffects(std::unique_ptr<EffectDescVector> effectDescriptors,
205 audio_devices_t device, const std::string& address) :
206 mEffectDescriptors(std::move(effectDescriptors)),
207 mDeviceType(device), mDeviceAddress(address) {}
208 /*virtual*/ ~DeviceEffects() = default;
209
210 std::vector<std::unique_ptr<AudioEffect>> mEffects;
211 audio_devices_t getDeviceType() const { return mDeviceType; }
212 std::string getDeviceAddress() const { return mDeviceAddress; }
213 const std::unique_ptr<EffectDescVector> mEffectDescriptors;
214
215 private:
216 const audio_devices_t mDeviceType;
217 const std::string mDeviceAddress;
218
219 };
220
bryant_liuba2b4392014-06-11 16:49:30 +0800221
222 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700223 static audio_source_t inputSourceNameToEnum(const char *name);
bryant_liuba2b4392014-06-11 16:49:30 +0800224
Eric Laurent223fd5c2014-11-11 13:43:36 -0800225 static const char *kStreamNames[AUDIO_STREAM_PUBLIC_CNT+1]; //+1 required as streams start from -1
bryant_liuba2b4392014-06-11 16:49:30 +0800226 audio_stream_type_t streamNameToEnum(const char *name);
227
bryant_liuba2b4392014-06-11 16:49:30 +0800228 // Parse audio_effects.conf
Kevin Rocard4fb615c2017-06-26 10:28:13 -0700229 status_t loadAudioEffectConfig(const char *path); // TODO: add legacy in the name
230 status_t loadAudioEffectXmlConfig(); // TODO: remove "Xml" in the name
bryant_liuba2b4392014-06-11 16:49:30 +0800231
232 // Load all effects descriptors in configuration file
233 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
234 EffectDesc *loadEffect(cnode *root);
235
236 // Load all automatic effect configurations
237 status_t loadInputEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
238 status_t loadStreamEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
239 EffectDescVector *loadEffectConfig(cnode *root, const Vector <EffectDesc *>& effects);
240
241 // Load all automatic effect parameters
242 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
243 effect_param_t *loadEffectParameter(cnode *root);
244 size_t readParamValue(cnode *node,
Eric Laurent138ed172016-02-10 10:40:44 -0800245 char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800246 size_t *curSize,
247 size_t *totSize);
Eric Laurent138ed172016-02-10 10:40:44 -0800248 size_t growParamSize(char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800249 size_t size,
250 size_t *curSize,
251 size_t *totSize);
252
Eric Laurentfb66dd92016-01-28 18:32:03 -0800253 // protects access to mInputSources, mInputSessions, mOutputStreams, mOutputSessions
Eric Laurent6c796322019-04-09 14:13:17 -0700254 // never hold AudioPolicyService::mLock when calling AudioPolicyEffects methods as
255 // those can call back into AudioPolicyService methods and try to acquire the mutex
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700256 Mutex mLock;
bryant_liuba2b4392014-06-11 16:49:30 +0800257 // Automatic input effects are configured per audio_source_t
258 KeyedVector< audio_source_t, EffectDescVector* > mInputSources;
259 // Automatic input effects are unique for audio_io_handle_t
Eric Laurentfb66dd92016-01-28 18:32:03 -0800260 KeyedVector< audio_session_t, EffectVector* > mInputSessions;
bryant_liuba2b4392014-06-11 16:49:30 +0800261
262 // Automatic output effects are organized per audio_stream_type_t
263 KeyedVector< audio_stream_type_t, EffectDescVector* > mOutputStreams;
264 // Automatic output effects are unique for audiosession ID
Eric Laurentfb66dd92016-01-28 18:32:03 -0800265 KeyedVector< audio_session_t, EffectVector* > mOutputSessions;
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100266
267 /**
268 * @brief mDeviceEffects map of device effects indexed by the device address
269 */
270 std::map<std::string, std::unique_ptr<DeviceEffects>> mDeviceEffects GUARDED_BY(mLock);
271
272 /**
273 * Device Effect initialization must be asynchronous: the audio_policy service parses and init
274 * effect on first reference. AudioFlinger will handle effect creation and register these
275 * effect on audio_policy service.
276 * We must store the reference of the furture garantee real asynchronous operation.
277 */
278 std::future<void> mDefaultDeviceEffectFuture;
bryant_liuba2b4392014-06-11 16:49:30 +0800279};
280
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800281} // namespace android
bryant_liuba2b4392014-06-11 16:49:30 +0800282
283#endif // ANDROID_AUDIOPOLICYEFFECTS_H