blob: b05e3a678bced179bc7168da6c66ba3589c6629f [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
Chong Zhang6d58e4b2020-03-31 09:41:10 -070062 ClientImpl(const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid,
63 uid_t uid, 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 =
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 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
Chong Zhang8e062632020-03-31 10:56:37 -0700159bool TranscodingClientManager::isClientIdRegistered(ClientIdType clientId) const {
160 return mClientIdToClientMap.find(clientId) != mClientIdToClientMap.end();
hkuang26587cb2020-01-16 10:36:08 -0800161}
162
163void 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 Zhang8e062632020-03-31 10:56:37 -0700169 snprintf(buffer, SIZE, " Total num of Clients: %zu\n", mClientIdToClientMap.size());
hkuang26587cb2020-01-16 10:36:08 -0800170 result.append(buffer);
171
Chong Zhang8e062632020-03-31 10:56:37 -0700172 if (mClientIdToClientMap.size() > 0) {
hkuang26587cb2020-01-16 10:36:08 -0800173 snprintf(buffer, SIZE, "========== Dumping all clients =========\n");
174 result.append(buffer);
175 }
176
Chong Zhang8e062632020-03-31 10:56:37 -0700177 for (const auto& iter : mClientIdToClientMap) {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700178 snprintf(buffer, SIZE, " -- Client id: %lld name: %s\n", (long long)iter.first,
179 iter.second->mClientName.c_str());
hkuang26587cb2020-01-16 10:36:08 -0800180 result.append(buffer);
181 }
182
183 write(fd, result.string(), result.size());
184}
185
Chong Zhang8e062632020-03-31 10:56:37 -0700186status_t TranscodingClientManager::addClient(
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700187 const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, uid_t uid,
188 const std::string& clientName, const std::string& opPackageName,
Chong Zhang8e062632020-03-31 10:56:37 -0700189 std::shared_ptr<ITranscodingClient>* outClient) {
hkuang26587cb2020-01-16 10:36:08 -0800190 // Validate the client.
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700191 if (callback == nullptr || pid < 0 || clientName.empty() || opPackageName.empty()) {
hkuang26587cb2020-01-16 10:36:08 -0800192 ALOGE("Invalid client");
193 return BAD_VALUE;
194 }
195
Chong Zhang8e062632020-03-31 10:56:37 -0700196 // Creates the client and uses its process id as client id.
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700197 std::shared_ptr<ClientImpl> client = ::ndk::SharedRefBase::make<ClientImpl>(
198 callback, pid, uid, clientName, opPackageName, this);
Chong Zhang8e062632020-03-31 10:56:37 -0700199
hkuang26587cb2020-01-16 10:36:08 -0800200 std::scoped_lock lock{mLock};
201
Chong Zhang8e062632020-03-31 10:56:37 -0700202 // Checks if the client already registers.
203 if (isClientIdRegistered(client->mClientId)) {
hkuang26587cb2020-01-16 10:36:08 -0800204 return ALREADY_EXISTS;
205 }
206
Chong Zhang8e062632020-03-31 10:56:37 -0700207 ALOGD("Adding client id %lld, pid %d, uid %d, name %s, package %s",
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700208 (long long)client->mClientId, client->mClientPid, client->mClientUid,
209 client->mClientName.c_str(), client->mClientOpPackageName.c_str());
hkuang9c04b8d2020-01-22 10:03:21 -0800210
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700211 AIBinder_linkToDeath(client->mClientCallback.get(), mDeathRecipient.get(),
212 reinterpret_cast<void*>(client.get()));
hkuang26587cb2020-01-16 10:36:08 -0800213
214 // Adds the new client to the map.
Chong Zhang8e062632020-03-31 10:56:37 -0700215 mClientIdToClientMap[client->mClientId] = client;
216
217 *outClient = client;
hkuang26587cb2020-01-16 10:36:08 -0800218
219 return OK;
220}
221
Chong Zhang8e062632020-03-31 10:56:37 -0700222status_t TranscodingClientManager::removeClient(ClientIdType clientId) {
223 ALOGD("Removing client id %lld", (long long)clientId);
hkuang26587cb2020-01-16 10:36:08 -0800224 std::scoped_lock lock{mLock};
225
226 // Checks if the client is valid.
Chong Zhang8e062632020-03-31 10:56:37 -0700227 auto it = mClientIdToClientMap.find(clientId);
228 if (it == mClientIdToClientMap.end()) {
229 ALOGE("Client id %lld does not exist", (long long)clientId);
hkuang26587cb2020-01-16 10:36:08 -0800230 return INVALID_OPERATION;
231 }
232
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700233 SpAIBinder callback = it->second->mClientCallback;
hkuang26587cb2020-01-16 10:36:08 -0800234
235 // Check if the client still live. If alive, unlink the death.
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700236 if (callback.get() != nullptr) {
237 AIBinder_unlinkToDeath(callback.get(), mDeathRecipient.get(),
238 reinterpret_cast<void*>(it->second.get()));
hkuang26587cb2020-01-16 10:36:08 -0800239 }
240
241 // Erase the entry.
Chong Zhang8e062632020-03-31 10:56:37 -0700242 mClientIdToClientMap.erase(it);
hkuang26587cb2020-01-16 10:36:08 -0800243
244 return OK;
245}
246
247size_t TranscodingClientManager::getNumOfClients() const {
248 std::scoped_lock lock{mLock};
Chong Zhang8e062632020-03-31 10:56:37 -0700249 return mClientIdToClientMap.size();
hkuang26587cb2020-01-16 10:36:08 -0800250}
251
hkuang26587cb2020-01-16 10:36:08 -0800252} // namespace android