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

Commit 1c2f7e19 authored by Vlad Popa's avatar Vlad Popa
Browse files

CSD: Improve the MelProcessor lifecycle.

The MelProcessor lifecycle is now tied to the streaming thread
lifecycle. Whenever there are new patches and tracks for
sound dose we resume/pause the MEL calculation depending on
the track use case and device type.

Test: logging and dumpsys
Bug: 275370058
Change-Id: I9fb90df9ffb991f41f4209483bdafaf2b7474964
parent 943db3f3
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -103,19 +103,24 @@ status_t AudioStreamOutSink::getTimestamp(ExtendedTimestamp &timestamp)
void AudioStreamOutSink::startMelComputation(const sp<audio_utils::MelProcessor>& processor)
{
    ALOGV("%s start mel computation for device %d", __func__, processor->getDeviceId());
    mMelProcessor = processor;
    // update format for MEL computation

    mMelProcessor.store(processor);
    if (processor) {
        processor->updateAudioFormat(mFormat.mSampleRate,  mFormat.mChannelCount, mFormat.mFormat);
        // update format for MEL computation
        processor->updateAudioFormat(mFormat.mSampleRate,
                                     mFormat.mChannelCount,
                                     mFormat.mFormat);
        processor->resume();
    }

}

void AudioStreamOutSink::stopMelComputation()
{
    auto melProcessor = mMelProcessor.load();
    if (melProcessor != nullptr) {
        ALOGV("%s stop mel computation for device %d", __func__, melProcessor->getDeviceId());
        mMelProcessor = nullptr;
        ALOGV("%s pause mel computation for device %d", __func__, melProcessor->getDeviceId());
        melProcessor->pause();
    }
}

+9 −8
Original line number Diff line number Diff line
@@ -174,6 +174,7 @@ void AudioFlinger::MelReporter::onCreateAudioPatch(audio_patch_handle_t handle,
        }
    }

    if (!newPatch.deviceHandles.empty()) {
        std::lock_guard _afl(mAudioFlinger.mLock);
        std::lock_guard _l(mLock);
        ALOGV("%s add patch handle %d to active devices", __func__, handle);
@@ -181,6 +182,7 @@ void AudioFlinger::MelReporter::onCreateAudioPatch(audio_patch_handle_t handle,
        newPatch.csdActive = true;
        mActiveMelPatches[handle] = newPatch;
    }
}

void AudioFlinger::MelReporter::startMelComputationForActivePatch_l(const ActiveMelPatch& patch) {
    auto outputThread = mAudioFlinger.checkOutputThread_l(patch.streamHandle);
@@ -264,7 +266,6 @@ void AudioFlinger::MelReporter::stopMelComputationForPatch_l(const ActiveMelPatc
        }
    }

    mSoundDoseManager->removeStreamProcessor(patch.streamHandle);
    if (outputThread != nullptr) {
        outputThread->stopMelComputation_l();
    }
+11 −4
Original line number Diff line number Diff line
@@ -10851,16 +10851,23 @@ void AudioFlinger::MmapPlaybackThread::startMelComputation_l(
        const sp<audio_utils::MelProcessor>& processor)
{
    ALOGV("%s: starting mel processor for thread %d", __func__, id());
    if (processor != nullptr) {
        mMelProcessor = processor;
    mMelProcessor.store(processor);
    if (processor) {
        processor->resume();
    }

    // no need to update output format for MMapPlaybackThread since it is
    // assigned constant for each thread
}

// stopMelComputation_l() must be called with AudioFlinger::mLock held
void AudioFlinger::MmapPlaybackThread::stopMelComputation_l()
{
    ALOGV("%s: stopping mel processor for thread %d", __func__, id());
    mMelProcessor = nullptr;
    ALOGV("%s: pausing mel processor for thread %d", __func__, id());
    auto melProcessor = mMelProcessor.load();
    if (melProcessor != nullptr) {
        melProcessor->pause();
    }
}

void AudioFlinger::MmapPlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args)
+24 −21
Original line number Diff line number Diff line
@@ -51,14 +51,16 @@ sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice(
    std::lock_guard _l(mLock);

    if (mHalSoundDose != nullptr && !mDisableCsd) {
        ALOGW("%s: using HAL MEL computation, no MelProcessor needed.", __func__);
        ALOGD("%s: using HAL MEL computation, no MelProcessor needed.", __func__);
        return nullptr;
    }

    auto streamProcessor = mActiveProcessors.find(streamHandle);
    sp<audio_utils::MelProcessor> processor;
    if (streamProcessor != mActiveProcessors.end() &&
            (processor = streamProcessor->second.promote())) {
    if (streamProcessor != mActiveProcessors.end()) {
        auto processor = streamProcessor->second.promote();
        // if processor is nullptr it means it was removed by the playback
        // thread and can be replaced in the mActiveProcessors map
        if (processor != nullptr) {
            ALOGV("%s: found callback for stream id %d", __func__, streamHandle);
            const auto activeTypeIt = mActiveDeviceTypes.find(deviceId);
            if (activeTypeIt != mActiveDeviceTypes.end()) {
@@ -67,7 +69,9 @@ sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice(
            processor->setDeviceId(deviceId);
            processor->setOutputRs2UpperBound(mRs2UpperBound);
            return processor;
    } else {
        }
    }

    ALOGV("%s: creating new callback for stream id %d", __func__, streamHandle);
    sp<audio_utils::MelProcessor> melProcessor = sp<audio_utils::MelProcessor>::make(
            sampleRate, channelCount, format, this, deviceId, mRs2UpperBound);
@@ -78,7 +82,6 @@ sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice(
    mActiveProcessors[streamHandle] = melProcessor;
    return melProcessor;
}
}

bool SoundDoseManager::setHalSoundDoseInterface(const std::shared_ptr<ISoundDose>& halSoundDose) {
    ALOGV("%s", __func__);