Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 1 | /* |
| 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 Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioEndpointManager" |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 21 | #include <assert.h> |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 22 | #include <functional> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 23 | #include <map> |
| 24 | #include <mutex> |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 25 | #include <sstream> |
| 26 | #include <utility/AAudioUtilities.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 27 | |
| 28 | #include "AAudioEndpointManager.h" |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 29 | #include "AAudioServiceEndpointShared.h" |
| 30 | #include "AAudioServiceEndpointMMAP.h" |
| 31 | #include "AAudioServiceEndpointCapture.h" |
| 32 | #include "AAudioServiceEndpointPlay.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 33 | |
| 34 | using namespace android; |
| 35 | using namespace aaudio; |
| 36 | |
| 37 | ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager); |
| 38 | |
| 39 | AAudioEndpointManager::AAudioEndpointManager() |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 40 | : Singleton<AAudioEndpointManager>() |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 41 | , mSharedStreams() |
| 42 | , mExclusiveStreams() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 45 | std::string AAudioEndpointManager::dump() const { |
| 46 | std::stringstream result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 47 | int index = 0; |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 48 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 49 | result << "AAudioEndpointManager:" << "\n"; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 50 | |
| 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 Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 59 | { |
| 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 Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 80 | 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 Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 89 | } |
| 90 | return result.str(); |
| 91 | } |
| 92 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 93 | |
| 94 | // Try to find an existing endpoint. |
| 95 | sp<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. |
| 111 | sp<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 | |
| 126 | sp<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 | |
| 136 | sp<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 Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 143 | |
| 144 | // Try to find an existing endpoint. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 145 | sp<AAudioServiceEndpoint> endpoint = findExclusiveEndpoint_l(configuration); |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 146 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 147 | // 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 Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 156 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 157 | 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 Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 163 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 164 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 165 | ALOGD("AAudioEndpointManager.openEndpoint(), created %p for device = %d", |
| 166 | endpoint.get(), configuration.getDeviceId()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 167 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 168 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 169 | if (endpoint.get() != nullptr) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 170 | // Increment the reference count under this lock. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 171 | endpoint->setOpenCount(endpoint->getOpenCount() + 1); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 172 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 173 | return endpoint; |
| 174 | } |
| 175 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 176 | sp<AAudioServiceEndpoint> AAudioEndpointManager::openSharedEndpoint( |
| 177 | AAudioService &aaudioService, |
| 178 | const aaudio::AAudioStreamRequest &request) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 179 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 180 | std::lock_guard<std::mutex> lock(mSharedLock); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 181 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 182 | const AAudioStreamConfiguration &configuration = request.getConstantConfiguration(); |
| 183 | aaudio_direction_t direction = configuration.getDirection(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 184 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 185 | // 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 Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 192 | switch (direction) { |
| 193 | case AAUDIO_DIRECTION_INPUT: |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 194 | endpoint = new AAudioServiceEndpointCapture(aaudioService); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 195 | break; |
| 196 | case AAUDIO_DIRECTION_OUTPUT: |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 197 | endpoint = new AAudioServiceEndpointPlay(aaudioService); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 198 | break; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 199 | default: |
| 200 | break; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 201 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 202 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 203 | 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 | |
| 224 | void 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 | |
| 232 | void 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 Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 250 | serviceEndpoint->close(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame^] | 251 | ALOGD("AAudioEndpointManager::closeExclusiveEndpoint() %p for device %d", |
| 252 | serviceEndpoint.get(), serviceEndpoint->getDeviceId()); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void 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 Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 277 | } |
| 278 | } |