1 /*
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 
20 #include <inttypes.h>
21 #include <media/TranscodingClientManager.h>
22 #include <utils/Log.h>
23 
24 namespace android {
25 
26 using Status = ::ndk::ScopedAStatus;
27 
28 // static
getInstance()29 TranscodingClientManager& TranscodingClientManager::getInstance() {
30     static TranscodingClientManager gInstance{};
31     return gInstance;
32 }
33 
34 // static
BinderDiedCallback(void * cookie)35 void 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.
39     TranscodingClientManager& manager = TranscodingClientManager::getInstance();
40     manager.removeClient(clientId);
41 }
42 
TranscodingClientManager()43 TranscodingClientManager::TranscodingClientManager()
44     : mDeathRecipient(AIBinder_DeathRecipient_new(BinderDiedCallback)) {
45     ALOGD("TranscodingClientManager started");
46 }
47 
~TranscodingClientManager()48 TranscodingClientManager::~TranscodingClientManager() {
49     ALOGD("TranscodingClientManager exited");
50 }
51 
isClientIdRegistered(int32_t clientId) const52 bool TranscodingClientManager::isClientIdRegistered(int32_t clientId) const {
53     std::scoped_lock lock{mLock};
54     return mClientIdToClientInfoMap.find(clientId) != mClientIdToClientInfoMap.end();
55 }
56 
dumpAllClients(int fd,const Vector<String16> & args __unused)57 void 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 
addClient(std::unique_ptr<ClientInfo> client)86 status_t TranscodingClientManager::addClient(std::unique_ptr<ClientInfo> client) {
87     // Validate the client.
88     if (client == nullptr || client->mClientId < 0 || client->mClientPid < 0 ||
89         client->mClientUid < 0 || client->mClientOpPackageName.empty() ||
90         client->mClientOpPackageName == "") {
91         ALOGE("Invalid client");
92         return BAD_VALUE;
93     }
94 
95     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 
103     ALOGD("Adding client id %d pid: %d uid: %d %s", client->mClientId, client->mClientPid,
104           client->mClientUid, client->mClientOpPackageName.c_str());
105 
106     AIBinder_linkToDeath(client->mClient->asBinder().get(), mDeathRecipient.get(),
107                          reinterpret_cast<void*>(client->mClientId));
108 
109     // Adds the new client to the map.
110     mClientIdToClientInfoMap[client->mClientId] = std::move(client);
111 
112     return OK;
113 }
114 
removeClient(int32_t clientId)115 status_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(),
131                                reinterpret_cast<void*>(clientId));
132     }
133 
134     // Erase the entry.
135     mClientIdToClientInfoMap.erase(it);
136 
137     return OK;
138 }
139 
getNumOfClients() const140 size_t TranscodingClientManager::getNumOfClients() const {
141     std::scoped_lock lock{mLock};
142     return mClientIdToClientInfoMap.size();
143 }
144 
145 }  // namespace android
146