Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d092df14 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Avoid storing 'this' in a wp<AudioEffect> while in constructor"

parents 97937ef8 69d41cc6
Loading
Loading
Loading
Loading
+28 −53
Original line number Diff line number Diff line
@@ -36,62 +36,10 @@ namespace android {
// ---------------------------------------------------------------------------

AudioEffect::AudioEffect(const String16& opPackageName)
    : mStatus(NO_INIT), mOpPackageName(opPackageName)
    : mOpPackageName(opPackageName)
{
}


AudioEffect::AudioEffect(const effect_uuid_t *type,
                const String16& opPackageName,
                const effect_uuid_t *uuid,
                int32_t priority,
                effect_callback_t cbf,
                void* user,
                audio_session_t sessionId,
                audio_io_handle_t io,
                const AudioDeviceTypeAddr& device
                )
    : mStatus(NO_INIT), mOpPackageName(opPackageName)
{
    AutoMutex lock(mConstructLock);
    mStatus = set(type, uuid, priority, cbf, user, sessionId, io, device);
}

AudioEffect::AudioEffect(const char *typeStr,
                const String16& opPackageName,
                const char *uuidStr,
                int32_t priority,
                effect_callback_t cbf,
                void* user,
                audio_session_t sessionId,
                audio_io_handle_t io,
                const AudioDeviceTypeAddr& device
                )
    : mStatus(NO_INIT), mOpPackageName(opPackageName)
{
    effect_uuid_t type;
    effect_uuid_t *pType = NULL;
    effect_uuid_t uuid;
    effect_uuid_t *pUuid = NULL;

    ALOGV("Constructor string\n - type: %s\n - uuid: %s", typeStr, uuidStr);

    if (typeStr != NULL) {
        if (stringToGuid(typeStr, &type) == NO_ERROR) {
            pType = &type;
        }
    }

    if (uuidStr != NULL) {
        if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
            pUuid = &uuid;
        }
    }

    AutoMutex lock(mConstructLock);
    mStatus = set(pType, pUuid, priority, cbf, user, sessionId, io, device);
}

status_t AudioEffect::set(const effect_uuid_t *type,
                const effect_uuid_t *uuid,
                int32_t priority,
@@ -183,6 +131,33 @@ status_t AudioEffect::set(const effect_uuid_t *type,
    return mStatus;
}

status_t AudioEffect::set(const char *typeStr,
                const char *uuidStr,
                int32_t priority,
                effect_callback_t cbf,
                void* user,
                audio_session_t sessionId,
                audio_io_handle_t io,
                const AudioDeviceTypeAddr& device)
{
    effect_uuid_t type;
    effect_uuid_t *pType = nullptr;
    effect_uuid_t uuid;
    effect_uuid_t *pUuid = nullptr;

    ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s",
            typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr");

    if (stringToGuid(typeStr, &type) == NO_ERROR) {
        pType = &type;
    }
    if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
        pUuid = &uuid;
    }

    return set(pType, pUuid, priority, cbf, user, sessionId, io, device);
}


