| 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> | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 27 | #include <media/AidlConversion.h> | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 28 |  | 
| Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 29 | #include "AAudioClientTracker.h" | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 30 | #include "AAudioEndpointManager.h" | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 31 | #include "AAudioServiceEndpointShared.h" | 
|  | 32 | #include "AAudioServiceEndpointMMAP.h" | 
|  | 33 | #include "AAudioServiceEndpointCapture.h" | 
|  | 34 | #include "AAudioServiceEndpointPlay.h" | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | using namespace android; | 
|  | 37 | using namespace aaudio; | 
|  | 38 |  | 
|  | 39 | ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager); | 
|  | 40 |  | 
|  | 41 | AAudioEndpointManager::AAudioEndpointManager() | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 42 | : Singleton<AAudioEndpointManager>() | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 43 | , mSharedStreams() | 
|  | 44 | , mExclusiveStreams() { | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 47 | std::string AAudioEndpointManager::dump() const NO_THREAD_SAFETY_ANALYSIS { | 
| Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 48 | std::stringstream result; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 49 | int index = 0; | 
| Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 50 |  | 
| Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 51 | result << "AAudioEndpointManager:" << "\n"; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 52 |  | 
|  | 53 | const bool isSharedLocked = AAudio_tryUntilTrue( | 
|  | 54 | [this]()->bool { return mSharedLock.try_lock(); } /* f */, | 
|  | 55 | 50 /* times */, | 
|  | 56 | 20 /* sleepMs */); | 
|  | 57 | if (!isSharedLocked) { | 
|  | 58 | result << "AAudioEndpointManager Shared may be deadlocked\n"; | 
| Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 61 | { | 
|  | 62 | const bool isExclusiveLocked = AAudio_tryUntilTrue( | 
|  | 63 | [this]() -> bool { return mExclusiveLock.try_lock(); } /* f */, | 
|  | 64 | 50 /* times */, | 
|  | 65 | 20 /* sleepMs */); | 
|  | 66 | if (!isExclusiveLocked) { | 
|  | 67 | result << "AAudioEndpointManager Exclusive may be deadlocked\n"; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | result << "Exclusive MMAP Endpoints: " << mExclusiveStreams.size() << "\n"; | 
|  | 71 | index = 0; | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 72 | for (const auto &stream : mExclusiveStreams) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 73 | result << "  #" << index++ << ":"; | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 74 | result << stream->dump() << "\n"; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 77 | result << "  ExclusiveSearchCount:  " << mExclusiveSearchCount << "\n"; | 
|  | 78 | result << "  ExclusiveFoundCount:   " << mExclusiveFoundCount << "\n"; | 
|  | 79 | result << "  ExclusiveOpenCount:    " << mExclusiveOpenCount << "\n"; | 
|  | 80 | result << "  ExclusiveCloseCount:   " << mExclusiveCloseCount << "\n"; | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 81 | result << "  ExclusiveStolenCount:  " << mExclusiveStolenCount << "\n"; | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 82 | result << "\n"; | 
|  | 83 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 84 | if (isExclusiveLocked) { | 
|  | 85 | mExclusiveLock.unlock(); | 
|  | 86 | } | 
| Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 89 | result << "Shared Endpoints: " << mSharedStreams.size() << "\n"; | 
|  | 90 | index = 0; | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 91 | for (const auto &stream : mSharedStreams) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 92 | result << "  #" << index++ << ":"; | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 93 | result << stream->dump() << "\n"; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 96 | result << "  SharedSearchCount:     " << mSharedSearchCount << "\n"; | 
|  | 97 | result << "  SharedFoundCount:      " << mSharedFoundCount << "\n"; | 
|  | 98 | result << "  SharedOpenCount:       " << mSharedOpenCount << "\n"; | 
|  | 99 | result << "  SharedCloseCount:      " << mSharedCloseCount << "\n"; | 
|  | 100 | result << "\n"; | 
|  | 101 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 102 | if (isSharedLocked) { | 
|  | 103 | mSharedLock.unlock(); | 
| Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 104 | } | 
|  | 105 | return result.str(); | 
|  | 106 | } | 
|  | 107 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 108 |  | 
|  | 109 | // Try to find an existing endpoint. | 
|  | 110 | sp<AAudioServiceEndpoint> AAudioEndpointManager::findExclusiveEndpoint_l( | 
|  | 111 | const AAudioStreamConfiguration &configuration) { | 
|  | 112 | sp<AAudioServiceEndpoint> endpoint; | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 113 | mExclusiveSearchCount++; | 
| Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 114 | for (const auto& ep : mExclusiveStreams) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 115 | if (ep->matches(configuration)) { | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 116 | mExclusiveFoundCount++; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 117 | endpoint = ep; | 
|  | 118 | break; | 
|  | 119 | } | 
|  | 120 | } | 
|  | 121 |  | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 122 | ALOGV("findExclusiveEndpoint_l(), found %p for device = %d, sessionId = %d", | 
|  | 123 | endpoint.get(), configuration.getDeviceId(), configuration.getSessionId()); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 124 | return endpoint; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | // Try to find an existing endpoint. | 
|  | 128 | sp<AAudioServiceEndpointShared> AAudioEndpointManager::findSharedEndpoint_l( | 
|  | 129 | const AAudioStreamConfiguration &configuration) { | 
|  | 130 | sp<AAudioServiceEndpointShared> endpoint; | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 131 | mSharedSearchCount++; | 
| Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 132 | for (const auto& ep  : mSharedStreams) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 133 | if (ep->matches(configuration)) { | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 134 | mSharedFoundCount++; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 135 | endpoint = ep; | 
|  | 136 | break; | 
|  | 137 | } | 
|  | 138 | } | 
|  | 139 |  | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 140 | ALOGV("findSharedEndpoint_l(), found %p for device = %d, sessionId = %d", | 
|  | 141 | endpoint.get(), configuration.getDeviceId(), configuration.getSessionId()); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 142 | return endpoint; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | sp<AAudioServiceEndpoint> AAudioEndpointManager::openEndpoint(AAudioService &audioService, | 
| Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 146 | const aaudio::AAudioStreamRequest &request) { | 
|  | 147 | if (request.getConstantConfiguration().getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) { | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 148 | sp<AAudioServiceEndpoint> endpointToSteal; | 
|  | 149 | sp<AAudioServiceEndpoint> foundEndpoint = | 
|  | 150 | openExclusiveEndpoint(audioService, request, endpointToSteal); | 
|  | 151 | if (endpointToSteal.get()) { | 
|  | 152 | endpointToSteal->releaseRegisteredStreams(); // free the MMAP resource | 
|  | 153 | } | 
|  | 154 | return foundEndpoint; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 155 | } else { | 
|  | 156 | return openSharedEndpoint(audioService, request); | 
|  | 157 | } | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | sp<AAudioServiceEndpoint> AAudioEndpointManager::openExclusiveEndpoint( | 
| Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 161 | AAudioService &aaudioService, | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 162 | const aaudio::AAudioStreamRequest &request, | 
|  | 163 | sp<AAudioServiceEndpoint> &endpointToSteal) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 164 |  | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 165 | const std::lock_guard<std::mutex> lock(mExclusiveLock); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 166 |  | 
|  | 167 | const AAudioStreamConfiguration &configuration = request.getConstantConfiguration(); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 168 |  | 
|  | 169 | // Try to find an existing endpoint. | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 170 | sp<AAudioServiceEndpoint> endpoint = findExclusiveEndpoint_l(configuration); | 
| Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 171 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 172 | // If we find an existing one then this one cannot be exclusive. | 
|  | 173 | if (endpoint.get() != nullptr) { | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 174 | if (kStealingEnabled | 
|  | 175 | && !endpoint->isForSharing() // not currently SHARED | 
|  | 176 | && !request.isSharingModeMatchRequired()) { // app did not request a shared stream | 
|  | 177 | ALOGD("%s() endpoint in EXCLUSIVE use. Steal it!", __func__); | 
|  | 178 | mExclusiveStolenCount++; | 
| Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 179 | // Prevent this process from getting another EXCLUSIVE stream. | 
|  | 180 | // This will prevent two clients from colliding after a DISCONNECTION | 
|  | 181 | // when they both try to open an exclusive stream at the same time. | 
|  | 182 | // That can result in a stream getting disconnected between the OPEN | 
|  | 183 | // and START calls. This will help preserve app compatibility. | 
|  | 184 | // An app can avoid having this happen by closing their streams when | 
|  | 185 | // the app is paused. | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 186 | const pid_t pid = VALUE_OR_FATAL( | 
| Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 187 | aidl2legacy_int32_t_pid_t(request.getAttributionSource().pid)); | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 188 | AAudioClientTracker::getInstance().setExclusiveEnabled(pid, false); | 
| Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 189 | endpointToSteal = endpoint; // return it to caller | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 190 | } | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 191 | return nullptr; | 
|  | 192 | } else { | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 193 | const sp<AAudioServiceEndpointMMAP> endpointMMap = | 
|  | 194 | new AAudioServiceEndpointMMAP(aaudioService); | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 195 | ALOGV("%s(), no match so try to open MMAP %p for dev %d", | 
|  | 196 | __func__, endpointMMap.get(), configuration.getDeviceId()); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 197 | endpoint = endpointMMap; | 
| Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 198 |  | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 199 | const aaudio_result_t result = endpoint->open(request); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 200 | if (result != AAUDIO_OK) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 201 | endpoint.clear(); | 
|  | 202 | } else { | 
|  | 203 | mExclusiveStreams.push_back(endpointMMap); | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 204 | mExclusiveOpenCount++; | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 205 | } | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 206 | } | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 207 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 208 | if (endpoint.get() != nullptr) { | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 209 | // Increment the reference count under this lock. | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 210 | endpoint->setOpenCount(endpoint->getOpenCount() + 1); | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 211 | endpoint->setForSharing(request.isSharingModeMatchRequired()); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 212 | } | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 213 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 214 | return endpoint; | 
|  | 215 | } | 
|  | 216 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 217 | sp<AAudioServiceEndpoint> AAudioEndpointManager::openSharedEndpoint( | 
|  | 218 | AAudioService &aaudioService, | 
|  | 219 | const aaudio::AAudioStreamRequest &request) { | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 220 |  | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 221 | const std::lock_guard<std::mutex> lock(mSharedLock); | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 222 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 223 | const AAudioStreamConfiguration &configuration = request.getConstantConfiguration(); | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 224 | const aaudio_direction_t direction = configuration.getDirection(); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 225 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 226 | // Try to find an existing endpoint. | 
|  | 227 | sp<AAudioServiceEndpointShared> endpoint = findSharedEndpoint_l(configuration); | 
|  | 228 |  | 
|  | 229 | // If we can't find an existing one then open a new one. | 
|  | 230 | if (endpoint.get() == nullptr) { | 
|  | 231 | // we must call openStream with audioserver identity | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 232 | const int64_t token = IPCThreadState::self()->clearCallingIdentity(); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 233 | switch (direction) { | 
|  | 234 | case AAUDIO_DIRECTION_INPUT: | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 235 | endpoint = new AAudioServiceEndpointCapture(aaudioService); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 236 | break; | 
|  | 237 | case AAUDIO_DIRECTION_OUTPUT: | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 238 | endpoint = new AAudioServiceEndpointPlay(aaudioService); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 239 | break; | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 240 | default: | 
|  | 241 | break; | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 242 | } | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 243 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 244 | if (endpoint.get() != nullptr) { | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 245 | const aaudio_result_t result = endpoint->open(request); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 246 | if (result != AAUDIO_OK) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 247 | endpoint.clear(); | 
|  | 248 | } else { | 
|  | 249 | mSharedStreams.push_back(endpoint); | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 250 | mSharedOpenCount++; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 251 | } | 
|  | 252 | } | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 253 | ALOGV("%s(), created endpoint %p, requested device = %d, dir = %d", | 
| Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 254 | __func__, endpoint.get(), configuration.getDeviceId(), (int)direction); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 255 | IPCThreadState::self()->restoreCallingIdentity(token); | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | if (endpoint.get() != nullptr) { | 
|  | 259 | // Increment the reference count under this lock. | 
|  | 260 | endpoint->setOpenCount(endpoint->getOpenCount() + 1); | 
|  | 261 | } | 
|  | 262 | return endpoint; | 
|  | 263 | } | 
|  | 264 |  | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 265 | void AAudioEndpointManager::closeEndpoint(const sp<AAudioServiceEndpoint>& serviceEndpoint) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 266 | if (serviceEndpoint->getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) { | 
|  | 267 | return closeExclusiveEndpoint(serviceEndpoint); | 
|  | 268 | } else { | 
|  | 269 | return closeSharedEndpoint(serviceEndpoint); | 
|  | 270 | } | 
|  | 271 | } | 
|  | 272 |  | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 273 | void AAudioEndpointManager::closeExclusiveEndpoint( | 
|  | 274 | const sp<AAudioServiceEndpoint>& serviceEndpoint) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 275 | if (serviceEndpoint.get() == nullptr) { | 
|  | 276 | return; | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | // Decrement the reference count under this lock. | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 280 | const std::lock_guard<std::mutex> lock(mExclusiveLock); | 
|  | 281 | const int32_t newRefCount = serviceEndpoint->getOpenCount() - 1; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 282 | serviceEndpoint->setOpenCount(newRefCount); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 283 |  | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 284 | // If no longer in use then actually close it. | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 285 | if (newRefCount <= 0) { | 
|  | 286 | mExclusiveStreams.erase( | 
|  | 287 | std::remove(mExclusiveStreams.begin(), mExclusiveStreams.end(), serviceEndpoint), | 
|  | 288 | mExclusiveStreams.end()); | 
|  | 289 |  | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 290 | serviceEndpoint->close(); | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 291 | mExclusiveCloseCount++; | 
|  | 292 | ALOGV("%s() %p for device %d", | 
| Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 293 | __func__, serviceEndpoint.get(), serviceEndpoint->getDeviceId()); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 294 | } | 
|  | 295 | } | 
|  | 296 |  | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 297 | void AAudioEndpointManager::closeSharedEndpoint(const sp<AAudioServiceEndpoint>& serviceEndpoint) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 298 | if (serviceEndpoint.get() == nullptr) { | 
|  | 299 | return; | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | // Decrement the reference count under this lock. | 
| jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 303 | const std::lock_guard<std::mutex> lock(mSharedLock); | 
|  | 304 | const int32_t newRefCount = serviceEndpoint->getOpenCount() - 1; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 305 | serviceEndpoint->setOpenCount(newRefCount); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 306 |  | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 307 | // If no longer in use then actually close it. | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 308 | if (newRefCount <= 0) { | 
|  | 309 | mSharedStreams.erase( | 
|  | 310 | std::remove(mSharedStreams.begin(), mSharedStreams.end(), serviceEndpoint), | 
|  | 311 | mSharedStreams.end()); | 
|  | 312 |  | 
|  | 313 | serviceEndpoint->close(); | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 314 |  | 
| Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 315 | mSharedCloseCount++; | 
| Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 316 | ALOGV("%s(%p) closed for device %d", | 
| Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 317 | __func__, serviceEndpoint.get(), serviceEndpoint->getDeviceId()); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 318 | } | 
|  | 319 | } |