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

Commit a014280f authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "aaudio: disable exclusive mode after steal" into rvc-dev am: 4e48c247

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/av/+/11665503

Change-Id: I3d9852586a4cdf34f187a202768789cbbe153334
parents 537843d6 4e48c247
Loading
Loading
Loading
Loading
+28 −10
Original line number Diff line number Diff line
@@ -106,18 +106,9 @@ int32_t AAudioClientTracker::getStreamCount(pid_t pid) {

aaudio_result_t
AAudioClientTracker::registerClientStream(pid_t pid, sp<AAudioServiceStreamBase> serviceStream) {
    aaudio_result_t result = AAUDIO_OK;
    ALOGV("registerClientStream(%d,)\n", pid);
    std::lock_guard<std::mutex> lock(mLock);
    sp<NotificationClient> notificationClient = mNotificationClients[pid];
    if (notificationClient == 0) {
        // This will get called the first time the audio server registers an internal stream.
        ALOGV("registerClientStream(%d,) unrecognized pid\n", pid);
        notificationClient = new NotificationClient(pid, nullptr);
        mNotificationClients[pid] = notificationClient;
    }
    notificationClient->registerClientStream(serviceStream);
    return result;
    return getNotificationClient_l(pid)->registerClientStream(serviceStream);
}

// Find the tracker for this process and remove it.
@@ -136,6 +127,33 @@ AAudioClientTracker::unregisterClientStream(pid_t pid,
    return AAUDIO_OK;
}

void AAudioClientTracker::setExclusiveEnabled(pid_t pid, bool enabled) {
    ALOGD("%s(%d, %d)\n", __func__, pid, enabled);
    std::lock_guard<std::mutex> lock(mLock);
    getNotificationClient_l(pid)->setExclusiveEnabled(enabled);
}

bool AAudioClientTracker::isExclusiveEnabled(pid_t pid) {
    std::lock_guard<std::mutex> lock(mLock);
    return getNotificationClient_l(pid)->isExclusiveEnabled();
}

sp<AAudioClientTracker::NotificationClient>
        AAudioClientTracker::getNotificationClient_l(pid_t pid) {
    sp<NotificationClient> notificationClient = mNotificationClients[pid];
    if (notificationClient == nullptr) {
        // This will get called the first time the audio server uses this PID.
        ALOGV("%s(%d,) unrecognized PID\n", __func__, pid);
        notificationClient = new AAudioClientTracker::NotificationClient(pid, nullptr);
        mNotificationClients[pid] = notificationClient;
    }
    return notificationClient;
}

// =======================================
// AAudioClientTracker::NotificationClient
// =======================================

AAudioClientTracker::NotificationClient::NotificationClient(pid_t pid, const sp<IBinder>& binder)
        : mProcessId(pid), mBinder(binder) {
}
+22 −1
Original line number Diff line number Diff line
@@ -58,6 +58,15 @@ public:
    aaudio_result_t unregisterClientStream(pid_t pid,
                                           android::sp<AAudioServiceStreamBase> serviceStream);

    /**
     * Specify whether a process is allowed to create an EXCLUSIVE MMAP stream.
     * @param pid
     * @param enabled
     */
    void setExclusiveEnabled(pid_t pid, bool enabled);

    bool isExclusiveEnabled(pid_t pid);

    android::AAudioService *getAAudioService() const {
        return mAAudioService;
    }
@@ -84,17 +93,29 @@ private:

        aaudio_result_t unregisterClientStream(android::sp<AAudioServiceStreamBase> serviceStream);

        void setExclusiveEnabled(bool enabled) {
            mExclusiveEnabled = enabled;
        }

        bool isExclusiveEnabled() {
            return mExclusiveEnabled;
        }

        // IBinder::DeathRecipient
        virtual     void    binderDied(const android::wp<IBinder>& who);

    protected:
    private:
        mutable std::mutex                              mLock;
        const pid_t                                     mProcessId;
        std::set<android::sp<AAudioServiceStreamBase>>  mStreams;
        // hold onto binder to receive death notifications
        android::sp<IBinder>                            mBinder;
        bool                                            mExclusiveEnabled = true;
    };

    // This must be called under mLock
    android::sp<NotificationClient> getNotificationClient_l(pid_t pid);

    mutable std::mutex                               mLock;
    std::map<pid_t, android::sp<NotificationClient>> mNotificationClients;
    android::AAudioService                          *mAAudioService = nullptr;
+10 −1
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include <sstream>
#include <utility/AAudioUtilities.h>

#include "AAudioClientTracker.h"
#include "AAudioEndpointManager.h"
#include "AAudioServiceEndpointShared.h"
#include "AAudioServiceEndpointMMAP.h"
@@ -174,7 +175,15 @@ sp<AAudioServiceEndpoint> AAudioEndpointManager::openExclusiveEndpoint(
                && !request.isSharingModeMatchRequired()) { // app did not request a shared stream
            ALOGD("%s() endpoint in EXCLUSIVE use. Steal it!", __func__);
            mExclusiveStolenCount++;
            endpointToSteal = endpoint;
            // Prevent this process from getting another EXCLUSIVE stream.
            // This will prevent two clients from colliding after a DISCONNECTION
            // when they both try to open an exclusive stream at the same time.
            // That can result in a stream getting disconnected between the OPEN
            // and START calls. This will help preserve app compatibility.
            // An app can avoid having this happen by closing their streams when
            // the app is paused.
            AAudioClientTracker::getInstance().setExclusiveEnabled(request.getProcessId(), false);
            endpointToSteal = endpoint; // return it to caller
        }
        return nullptr;
    } else {
+2 −1
Original line number Diff line number Diff line
@@ -117,7 +117,8 @@ aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &req
        return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
    }

    if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
    if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE
        && AAudioClientTracker::getInstance().isExclusiveEnabled(request.getProcessId())) {
        // only trust audioserver for in service indication
        bool inService = false;
        if (isCallerInService()) {