blob: 69367b1dc092225e81ad6458ebbeba8a214799a6 [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>
bryant_liuba2b4392014-06-11 16:49:30 +080026#include <utils/Vector.h>
27#include <utils/SortedVector.h>
28
29namespace 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.
36class AudioPolicyEffects : public RefBase
37{
38
39public:
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
Eric Laurent8b1e80b2014-10-07 09:08:47 -070047 // 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
bryant_liuba2b4392014-06-11 16:49:30 +080051 // Return a list of effect descriptors for default input effects
52 // associated with audioSession
Eric Laurentfb66dd92016-01-28 18:32:03 -080053 status_t queryDefaultInputEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +080054 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,
Eric Laurentfb66dd92016-01-28 18:32:03 -080061 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080062
63 // Add all input effects associated to this input
Eric Laurentfb66dd92016-01-28 18:32:03 -080064 status_t releaseInputEffects(audio_io_handle_t input,
65 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080066
bryant_liuba2b4392014-06-11 16:49:30 +080067 // Return a list of effect descriptors for default output effects
68 // associated with audioSession
Eric Laurentfb66dd92016-01-28 18:32:03 -080069 status_t queryDefaultOutputSessionEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +080070 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,
Eric Laurentfb66dd92016-01-28 18:32:03 -080077 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080078
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,
Eric Laurentfb66dd92016-01-28 18:32:03 -080082 audio_session_t audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +080083
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -070084 // Add the effect to the list of default effects for streams of type |stream|.
85 status_t addStreamDefaultEffect(const effect_uuid_t *type,
86 const String16& opPackageName,
87 const effect_uuid_t *uuid,
88 int32_t priority,
89 audio_usage_t usage,
90 audio_unique_id_t* id);
91
92 // Remove the default stream effect from wherever it's attached.
93 status_t removeStreamDefaultEffect(audio_unique_id_t id);
94
bryant_liuba2b4392014-06-11 16:49:30 +080095private:
96
97 // class to store the description of an effects and its parameters
98 // as defined in audio_effects.conf
99 class EffectDesc {
100 public:
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700101 EffectDesc(const char *name,
102 const effect_uuid_t& typeUuid,
103 const String16& opPackageName,
104 const effect_uuid_t& uuid,
105 uint32_t priority,
106 audio_unique_id_t id) :
bryant_liuba2b4392014-06-11 16:49:30 +0800107 mName(strdup(name)),
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700108 mTypeUuid(typeUuid),
109 mOpPackageName(opPackageName),
110 mUuid(uuid),
111 mPriority(priority),
112 mId(id) { }
113 EffectDesc(const char *name, const effect_uuid_t& uuid) :
114 EffectDesc(name,
115 *EFFECT_UUID_NULL,
116 String16(""),
117 uuid,
118 0,
119 AUDIO_UNIQUE_ID_ALLOCATE) { }
bryant_liuba2b4392014-06-11 16:49:30 +0800120 EffectDesc(const EffectDesc& orig) :
121 mName(strdup(orig.mName)),
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700122 mTypeUuid(orig.mTypeUuid),
123 mOpPackageName(orig.mOpPackageName),
124 mUuid(orig.mUuid),
125 mPriority(orig.mPriority),
126 mId(orig.mId) {
bryant_liuba2b4392014-06-11 16:49:30 +0800127 // deep copy mParams
128 for (size_t k = 0; k < orig.mParams.size(); k++) {
129 effect_param_t *origParam = orig.mParams[k];
130 // psize and vsize are rounded up to an int boundary for allocation
131 size_t origSize = sizeof(effect_param_t) +
132 ((origParam->psize + 3) & ~3) +
133 ((origParam->vsize + 3) & ~3);
134 effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
135 memcpy(dupParam, origParam, origSize);
136 // This works because the param buffer allocation is also done by
137 // multiples of 4 bytes originally. In theory we should memcpy only
138 // the actual param size, that is without rounding vsize.
139 mParams.add(dupParam);
140 }
141 }
142 /*virtual*/ ~EffectDesc() {
143 free(mName);
144 for (size_t k = 0; k < mParams.size(); k++) {
145 free(mParams[k]);
146 }
147 }
148 char *mName;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700149 effect_uuid_t mTypeUuid;
150 String16 mOpPackageName;
bryant_liuba2b4392014-06-11 16:49:30 +0800151 effect_uuid_t mUuid;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700152 int32_t mPriority;
153 audio_unique_id_t mId;
bryant_liuba2b4392014-06-11 16:49:30 +0800154 Vector <effect_param_t *> mParams;
155 };
156
157 // class to store voctor of EffectDesc
158 class EffectDescVector {
159 public:
160 EffectDescVector() {}
161 /*virtual*/ ~EffectDescVector() {
162 for (size_t j = 0; j < mEffects.size(); j++) {
163 delete mEffects[j];
164 }
165 }
166 Vector <EffectDesc *> mEffects;
167 };
168
169 // class to store voctor of AudioEffects
170 class EffectVector {
171 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700172 explicit EffectVector(audio_session_t session) : mSessionId(session), mRefCount(0) {}
bryant_liuba2b4392014-06-11 16:49:30 +0800173 /*virtual*/ ~EffectVector() {}
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700174
175 // Enable or disable all effects in effect vector
176 void setProcessorEnabled(bool enabled);
177
Glenn Kastend848eb42016-03-08 13:42:11 -0800178 const audio_session_t mSessionId;
bryant_liu890a5632014-08-20 18:06:13 +0800179 // AudioPolicyManager keeps mLock, no need for lock on reference count here
180 int mRefCount;
bryant_liuba2b4392014-06-11 16:49:30 +0800181 Vector< sp<AudioEffect> >mEffects;
182 };
183
184
185 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700186 static audio_source_t inputSourceNameToEnum(const char *name);
bryant_liuba2b4392014-06-11 16:49:30 +0800187
Eric Laurent223fd5c2014-11-11 13:43:36 -0800188 static const char *kStreamNames[AUDIO_STREAM_PUBLIC_CNT+1]; //+1 required as streams start from -1
bryant_liuba2b4392014-06-11 16:49:30 +0800189 audio_stream_type_t streamNameToEnum(const char *name);
190
bryant_liuba2b4392014-06-11 16:49:30 +0800191 // Parse audio_effects.conf
Kevin Rocard4fb615c2017-06-26 10:28:13 -0700192 status_t loadAudioEffectConfig(const char *path); // TODO: add legacy in the name
193 status_t loadAudioEffectXmlConfig(); // TODO: remove "Xml" in the name
bryant_liuba2b4392014-06-11 16:49:30 +0800194
195 // Load all effects descriptors in configuration file
196 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
197 EffectDesc *loadEffect(cnode *root);
198
199 // Load all automatic effect configurations
200 status_t loadInputEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
201 status_t loadStreamEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects);
202 EffectDescVector *loadEffectConfig(cnode *root, const Vector <EffectDesc *>& effects);
203
204 // Load all automatic effect parameters
205 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
206 effect_param_t *loadEffectParameter(cnode *root);
207 size_t readParamValue(cnode *node,
Eric Laurent138ed172016-02-10 10:40:44 -0800208 char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800209 size_t *curSize,
210 size_t *totSize);
Eric Laurent138ed172016-02-10 10:40:44 -0800211 size_t growParamSize(char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800212 size_t size,
213 size_t *curSize,
214 size_t *totSize);
215
Eric Laurentfb66dd92016-01-28 18:32:03 -0800216 // protects access to mInputSources, mInputSessions, mOutputStreams, mOutputSessions
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700217 Mutex mLock;
bryant_liuba2b4392014-06-11 16:49:30 +0800218 // Automatic input effects are configured per audio_source_t
219 KeyedVector< audio_source_t, EffectDescVector* > mInputSources;
220 // Automatic input effects are unique for audio_io_handle_t
Eric Laurentfb66dd92016-01-28 18:32:03 -0800221 KeyedVector< audio_session_t, EffectVector* > mInputSessions;
bryant_liuba2b4392014-06-11 16:49:30 +0800222
223 // Automatic output effects are organized per audio_stream_type_t
224 KeyedVector< audio_stream_type_t, EffectDescVector* > mOutputStreams;
225 // Automatic output effects are unique for audiosession ID
Eric Laurentfb66dd92016-01-28 18:32:03 -0800226 KeyedVector< audio_session_t, EffectVector* > mOutputSessions;
bryant_liuba2b4392014-06-11 16:49:30 +0800227};
228
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800229} // namespace android
bryant_liuba2b4392014-06-11 16:49:30 +0800230
231#endif // ANDROID_AUDIOPOLICYEFFECTS_H