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

Commit 094dc766 authored by Andy Hung's avatar Andy Hung
Browse files

MelReporter, PatchCommandThread: Update to audio_utils mutex

Test: atest AudioTrackTest AudioRecordTest
Test: atest AAudioTests AudioTrackOffloadTest
Test: atest AudioPlaybackCaptureTest
Test: Camera YouTube
Bug: 298534151
Merged-In: I3dc0999d66cc46db32e251ac879f92e36a93e7fc
Change-Id: I3dc0999d66cc46db32e251ac879f92e36a93e7fc
parent 2ac52f13
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ bool MelReporter::activateHalSoundDoseComputation(const std::string& module,

void MelReporter::activateInternalSoundDoseComputation() {
    {
        std::lock_guard _l(mLock);
        audio_utils::lock_guard _l(mutex());
        if (!mUseHalSoundDoseInterface) {
            // no need to start internal MEL on active patches
            return;
@@ -111,8 +111,8 @@ void MelReporter::updateMetadataForCsd(audio_io_handle_t streamHandle,
        return;
    }

    std::lock_guard _laf(mAfMelReporterCallback->mutex());
    std::lock_guard _l(mLock);
    audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex());
    audio_utils::lock_guard _l(mutex());
    auto activeMelPatchId = activePatchStreamHandle_l(streamHandle);
    if (!activeMelPatchId) {
        ALOGV("%s stream handle %d does not have an active patch", __func__, streamHandle);
@@ -171,8 +171,8 @@ void MelReporter::onCreateAudioPatch(audio_patch_handle_t handle,
    }

    if (!newPatch.deviceHandles.empty()) {
        std::lock_guard _afl(mAfMelReporterCallback->mutex());
        std::lock_guard _l(mLock);
        audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex());
        audio_utils::lock_guard _l(mutex());
        ALOGV("%s add patch handle %d to active devices", __func__, handle);
        startMelComputationForActivePatch_l(newPatch);
        newPatch.csdActive = true;
@@ -213,7 +213,7 @@ void MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) {

    ActiveMelPatch melPatch;
    {
        std::lock_guard _l(mLock);
        audio_utils::lock_guard _l(mutex());

        auto patchIt = mActiveMelPatches.find(handle);
        if (patchIt == mActiveMelPatches.end()) {
@@ -226,8 +226,8 @@ void MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) {
        mActiveMelPatches.erase(patchIt);
    }

    std::lock_guard _afl(mAfMelReporterCallback->mutex());
    std::lock_guard _l(mLock);
    audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex());
    audio_utils::lock_guard _l(mutex());
    stopMelComputationForPatch_l(melPatch);
}

@@ -239,7 +239,7 @@ sp<media::ISoundDose> MelReporter::getSoundDoseInterface(

void MelReporter::stopInternalMelComputation() {
    ALOGV("%s", __func__);
    std::lock_guard _l(mLock);
    audio_utils::lock_guard _l(mutex());
    mActiveMelPatches.clear();
    mUseHalSoundDoseInterface = true;
}
@@ -286,7 +286,7 @@ bool MelReporter::useHalSoundDoseInterface_l() {
}

std::string MelReporter::dump() {
    std::lock_guard _l(mLock);
    audio_utils::lock_guard _l(mutex());
    std::string output("\nSound Dose:\n");
    output.append(mSoundDoseManager->dump());
    return output;
+14 −13
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@
#include <audio_utils/mutex.h>
#include <sounddose/SoundDoseManager.h>

#include <mutex>
#include <unordered_map>

namespace android {
@@ -100,30 +99,32 @@ private:
    bool shouldComputeMelForDeviceType(audio_devices_t device);

    void stopInternalMelComputation();
    audio_utils::mutex& mutex() const { return mMutex; }

    /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
    void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
    /** Should be called with the following order of locks: mAudioFlinger.mutex() -> mutex(). */
    void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mutex());

    /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
    void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
    /** Should be called with the following order of locks: mAudioFlinger.mutex() -> mutex(). */
    void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mutex());

    std::optional<audio_patch_handle_t>
    activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mLock);
    activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mutex());

    bool useHalSoundDoseInterface_l() REQUIRES(mLock);
    bool useHalSoundDoseInterface_l() REQUIRES(mutex());

    const sp<IAfMelReporterCallback> mAfMelReporterCallback;

    sp<SoundDoseManager> mSoundDoseManager;
    /* const */ sp<SoundDoseManager> mSoundDoseManager;  // set onFirstRef

    /**
     * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock.
     * Locking order AudioFlinger::mLock -> PatchCommandThread::mLock -> MelReporter::mLock.
     * Locking order AudioFlinger::mutex() -> PatchCommandThread::mutex() -> MelReporter::mutex().
     */
    std::mutex mLock;
    std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches GUARDED_BY(mLock);
    std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mLock);
    bool mUseHalSoundDoseInterface GUARDED_BY(mLock) = false;
    mutable audio_utils::mutex mMutex;
    std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches
            GUARDED_BY(mutex());
    std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mutex());
    bool mUseHalSoundDoseInterface GUARDED_BY(mutex()) = false;
};

}  // namespace android
+6 −7
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ constexpr char kPatchCommandThreadName[] = "AudioFlinger_PatchCommandThread";
PatchCommandThread::~PatchCommandThread() {
    exit();

    std::lock_guard _l(mLock);
    audio_utils::lock_guard _l(mutex());
    mCommands.clear();
}

