blob: 80b0f913917fcc2361ba8bfcf2417c88ceba4819 [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 Yaoc11621e2023-04-28 01:55:57 +000042 * With HIDL HAL, the configuration file `audio_effects.xml` will be loaded by libAudioHal.
Shunkai Yao8d6489a2023-04-18 23:14:25 +000043 * With AIDL HAL, the configuration will be queried with the method `IFactory::queryProcessing()`.
44 */
bryant_liuba2b4392014-06-11 16:49:30 +080045class AudioPolicyEffects : public RefBase
46{
47
48public:
49
Shunkai Yaoc11621e2023-04-28 01:55:57 +000050 // The constructor will parse audio_effects.xml
bryant_liuba2b4392014-06-11 16:49:30 +080051 // First it will look whether vendor specific file exists,
52 // otherwise it will parse the system default file.
Shunkai Yao8d6489a2023-04-18 23:14:25 +000053 explicit AudioPolicyEffects(const sp<EffectsFactoryHalInterface>& effectsFactoryHal);
bryant_liuba2b4392014-06-11 16:49:30 +080054 virtual ~AudioPolicyEffects();
55
Eric Laurent8b1e80b2014-10-07 09:08:47 -070056 // NOTE: methods on AudioPolicyEffects should never be called with the AudioPolicyService
57 // main mutex (mLock) held as they will indirectly call back into AudioPolicyService when
58 // managing audio effects.
59
bryant_liuba2b4392014-06-11 16:49:30 +080060 // Return a list of effect descriptors for default input effects
61 // associated with audioSession
Eric Laurentfb66dd92016-01-28 18:32:03 -080062 status_t queryDefaultInputEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +080063 effect_descriptor_t *descriptors,
64 uint32_t *count);
65
66 // Add all input effects associated with this input
67 // Effects are attached depending on the audio_source_t
68 status_t addInputEffects(audio_io_handle_t input,
69 audio_source_t inputSource,
Eric Laurentfb66dd92016-01-28 18:32:03 -080070 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080071
72 // Add all input effects associated to this input
Eric Laurentfb66dd92016-01-28 18:32:03 -080073 status_t releaseInputEffects(audio_io_handle_t input,
74 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080075
bryant_liuba2b4392014-06-11 16:49:30 +080076 // Return a list of effect descriptors for default output effects
77 // associated with audioSession
Eric Laurentfb66dd92016-01-28 18:32:03 -080078 status_t queryDefaultOutputSessionEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +080079 effect_descriptor_t *descriptors,
80 uint32_t *count);
81
82 // Add all output effects associated to this output
83 // Effects are attached depending on the audio_stream_type_t
84 status_t addOutputSessionEffects(audio_io_handle_t output,
85 audio_stream_type_t stream,
Eric Laurentfb66dd92016-01-28 18:32:03 -080086 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080087
88 // release all output effects associated with this output stream and audiosession
89 status_t releaseOutputSessionEffects(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
Ari Hausman-Cohen24628312018-08-13 15:01:09 -070093 // Add the effect to the list of default effects for sources of type |source|.
94 status_t addSourceDefaultEffect(const effect_uuid_t *type,
95 const String16& opPackageName,
96 const effect_uuid_t *uuid,
97 int32_t priority,
98 audio_source_t source,
99 audio_unique_id_t* id);
100
101 // Add the effect to the list of default effects for streams of a given usage.
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700102 status_t addStreamDefaultEffect(const effect_uuid_t *type,
103 const String16& opPackageName,
104 const effect_uuid_t *uuid,
105 int32_t priority,
106 audio_usage_t usage,
107 audio_unique_id_t* id);
108
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700109 // Remove the default source effect from wherever it's attached.
110 status_t removeSourceDefaultEffect(audio_unique_id_t id);
111
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700112 // Remove the default stream effect from wherever it's attached.
113 status_t removeStreamDefaultEffect(audio_unique_id_t id);
114
bryant_liuba2b4392014-06-11 16:49:30 +0800115private:
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100116 void initDefaultDeviceEffects();
bryant_liuba2b4392014-06-11 16:49:30 +0800117
118 // class to store the description of an effects and its parameters
Shunkai Yaoc11621e2023-04-28 01:55:57 +0000119 // as defined in audio_effects.xml
bryant_liuba2b4392014-06-11 16:49:30 +0800120 class EffectDesc {
121 public:
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700122 EffectDesc(const char *name,
123 const effect_uuid_t& typeUuid,
124 const String16& opPackageName,
125 const effect_uuid_t& uuid,
126 uint32_t priority,
127 audio_unique_id_t id) :
bryant_liuba2b4392014-06-11 16:49:30 +0800128 mName(strdup(name)),
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700129 mTypeUuid(typeUuid),
130 mOpPackageName(opPackageName),
131 mUuid(uuid),
132 mPriority(priority),
133 mId(id) { }
134 EffectDesc(const char *name, const effect_uuid_t& uuid) :
135 EffectDesc(name,
136 *EFFECT_UUID_NULL,
137 String16(""),
138 uuid,
139 0,
140 AUDIO_UNIQUE_ID_ALLOCATE) { }
bryant_liuba2b4392014-06-11 16:49:30 +0800141 EffectDesc(const EffectDesc& orig) :
142 mName(strdup(orig.mName)),
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700143 mTypeUuid(orig.mTypeUuid),
144 mOpPackageName(orig.mOpPackageName),
145 mUuid(orig.mUuid),
146 mPriority(orig.mPriority),
147 mId(orig.mId) {
bryant_liuba2b4392014-06-11 16:49:30 +0800148 // deep copy mParams
149 for (size_t k = 0; k < orig.mParams.size(); k++) {
150 effect_param_t *origParam = orig.mParams[k];
151 // psize and vsize are rounded up to an int boundary for allocation
152 size_t origSize = sizeof(effect_param_t) +
153 ((origParam->psize + 3) & ~3) +
154 ((origParam->vsize + 3) & ~3);
155 effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
156 memcpy(dupParam, origParam, origSize);
157 // This works because the param buffer allocation is also done by
158 // multiples of 4 bytes originally. In theory we should memcpy only
159 // the actual param size, that is without rounding vsize.
160 mParams.add(dupParam);
161 }
162 }
163 /*virtual*/ ~EffectDesc() {
164 free(mName);
165 for (size_t k = 0; k < mParams.size(); k++) {
166 free(mParams[k]);
167 }
168 }
169 char *mName;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700170 effect_uuid_t mTypeUuid;
171 String16 mOpPackageName;
bryant_liuba2b4392014-06-11 16:49:30 +0800172 effect_uuid_t mUuid;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700173 int32_t mPriority;
174 audio_unique_id_t mId;
bryant_liuba2b4392014-06-11 16:49:30 +0800175 Vector <effect_param_t *> mParams;
176 };
177
178 // class to store voctor of EffectDesc
179 class EffectDescVector {
180 public:
181 EffectDescVector() {}
182 /*virtual*/ ~EffectDescVector() {
183 for (size_t j = 0; j < mEffects.size(); j++) {
184 delete mEffects[j];
185 }
186 }
187 Vector <EffectDesc *> mEffects;
188 };
189
190 // class to store voctor of AudioEffects
191 class EffectVector {
192 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700193 explicit EffectVector(audio_session_t session) : mSessionId(session), mRefCount(0) {}
bryant_liuba2b4392014-06-11 16:49:30 +0800194 /*virtual*/ ~EffectVector() {}
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700195
196 // Enable or disable all effects in effect vector
197 void setProcessorEnabled(bool enabled);
198
Glenn Kastend848eb42016-03-08 13:42:11 -0800199 const audio_session_t mSessionId;
bryant_liu890a5632014-08-20 18:06:13 +0800200 // AudioPolicyManager keeps mLock, no need for lock on reference count here
201 int mRefCount;
bryant_liuba2b4392014-06-11 16:49:30 +0800202 Vector< sp<AudioEffect> >mEffects;
203 };
204
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100205 /**
206 * @brief The DeviceEffects class stores the effects associated to a given Device Port.
207 */
208 class DeviceEffects {
209 public:
210 DeviceEffects(std::unique_ptr<EffectDescVector> effectDescriptors,
211 audio_devices_t device, const std::string& address) :
212 mEffectDescriptors(std::move(effectDescriptors)),
213 mDeviceType(device), mDeviceAddress(address) {}
214 /*virtual*/ ~DeviceEffects() = default;
215
Jintao Zhu230397e2021-03-07 14:15:10 +0800216 std::vector< sp<AudioEffect> > mEffects;
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100217 audio_devices_t getDeviceType() const { return mDeviceType; }
218 std::string getDeviceAddress() const { return mDeviceAddress; }
219 const std::unique_ptr<EffectDescVector> mEffectDescriptors;
220
221 private:
222 const audio_devices_t mDeviceType;
223 const std::string mDeviceAddress;
224
225 };
226
bryant_liuba2b4392014-06-11 16:49:30 +0800227 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700228 static audio_source_t inputSourceNameToEnum(const char *name);
bryant_liuba2b4392014-06-11 16:49:30 +0800229
Eric Laurent223fd5c2014-11-11 13:43:36 -0800230 static const char *kStreamNames[AUDIO_STREAM_PUBLIC_CNT+1]; //+1 required as streams start from -1
bryant_liuba2b4392014-06-11 16:49:30 +0800231 audio_stream_type_t streamNameToEnum(const char *name);
232
Shunkai Yaoc11621e2023-04-28 01:55:57 +0000233 // Parse audio_effects.xml
Shunkai Yao8d6489a2023-04-18 23:14:25 +0000234 status_t loadAudioEffectConfig(const sp<EffectsFactoryHalInterface>& effectsFactoryHal);
bryant_liuba2b4392014-06-11 16:49:30 +0800235
236 // Load all effects descriptors in configuration file
237 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
238 EffectDesc *loadEffect(cnode *root);
239
240 // Load all automatic effect configurations
241 status_t loadInputEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
242 status_t loadStreamEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
243 EffectDescVector *loadEffectConfig(cnode *root, const Vector <EffectDesc *>& effects);
244
245 // Load all automatic effect parameters
246 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
247 effect_param_t *loadEffectParameter(cnode *root);
248 size_t readParamValue(cnode *node,
Eric Laurent138ed172016-02-10 10:40:44 -0800249 char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800250 size_t *curSize,
251 size_t *totSize);
Eric Laurent138ed172016-02-10 10:40:44 -0800252 size_t growParamSize(char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800253 size_t size,
254 size_t *curSize,
255 size_t *totSize);
256
Eric Laurentfb66dd92016-01-28 18:32:03 -0800257 // protects access to mInputSources, mInputSessions, mOutputStreams, mOutputSessions
Eric Laurent6c796322019-04-09 14:13:17 -0700258 // never hold AudioPolicyService::mLock when calling AudioPolicyEffects methods as
259 // those can call back into AudioPolicyService methods and try to acquire the mutex
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700260 Mutex mLock;
bryant_liuba2b4392014-06-11 16:49:30 +0800261 // Automatic input effects are configured per audio_source_t
262 KeyedVector< audio_source_t, EffectDescVector* > mInputSources;
263 // Automatic input effects are unique for audio_io_handle_t
Eric Laurentfb66dd92016-01-28 18:32:03 -0800264 KeyedVector< audio_session_t, EffectVector* > mInputSessions;
bryant_liuba2b4392014-06-11 16:49:30 +0800265
266 // Automatic output effects are organized per audio_stream_type_t
267 KeyedVector< audio_stream_type_t, EffectDescVector* > mOutputStreams;
268 // Automatic output effects are unique for audiosession ID
Eric Laurentfb66dd92016-01-28 18:32:03 -0800269 KeyedVector< audio_session_t, EffectVector* > mOutputSessions;
François Gaffiee8e0e7a2020-01-07 15:16:14 +0100270
271 /**
272 * @brief mDeviceEffects map of device effects indexed by the device address
273 */
274 std::map<std::string, std::unique_ptr<DeviceEffects>> mDeviceEffects GUARDED_BY(mLock);
275
276 /**
277 * Device Effect initialization must be asynchronous: the audio_policy service parses and init
278 * effect on first reference. AudioFlinger will handle effect creation and register these
279 * effect on audio_policy service.
280 * We must store the reference of the furture garantee real asynchronous operation.
281 */
282 std::future<void> mDefaultDeviceEffectFuture;
bryant_liuba2b4392014-06-11 16:49:30 +0800283};
284
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800285} // namespace android
bryant_liuba2b4392014-06-11 16:49:30 +0800286
287#endif // ANDROID_AUDIOPOLICYEFFECTS_H