• 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 <cutils/misc.h>
24 #include <media/AudioEffect.h>
25 #include <system/audio.h>
26 #include <utils/Vector.h>
27 #include <utils/SortedVector.h>
28 
29 namespace android {
30 
31 // ----------------------------------------------------------------------------
32 
33 // AudioPolicyEffects class
34 // This class will manage all effects attached to input and output streams in
35 // AudioPolicyService as configured in audio_effects.conf.
36 class AudioPolicyEffects : public RefBase
37 {
38 
39 public:
40 
41     // The constructor will parse audio_effects.conf
42     // First it will look whether vendor specific file exists,
43     // otherwise it will parse the system default file.
44 	         AudioPolicyEffects();
45     virtual ~AudioPolicyEffects();
46 
47     // NOTE: methods on AudioPolicyEffects should never be called with the AudioPolicyService
48     // main mutex (mLock) held as they will indirectly call back into AudioPolicyService when
49     // managing audio effects.
50 
51     // Return a list of effect descriptors for default input effects
52     // associated with audioSession
53     status_t queryDefaultInputEffects(audio_session_t audioSession,
54                              effect_descriptor_t *descriptors,
55                              uint32_t *count);
56 
57     // Add all input effects associated with this input
58     // Effects are attached depending on the audio_source_t
59     status_t addInputEffects(audio_io_handle_t input,
60                              audio_source_t inputSource,
61                              audio_session_t audioSession);
62 
63     // Add all input effects associated to this input
64     status_t releaseInputEffects(audio_io_handle_t input,
65                                  audio_session_t audioSession);
66 
67     // Return a list of effect descriptors for default output effects
68     // associated with audioSession
69     status_t queryDefaultOutputSessionEffects(audio_session_t audioSession,
70                              effect_descriptor_t *descriptors,
71                              uint32_t *count);
72 
73     // Add all output effects associated to this output
74     // Effects are attached depending on the audio_stream_type_t
75     status_t addOutputSessionEffects(audio_io_handle_t output,
76                              audio_stream_type_t stream,
77                              audio_session_t audioSession);
78 
79     // release all output effects associated with this output stream and audiosession
80     status_t releaseOutputSessionEffects(audio_io_handle_t output,
81                              audio_stream_type_t stream,
82                              audio_session_t audioSession);
83 
84     // Add the effect to the list of default effects for sources of type |source|.
85     status_t addSourceDefaultEffect(const effect_uuid_t *type,
86                                     const String16& opPackageName,
87                                     const effect_uuid_t *uuid,
88                                     int32_t priority,
89                                     audio_source_t source,
90                                     audio_unique_id_t* id);
91 
92     // Add the effect to the list of default effects for streams of a given usage.
93     status_t addStreamDefaultEffect(const effect_uuid_t *type,
94                                     const String16& opPackageName,
95                                     const effect_uuid_t *uuid,
96                                     int32_t priority,
97                                     audio_usage_t usage,
98                                     audio_unique_id_t* id);
99 
100     // Remove the default source effect from wherever it's attached.
101     status_t removeSourceDefaultEffect(audio_unique_id_t id);
102 
103     // Remove the default stream effect from wherever it's attached.
104     status_t removeStreamDefaultEffect(audio_unique_id_t id);
105 
106 private:
107 
108     // class to store the description of an effects and its parameters
109     // as defined in audio_effects.conf
110     class EffectDesc {
111     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)112         EffectDesc(const char *name,
113                    const effect_uuid_t& typeUuid,
114                    const String16& opPackageName,
115                    const effect_uuid_t& uuid,
116                    uint32_t priority,
117                    audio_unique_id_t id) :
118                         mName(strdup(name)),
119                         mTypeUuid(typeUuid),
120                         mOpPackageName(opPackageName),
121                         mUuid(uuid),
122                         mPriority(priority),
123                         mId(id) { }
EffectDesc(const char * name,const effect_uuid_t & uuid)124         EffectDesc(const char *name, const effect_uuid_t& uuid) :
125                         EffectDesc(name,
126                                    *EFFECT_UUID_NULL,
127                                    String16(""),
128                                    uuid,
129                                    0,
130                                    AUDIO_UNIQUE_ID_ALLOCATE) { }
EffectDesc(const EffectDesc & orig)131         EffectDesc(const EffectDesc& orig) :
132                         mName(strdup(orig.mName)),
133                         mTypeUuid(orig.mTypeUuid),
134                         mOpPackageName(orig.mOpPackageName),
135                         mUuid(orig.mUuid),
136                         mPriority(orig.mPriority),
137                         mId(orig.mId) {
138                             // deep copy mParams
139                             for (size_t k = 0; k < orig.mParams.size(); k++) {
140                                 effect_param_t *origParam = orig.mParams[k];
141                                 // psize and vsize are rounded up to an int boundary for allocation
142                                 size_t origSize = sizeof(effect_param_t) +
143                                                   ((origParam->psize + 3) & ~3) +
144                                                   ((origParam->vsize + 3) & ~3);
145                                 effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
146                                 memcpy(dupParam, origParam, origSize);
147                                 // This works because the param buffer allocation is also done by
148                                 // multiples of 4 bytes originally. In theory we should memcpy only
149                                 // the actual param size, that is without rounding vsize.
150                                 mParams.add(dupParam);
151                             }
152                         }
~EffectDesc()153         /*virtual*/ ~EffectDesc() {
154             free(mName);
155             for (size_t k = 0; k < mParams.size(); k++) {
156                 free(mParams[k]);
157             }
158         }
159         char *mName;
160         effect_uuid_t mTypeUuid;
161         String16 mOpPackageName;
162         effect_uuid_t mUuid;
163         int32_t mPriority;
164         audio_unique_id_t mId;
165         Vector <effect_param_t *> mParams;
166     };
167 
168     // class to store voctor of EffectDesc
169     class EffectDescVector {
170     public:
EffectDescVector()171         EffectDescVector() {}
~EffectDescVector()172         /*virtual*/ ~EffectDescVector() {
173             for (size_t j = 0; j < mEffects.size(); j++) {
174                 delete mEffects[j];
175             }
176         }
177         Vector <EffectDesc *> mEffects;
178     };
179 
180     // class to store voctor of AudioEffects
181     class EffectVector {
182     public:
EffectVector(audio_session_t session)183         explicit EffectVector(audio_session_t session) : mSessionId(session), mRefCount(0) {}
~EffectVector()184         /*virtual*/ ~EffectVector() {}
185 
186         // Enable or disable all effects in effect vector
187         void setProcessorEnabled(bool enabled);
188 
189         const audio_session_t mSessionId;
190         // AudioPolicyManager keeps mLock, no need for lock on reference count here
191         int mRefCount;
192         Vector< sp<AudioEffect> >mEffects;
193     };
194 
195 
196     static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
197     static audio_source_t inputSourceNameToEnum(const char *name);
198 
199     static const char *kStreamNames[AUDIO_STREAM_PUBLIC_CNT+1]; //+1 required as streams start from -1
200     audio_stream_type_t streamNameToEnum(const char *name);
201 
202     // Parse audio_effects.conf
203     status_t loadAudioEffectConfig(const char *path); // TODO: add legacy in the name
204     status_t loadAudioEffectXmlConfig(); // TODO: remove "Xml" in the name
205 
206     // Load all effects descriptors in configuration file
207     status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
208     EffectDesc *loadEffect(cnode *root);
209 
210     // Load all automatic effect configurations
211     status_t loadInputEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
212     status_t loadStreamEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
213     EffectDescVector *loadEffectConfig(cnode *root, const Vector <EffectDesc *>& effects);
214 
215     // Load all automatic effect parameters
216     void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
217     effect_param_t *loadEffectParameter(cnode *root);
218     size_t readParamValue(cnode *node,
219                           char **param,
220                           size_t *curSize,
221                           size_t *totSize);
222     size_t growParamSize(char **param,
223                          size_t size,
224                          size_t *curSize,
225                          size_t *totSize);
226 
227     // protects access to mInputSources, mInputSessions, mOutputStreams, mOutputSessions
228     // never hold AudioPolicyService::mLock when calling AudioPolicyEffects methods as
229     // those can call back into AudioPolicyService methods and try to acquire the mutex
230     Mutex mLock;
231     // Automatic input effects are configured per audio_source_t
232     KeyedVector< audio_source_t, EffectDescVector* > mInputSources;
233     // Automatic input effects are unique for audio_io_handle_t
234     KeyedVector< audio_session_t, EffectVector* > mInputSessions;
235 
236     // Automatic output effects are organized per audio_stream_type_t
237     KeyedVector< audio_stream_type_t, EffectDescVector* > mOutputStreams;
238     // Automatic output effects are unique for audiosession ID
239     KeyedVector< audio_session_t, EffectVector* > mOutputSessions;
240 };
241 
242 } // namespace android
243 
244 #endif // ANDROID_AUDIOPOLICYEFFECTS_H
245