blob: 6bc8613726f4070611bcc67a574df8f17dcd8629 [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
Chong Zhang8e062632020-03-31 10:56:37 -070020#include <aidl/android/media/BnTranscodingClient.h>
21#include <android/binder_ibinder.h>
hkuang9c04b8d2020-01-22 10:03:21 -080022#include <inttypes.h>
hkuang26587cb2020-01-16 10:36:08 -080023#include <media/TranscodingClientManager.h>
Chong Zhang6d58e4b2020-03-31 09:41:10 -070024#include <media/TranscodingRequest.h>
hkuang26587cb2020-01-16 10:36:08 -080025#include <utils/Log.h>
26
27namespace android {
28
Chong Zhang8e062632020-03-31 10:56:37 -070029using ::aidl::android::media::BnTranscodingClient;
30using ::aidl::android::media::TranscodingJobParcel;
31using ::aidl::android::media::TranscodingRequestParcel;
hkuang26587cb2020-01-16 10:36:08 -080032using Status = ::ndk::ScopedAStatus;
Chong Zhang8e062632020-03-31 10:56:37 -070033using ::ndk::SpAIBinder;
34
35///////////////////////////////////////////////////////////////////////////////
36
37/**
38 * ClientImpl implements a single client and contains all its information.
39 */
40struct TranscodingClientManager::ClientImpl : public BnTranscodingClient {
Chong Zhang6d58e4b2020-03-31 09:41:10 -070041 /* The remote client callback that this ClientInfo is associated with.
Chong Zhang8e062632020-03-31 10:56:37 -070042 * 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 Zhang6d58e4b2020-03-31 09:41:10 -070046 SpAIBinder mClientCallback;
Chong Zhang8e062632020-03-31 10:56:37 -070047 /* 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 Zhang6d58e4b2020-03-31 09:41:10 -070052 pid_t mClientPid;
53 uid_t mClientUid;
Chong Zhang8e062632020-03-31 10:56:37 -070054 std::string mClientName;
55 std::string mClientOpPackageName;
Chong Zhang6d58e4b2020-03-31 09:41:10 -070056
57 // Next jobId to assign
58 std::atomic<std::int32_t> mNextJobId;
59 // Pointer to the client manager for this client
Chong Zhang8e062632020-03-31 10:56:37 -070060 TranscodingClientManager* mOwner;
61
hkuang08b38d02020-04-17 14:29:33 -070062 ClientImpl(const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, uid_t uid,
63 const std::string& clientName, const std::string& opPackageName,
Chong Zhang8e062632020-03-31 10:56:37 -070064 TranscodingClientManager* owner);
65
66 Status submitRequest(const TranscodingRequestParcel& /*in_request*/,
Chong Zhang6d58e4b2020-03-31 09:41:10 -070067 TranscodingJobParcel* /*out_job*/, bool* /*_aidl_return*/) override;
Chong Zhang8e062632020-03-31 10:56:37 -070068
69 Status cancelJob(int32_t /*in_jobId*/, bool* /*_aidl_return*/) override;
70
Chong Zhang6d58e4b2020-03-31 09:41:10 -070071 Status getJobWithId(int32_t /*in_jobId*/, TranscodingJobParcel* /*out_job*/,
72 bool* /*_aidl_return*/) override;
Chong Zhang8e062632020-03-31 10:56:37 -070073
74 Status unregister() override;
75};
76
77TranscodingClientManager::ClientImpl::ClientImpl(
Chong Zhang6d58e4b2020-03-31 09:41:10 -070078 const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, uid_t uid,
79 const std::string& clientName, const std::string& opPackageName,
Chong Zhang8e062632020-03-31 10:56:37 -070080 TranscodingClientManager* owner)
Chong Zhang6d58e4b2020-03-31 09:41:10 -070081 : 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 Zhang8e062632020-03-31 10:56:37 -070089
90Status TranscodingClientManager::ClientImpl::submitRequest(
Chong Zhang6d58e4b2020-03-31 09:41:10 -070091 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 =
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700102 mOwner->mJobScheduler->submit(mClientId, jobId, mClientUid, in_request,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700103 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 Zhang8e062632020-03-31 10:56:37 -0700112 return Status::ok();
113}
114
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700115Status TranscodingClientManager::ClientImpl::cancelJob(int32_t in_jobId, bool* _aidl_return) {
116 *_aidl_return = mOwner->mJobScheduler->cancel(mClientId, in_jobId);
Chong Zhang8e062632020-03-31 10:56:37 -0700117 return Status::ok();
118}
119
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700120Status 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 Zhang8e062632020-03-31 10:56:37 -0700129 return Status::ok();
130}
131
132Status TranscodingClientManager::ClientImpl::unregister() {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700133 // 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 Zhang8e062632020-03-31 10:56:37 -0700136 mOwner->removeClient(mClientId);
137 return Status::ok();
138}
139
140///////////////////////////////////////////////////////////////////////////////
hkuang26587cb2020-01-16 10:36:08 -0800141
142// static
hkuang9c04b8d2020-01-22 10:03:21 -0800143void TranscodingClientManager::BinderDiedCallback(void* cookie) {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700144 ClientImpl* client = static_cast<ClientImpl*>(cookie);
145 ALOGD("Client %lld is dead", (long long)client->mClientId);
146 client->unregister();
hkuang9c04b8d2020-01-22 10:03:21 -0800147}
148
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700149TranscodingClientManager::TranscodingClientManager(
150 const std::shared_ptr<SchedulerClientInterface>& scheduler)
151 : mDeathRecipient(AIBinder_DeathRecipient_new(BinderDiedCallback)), mJobScheduler(scheduler) {
hkuang26587cb2020-01-16 10:36:08 -0800152 ALOGD("TranscodingClientManager started");
153}
154
155TranscodingClientManager::~TranscodingClientManager() {
156 ALOGD("TranscodingClientManager exited");
157}
158
hkuang26587cb2020-01-16 10:36:08 -0800159void TranscodingClientManager::dumpAllClients(int fd, const Vector<String16>& args __unused) {
160 String8 result;
161
162 const size_t SIZE = 256;
163 char buffer[SIZE];
hkuang08b38d02020-04-17 14:29:33 -0700164 std::scoped_lock lock{mLock};
hkuang26587cb2020-01-16 10:36:08 -0800165
Chong Zhang8e062632020-03-31 10:56:37 -0700166 snprintf(buffer, SIZE, " Total num of Clients: %zu\n", mClientIdToClientMap.size());
hkuang26587cb2020-01-16 10:36:08 -0800167 result.append(buffer);
168
Chong Zhang8e062632020-03-31 10:56:37 -0700169 if (mClientIdToClientMap.size() > 0) {
hkuang26587cb2020-01-16 10:36:08 -0800170 snprintf(buffer, SIZE, "========== Dumping all clients =========\n");
171 result.append(buffer);
172 }
173
Chong Zhang8e062632020-03-31 10:56:37 -0700174 for (const auto& iter : mClientIdToClientMap) {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700175 snprintf(buffer, SIZE, " -- Client id: %lld name: %s\n", (long long)iter.first,
176 iter.second->mClientName.c_str());
hkuang26587cb2020-01-16 10:36:08 -0800177 result.append(buffer);
178 }
179
180 write(fd, result.string(), result.size());
181}
182
Chong Zhang8e062632020-03-31 10:56:37 -0700183status_t TranscodingClientManager::addClient(
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700184 const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, uid_t uid,
185 const std::string& clientName, const std::string& opPackageName,
Chong Zhang8e062632020-03-31 10:56:37 -0700186 std::shared_ptr<ITranscodingClient>* outClient) {
hkuang26587cb2020-01-16 10:36:08 -0800187 // Validate the client.
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700188 if (callback == nullptr || pid < 0 || clientName.empty() || opPackageName.empty()) {
hkuang26587cb2020-01-16 10:36:08 -0800189 ALOGE("Invalid client");
190 return BAD_VALUE;
191 }
192
Chong Zhang8e062632020-03-31 10:56:37 -0700193 // Creates the client and uses its process id as client id.
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700194 std::shared_ptr<ClientImpl> client = ::ndk::SharedRefBase::make<ClientImpl>(
195 callback, pid, uid, clientName, opPackageName, this);
Chong Zhang8e062632020-03-31 10:56:37 -0700196
hkuang26587cb2020-01-16 10:36:08 -0800197 std::scoped_lock lock{mLock};
198
Chong Zhang8e062632020-03-31 10:56:37 -0700199 // Checks if the client already registers.
hkuang08b38d02020-04-17 14:29:33 -0700200 if (mClientIdToClientMap.find(client->mClientId) != mClientIdToClientMap.end()) {
hkuang26587cb2020-01-16 10:36:08 -0800201 return ALREADY_EXISTS;
202 }
203
Chong Zhang8e062632020-03-31 10:56:37 -0700204 ALOGD("Adding client id %lld, pid %d, uid %d, name %s, package %s",
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700205 (long long)client->mClientId, client->mClientPid, client->mClientUid,
206 client->mClientName.c_str(), client->mClientOpPackageName.c_str());
hkuang9c04b8d2020-01-22 10:03:21 -0800207
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700208 AIBinder_linkToDeath(client->mClientCallback.get(), mDeathRecipient.get(),
209 reinterpret_cast<void*>(client.get()));
hkuang26587cb2020-01-16 10:36:08 -0800210
211 // Adds the new client to the map.
Chong Zhang8e062632020-03-31 10:56:37 -0700212 mClientIdToClientMap[client->mClientId] = client;
213
214 *outClient = client;
hkuang26587cb2020-01-16 10:36:08 -0800215
216 return OK;
217}
218
Chong Zhang8e062632020-03-31 10:56:37 -0700219status_t TranscodingClientManager::removeClient(ClientIdType clientId) {
220 ALOGD("Removing client id %lld", (long long)clientId);
hkuang26587cb2020-01-16 10:36:08 -0800221 std::scoped_lock lock{mLock};
222
223 // Checks if the client is valid.
Chong Zhang8e062632020-03-31 10:56:37 -0700224 auto it = mClientIdToClientMap.find(clientId);
225 if (it == mClientIdToClientMap.end()) {
226 ALOGE("Client id %lld does not exist", (long long)clientId);
hkuang26587cb2020-01-16 10:36:08 -0800227 return INVALID_OPERATION;
228 }
229
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700230 SpAIBinder callback = it->second->mClientCallback;
hkuang26587cb2020-01-16 10:36:08 -0800231
232 // Check if the client still live. If alive, unlink the death.
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700233 if (callback.get() != nullptr) {
234 AIBinder_unlinkToDeath(callback.get(), mDeathRecipient.get(),
235 reinterpret_cast<void*>(it->second.get()));
hkuang26587cb2020-01-16 10:36:08 -0800236 }
237
238 // Erase the entry.
Chong Zhang8e062632020-03-31 10:56:37 -0700239 mClientIdToClientMap.erase(it);
hkuang26587cb2020-01-16 10:36:08 -0800240
241 return OK;
242}
243
244size_t TranscodingClientManager::getNumOfClients() const {
245 std::scoped_lock lock{mLock};
Chong Zhang8e062632020-03-31 10:56:37 -0700246 return mClientIdToClientMap.size();
hkuang26587cb2020-01-16 10:36:08 -0800247}
248
hkuang26587cb2020-01-16 10:36:08 -0800249} // namespace android