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/AudioSystem.h>
25 #include <system/audio_effect.h>
26 #include <android/content/AttributionSourceState.h>
27 
28 #include <utils/RefBase.h>
29 #include <utils/Errors.h>
30 #include <binder/IInterface.h>
31 
32 #include "android/media/IEffect.h"
33 #include "android/media/BnEffectClient.h"
34 
35 namespace android {
36 
37 // ----------------------------------------------------------------------------
38 
39 struct effect_param_cblk_t;
40 
41 // ----------------------------------------------------------------------------
42 
43 class AudioEffect : public virtual RefBase
44 {
45 public:
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      * Returns a descriptor for the specified effect uuid or type.
95      *
96      * Lookup an effect by uuid, or if that's unspecified (EFFECT_UUID_NULL),
97      * do so by type and preferred flags instead.
98      *
99      * Parameters:
100      *      uuid:       pointer to effect uuid.
101      *      type:       pointer to effect type uuid.
102      *      preferredTypeFlags: if multiple effects of the given type exist,
103      *                  one with a matching type flag will be chosen over one without.
104      *                  Use EFFECT_FLAG_TYPE_MASK to indicate no preference.
105      *      descriptor: address where the effect descriptor should be returned.
106      *
107      * Returned status (from utils/Errors.h) can be:
108      *      NO_ERROR        successful operation.
109      *      PERMISSION_DENIED could not get AudioFlinger interface
110      *      NO_INIT         effect library failed to initialize
111      *      BAD_VALUE       invalid type or descriptor pointers
112      *      NAME_NOT_FOUND  no effect with this uuid found
113      *
114      * Returned value
115      *   *descriptor updated with effect descriptor
116      */
117     static status_t getEffectDescriptor(const effect_uuid_t *uuid,
118                                         const effect_uuid_t *type,
119                                         uint32_t preferredTypeFlag,
120                                         effect_descriptor_t *descriptor);
121 
122     /*
123      * Returns a list of descriptors corresponding to the pre processings enabled by default
124      * on an AudioRecord with the supplied audio session ID.
125      *
126      * Parameters:
127      *      audioSession:  audio session ID.
128      *      descriptors: address where the effect descriptors should be returned.
129      *      count: as input, the maximum number of descriptor than should be returned
130      *             as output, the number of descriptor returned if status is NO_ERROR or the actual
131      *             number of enabled pre processings if status is NO_MEMORY
132      *
133      * Returned status (from utils/Errors.h) can be:
134      *      NO_ERROR        successful operation.
135      *      NO_MEMORY       the number of descriptor to return is more than the maximum number
136      *                      indicated by count.
137      *      PERMISSION_DENIED could not get AudioFlinger interface
138      *      NO_INIT         effect library failed to initialize
139      *      BAD_VALUE       invalid audio session, or invalid descriptor or count pointers
140      *
141      * Returned value
142      *   *descriptor updated with descriptors of pre processings enabled by default
143      *   *count      number of descriptors returned if returned status is NO_ERROR.
144      *               total number of pre processing enabled by default if returned status is
145      *               NO_MEMORY. This happens if the count passed as input is less than the number
146      *               of descriptors to return.
147      *               *count is limited to kMaxPreProcessing on return.
148      */
149     static status_t queryDefaultPreProcessing(audio_session_t audioSession,
150                                               effect_descriptor_t *descriptors,
151                                               uint32_t *count);
152 
153     /*
154      * Gets a new system-wide unique effect id.
155      *
156      * Parameters:
157      *      id: The address to return the generated id.
158      *
159      * Returned status (from utils/Errors.h) can be:
160      *      NO_ERROR        successful operation.
161      *      PERMISSION_DENIED could not get AudioFlinger interface
162      *                        or caller lacks required permissions.
163      *      BAD_VALUE       invalid pointer to id
164      * Returned value
165      *   *id:  The new unique system-wide effect id.
166      */
167     static status_t newEffectUniqueId(audio_unique_id_t* id);
168 
169     /*
170      * Static methods for adding/removing system-wide effects.
171      */
172 
173     /*
174      * Adds an effect to the list of default output effects for a given source type.
175      *
176      * If the effect is no longer available when a source of the given type
177      * is created, the system will continue without adding it.
178      *
179      * Parameters:
180      *   typeStr:  Type uuid of effect to be a default: can be null if uuidStr is specified.
181      *             This may correspond to the OpenSL ES interface implemented by this effect,
182      *             or could be some vendor-defined type.
183      *   opPackageName: The package name used for app op checks.
184      *   uuidStr:  Uuid of effect to be a default: can be null if type is specified.
185      *             This uuid corresponds to a particular implementation of an effect type.
186      *             Note if both uuidStr and typeStr are specified, typeStr is ignored.
187      *   priority: Requested priority for effect control: the priority level corresponds to the
188      *             value of priority parameter: negative values indicate lower priorities, positive
189      *             values higher priorities, 0 being the normal priority.
190      *   source:   The source this effect should be a default for.
191      *   id:       Address where the system-wide unique id of the default effect should be returned.
192      *
193      * Returned status (from utils/Errors.h) can be:
194      *      NO_ERROR        successful operation.
195      *      PERMISSION_DENIED could not get AudioFlinger interface
196      *                        or caller lacks required permissions.
197      *      NO_INIT         effect library failed to initialize.
198      *      BAD_VALUE       invalid source, type uuid or implementation uuid, or id pointer
199      *      NAME_NOT_FOUND  no effect with this uuid or type found.
200      *
201      * Returned value
202      *   *id:  The system-wide unique id of the added default effect.
203      */
204     static status_t addSourceDefaultEffect(const char* typeStr,
205                                            const String16& opPackageName,
206                                            const char* uuidStr,
207                                            int32_t priority,
208                                            audio_source_t source,
209                                            audio_unique_id_t* id);
210 
211     /*
212      * Adds an effect to the list of default output effects for a given stream type.
213      *
214      * If the effect is no longer available when a stream of the given type
215      * is created, the system will continue without adding it.
216      *
217      * Parameters:
218      *   typeStr:  Type uuid of effect to be a default: can be null if uuidStr is specified.
219      *             This may correspond to the OpenSL ES interface implemented by this effect,
220      *             or could be some vendor-defined type.
221      *   opPackageName: The package name used for app op checks.
222      *   uuidStr:  Uuid of effect to be a default: can be null if type is specified.
223      *             This uuid corresponds to a particular implementation of an effect type.
224      *             Note if both uuidStr and typeStr are specified, typeStr is ignored.
225      *   priority: Requested priority for effect control: the priority level corresponds to the
226      *             value of priority parameter: negative values indicate lower priorities, positive
227      *             values higher priorities, 0 being the normal priority.
228      *   usage:    The usage this effect should be a default for. Unrecognized values will be
229      *             treated as AUDIO_USAGE_UNKNOWN.
230      *   id:       Address where the system-wide unique id of the default effect should be returned.
231      *
232      * Returned status (from utils/Errors.h) can be:
233      *      NO_ERROR        successful operation.
234      *      PERMISSION_DENIED could not get AudioFlinger interface
235      *                        or caller lacks required permissions.
236      *      NO_INIT         effect library failed to initialize.
237      *      BAD_VALUE       invalid type uuid or implementation uuid, or id pointer
238      *      NAME_NOT_FOUND  no effect with this uuid or type found.
239      *
240      * Returned value
241      *   *id:  The system-wide unique id of the added default effect.
242      */
243     static status_t addStreamDefaultEffect(const char* typeStr,
244                                            const String16& opPackageName,
245                                            const char* uuidStr,
246                                            int32_t priority,
247                                            audio_usage_t usage,
248                                            audio_unique_id_t* id);
249 
250     /*
251      * Removes an effect from the list of default output effects for a given source type.
252      *
253      * Parameters:
254      *      id: The system-wide unique id of the effect that should no longer be a default.
255      *
256      * Returned status (from utils/Errors.h) can be:
257      *      NO_ERROR        successful operation.
258      *      PERMISSION_DENIED could not get AudioFlinger interface
259      *                        or caller lacks required permissions.
260      *      NO_INIT         effect library failed to initialize.
261      *      BAD_VALUE       invalid id.
262      */
263     static status_t removeSourceDefaultEffect(audio_unique_id_t id);
264 
265     /*
266      * Removes an effect from the list of default output effects for a given stream type.
267      *
268      * Parameters:
269      *      id: The system-wide unique id of the effect that should no longer be a default.
270      *
271      * Returned status (from utils/Errors.h) can be:
272      *      NO_ERROR        successful operation.
273      *      PERMISSION_DENIED could not get AudioFlinger interface
274      *                        or caller lacks required permissions.
275      *      NO_INIT         effect library failed to initialize.
276      *      BAD_VALUE       invalid id.
277      */
278     static status_t removeStreamDefaultEffect(audio_unique_id_t id);
279 
280     /*
281      * Events used by callback function (legacy_callback_t).
282      */
283     enum event_type {
284         EVENT_CONTROL_STATUS_CHANGED = 0,
285         EVENT_ENABLE_STATUS_CHANGED = 1,
286         EVENT_PARAMETER_CHANGED = 2,
287         EVENT_ERROR = 3,
288         EVENT_FRAMES_PROCESSED = 4,
289     };
290 
291     class IAudioEffectCallback : public virtual RefBase {
292         friend AudioEffect;
293      protected:
294         /* The event is received when an application loses or
295          * gains the control of the effect engine. Loss of control happens
296          * if another application requests the use of the engine by creating an AudioEffect for
297          * the same effect type but with a higher priority. Control is returned when the
298          * application having the control deletes its AudioEffect object.
299          * @param isGranted: True if control has been granted. False if stolen.
300          */
onControlStatusChanged(bool isGranted)301         virtual void onControlStatusChanged([[maybe_unused]] bool isGranted) {}
302 
303         /* The event is received by all applications not having the
304          * control of the effect engine when the effect is enabled or disabled.
305          * @param isEnabled: True if enabled. False if disabled.
306          */
onEnableStatusChanged(bool isEnabled)307         virtual void onEnableStatusChanged([[maybe_unused]] bool isEnabled) {}
308 
309         /* The event is received by all applications not having the
310          * control of the effect engine when an effect parameter is changed.
311          * @param param: A vector containing the raw bytes of a effect_param_type containing
312          *   raw data representing a param type, value pair.
313          */
314         // TODO pass an AIDL parcel instead of effect_param_type
onParameterChanged(std::vector<uint8_t> param)315         virtual void onParameterChanged([[maybe_unused]] std::vector<uint8_t> param) {}
316 
317         /* The event is received when the binder connection to the mediaserver
318          * is no longer valid. Typically the server has been killed.
319          * @param errorCode: A code representing the type of error.
320          */
onError(status_t errorCode)321         virtual void onError([[maybe_unused]] status_t errorCode) {}
322 
323 
324         /* The event is received when the audio server has processed a block of
325          * data.
326          * @param framesProcessed: The number of frames the audio server has
327          *   processed.
328          */
onFramesProcessed(int32_t framesProcessed)329         virtual void onFramesProcessed([[maybe_unused]] int32_t framesProcessed) {}
330     };
331 
332     /* Callback function notifying client application of a change in effect engine state or
333      * configuration.
334      * An effect engine can be shared by several applications but only one has the control
335      * of the engine activity and configuration at a time.
336      * The EVENT_CONTROL_STATUS_CHANGED event is received when an application loses or
337      * retrieves the control of the effect engine. Loss of control happens
338      * if another application requests the use of the engine by creating an AudioEffect for
339      * the same effect type but with a higher priority. Control is returned when the
340      * application having the control deletes its AudioEffect object.
341      * The EVENT_ENABLE_STATUS_CHANGED event is received by all applications not having the
342      * control of the effect engine when the effect is enabled or disabled.
343      * The EVENT_PARAMETER_CHANGED event is received by all applications not having the
344      * control of the effect engine when an effect parameter is changed.
345      * The EVENT_ERROR event is received when the media server process dies.
346      *
347      * Parameters:
348      *
349      * event:   type of event notified (see enum AudioEffect::event_type).
350      * user:    Pointer to context for use by the callback receiver.
351      * info:    Pointer to optional parameter according to event type:
352      *  - EVENT_CONTROL_STATUS_CHANGED:  boolean indicating if control is granted (true)
353      *  or stolen (false).
354      *  - EVENT_ENABLE_STATUS_CHANGED: boolean indicating if effect is now enabled (true)
355      *  or disabled (false).
356      *  - EVENT_PARAMETER_CHANGED: pointer to a effect_param_t structure.
357      *  - EVENT_ERROR:  status_t indicating the error (DEAD_OBJECT when media server dies).
358      */
359 
360     typedef void (*legacy_callback_t)(int32_t event, void* user, void *info);
361 
362 
363     /* Constructor.
364      * AudioEffect is the base class for creating and controlling an effect engine from
365      * the application process. Creating an AudioEffect object will create the effect engine
366      * in the AudioFlinger if no engine of the specified type exists. If one exists, this engine
367      * will be used. The application creating the AudioEffect object (or a derived class like
368      * Reverb for instance) will either receive control of the effect engine or not, depending
369      * on the priority parameter. If priority is higher than the priority used by the current
370      * effect engine owner, the control will be transfered to the new application. Otherwise
371      * control will remain to the previous application. In this case, the new application will be
372      * notified of changes in effect engine state or control ownership by the effect callback.
373      * After creating the AudioEffect, the application must call the initCheck() method and
374      * check the creation status before trying to control the effect engine (see initCheck()).
375      * If the effect is to be applied to an AudioTrack or MediaPlayer only the application
376      * must specify the audio session ID corresponding to this player.
377      */
378 
379     /* Simple Constructor.
380      *
381      * Parameters:
382      *
383      * client:      Attribution source for app-op checks
384      */
385     explicit AudioEffect(const android::content::AttributionSourceState& client);
386 
387     /* Terminates the AudioEffect and unregisters it from AudioFlinger.
388      * The effect engine is also destroyed if this AudioEffect was the last controlling
389      * the engine.
390      */
391                         ~AudioEffect();
392 
393     /**
394      * Initialize an uninitialized AudioEffect.
395      *
396      * Parameters:
397      *
398      * type:  type of effect created: can be null if uuid is specified. This corresponds to
399      *        the OpenSL ES interface implemented by this effect.
400      * uuid:  Uuid of effect created: can be null if type is specified. This uuid corresponds to
401      *        a particular implementation of an effect type.
402      * priority:    requested priority for effect control: the priority level corresponds to the
403      *      value of priority parameter: negative values indicate lower priorities, positive values
404      *      higher priorities, 0 being the normal priority.
405      * cbf:         optional callback function (see legacy_callback_t)
406      * user:        pointer to context for use by the callback receiver.
407      * sessionId:   audio session this effect is associated to.
408      *      If equal to AUDIO_SESSION_OUTPUT_MIX, the effect will be global to
409      *      the output mix.  Otherwise, the effect will be applied to all players
410      *      (AudioTrack or MediaPLayer) within the same audio session.
411      * io:  HAL audio output or input stream to which this effect must be attached. Leave at 0 for
412      *      automatic output selection by AudioFlinger.
413      * device: An audio device descriptor. Only used when "sessionID" is AUDIO_SESSION_DEVICE.
414      *         Specifies the audio device type and address the effect must be attached to.
415      *         If "sessionID" is AUDIO_SESSION_DEVICE then "io" must be AUDIO_IO_HANDLE_NONE.
416      * probe: true if created in a degraded mode to only verify if effect creation is possible.
417      *        In this mode, no IEffect interface to AudioFlinger is created and all actions
418      *        besides getters implemented in client AudioEffect object are no ops
419      *        after effect creation.
420      *
421      * Returned status (from utils/Errors.h) can be:
422      *  - NO_ERROR or ALREADY_EXISTS: successful initialization
423      *  - INVALID_OPERATION: AudioEffect is already initialized
424      *  - BAD_VALUE: invalid parameter
425      *  - NO_INIT: audio flinger or audio hardware not initialized
426      */
427             status_t    set(const effect_uuid_t *type,
428                             const effect_uuid_t *uuid = nullptr,
429                             int32_t priority = 0,
430                             const wp<IAudioEffectCallback>& callback = nullptr,
431                             audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX,
432                             audio_io_handle_t io = AUDIO_IO_HANDLE_NONE,
433                             const AudioDeviceTypeAddr& device = {},
434                             bool probe = false,
435                             bool notifyFramesProcessed = false);
436 
437             status_t    set(const effect_uuid_t *type,
438                             const effect_uuid_t *uuid,
439                             int32_t priority,
440                             legacy_callback_t cbf,
441                             void* user,
442                             audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX,
443                             audio_io_handle_t io = AUDIO_IO_HANDLE_NONE,
444                             const AudioDeviceTypeAddr& device = {},
445                             bool probe = false,
446                             bool notifyFramesProcessed = false);
447     /*
448      * Same as above but with type and uuid specified by character strings.
449      */
450             status_t    set(const char *typeStr,
451                             const char *uuidStr = nullptr,
452                             int32_t priority = 0,
453                             const wp<IAudioEffectCallback>& callback = nullptr,
454                             audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX,
455                             audio_io_handle_t io = AUDIO_IO_HANDLE_NONE,
456                             const AudioDeviceTypeAddr& device = {},
457                             bool probe = false,
458                             bool notifyFramesProcessed = false);
459 
460 
461             status_t    set(const char *typeStr,
462                             const char *uuidStr,
463                             int32_t priority,
464                             legacy_callback_t cbf,
465                             void* user,
466                             audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX,
467                             audio_io_handle_t io = AUDIO_IO_HANDLE_NONE,
468                             const AudioDeviceTypeAddr& device = {},
469                             bool probe = false,
470                             bool notifyFramesProcessed = false);
471 
472     /* Result of constructing the AudioEffect. This must be checked
473      * before using any AudioEffect API.
474      * initCheck() can return:
475      *  - NO_ERROR:    the effect engine is successfully created and the application has control.
476      *  - ALREADY_EXISTS: the effect engine is successfully created but the application does not
477      *              have control.
478      *  - NO_INIT:     the effect creation failed.
479      *
480      */
481             status_t    initCheck() const;
482 
483 
484     /* Returns the unique effect Id for the controlled effect engine. This ID is unique
485      * system wide and is used for instance in the case of auxiliary effects to attach
486      * the effect to an AudioTrack or MediaPlayer.
487      *
488      */
id()489             int32_t     id() const { return mId; }
490 
491     /* Returns a descriptor for the effect (see effect_descriptor_t in audio_effect.h).
492      */
493             effect_descriptor_t descriptor() const;
494 
495     /* Returns effect control priority of this AudioEffect object.
496      */
priority()497             int32_t     priority() const { return mPriority; }
498 
499 
500     /* Enables or disables the effect engine.
501      *
502      * Parameters:
503      *  enabled: requested enable state.
504      *
505      * Returned status (from utils/Errors.h) can be:
506      *  - NO_ERROR: successful operation
507      *  - INVALID_OPERATION: the application does not have control of the effect engine or the
508      *  effect is already in the requested state.
509      */
510     virtual status_t    setEnabled(bool enabled);
511             bool        getEnabled() const;
512 
513     /* Sets a parameter value.
514      *
515      * Parameters:
516      *      param:  pointer to effect_param_t structure containing the parameter
517      *          and its value (See audio_effect.h).
518      * Returned status (from utils/Errors.h) can be:
519      *  - NO_ERROR: successful operation.
520      *  - INVALID_OPERATION: the application does not have control of the effect engine.
521      *  - BAD_VALUE: invalid parameter structure pointer, or invalid identifier or value.
522      *  - DEAD_OBJECT: the effect engine has been deleted.
523      */
524      virtual status_t   setParameter(effect_param_t *param);
525 
526     /* Prepare a new parameter value that will be set by next call to
527      * setParameterCommit(). This method can be used to set multiple parameters
528      * in a synchronous manner or to avoid multiple binder calls for each
529      * parameter.
530      *
531      * Parameters:
532      *      param:  pointer to effect_param_t structure containing the parameter
533      *          and its value (See audio_effect.h).
534      *
535      * Returned status (from utils/Errors.h) can be:
536      *  - NO_ERROR: successful operation.
537      *  - INVALID_OPERATION: the application does not have control of the effect engine.
538      *  - NO_MEMORY: no more space available in shared memory used for deferred parameter
539      *  setting.
540      */
541      virtual status_t   setParameterDeferred(effect_param_t *param);
542 
543      /* Commit all parameter values previously prepared by setParameterDeferred().
544       *
545       * Parameters:
546       *     none
547       *
548       * Returned status (from utils/Errors.h) can be:
549       *  - NO_ERROR: successful operation.
550       *  - INVALID_OPERATION: No new parameter values ready for commit.
551       *  - BAD_VALUE: invalid parameter identifier or value: there is no indication
552       *     as to which of the parameters caused this error.
553       *  - DEAD_OBJECT: the effect engine has been deleted.
554       */
555      virtual status_t   setParameterCommit();
556 
557     /* Gets a parameter value.
558      *
559      * Parameters:
560      *      param:  pointer to effect_param_t structure containing the parameter
561      *          and the returned value (See audio_effect.h).
562      *
563      * Returned status (from utils/Errors.h) can be:
564      *  - NO_ERROR: successful operation.
565      *  - INVALID_OPERATION: the AudioEffect was not successfully initialized.
566      *  - BAD_VALUE: invalid parameter structure pointer, or invalid parameter identifier.
567      *  - DEAD_OBJECT: the effect engine has been deleted.
568      */
569      virtual status_t   getParameter(effect_param_t *param);
570 
571      /* Sends a command and receives a response to/from effect engine.
572       *     See audio_effect.h for details on effect command() function, valid command codes
573       *     and formats.
574       */
575      virtual status_t command(uint32_t cmdCode,
576                               uint32_t cmdSize,
577                               void *cmdData,
578                               uint32_t *replySize,
579                               void *replyData);
580 
581     /* Retrieves the configuration of the effect.
582      *
583      * Parameters:
584      *      inputCfg:  pointer to audio_config_base_t structure receiving input
585      *          configuration of the effect
586      *      outputCfg: pointer to audio_config_base_t structure receiving output
587      *          configuration of the effect
588      *
589      * Channel masks of the returned configs are "input" or "output" depending
590      * on the direction of the stream that the effect is attached to.
591      *
592      * Returned status (from utils/Errors.h) can be:
593      *  - NO_ERROR: successful operation.
594      *  - INVALID_OPERATION: the AudioEffect was not successfully initialized.
595      *  - BAD_VALUE: null config pointers
596      *  - DEAD_OBJECT: the effect engine has been deleted.
597      */
598      virtual status_t   getConfigs(audio_config_base_t *inputCfg,
599                                    audio_config_base_t *outputCfg);
600 
601      /*
602       * Utility functions.
603       */
604 
605      /* Converts the string passed as first argument to the effect_uuid_t
606       * pointed to by second argument
607       */
608      static status_t stringToGuid(const char *str, effect_uuid_t *guid);
609      /* Converts the effect_uuid_t pointed to by first argument to the
610       * string passed as second argument
611       */
612      static status_t guidToString(const effect_uuid_t *guid, char *str, size_t maxLen);
613 
614      // kMaxPreProcessing is a reasonable value for the maximum number of preprocessing effects
615      // that can be applied simultaneously.
616      static const uint32_t kMaxPreProcessing = 10;
617 
618 protected:
619      android::content::AttributionSourceState mClientAttributionSource; // source for app op checks.
620      bool                     mEnabled = false;   // enable state
621      audio_session_t          mSessionId = AUDIO_SESSION_OUTPUT_MIX; // audio session ID
622      int32_t                  mPriority = 0;      // priority for effect control
623      status_t                 mStatus = NO_INIT;  // effect status
624      bool                     mProbe = false;     // effect created in probe mode: all commands
625                                                  // are no ops because mIEffect is nullptr
626 
627      wp<IAudioEffectCallback> mCallback = nullptr; // callback interface for status, control and
628                                                    // parameter changes notifications
629      sp<IAudioEffectCallback> mLegacyWrapper = nullptr;
630      effect_descriptor_t      mDescriptor = {};   // effect descriptor
631      int32_t                  mId = -1;           // system wide unique effect engine instance ID
632      Mutex                    mLock;              // Mutex for mEnabled access
633 
634 
635      // IEffectClient
636      virtual void controlStatusChanged(bool controlGranted);
637      virtual void enableStatusChanged(bool enabled);
638      virtual void commandExecuted(int32_t cmdCode,
639                                   const std::vector<uint8_t>& cmdData,
640                                   const std::vector<uint8_t>& replyData);
641      virtual void framesProcessed(int32_t frames);
642 
643 private:
644 
645      // Implements the IEffectClient interface
646     class EffectClient :
647         public media::BnEffectClient, public android::IBinder::DeathRecipient
648     {
649     public:
650 
EffectClient(const sp<AudioEffect> & effect)651         explicit EffectClient(const sp<AudioEffect>& effect) : mEffect(effect){}
652 
653         // IEffectClient
controlStatusChanged(bool controlGranted)654         binder::Status controlStatusChanged(bool controlGranted) override {
655             sp<AudioEffect> effect = mEffect.promote();
656             if (effect != 0) {
657                 effect->controlStatusChanged(controlGranted);
658             }
659             return binder::Status::ok();
660         }
enableStatusChanged(bool enabled)661         binder::Status enableStatusChanged(bool enabled) override {
662             sp<AudioEffect> effect = mEffect.promote();
663             if (effect != 0) {
664                 effect->enableStatusChanged(enabled);
665             }
666             return binder::Status::ok();
667         }
commandExecuted(int32_t cmdCode,const std::vector<uint8_t> & cmdData,const std::vector<uint8_t> & replyData)668         binder::Status commandExecuted(int32_t cmdCode,
669                              const std::vector<uint8_t>& cmdData,
670                              const std::vector<uint8_t>& replyData) override {
671             sp<AudioEffect> effect = mEffect.promote();
672             if (effect != 0) {
673                 effect->commandExecuted(cmdCode, cmdData, replyData);
674             }
675             return binder::Status::ok();
676         }
framesProcessed(int32_t frames)677         binder::Status framesProcessed(int32_t frames) override {
678             sp<AudioEffect> effect = mEffect.promote();
679             if (effect != 0) {
680                 effect->framesProcessed(frames);
681             }
682             return binder::Status::ok();
683         }
684 
685 
686         // IBinder::DeathRecipient
binderDied(const wp<IBinder> &)687         virtual void binderDied(const wp<IBinder>& /*who*/) {
688             sp<AudioEffect> effect = mEffect.promote();
689             if (effect != 0) {
690                 effect->binderDied();
691             }
692         }
693 
694     private:
695         wp<AudioEffect> mEffect;
696     };
697 
698     void binderDied();
699 
700     sp<media::IEffect>      mIEffect;           // IEffect binder interface
701     sp<EffectClient>        mIEffectClient;     // IEffectClient implementation
702     sp<IMemory>             mCblkMemory;        // shared memory for deferred parameter setting
703     effect_param_cblk_t*    mCblk = nullptr;    // control block for deferred parameter setting
704 };
705 
706 
707 }; // namespace android
708 
709 #endif // ANDROID_AUDIOEFFECT_H
710