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