blob: 02dfc1bb97b40ac8316605ddf4f2a90b0ed0d2a7 [file] [log] [blame]
Eric Laurent948235c2010-06-09 00:17:29 -07001/*
2 * Copyright (C) 2009 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_AUDIOEFFECT_H
18#define ANDROID_AUDIOEFFECT_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <media/IAudioFlinger.h>
Eric Laurent0f7f4ec2011-07-24 13:36:09 -070024#include <media/IAudioPolicyService.h>
Eric Laurent948235c2010-06-09 00:17:29 -070025#include <media/IEffect.h>
26#include <media/IEffectClient.h>
Eric Laurent0fb66c22011-05-17 19:16:02 -070027#include <hardware/audio_effect.h>
Eric Laurent948235c2010-06-09 00:17:29 -070028#include <media/AudioSystem.h>
29
30#include <utils/RefBase.h>
31#include <utils/Errors.h>
32#include <binder/IInterface.h>
33
34
35namespace android {
36
37// ----------------------------------------------------------------------------
38
39class effect_param_cblk_t;
40
41// ----------------------------------------------------------------------------
42
43class AudioEffect : public RefBase
44{
45public:
46
47 /*
Eric Laurent948235c2010-06-09 00:17:29 -070048 * Static methods for effects enumeration.
49 */
50
51 /*
52 * Returns the number of effects available. This method together
Eric Laurent53334cd2010-06-23 17:38:20 -070053 * with queryEffect() is used to enumerate all effects:
Eric Laurent948235c2010-06-09 00:17:29 -070054 * The enumeration sequence is:
Eric Laurent53334cd2010-06-23 17:38:20 -070055 * queryNumberEffects(&num_effects);
56 * for (i = 0; i < num_effects; i++)
57 * queryEffect(i,...);
Eric Laurent948235c2010-06-09 00:17:29 -070058 *
59 * Parameters:
Eric Laurent53334cd2010-06-23 17:38:20 -070060 * numEffects: address where the number of effects should be returned.
Eric Laurent948235c2010-06-09 00:17:29 -070061 *
62 * Returned status (from utils/Errors.h) can be:
63 * NO_ERROR successful operation.
64 * PERMISSION_DENIED could not get AudioFlinger interface
65 * NO_INIT effect library failed to initialize
66 * BAD_VALUE invalid numEffects pointer
67 *
68 * Returned value
69 * *numEffects: updated with number of effects available
70 */
71 static status_t queryNumberEffects(uint32_t *numEffects);
72
73 /*
Eric Laurent53334cd2010-06-23 17:38:20 -070074 * Returns an effect descriptor during effect
Eric Laurent948235c2010-06-09 00:17:29 -070075 * enumeration.
76 *
77 * Parameters:
Eric Laurent53334cd2010-06-23 17:38:20 -070078 * index: index of the queried effect.
79 * descriptor: address where the effect descriptor should be returned.
Eric Laurent948235c2010-06-09 00:17:29 -070080 *
81 * Returned status (from utils/Errors.h) can be:
82 * NO_ERROR successful operation.
Eric Laurent948235c2010-06-09 00:17:29 -070083 * PERMISSION_DENIED could not get AudioFlinger interface
84 * NO_INIT effect library failed to initialize
Eric Laurent53334cd2010-06-23 17:38:20 -070085 * BAD_VALUE invalid descriptor pointer or index
Eric Laurent948235c2010-06-09 00:17:29 -070086 * INVALID_OPERATION effect list has changed since last execution of queryNumberEffects()
87 *
88 * Returned value
89 * *descriptor: updated with effect descriptor
90 */
Eric Laurent53334cd2010-06-23 17:38:20 -070091 static status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
Eric Laurent948235c2010-06-09 00:17:29 -070092
93
94 /*
95 * Returns the descriptor for the specified effect uuid.
96 *
97 * Parameters:
98 * uuid: pointer to effect uuid.
99 * descriptor: address where the effect descriptor should be returned.
100 *
101 * Returned status (from utils/Errors.h) can be:
102 * NO_ERROR successful operation.
103 * PERMISSION_DENIED could not get AudioFlinger interface
104 * NO_INIT effect library failed to initialize
105 * BAD_VALUE invalid uuid or descriptor pointers
106 * NAME_NOT_FOUND no effect with this uuid found
107 *
108 * Returned value
109 * *descriptor updated with effect descriptor
110 */
Glenn Kasten67313332012-01-30 07:40:52 -0800111 static status_t getEffectDescriptor(const effect_uuid_t *uuid,
Glenn Kasten3f6d83a2012-01-26 16:25:10 -0800112 effect_descriptor_t *descriptor) /*const*/;
Eric Laurent948235c2010-06-09 00:17:29 -0700113
114
115 /*
Eric Laurent0f7f4ec2011-07-24 13:36:09 -0700116 * Returns a list of descriptors corresponding to the pre processings enabled by default
117 * on an AudioRecord with the supplied audio session ID.
118 *
119 * Parameters:
120 * audioSession: audio session ID.
121 * descriptors: address where the effect descriptors should be returned.
122 * count: as input, the maximum number of descriptor than should be returned
123 * as output, the number of descriptor returned if status is NO_ERROR or the actual
124 * number of enabled pre processings if status is NO_MEMORY
125 *
126 * Returned status (from utils/Errors.h) can be:
127 * NO_ERROR successful operation.
128 * NO_MEMORY the number of descriptor to return is more than the maximum number
129 * indicated by count.
130 * PERMISSION_DENIED could not get AudioFlinger interface
131 * NO_INIT effect library failed to initialize
132 * BAD_VALUE invalid audio session or descriptor pointers
133 *
134 * Returned value
135 * *descriptor updated with descriptors of pre processings enabled by default
136 * *count number of descriptors returned if returned status is N_ERROR.
137 * total number of pre processing enabled by default if returned status is
138 * NO_MEMORY. This happens if the count passed as input is less than the number
139 * of descriptors to return
140 */
141 static status_t queryDefaultPreProcessing(int audioSession,
142 effect_descriptor_t *descriptors,
143 uint32_t *count);
144
145 /*
Eric Laurent948235c2010-06-09 00:17:29 -0700146 * Events used by callback function (effect_callback_t).
147 */
148 enum event_type {
149 EVENT_CONTROL_STATUS_CHANGED = 0,
150 EVENT_ENABLE_STATUS_CHANGED = 1,
151 EVENT_PARAMETER_CHANGED = 2,
152 EVENT_ERROR = 3
153 };
154
155 /* Callback function notifying client application of a change in effect engine state or
156 * configuration.
157 * An effect engine can be shared by several applications but only one has the control
158 * of the engine activity and configuration at a time.
159 * The EVENT_CONTROL_STATUS_CHANGED event is received when an application loses or
160 * retrieves the control of the effect engine. Loss of control happens
161 * if another application requests the use of the engine by creating an AudioEffect for
162 * the same effect type but with a higher priority. Control is returned when the
163 * application having the control deletes its AudioEffect object.
164 * The EVENT_ENABLE_STATUS_CHANGED event is received by all applications not having the
165 * control of the effect engine when the effect is enabled or disabled.
166 * The EVENT_PARAMETER_CHANGED event is received by all applications not having the
167 * control of the effect engine when an effect parameter is changed.
168 * The EVENT_ERROR event is received when the media server process dies.
169 *
170 * Parameters:
171 *
172 * event: type of event notified (see enum AudioEffect::event_type).
173 * user: Pointer to context for use by the callback receiver.
174 * info: Pointer to optional parameter according to event type:
175 * - EVENT_CONTROL_STATUS_CHANGED: boolean indicating if control is granted (true)
176 * or stolen (false).
177 * - EVENT_ENABLE_STATUS_CHANGED: boolean indicating if effect is now enabled (true)
178 * or disabled (false).
179 * - EVENT_PARAMETER_CHANGED: pointer to a effect_param_t structure.
180 * - EVENT_ERROR: status_t indicating the error (DEAD_OBJECT when media server dies).
181 */
182
183 typedef void (*effect_callback_t)(int32_t event, void* user, void *info);
184
185
186 /* Constructor.
187 * AudioEffect is the base class for creating and controlling an effect engine from
188 * the application process. Creating an AudioEffect object will create the effect engine
189 * in the AudioFlinger if no engine of the specified type exists. If one exists, this engine
190 * will be used. The application creating the AudioEffect object (or a derived class like
191 * Reverb for instance) will either receive control of the effect engine or not, depending
192 * on the priority parameter. If priority is higher than the priority used by the current
193 * effect engine owner, the control will be transfered to the new application. Otherwise
194 * control will remain to the previous application. In this case, the new application will be
195 * notified of changes in effect engine state or control ownership by the effect callback.
196 * After creating the AudioEffect, the application must call the initCheck() method and
197 * check the creation status before trying to control the effect engine (see initCheck()).
198 * If the effect is to be applied to an AudioTrack or MediaPlayer only the application
199 * must specify the audio session ID corresponding to this player.
200 */
201
202 /* Simple Constructor.
203 */
204 AudioEffect();
205
206
207 /* Constructor.
208 *
209 * Parameters:
210 *
211 * type: type of effect created: can be null if uuid is specified. This corresponds to
212 * the OpenSL ES interface implemented by this effect.
213 * uuid: Uuid of effect created: can be null if type is specified. This uuid corresponds to
214 * a particular implementation of an effect type.
215 * priority: requested priority for effect control: the priority level corresponds to the
216 * value of priority parameter: negative values indicate lower priorities, positive values
217 * higher priorities, 0 being the normal priority.
218 * cbf: optional callback function (see effect_callback_t)
219 * user: pointer to context for use by the callback receiver.
220 * sessionID: audio session this effect is associated to. If 0, the effect will be global to
221 * the output mix. If not 0, the effect will be applied to all players
222 * (AudioTrack or MediaPLayer) within the same audio session.
Eric Laurent464d5b32011-06-17 21:29:58 -0700223 * io: HAL audio output or input stream to which this effect must be attached. Leave at 0 for
Eric Laurent948235c2010-06-09 00:17:29 -0700224 * automatic output selection by AudioFlinger.
225 */
226
227 AudioEffect(const effect_uuid_t *type,
228 const effect_uuid_t *uuid = NULL,
229 int32_t priority = 0,
Glenn Kasten3694ec12012-01-27 16:47:15 -0800230 effect_callback_t cbf = NULL,
231 void* user = NULL,
Eric Laurent948235c2010-06-09 00:17:29 -0700232 int sessionId = 0,
Eric Laurent464d5b32011-06-17 21:29:58 -0700233 audio_io_handle_t io = 0
Eric Laurent948235c2010-06-09 00:17:29 -0700234 );
235
236 /* Constructor.
237 * Same as above but with type and uuid specified by character strings
238 */
239 AudioEffect(const char *typeStr,
240 const char *uuidStr = NULL,
241 int32_t priority = 0,
Glenn Kasten3694ec12012-01-27 16:47:15 -0800242 effect_callback_t cbf = NULL,
243 void* user = NULL,
Eric Laurent948235c2010-06-09 00:17:29 -0700244 int sessionId = 0,
Eric Laurent464d5b32011-06-17 21:29:58 -0700245 audio_io_handle_t io = 0
Eric Laurent948235c2010-06-09 00:17:29 -0700246 );
247
248 /* Terminates the AudioEffect and unregisters it from AudioFlinger.
249 * The effect engine is also destroyed if this AudioEffect was the last controlling
250 * the engine.
251 */
252 ~AudioEffect();
253
254 /* Initialize an uninitialized AudioEffect.
255 * Returned status (from utils/Errors.h) can be:
256 * - NO_ERROR or ALREADY_EXISTS: successful initialization
257 * - INVALID_OPERATION: AudioEffect is already initialized
258 * - BAD_VALUE: invalid parameter
259 * - NO_INIT: audio flinger or audio hardware not initialized
260 * */
261 status_t set(const effect_uuid_t *type,
262 const effect_uuid_t *uuid = NULL,
263 int32_t priority = 0,
Glenn Kasten3694ec12012-01-27 16:47:15 -0800264 effect_callback_t cbf = NULL,
265 void* user = NULL,
Eric Laurent948235c2010-06-09 00:17:29 -0700266 int sessionId = 0,
Eric Laurent464d5b32011-06-17 21:29:58 -0700267 audio_io_handle_t io = 0
Eric Laurent948235c2010-06-09 00:17:29 -0700268 );
269
270 /* Result of constructing the AudioEffect. This must be checked
271 * before using any AudioEffect API.
272 * initCheck() can return:
273 * - NO_ERROR: the effect engine is successfully created and the application has control.
274 * - ALREADY_EXISTS: the effect engine is successfully created but the application does not
275 * have control.
276 * - NO_INIT: the effect creation failed.
277 *
278 */
279 status_t initCheck() const;
280
281
282 /* Returns the unique effect Id for the controlled effect engine. This ID is unique
283 * system wide and is used for instance in the case of auxiliary effects to attach
284 * the effect to an AudioTrack or MediaPlayer.
285 *
286 */
287 int32_t id() const { return mId; }
288
Eric Laurent0fb66c22011-05-17 19:16:02 -0700289 /* Returns a descriptor for the effect (see effect_descriptor_t in audio_effect.h).
Eric Laurent948235c2010-06-09 00:17:29 -0700290 */
291 effect_descriptor_t descriptor() const;
292
293 /* Returns effect control priority of this AudioEffect object.
294 */
295 int32_t priority() const { return mPriority; }
296
297
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700298 /* Enables or disables the effect engine.
Eric Laurent948235c2010-06-09 00:17:29 -0700299 *
300 * Parameters:
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700301 * enabled: requested enable state.
Eric Laurent948235c2010-06-09 00:17:29 -0700302 *
303 * Returned status (from utils/Errors.h) can be:
304 * - NO_ERROR: successful operation
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700305 * - INVALID_OPERATION: the application does not have control of the effect engine or the
306 * effect is already in the requested state.
Eric Laurent948235c2010-06-09 00:17:29 -0700307 */
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700308 virtual status_t setEnabled(bool enabled);
309 bool getEnabled() const;
Eric Laurent948235c2010-06-09 00:17:29 -0700310
311 /* Sets a parameter value.
312 *
313 * Parameters:
314 * param: pointer to effect_param_t structure containing the parameter
Eric Laurent0fb66c22011-05-17 19:16:02 -0700315 * and its value (See audio_effect.h).
Eric Laurent948235c2010-06-09 00:17:29 -0700316 * Returned status (from utils/Errors.h) can be:
317 * - NO_ERROR: successful operation.
318 * - INVALID_OPERATION: the application does not have control of the effect engine.
319 * - BAD_VALUE: invalid parameter identifier or value.
320 * - DEAD_OBJECT: the effect engine has been deleted.
321 */
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700322 virtual status_t setParameter(effect_param_t *param);
Eric Laurent948235c2010-06-09 00:17:29 -0700323
324 /* Prepare a new parameter value that will be set by next call to
325 * setParameterCommit(). This method can be used to set multiple parameters
326 * in a synchronous manner or to avoid multiple binder calls for each
327 * parameter.
328 *
329 * Parameters:
330 * param: pointer to effect_param_t structure containing the parameter
Eric Laurent0fb66c22011-05-17 19:16:02 -0700331 * and its value (See audio_effect.h).
Eric Laurent948235c2010-06-09 00:17:29 -0700332 *
333 * Returned status (from utils/Errors.h) can be:
334 * - NO_ERROR: successful operation.
335 * - INVALID_OPERATION: the application does not have control of the effect engine.
336 * - NO_MEMORY: no more space available in shared memory used for deferred parameter
337 * setting.
338 */
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700339 virtual status_t setParameterDeferred(effect_param_t *param);
Eric Laurent948235c2010-06-09 00:17:29 -0700340
341 /* Commit all parameter values previously prepared by setParameterDeferred().
342 *
343 * Parameters:
344 * none
345 *
346 * Returned status (from utils/Errors.h) can be:
347 * - NO_ERROR: successful operation.
348 * - INVALID_OPERATION: No new parameter values ready for commit.
349 * - BAD_VALUE: invalid parameter identifier or value: there is no indication
350 * as to which of the parameters caused this error.
351 * - DEAD_OBJECT: the effect engine has been deleted.
352 */
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700353 virtual status_t setParameterCommit();
Eric Laurent948235c2010-06-09 00:17:29 -0700354
355 /* Gets a parameter value.
356 *
357 * Parameters:
358 * param: pointer to effect_param_t structure containing the parameter
Eric Laurent0fb66c22011-05-17 19:16:02 -0700359 * and the returned value (See audio_effect.h).
Eric Laurent948235c2010-06-09 00:17:29 -0700360 *
361 * Returned status (from utils/Errors.h) can be:
362 * - NO_ERROR: successful operation.
363 * - INVALID_OPERATION: the AudioEffect was not successfully initialized.
364 * - BAD_VALUE: invalid parameter identifier.
365 * - DEAD_OBJECT: the effect engine has been deleted.
366 */
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700367 virtual status_t getParameter(effect_param_t *param);
Eric Laurent948235c2010-06-09 00:17:29 -0700368
369 /* Sends a command and receives a response to/from effect engine.
Eric Laurent0fb66c22011-05-17 19:16:02 -0700370 * See audio_effect.h for details on effect command() function, valid command codes
Eric Laurent948235c2010-06-09 00:17:29 -0700371 * and formats.
372 */
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700373 virtual status_t command(uint32_t cmdCode,
374 uint32_t cmdSize,
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700375 void *cmdData,
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700376 uint32_t *replySize,
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700377 void *replyData);
Eric Laurent948235c2010-06-09 00:17:29 -0700378
379
380 /*
381 * Utility functions.
382 */
383
384 /* Converts the string passed as first argument to the effect_uuid_t
385 * pointed to by second argument
386 */
387 static status_t stringToGuid(const char *str, effect_uuid_t *guid);
388 /* Converts the effect_uuid_t pointed to by first argument to the
389 * string passed as second argument
390 */
391 static status_t guidToString(const effect_uuid_t *guid, char *str, size_t maxLen);
392
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700393protected:
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800394 bool mEnabled; // enable state
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700395 int32_t mSessionId; // audio session ID
396 int32_t mPriority; // priority for effect control
397 status_t mStatus; // effect status
398 effect_callback_t mCbf; // callback function for status, control and
399 // parameter changes notifications
400 void* mUserData; // client context for callback function
401 effect_descriptor_t mDescriptor; // effect descriptor
402 int32_t mId; // system wide unique effect engine instance ID
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800403 Mutex mLock; // Mutex for mEnabled access
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700404
Eric Laurent948235c2010-06-09 00:17:29 -0700405private:
406
407 // Implements the IEffectClient interface
408 class EffectClient : public android::BnEffectClient, public android::IBinder::DeathRecipient
409 {
410 public:
411
412 EffectClient(AudioEffect *effect) : mEffect(effect){}
413
414 // IEffectClient
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700415 virtual void controlStatusChanged(bool controlGranted) {
416 mEffect->controlStatusChanged(controlGranted);
417 }
418 virtual void enableStatusChanged(bool enabled) {
419 mEffect->enableStatusChanged(enabled);
420 }
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700421 virtual void commandExecuted(uint32_t cmdCode,
422 uint32_t cmdSize,
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700423 void *pCmdData,
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700424 uint32_t replySize,
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700425 void *pReplyData) {
Eric Laurent948235c2010-06-09 00:17:29 -0700426 mEffect->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
427 }
428
429 // IBinder::DeathRecipient
430 virtual void binderDied(const wp<IBinder>& who) {mEffect->binderDied();}
431
432 private:
433 AudioEffect *mEffect;
434 };
435
436
437 friend class EffectClient;
438
439 // IEffectClient
440 void controlStatusChanged(bool controlGranted);
441 void enableStatusChanged(bool enabled);
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700442 void commandExecuted(uint32_t cmdCode,
443 uint32_t cmdSize,
444 void *pCmdData,
445 uint32_t replySize,
446 void *pReplyData);
Eric Laurent948235c2010-06-09 00:17:29 -0700447 void binderDied();
448
449
450 sp<IEffect> mIEffect; // IEffect binder interface
451 sp<EffectClient> mIEffectClient; // IEffectClient implementation
452 sp<IMemory> mCblkMemory; // shared memory for deferred parameter setting
453 effect_param_cblk_t* mCblk; // control block for deferred parameter setting
Eric Laurent948235c2010-06-09 00:17:29 -0700454};
455
456
457}; // namespace android
458
459#endif // ANDROID_AUDIOEFFECT_H