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
18 #define LOG_TAG "AAudioClientTracker"
19 //#define LOG_NDEBUG 0
20 #include <utils/Log.h>
21
22 #include <assert.h>
23 #include <binder/IPCThreadState.h>
24 #include <iomanip>
25 #include <iostream>
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
AAudioClientTracker()40 AAudioClientTracker::AAudioClientTracker()
41 : Singleton<AAudioClientTracker>() {
42 }
43
dump() const44 std::string AAudioClientTracker::dump() const NO_THREAD_SAFETY_ANALYSIS {
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
65 // Create a tracker for the client.
registerClient(pid_t pid,const sp<IAAudioClient> & client)66 aaudio_result_t AAudioClientTracker::registerClient(pid_t pid,
67 const sp<IAAudioClient>& client) {
68 ALOGV("registerClient(), calling pid = %d, getpid() = %d\n", pid, getpid());
69
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
76 std::lock_guard<std::mutex> lock(mLock);
77 if (mNotificationClients.count(pid) == 0) {
78 sp<IBinder> binder = IInterface::asBinder(client);
79 sp<NotificationClient> notificationClient = new NotificationClient(pid, binder);
80 mNotificationClients[pid] = notificationClient;
81
82 status_t status = binder->linkToDeath(notificationClient);
83 ALOGW_IF(status != NO_ERROR, "registerClient() linkToDeath = %d\n", status);
84 return AAudioConvert_androidToAAudioResult(status);
85 } else {
86 ALOGW("registerClient(%d) already registered!", pid);
87 return AAUDIO_OK; // TODO should this be considered an error
88 }
89 }
90
unregisterClient(pid_t pid)91 void AAudioClientTracker::unregisterClient(pid_t pid) {
92 ALOGV("unregisterClient(), calling pid = %d, getpid() = %d\n", pid, getpid());
93 std::lock_guard<std::mutex> lock(mLock);
94 mNotificationClients.erase(pid);
95 }
96
getStreamCount(pid_t pid)97 int32_t AAudioClientTracker::getStreamCount(pid_t pid) {
98 std::lock_guard<std::mutex> lock(mLock);
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
107 aaudio_result_t
registerClientStream(pid_t pid,sp<AAudioServiceStreamBase> serviceStream)108 AAudioClientTracker::registerClientStream(pid_t pid, sp<AAudioServiceStreamBase> serviceStream) {
109 ALOGV("registerClientStream(%d,)\n", pid);
110 std::lock_guard<std::mutex> lock(mLock);
111 return getNotificationClient_l(pid)->registerClientStream(serviceStream);
112 }
113
114 // Find the tracker for this process and remove it.
115 aaudio_result_t
unregisterClientStream(pid_t pid,sp<AAudioServiceStreamBase> serviceStream)116 AAudioClientTracker::unregisterClientStream(pid_t pid,
117 sp<AAudioServiceStreamBase> serviceStream) {
118 ALOGV("unregisterClientStream(%d,)\n", pid);
119 std::lock_guard<std::mutex> lock(mLock);
120 auto it = mNotificationClients.find(pid);
121 if (it != mNotificationClients.end()) {
122 ALOGV("unregisterClientStream(%d,) found NotificationClient\n", pid);
123 it->second->unregisterClientStream(serviceStream);
124 } else {
125 ALOGE("unregisterClientStream(%d,) missing NotificationClient\n", pid);
126 }
127 return AAUDIO_OK;
128 }
129
setExclusiveEnabled(pid_t pid,bool enabled)130 void AAudioClientTracker::setExclusiveEnabled(pid_t pid, bool enabled) {
131 ALOGD("%s(%d, %d)\n", __func__, pid, enabled);
132 std::lock_guard<std::mutex> lock(mLock);
133 getNotificationClient_l(pid)->setExclusiveEnabled(enabled);
134 }
135
isExclusiveEnabled(pid_t pid)136 bool AAudioClientTracker::isExclusiveEnabled(pid_t pid) {
137 std::lock_guard<std::mutex> lock(mLock);
138 return getNotificationClient_l(pid)->isExclusiveEnabled();
139 }
140
141 sp<AAudioClientTracker::NotificationClient>
getNotificationClient_l(pid_t pid)142 AAudioClientTracker::getNotificationClient_l(pid_t pid) {
143 sp<NotificationClient> notificationClient = mNotificationClients[pid];
144 if (notificationClient == nullptr) {
145 // This will get called the first time the audio server uses this PID.
146 ALOGV("%s(%d,) unrecognized PID\n", __func__, pid);
147 notificationClient = new AAudioClientTracker::NotificationClient(pid, nullptr);
148 mNotificationClients[pid] = notificationClient;
149 }
150 return notificationClient;
151 }
152
153 // =======================================
154 // AAudioClientTracker::NotificationClient
155 // =======================================
156
NotificationClient(pid_t pid,const sp<IBinder> & binder)157 AAudioClientTracker::NotificationClient::NotificationClient(pid_t pid, const sp<IBinder>& binder)
158 : mProcessId(pid), mBinder(binder) {
159 }
160
~NotificationClient()161 AAudioClientTracker::NotificationClient::~NotificationClient() {
162 }
163
getStreamCount()164 int32_t AAudioClientTracker::NotificationClient::getStreamCount() {
165 std::lock_guard<std::mutex> lock(mLock);
166 return mStreams.size();
167 }
168
registerClientStream(sp<AAudioServiceStreamBase> serviceStream)169 aaudio_result_t AAudioClientTracker::NotificationClient::registerClientStream(
170 sp<AAudioServiceStreamBase> serviceStream) {
171 std::lock_guard<std::mutex> lock(mLock);
172 mStreams.insert(serviceStream);
173 return AAUDIO_OK;
174 }
175
unregisterClientStream(sp<AAudioServiceStreamBase> serviceStream)176 aaudio_result_t AAudioClientTracker::NotificationClient::unregisterClientStream(
177 sp<AAudioServiceStreamBase> serviceStream) {
178 std::lock_guard<std::mutex> lock(mLock);
179 mStreams.erase(serviceStream);
180 return AAUDIO_OK;
181 }
182
183 // Close any open streams for the client.
binderDied(const wp<IBinder> & who __unused)184 void AAudioClientTracker::NotificationClient::binderDied(const wp<IBinder>& who __unused) {
185 AAudioService *aaudioService = AAudioClientTracker::getInstance().getAAudioService();
186 if (aaudioService != nullptr) {
187 // Copy the current list of streams to another vector because closing them below
188 // will cause unregisterClientStream() calls back to this object.
189 std::set<sp<AAudioServiceStreamBase>> streamsToClose;
190
191 {
192 std::lock_guard<std::mutex> lock(mLock);
193 for (const auto& serviceStream : mStreams) {
194 streamsToClose.insert(serviceStream);
195 }
196 }
197
198 for (const auto& serviceStream : streamsToClose) {
199 aaudio_handle_t handle = serviceStream->getHandle();
200 ALOGW("binderDied() close abandoned stream 0x%08X\n", handle);
201 aaudioService->asAAudioServiceInterface().closeStream(handle);
202 }
203 // mStreams should be empty now
204 }
205 sp<NotificationClient> keep(this);
206 AAudioClientTracker::getInstance().unregisterClient(mProcessId);
207 }
208
209
dump() const210 std::string AAudioClientTracker::NotificationClient::dump() const NO_THREAD_SAFETY_ANALYSIS {
211 std::stringstream result;
212 const bool isLocked = AAudio_tryUntilTrue(
213 [this]()->bool { return mLock.try_lock(); } /* f */,
214 50 /* times */,
215 20 /* sleepMs */);
216 if (!isLocked) {
217 result << "AAudioClientTracker::NotificationClient may be deadlocked\n";
218 }
219
220 result << " client: pid = " << mProcessId << " has " << mStreams.size() << " streams\n";
221 for (const auto& serviceStream : mStreams) {
222 result << " stream: 0x" << std::setfill('0') << std::setw(8) << std::hex
223 << serviceStream->getHandle()
224 << std::dec << std::setfill(' ') << "\n";
225 }
226
227 if (isLocked) {
228 mLock.unlock();
229 }
230 return result.str();
231 }
232