blob: 407f6d5b8dcb223336fb4d47433bbc18ec72415b [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Eric Laurenta54f1282017-07-01 19:39:32 -070017#define LOG_TAG "AAudioEndpointManager"
Phil Burk71f35bb2017-04-13 16:05:07 -070018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burkc0c70e32017-02-09 13:18:38 -080021#include <assert.h>
Andy Hung47c5e532017-06-26 18:28:00 -070022#include <functional>
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include <map>
24#include <mutex>
Andy Hung47c5e532017-06-26 18:28:00 -070025#include <sstream>
26#include <utility/AAudioUtilities.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080027
Phil Burk836f9df2020-05-29 13:20:28 -070028#include "AAudioClientTracker.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080029#include "AAudioEndpointManager.h"
Phil Burk39f02dd2017-08-04 09:13:31 -070030#include "AAudioServiceEndpointShared.h"
31#include "AAudioServiceEndpointMMAP.h"
32#include "AAudioServiceEndpointCapture.h"
33#include "AAudioServiceEndpointPlay.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080034
35using namespace android;
36using namespace aaudio;
37
38ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager);
39
40AAudioEndpointManager::AAudioEndpointManager()
Phil Burk71f35bb2017-04-13 16:05:07 -070041 : Singleton<AAudioEndpointManager>()
Phil Burk39f02dd2017-08-04 09:13:31 -070042 , mSharedStreams()
43 , mExclusiveStreams() {
Phil Burkc0c70e32017-02-09 13:18:38 -080044}
45
Phil Burk0bd745e2020-10-17 18:20:01 +000046std::string AAudioEndpointManager::dump() const NO_THREAD_SAFETY_ANALYSIS {
Andy Hung47c5e532017-06-26 18:28:00 -070047 std::stringstream result;
Phil Burk39f02dd2017-08-04 09:13:31 -070048 int index = 0;
Andy Hung47c5e532017-06-26 18:28:00 -070049
Phil Burk4501b352017-06-29 18:12:36 -070050 result << "AAudioEndpointManager:" << "\n";
Phil Burk39f02dd2017-08-04 09:13:31 -070051
52 const bool isSharedLocked = AAudio_tryUntilTrue(
53 [this]()->bool { return mSharedLock.try_lock(); } /* f */,
54 50 /* times */,
55 20 /* sleepMs */);
56 if (!isSharedLocked) {
57 result << "AAudioEndpointManager Shared may be deadlocked\n";
Andy Hung47c5e532017-06-26 18:28:00 -070058 }
59
Phil Burk39f02dd2017-08-04 09:13:31 -070060 {
61 const bool isExclusiveLocked = AAudio_tryUntilTrue(
62 [this]() -> bool { return mExclusiveLock.try_lock(); } /* f */,
63 50 /* times */,
64 20 /* sleepMs */);
65 if (!isExclusiveLocked) {
66 result << "AAudioEndpointManager Exclusive may be deadlocked\n";
67 }
68
69 result << "Exclusive MMAP Endpoints: " << mExclusiveStreams.size() << "\n";
70 index = 0;
Phil Burk55e5eab2018-04-10 15:16:38 -070071 for (const auto &stream : mExclusiveStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -070072 result << " #" << index++ << ":";
Phil Burk55e5eab2018-04-10 15:16:38 -070073 result << stream->dump() << "\n";
Phil Burk39f02dd2017-08-04 09:13:31 -070074 }
75
Phil Burk55e5eab2018-04-10 15:16:38 -070076 result << " ExclusiveSearchCount: " << mExclusiveSearchCount << "\n";
77 result << " ExclusiveFoundCount: " << mExclusiveFoundCount << "\n";
78 result << " ExclusiveOpenCount: " << mExclusiveOpenCount << "\n";
79 result << " ExclusiveCloseCount: " << mExclusiveCloseCount << "\n";
Phil Burk6e463ce2020-04-13 10:20:20 -070080 result << " ExclusiveStolenCount: " << mExclusiveStolenCount << "\n";
Phil Burk55e5eab2018-04-10 15:16:38 -070081 result << "\n";
82
Phil Burk39f02dd2017-08-04 09:13:31 -070083 if (isExclusiveLocked) {
84 mExclusiveLock.unlock();
85 }
Andy Hung47c5e532017-06-26 18:28:00 -070086 }
87
Phil Burk39f02dd2017-08-04 09:13:31 -070088 result << "Shared Endpoints: " << mSharedStreams.size() << "\n";
89 index = 0;
Phil Burk55e5eab2018-04-10 15:16:38 -070090 for (const auto &stream : mSharedStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -070091 result << " #" << index++ << ":";
Phil Burk55e5eab2018-04-10 15:16:38 -070092 result << stream->dump() << "\n";
Phil Burk39f02dd2017-08-04 09:13:31 -070093 }
94
Phil Burk55e5eab2018-04-10 15:16:38 -070095 result << " SharedSearchCount: " << mSharedSearchCount << "\n";
96 result << " SharedFoundCount: " << mSharedFoundCount << "\n";
97 result << " SharedOpenCount: " << mSharedOpenCount << "\n";
98 result << " SharedCloseCount: " << mSharedCloseCount << "\n";
99 result << "\n";
100
Phil Burk39f02dd2017-08-04 09:13:31 -0700101 if (isSharedLocked) {
102 mSharedLock.unlock();
Andy Hung47c5e532017-06-26 18:28:00 -0700103 }
104 return result.str();
105}
106
Phil Burk39f02dd2017-08-04 09:13:31 -0700107
108// Try to find an existing endpoint.
109sp<AAudioServiceEndpoint> AAudioEndpointManager::findExclusiveEndpoint_l(
110 const AAudioStreamConfiguration &configuration) {
111 sp<AAudioServiceEndpoint> endpoint;
Phil Burk55e5eab2018-04-10 15:16:38 -0700112 mExclusiveSearchCount++;
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800113 for (const auto& ep : mExclusiveStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700114 if (ep->matches(configuration)) {
Phil Burk55e5eab2018-04-10 15:16:38 -0700115 mExclusiveFoundCount++;
Phil Burk39f02dd2017-08-04 09:13:31 -0700116 endpoint = ep;
117 break;
118 }
119 }
120
Phil Burk4e1af9f2018-01-03 15:54:35 -0800121 ALOGV("findExclusiveEndpoint_l(), found %p for device = %d, sessionId = %d",
122 endpoint.get(), configuration.getDeviceId(), configuration.getSessionId());
Phil Burk39f02dd2017-08-04 09:13:31 -0700123 return endpoint;
124}
125
126// Try to find an existing endpoint.
127sp<AAudioServiceEndpointShared> AAudioEndpointManager::findSharedEndpoint_l(
128 const AAudioStreamConfiguration &configuration) {
129 sp<AAudioServiceEndpointShared> endpoint;
Phil Burk55e5eab2018-04-10 15:16:38 -0700130 mSharedSearchCount++;
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800131 for (const auto& ep : mSharedStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700132 if (ep->matches(configuration)) {
Phil Burk55e5eab2018-04-10 15:16:38 -0700133 mSharedFoundCount++;
Phil Burk39f02dd2017-08-04 09:13:31 -0700134 endpoint = ep;
135 break;
136 }
137 }
138
Phil Burk4e1af9f2018-01-03 15:54:35 -0800139 ALOGV("findSharedEndpoint_l(), found %p for device = %d, sessionId = %d",
140 endpoint.get(), configuration.getDeviceId(), configuration.getSessionId());
Phil Burk39f02dd2017-08-04 09:13:31 -0700141 return endpoint;
142}
143
144sp<AAudioServiceEndpoint> AAudioEndpointManager::openEndpoint(AAudioService &audioService,
Phil Burk15f97c92018-09-04 14:06:27 -0700145 const aaudio::AAudioStreamRequest &request) {
146 if (request.getConstantConfiguration().getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burk6e463ce2020-04-13 10:20:20 -0700147 sp<AAudioServiceEndpoint> endpointToSteal;
148 sp<AAudioServiceEndpoint> foundEndpoint =
149 openExclusiveEndpoint(audioService, request, endpointToSteal);
150 if (endpointToSteal.get()) {
151 endpointToSteal->releaseRegisteredStreams(); // free the MMAP resource
152 }
153 return foundEndpoint;
Phil Burk39f02dd2017-08-04 09:13:31 -0700154 } else {
155 return openSharedEndpoint(audioService, request);
156 }
157}
158
159sp<AAudioServiceEndpoint> AAudioEndpointManager::openExclusiveEndpoint(
Phil Burkbbd52862018-04-13 11:37:42 -0700160 AAudioService &aaudioService,
Phil Burk6e463ce2020-04-13 10:20:20 -0700161 const aaudio::AAudioStreamRequest &request,
162 sp<AAudioServiceEndpoint> &endpointToSteal) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700163
164 std::lock_guard<std::mutex> lock(mExclusiveLock);
165
166 const AAudioStreamConfiguration &configuration = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -0700167
168 // Try to find an existing endpoint.
Phil Burk39f02dd2017-08-04 09:13:31 -0700169 sp<AAudioServiceEndpoint> endpoint = findExclusiveEndpoint_l(configuration);
Eric Laurenta17ae742017-06-29 15:43:55 -0700170
Phil Burk39f02dd2017-08-04 09:13:31 -0700171 // If we find an existing one then this one cannot be exclusive.
172 if (endpoint.get() != nullptr) {
Phil Burk6e463ce2020-04-13 10:20:20 -0700173 if (kStealingEnabled
174 && !endpoint->isForSharing() // not currently SHARED
175 && !request.isSharingModeMatchRequired()) { // app did not request a shared stream
176 ALOGD("%s() endpoint in EXCLUSIVE use. Steal it!", __func__);
177 mExclusiveStolenCount++;
Phil Burk836f9df2020-05-29 13:20:28 -0700178 // Prevent this process from getting another EXCLUSIVE stream.
179 // This will prevent two clients from colliding after a DISCONNECTION
180 // when they both try to open an exclusive stream at the same time.
181 // That can result in a stream getting disconnected between the OPEN
182 // and START calls. This will help preserve app compatibility.
183 // An app can avoid having this happen by closing their streams when
184 // the app is paused.
185 AAudioClientTracker::getInstance().setExclusiveEnabled(request.getProcessId(), false);
186 endpointToSteal = endpoint; // return it to caller
Phil Burk6e463ce2020-04-13 10:20:20 -0700187 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700188 return nullptr;
189 } else {
Phil Burkbbd52862018-04-13 11:37:42 -0700190 sp<AAudioServiceEndpointMMAP> endpointMMap = new AAudioServiceEndpointMMAP(aaudioService);
Phil Burk6e463ce2020-04-13 10:20:20 -0700191 ALOGV("%s(), no match so try to open MMAP %p for dev %d",
192 __func__, endpointMMap.get(), configuration.getDeviceId());
Phil Burk39f02dd2017-08-04 09:13:31 -0700193 endpoint = endpointMMap;
Eric Laurenta17ae742017-06-29 15:43:55 -0700194
Phil Burk39f02dd2017-08-04 09:13:31 -0700195 aaudio_result_t result = endpoint->open(request);
196 if (result != AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700197 endpoint.clear();
198 } else {
199 mExclusiveStreams.push_back(endpointMMap);
Phil Burk55e5eab2018-04-10 15:16:38 -0700200 mExclusiveOpenCount++;
Phil Burk11e8d332017-05-24 09:59:02 -0700201 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800202 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700203
Phil Burk39f02dd2017-08-04 09:13:31 -0700204 if (endpoint.get() != nullptr) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700205 // Increment the reference count under this lock.
Phil Burk39f02dd2017-08-04 09:13:31 -0700206 endpoint->setOpenCount(endpoint->getOpenCount() + 1);
Phil Burk6e463ce2020-04-13 10:20:20 -0700207 endpoint->setForSharing(request.isSharingModeMatchRequired());
Phil Burk71f35bb2017-04-13 16:05:07 -0700208 }
Phil Burk6e463ce2020-04-13 10:20:20 -0700209
Phil Burkc0c70e32017-02-09 13:18:38 -0800210 return endpoint;
211}
212
Phil Burk39f02dd2017-08-04 09:13:31 -0700213sp<AAudioServiceEndpoint> AAudioEndpointManager::openSharedEndpoint(
214 AAudioService &aaudioService,
215 const aaudio::AAudioStreamRequest &request) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700216
Phil Burk39f02dd2017-08-04 09:13:31 -0700217 std::lock_guard<std::mutex> lock(mSharedLock);
Phil Burkec89b2e2017-06-20 15:05:06 -0700218
Phil Burk39f02dd2017-08-04 09:13:31 -0700219 const AAudioStreamConfiguration &configuration = request.getConstantConfiguration();
220 aaudio_direction_t direction = configuration.getDirection();
Phil Burk71f35bb2017-04-13 16:05:07 -0700221
Phil Burk39f02dd2017-08-04 09:13:31 -0700222 // Try to find an existing endpoint.
223 sp<AAudioServiceEndpointShared> endpoint = findSharedEndpoint_l(configuration);
224
225 // If we can't find an existing one then open a new one.
226 if (endpoint.get() == nullptr) {
227 // we must call openStream with audioserver identity
228 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Phil Burk71f35bb2017-04-13 16:05:07 -0700229 switch (direction) {
230 case AAUDIO_DIRECTION_INPUT:
Phil Burk39f02dd2017-08-04 09:13:31 -0700231 endpoint = new AAudioServiceEndpointCapture(aaudioService);
Phil Burk71f35bb2017-04-13 16:05:07 -0700232 break;
233 case AAUDIO_DIRECTION_OUTPUT:
Phil Burk39f02dd2017-08-04 09:13:31 -0700234 endpoint = new AAudioServiceEndpointPlay(aaudioService);
Phil Burk71f35bb2017-04-13 16:05:07 -0700235 break;
Phil Burk11e8d332017-05-24 09:59:02 -0700236 default:
237 break;
Phil Burk71f35bb2017-04-13 16:05:07 -0700238 }
Phil Burk87c9f642017-05-17 07:22:39 -0700239
Phil Burk39f02dd2017-08-04 09:13:31 -0700240 if (endpoint.get() != nullptr) {
241 aaudio_result_t result = endpoint->open(request);
242 if (result != AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700243 endpoint.clear();
244 } else {
245 mSharedStreams.push_back(endpoint);
Phil Burk55e5eab2018-04-10 15:16:38 -0700246 mSharedOpenCount++;
Phil Burk39f02dd2017-08-04 09:13:31 -0700247 }
248 }
Phil Burk55e5eab2018-04-10 15:16:38 -0700249 ALOGV("%s(), created endpoint %p, requested device = %d, dir = %d",
Phil Burk19e990e2018-03-22 13:59:34 -0700250 __func__, endpoint.get(), configuration.getDeviceId(), (int)direction);
Phil Burk39f02dd2017-08-04 09:13:31 -0700251 IPCThreadState::self()->restoreCallingIdentity(token);
252 }
253
254 if (endpoint.get() != nullptr) {
255 // Increment the reference count under this lock.
256 endpoint->setOpenCount(endpoint->getOpenCount() + 1);
257 }
258 return endpoint;
259}
260
261void AAudioEndpointManager::closeEndpoint(sp<AAudioServiceEndpoint>serviceEndpoint) {
262 if (serviceEndpoint->getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) {
263 return closeExclusiveEndpoint(serviceEndpoint);
264 } else {
265 return closeSharedEndpoint(serviceEndpoint);
266 }
267}
268
269void AAudioEndpointManager::closeExclusiveEndpoint(sp<AAudioServiceEndpoint> serviceEndpoint) {
270 if (serviceEndpoint.get() == nullptr) {
271 return;
272 }
273
274 // Decrement the reference count under this lock.
275 std::lock_guard<std::mutex> lock(mExclusiveLock);
276 int32_t newRefCount = serviceEndpoint->getOpenCount() - 1;
277 serviceEndpoint->setOpenCount(newRefCount);
Phil Burk39f02dd2017-08-04 09:13:31 -0700278
Phil Burkfbf031e2017-10-12 15:58:31 -0700279 // If no longer in use then actually close it.
Phil Burk39f02dd2017-08-04 09:13:31 -0700280 if (newRefCount <= 0) {
281 mExclusiveStreams.erase(
282 std::remove(mExclusiveStreams.begin(), mExclusiveStreams.end(), serviceEndpoint),
283 mExclusiveStreams.end());
284
Phil Burk71f35bb2017-04-13 16:05:07 -0700285 serviceEndpoint->close();
Phil Burk55e5eab2018-04-10 15:16:38 -0700286 mExclusiveCloseCount++;
287 ALOGV("%s() %p for device %d",
Phil Burk19e990e2018-03-22 13:59:34 -0700288 __func__, serviceEndpoint.get(), serviceEndpoint->getDeviceId());
Phil Burk39f02dd2017-08-04 09:13:31 -0700289 }
290}
291
292void AAudioEndpointManager::closeSharedEndpoint(sp<AAudioServiceEndpoint> serviceEndpoint) {
293 if (serviceEndpoint.get() == nullptr) {
294 return;
295 }
296
297 // Decrement the reference count under this lock.
298 std::lock_guard<std::mutex> lock(mSharedLock);
299 int32_t newRefCount = serviceEndpoint->getOpenCount() - 1;
300 serviceEndpoint->setOpenCount(newRefCount);
Phil Burk39f02dd2017-08-04 09:13:31 -0700301
Phil Burkfbf031e2017-10-12 15:58:31 -0700302 // If no longer in use then actually close it.
Phil Burk39f02dd2017-08-04 09:13:31 -0700303 if (newRefCount <= 0) {
304 mSharedStreams.erase(
305 std::remove(mSharedStreams.begin(), mSharedStreams.end(), serviceEndpoint),
306 mSharedStreams.end());
307
308 serviceEndpoint->close();
Phil Burkdd582922020-10-15 20:29:51 +0000309
Phil Burk55e5eab2018-04-10 15:16:38 -0700310 mSharedCloseCount++;
Phil Burk8b4e05e2019-12-17 12:12:09 -0800311 ALOGV("%s(%p) closed for device %d",
Phil Burk19e990e2018-03-22 13:59:34 -0700312 __func__, serviceEndpoint.get(), serviceEndpoint->getDeviceId());
Phil Burk71f35bb2017-04-13 16:05:07 -0700313 }
314}