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

Commit 98b8d45c authored by Jiabin Huang's avatar Jiabin Huang Committed by Android Build Cherrypicker Worker
Browse files

Add RWLock when accessing tee patches.

Bug: 314041707
Test: atest AudioPlaybackCaptureTest
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:73f9705e6ede00d224047736a8cb31e7790f2f7d)
Merged-In: I3d6e7d434a983c869e3523687e5fb5cb67a253af
Change-Id: I3d6e7d434a983c869e3523687e5fb5cb67a253af
parent d85f7841
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <audio_utils/mutex.h>
#include <audio_utils/LinearMap.h>
#include <binder/AppOpsManager.h>
#include <utils/RWLock.h>

namespace android {

@@ -352,6 +353,7 @@ private:
    // Must hold thread lock to access tee patches
    template <class F>
    void                forEachTeePatchTrack_l(F f) {
        RWLock::AutoRLock readLock(mTeePatchesRWLock);
        for (auto& tp : mTeePatches) { f(tp.patchTrack); }
    };

@@ -387,6 +389,7 @@ private:
    audio_output_flags_t mFlags;
    TeePatches mTeePatches;
    std::optional<TeePatches> mTeePatchesToUpdate;
    RWLock              mTeePatchesRWLock;
    const float         mSpeed;
    const bool          mIsSpatialized;
    const bool          mIsBitPerfect;
+12 −3
Original line number Diff line number Diff line
@@ -1081,7 +1081,13 @@ void Track::interceptBuffer(
        // Additionally PatchProxyBufferProvider::obtainBuffer (called by PathTrack::getNextBuffer)
        // does not allow 0 frame size request contrary to getNextBuffer
    }
    for (auto& teePatch : mTeePatches) {
    TeePatches teePatches;
    if (mTeePatchesRWLock.tryReadLock() == NO_ERROR) {
        // Cache a copy of tee patches in case it is updated while using.
        teePatches = mTeePatches;
        mTeePatchesRWLock.unlock();
    }
    for (auto& teePatch : teePatches) {
        IAfPatchRecord* patchRecord = teePatch.patchRecord.get();
        const size_t framesWritten = patchRecord->writeFrames(
                sourceBuffer.i8, frameCount, mFrameSize);
@@ -1094,7 +1100,7 @@ void Track::interceptBuffer(
    using namespace std::chrono_literals;
    // Average is ~20us per track, this should virtually never be logged (Logging takes >200us)
    ALOGD_IF(spent > 500us, "%s: took %lldus to intercept %zu tracks", __func__,
             spent.count(), mTeePatches.size());
             spent.count(), teePatches.size());
}

// ExtendedAudioBufferProvider interface
@@ -1616,7 +1622,10 @@ void Track::copyMetadataTo(MetadataInserter& backInserter) const
void Track::updateTeePatches_l() {
    if (mTeePatchesToUpdate.has_value()) {
        forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->destroy(); });
        mTeePatches = mTeePatchesToUpdate.value();
        {
            RWLock::AutoWLock writeLock(mTeePatchesRWLock);
            mTeePatches = std::move(mTeePatchesToUpdate.value());
        }
        if (mState == TrackBase::ACTIVE || mState == TrackBase::RESUMING ||
                mState == TrackBase::STOPPING_1) {
            forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->start(); });