• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <future>
24 
25 #include <android-base/thread_annotations.h>
26 #include <cutils/misc.h>
27 #include <media/AudioEffect.h>
28 #include <media/audiohal/EffectsFactoryHalInterface.h>
29 #include <system/audio.h>
30 #include <utils/Vector.h>
31 #include <utils/SortedVector.h>
32 
33 namespace android {
34 
35 // ----------------------------------------------------------------------------
36 
37 /**
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:
42  *
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  *
48  * With AIDL HAL, the configuration will be queried with the method `IFactory::queryProcessing()`.
49  */
50 class AudioPolicyEffects : public RefBase
51 {
52 
53 public:
54 
55     // The constructor will parse audio_effects.conf
56     // First it will look whether vendor specific file exists,
57     // otherwise it will parse the system default file.
58     explicit AudioPolicyEffects(const sp<EffectsFactoryHalInterface>& effectsFactoryHal);
59     virtual ~AudioPolicyEffects();
60 
61     // 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 
65     // Return a list of effect descriptors for default input effects
66     // associated with audioSession
67     status_t queryDefaultInputEffects(audio_session_t audioSession,
68                              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,
75                              audio_session_t audioSession);
76 
77     // Add all input effects associated to this input
78     status_t releaseInputEffects(audio_io_handle_t input,
79                                  audio_session_t audioSession);
80 
81     // Return a list of effect descriptors for default output effects
82     // associated with audioSession
83     status_t queryDefaultOutputSessionEffects(audio_session_t audioSession,
84                              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,
91                              audio_session_t audioSession);
92 
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,
96                              audio_session_t audioSession);
97 
98     // 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.
107     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 
114     // Remove the default source effect from wherever it's attached.
115     status_t removeSourceDefaultEffect(audio_unique_id_t id);
116 
117     // Remove the default stream effect from wherever it's attached.
118     status_t removeStreamDefaultEffect(audio_unique_id_t id);
119 
120 private:
121     void initDefaultDeviceEffects();
122 
123     // class to store the description of an effects and its parameters
124     // as defined in audio_effects.conf
125     class EffectDesc {
126     public:
EffectDesc(const char * name,const effect_uuid_t & typeUuid,const String16 & opPackageName,const effect_uuid_t & uuid,uint32_t priority,audio_unique_id_t id)127         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) :
133                         mName(strdup(name)),
134                         mTypeUuid(typeUuid),
135                         mOpPackageName(opPackageName),
136                         mUuid(uuid),
137                         mPriority(priority),
138                         mId(id) { }
EffectDesc(const char * name,const effect_uuid_t & uuid)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) { }
EffectDesc(const EffectDesc & orig)146         EffectDesc(const EffectDesc& orig) :
147                         mName(strdup(orig.mName)),
148                         mTypeUuid(orig.mTypeUuid),
149                         mOpPackageName(orig.mOpPackageName),
150                         mUuid(orig.mUuid),
151                         mPriority(orig.mPriority),
152                         mId(orig.mId) {
153                             // 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                         }
~EffectDesc()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;
175         effect_uuid_t mTypeUuid;
176         String16 mOpPackageName;
177         effect_uuid_t mUuid;
178         int32_t mPriority;
179         audio_unique_id_t mId;
180         Vector <effect_param_t *> mParams;
181     };
182 
183     // class to store voctor of EffectDesc
184     class EffectDescVector {
185     public:
EffectDescVector()186         EffectDescVector() {}
~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:
EffectVector(audio_session_t session)198         explicit EffectVector(audio_session_t session) : mSessionId(session), mRefCount(0) {}
~EffectVector()199         /*virtual*/ ~EffectVector() {}
200 
201         // Enable or disable all effects in effect vector
202         void setProcessorEnabled(bool enabled);
203 
204         const audio_session_t mSessionId;
205         // AudioPolicyManager keeps mLock, no need for lock on reference count here
206         int mRefCount;
207         Vector< sp<AudioEffect> >mEffects;
208     };
209 
210     /**
211      * @brief The DeviceEffects class stores the effects associated to a given Device Port.
212      */
213     class DeviceEffects {
214     public:
DeviceEffects(std::unique_ptr<EffectDescVector> effectDescriptors,audio_devices_t device,const std::string & address)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 
221         std::vector< sp<AudioEffect> > mEffects;
getDeviceType()222         audio_devices_t getDeviceType() const { return mDeviceType; }
getDeviceAddress()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 
232     static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
233     static audio_source_t inputSourceNameToEnum(const char *name);
234 
235     static const char *kStreamNames[AUDIO_STREAM_PUBLIC_CNT+1]; //+1 required as streams start from -1
236     audio_stream_type_t streamNameToEnum(const char *name);
237 
238     // Parse audio_effects.conf
239     status_t loadAudioEffectConfigLegacy(const char *path);
240     status_t loadAudioEffectConfig(const sp<EffectsFactoryHalInterface>& effectsFactoryHal);
241 
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,
255                           char **param,
256                           size_t *curSize,
257                           size_t *totSize);
258     size_t growParamSize(char **param,
259                          size_t size,
260                          size_t *curSize,
261                          size_t *totSize);
262 
263     // protects access to mInputSources, mInputSessions, mOutputStreams, mOutputSessions
264     // never hold AudioPolicyService::mLock when calling AudioPolicyEffects methods as
265     // those can call back into AudioPolicyService methods and try to acquire the mutex
266     Mutex mLock;
267     // 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
270     KeyedVector< audio_session_t, EffectVector* > mInputSessions;
271 
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
275     KeyedVector< audio_session_t, EffectVector* > mOutputSessions;
276 
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;
289 };
290 
291 } // namespace android
292 
293 #endif // ANDROID_AUDIOPOLICYEFFECTS_H
294