Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [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 | |
| 17 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 18 | #define LOG_TAG "AAudioClientTracker" |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <assert.h> |
| 23 | #include <binder/IPCThreadState.h> |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 24 | #include <iomanip> |
| 25 | #include <iostream> |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 26 | #include <map> |
| 27 | #include <mutex> |
| 28 | #include <utils/Singleton.h> |
| 29 | |
| 30 | #include "utility/AAudioUtilities.h" |
| 31 | #include "AAudioEndpointManager.h" |
| 32 | #include "AAudioServiceEndpoint.h" |
| 33 | #include "AAudioClientTracker.h" |
| 34 | |
| 35 | using namespace android; |
| 36 | using namespace aaudio; |
| 37 | |
| 38 | ANDROID_SINGLETON_STATIC_INSTANCE(AAudioClientTracker); |
| 39 | |
| 40 | AAudioClientTracker::AAudioClientTracker() |
| 41 | : Singleton<AAudioClientTracker>() { |
| 42 | } |
| 43 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 44 | std::string AAudioClientTracker::dump() const NO_THREAD_SAFETY_ANALYSIS { |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 45 | std::stringstream result; |
| 46 | const bool isLocked = AAudio_tryUntilTrue( |
| 47 | [this]()->bool { return mLock.try_lock(); } /* f */, |
| 48 | 50 /* times */, |
| 49 | 20 /* sleepMs */); |
| 50 | if (!isLocked) { |
| 51 | result << "AAudioClientTracker may be deadlocked\n"; |
| 52 | } |
| 53 | |
| 54 | result << "AAudioClientTracker:\n"; |
| 55 | for (const auto& it : mNotificationClients) { |
| 56 | result << it.second->dump(); |
| 57 | } |
| 58 | |
| 59 | if (isLocked) { |
| 60 | mLock.unlock(); |
| 61 | } |
| 62 | return result.str(); |
| 63 | } |
| 64 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 65 | // Create a tracker for the client. |
| 66 | aaudio_result_t AAudioClientTracker::registerClient(pid_t pid, |
| 67 | const sp<IAAudioClient>& client) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 68 | ALOGV("registerClient(), calling pid = %d, getpid() = %d\n", pid, getpid()); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 69 | |
Phil Burk | ef7eaaf | 2019-05-01 11:26:35 -0700 | [diff] [blame] | 70 | if (client.get() == nullptr) { |
| 71 | ALOGE("AAudioClientTracker::%s() client is NULL!", __func__); |
| 72 | android_errorWriteLog(0x534e4554, "116230453"); |
| 73 | return AAUDIO_ERROR_NULL; |
| 74 | } |
| 75 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 76 | const std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 77 | if (mNotificationClients.count(pid) == 0) { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 78 | const sp<IBinder> binder = IInterface::asBinder(client); |
| 79 | const sp<NotificationClient> notificationClient = new NotificationClient(pid, binder); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 80 | mNotificationClients[pid] = notificationClient; |
| 81 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 82 | const status_t status = binder->linkToDeath(notificationClient); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 83 | ALOGW_IF(status != NO_ERROR, "registerClient() linkToDeath = %d\n", status); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 84 | return AAudioConvert_androidToAAudioResult(status); |
| 85 | } else { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 86 | ALOGW("registerClient(%d) already registered!", pid); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 87 | return AAUDIO_OK; // TODO should this be considered an error |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void AAudioClientTracker::unregisterClient(pid_t pid) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 92 | ALOGV("unregisterClient(), calling pid = %d, getpid() = %d\n", pid, getpid()); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 93 | const std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 94 | mNotificationClients.erase(pid); |
| 95 | } |
| 96 | |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 97 | int32_t AAudioClientTracker::getStreamCount(pid_t pid) { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 98 | const std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 99 | auto it = mNotificationClients.find(pid); |
| 100 | if (it != mNotificationClients.end()) { |
| 101 | return it->second->getStreamCount(); |
| 102 | } else { |
| 103 | return 0; // no existing client |
| 104 | } |
| 105 | } |
| 106 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 107 | aaudio_result_t |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 108 | AAudioClientTracker::registerClientStream( |
| 109 | pid_t pid, const sp<AAudioServiceStreamBase>& serviceStream) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 110 | ALOGV("registerClientStream(%d,)\n", pid); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 111 | const std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 112 | return getNotificationClient_l(pid)->registerClientStream(serviceStream); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Find the tracker for this process and remove it. |
| 116 | aaudio_result_t |
| 117 | AAudioClientTracker::unregisterClientStream(pid_t pid, |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 118 | const sp<AAudioServiceStreamBase>& serviceStream) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 119 | ALOGV("unregisterClientStream(%d,)\n", pid); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 120 | const std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 121 | auto it = mNotificationClients.find(pid); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 122 | if (it != mNotificationClients.end()) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 123 | ALOGV("unregisterClientStream(%d,) found NotificationClient\n", pid); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 124 | it->second->unregisterClientStream(serviceStream); |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 125 | } else { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 126 | ALOGE("unregisterClientStream(%d,) missing NotificationClient\n", pid); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 127 | } |
| 128 | return AAUDIO_OK; |
| 129 | } |
| 130 | |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 131 | void AAudioClientTracker::setExclusiveEnabled(pid_t pid, bool enabled) { |
| 132 | ALOGD("%s(%d, %d)\n", __func__, pid, enabled); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 133 | const std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 134 | getNotificationClient_l(pid)->setExclusiveEnabled(enabled); |
| 135 | } |
| 136 | |
| 137 | bool AAudioClientTracker::isExclusiveEnabled(pid_t pid) { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 138 | const std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 139 | return getNotificationClient_l(pid)->isExclusiveEnabled(); |
| 140 | } |
| 141 | |
| 142 | sp<AAudioClientTracker::NotificationClient> |
| 143 | AAudioClientTracker::getNotificationClient_l(pid_t pid) { |
| 144 | sp<NotificationClient> notificationClient = mNotificationClients[pid]; |
| 145 | if (notificationClient == nullptr) { |
| 146 | // This will get called the first time the audio server uses this PID. |
| 147 | ALOGV("%s(%d,) unrecognized PID\n", __func__, pid); |
| 148 | notificationClient = new AAudioClientTracker::NotificationClient(pid, nullptr); |
| 149 | mNotificationClients[pid] = notificationClient; |
| 150 | } |
| 151 | return notificationClient; |
| 152 | } |
| 153 | |
| 154 | // ======================================= |
| 155 | // AAudioClientTracker::NotificationClient |
| 156 | // ======================================= |
| 157 | |
Steven Moreland | 37a6163 | 2019-12-04 14:06:50 -0800 | [diff] [blame] | 158 | AAudioClientTracker::NotificationClient::NotificationClient(pid_t pid, const sp<IBinder>& binder) |
| 159 | : mProcessId(pid), mBinder(binder) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 162 | int32_t AAudioClientTracker::NotificationClient::getStreamCount() { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 163 | const std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 164 | return mStreams.size(); |
| 165 | } |
| 166 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 167 | aaudio_result_t AAudioClientTracker::NotificationClient::registerClientStream( |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 168 | const sp<AAudioServiceStreamBase>& serviceStream) { |
| 169 | const std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 170 | mStreams.insert(serviceStream); |
| 171 | return AAUDIO_OK; |
| 172 | } |
| 173 | |
| 174 | aaudio_result_t AAudioClientTracker::NotificationClient::unregisterClientStream( |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 175 | const sp<AAudioServiceStreamBase>& serviceStream) { |
| 176 | const std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 177 | mStreams.erase(serviceStream); |
| 178 | return AAUDIO_OK; |
| 179 | } |
| 180 | |
| 181 | // Close any open streams for the client. |
| 182 | void AAudioClientTracker::NotificationClient::binderDied(const wp<IBinder>& who __unused) { |
| 183 | AAudioService *aaudioService = AAudioClientTracker::getInstance().getAAudioService(); |
| 184 | if (aaudioService != nullptr) { |
| 185 | // Copy the current list of streams to another vector because closing them below |
| 186 | // will cause unregisterClientStream() calls back to this object. |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 187 | std::set<sp<AAudioServiceStreamBase>> streamsToClose; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 188 | |
| 189 | { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 190 | const std::lock_guard<std::mutex> lock(mLock); |
Chih-Hung Hsieh | 48fc619 | 2017-08-04 14:37:31 -0700 | [diff] [blame] | 191 | for (const auto& serviceStream : mStreams) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 192 | streamsToClose.insert(serviceStream); |
| 193 | } |
| 194 | } |
| 195 | |
Chih-Hung Hsieh | 48fc619 | 2017-08-04 14:37:31 -0700 | [diff] [blame] | 196 | for (const auto& serviceStream : streamsToClose) { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 197 | const aaudio_handle_t handle = serviceStream->getHandle(); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 198 | ALOGW("binderDied() close abandoned stream 0x%08X\n", handle); |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 199 | AAudioHandleInfo handleInfo(DEFAULT_AAUDIO_SERVICE_ID, handle); |
| 200 | aaudioService->asAAudioServiceInterface().closeStream(handleInfo); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 201 | } |
| 202 | // mStreams should be empty now |
| 203 | } |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 204 | const sp<NotificationClient> keep(this); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 205 | AAudioClientTracker::getInstance().unregisterClient(mProcessId); |
| 206 | } |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 207 | |
| 208 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 209 | std::string AAudioClientTracker::NotificationClient::dump() const NO_THREAD_SAFETY_ANALYSIS { |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 210 | std::stringstream result; |
| 211 | const bool isLocked = AAudio_tryUntilTrue( |
| 212 | [this]()->bool { return mLock.try_lock(); } /* f */, |
| 213 | 50 /* times */, |
| 214 | 20 /* sleepMs */); |
| 215 | if (!isLocked) { |
| 216 | result << "AAudioClientTracker::NotificationClient may be deadlocked\n"; |
| 217 | } |
| 218 | |
| 219 | result << " client: pid = " << mProcessId << " has " << mStreams.size() << " streams\n"; |
Chih-Hung Hsieh | 48fc619 | 2017-08-04 14:37:31 -0700 | [diff] [blame] | 220 | for (const auto& serviceStream : mStreams) { |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 221 | result << " stream: 0x" << std::setfill('0') << std::setw(8) << std::hex |
| 222 | << serviceStream->getHandle() |
| 223 | << std::dec << std::setfill(' ') << "\n"; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (isLocked) { |
| 227 | mLock.unlock(); |
| 228 | } |
| 229 | return result.str(); |
| 230 | } |