blob: f41219ead9ca2021ee8cbf44b1fb1de53e4223c8 [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;
70 for (const auto &output : mExclusiveStreams) {
71 result << " #" << index++ << ":";
72 result << output->dump() << "\n";
73 }
74
75 if (isExclusiveLocked) {
76 mExclusiveLock.unlock();
77 }
Andy Hung47c5e532017-06-26 18:28:00 -070078 }
79
Phil Burk39f02dd2017-08-04 09:13:31 -070080 result << "Shared Endpoints: " << mSharedStreams.size() << "\n";
81 index = 0;
82 for (const auto &input : mSharedStreams) {
83 result << " #" << index++ << ":";
84 result << input->dump() << "\n";
85 }
86
87 if (isSharedLocked) {
88 mSharedLock.unlock();
Andy Hung47c5e532017-06-26 18:28:00 -070089 }
90 return result.str();
91}
92
Phil Burk39f02dd2017-08-04 09:13:31 -070093
94// Try to find an existing endpoint.
95sp<AAudioServiceEndpoint> AAudioEndpointManager::findExclusiveEndpoint_l(
96 const AAudioStreamConfiguration &configuration) {
97 sp<AAudioServiceEndpoint> endpoint;
98 for (const auto ep : mExclusiveStreams) {
99 if (ep->matches(configuration)) {
100 endpoint = ep;
101 break;
102 }
103 }
104
105 ALOGD("AAudioEndpointManager.findExclusiveEndpoint_l(), found %p for device = %d",
106 endpoint.get(), configuration.getDeviceId());
107 return endpoint;
108}
109
110// Try to find an existing endpoint.
111sp<AAudioServiceEndpointShared> AAudioEndpointManager::findSharedEndpoint_l(
112 const AAudioStreamConfiguration &configuration) {
113 sp<AAudioServiceEndpointShared> endpoint;
114 for (const auto ep : mSharedStreams) {
115 if (ep->matches(configuration)) {
116 endpoint = ep;
117 break;
118 }
119 }
120
121 ALOGD("AAudioEndpointManager.findSharedEndpoint_l(), found %p for device = %d",
122 endpoint.get(), configuration.getDeviceId());
123 return endpoint;
124}
125
126sp<AAudioServiceEndpoint> AAudioEndpointManager::openEndpoint(AAudioService &audioService,
127 const aaudio::AAudioStreamRequest &request,
128 aaudio_sharing_mode_t sharingMode) {
129 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
130 return openExclusiveEndpoint(audioService, request);
131 } else {
132 return openSharedEndpoint(audioService, request);
133 }
134}
135
136sp<AAudioServiceEndpoint> AAudioEndpointManager::openExclusiveEndpoint(
137 AAudioService &aaudioService __unused,
138 const aaudio::AAudioStreamRequest &request) {
139
140 std::lock_guard<std::mutex> lock(mExclusiveLock);
141
142 const AAudioStreamConfiguration &configuration = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -0700143
144 // Try to find an existing endpoint.
Phil Burk39f02dd2017-08-04 09:13:31 -0700145 sp<AAudioServiceEndpoint> endpoint = findExclusiveEndpoint_l(configuration);
Eric Laurenta17ae742017-06-29 15:43:55 -0700146
Phil Burk39f02dd2017-08-04 09:13:31 -0700147 // If we find an existing one then this one cannot be exclusive.
148 if (endpoint.get() != nullptr) {
149 ALOGE("AAudioEndpointManager.openExclusiveEndpoint() already in use");
150 // Already open so do not allow a second stream.
151 return nullptr;
152 } else {
153 sp<AAudioServiceEndpointMMAP> endpointMMap = new AAudioServiceEndpointMMAP();
154 ALOGE("AAudioEndpointManager.openEndpoint(),created MMAP %p", endpointMMap.get());
155 endpoint = endpointMMap;
Eric Laurenta17ae742017-06-29 15:43:55 -0700156
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 aaudio_result_t result = endpoint->open(request);
158 if (result != AAUDIO_OK) {
159 ALOGE("AAudioEndpointManager.openEndpoint(), open failed");
160 endpoint.clear();
161 } else {
162 mExclusiveStreams.push_back(endpointMMap);
Phil Burk11e8d332017-05-24 09:59:02 -0700163 }
Phil Burk11e8d332017-05-24 09:59:02 -0700164
Phil Burk39f02dd2017-08-04 09:13:31 -0700165 ALOGD("AAudioEndpointManager.openEndpoint(), created %p for device = %d",
166 endpoint.get(), configuration.getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800167 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700168
Phil Burk39f02dd2017-08-04 09:13:31 -0700169 if (endpoint.get() != nullptr) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700170 // Increment the reference count under this lock.
Phil Burk39f02dd2017-08-04 09:13:31 -0700171 endpoint->setOpenCount(endpoint->getOpenCount() + 1);
Phil Burk71f35bb2017-04-13 16:05:07 -0700172 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800173 return endpoint;
174}
175
Phil Burk39f02dd2017-08-04 09:13:31 -0700176sp<AAudioServiceEndpoint> AAudioEndpointManager::openSharedEndpoint(
177 AAudioService &aaudioService,
178 const aaudio::AAudioStreamRequest &request) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700179
Phil Burk39f02dd2017-08-04 09:13:31 -0700180 std::lock_guard<std::mutex> lock(mSharedLock);
Phil Burkec89b2e2017-06-20 15:05:06 -0700181
Phil Burk39f02dd2017-08-04 09:13:31 -0700182 const AAudioStreamConfiguration &configuration = request.getConstantConfiguration();
183 aaudio_direction_t direction = configuration.getDirection();
Phil Burk71f35bb2017-04-13 16:05:07 -0700184
Phil Burk39f02dd2017-08-04 09:13:31 -0700185 // Try to find an existing endpoint.
186 sp<AAudioServiceEndpointShared> endpoint = findSharedEndpoint_l(configuration);
187
188 // If we can't find an existing one then open a new one.
189 if (endpoint.get() == nullptr) {
190 // we must call openStream with audioserver identity
191 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Phil Burk71f35bb2017-04-13 16:05:07 -0700192 switch (direction) {
193 case AAUDIO_DIRECTION_INPUT:
Phil Burk39f02dd2017-08-04 09:13:31 -0700194 endpoint = new AAudioServiceEndpointCapture(aaudioService);
Phil Burk71f35bb2017-04-13 16:05:07 -0700195 break;
196 case AAUDIO_DIRECTION_OUTPUT:
Phil Burk39f02dd2017-08-04 09:13:31 -0700197 endpoint = new AAudioServiceEndpointPlay(aaudioService);
Phil Burk71f35bb2017-04-13 16:05:07 -0700198 break;
Phil Burk11e8d332017-05-24 09:59:02 -0700199 default:
200 break;
Phil Burk71f35bb2017-04-13 16:05:07 -0700201 }
Phil Burk87c9f642017-05-17 07:22:39 -0700202
Phil Burk39f02dd2017-08-04 09:13:31 -0700203 if (endpoint.get() != nullptr) {
204 aaudio_result_t result = endpoint->open(request);
205 if (result != AAUDIO_OK) {
206 ALOGE("AAudioEndpointManager.openEndpoint(), open failed");
207 endpoint.clear();
208 } else {
209 mSharedStreams.push_back(endpoint);
210 }
211 }
212 ALOGD("AAudioEndpointManager.openSharedEndpoint(), created %p for device = %d, dir = %d",
213 endpoint.get(), configuration.getDeviceId(), (int)direction);
214 IPCThreadState::self()->restoreCallingIdentity(token);
215 }
216
217 if (endpoint.get() != nullptr) {
218 // Increment the reference count under this lock.
219 endpoint->setOpenCount(endpoint->getOpenCount() + 1);
220 }
221 return endpoint;
222}
223
224void AAudioEndpointManager::closeEndpoint(sp<AAudioServiceEndpoint>serviceEndpoint) {
225 if (serviceEndpoint->getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) {
226 return closeExclusiveEndpoint(serviceEndpoint);
227 } else {
228 return closeSharedEndpoint(serviceEndpoint);
229 }
230}
231
232void AAudioEndpointManager::closeExclusiveEndpoint(sp<AAudioServiceEndpoint> serviceEndpoint) {
233 if (serviceEndpoint.get() == nullptr) {
234 return;
235 }
236
237 // Decrement the reference count under this lock.
238 std::lock_guard<std::mutex> lock(mExclusiveLock);
239 int32_t newRefCount = serviceEndpoint->getOpenCount() - 1;
240 serviceEndpoint->setOpenCount(newRefCount);
241 ALOGD("AAudioEndpointManager::closeExclusiveEndpoint(%p) newRefCount = %d",
242 serviceEndpoint.get(), newRefCount);
243
244 // If no longer in use then close and delete it.
245 if (newRefCount <= 0) {
246 mExclusiveStreams.erase(
247 std::remove(mExclusiveStreams.begin(), mExclusiveStreams.end(), serviceEndpoint),
248 mExclusiveStreams.end());
249
Phil Burk71f35bb2017-04-13 16:05:07 -0700250 serviceEndpoint->close();
Phil Burk39f02dd2017-08-04 09:13:31 -0700251 ALOGD("AAudioEndpointManager::closeExclusiveEndpoint() %p for device %d",
252 serviceEndpoint.get(), serviceEndpoint->getDeviceId());
253 }
254}
255
256void AAudioEndpointManager::closeSharedEndpoint(sp<AAudioServiceEndpoint> serviceEndpoint) {
257 if (serviceEndpoint.get() == nullptr) {
258 return;
259 }
260
261 // Decrement the reference count under this lock.
262 std::lock_guard<std::mutex> lock(mSharedLock);
263 int32_t newRefCount = serviceEndpoint->getOpenCount() - 1;
264 serviceEndpoint->setOpenCount(newRefCount);
265 ALOGD("AAudioEndpointManager::closeSharedEndpoint(%p) newRefCount = %d",
266 serviceEndpoint.get(), newRefCount);
267
268 // If no longer in use then close and delete it.
269 if (newRefCount <= 0) {
270 mSharedStreams.erase(
271 std::remove(mSharedStreams.begin(), mSharedStreams.end(), serviceEndpoint),
272 mSharedStreams.end());
273
274 serviceEndpoint->close();
275 ALOGD("AAudioEndpointManager::closeSharedEndpoint() %p for device %d",
276 serviceEndpoint.get(), serviceEndpoint->getDeviceId());
Phil Burk71f35bb2017-04-13 16:05:07 -0700277 }
278}