AudioPolicyEffects.h revision a7e04803ffa0d8d4cc51a122f68bf5038422aabb
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    // Return a list of effect descriptors for default input effects
49    // associated with audioSession
50    status_t queryDefaultInputEffects(int audioSession,
51                             effect_descriptor_t *descriptors,
52                             uint32_t *count);
53
54    // Add all input effects associated with this input
55    // Effects are attached depending on the audio_source_t
56    status_t addInputEffects(audio_io_handle_t input,
57                             audio_source_t inputSource,
58                             int audioSession);
59
60    // Add all input effects associated to this input
61    status_t releaseInputEffects(audio_io_handle_t input);
62
63
64    // Return a list of effect descriptors for default output effects
65    // associated with audioSession
66    status_t queryDefaultOutputSessionEffects(int audioSession,
67                             effect_descriptor_t *descriptors,
68                             uint32_t *count);
69
70    // Add all output effects associated to this output
71    // Effects are attached depending on the audio_stream_type_t
72    status_t addOutputSessionEffects(audio_io_handle_t output,
73                             audio_stream_type_t stream,
74                             int audioSession);
75
76    // release all output effects associated with this output stream and audiosession
77    status_t releaseOutputSessionEffects(audio_io_handle_t output,
78                             audio_stream_type_t stream,
79                             int audioSession);
80
81private:
82
83    // class to store the description of an effects and its parameters
84    // as defined in audio_effects.conf
85    class EffectDesc {
86    public:
87        EffectDesc(const char *name, const effect_uuid_t& uuid) :
88                        mName(strdup(name)),
89                        mUuid(uuid) { }
90        EffectDesc(const EffectDesc& orig) :
91                        mName(strdup(orig.mName)),
92                        mUuid(orig.mUuid) {
93                            // deep copy mParams
94                            for (size_t k = 0; k < orig.mParams.size(); k++) {
95                                effect_param_t *origParam = orig.mParams[k];
96                                // psize and vsize are rounded up to an int boundary for allocation
97                                size_t origSize = sizeof(effect_param_t) +
98                                                  ((origParam->psize + 3) & ~3) +
99                                                  ((origParam->vsize + 3) & ~3);
100                                effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
101                                memcpy(dupParam, origParam, origSize);
102                                // This works because the param buffer allocation is also done by
103                                // multiples of 4 bytes originally. In theory we should memcpy only
104                                // the actual param size, that is without rounding vsize.
105                                mParams.add(dupParam);
106                            }
107                        }
108        /*virtual*/ ~EffectDesc() {
109            free(mName);
110            for (size_t k = 0; k < mParams.size(); k++) {
111                free(mParams[k]);
112            }
113        }
114        char *mName;
115        effect_uuid_t mUuid;
116        Vector <effect_param_t *> mParams;
117    };
118
119    // class to store voctor of EffectDesc
120    class EffectDescVector {
121    public:
122        EffectDescVector() {}
123        /*virtual*/ ~EffectDescVector() {
124            for (size_t j = 0; j < mEffects.size(); j++) {
125                delete mEffects[j];
126            }
127        }
128        Vector <EffectDesc *> mEffects;
129    };
130
131    // class to store voctor of AudioEffects
132    class EffectVector {
133    public:
134        EffectVector(int session) : mSessionId(session) {}
135        /*virtual*/ ~EffectVector() {}
136        const int mSessionId;
137        Vector< sp<AudioEffect> >mEffects;
138    };
139
140
141    static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
142    audio_source_t inputSourceNameToEnum(const char *name);
143
144    static const char *kStreamNames[AUDIO_STREAM_CNT+1]; //+1 required as streams start from -1
145    audio_stream_type_t streamNameToEnum(const char *name);
146
147    // Enable or disable all effects in effect vector
148    void setProcessorEnabled(const EffectVector *effectVector, bool enabled);
149
150    // Parse audio_effects.conf
151    status_t loadAudioEffectConfig(const char *path);
152
153    // Load all effects descriptors in configuration file
154    status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
155    EffectDesc *loadEffect(cnode *root);
156
157    // Load all automatic effect configurations
158    status_t loadInputEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
159    status_t loadStreamEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
160    EffectDescVector *loadEffectConfig(cnode *root, const Vector <EffectDesc *>& effects);
161
162    // Load all automatic effect parameters
163    void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
164    effect_param_t *loadEffectParameter(cnode *root);
165    size_t readParamValue(cnode *node,
166                          char *param,
167                          size_t *curSize,
168                          size_t *totSize);
169    size_t growParamSize(char *param,
170                         size_t size,
171                         size_t *curSize,
172                         size_t *totSize);
173
174    // Automatic input effects are configured per audio_source_t
175    KeyedVector< audio_source_t, EffectDescVector* > mInputSources;
176    // Automatic input effects are unique for audio_io_handle_t
177    KeyedVector< audio_io_handle_t, EffectVector* > mInputs;
178
179    // Automatic output effects are organized per audio_stream_type_t
180    KeyedVector< audio_stream_type_t, EffectDescVector* > mOutputStreams;
181    // Automatic output effects are unique for audiosession ID
182    KeyedVector< int32_t, EffectVector* > mOutputSessions;
183};
184
185}; // namespace android
186
187#endif // ANDROID_AUDIOPOLICYEFFECTS_H
188