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

Commit a46a60cf authored by Shunkai Yao's avatar Shunkai Yao Committed by Automerger Merge Worker
Browse files

Merge changes from topic "aidl_effect_lock" am: 6900a3c9 am: d1bd493c

parents 043542d1 d1bd493c
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ package android.hardware.audio.effect;
union BassBoost {
  android.hardware.audio.effect.VendorExtension vendor;
  int strengthPm;
  const int MIN_PER_MILLE_STRENGTH = 0;
  const int MAX_PER_MILLE_STRENGTH = 1000;
  @VintfStability
  union Id {
    int vendorExtensionTag;
+10 −0
Original line number Diff line number Diff line
@@ -58,6 +58,16 @@ union BassBoost {
        boolean strengthSupported;
    }

    /**
     * Minimal possible per mille strength.
     */
    const int MIN_PER_MILLE_STRENGTH = 0;

    /**
     * Maximum possible per mille strength.
     */
    const int MAX_PER_MILLE_STRENGTH = 1000;

    /**
     * The per mille strength of the bass boost effect.
     *
+45 −58
Original line number Diff line number Diff line
@@ -25,41 +25,33 @@ ndk::ScopedAStatus EffectImpl::open(const Parameter::Common& common,
                                    const std::optional<Parameter::Specific>& specific,
                                    OpenEffectReturn* ret) {
    LOG(DEBUG) << __func__;
    {
        std::lock_guard lg(mMutex);
    RETURN_OK_IF(mState != State::INIT);
        mContext = createContext(common);
        RETURN_IF(!mContext, EX_ILLEGAL_ARGUMENT, "createContextFailed");
        setContext(mContext);
    }
    auto context = createContext(common);
    RETURN_IF(!context, EX_NULL_POINTER, "createContextFailed");

    RETURN_IF_ASTATUS_NOT_OK(setParameterCommon(common), "setCommParamErr");
    if (specific.has_value()) {
        RETURN_IF_ASTATUS_NOT_OK(setParameterSpecific(specific.value()), "setSpecParamErr");
    }

    RETURN_IF(createThread(LOG_TAG) != RetCode::SUCCESS, EX_UNSUPPORTED_OPERATION,
              "FailedToCreateWorker");

    {
        std::lock_guard lg(mMutex);
        mContext->dupeFmq(ret);
    mState = State::IDLE;
    }
    context->dupeFmq(ret);
    RETURN_IF(createThread(context, getEffectName()) != RetCode::SUCCESS, EX_UNSUPPORTED_OPERATION,
              "FailedToCreateWorker");
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus EffectImpl::close() {
    std::lock_guard lg(mMutex);
    RETURN_OK_IF(mState == State::INIT);
    RETURN_IF(mState == State::PROCESSING, EX_ILLEGAL_STATE, "closeAtProcessing");

    // stop the worker thread, ignore the return code
    RETURN_IF(destroyThread() != RetCode::SUCCESS, EX_UNSUPPORTED_OPERATION,
              "FailedToDestroyWorker");
    mState = State::INIT;
    RETURN_IF(releaseContext() != RetCode::SUCCESS, EX_UNSUPPORTED_OPERATION,
              "FailedToCreateWorker");
    mState = State::INIT;

    LOG(DEBUG) << __func__;
    return ndk::ScopedAStatus::ok();
}
@@ -113,29 +105,30 @@ ndk::ScopedAStatus EffectImpl::getParameter(const Parameter::Id& id, Parameter*
}

ndk::ScopedAStatus EffectImpl::setParameterCommon(const Parameter& param) {
    std::lock_guard lg(mMutex);
    RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
    auto context = getContext();
    RETURN_IF(!context, EX_NULL_POINTER, "nullContext");

    auto tag = param.getTag();
    switch (tag) {
        case Parameter::common:
            RETURN_IF(mContext->setCommon(param.get<Parameter::common>()) != RetCode::SUCCESS,
            RETURN_IF(context->setCommon(param.get<Parameter::common>()) != RetCode::SUCCESS,
                      EX_ILLEGAL_ARGUMENT, "setCommFailed");
            break;
        case Parameter::deviceDescription:
            RETURN_IF(mContext->setOutputDevice(param.get<Parameter::deviceDescription>()) !=
            RETURN_IF(context->setOutputDevice(param.get<Parameter::deviceDescription>()) !=
                              RetCode::SUCCESS,
                      EX_ILLEGAL_ARGUMENT, "setDeviceFailed");
            break;
        case Parameter::mode:
            RETURN_IF(mContext->setAudioMode(param.get<Parameter::mode>()) != RetCode::SUCCESS,
            RETURN_IF(context->setAudioMode(param.get<Parameter::mode>()) != RetCode::SUCCESS,
                      EX_ILLEGAL_ARGUMENT, "setModeFailed");
            break;
        case Parameter::source:
            RETURN_IF(mContext->setAudioSource(param.get<Parameter::source>()) != RetCode::SUCCESS,
            RETURN_IF(context->setAudioSource(param.get<Parameter::source>()) != RetCode::SUCCESS,
                      EX_ILLEGAL_ARGUMENT, "setSourceFailed");
            break;
        case Parameter::volumeStereo:
            RETURN_IF(mContext->setVolumeStereo(param.get<Parameter::volumeStereo>()) !=
            RETURN_IF(context->setVolumeStereo(param.get<Parameter::volumeStereo>()) !=
                              RetCode::SUCCESS,
                      EX_ILLEGAL_ARGUMENT, "setVolumeStereoFailed");
            break;
@@ -149,27 +142,28 @@ ndk::ScopedAStatus EffectImpl::setParameterCommon(const Parameter& param) {
}

ndk::ScopedAStatus EffectImpl::getParameterCommon(const Parameter::Tag& tag, Parameter* param) {
    std::lock_guard lg(mMutex);
    RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
    auto context = getContext();
    RETURN_IF(!context, EX_NULL_POINTER, "nullContext");

    switch (tag) {
        case Parameter::common: {
            param->set<Parameter::common>(mContext->getCommon());
            param->set<Parameter::common>(context->getCommon());
            break;
        }
        case Parameter::deviceDescription: {
            param->set<Parameter::deviceDescription>(mContext->getOutputDevice());
            param->set<Parameter::deviceDescription>(context->getOutputDevice());
            break;
        }
        case Parameter::mode: {
            param->set<Parameter::mode>(mContext->getAudioMode());
            param->set<Parameter::mode>(context->getAudioMode());
            break;
        }
        case Parameter::source: {
            param->set<Parameter::source>(mContext->getAudioSource());
            param->set<Parameter::source>(context->getAudioSource());
            break;
        }
        case Parameter::volumeStereo: {
            param->set<Parameter::volumeStereo>(mContext->getVolumeStereo());
            param->set<Parameter::volumeStereo>(context->getVolumeStereo());
            break;
        }
        default: {
@@ -182,39 +176,30 @@ ndk::ScopedAStatus EffectImpl::getParameterCommon(const Parameter::Tag& tag, Par
}

ndk::ScopedAStatus EffectImpl::getState(State* state) {
    std::lock_guard lg(mMutex);
    *state = mState;
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus EffectImpl::command(CommandId command) {
    std::lock_guard lg(mMutex);
    RETURN_IF(mState == State::INIT, EX_ILLEGAL_STATE, "CommandStateError");
    LOG(DEBUG) << __func__ << ": receive command: " << toString(command) << " at state "
               << toString(mState);
    RETURN_IF(mState == State::INIT, EX_ILLEGAL_STATE, "CommandStateError");
    RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");

    switch (command) {
        case CommandId::START:
            RETURN_IF(mState == State::INIT, EX_ILLEGAL_STATE, "instanceNotOpen");
            RETURN_OK_IF(mState == State::PROCESSING);
            RETURN_IF_ASTATUS_NOT_OK(commandStart(), "commandStartFailed");
            mState = State::PROCESSING;
            RETURN_IF_ASTATUS_NOT_OK(commandImpl(command), "commandImplFailed");
            startThread();
            return ndk::ScopedAStatus::ok();
            mState = State::PROCESSING;
            break;
        case CommandId::STOP:
            RETURN_OK_IF(mState == State::IDLE);
            mState = State::IDLE;
            RETURN_IF_ASTATUS_NOT_OK(commandStop(), "commandStopFailed");
            stopThread();
            return ndk::ScopedAStatus::ok();
        case CommandId::RESET:
            RETURN_OK_IF(mState == State::IDLE);
            mState = State::IDLE;
            RETURN_IF_ASTATUS_NOT_OK(commandStop(), "commandStopFailed");
            stopThread();
            mContext->resetBuffer();
            return ndk::ScopedAStatus::ok();
            RETURN_IF_ASTATUS_NOT_OK(commandImpl(command), "commandImplFailed");
            mState = State::IDLE;
            break;
        default:
            LOG(ERROR) << __func__ << " instance still processing";
            return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
@@ -224,6 +209,15 @@ ndk::ScopedAStatus EffectImpl::command(CommandId command) {
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus EffectImpl::commandImpl(CommandId command) {
    auto context = getContext();
    RETURN_IF(!context, EX_NULL_POINTER, "nullContext");
    if (command == CommandId::RESET) {
        context->resetBuffer();
    }
    return ndk::ScopedAStatus::ok();
}

void EffectImpl::cleanUp() {
    command(CommandId::STOP);
    close();
@@ -238,19 +232,12 @@ IEffect::Status EffectImpl::status(binder_status_t status, size_t consumed, size
}

// A placeholder processing implementation to copy samples from input to output
IEffect::Status EffectImpl::effectProcessImpl(float* in, float* out, int processSamples) {
    // lock before access context/parameters
    std::lock_guard lg(mMutex);
    IEffect::Status status = {EX_NULL_POINTER, 0, 0};
    RETURN_VALUE_IF(!mContext, status, "nullContext");
    auto frameSize = mContext->getInputFrameSize();
    RETURN_VALUE_IF(0 == frameSize, status, "frameSizeIs0");
    LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << processSamples
               << " frames " << processSamples * sizeof(float) / frameSize;
    for (int i = 0; i < processSamples; i++) {
IEffect::Status EffectImpl::effectProcessImpl(float* in, float* out, int samples) {
    for (int i = 0; i < samples; i++) {
        *out++ = *in++;
    }
    LOG(DEBUG) << __func__ << " done processing " << processSamples << " samples";
    return {STATUS_OK, processSamples, processSamples};
    LOG(DEBUG) << __func__ << " done processing " << samples << " samples";
    return {STATUS_OK, samples, samples};
}

}  // namespace aidl::android::hardware::audio::effect
+51 −8
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
 * limitations under the License.
 */

#include <memory>
#define LOG_TAG "AHAL_EffectThread"
#include <android-base/logging.h>
#include <pthread.h>
@@ -32,13 +33,18 @@ EffectThread::~EffectThread() {
    LOG(DEBUG) << __func__ << " done";
};

RetCode EffectThread::createThread(const std::string& name, const int priority) {
RetCode EffectThread::createThread(std::shared_ptr<EffectContext> context, const std::string& name,
                                   const int priority) {
    if (mThread.joinable()) {
        LOG(WARNING) << __func__ << " thread already created, no-op";
        return RetCode::SUCCESS;
    }
    mName = name;
    mPriority = priority;
    {
        std::lock_guard lg(mThreadMutex);
        mThreadContext = std::move(context);
    }
    mThread = std::thread(&EffectThread::threadLoop, this);
    LOG(DEBUG) << __func__ << " " << name << " priority " << mPriority << " done";
    return RetCode::SUCCESS;
@@ -46,7 +52,7 @@ RetCode EffectThread::createThread(const std::string& name, const int priority)

RetCode EffectThread::destroyThread() {
    {
        std::lock_guard lg(mMutex);
        std::lock_guard lg(mThreadMutex);
        mStop = mExit = true;
    }
    mCv.notify_one();
@@ -54,6 +60,11 @@ RetCode EffectThread::destroyThread() {
    if (mThread.joinable()) {
        mThread.join();
    }

    {
        std::lock_guard lg(mThreadMutex);
        mThreadContext.reset();
    }
    LOG(DEBUG) << __func__ << " done";
    return RetCode::SUCCESS;
}
@@ -65,7 +76,7 @@ RetCode EffectThread::startThread() {
    }

    {
        std::lock_guard lg(mMutex);
        std::lock_guard lg(mThreadMutex);
        if (!mStop) {
            LOG(WARNING) << __func__ << " already start";
            return RetCode::SUCCESS;
@@ -85,7 +96,7 @@ RetCode EffectThread::stopThread() {
    }

    {
        std::lock_guard lg(mMutex);
        std::lock_guard lg(mThreadMutex);
        if (mStop) {
            LOG(WARNING) << __func__ << " already stop";
            return RetCode::SUCCESS;
@@ -97,13 +108,13 @@ RetCode EffectThread::stopThread() {
}

void EffectThread::threadLoop() {
    pthread_setname_np(pthread_self(), mName.substr(0, MAX_TASK_COMM_LEN - 1).c_str());
    pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str());
    setpriority(PRIO_PROCESS, 0, mPriority);
    while (true) {
        bool needExit = false;
        {
            std::unique_lock l(mMutex);
            mCv.wait(l, [&]() REQUIRES(mMutex) {
            std::unique_lock l(mThreadMutex);
            mCv.wait(l, [&]() REQUIRES(mThreadMutex) {
                needExit = mExit;
                return mExit || !mStop;
            });
@@ -112,9 +123,41 @@ void EffectThread::threadLoop() {
            LOG(WARNING) << __func__ << " EXIT!";
            return;
        }
        // process without lock

        process();
    }
}

void EffectThread::process() {
    std::shared_ptr<EffectContext> context;
    {
        std::lock_guard lg(mThreadMutex);
        context = mThreadContext;
        RETURN_VALUE_IF(!context, void(), "nullContext");
    }
    std::shared_ptr<EffectContext::StatusMQ> statusMQ = context->getStatusFmq();
    std::shared_ptr<EffectContext::DataMQ> inputMQ = context->getInputDataFmq();
    std::shared_ptr<EffectContext::DataMQ> outputMQ = context->getOutputDataFmq();
    auto buffer = context->getWorkBuffer();

    // Only this worker will read from input data MQ and write to output data MQ.
    auto readSamples = inputMQ->availableToRead(), writeSamples = outputMQ->availableToWrite();
    if (readSamples && writeSamples) {
        auto processSamples = std::min(readSamples, writeSamples);
        LOG(DEBUG) << __func__ << " available to read " << readSamples << " available to write "
                   << writeSamples << " process " << processSamples;

        inputMQ->read(buffer, processSamples);

        // call effectProcessImpl without lock
        IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples);
        outputMQ->write(buffer, status.fmqProduced);
        statusMQ->writeBlocking(&status, 1);
        LOG(DEBUG) << __func__ << " done processing, effect consumed " << status.fmqConsumed
                   << " produced " << status.fmqProduced;
    } else {
        // TODO: maybe add some sleep here to avoid busy waiting
    }
}

}  // namespace aidl::android::hardware::audio::effect
+59 −12
Original line number Diff line number Diff line
@@ -14,10 +14,11 @@
 * limitations under the License.
 */

#include <algorithm>
#include <cstddef>
#include <memory>
#define LOG_TAG "AHAL_BassBoostSw"
#include <Utils.h>
#include <algorithm>
#include <unordered_set>

#include <android-base/logging.h>
@@ -73,28 +74,74 @@ ndk::ScopedAStatus BassBoostSw::getDescriptor(Descriptor* _aidl_return) {
ndk::ScopedAStatus BassBoostSw::setParameterSpecific(const Parameter::Specific& specific) {
    RETURN_IF(Parameter::Specific::bassBoost != specific.getTag(), EX_ILLEGAL_ARGUMENT,
              "EffectNotSupported");
    std::lock_guard lg(mMutex);
    RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");

    mSpecificParam = specific.get<Parameter::Specific::bassBoost>();
    LOG(DEBUG) << __func__ << " success with: " << specific.toString();
    auto& bbParam = specific.get<Parameter::Specific::bassBoost>();
    auto tag = bbParam.getTag();

    switch (tag) {
        case BassBoost::strengthPm: {
            RETURN_IF(!mStrengthSupported, EX_ILLEGAL_ARGUMENT, "SettingStrengthNotSupported");

            RETURN_IF(mContext->setBbStrengthPm(bbParam.get<BassBoost::strengthPm>()) !=
                              RetCode::SUCCESS,
                      EX_ILLEGAL_ARGUMENT, "strengthPmNotSupported");
            return ndk::ScopedAStatus::ok();
        }
        default: {
            LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
            return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
                                                                    "BassBoostTagNotSupported");
        }
    }
}

ndk::ScopedAStatus BassBoostSw::getParameterSpecific(const Parameter::Id& id,
                                                     Parameter::Specific* specific) {
    auto tag = id.getTag();
    RETURN_IF(Parameter::Id::bassBoostTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
    specific->set<Parameter::Specific::bassBoost>(mSpecificParam);
    auto bbId = id.get<Parameter::Id::bassBoostTag>();
    auto bbIdTag = bbId.getTag();
    switch (bbIdTag) {
        case BassBoost::Id::commonTag:
            return getParameterBassBoost(bbId.get<BassBoost::Id::commonTag>(), specific);
        default:
            LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
            return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
                                                                    "BassBoostTagNotSupported");
    }
}

ndk::ScopedAStatus BassBoostSw::getParameterBassBoost(const BassBoost::Tag& tag,
                                                      Parameter::Specific* specific) {
    RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
    BassBoost bbParam;
    switch (tag) {
        case BassBoost::strengthPm: {
            bbParam.set<BassBoost::strengthPm>(mContext->getBbStrengthPm());
            break;
        }
        default: {
            LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
            return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
                                                                    "BassBoostTagNotSupported");
        }
    }

    specific->set<Parameter::Specific::bassBoost>(bbParam);
    return ndk::ScopedAStatus::ok();
}

std::shared_ptr<EffectContext> BassBoostSw::createContext(const Parameter::Common& common) {
    if (mContext) {
        LOG(DEBUG) << __func__ << " context already exist";
    } else {
        mContext = std::make_shared<BassBoostSwContext>(1 /* statusFmqDepth */, common);
    }
    return mContext;
}
    mContext = std::make_shared<BassBoostSwContext>(1 /* statusFmqDepth */, common);

std::shared_ptr<EffectContext> BassBoostSw::getContext() {
    return mContext;
}

@@ -106,13 +153,13 @@ RetCode BassBoostSw::releaseContext() {
}

// Processing method running in EffectWorker thread.
IEffect::Status BassBoostSw::effectProcessImpl(float* in, float* out, int process) {
IEffect::Status BassBoostSw::effectProcessImpl(float* in, float* out, int samples) {
    // TODO: get data buffer and process.
    LOG(DEBUG) << __func__ << " in " << in << " out " << out << " process " << process;
    for (int i = 0; i < process; i++) {
    LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
    for (int i = 0; i < samples; i++) {
        *out++ = *in++;
    }
    return {STATUS_OK, process, process};
    return {STATUS_OK, samples, samples};
}

}  // namespace aidl::android::hardware::audio::effect
Loading