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