blob: 9f65a96859a7a01931bf3ba4d83f2820fda82a30 [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>
Shunkai Yao8d6489a2023-04-18 23:14:25 +000023#include <future>
24
25#include <android-base/thread_annotations.h>
bryant_liuba2b4392014-06-11 16:49:30 +080026#include <cutils/misc.h>
27#include <media/AudioEffect.h>
Shunkai Yao8d6489a2023-04-18 23:14:25 +000028#include <media/audiohal/EffectsFactoryHalInterface.h>
bryant_liuba2b4392014-06-11 16:49:30 +080029#include <system/audio.h>
bryant_liuba2b4392014-06-11 16:49:30 +080030#include <utils/Vector.h>
31#include <utils/SortedVector.h>
32
33namespace android {
34
35// ----------------------------------------------------------------------------
36
Shunkai Yao8d6489a2023-04-18 23:14:25 +000037/**
38 * AudioPolicyEffects class.
39 *
40 * This class manages all effects attached to input and output streams in AudioPolicyService.
41 * The effect configurations can be queried in several ways:
Shunkai Yaoeab8a7c2023-05-15 21:07:08 +000042 *
43 * With HIDL HAL, the configuration file `audio_effects.xml` will be loaded by libAudioHal. If this
44 * file does not exist, AudioPolicyEffects class will fallback to load configuration from
45 * `/vendor/etc/audio_effects.conf` (AUDIO_EFFECT_VENDOR_CONFIG_FILE). If this file also does not
46 * exist, the configuration will be loaded from the file `/system/etc/audio_effects.conf`.
47 *
Shunkai Yao8d6489a2023-04-18 23:14:25 +000048 * With AIDL HAL, the configuration will be queried with the method `IFactory::queryProcessing()`.
49 */
bryant_liuba2b4392014-06-11 16:49:30 +080050class AudioPolicyEffects : public RefBase
51{
52
53public:
54
Shunkai Yaoeab8a7c2023-05-15 21:07:08 +000055 // The constructor will parse audio_effects.conf
bryant_liuba2b4392014-06-11 16:49:30 +080056 // First it will look whether vendor specific file exists,
57 // otherwise it will parse the system default file.
Shunkai Yao8d6489a2023-04-18 23:14:25 +000058 explicit AudioPolicyEffects(const sp<EffectsFactoryHalInterface>& effectsFactoryHal);
bryant_liuba2b4392014-06-11 16:49:30 +080059 virtual ~AudioPolicyEffects();
60
Eric Laurent8b1e80b2014-10-07 09:08:47 -070061 // NOTE: methods on AudioPolicyEffects should never be called with the AudioPolicyService
62 // main mutex (mLock) held as they will indirectly call back into AudioPolicyService when
63 // managing audio effects.
64
bryant_liuba2b4392014-06-11 16:49:30 +080065 // Return a list of effect descriptors for default input effects
66 // associated with audioSession
Eric Laurentfb66dd92016-01-28 18:32:03 -080067 status_t queryDefaultInputEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +080068 effect_descriptor_t *descriptors,
69 uint32_t *count);
70
71 // Add all input effects associated with this input
72 // Effects are attached depending on the audio_source_t
73 status_t addInputEffects(audio_io_handle_t input,
74 audio_source_t inputSource,
Eric Laurentfb66dd92016-01-28 18:32:03 -080075 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080076
77 // Add all input effects associated to this input
Eric Laurentfb66dd92016-01-28 18:32:03 -080078 status_t releaseInputEffects(audio_io_handle_t input,
79 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080080
bryant_liuba2b4392014-06-11 16:49:30 +080081 // Return a list of effect descriptors for default output effects
82 // associated with audioSession
Eric Laurentfb66dd92016-01-28 18:32:03 -080083 status_t queryDefaultOutputSessionEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +080084 effect_descriptor_t *descriptors,
85 uint32_t *count);
86
87 // Add all output effects associated to this output
88 // Effects are attached depending on the audio_stream_type_t
89 status_t addOutputSessionEffects(audio_io_handle_t output,
90 audio_stream_type_t stream,
Eric Laurentfb66dd92016-01-28 18:32:03 -080091 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080092
93 // release all output effects associated with this output stream and audiosession
94 status_t releaseOutputSessionEffects(audio_io_handle_t output,
95 audio_stream_type_t stream,
Eric Laurentfb66dd92016-01-28 18:32:03 -080096 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080097
Ari Hausman-Cohen24628312018-08-13 15:01:09 -070098 // Add the effect to the list of default effects for sources of type |source|.
99 status_t addSourceDefaultEffect(const effect_uuid_t *type,
100 const String16& opPackageName,
101 const effect_uuid_t *uuid,
102 int32_t priority,
103 audio_source_t source,
104 audio_unique_id_t* id);
105
106 // Add the effect to the list of default effects for streams of a given usage.
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700107 status_t addStreamDefaultEffect(const effect_uuid_t *type,
108 const String16& opPackageName,
109 const effect_uuid_t *uuid,
110 int32_t priority,
111 audio_usage_t usage,
112 audio_unique_id_t* id);
113
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700114 // Remove the default source effect from wherever it's attached.
115 status_t removeSourceDefaultEffect(audio_unique_id_t id);
116
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700117 // Remove the default stream effect from wherever it's attached.
118 status_t removeStreamDefaultEffect(audio_unique_id_t id);
119
bryant_liuba2b4392014-06-11 16:49:30 +0800120private:
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100121 void initDefaultDeviceEffects();
bryant_liuba2b4392014-06-11 16:49:30 +0800122
123 // class to store the description of an effects and its parameters
Shunkai Yaoeab8a7c2023-05-15 21:07:08 +0000124 // as defined in audio_effects.conf
bryant_liuba2b4392014-06-11 16:49:30 +0800125 class EffectDesc {
126 public:
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700127 EffectDesc(const char *name,
128 const effect_uuid_t& typeUuid,
129 const String16& opPackageName,
130 const effect_uuid_t& uuid,
131 uint32_t priority,
132 audio_unique_id_t id) :
bryant_liuba2b4392014-06-11 16:49:30 +0800133 mName(strdup(name)),
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700134 mTypeUuid(typeUuid),
135 mOpPackageName(opPackageName),
136 mUuid(uuid),
137 mPriority(priority),
138 mId(id) { }
139 EffectDesc(const char *name, const effect_uuid_t& uuid) :
140 EffectDesc(name,
141 *EFFECT_UUID_NULL,
142 String16(""),
143 uuid,
144 0,
145 AUDIO_UNIQUE_ID_ALLOCATE) { }
bryant_liuba2b4392014-06-11 16:49:30 +0800146 EffectDesc(const EffectDesc& orig) :
147 mName(strdup(orig.mName)),
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700148 mTypeUuid(orig.mTypeUuid),
149 mOpPackageName(orig.mOpPackageName),
150 mUuid(orig.mUuid),
151 mPriority(orig.mPriority),
152 mId(orig.mId) {
bryant_liuba2b4392014-06-11 16:49:30 +0800153 // deep copy mParams
154 for (size_t k = 0; k < orig.mParams.size(); k++) {
155 effect_param_t *origParam = orig.mParams[k];
156 // psize and vsize are rounded up to an int boundary for allocation
157 size_t origSize = sizeof(effect_param_t) +
158 ((origParam->psize + 3) & ~3) +
159 ((origParam->vsize + 3) & ~3);
160 effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
161 memcpy(dupParam, origParam, origSize);
162 // This works because the param buffer allocation is also done by
163 // multiples of 4 bytes originally. In theory we should memcpy only
164 // the actual param size, that is without rounding vsize.
165 mParams.add(dupParam);
166 }
167 }
168 /*virtual*/ ~EffectDesc() {
169 free(mName);
170 for (size_t k = 0; k < mParams.size(); k++) {
171 free(mParams[k]);
172 }
173 }
174 char *mName;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700175 effect_uuid_t mTypeUuid;
176 String16 mOpPackageName;
bryant_liuba2b4392014-06-11 16:49:30 +0800177 effect_uuid_t mUuid;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700178 int32_t mPriority;
179 audio_unique_id_t mId;
bryant_liuba2b4392014-06-11 16:49:30 +0800180 Vector <effect_param_t *> mParams;
181 };
182
183 // class to store voctor of EffectDesc
184 class EffectDescVector {
185 public:
186 EffectDescVector() {}
187 /*virtual*/ ~EffectDescVector() {
188 for (size_t j = 0; j < mEffects.size(); j++) {
189 delete mEffects[j];
190 }
191 }
192 Vector <EffectDesc *> mEffects;
193 };
194
195 // class to store voctor of AudioEffects
196 class EffectVector {
197 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700198 explicit EffectVector(audio_session_t session) : mSessionId(session), mRefCount(0) {}
bryant_liuba2b4392014-06-11 16:49:30 +0800199 /*virtual*/ ~EffectVector() {}
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700200
201 // Enable or disable all effects in effect vector
202 void setProcessorEnabled(bool enabled);
203
Glenn Kastend848eb42016-03-08 13:42:11 -0800204 const audio_session_t mSessionId;
bryant_liu890a5632014-08-20 18:06:13 +0800205 // AudioPolicyManager keeps mLock, no need for lock on reference count here
206 int mRefCount;
bryant_liuba2b4392014-06-11 16:49:30 +0800207 Vector< sp<AudioEffect> >mEffects;
208 };
209
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100210 /**
211 * @brief The DeviceEffects class stores the effects associated to a given Device Port.
212 */
213 class DeviceEffects {
214 public:
215 DeviceEffects(std::unique_ptr<EffectDescVector> effectDescriptors,
216 audio_devices_t device, const std::string& address) :
217 mEffectDescriptors(std::move(effectDescriptors)),
218 mDeviceType(device), mDeviceAddress(address) {}
219 /*virtual*/ ~DeviceEffects() = default;
220
Jintao Zhu230397e2021-03-07 14:15:10 +0800221 std::vector< sp<AudioEffect> > mEffects;
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100222 audio_devices_t getDeviceType() const { return mDeviceType; }
223 std::string getDeviceAddress() const { return mDeviceAddress; }
224 const std::unique_ptr<EffectDescVector> mEffectDescriptors;
225
226 private:
227 const audio_devices_t mDeviceType;
228 const std::string mDeviceAddress;
229
230 };
231
bryant_liuba2b4392014-06-11 16:49:30 +0800232 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700233 static audio_source_t inputSourceNameToEnum(const char *name);
bryant_liuba2b4392014-06-11 16:49:30 +0800234
Eric Laurent223fd5c2014-11-11 13:43:36 -0800235 static const char *kStreamNames[AUDIO_STREAM_PUBLIC_CNT+1]; //+1 required as streams start from -1
bryant_liuba2b4392014-06-11 16:49:30 +0800236 audio_stream_type_t streamNameToEnum(const char *name);
237
Shunkai Yaoeab8a7c2023-05-15 21:07:08 +0000238 // Parse audio_effects.conf
239 status_t loadAudioEffectConfigLegacy(const char *path);
Shunkai Yao8d6489a2023-04-18 23:14:25 +0000240 status_t loadAudioEffectConfig(const sp<EffectsFactoryHalInterface>& effectsFactoryHal);
bryant_liuba2b4392014-06-11 16:49:30 +0800241
242 // Load all effects descriptors in configuration file
243 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
244 EffectDesc *loadEffect(cnode *root);
245
246 // Load all automatic effect configurations
247 status_t loadInputEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
248 status_t loadStreamEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
249 EffectDescVector *loadEffectConfig(cnode *root, const Vector <EffectDesc *>& effects);
250
251 // Load all automatic effect parameters
252 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
253 effect_param_t *loadEffectParameter(cnode *root);
254 size_t readParamValue(cnode *node,
Eric Laurent138ed172016-02-10 10:40:44 -0800255 char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800256 size_t *curSize,
257 size_t *totSize);
Eric Laurent138ed172016-02-10 10:40:44 -0800258 size_t growParamSize(char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800259 size_t size,
260 size_t *curSize,
261 size_t *totSize);
262
Eric Laurentfb66dd92016-01-28 18:32:03 -0800263 // protects access to mInputSources, mInputSessions, mOutputStreams, mOutputSessions
Eric Laurent6c796322019-04-09 14:13:17 -0700264 // never hold AudioPolicyService::mLock when calling AudioPolicyEffects methods as
265 // those can call back into AudioPolicyService methods and try to acquire the mutex
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700266 Mutex mLock;
bryant_liuba2b4392014-06-11 16:49:30 +0800267 // Automatic input effects are configured per audio_source_t
268 KeyedVector< audio_source_t, EffectDescVector* > mInputSources;
269 // Automatic input effects are unique for audio_io_handle_t
Eric Laurentfb66dd92016-01-28 18:32:03 -0800270 KeyedVector< audio_session_t, EffectVector* > mInputSessions;
bryant_liuba2b4392014-06-11 16:49:30 +0800271
272 // Automatic output effects are organized per audio_stream_type_t
273 KeyedVector< audio_stream_type_t, EffectDescVector* > mOutputStreams;
274 // Automatic output effects are unique for audiosession ID
Eric Laurentfb66dd92016-01-28 18:32:03 -0800275 KeyedVector< audio_session_t, EffectVector* > mOutputSessions;
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100276
277 /**
278 * @brief mDeviceEffects map of device effects indexed by the device address
279 */
280 std::map<std::string, std::unique_ptr<DeviceEffects>> mDeviceEffects GUARDED_BY(mLock);
281
282 /**
283 * Device Effect initialization must be asynchronous: the audio_policy service parses and init
284 * effect on first reference. AudioFlinger will handle effect creation and register these
285 * effect on audio_policy service.
286 * We must store the reference of the furture garantee real asynchronous operation.
287 */
288 std::future<void> mDefaultDeviceEffectFuture;
bryant_liuba2b4392014-06-11 16:49:30 +0800289};
290
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800291} // namespace android
bryant_liuba2b4392014-06-11 16:49:30 +0800292
293#endif // ANDROID_AUDIOPOLICYEFFECTS_H