blob: e0db26165688f7ea57b9af928f74094cbb605907 [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
28#include "AAudioEndpointManager.h"
Phil Burk39f02dd2017-08-04 09:13:31 -070029#include "AAudioServiceEndpointShared.h"
30#include "AAudioServiceEndpointMMAP.h"
31#include "AAudioServiceEndpointCapture.h"
32#include "AAudioServiceEndpointPlay.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080033
34using namespace android;
35using namespace aaudio;
36
37ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager);
38
39AAudioEndpointManager::AAudioEndpointManager()
Phil Burk71f35bb2017-04-13 16:05:07 -070040 : Singleton<AAudioEndpointManager>()
Phil Burk39f02dd2017-08-04 09:13:31 -070041 , mSharedStreams()
42 , mExclusiveStreams() {
Phil Burkc0c70e32017-02-09 13:18:38 -080043}
44
Andy Hung47c5e532017-06-26 18:28:00 -070045std::string AAudioEndpointManager::dump() const {
46 std::stringstream result;
Phil Burk39f02dd2017-08-04 09:13:31 -070047 int index = 0;
Andy Hung47c5e532017-06-26 18:28:00 -070048
Phil Burk4501b352017-06-29 18:12:36 -070049 result << "AAudioEndpointManager:" << "\n";
Phil Burk39f02dd2017-08-04 09:13:31 -070050
51 const bool isSharedLocked = AAudio_tryUntilTrue(
52 [this]()->bool { return mSharedLock.try_lock(); } /* f */,
53 50 /* times */,
54 20 /* sleepMs */);
55 if (!isSharedLocked) {
56 result << "AAudioEndpointManager Shared may be deadlocked\n";
Andy Hung47c5e532017-06-26 18:28:00 -070057 }
58
Phil Burk39f02dd2017-08-04 09:13:31 -070059 {
60 const bool isExclusiveLocked = AAudio_tryUntilTrue(
61 [this]() -> bool { return mExclusiveLock.try_lock(); } /* f */,
62 50 /* times */,
63 20 /* sleepMs */);
64 if (!isExclusiveLocked) {
65 result << "AAudioEndpointManager Exclusive may be deadlocked\n";
66 }
67
68 result << "Exclusive MMAP Endpoints: " << mExclusiveStreams.size() << "\n";
69 index = 0;
Phil Burk55e5eab2018-04-10 15:16:38 -070070 for (const auto &stream : mExclusiveStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -070071 result << " #" << index++ << ":";
Phil Burk55e5eab2018-04-10 15:16:38 -070072 result << stream->dump() << "\n";
Phil Burk39f02dd2017-08-04 09:13:31 -070073 }
74
Phil Burk55e5eab2018-04-10 15:16:38 -070075 result << " ExclusiveSearchCount: " << mExclusiveSearchCount << "\n";
76 result << " ExclusiveFoundCount: " << mExclusiveFoundCount << "\n";
77 result << " ExclusiveOpenCount: " << mExclusiveOpenCount << "\n";
78 result << " ExclusiveCloseCount: " << mExclusiveCloseCount << "\n";
79 result << "\n";
80
Phil Burk39f02dd2017-08-04 09:13:31 -070081 if (isExclusiveLocked) {
82 mExclusiveLock.unlock();
83 }
Andy Hung47c5e532017-06-26 18:28:00 -070084 }
85
Phil Burk39f02dd2017-08-04 09:13:31 -070086 result << "Shared Endpoints: " << mSharedStreams.size() << "\n";
87 index = 0;
Phil Burk55e5eab2018-04-10 15:16:38 -070088 for (const auto &stream : mSharedStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -070089 result << " #" << index++ << ":";
Phil Burk55e5eab2018-04-10 15:16:38 -070090 result << stream->dump() << "\n";
Phil Burk39f02dd2017-08-04 09:13:31 -070091 }
92
Phil Burk55e5eab2018-04-10 15:16:38 -070093 result << " SharedSearchCount: " << mSharedSearchCount << "\n";
94 result << " SharedFoundCount: " << mSharedFoundCount << "\n";
95 result << " SharedOpenCount: " << mSharedOpenCount << "\n";
96 result << " SharedCloseCount: " << mSharedCloseCount << "\n";
97 result << "\n";
98
Phil Burk39f02dd2017-08-04 09:13:31 -070099 if (isSharedLocked) {
100 mSharedLock.unlock();
Andy Hung47c5e532017-06-26 18:28:00 -0700101 }
102 return result.str();
103}
104
Phil Burk39f02dd2017-08-04 09:13:31 -0700105
106// Try to find an existing endpoint.
107sp<AAudioServiceEndpoint> AAudioEndpointManager::findExclusiveEndpoint_l(
108 const AAudioStreamConfiguration &configuration) {
109 sp<AAudioServiceEndpoint> endpoint;
Phil Burk55e5eab2018-04-10 15:16:38 -0700110 mExclusiveSearchCount++;
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800111 for (const auto& ep : mExclusiveStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700112 if (ep->matches(configuration)) {
Phil Burk55e5eab2018-04-10 15:16:38 -0700113 mExclusiveFoundCount++;
Phil Burk39f02dd2017-08-04 09:13:31 -0700114 endpoint = ep;
115 break;
116 }
117 }
118
Phil Burk4e1af9f2018-01-03 15:54:35 -0800119 ALOGV("findExclusiveEndpoint_l(), found %p for device = %d, sessionId = %d",
120 endpoint.get(), configuration.getDeviceId(), configuration.getSessionId());
Phil Burk39f02dd2017-08-04 09:13:31 -0700121 return endpoint;
122}
123
124// Try to find an existing endpoint.
125sp<AAudioServiceEndpointShared> AAudioEndpointManager::findSharedEndpoint_l(
126 const AAudioStreamConfiguration &configuration) {
127 sp<AAudioServiceEndpointShared> endpoint;
Phil Burk55e5eab2018-04-10 15:16:38 -0700128 mSharedSearchCount++;
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800129 for (const auto& ep : mSharedStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700130 if (ep->matches(configuration)) {
Phil Burk55e5eab2018-04-10 15:16:38 -0700131 mSharedFoundCount++;
Phil Burk39f02dd2017-08-04 09:13:31 -0700132 endpoint = ep;
133 break;
134 }
135 }
136
Phil Burk4e1af9f2018-01-03 15:54:35 -0800137 ALOGV("findSharedEndpoint_l(), found %p for device = %d, sessionId = %d",
138 endpoint.get(), configuration.getDeviceId(), configuration.getSessionId());
Phil Burk39f02dd2017-08-04 09:13:31 -0700139 return endpoint;
140}
141
142sp<AAudioServiceEndpoint> AAudioEndpointManager::openEndpoint(AAudioService &audioService,
143 const aaudio::AAudioStreamRequest &request,
144 aaudio_sharing_mode_t sharingMode) {
145 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
146 return openExclusiveEndpoint(audioService, request);
147 } else {
148 return openSharedEndpoint(audioService, request);
149 }
150}
151
152sp<AAudioServiceEndpoint> AAudioEndpointManager::openExclusiveEndpoint(
Phil Burkbbd52862018-04-13 11:37:42 -0700153 AAudioService &aaudioService,
Phil Burk39f02dd2017-08-04 09:13:31 -0700154 const aaudio::AAudioStreamRequest &request) {
155
156 std::lock_guard<std::mutex> lock(mExclusiveLock);
157
158 const AAudioStreamConfiguration &configuration = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -0700159
160 // Try to find an existing endpoint.
Phil Burk39f02dd2017-08-04 09:13:31 -0700161 sp<AAudioServiceEndpoint> endpoint = findExclusiveEndpoint_l(configuration);
Eric Laurenta17ae742017-06-29 15:43:55 -0700162
Phil Burk39f02dd2017-08-04 09:13:31 -0700163 // If we find an existing one then this one cannot be exclusive.
164 if (endpoint.get() != nullptr) {
Phil Burk55e5eab2018-04-10 15:16:38 -0700165 ALOGW("openExclusiveEndpoint() already in use");
Phil Burk39f02dd2017-08-04 09:13:31 -0700166 // Already open so do not allow a second stream.
167 return nullptr;
168 } else {
Phil Burkbbd52862018-04-13 11:37:42 -0700169 sp<AAudioServiceEndpointMMAP> endpointMMap = new AAudioServiceEndpointMMAP(aaudioService);
Phil Burk55e5eab2018-04-10 15:16:38 -0700170 ALOGV("openExclusiveEndpoint(), no match so try to open MMAP %p for dev %d",
Phil Burk4e1af9f2018-01-03 15:54:35 -0800171 endpointMMap.get(), configuration.getDeviceId());
Phil Burk39f02dd2017-08-04 09:13:31 -0700172 endpoint = endpointMMap;
Eric Laurenta17ae742017-06-29 15:43:55 -0700173
Phil Burk39f02dd2017-08-04 09:13:31 -0700174 aaudio_result_t result = endpoint->open(request);
175 if (result != AAUDIO_OK) {
Phil Burkbbd52862018-04-13 11:37:42 -0700176 ALOGE("openExclusiveEndpoint(), open failed");
Phil Burk39f02dd2017-08-04 09:13:31 -0700177 endpoint.clear();
178 } else {
179 mExclusiveStreams.push_back(endpointMMap);
Phil Burk55e5eab2018-04-10 15:16:38 -0700180 mExclusiveOpenCount++;
Phil Burk11e8d332017-05-24 09:59:02 -0700181 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800182 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700183
Phil Burk39f02dd2017-08-04 09:13:31 -0700184 if (endpoint.get() != nullptr) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700185 // Increment the reference count under this lock.
Phil Burk39f02dd2017-08-04 09:13:31 -0700186 endpoint->setOpenCount(endpoint->getOpenCount() + 1);
Phil Burk71f35bb2017-04-13 16:05:07 -0700187 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800188 return endpoint;
189}
190
Phil Burk39f02dd2017-08-04 09:13:31 -0700191sp<AAudioServiceEndpoint> AAudioEndpointManager::openSharedEndpoint(
192 AAudioService &aaudioService,
193 const aaudio::AAudioStreamRequest &request) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700194
Phil Burk39f02dd2017-08-04 09:13:31 -0700195 std::lock_guard<std::mutex> lock(mSharedLock);
Phil Burkec89b2e2017-06-20 15:05:06 -0700196
Phil Burk39f02dd2017-08-04 09:13:31 -0700197 const AAudioStreamConfiguration &configuration = request.getConstantConfiguration();
198 aaudio_direction_t direction = configuration.getDirection();
Phil Burk71f35bb2017-04-13 16:05:07 -0700199
Phil Burk39f02dd2017-08-04 09:13:31 -0700200 // Try to find an existing endpoint.
201 sp<AAudioServiceEndpointShared> endpoint = findSharedEndpoint_l(configuration);
202
203 // If we can't find an existing one then open a new one.
204 if (endpoint.get() == nullptr) {
205 // we must call openStream with audioserver identity
206 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Phil Burk71f35bb2017-04-13 16:05:07 -0700207 switch (direction) {
208 case AAUDIO_DIRECTION_INPUT:
Phil Burk39f02dd2017-08-04 09:13:31 -0700209 endpoint = new AAudioServiceEndpointCapture(aaudioService);
Phil Burk71f35bb2017-04-13 16:05:07 -0700210 break;
211 case AAUDIO_DIRECTION_OUTPUT:
Phil Burk39f02dd2017-08-04 09:13:31 -0700212 endpoint = new AAudioServiceEndpointPlay(aaudioService);
Phil Burk71f35bb2017-04-13 16:05:07 -0700213 break;
Phil Burk11e8d332017-05-24 09:59:02 -0700214 default:
215 break;
Phil Burk71f35bb2017-04-13 16:05:07 -0700216 }
Phil Burk87c9f642017-05-17 07:22:39 -0700217
Phil Burk39f02dd2017-08-04 09:13:31 -0700218 if (endpoint.get() != nullptr) {
219 aaudio_result_t result = endpoint->open(request);
220 if (result != AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700221 endpoint.clear();
222 } else {
223 mSharedStreams.push_back(endpoint);
Phil Burk55e5eab2018-04-10 15:16:38 -0700224 mSharedOpenCount++;
Phil Burk39f02dd2017-08-04 09:13:31 -0700225 }
226 }
Phil Burk55e5eab2018-04-10 15:16:38 -0700227 ALOGV("%s(), created endpoint %p, requested device = %d, dir = %d",
Phil Burk19e990e2018-03-22 13:59:34 -0700228 __func__, endpoint.get(), configuration.getDeviceId(), (int)direction);
Phil Burk39f02dd2017-08-04 09:13:31 -0700229 IPCThreadState::self()->restoreCallingIdentity(token);
230 }
231
232 if (endpoint.get() != nullptr) {
233 // Increment the reference count under this lock.
234 endpoint->setOpenCount(endpoint->getOpenCount() + 1);
235 }
236 return endpoint;
237}
238
239void AAudioEndpointManager::closeEndpoint(sp<AAudioServiceEndpoint>serviceEndpoint) {
240 if (serviceEndpoint->getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) {
241 return closeExclusiveEndpoint(serviceEndpoint);
242 } else {
243 return closeSharedEndpoint(serviceEndpoint);
244 }
245}
246
247void AAudioEndpointManager::closeExclusiveEndpoint(sp<AAudioServiceEndpoint> serviceEndpoint) {
248 if (serviceEndpoint.get() == nullptr) {
249 return;
250 }
251
252 // Decrement the reference count under this lock.
253 std::lock_guard<std::mutex> lock(mExclusiveLock);
254 int32_t newRefCount = serviceEndpoint->getOpenCount() - 1;
255 serviceEndpoint->setOpenCount(newRefCount);
Phil Burk39f02dd2017-08-04 09:13:31 -0700256
Phil Burkfbf031e2017-10-12 15:58:31 -0700257 // If no longer in use then actually close it.
Phil Burk39f02dd2017-08-04 09:13:31 -0700258 if (newRefCount <= 0) {
259 mExclusiveStreams.erase(
260 std::remove(mExclusiveStreams.begin(), mExclusiveStreams.end(), serviceEndpoint),
261 mExclusiveStreams.end());
262
Phil Burk71f35bb2017-04-13 16:05:07 -0700263 serviceEndpoint->close();
Phil Burk55e5eab2018-04-10 15:16:38 -0700264 mExclusiveCloseCount++;
265 ALOGV("%s() %p for device %d",
Phil Burk19e990e2018-03-22 13:59:34 -0700266 __func__, serviceEndpoint.get(), serviceEndpoint->getDeviceId());
Phil Burk39f02dd2017-08-04 09:13:31 -0700267 }
268}
269
270void AAudioEndpointManager::closeSharedEndpoint(sp<AAudioServiceEndpoint> serviceEndpoint) {
271 if (serviceEndpoint.get() == nullptr) {
272 return;
273 }
274
275 // Decrement the reference count under this lock.
276 std::lock_guard<std::mutex> lock(mSharedLock);
277 int32_t newRefCount = serviceEndpoint->getOpenCount() - 1;
278 serviceEndpoint->setOpenCount(newRefCount);
Phil Burk39f02dd2017-08-04 09:13:31 -0700279
Phil Burkfbf031e2017-10-12 15:58:31 -0700280 // If no longer in use then actually close it.
Phil Burk39f02dd2017-08-04 09:13:31 -0700281 if (newRefCount <= 0) {
282 mSharedStreams.erase(
283 std::remove(mSharedStreams.begin(), mSharedStreams.end(), serviceEndpoint),
284 mSharedStreams.end());
285
286 serviceEndpoint->close();
Phil Burk55e5eab2018-04-10 15:16:38 -0700287 mSharedCloseCount++;
288 ALOGV("%s() %p for device %d",
Phil Burk19e990e2018-03-22 13:59:34 -0700289 __func__, serviceEndpoint.get(), serviceEndpoint->getDeviceId());
Phil Burk71f35bb2017-04-13 16:05:07 -0700290 }
291}