AudioEffect.h revision 57dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfc
1/*
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>
24#include <media/IAudioPolicyService.h>
25#include <media/IEffect.h>
26#include <media/IEffectClient.h>
27#include <hardware/audio_effect.h>
28#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    /*
48     *  Static methods for effects enumeration.
49     */
50
51    /*
52     * Returns the number of effects available. This method together
53     * with queryEffect() is used to enumerate all effects:
54     * The enumeration sequence is:
55     *      queryNumberEffects(&num_effects);
56     *      for (i = 0; i < num_effects; i++)
57     *          queryEffect(i,...);
58     *
59     * Parameters:
60     *      numEffects:    address where the number of effects should be returned.
61     *
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    /*
74     * Returns an effect descriptor during effect
75     * enumeration.
76     *
77     * Parameters:
78     *      index:      index of the queried effect.
79     *      descriptor: address where the effect descriptor should be returned.
80     *
81     * Returned status (from utils/Errors.h) can be:
82     *      NO_ERROR        successful operation.
83     *      PERMISSION_DENIED could not get AudioFlinger interface
84     *      NO_INIT         effect library failed to initialize
85     *      BAD_VALUE       invalid descriptor pointer or index
86     *      INVALID_OPERATION  effect list has changed since last execution of queryNumberEffects()
87     *
88     * Returned value
89     *   *descriptor:     updated with effect descriptor
90     */
91    static status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
92
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     */
111    static status_t getEffectDescriptor(effect_uuid_t *uuid, effect_descriptor_t *descriptor);
112
113
114    /*
115     * Returns a list of descriptors corresponding to the pre processings enabled by default
116     * on an AudioRecord with the supplied audio session ID.
117     *
118     * Parameters:
119     *      audioSession:  audio session ID.
120     *      descriptors: address where the effect descriptors should be returned.
121     *      count: as input, the maximum number of descriptor than should be returned
122     *             as output, the number of descriptor returned if status is NO_ERROR or the actual
123     *             number of enabled pre processings if status is NO_MEMORY
124     *
125     * Returned status (from utils/Errors.h) can be:
126     *      NO_ERROR        successful operation.
127     *      NO_MEMORY       the number of descriptor to return is more than the maximum number
128     *                      indicated by count.
129     *      PERMISSION_DENIED could not get AudioFlinger interface
130     *      NO_INIT         effect library failed to initialize
131     *      BAD_VALUE       invalid audio session or descriptor pointers
132     *
133     * Returned value
134     *   *descriptor updated with descriptors of pre processings enabled by default
135     *   *count      number of descriptors returned if returned status is N_ERROR.
136     *               total number of pre processing enabled by default if returned status is
137     *               NO_MEMORY. This happens if the count passed as input is less than the number
138     *               of descriptors to return
139     */
140    static status_t queryDefaultPreProcessing(int audioSession,
141                                              effect_descriptor_t *descriptors,
142                                              uint32_t *count);
143
144    /*
145     * Events used by callback function (effect_callback_t).
146     */
147    enum event_type {
148        EVENT_CONTROL_STATUS_CHANGED = 0,
149        EVENT_ENABLE_STATUS_CHANGED = 1,
150        EVENT_PARAMETER_CHANGED = 2,
151        EVENT_ERROR = 3
152    };
153
154    /* Callback function notifying client application of a change in effect engine state or
155     * configuration.
156     * An effect engine can be shared by several applications but only one has the control
157     * of the engine activity and configuration at a time.
158     * The EVENT_CONTROL_STATUS_CHANGED event is received when an application loses or
159     * retrieves the control of the effect engine. Loss of control happens
160     * if another application requests the use of the engine by creating an AudioEffect for
161     * the same effect type but with a higher priority. Control is returned when the
162     * application having the control deletes its AudioEffect object.
163     * The EVENT_ENABLE_STATUS_CHANGED event is received by all applications not having the
164     * control of the effect engine when the effect is enabled or disabled.
165     * The EVENT_PARAMETER_CHANGED event is received by all applications not having the
166     * control of the effect engine when an effect parameter is changed.
167     * The EVENT_ERROR event is received when the media server process dies.
168     *
169     * Parameters:
170     *
171     * event:   type of event notified (see enum AudioEffect::event_type).
172     * user:    Pointer to context for use by the callback receiver.
173     * info:    Pointer to optional parameter according to event type:
174     *  - EVENT_CONTROL_STATUS_CHANGED:  boolean indicating if control is granted (true)
175     *  or stolen (false).
176     *  - EVENT_ENABLE_STATUS_CHANGED: boolean indicating if effect is now enabled (true)
177     *  or disabled (false).
178     *  - EVENT_PARAMETER_CHANGED: pointer to a effect_param_t structure.
179     *  - EVENT_ERROR:  status_t indicating the error (DEAD_OBJECT when media server dies).
180     */
181
182    typedef void (*effect_callback_t)(int32_t event, void* user, void *info);
183
184
185    /* Constructor.
186     * AudioEffect is the base class for creating and controlling an effect engine from
187     * the application process. Creating an AudioEffect object will create the effect engine
188     * in the AudioFlinger if no engine of the specified type exists. If one exists, this engine
189     * will be used. The application creating the AudioEffect object (or a derived class like
190     * Reverb for instance) will either receive control of the effect engine or not, depending
191     * on the priority parameter. If priority is higher than the priority used by the current
192     * effect engine owner, the control will be transfered to the new application. Otherwise
193     * control will remain to the previous application. In this case, the new application will be
194     * notified of changes in effect engine state or control ownership by the effect callback.
195     * After creating the AudioEffect, the application must call the initCheck() method and
196     * check the creation status before trying to control the effect engine (see initCheck()).
197     * If the effect is to be applied to an AudioTrack or MediaPlayer only the application
198     * must specify the audio session ID corresponding to this player.
199     */
200
201    /* Simple Constructor.
202     */
203    AudioEffect();
204
205
206    /* Constructor.
207     *
208     * Parameters:
209     *
210     * type:  type of effect created: can be null if uuid is specified. This corresponds to
211     *        the OpenSL ES interface implemented by this effect.
212     * uuid:  Uuid of effect created: can be null if type is specified. This uuid corresponds to
213     *        a particular implementation of an effect type.
214     * priority:    requested priority for effect control: the priority level corresponds to the
215     *      value of priority parameter: negative values indicate lower priorities, positive values
216     *      higher priorities, 0 being the normal priority.
217     * cbf:         optional callback function (see effect_callback_t)
218     * user:        pointer to context for use by the callback receiver.
219     * sessionID:   audio session this effect is associated to. If 0, the effect will be global to
220     *      the output mix. If not 0, the effect will be applied to all players
221     *      (AudioTrack or MediaPLayer) within the same audio session.
222     * io:  HAL audio output or input stream to which this effect must be attached. Leave at 0 for
223     *      automatic output selection by AudioFlinger.
224     */
225
226    AudioEffect(const effect_uuid_t *type,
227                const effect_uuid_t *uuid = NULL,
228                  int32_t priority = 0,
229                  effect_callback_t cbf = 0,
230                  void* user = 0,
231                  int sessionId = 0,
232                  audio_io_handle_t io = 0
233                  );
234
235    /* Constructor.
236     *      Same as above but with type and uuid specified by character strings
237     */
238    AudioEffect(const char *typeStr,
239                    const char *uuidStr = NULL,
240                    int32_t priority = 0,
241                    effect_callback_t cbf = 0,
242                    void* user = 0,
243                    int sessionId = 0,
244                    audio_io_handle_t io = 0
245                    );
246
247    /* Terminates the AudioEffect and unregisters it from AudioFlinger.
248     * The effect engine is also destroyed if this AudioEffect was the last controlling
249     * the engine.
250     */
251                        ~AudioEffect();
252
253    /* Initialize an uninitialized AudioEffect.
254    * Returned status (from utils/Errors.h) can be:
255    *  - NO_ERROR or ALREADY_EXISTS: successful initialization
256    *  - INVALID_OPERATION: AudioEffect is already initialized
257    *  - BAD_VALUE: invalid parameter
258    *  - NO_INIT: audio flinger or audio hardware not initialized
259    * */
260            status_t    set(const effect_uuid_t *type,
261                            const effect_uuid_t *uuid = NULL,
262                            int32_t priority = 0,
263                            effect_callback_t cbf = 0,
264                            void* user = 0,
265                            int sessionId = 0,
266                            audio_io_handle_t io = 0
267                            );
268
269    /* Result of constructing the AudioEffect. This must be checked
270     * before using any AudioEffect API.
271     * initCheck() can return:
272     *  - NO_ERROR:    the effect engine is successfully created and the application has control.
273     *  - ALREADY_EXISTS: the effect engine is successfully created but the application does not
274     *              have control.
275     *  - NO_INIT:     the effect creation failed.
276     *
277     */
278            status_t    initCheck() const;
279
280
281    /* Returns the unique effect Id for the controlled effect engine. This ID is unique
282     * system wide and is used for instance in the case of auxiliary effects to attach
283     * the effect to an AudioTrack or MediaPlayer.
284     *
285     */
286            int32_t     id() const { return mId; }
287
288    /* Returns a descriptor for the effect (see effect_descriptor_t in audio_effect.h).
289     */
290            effect_descriptor_t descriptor() const;
291
292    /* Returns effect control priority of this AudioEffect object.
293     */
294            int32_t     priority() const { return mPriority; }
295
296
297    /* Enables or disables the effect engine.
298     *
299     * Parameters:
300     *  enabled: requested enable state.
301     *
302     * Returned status (from utils/Errors.h) can be:
303     *  - NO_ERROR: successful operation
304     *  - INVALID_OPERATION: the application does not have control of the effect engine or the
305     *  effect is already in the requested state.
306     */
307    virtual status_t    setEnabled(bool enabled);
308            bool        getEnabled() const;
309
310    /* Sets a parameter value.
311     *
312     * Parameters:
313     *      param:  pointer to effect_param_t structure containing the parameter
314     *          and its value (See audio_effect.h).
315     * Returned status (from utils/Errors.h) can be:
316     *  - NO_ERROR: successful operation.
317     *  - INVALID_OPERATION: the application does not have control of the effect engine.
318     *  - BAD_VALUE: invalid parameter identifier or value.
319     *  - DEAD_OBJECT: the effect engine has been deleted.
320     */
321     virtual status_t   setParameter(effect_param_t *param);
322
323    /* Prepare a new parameter value that will be set by next call to
324     * setParameterCommit(). This method can be used to set multiple parameters
325     * in a synchronous manner or to avoid multiple binder calls for each
326     * parameter.
327     *
328     * Parameters:
329     *      param:  pointer to effect_param_t structure containing the parameter
330     *          and its value (See audio_effect.h).
331     *
332     * Returned status (from utils/Errors.h) can be:
333     *  - NO_ERROR: successful operation.
334     *  - INVALID_OPERATION: the application does not have control of the effect engine.
335     *  - NO_MEMORY: no more space available in shared memory used for deferred parameter
336     *  setting.
337     */
338     virtual status_t   setParameterDeferred(effect_param_t *param);
339
340     /* Commit all parameter values previously prepared by setParameterDeferred().
341      *
342      * Parameters:
343      *     none
344      *
345      * Returned status (from utils/Errors.h) can be:
346      *  - NO_ERROR: successful operation.
347      *  - INVALID_OPERATION: No new parameter values ready for commit.
348      *  - BAD_VALUE: invalid parameter identifier or value: there is no indication
349      *     as to which of the parameters caused this error.
350      *  - DEAD_OBJECT: the effect engine has been deleted.
351      */
352     virtual status_t   setParameterCommit();
353
354    /* Gets a parameter value.
355     *
356     * Parameters:
357     *      param:  pointer to effect_param_t structure containing the parameter
358     *          and the returned value (See audio_effect.h).
359     *
360     * Returned status (from utils/Errors.h) can be:
361     *  - NO_ERROR: successful operation.
362     *  - INVALID_OPERATION: the AudioEffect was not successfully initialized.
363     *  - BAD_VALUE: invalid parameter identifier.
364     *  - DEAD_OBJECT: the effect engine has been deleted.
365     */
366     virtual status_t   getParameter(effect_param_t *param);
367
368     /* Sends a command and receives a response to/from effect engine.
369      *     See audio_effect.h for details on effect command() function, valid command codes
370      *     and formats.
371      */
372     virtual status_t command(uint32_t cmdCode,
373                              uint32_t cmdSize,
374                              void *cmdData,
375                              uint32_t *replySize,
376                              void *replyData);
377
378
379     /*
380      * Utility functions.
381      */
382
383     /* Converts the string passed as first argument to the effect_uuid_t
384      * pointed to by second argument
385      */
386     static status_t stringToGuid(const char *str, effect_uuid_t *guid);
387     /* Converts the effect_uuid_t pointed to by first argument to the
388      * string passed as second argument
389      */
390     static status_t guidToString(const effect_uuid_t *guid, char *str, size_t maxLen);
391
392protected:
393     bool                    mEnabled;           // enable state
394     int32_t                 mSessionId;         // audio session ID
395     int32_t                 mPriority;          // priority for effect control
396     status_t                mStatus;            // effect status
397     effect_callback_t       mCbf;               // callback function for status, control and
398                                                 // parameter changes notifications
399     void*                   mUserData;          // client context for callback function
400     effect_descriptor_t     mDescriptor;        // effect descriptor
401     int32_t                 mId;                // system wide unique effect engine instance ID
402     Mutex                   mLock;               // Mutex for mEnabled access
403
404private:
405
406     // Implements the IEffectClient interface
407    class EffectClient : public android::BnEffectClient,  public android::IBinder::DeathRecipient
408    {
409    public:
410
411        EffectClient(AudioEffect *effect) : mEffect(effect){}
412
413        // IEffectClient
414        virtual void controlStatusChanged(bool controlGranted) {
415            mEffect->controlStatusChanged(controlGranted);
416        }
417        virtual void enableStatusChanged(bool enabled) {
418            mEffect->enableStatusChanged(enabled);
419        }
420        virtual void commandExecuted(uint32_t cmdCode,
421                                     uint32_t cmdSize,
422                                     void *pCmdData,
423                                     uint32_t replySize,
424                                     void *pReplyData) {
425            mEffect->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
426        }
427
428        // IBinder::DeathRecipient
429        virtual void binderDied(const wp<IBinder>& who) {mEffect->binderDied();}
430
431    private:
432        AudioEffect *mEffect;
433    };
434
435
436    friend class EffectClient;
437
438    // IEffectClient
439    void controlStatusChanged(bool controlGranted);
440    void enableStatusChanged(bool enabled);
441    void commandExecuted(uint32_t cmdCode,
442                         uint32_t cmdSize,
443                         void *pCmdData,
444                         uint32_t replySize,
445                         void *pReplyData);
446    void binderDied();
447
448
449    sp<IEffect>             mIEffect;           // IEffect binder interface
450    sp<EffectClient>        mIEffectClient;     // IEffectClient implementation
451    sp<IMemory>             mCblkMemory;        // shared memory for deferred parameter setting
452    effect_param_cblk_t*    mCblk;              // control block for deferred parameter setting
453};
454
455
456}; // namespace android
457
458#endif // ANDROID_AUDIOEFFECT_H
459