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

Commit 2a06fca9 authored by Vlad Popa's avatar Vlad Popa
Browse files

CSD: enable sound dose with internal MEL

Test: logs and dumpsys
Bug: 268035692
Change-Id: I0dd8ca5f8c925d984a782293bba1a7be121fca7c
parent 1a0c3abf
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -48,6 +48,15 @@ interface ISoundDose {
     */
    oneway void updateAttenuation(float attenuationDB, int device);

    /**
     * Disable the calculation of sound dose. This has the effect that no MEL
     * values will be computed on the framework side. The MEL returned from
     * the IHalSoundDoseCallbacks will be ignored.
     * Should only be called once at startup if the AudioService does not
     * support CSD.
     */
    oneway void disableCsd();

    /* -------------------------- Test API methods --------------------------
    /** Get the currently used RS2 upper bound. */
    float getOutputRs2UpperBound();
+18 −0
Original line number Diff line number Diff line
@@ -81,6 +81,10 @@ void AudioFlinger::MelReporter::onFirstRef() {
}

bool AudioFlinger::MelReporter::shouldComputeMelForDeviceType(audio_devices_t device) {
    if (mSoundDoseManager->isCsdDisabled()) {
        ALOGV("%s csd is disabled", __func__);
        return false;
    }
    if (mSoundDoseManager->forceComputeCsdOnAllDevices()) {
        return true;
    }
@@ -102,6 +106,11 @@ bool AudioFlinger::MelReporter::shouldComputeMelForDeviceType(audio_devices_t de

void AudioFlinger::MelReporter::updateMetadataForCsd(audio_io_handle_t streamHandle,
        const std::vector<playback_track_metadata_v7_t>& metadataVec) {
    if (mSoundDoseManager->isCsdDisabled()) {
        ALOGV("%s csd is disabled", __func__);
        return;
    }

    std::lock_guard _laf(mAudioFlinger.mLock);
    std::lock_guard _l(mLock);
    auto activeMelPatchId = activePatchStreamHandle_l(streamHandle);
@@ -133,6 +142,10 @@ void AudioFlinger::MelReporter::updateMetadataForCsd(audio_io_handle_t streamHan

void AudioFlinger::MelReporter::onCreateAudioPatch(audio_patch_handle_t handle,
        const PatchPanel::Patch& patch) {
    if (mSoundDoseManager->isCsdDisabled()) {
        ALOGV("%s csd is disabled", __func__);
        return;
    }
    if (useHalSoundDoseInterface()) {
        ALOGV("%s using HAL sound dose, ignore new patch", __func__);
        return;
@@ -193,6 +206,11 @@ void AudioFlinger::MelReporter::startMelComputationForActivePatch_l(const Active
}

void AudioFlinger::MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) {
    if (mSoundDoseManager->isCsdDisabled()) {
        ALOGV("%s csd is disabled", __func__);
        return;
    }

    ActiveMelPatch melPatch;
    {
        std::lock_guard _l(mLock);
+52 −1
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice(
        size_t channelCount, audio_format_t format) {
    std::lock_guard _l(mLock);

    if (mHalSoundDose != nullptr) {
    if (mHalSoundDose != nullptr && !mDisableCsd) {
        ALOGW("%s: using HAL MEL computation, no MelProcessor needed.", __func__);
        return nullptr;
    }
@@ -287,6 +287,15 @@ binder::Status SoundDoseManager::SoundDose::updateAttenuation(float attenuationD
    return binder::Status::ok();
}

binder::Status SoundDoseManager::SoundDose::disableCsd() {
    ALOGV("%s", __func__);
    auto soundDoseManager = mSoundDoseManager.promote();
    if (soundDoseManager != nullptr) {
        soundDoseManager->disableCsd();
    }
    return binder::Status::ok();
}

binder::Status SoundDoseManager::SoundDose::getOutputRs2UpperBound(float* value) {
    ALOGV("%s", __func__);
    auto soundDoseManager = mSoundDoseManager.promote();
@@ -343,6 +352,28 @@ void SoundDoseManager::updateAttenuation(float attenuationDB, audio_devices_t de
    }
}

void SoundDoseManager::disableCsd() {
    ALOGV("%s",  __func__);

    std::lock_guard _l(mLock);
    mDisableCsd = true;

    // Normally, there should be no active MelProcessors when this method is called
    // We pause however every cached MelProcessor as a defensive mechanism to not
    // have unnecessary processing
    for (auto& activeEntry : mActiveProcessors) {
        auto melProcessor = activeEntry.second.promote();
        if (melProcessor != nullptr) {
            melProcessor->pause();
        }
    }
}

bool SoundDoseManager::isCsdDisabled() {
    std::lock_guard _l(mLock);
    return mDisableCsd;
}

void SoundDoseManager::setUseFrameworkMel(bool useFrameworkMel) {
    // invalidate any HAL sound dose interface used
    setHalSoundDoseInterface(nullptr);
@@ -392,11 +423,16 @@ void SoundDoseManager::onNewMelValues(const std::vector<float>& mels, size_t off
                                      audio_port_handle_t deviceId) const {
    ALOGV("%s", __func__);


    sp<media::ISoundDoseCallback> soundDoseCallback;
    std::vector<audio_utils::CsdRecord> records;
    float currentCsd;
    {
        std::lock_guard _l(mLock);
        if (mDisableCsd) {
            return;
        }


        int64_t timestampSec = getMonotonicSecond();

@@ -432,6 +468,13 @@ sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const {
void SoundDoseManager::onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const {
    ALOGV("%s: Momentary exposure for device %d triggered: %f MEL", __func__, deviceId, currentMel);

    {
        std::lock_guard _l(mLock);
        if (mDisableCsd) {
            return;
        }
    }

    auto soundDoseCallback = getSoundDoseCallback();
    if (soundDoseCallback != nullptr) {
        soundDoseCallback->onMomentaryExposure(currentMel, deviceId);
@@ -451,6 +494,14 @@ sp<media::ISoundDose> SoundDoseManager::getSoundDoseInterface(

std::string SoundDoseManager::dump() const {
    std::string output;
    {
        std::lock_guard _l(mLock);
        if (mDisableCsd) {
            base::StringAppendF(&output, "CSD is disabled");
            return output;
        }
    }

    mMelAggregator->foreachCsd([&output](audio_utils::CsdRecord csdRecord) {
        base::StringAppendF(&output,
                            "CSD %f with average MEL %f in interval [%" PRId64 ", %" PRId64 "]",
+9 −1
Original line number Diff line number Diff line
@@ -101,6 +101,9 @@ public:
    /** Clear all map entries with passed audio_port_handle_t. */
    void clearMapDeviceIdEntries(audio_port_handle_t deviceId);

    /** Returns true if CSD is disabled. */
    bool isCsdDisabled();

    std::string dump() const;

    // used for testing only
@@ -134,6 +137,8 @@ private:
                                const std::vector<media::SoundDoseRecord>& records) override;
        binder::Status updateAttenuation(float attenuationDB, int device) override;
        binder::Status getOutputRs2UpperBound(float* value) override;
        binder::Status disableCsd() override;

        binder::Status getCsd(float* value) override;
        binder::Status forceUseFrameworkMel(bool useFrameworkMel) override;
        binder::Status forceComputeCsdOnAllDevices(bool computeCsdOnAllDevices) override;
@@ -164,6 +169,7 @@ private:
    sp<media::ISoundDoseCallback> getSoundDoseCallback() const;

    void updateAttenuation(float attenuationDB, audio_devices_t deviceType);
    void disableCsd();
    void setUseFrameworkMel(bool useFrameworkMel);
    void setComputeCsdOnAllDevices(bool computeCsdOnAllDevices);
    /** Returns the HAL sound dose interface or null if internal MEL computation is used. */
@@ -191,8 +197,10 @@ private:
    std::shared_ptr<ISoundDose> mHalSoundDose GUARDED_BY(mLock);
    std::shared_ptr<HalSoundDoseCallback> mHalSoundDoseCallback GUARDED_BY(mLock);

    bool mUseFrameworkMel GUARDED_BY(mLock) = false;
    bool mUseFrameworkMel GUARDED_BY(mLock) = true;
    bool mComputeCsdOnAllDevices GUARDED_BY(mLock) = false;

    bool mDisableCsd GUARDED_BY(mLock) = false;
};

}  // namespace android
+2 −1
Original line number Diff line number Diff line
@@ -239,7 +239,8 @@ TEST_F(SoundDoseManagerTest, GetDefaultForceComputeCsdOnAllDevices) {
}

TEST_F(SoundDoseManagerTest, GetDefaultForceUseFrameworkMel) {
    EXPECT_FALSE(mSoundDoseManager->forceUseFrameworkMel());
    // TODO: for now dogfooding with internal MEL. Revert to false when using the HAL MELs
    EXPECT_TRUE(mSoundDoseManager->forceUseFrameworkMel());
}

}  // namespace