AudioEffect::~AudioEffect()
{
+38 −77
Original line number Diff line number Diff line
@@ -339,16 +339,21 @@ public:
     *
     * opPackageName:      The package name used for app op checks.
     */
    AudioEffect(const String16& opPackageName);
    explicit AudioEffect(const String16& opPackageName);

    /* Terminates the AudioEffect and unregisters it from AudioFlinger.
     * The effect engine is also destroyed if this AudioEffect was the last controlling
     * the engine.
     */
                        ~AudioEffect();

    /* Constructor.
    /**
     * Initialize an uninitialized AudioEffect.
     *
     * Parameters:
     *
     * type:  type of effect created: can be null if uuid is specified. This corresponds to
     *        the OpenSL ES interface implemented by this effect.
     * opPackageName:  The package name used for app op checks.
     * uuid:  Uuid of effect created: can be null if type is specified. This uuid corresponds to
     *        a particular implementation of an effect type.
     * priority:    requested priority for effect control: the priority level corresponds to the
@@ -356,7 +361,7 @@ public:
     *      higher priorities, 0 being the normal priority.
     * cbf:         optional callback function (see effect_callback_t)
     * user:        pointer to context for use by the callback receiver.
     * sessionID:   audio session this effect is associated to.
     * sessionId:   audio session this effect is associated to.
     *      If equal to AUDIO_SESSION_OUTPUT_MIX, the effect will be global to
     *      the output mix.  Otherwise, the effect will be applied to all players
     *      (AudioTrack or MediaPLayer) within the same audio session.
@@ -365,55 +370,32 @@ public:
     * device: An audio device descriptor. Only used when "sessionID" is AUDIO_SESSION_DEVICE.
     *         Specifies the audio device type and address the effect must be attached to.
     *         If "sessionID" is AUDIO_SESSION_DEVICE then "io" must be AUDIO_IO_HANDLE_NONE.
     *
     * Returned status (from utils/Errors.h) can be:
     *  - NO_ERROR or ALREADY_EXISTS: successful initialization
     *  - INVALID_OPERATION: AudioEffect is already initialized
     *  - BAD_VALUE: invalid parameter
     *  - NO_INIT: audio flinger or audio hardware not initialized
     */

    AudioEffect(const effect_uuid_t *type,
                const String16& opPackageName,
            status_t    set(const effect_uuid_t *type,
                            const effect_uuid_t *uuid = NULL,
                            int32_t priority = 0,
                            effect_callback_t cbf = NULL,
                            void* user = NULL,
                            audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX,
                            audio_io_handle_t io = AUDIO_IO_HANDLE_NONE,
                const AudioDeviceTypeAddr& device = {}
                );

    /* Constructor.
     *      Same as above but with type and uuid specified by character strings
                            const AudioDeviceTypeAddr& device = {});
    /*
     * Same as above but with type and uuid specified by character strings.
     */
    AudioEffect(const char *typeStr,
                    const String16& opPackageName,
            status_t    set(const char *typeStr,
                            const char *uuidStr = NULL,
                            int32_t priority = 0,
                            effect_callback_t cbf = NULL,
                            void* user = NULL,
                            audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX,
                            audio_io_handle_t io = AUDIO_IO_HANDLE_NONE,
                    const AudioDeviceTypeAddr& device = {}
                    );

    /* Terminates the AudioEffect and unregisters it from AudioFlinger.
     * The effect engine is also destroyed if this AudioEffect was the last controlling
     * the engine.
     */
                        ~AudioEffect();

    /* Initialize an uninitialized AudioEffect.
    * Returned status (from utils/Errors.h) can be:
    *  - NO_ERROR or ALREADY_EXISTS: successful initialization
    *  - INVALID_OPERATION: AudioEffect is already initialized
    *  - BAD_VALUE: invalid parameter
    *  - NO_INIT: audio flinger or audio hardware not initialized
    * */
            status_t    set(const effect_uuid_t *type,
                            const effect_uuid_t *uuid = NULL,
                            int32_t priority = 0,
                            effect_callback_t cbf = NULL,
                            void* user = NULL,
                            audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX,
                            audio_io_handle_t io = AUDIO_IO_HANDLE_NONE,
                            const AudioDeviceTypeAddr& device = {}
                            );
                            const AudioDeviceTypeAddr& device = {});

    /* Result of constructing the AudioEffect. This must be checked
     * before using any AudioEffect API.
@@ -543,19 +525,18 @@ public:
     static const uint32_t kMaxPreProcessing = 10;

protected:
     bool                    mEnabled;           // enable state
     audio_session_t         mSessionId;         // audio session ID
     int32_t                 mPriority;          // priority for effect control
     status_t                mStatus;            // effect status
     effect_callback_t       mCbf;               // callback function for status, control and
     const String16          mOpPackageName;     // The package name used for app op checks.
     bool                    mEnabled = false;   // enable state
     audio_session_t         mSessionId = AUDIO_SESSION_OUTPUT_MIX; // audio session ID
     int32_t                 mPriority = 0;      // priority for effect control
     status_t                mStatus = NO_INIT;  // effect status
     effect_callback_t       mCbf = nullptr;     // callback function for status, control and
                                                 // parameter changes notifications
     void*                   mUserData;          // client context for callback function
     effect_descriptor_t     mDescriptor;        // effect descriptor
     int32_t                 mId;                // system wide unique effect engine instance ID
     void*                   mUserData = nullptr;// client context for callback function
     effect_descriptor_t     mDescriptor = {};   // effect descriptor
     int32_t                 mId = -1;           // system wide unique effect engine instance ID
     Mutex                   mLock;              // Mutex for mEnabled access
     Mutex                   mConstructLock;     // Mutex for integrality construction

     String16                mOpPackageName;     // The package name used for app op checks.

     // IEffectClient
     virtual void controlStatusChanged(bool controlGranted);
@@ -580,22 +561,12 @@ private:
        virtual void controlStatusChanged(bool controlGranted) {
            sp<AudioEffect> effect = mEffect.promote();
            if (effect != 0) {
                {
                    // Got the mConstructLock means the construction of AudioEffect
                    // has finished, we should release the mConstructLock immediately.
                    AutoMutex lock(effect->mConstructLock);
                }
                effect->controlStatusChanged(controlGranted);
            }
        }
        virtual void enableStatusChanged(bool enabled) {
            sp<AudioEffect> effect = mEffect.promote();
            if (effect != 0) {
                {
                    // Got the mConstructLock means the construction of AudioEffect
                    // has finished, we should release the mConstructLock immediately.
                    AutoMutex lock(effect->mConstructLock);
                }
                effect->enableStatusChanged(enabled);
            }
        }
@@ -606,11 +577,6 @@ private:
                                     void *pReplyData) {
            sp<AudioEffect> effect = mEffect.promote();
            if (effect != 0) {
                {
                    // Got the mConstructLock means the construction of AudioEffect
                    // has finished, we should release the mConstructLock immediately.
                    AutoMutex lock(effect->mConstructLock);
                }
                effect->commandExecuted(
                    cmdCode, cmdSize, pCmdData, replySize, pReplyData);
            }
@@ -620,11 +586,6 @@ private:
        virtual void binderDied(const wp<IBinder>& /*who*/) {
            sp<AudioEffect> effect = mEffect.promote();
            if (effect != 0) {
                {
                    // Got the mConstructLock means the construction of AudioEffect
                    // has finished, we should release the mConstructLock immediately.
                    AutoMutex lock(effect->mConstructLock);
                }
                effect->binderDied();
            }
        }
@@ -638,8 +599,8 @@ private:
    sp<IEffect>             mIEffect;           // IEffect binder interface
    sp<EffectClient>        mIEffectClient;     // IEffectClient implementation
    sp<IMemory>             mCblkMemory;        // shared memory for deferred parameter setting
    effect_param_cblk_t*    mCblk;              // control block for deferred parameter setting
    pid_t                   mClientPid;
    effect_param_cblk_t*    mCblk = nullptr;    // control block for deferred parameter setting
    pid_t                   mClientPid = (pid_t)-1;
};


