blob: 6e14434c336d5ad07261ae93e683d06fd1339127 [file] [log] [blame]
Phil Burk11e8d332017-05-24 09:59:02 -07001/*
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 Burkfbf031e2017-10-12 15:58:31 -070018#define LOG_TAG "AAudioClientTracker"
Phil Burk11e8d332017-05-24 09:59:02 -070019//#define LOG_NDEBUG 0
20#include <utils/Log.h>
21
22#include <assert.h>
23#include <binder/IPCThreadState.h>
Phil Burk55e5eab2018-04-10 15:16:38 -070024#include <iomanip>
25#include <iostream>
Phil Burk11e8d332017-05-24 09:59:02 -070026#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
35using namespace android;
36using namespace aaudio;
37
38ANDROID_SINGLETON_STATIC_INSTANCE(AAudioClientTracker);
39
40AAudioClientTracker::AAudioClientTracker()
41 : Singleton<AAudioClientTracker>() {
42}
43
Phil Burk4501b352017-06-29 18:12:36 -070044std::string AAudioClientTracker::dump() const {
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 Burk11e8d332017-05-24 09:59:02 -070065// Create a tracker for the client.
66aaudio_result_t AAudioClientTracker::registerClient(pid_t pid,
67 const sp<IAAudioClient>& client) {
Phil Burkfbf031e2017-10-12 15:58:31 -070068 ALOGV("registerClient(), calling pid = %d, getpid() = %d\n", pid, getpid());
Phil Burk11e8d332017-05-24 09:59:02 -070069
Phil Burkef7eaaf2019-05-01 11:26:35 -070070 if (client.get() == nullptr) {
71 ALOGE("AAudioClientTracker::%s() client is NULL!", __func__);
72 android_errorWriteLog(0x534e4554, "116230453");
73 return AAUDIO_ERROR_NULL;
74 }
75
Phil Burk11e8d332017-05-24 09:59:02 -070076 std::lock_guard<std::mutex> lock(mLock);
77 if (mNotificationClients.count(pid) == 0) {
Steven Moreland37a61632019-12-04 14:06:50 -080078 sp<IBinder> binder = IInterface::asBinder(client);
79 sp<NotificationClient> notificationClient = new NotificationClient(pid, binder);
Phil Burk11e8d332017-05-24 09:59:02 -070080 mNotificationClients[pid] = notificationClient;
81
Phil Burk11e8d332017-05-24 09:59:02 -070082 status_t status = binder->linkToDeath(notificationClient);
Phil Burkfbf031e2017-10-12 15:58:31 -070083 ALOGW_IF(status != NO_ERROR, "registerClient() linkToDeath = %d\n", status);
Phil Burk11e8d332017-05-24 09:59:02 -070084 return AAudioConvert_androidToAAudioResult(status);
85 } else {
Phil Burkfbf031e2017-10-12 15:58:31 -070086 ALOGW("registerClient(%d) already registered!", pid);
Phil Burk11e8d332017-05-24 09:59:02 -070087 return AAUDIO_OK; // TODO should this be considered an error
88 }
89}
90
91void AAudioClientTracker::unregisterClient(pid_t pid) {
Phil Burkfbf031e2017-10-12 15:58:31 -070092 ALOGV("unregisterClient(), calling pid = %d, getpid() = %d\n", pid, getpid());
Phil Burk11e8d332017-05-24 09:59:02 -070093 std::lock_guard<std::mutex> lock(mLock);
94 mNotificationClients.erase(pid);
95}
96
Phil Burk91692942017-06-30 12:23:05 -070097int32_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
Phil Burk11e8d332017-05-24 09:59:02 -0700107aaudio_result_t
108AAudioClientTracker::registerClientStream(pid_t pid, sp<AAudioServiceStreamBase> serviceStream) {
109 aaudio_result_t result = AAUDIO_OK;
Phil Burk29ccc292019-04-15 08:58:08 -0700110 ALOGV("registerClientStream(%d,)\n", pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700111 std::lock_guard<std::mutex> lock(mLock);
112 sp<NotificationClient> notificationClient = mNotificationClients[pid];
113 if (notificationClient == 0) {
114 // This will get called the first time the audio server registers an internal stream.
Phil Burkfbf031e2017-10-12 15:58:31 -0700115 ALOGV("registerClientStream(%d,) unrecognized pid\n", pid);
Steven Moreland37a61632019-12-04 14:06:50 -0800116 notificationClient = new NotificationClient(pid, nullptr);
Phil Burk11e8d332017-05-24 09:59:02 -0700117 mNotificationClients[pid] = notificationClient;
118 }
119 notificationClient->registerClientStream(serviceStream);
120 return result;
121}
122
123// Find the tracker for this process and remove it.
124aaudio_result_t
125AAudioClientTracker::unregisterClientStream(pid_t pid,
126 sp<AAudioServiceStreamBase> serviceStream) {
Phil Burk29ccc292019-04-15 08:58:08 -0700127 ALOGV("unregisterClientStream(%d,)\n", pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700128 std::lock_guard<std::mutex> lock(mLock);
Phil Burk91692942017-06-30 12:23:05 -0700129 auto it = mNotificationClients.find(pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700130 if (it != mNotificationClients.end()) {
Phil Burk29ccc292019-04-15 08:58:08 -0700131 ALOGV("unregisterClientStream(%d,) found NotificationClient\n", pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700132 it->second->unregisterClientStream(serviceStream);
Phil Burk4501b352017-06-29 18:12:36 -0700133 } else {
Phil Burk29ccc292019-04-15 08:58:08 -0700134 ALOGE("unregisterClientStream(%d,) missing NotificationClient\n", pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700135 }
136 return AAUDIO_OK;
137}
138
Steven Moreland37a61632019-12-04 14:06:50 -0800139AAudioClientTracker::NotificationClient::NotificationClient(pid_t pid, const sp<IBinder>& binder)
140 : mProcessId(pid), mBinder(binder) {
Phil Burk11e8d332017-05-24 09:59:02 -0700141}
142
143AAudioClientTracker::NotificationClient::~NotificationClient() {
Phil Burk11e8d332017-05-24 09:59:02 -0700144}
145
Phil Burk91692942017-06-30 12:23:05 -0700146int32_t AAudioClientTracker::NotificationClient::getStreamCount() {
147 std::lock_guard<std::mutex> lock(mLock);
148 return mStreams.size();
149}
150
Phil Burk11e8d332017-05-24 09:59:02 -0700151aaudio_result_t AAudioClientTracker::NotificationClient::registerClientStream(
152 sp<AAudioServiceStreamBase> serviceStream) {
153 std::lock_guard<std::mutex> lock(mLock);
154 mStreams.insert(serviceStream);
155 return AAUDIO_OK;
156}
157
158aaudio_result_t AAudioClientTracker::NotificationClient::unregisterClientStream(
159 sp<AAudioServiceStreamBase> serviceStream) {
160 std::lock_guard<std::mutex> lock(mLock);
161 mStreams.erase(serviceStream);
162 return AAUDIO_OK;
163}
164
165// Close any open streams for the client.
166void AAudioClientTracker::NotificationClient::binderDied(const wp<IBinder>& who __unused) {
167 AAudioService *aaudioService = AAudioClientTracker::getInstance().getAAudioService();
168 if (aaudioService != nullptr) {
169 // Copy the current list of streams to another vector because closing them below
170 // will cause unregisterClientStream() calls back to this object.
Phil Burk4501b352017-06-29 18:12:36 -0700171 std::set<sp<AAudioServiceStreamBase>> streamsToClose;
Phil Burk11e8d332017-05-24 09:59:02 -0700172
173 {
174 std::lock_guard<std::mutex> lock(mLock);
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700175 for (const auto& serviceStream : mStreams) {
Phil Burk11e8d332017-05-24 09:59:02 -0700176 streamsToClose.insert(serviceStream);
177 }
178 }
179
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700180 for (const auto& serviceStream : streamsToClose) {
Phil Burk11e8d332017-05-24 09:59:02 -0700181 aaudio_handle_t handle = serviceStream->getHandle();
Phil Burkfbf031e2017-10-12 15:58:31 -0700182 ALOGW("binderDied() close abandoned stream 0x%08X\n", handle);
Phil Burk11e8d332017-05-24 09:59:02 -0700183 aaudioService->closeStream(handle);
184 }
185 // mStreams should be empty now
186 }
187 sp<NotificationClient> keep(this);
188 AAudioClientTracker::getInstance().unregisterClient(mProcessId);
189}
Phil Burk4501b352017-06-29 18:12:36 -0700190
191
192std::string AAudioClientTracker::NotificationClient::dump() const {
193 std::stringstream result;
194 const bool isLocked = AAudio_tryUntilTrue(
195 [this]()->bool { return mLock.try_lock(); } /* f */,
196 50 /* times */,
197 20 /* sleepMs */);
198 if (!isLocked) {
199 result << "AAudioClientTracker::NotificationClient may be deadlocked\n";
200 }
201
202 result << " client: pid = " << mProcessId << " has " << mStreams.size() << " streams\n";
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700203 for (const auto& serviceStream : mStreams) {
Phil Burk55e5eab2018-04-10 15:16:38 -0700204 result << " stream: 0x" << std::setfill('0') << std::setw(8) << std::hex
205 << serviceStream->getHandle()
206 << std::dec << std::setfill(' ') << "\n";
Phil Burk4501b352017-06-29 18:12:36 -0700207 }
208
209 if (isLocked) {
210 mLock.unlock();
211 }
212 return result.str();
213}