hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 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 <media/TranscodingClientManager.h> |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | class DeathNotifier; |
| 26 | using Status = ::ndk::ScopedAStatus; |
| 27 | |
| 28 | // static |
| 29 | sp<TranscodingClientManager> TranscodingClientManager::getInstance() { |
| 30 | static sp<TranscodingClientManager> sInstance = new TranscodingClientManager(); |
| 31 | return sInstance; |
| 32 | } |
| 33 | |
| 34 | TranscodingClientManager::TranscodingClientManager() |
| 35 | : mDeathRecipient(AIBinder_DeathRecipient_new( |
| 36 | TranscodingClientManager::DeathNotifier::BinderDiedCallback)) { |
| 37 | ALOGD("TranscodingClientManager started"); |
| 38 | } |
| 39 | |
| 40 | TranscodingClientManager::~TranscodingClientManager() { |
| 41 | ALOGD("TranscodingClientManager exited"); |
| 42 | } |
| 43 | |
| 44 | bool TranscodingClientManager::isClientIdRegistered(int32_t clientId) const { |
| 45 | std::scoped_lock lock{mLock}; |
| 46 | return mClientIdToClientInfoMap.find(clientId) != mClientIdToClientInfoMap.end(); |
| 47 | } |
| 48 | |
| 49 | void TranscodingClientManager::dumpAllClients(int fd, const Vector<String16>& args __unused) { |
| 50 | String8 result; |
| 51 | |
| 52 | const size_t SIZE = 256; |
| 53 | char buffer[SIZE]; |
| 54 | |
| 55 | snprintf(buffer, SIZE, " Total num of Clients: %zu\n", mClientIdToClientInfoMap.size()); |
| 56 | result.append(buffer); |
| 57 | |
| 58 | if (mClientIdToClientInfoMap.size() > 0) { |
| 59 | snprintf(buffer, SIZE, "========== Dumping all clients =========\n"); |
| 60 | result.append(buffer); |
| 61 | } |
| 62 | |
| 63 | for (const auto& iter : mClientIdToClientInfoMap) { |
| 64 | const std::shared_ptr<ITranscodingServiceClient> client = iter.second->mClient; |
| 65 | std::string clientName; |
| 66 | Status status = client->getName(&clientName); |
| 67 | if (!status.isOk()) { |
| 68 | ALOGE("Failed to get client: %d information", iter.first); |
| 69 | continue; |
| 70 | } |
| 71 | snprintf(buffer, SIZE, " -- Clients: %d name: %s\n", iter.first, clientName.c_str()); |
| 72 | result.append(buffer); |
| 73 | } |
| 74 | |
| 75 | write(fd, result.string(), result.size()); |
| 76 | } |
| 77 | |
| 78 | status_t TranscodingClientManager::addClient(std::unique_ptr<ClientInfo> client) { |
| 79 | // Validate the client. |
| 80 | if (client == nullptr || client->mClientId <= 0 || client->mClientPid <= 0 || |
| 81 | client->mClientUid <= 0 || client->mClientOpPackageName.empty() || |
| 82 | client->mClientOpPackageName == "") { |
| 83 | ALOGE("Invalid client"); |
| 84 | return BAD_VALUE; |
| 85 | } |
| 86 | |
| 87 | ALOGD("Adding client id %d %s", client->mClientId, client->mClientOpPackageName.c_str()); |
| 88 | std::scoped_lock lock{mLock}; |
| 89 | |
| 90 | // Check if the client already exists. |
| 91 | if (mClientIdToClientInfoMap.count(client->mClientId) != 0) { |
| 92 | ALOGW("Client already exists."); |
| 93 | return ALREADY_EXISTS; |
| 94 | } |
| 95 | |
| 96 | // Listen to the death of the client. |
| 97 | client->mDeathNotifier = new DeathNotifier(); |
| 98 | AIBinder_linkToDeath(client->mClient->asBinder().get(), mDeathRecipient.get(), |
| 99 | client->mDeathNotifier.get()); |
| 100 | |
| 101 | // Adds the new client to the map. |
| 102 | mClientIdToClientInfoMap[client->mClientId] = std::move(client); |
| 103 | |
| 104 | return OK; |
| 105 | } |
| 106 | |
| 107 | status_t TranscodingClientManager::removeClient(int32_t clientId) { |
| 108 | ALOGD("Removing client id %d", clientId); |
| 109 | std::scoped_lock lock{mLock}; |
| 110 | |
| 111 | // Checks if the client is valid. |
| 112 | auto it = mClientIdToClientInfoMap.find(clientId); |
| 113 | if (it == mClientIdToClientInfoMap.end()) { |
| 114 | ALOGE("Client id %d does not exist", clientId); |
| 115 | return INVALID_OPERATION; |
| 116 | } |
| 117 | |
| 118 | std::shared_ptr<ITranscodingServiceClient> client = it->second->mClient; |
| 119 | |
| 120 | // Check if the client still live. If alive, unlink the death. |
| 121 | if (client) { |
| 122 | AIBinder_unlinkToDeath(client->asBinder().get(), mDeathRecipient.get(), |
| 123 | it->second->mDeathNotifier.get()); |
| 124 | } |
| 125 | |
| 126 | // Erase the entry. |
| 127 | mClientIdToClientInfoMap.erase(it); |
| 128 | |
| 129 | return OK; |
| 130 | } |
| 131 | |
| 132 | size_t TranscodingClientManager::getNumOfClients() const { |
| 133 | std::scoped_lock lock{mLock}; |
| 134 | return mClientIdToClientInfoMap.size(); |
| 135 | } |
| 136 | |
| 137 | // static |
| 138 | void TranscodingClientManager::DeathNotifier::BinderDiedCallback(void* cookie) { |
| 139 | int32_t* pClientId = static_cast<int32_t*>(cookie); |
| 140 | ALOGD("Client %d is dead", *pClientId); |
| 141 | // Don't check for pid validity since we know it's already dead. |
| 142 | sp<TranscodingClientManager> manager = TranscodingClientManager::getInstance(); |
| 143 | manager->removeClient(*pClientId); |
| 144 | } |
| 145 | |
| 146 | } // namespace android |