blob: 58ea24cbc9188205d37e62c08250a6b2d5a67246 [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>
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
Eric Laurent8b1e80b2014-10-07 09:08:47 -070048 // 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
bryant_liuba2b4392014-06-11 16:49:30 +080052 // Return a list of effect descriptors for default input effects
53 // associated with audioSession
Eric Laurentfb66dd92016-01-28 18:32:03 -080054 status_t queryDefaultInputEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +080055 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,
Eric Laurentfb66dd92016-01-28 18:32:03 -080062 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080063
64 // Add all input effects associated to this input
Eric Laurentfb66dd92016-01-28 18:32:03 -080065 status_t releaseInputEffects(audio_io_handle_t input,
66 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080067
68
69 // Return a list of effect descriptors for default output effects
70 // associated with audioSession
Eric Laurentfb66dd92016-01-28 18:32:03 -080071 status_t queryDefaultOutputSessionEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +080072 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,
Eric Laurentfb66dd92016-01-28 18:32:03 -080079 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080080
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,
Eric Laurentfb66dd92016-01-28 18:32:03 -080084 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080085
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:
bryant_liu890a5632014-08-20 18:06:13 +0800139 EffectVector(int session) : mSessionId(session), mRefCount(0) {}
bryant_liuba2b4392014-06-11 16:49:30 +0800140 /*virtual*/ ~EffectVector() {}
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700141
142 // Enable or disable all effects in effect vector
143 void setProcessorEnabled(bool enabled);
144
bryant_liuba2b4392014-06-11 16:49:30 +0800145 const int mSessionId;
bryant_liu890a5632014-08-20 18:06:13 +0800146 // AudioPolicyManager keeps mLock, no need for lock on reference count here
147 int mRefCount;
bryant_liuba2b4392014-06-11 16:49:30 +0800148 Vector< sp<AudioEffect> >mEffects;
149 };
150
151
152 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700153 static audio_source_t inputSourceNameToEnum(const char *name);
bryant_liuba2b4392014-06-11 16:49:30 +0800154
Eric Laurent223fd5c2014-11-11 13:43:36 -0800155 static const char *kStreamNames[AUDIO_STREAM_PUBLIC_CNT+1]; //+1 required as streams start from -1
bryant_liuba2b4392014-06-11 16:49:30 +0800156 audio_stream_type_t streamNameToEnum(const char *name);
157
bryant_liuba2b4392014-06-11 16:49:30 +0800158 // 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
Eric Laurentfb66dd92016-01-28 18:32:03 -0800182 // protects access to mInputSources, mInputSessions, mOutputStreams, mOutputSessions
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700183 Mutex mLock;
bryant_liuba2b4392014-06-11 16:49:30 +0800184 // 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
Eric Laurentfb66dd92016-01-28 18:32:03 -0800187 KeyedVector< audio_session_t, EffectVector* > mInputSessions;
bryant_liuba2b4392014-06-11 16:49:30 +0800188
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
Eric Laurentfb66dd92016-01-28 18:32:03 -0800192 KeyedVector< audio_session_t, EffectVector* > mOutputSessions;
bryant_liuba2b4392014-06-11 16:49:30 +0800193};
194
195}; // namespace android
196
197#endif // ANDROID_AUDIOPOLICYEFFECTS_H