+9 −9
Original line number Diff line number Diff line
@@ -121,8 +121,8 @@ status_t AudioPolicyEffects::addInputEffects(audio_io_handle_t input,
        Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
        for (size_t i = 0; i < effects.size(); i++) {
            EffectDesc *effect = effects[i];
            sp<AudioEffect> fx = new AudioEffect(NULL, String16("android"), &effect->mUuid, -1, 0,
                                                 0, audioSession, input);
            sp<AudioEffect> fx = new AudioEffect(String16("android"));
            fx->set(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
            status_t status = fx->initCheck();
            if (status != NO_ERROR && status != ALREADY_EXISTS) {
                ALOGW("addInputEffects(): failed to create Fx %s on source %d",
@@ -270,8 +270,8 @@ status_t AudioPolicyEffects::addOutputSessionEffects(audio_io_handle_t output,
        Vector <EffectDesc *> effects = mOutputStreams.valueAt(index)->mEffects;
        for (size_t i = 0; i < effects.size(); i++) {
            EffectDesc *effect = effects[i];
            sp<AudioEffect> fx = new AudioEffect(NULL, String16("android"), &effect->mUuid, 0, 0, 0,
                                                 audioSession, output);
            sp<AudioEffect> fx = new AudioEffect(String16("android"));
            fx->set(NULL, &effect->mUuid, 0, 0, 0, audioSession, output);
            status_t status = fx->initCheck();
            if (status != NO_ERROR && status != ALREADY_EXISTS) {
                ALOGE("addOutputSessionEffects(): failed to create Fx  %s on session %d",
@@ -967,8 +967,8 @@ void AudioPolicyEffects::initDefaultDeviceEffects()
    for (const auto& deviceEffectsIter : mDeviceEffects) {
        const auto& deviceEffects =  deviceEffectsIter.second;
        for (const auto& effectDesc : deviceEffects->mEffectDescriptors->mEffects) {
            auto fx = std::make_unique<AudioEffect>(
                        EFFECT_UUID_NULL, String16("android"), &effectDesc->mUuid, 0, nullptr,
            auto fx = std::make_unique<AudioEffect>(String16("android"));
            fx->set(EFFECT_UUID_NULL, &effectDesc->mUuid, 0, nullptr,
                    nullptr, AUDIO_SESSION_DEVICE, AUDIO_IO_HANDLE_NONE,
                    AudioDeviceTypeAddr{deviceEffects->getDeviceType(),
                                        deviceEffects->getDeviceAddress()});