aaudio: steal exclusive streams
An app (B) that asks for an exclusive stream can steal
an exclusive stream from an earlier app (A).
App B will be given the MMAP resource as a SHARED stream.
The stream for app A will be disconnected and released
by the service.
If app A reopens a stream then it will get a SHARED
stream.
The order of the opening of the streams is controlled by using a
recursive_mutex in AAudioService::openStream().
Bug: 129846760
Test: media/libaaudio/tests/test_steal_exclusive.cpp
Test: also
Test: Launch AudioTroubleMaker. It should say "EXCLUSIVE".
Test: Press Home button.
Test: Siren sound from AudioTroubleMaker should continue.
Test: Launch OboeTester
Test: TEST OUTPUT, then Open, Start
Test: You should get an MMAP SHARED stream on Pixel.
Test: Go back to AudioTroubleMaker. It should say "SHARED".
Change-Id: I7f8339d8ed62546520a9b46ed398418b41ca2832
diff --git a/services/oboeservice/AAudioService.cpp b/services/oboeservice/AAudioService.cpp
index cac7453..ecbcb7e 100644
--- a/services/oboeservice/AAudioService.cpp
+++ b/services/oboeservice/AAudioService.cpp
@@ -85,6 +85,17 @@
aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request,
aaudio::AAudioStreamConfiguration &configurationOutput) {
+ // A lock in is used to order the opening of endpoints when an
+ // EXCLUSIVE endpoint is stolen. We want the order to be:
+ // 1) Thread A opens exclusive MMAP endpoint
+ // 2) Thread B wants to open an exclusive MMAP endpoint so it steals the one from A
+ // under this lock.
+ // 3) Thread B opens a shared MMAP endpoint.
+ // 4) Thread A can then get the lock and also open a shared stream.
+ // Without the lock. Thread A might sneak in and reallocate an exclusive stream
+ // before B can open the shared stream.
+ std::unique_lock<std::recursive_mutex> lock(mOpenLock);
+
aaudio_result_t result = AAUDIO_OK;
sp<AAudioServiceStreamBase> serviceStream;
const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
@@ -139,7 +150,6 @@
return result;
} else {
aaudio_handle_t handle = mStreamTracker.addStreamForHandle(serviceStream.get());
- ALOGV("openStream(): handle = 0x%08X", handle);
serviceStream->setHandle(handle);
pid_t pid = request.getProcessId();
AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
@@ -147,6 +157,7 @@
// Log open in MediaMetrics after we have the handle because we need the handle to
// create the metrics ID.
serviceStream->logOpen(handle);
+ ALOGV("%s(): return handle = 0x%08X", __func__, handle);
return handle;
}
}
@@ -180,7 +191,10 @@
ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle);
return AAUDIO_ERROR_INVALID_HANDLE;
}
+ return closeStream(serviceStream);
+}
+aaudio_result_t AAudioService::closeStream(sp<AAudioServiceStreamBase> serviceStream) {
pid_t pid = serviceStream->getOwnerProcessId();
AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);