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 | |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 28 | #include "AAudioClientTracker.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 29 | #include "AAudioEndpointManager.h" |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 30 | #include "AAudioServiceEndpointShared.h" |
| 31 | #include "AAudioServiceEndpointMMAP.h" |
| 32 | #include "AAudioServiceEndpointCapture.h" |
| 33 | #include "AAudioServiceEndpointPlay.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 34 | |
| 35 | using namespace android; |
| 36 | using namespace aaudio; |
| 37 | |
| 38 | ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager); |
| 39 | |
| 40 | AAudioEndpointManager::AAudioEndpointManager() |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 41 | : Singleton<AAudioEndpointManager>() |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 42 | , mSharedStreams() |
| 43 | , mExclusiveStreams() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 46 | std::string AAudioEndpointManager::dump() const { |
| 47 | std::stringstream result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 48 | int index = 0; |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 49 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 50 | result << "AAudioEndpointManager:" << "\n"; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 51 | |
| 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 Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 60 | { |
| 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 Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 71 | for (const auto &stream : mExclusiveStreams) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 72 | result << " #" << index++ << ":"; |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 73 | result << stream->dump() << "\n"; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 76 | result << " ExclusiveSearchCount: " << mExclusiveSearchCount << "\n"; |
| 77 | result << " ExclusiveFoundCount: " << mExclusiveFoundCount << "\n"; |
| 78 | result << " ExclusiveOpenCount: " << mExclusiveOpenCount << "\n"; |
| 79 | result << " ExclusiveCloseCount: " << mExclusiveCloseCount << "\n"; |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 80 | result << " ExclusiveStolenCount: " << mExclusiveStolenCount << "\n"; |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 81 | result << "\n"; |
| 82 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 83 | if (isExclusiveLocked) { |
| 84 | mExclusiveLock.unlock(); |
| 85 | } |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 88 | result << "Shared Endpoints: " << mSharedStreams.size() << "\n"; |
| 89 | index = 0; |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 90 | for (const auto &stream : mSharedStreams) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 91 | result << " #" << index++ << ":"; |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 92 | result << stream->dump() << "\n"; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 95 | result << " SharedSearchCount: " << mSharedSearchCount << "\n"; |
| 96 | result << " SharedFoundCount: " << mSharedFoundCount << "\n"; |
| 97 | result << " SharedOpenCount: " << mSharedOpenCount << "\n"; |
| 98 | result << " SharedCloseCount: " << mSharedCloseCount << "\n"; |
| 99 | result << "\n"; |
| 100 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 101 | if (isSharedLocked) { |
| 102 | mSharedLock.unlock(); |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 103 | } |
| 104 | return result.str(); |
| 105 | } |
| 106 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 107 | |
| 108 | // Try to find an existing endpoint. |
| 109 | sp<AAudioServiceEndpoint> AAudioEndpointManager::findExclusiveEndpoint_l( |
| 110 | const AAudioStreamConfiguration &configuration) { |
| 111 | sp<AAudioServiceEndpoint> endpoint; |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 112 | mExclusiveSearchCount++; |
Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 113 | for (const auto& ep : mExclusiveStreams) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 114 | if (ep->matches(configuration)) { |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 115 | mExclusiveFoundCount++; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 116 | endpoint = ep; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 121 | ALOGV("findExclusiveEndpoint_l(), found %p for device = %d, sessionId = %d", |
| 122 | endpoint.get(), configuration.getDeviceId(), configuration.getSessionId()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 123 | return endpoint; |
| 124 | } |
| 125 | |
| 126 | // Try to find an existing endpoint. |
| 127 | sp<AAudioServiceEndpointShared> AAudioEndpointManager::findSharedEndpoint_l( |
| 128 | const AAudioStreamConfiguration &configuration) { |
| 129 | sp<AAudioServiceEndpointShared> endpoint; |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 130 | mSharedSearchCount++; |
Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 131 | for (const auto& ep : mSharedStreams) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 132 | if (ep->matches(configuration)) { |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 133 | mSharedFoundCount++; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 134 | endpoint = ep; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 139 | ALOGV("findSharedEndpoint_l(), found %p for device = %d, sessionId = %d", |
| 140 | endpoint.get(), configuration.getDeviceId(), configuration.getSessionId()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 141 | return endpoint; |
| 142 | } |
| 143 | |
| 144 | sp<AAudioServiceEndpoint> AAudioEndpointManager::openEndpoint(AAudioService &audioService, |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 145 | const aaudio::AAudioStreamRequest &request) { |
| 146 | if (request.getConstantConfiguration().getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 147 | 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 Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 154 | } else { |
| 155 | return openSharedEndpoint(audioService, request); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | sp<AAudioServiceEndpoint> AAudioEndpointManager::openExclusiveEndpoint( |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 160 | AAudioService &aaudioService, |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 161 | const aaudio::AAudioStreamRequest &request, |
| 162 | sp<AAudioServiceEndpoint> &endpointToSteal) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 163 | |
| 164 | std::lock_guard<std::mutex> lock(mExclusiveLock); |
| 165 | |
| 166 | const AAudioStreamConfiguration &configuration = request.getConstantConfiguration(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 167 | |
| 168 | // Try to find an existing endpoint. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 169 | sp<AAudioServiceEndpoint> endpoint = findExclusiveEndpoint_l(configuration); |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 170 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 171 | // If we find an existing one then this one cannot be exclusive. |
| 172 | if (endpoint.get() != nullptr) { |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 173 | 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 Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 178 | // 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 Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 187 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 188 | return nullptr; |
| 189 | } else { |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 190 | sp<AAudioServiceEndpointMMAP> endpointMMap = new AAudioServiceEndpointMMAP(aaudioService); |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 191 | ALOGV("%s(), no match so try to open MMAP %p for dev %d", |
| 192 | __func__, endpointMMap.get(), configuration.getDeviceId()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 193 | endpoint = endpointMMap; |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 194 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 195 | aaudio_result_t result = endpoint->open(request); |
| 196 | if (result != AAUDIO_OK) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 197 | endpoint.clear(); |
| 198 | } else { |
| 199 | mExclusiveStreams.push_back(endpointMMap); |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 200 | mExclusiveOpenCount++; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 201 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 202 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 203 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 204 | if (endpoint.get() != nullptr) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 205 | // Increment the reference count under this lock. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 206 | endpoint->setOpenCount(endpoint->getOpenCount() + 1); |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 207 | endpoint->setForSharing(request.isSharingModeMatchRequired()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 208 | } |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 209 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 210 | return endpoint; |
| 211 | } |
| 212 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 213 | sp<AAudioServiceEndpoint> AAudioEndpointManager::openSharedEndpoint( |
| 214 | AAudioService &aaudioService, |
| 215 | const aaudio::AAudioStreamRequest &request) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 216 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 217 | std::lock_guard<std::mutex> lock(mSharedLock); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 218 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 219 | const AAudioStreamConfiguration &configuration = request.getConstantConfiguration(); |
| 220 | aaudio_direction_t direction = configuration.getDirection(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 221 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 222 | // 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 Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 229 | switch (direction) { |
| 230 | case AAUDIO_DIRECTION_INPUT: |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 231 | endpoint = new AAudioServiceEndpointCapture(aaudioService); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 232 | break; |
| 233 | case AAUDIO_DIRECTION_OUTPUT: |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 234 | endpoint = new AAudioServiceEndpointPlay(aaudioService); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 235 | break; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 236 | default: |
| 237 | break; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 238 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 239 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 240 | if (endpoint.get() != nullptr) { |
| 241 | aaudio_result_t result = endpoint->open(request); |
| 242 | if (result != AAUDIO_OK) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 243 | endpoint.clear(); |
| 244 | } else { |
| 245 | mSharedStreams.push_back(endpoint); |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 246 | mSharedOpenCount++; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 249 | ALOGV("%s(), created endpoint %p, requested device = %d, dir = %d", |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 250 | __func__, endpoint.get(), configuration.getDeviceId(), (int)direction); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 251 | 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 | |
| 261 | void 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 | |
| 269 | void 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 Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 278 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 279 | // If no longer in use then actually close it. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 280 | if (newRefCount <= 0) { |
| 281 | mExclusiveStreams.erase( |
| 282 | std::remove(mExclusiveStreams.begin(), mExclusiveStreams.end(), serviceEndpoint), |
| 283 | mExclusiveStreams.end()); |
| 284 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 285 | serviceEndpoint->close(); |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 286 | mExclusiveCloseCount++; |
| 287 | ALOGV("%s() %p for device %d", |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 288 | __func__, serviceEndpoint.get(), serviceEndpoint->getDeviceId()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | void 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 Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 301 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 302 | // If no longer in use then actually close it. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 303 | if (newRefCount <= 0) { |
| 304 | mSharedStreams.erase( |
| 305 | std::remove(mSharedStreams.begin(), mSharedStreams.end(), serviceEndpoint), |
| 306 | mSharedStreams.end()); |
| 307 | |
| 308 | serviceEndpoint->close(); |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 309 | mSharedCloseCount++; |
Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 310 | ALOGV("%s(%p) closed for device %d", |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 311 | __func__, serviceEndpoint.get(), serviceEndpoint->getDeviceId()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 312 | } |
| 313 | } |