blob: 7264a9bd257b5b3e22e8b601902e6e0dd4e0c967 [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
70 std::lock_guard<std::mutex> lock(mLock);
71 if (mNotificationClients.count(pid) == 0) {
72 sp<NotificationClient> notificationClient = new NotificationClient(pid);
73 mNotificationClients[pid] = notificationClient;
74
75 sp<IBinder> binder = IInterface::asBinder(client);
76 status_t status = binder->linkToDeath(notificationClient);
Phil Burkfbf031e2017-10-12 15:58:31 -070077 ALOGW_IF(status != NO_ERROR, "registerClient() linkToDeath = %d\n", status);
Phil Burk11e8d332017-05-24 09:59:02 -070078 return AAudioConvert_androidToAAudioResult(status);
79 } else {
Phil Burkfbf031e2017-10-12 15:58:31 -070080 ALOGW("registerClient(%d) already registered!", pid);
Phil Burk11e8d332017-05-24 09:59:02 -070081 return AAUDIO_OK; // TODO should this be considered an error
82 }
83}
84
85void AAudioClientTracker::unregisterClient(pid_t pid) {
Phil Burkfbf031e2017-10-12 15:58:31 -070086 ALOGV("unregisterClient(), calling pid = %d, getpid() = %d\n", pid, getpid());
Phil Burk11e8d332017-05-24 09:59:02 -070087 std::lock_guard<std::mutex> lock(mLock);
88 mNotificationClients.erase(pid);
89}
90
Phil Burk91692942017-06-30 12:23:05 -070091int32_t AAudioClientTracker::getStreamCount(pid_t pid) {
92 std::lock_guard<std::mutex> lock(mLock);
93 auto it = mNotificationClients.find(pid);
94 if (it != mNotificationClients.end()) {
95 return it->second->getStreamCount();
96 } else {
97 return 0; // no existing client
98 }
99}
100
Phil Burk11e8d332017-05-24 09:59:02 -0700101aaudio_result_t
102AAudioClientTracker::registerClientStream(pid_t pid, sp<AAudioServiceStreamBase> serviceStream) {
103 aaudio_result_t result = AAUDIO_OK;
Phil Burkfbf031e2017-10-12 15:58:31 -0700104 ALOGV("registerClientStream(%d, %p)\n", pid, serviceStream.get());
Phil Burk11e8d332017-05-24 09:59:02 -0700105 std::lock_guard<std::mutex> lock(mLock);
106 sp<NotificationClient> notificationClient = mNotificationClients[pid];
107 if (notificationClient == 0) {
108 // This will get called the first time the audio server registers an internal stream.
Phil Burkfbf031e2017-10-12 15:58:31 -0700109 ALOGV("registerClientStream(%d,) unrecognized pid\n", pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700110 notificationClient = new NotificationClient(pid);
111 mNotificationClients[pid] = notificationClient;
112 }
113 notificationClient->registerClientStream(serviceStream);
114 return result;
115}
116
117// Find the tracker for this process and remove it.
118aaudio_result_t
119AAudioClientTracker::unregisterClientStream(pid_t pid,
120 sp<AAudioServiceStreamBase> serviceStream) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700121 ALOGV("unregisterClientStream(%d, %p)\n", pid, serviceStream.get());
Phil Burk11e8d332017-05-24 09:59:02 -0700122 std::lock_guard<std::mutex> lock(mLock);
Phil Burk91692942017-06-30 12:23:05 -0700123 auto it = mNotificationClients.find(pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700124 if (it != mNotificationClients.end()) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700125 ALOGV("unregisterClientStream(%d, %p) found NotificationClient\n",
Phil Burk4501b352017-06-29 18:12:36 -0700126 pid, serviceStream.get());
Phil Burk11e8d332017-05-24 09:59:02 -0700127 it->second->unregisterClientStream(serviceStream);
Phil Burk4501b352017-06-29 18:12:36 -0700128 } else {
Phil Burkfbf031e2017-10-12 15:58:31 -0700129 ALOGE("unregisterClientStream(%d, %p) missing NotificationClient\n",
Phil Burk4501b352017-06-29 18:12:36 -0700130 pid, serviceStream.get());
Phil Burk11e8d332017-05-24 09:59:02 -0700131 }
132 return AAUDIO_OK;
133}
134
135AAudioClientTracker::NotificationClient::NotificationClient(pid_t pid)
136 : mProcessId(pid) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700137 //ALOGD("NotificationClient(%d) created %p\n", pid, this);
Phil Burk11e8d332017-05-24 09:59:02 -0700138}
139
140AAudioClientTracker::NotificationClient::~NotificationClient() {
Phil Burkfbf031e2017-10-12 15:58:31 -0700141 //ALOGD("~NotificationClient() destroyed %p\n", this);
Phil Burk11e8d332017-05-24 09:59:02 -0700142}
143
Phil Burk91692942017-06-30 12:23:05 -0700144int32_t AAudioClientTracker::NotificationClient::getStreamCount() {
145 std::lock_guard<std::mutex> lock(mLock);
146 return mStreams.size();
147}
148
Phil Burk11e8d332017-05-24 09:59:02 -0700149aaudio_result_t AAudioClientTracker::NotificationClient::registerClientStream(
150 sp<AAudioServiceStreamBase> serviceStream) {
151 std::lock_guard<std::mutex> lock(mLock);
152 mStreams.insert(serviceStream);
153 return AAUDIO_OK;
154}
155
156aaudio_result_t AAudioClientTracker::NotificationClient::unregisterClientStream(
157 sp<AAudioServiceStreamBase> serviceStream) {
158 std::lock_guard<std::mutex> lock(mLock);
159 mStreams.erase(serviceStream);
160 return AAUDIO_OK;
161}
162
163// Close any open streams for the client.
164void AAudioClientTracker::NotificationClient::binderDied(const wp<IBinder>& who __unused) {
165 AAudioService *aaudioService = AAudioClientTracker::getInstance().getAAudioService();
166 if (aaudioService != nullptr) {
167 // Copy the current list of streams to another vector because closing them below
168 // will cause unregisterClientStream() calls back to this object.
Phil Burk4501b352017-06-29 18:12:36 -0700169 std::set<sp<AAudioServiceStreamBase>> streamsToClose;
Phil Burk11e8d332017-05-24 09:59:02 -0700170
171 {
172 std::lock_guard<std::mutex> lock(mLock);
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700173 for (const auto& serviceStream : mStreams) {
Phil Burk11e8d332017-05-24 09:59:02 -0700174 streamsToClose.insert(serviceStream);
175 }
176 }
177
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700178 for (const auto& serviceStream : streamsToClose) {
Phil Burk11e8d332017-05-24 09:59:02 -0700179 aaudio_handle_t handle = serviceStream->getHandle();
Phil Burkfbf031e2017-10-12 15:58:31 -0700180 ALOGW("binderDied() close abandoned stream 0x%08X\n", handle);
Phil Burk11e8d332017-05-24 09:59:02 -0700181 aaudioService->closeStream(handle);
182 }
183 // mStreams should be empty now
184 }
185 sp<NotificationClient> keep(this);
186 AAudioClientTracker::getInstance().unregisterClient(mProcessId);
187}
Phil Burk4501b352017-06-29 18:12:36 -0700188
189
190std::string AAudioClientTracker::NotificationClient::dump() const {
191 std::stringstream result;
192 const bool isLocked = AAudio_tryUntilTrue(
193 [this]()->bool { return mLock.try_lock(); } /* f */,
194 50 /* times */,
195 20 /* sleepMs */);
196 if (!isLocked) {
197 result << "AAudioClientTracker::NotificationClient may be deadlocked\n";
198 }
199
200 result << " client: pid = " << mProcessId << " has " << mStreams.size() << " streams\n";
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700201 for (const auto& serviceStream : mStreams) {
Phil Burk55e5eab2018-04-10 15:16:38 -0700202 result << " stream: 0x" << std::setfill('0') << std::setw(8) << std::hex
203 << serviceStream->getHandle()
204 << std::dec << std::setfill(' ') << "\n";
Phil Burk4501b352017-06-29 18:12:36 -0700205 }
206
207 if (isLocked) {
208 mLock.unlock();
209 }
210 return result.str();
211}