@@ -39,7 +39,7 @@ void PatchCommandThread::onFirstRef() {

void PatchCommandThread::addListener(const sp<PatchCommandListener>& listener) {
    ALOGV("%s add listener %p", __func__, static_cast<void*>(listener.get()));
    std::lock_guard _l(mListenerLock);
    audio_utils::lock_guard _l(listenerMutex());
    mListeners.emplace_back(listener);
}

@@ -59,9 +59,8 @@ void PatchCommandThread::releaseAudioPatch(audio_patch_handle_t handle) {
}

bool PatchCommandThread::threadLoop()
NO_THREAD_SAFETY_ANALYSIS  // bug in clang compiler.
{
    std::unique_lock _l(mLock);
    audio_utils::unique_lock _l(mutex());

    while (!exitPending()) {
        while (!mCommands.empty() && !exitPending()) {
@@ -71,7 +70,7 @@ NO_THREAD_SAFETY_ANALYSIS // bug in clang compiler.

            std::vector<wp<PatchCommandListener>> listenersCopy;
            {
                std::lock_guard _ll(mListenerLock);
                audio_utils::lock_guard _ll(listenerMutex());
                listenersCopy = mListeners;
            }

@@ -122,7 +121,7 @@ NO_THREAD_SAFETY_ANALYSIS // bug in clang compiler.
}

void PatchCommandThread::sendCommand(const sp<Command>& command) {
    std::lock_guard _l(mLock);
    audio_utils::lock_guard _l(mutex());
    mCommands.emplace_back(command);
    mWaitWorkCV.notify_one();
}
@@ -148,7 +147,7 @@ void PatchCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle) {
void PatchCommandThread::exit() {
    ALOGV("%s", __func__);
    {
        std::lock_guard _l(mLock);
        audio_utils::lock_guard _l(mutex());
        requestExit();
        mWaitWorkCV.notify_one();
    }
+8 −5
Original line number Diff line number Diff line
@@ -100,13 +100,16 @@ private:

    void sendCommand(const sp<Command>& command);

    audio_utils::mutex& mutex() const { return mMutex; }
    audio_utils::mutex& listenerMutex() const { return mListenerMutex; }

    std::string mThreadName;
    std::mutex mLock;
    std::condition_variable mWaitWorkCV;
    std::deque<sp<Command>> mCommands GUARDED_BY(mLock); // list of pending commands
    mutable audio_utils::mutex mMutex;
    audio_utils::condition_variable mWaitWorkCV;
    std::deque<sp<Command>> mCommands GUARDED_BY(mutex()); // list of pending commands

    std::mutex mListenerLock;
    std::vector<wp<PatchCommandListener>> mListeners GUARDED_BY(mListenerLock);
    mutable audio_utils::mutex mListenerMutex;
    std::vector<wp<PatchCommandListener>> mListeners GUARDED_BY(listenerMutex());
};

}  // namespace android
+2 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include "TrackBase.h"

#include <android/os/BnExternalVibrationController.h>
#include <audio_utils/mutex.h>
#include <audio_utils/LinearMap.h>
#include <binder/AppOpsManager.h>

@@ -467,7 +468,7 @@ private:
     */
    SourceMetadatas mTrackMetadatas;
    /** Protects mTrackMetadatas against concurrent access. */
    mutable std::mutex mTrackMetadatasMutex;
    mutable audio_utils::mutex mTrackMetadatasMutex;
};  // end of OutputTrack

// playback track, used by PatchPanel
Loading