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