blob: 7252437f45217a4fe8cb3dfe4768ec6ccad8cfea [file] [log] [blame]
hkuang26587cb2020-01-16 10:36:08 -08001/*
2 * Copyright (C) 2020 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// #define LOG_NDEBUG 0
18#define LOG_TAG "TranscodingClientManager"
19
hkuang9c04b8d2020-01-22 10:03:21 -080020#include <inttypes.h>
hkuang26587cb2020-01-16 10:36:08 -080021#include <media/TranscodingClientManager.h>
22#include <utils/Log.h>
23
24namespace android {
25
hkuang26587cb2020-01-16 10:36:08 -080026using Status = ::ndk::ScopedAStatus;
27
28// static
hkuang5172cab2020-01-31 12:40:28 -080029TranscodingClientManager& TranscodingClientManager::getInstance() {
30 static TranscodingClientManager gInstance{};
31 return gInstance;
hkuang26587cb2020-01-16 10:36:08 -080032}
33
hkuang9c04b8d2020-01-22 10:03:21 -080034// static
35void TranscodingClientManager::BinderDiedCallback(void* cookie) {
36 int32_t clientId = static_cast<int32_t>(reinterpret_cast<intptr_t>(cookie));
37 ALOGD("Client %" PRId32 " is dead", clientId);
38 // Don't check for pid validity since we know it's already dead.
hkuang5172cab2020-01-31 12:40:28 -080039 TranscodingClientManager& manager = TranscodingClientManager::getInstance();
40 manager.removeClient(clientId);
hkuang9c04b8d2020-01-22 10:03:21 -080041}
42
hkuang26587cb2020-01-16 10:36:08 -080043TranscodingClientManager::TranscodingClientManager()
hkuang9c04b8d2020-01-22 10:03:21 -080044 : mDeathRecipient(AIBinder_DeathRecipient_new(BinderDiedCallback)) {
hkuang26587cb2020-01-16 10:36:08 -080045 ALOGD("TranscodingClientManager started");
46}
47
48TranscodingClientManager::~TranscodingClientManager() {
49 ALOGD("TranscodingClientManager exited");
50}
51
52bool TranscodingClientManager::isClientIdRegistered(int32_t clientId) const {
53 std::scoped_lock lock{mLock};
54 return mClientIdToClientInfoMap.find(clientId) != mClientIdToClientInfoMap.end();
55}
56
57void TranscodingClientManager::dumpAllClients(int fd, const Vector<String16>& args __unused) {
58 String8 result;
59
60 const size_t SIZE = 256;
61 char buffer[SIZE];
62
63 snprintf(buffer, SIZE, " Total num of Clients: %zu\n", mClientIdToClientInfoMap.size());
64 result.append(buffer);
65
66 if (mClientIdToClientInfoMap.size() > 0) {
67 snprintf(buffer, SIZE, "========== Dumping all clients =========\n");
68 result.append(buffer);
69 }
70
71 for (const auto& iter : mClientIdToClientInfoMap) {
72 const std::shared_ptr<ITranscodingServiceClient> client = iter.second->mClient;
73 std::string clientName;
74 Status status = client->getName(&clientName);
75 if (!status.isOk()) {
76 ALOGE("Failed to get client: %d information", iter.first);
77 continue;
78 }
79 snprintf(buffer, SIZE, " -- Clients: %d name: %s\n", iter.first, clientName.c_str());
80 result.append(buffer);
81 }
82
83 write(fd, result.string(), result.size());
84}
85
86status_t TranscodingClientManager::addClient(std::unique_ptr<ClientInfo> client) {
87 // Validate the client.
hkuang9c04b8d2020-01-22 10:03:21 -080088 if (client == nullptr || client->mClientId < 0 || client->mClientPid < 0 ||
89 client->mClientUid < 0 || client->mClientOpPackageName.empty() ||
hkuang26587cb2020-01-16 10:36:08 -080090 client->mClientOpPackageName == "") {
91 ALOGE("Invalid client");
92 return BAD_VALUE;
93 }
94
hkuang26587cb2020-01-16 10:36:08 -080095 std::scoped_lock lock{mLock};
96
97 // Check if the client already exists.
98 if (mClientIdToClientInfoMap.count(client->mClientId) != 0) {
99 ALOGW("Client already exists.");
100 return ALREADY_EXISTS;
101 }
102
hkuang9c04b8d2020-01-22 10:03:21 -0800103 ALOGD("Adding client id %d pid: %d uid: %d %s", client->mClientId, client->mClientPid,
104 client->mClientUid, client->mClientOpPackageName.c_str());
105
hkuang26587cb2020-01-16 10:36:08 -0800106 AIBinder_linkToDeath(client->mClient->asBinder().get(), mDeathRecipient.get(),
hkuang9c04b8d2020-01-22 10:03:21 -0800107 reinterpret_cast<void*>(client->mClientId));
hkuang26587cb2020-01-16 10:36:08 -0800108
109 // Adds the new client to the map.
110 mClientIdToClientInfoMap[client->mClientId] = std::move(client);
111
112 return OK;
113}
114
115status_t TranscodingClientManager::removeClient(int32_t clientId) {
116 ALOGD("Removing client id %d", clientId);
117 std::scoped_lock lock{mLock};
118
119 // Checks if the client is valid.
120 auto it = mClientIdToClientInfoMap.find(clientId);
121 if (it == mClientIdToClientInfoMap.end()) {
122 ALOGE("Client id %d does not exist", clientId);
123 return INVALID_OPERATION;
124 }
125
126 std::shared_ptr<ITranscodingServiceClient> client = it->second->mClient;
127
128 // Check if the client still live. If alive, unlink the death.
129 if (client) {
130 AIBinder_unlinkToDeath(client->asBinder().get(), mDeathRecipient.get(),
hkuang9c04b8d2020-01-22 10:03:21 -0800131 reinterpret_cast<void*>(clientId));
hkuang26587cb2020-01-16 10:36:08 -0800132 }
133
134 // Erase the entry.
135 mClientIdToClientInfoMap.erase(it);
136
137 return OK;
138}
139
140size_t TranscodingClientManager::getNumOfClients() const {
141 std::scoped_lock lock{mLock};
142 return mClientIdToClientInfoMap.size();
143}
144
hkuang26587cb2020-01-16 10:36:08 -0800145} // namespace android