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 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 20 | #include <aidl/android/media/BnTranscodingClient.h> |
| 21 | #include <android/binder_ibinder.h> |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 22 | #include <inttypes.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 23 | #include <media/TranscodingClientManager.h> |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 24 | #include <media/TranscodingRequest.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 29 | using ::aidl::android::media::BnTranscodingClient; |
| 30 | using ::aidl::android::media::TranscodingJobParcel; |
| 31 | using ::aidl::android::media::TranscodingRequestParcel; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 32 | using Status = ::ndk::ScopedAStatus; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 33 | using ::ndk::SpAIBinder; |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | /** |
| 38 | * ClientImpl implements a single client and contains all its information. |
| 39 | */ |
| 40 | struct TranscodingClientManager::ClientImpl : public BnTranscodingClient { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 41 | /* The remote client callback that this ClientInfo is associated with. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 42 | * Once the ClientInfo is created, we hold an SpAIBinder so that the binder |
| 43 | * object doesn't get created again, otherwise the binder object pointer |
| 44 | * may not be unique. |
| 45 | */ |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 46 | SpAIBinder mClientCallback; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 47 | /* A unique id assigned to the client by the service. This number is used |
| 48 | * by the service for indexing. Here we use the binder object's pointer |
| 49 | * (casted to int64t_t) as the client id. |
| 50 | */ |
| 51 | ClientIdType mClientId; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 52 | pid_t mClientPid; |
| 53 | uid_t mClientUid; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 54 | std::string mClientName; |
| 55 | std::string mClientOpPackageName; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 56 | |
| 57 | // Next jobId to assign |
| 58 | std::atomic<std::int32_t> mNextJobId; |
| 59 | // Pointer to the client manager for this client |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 60 | TranscodingClientManager* mOwner; |
| 61 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 62 | ClientImpl(const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, |
| 63 | uid_t uid, const std::string& clientName, const std::string& opPackageName, |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 64 | TranscodingClientManager* owner); |
| 65 | |
| 66 | Status submitRequest(const TranscodingRequestParcel& /*in_request*/, |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 67 | TranscodingJobParcel* /*out_job*/, bool* /*_aidl_return*/) override; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 68 | |
| 69 | Status cancelJob(int32_t /*in_jobId*/, bool* /*_aidl_return*/) override; |
| 70 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 71 | Status getJobWithId(int32_t /*in_jobId*/, TranscodingJobParcel* /*out_job*/, |
| 72 | bool* /*_aidl_return*/) override; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 73 | |
| 74 | Status unregister() override; |
| 75 | }; |
| 76 | |
| 77 | TranscodingClientManager::ClientImpl::ClientImpl( |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 78 | const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, uid_t uid, |
| 79 | const std::string& clientName, const std::string& opPackageName, |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 80 | TranscodingClientManager* owner) |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 81 | : mClientCallback((callback != nullptr) ? callback->asBinder() : nullptr), |
| 82 | mClientId((int64_t)mClientCallback.get()), |
| 83 | mClientPid(pid), |
| 84 | mClientUid(uid), |
| 85 | mClientName(clientName), |
| 86 | mClientOpPackageName(opPackageName), |
| 87 | mNextJobId(0), |
| 88 | mOwner(owner) {} |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 89 | |
| 90 | Status TranscodingClientManager::ClientImpl::submitRequest( |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 91 | const TranscodingRequestParcel& in_request, TranscodingJobParcel* out_job, |
| 92 | bool* _aidl_return) { |
| 93 | if (in_request.fileName.empty()) { |
| 94 | // This is the only error we check for now. |
| 95 | *_aidl_return = false; |
| 96 | return Status::ok(); |
| 97 | } |
| 98 | |
| 99 | int32_t jobId = mNextJobId.fetch_add(1); |
| 100 | |
| 101 | *_aidl_return = |
| 102 | mOwner->mJobScheduler->submit(mClientId, jobId, mClientPid, in_request, |
| 103 | ITranscodingClientCallback::fromBinder(mClientCallback)); |
| 104 | |
| 105 | if (*_aidl_return) { |
| 106 | out_job->jobId = jobId; |
| 107 | |
| 108 | // TODO(chz): is some of this coming from JobScheduler? |
| 109 | *(TranscodingRequest*)&out_job->request = in_request; |
| 110 | out_job->awaitNumberOfJobs = 0; |
| 111 | } |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 112 | return Status::ok(); |
| 113 | } |
| 114 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 115 | Status TranscodingClientManager::ClientImpl::cancelJob(int32_t in_jobId, bool* _aidl_return) { |
| 116 | *_aidl_return = mOwner->mJobScheduler->cancel(mClientId, in_jobId); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 117 | return Status::ok(); |
| 118 | } |
| 119 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 120 | Status TranscodingClientManager::ClientImpl::getJobWithId(int32_t in_jobId, |
| 121 | TranscodingJobParcel* out_job, |
| 122 | bool* _aidl_return) { |
| 123 | *_aidl_return = mOwner->mJobScheduler->getJob(mClientId, in_jobId, &out_job->request); |
| 124 | |
| 125 | if (*_aidl_return) { |
| 126 | out_job->jobId = in_jobId; |
| 127 | out_job->awaitNumberOfJobs = 0; |
| 128 | } |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 129 | return Status::ok(); |
| 130 | } |
| 131 | |
| 132 | Status TranscodingClientManager::ClientImpl::unregister() { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 133 | // TODO(chz): Decide what to do about this client's jobs. |
| 134 | // If app crashed, it could be relaunched later. Do we want to keep the |
| 135 | // jobs around for that? |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 136 | mOwner->removeClient(mClientId); |
| 137 | return Status::ok(); |
| 138 | } |
| 139 | |
| 140 | /////////////////////////////////////////////////////////////////////////////// |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 141 | |
| 142 | // static |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 143 | void TranscodingClientManager::BinderDiedCallback(void* cookie) { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 144 | ClientImpl* client = static_cast<ClientImpl*>(cookie); |
| 145 | ALOGD("Client %lld is dead", (long long)client->mClientId); |
| 146 | client->unregister(); |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 149 | TranscodingClientManager::TranscodingClientManager( |
| 150 | const std::shared_ptr<SchedulerClientInterface>& scheduler) |
| 151 | : mDeathRecipient(AIBinder_DeathRecipient_new(BinderDiedCallback)), mJobScheduler(scheduler) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 152 | ALOGD("TranscodingClientManager started"); |
| 153 | } |
| 154 | |
| 155 | TranscodingClientManager::~TranscodingClientManager() { |
| 156 | ALOGD("TranscodingClientManager exited"); |
| 157 | } |
| 158 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 159 | bool TranscodingClientManager::isClientIdRegistered(ClientIdType clientId) const { |
| 160 | return mClientIdToClientMap.find(clientId) != mClientIdToClientMap.end(); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void TranscodingClientManager::dumpAllClients(int fd, const Vector<String16>& args __unused) { |
| 164 | String8 result; |
| 165 | |
| 166 | const size_t SIZE = 256; |
| 167 | char buffer[SIZE]; |
| 168 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 169 | snprintf(buffer, SIZE, " Total num of Clients: %zu\n", mClientIdToClientMap.size()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 170 | result.append(buffer); |
| 171 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 172 | if (mClientIdToClientMap.size() > 0) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 173 | snprintf(buffer, SIZE, "========== Dumping all clients =========\n"); |
| 174 | result.append(buffer); |
| 175 | } |
| 176 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 177 | for (const auto& iter : mClientIdToClientMap) { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 178 | snprintf(buffer, SIZE, " -- Client id: %lld name: %s\n", (long long)iter.first, |
| 179 | iter.second->mClientName.c_str()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 180 | result.append(buffer); |
| 181 | } |
| 182 | |
| 183 | write(fd, result.string(), result.size()); |
| 184 | } |
| 185 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 186 | status_t TranscodingClientManager::addClient( |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 187 | const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, uid_t uid, |
| 188 | const std::string& clientName, const std::string& opPackageName, |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 189 | std::shared_ptr<ITranscodingClient>* outClient) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 190 | // Validate the client. |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 191 | if (callback == nullptr || pid < 0 || clientName.empty() || opPackageName.empty()) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 192 | ALOGE("Invalid client"); |
| 193 | return BAD_VALUE; |
| 194 | } |
| 195 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 196 | // Creates the client and uses its process id as client id. |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 197 | std::shared_ptr<ClientImpl> client = ::ndk::SharedRefBase::make<ClientImpl>( |
| 198 | callback, pid, uid, clientName, opPackageName, this); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 199 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 200 | std::scoped_lock lock{mLock}; |
| 201 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 202 | // Checks if the client already registers. |
| 203 | if (isClientIdRegistered(client->mClientId)) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 204 | return ALREADY_EXISTS; |
| 205 | } |
| 206 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 207 | ALOGD("Adding client id %lld, pid %d, uid %d, name %s, package %s", |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 208 | (long long)client->mClientId, client->mClientPid, client->mClientUid, |
| 209 | client->mClientName.c_str(), client->mClientOpPackageName.c_str()); |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 210 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 211 | AIBinder_linkToDeath(client->mClientCallback.get(), mDeathRecipient.get(), |
| 212 | reinterpret_cast<void*>(client.get())); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 213 | |
| 214 | // Adds the new client to the map. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 215 | mClientIdToClientMap[client->mClientId] = client; |
| 216 | |
| 217 | *outClient = client; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 218 | |
| 219 | return OK; |
| 220 | } |
| 221 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 222 | status_t TranscodingClientManager::removeClient(ClientIdType clientId) { |
| 223 | ALOGD("Removing client id %lld", (long long)clientId); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 224 | std::scoped_lock lock{mLock}; |
| 225 | |
| 226 | // Checks if the client is valid. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 227 | auto it = mClientIdToClientMap.find(clientId); |
| 228 | if (it == mClientIdToClientMap.end()) { |
| 229 | ALOGE("Client id %lld does not exist", (long long)clientId); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 230 | return INVALID_OPERATION; |
| 231 | } |
| 232 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 233 | SpAIBinder callback = it->second->mClientCallback; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 234 | |
| 235 | // Check if the client still live. If alive, unlink the death. |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 236 | if (callback.get() != nullptr) { |
| 237 | AIBinder_unlinkToDeath(callback.get(), mDeathRecipient.get(), |
| 238 | reinterpret_cast<void*>(it->second.get())); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Erase the entry. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 242 | mClientIdToClientMap.erase(it); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 243 | |
| 244 | return OK; |
| 245 | } |
| 246 | |
| 247 | size_t TranscodingClientManager::getNumOfClients() const { |
| 248 | std::scoped_lock lock{mLock}; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 249 | return mClientIdToClientMap.size(); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 250 | } |
| 251 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 252 | } // namespace android |