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> |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 21 | #include <aidl/android/media/IMediaTranscodingService.h> |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 22 | #include <android/binder_ibinder.h> |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 23 | #include <inttypes.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 24 | #include <media/TranscodingClientManager.h> |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 25 | #include <media/TranscodingRequest.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 27 | namespace android { |
| 28 | |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 29 | static_assert(sizeof(ClientIdType) == sizeof(void*), "ClientIdType should be pointer-sized"); |
| 30 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 31 | using ::aidl::android::media::BnTranscodingClient; |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 32 | using ::aidl::android::media::IMediaTranscodingService; // For service error codes |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 33 | using ::aidl::android::media::TranscodingJobParcel; |
| 34 | using ::aidl::android::media::TranscodingRequestParcel; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 35 | using Status = ::ndk::ScopedAStatus; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 36 | using ::ndk::SpAIBinder; |
| 37 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 38 | //static |
| 39 | std::atomic<ClientIdType> TranscodingClientManager::sCookieCounter = 0; |
| 40 | //static |
| 41 | std::mutex TranscodingClientManager::sCookie2ClientLock; |
| 42 | //static |
| 43 | std::map<ClientIdType, std::shared_ptr<TranscodingClientManager::ClientImpl>> |
| 44 | TranscodingClientManager::sCookie2Client; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | |
| 47 | /** |
| 48 | * ClientImpl implements a single client and contains all its information. |
| 49 | */ |
| 50 | struct TranscodingClientManager::ClientImpl : public BnTranscodingClient { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 51 | /* The remote client callback that this ClientInfo is associated with. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 52 | * Once the ClientInfo is created, we hold an SpAIBinder so that the binder |
| 53 | * object doesn't get created again, otherwise the binder object pointer |
| 54 | * may not be unique. |
| 55 | */ |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 56 | SpAIBinder mClientBinder; |
| 57 | std::shared_ptr<ITranscodingClientCallback> mClientCallback; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 58 | /* A unique id assigned to the client by the service. This number is used |
| 59 | * by the service for indexing. Here we use the binder object's pointer |
| 60 | * (casted to int64t_t) as the client id. |
| 61 | */ |
| 62 | ClientIdType mClientId; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 63 | pid_t mClientPid; |
| 64 | uid_t mClientUid; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 65 | std::string mClientName; |
| 66 | std::string mClientOpPackageName; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 67 | |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 68 | // Next jobId to assign. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 69 | std::atomic<int32_t> mNextJobId; |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 70 | // Whether this client has been unregistered already. |
| 71 | std::atomic<bool> mAbandoned; |
| 72 | // Weak pointer to the client manager for this client. |
| 73 | std::weak_ptr<TranscodingClientManager> mOwner; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 74 | |
hkuang | 08b38d0 | 2020-04-17 14:29:33 -0700 | [diff] [blame] | 75 | ClientImpl(const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, uid_t uid, |
| 76 | const std::string& clientName, const std::string& opPackageName, |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 77 | const std::weak_ptr<TranscodingClientManager>& owner); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 78 | |
| 79 | Status submitRequest(const TranscodingRequestParcel& /*in_request*/, |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 80 | TranscodingJobParcel* /*out_job*/, bool* /*_aidl_return*/) override; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 81 | |
| 82 | Status cancelJob(int32_t /*in_jobId*/, bool* /*_aidl_return*/) override; |
| 83 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 84 | Status getJobWithId(int32_t /*in_jobId*/, TranscodingJobParcel* /*out_job*/, |
| 85 | bool* /*_aidl_return*/) override; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 86 | |
| 87 | Status unregister() override; |
| 88 | }; |
| 89 | |
| 90 | TranscodingClientManager::ClientImpl::ClientImpl( |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 91 | const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, uid_t uid, |
| 92 | const std::string& clientName, const std::string& opPackageName, |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 93 | const std::weak_ptr<TranscodingClientManager>& owner) |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 94 | : mClientBinder((callback != nullptr) ? callback->asBinder() : nullptr), |
| 95 | mClientCallback(callback), |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 96 | mClientId(sCookieCounter.fetch_add(1, std::memory_order_relaxed)), |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 97 | mClientPid(pid), |
| 98 | mClientUid(uid), |
| 99 | mClientName(clientName), |
| 100 | mClientOpPackageName(opPackageName), |
| 101 | mNextJobId(0), |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 102 | mAbandoned(false), |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 103 | mOwner(owner) {} |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 104 | |
| 105 | Status TranscodingClientManager::ClientImpl::submitRequest( |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 106 | const TranscodingRequestParcel& in_request, TranscodingJobParcel* out_job, |
| 107 | bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 108 | *_aidl_return = false; |
| 109 | |
| 110 | std::shared_ptr<TranscodingClientManager> owner; |
| 111 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 112 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 113 | } |
| 114 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 115 | if (in_request.fileName.empty()) { |
| 116 | // This is the only error we check for now. |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 117 | return Status::ok(); |
| 118 | } |
| 119 | |
| 120 | int32_t jobId = mNextJobId.fetch_add(1); |
| 121 | |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 122 | *_aidl_return = |
| 123 | owner->mJobScheduler->submit(mClientId, jobId, mClientUid, in_request, mClientCallback); |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 124 | |
| 125 | if (*_aidl_return) { |
| 126 | out_job->jobId = jobId; |
| 127 | |
| 128 | // TODO(chz): is some of this coming from JobScheduler? |
| 129 | *(TranscodingRequest*)&out_job->request = in_request; |
| 130 | out_job->awaitNumberOfJobs = 0; |
| 131 | } |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 132 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 133 | return Status::ok(); |
| 134 | } |
| 135 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 136 | Status TranscodingClientManager::ClientImpl::cancelJob(int32_t in_jobId, bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 137 | *_aidl_return = false; |
| 138 | |
| 139 | std::shared_ptr<TranscodingClientManager> owner; |
| 140 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 141 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 142 | } |
| 143 | |
| 144 | if (in_jobId < 0) { |
| 145 | return Status::ok(); |
| 146 | } |
| 147 | |
| 148 | *_aidl_return = owner->mJobScheduler->cancel(mClientId, in_jobId); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 149 | return Status::ok(); |
| 150 | } |
| 151 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 152 | Status TranscodingClientManager::ClientImpl::getJobWithId(int32_t in_jobId, |
| 153 | TranscodingJobParcel* out_job, |
| 154 | bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 155 | *_aidl_return = false; |
| 156 | |
| 157 | std::shared_ptr<TranscodingClientManager> owner; |
| 158 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 159 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 160 | } |
| 161 | |
| 162 | if (in_jobId < 0) { |
| 163 | return Status::ok(); |
| 164 | } |
| 165 | |
| 166 | *_aidl_return = owner->mJobScheduler->getJob(mClientId, in_jobId, &out_job->request); |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 167 | |
| 168 | if (*_aidl_return) { |
| 169 | out_job->jobId = in_jobId; |
| 170 | out_job->awaitNumberOfJobs = 0; |
| 171 | } |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 172 | return Status::ok(); |
| 173 | } |
| 174 | |
| 175 | Status TranscodingClientManager::ClientImpl::unregister() { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 176 | bool abandoned = mAbandoned.exchange(true); |
| 177 | |
| 178 | std::shared_ptr<TranscodingClientManager> owner; |
| 179 | if (abandoned || (owner = mOwner.lock()) == nullptr) { |
| 180 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 181 | } |
| 182 | |
| 183 | // Use jobId == -1 to cancel all realtime jobs for this client with the scheduler. |
| 184 | owner->mJobScheduler->cancel(mClientId, -1); |
| 185 | owner->removeClient(mClientId); |
| 186 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 187 | return Status::ok(); |
| 188 | } |
| 189 | |
| 190 | /////////////////////////////////////////////////////////////////////////////// |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 191 | |
| 192 | // static |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 193 | void TranscodingClientManager::BinderDiedCallback(void* cookie) { |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 194 | ClientIdType clientId = reinterpret_cast<ClientIdType>(cookie); |
| 195 | |
| 196 | ALOGD("Client %lld is dead", (long long)clientId); |
| 197 | |
| 198 | std::shared_ptr<ClientImpl> client; |
| 199 | |
| 200 | { |
| 201 | std::scoped_lock lock{sCookie2ClientLock}; |
| 202 | |
| 203 | auto it = sCookie2Client.find(clientId); |
| 204 | if (it != sCookie2Client.end()) { |
| 205 | client = it->second; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (client != nullptr) { |
| 210 | client->unregister(); |
| 211 | } |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 214 | TranscodingClientManager::TranscodingClientManager( |
| 215 | const std::shared_ptr<SchedulerClientInterface>& scheduler) |
| 216 | : mDeathRecipient(AIBinder_DeathRecipient_new(BinderDiedCallback)), mJobScheduler(scheduler) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 217 | ALOGD("TranscodingClientManager started"); |
| 218 | } |
| 219 | |
| 220 | TranscodingClientManager::~TranscodingClientManager() { |
| 221 | ALOGD("TranscodingClientManager exited"); |
| 222 | } |
| 223 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 224 | void TranscodingClientManager::dumpAllClients(int fd, const Vector<String16>& args __unused) { |
| 225 | String8 result; |
| 226 | |
| 227 | const size_t SIZE = 256; |
| 228 | char buffer[SIZE]; |
hkuang | 08b38d0 | 2020-04-17 14:29:33 -0700 | [diff] [blame] | 229 | std::scoped_lock lock{mLock}; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 230 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 231 | snprintf(buffer, SIZE, " Total num of Clients: %zu\n", mClientIdToClientMap.size()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 232 | result.append(buffer); |
| 233 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 234 | if (mClientIdToClientMap.size() > 0) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 235 | snprintf(buffer, SIZE, "========== Dumping all clients =========\n"); |
| 236 | result.append(buffer); |
| 237 | } |
| 238 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 239 | for (const auto& iter : mClientIdToClientMap) { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 240 | snprintf(buffer, SIZE, " -- Client id: %lld name: %s\n", (long long)iter.first, |
| 241 | iter.second->mClientName.c_str()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 242 | result.append(buffer); |
| 243 | } |
| 244 | |
| 245 | write(fd, result.string(), result.size()); |
| 246 | } |
| 247 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 248 | status_t TranscodingClientManager::addClient( |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 249 | const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, uid_t uid, |
| 250 | const std::string& clientName, const std::string& opPackageName, |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 251 | std::shared_ptr<ITranscodingClient>* outClient) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 252 | // Validate the client. |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 253 | if (callback == nullptr || pid < 0 || clientName.empty() || opPackageName.empty()) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 254 | ALOGE("Invalid client"); |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 255 | return IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 258 | SpAIBinder binder = callback->asBinder(); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 259 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 260 | std::scoped_lock lock{mLock}; |
| 261 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 262 | // Checks if the client already registers. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 263 | if (mRegisteredCallbacks.count((uintptr_t)binder.get()) > 0) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 264 | return IMediaTranscodingService::ERROR_ALREADY_EXISTS; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 267 | // Creates the client and uses its process id as client id. |
| 268 | std::shared_ptr<ClientImpl> client = ::ndk::SharedRefBase::make<ClientImpl>( |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 269 | callback, pid, uid, clientName, opPackageName, shared_from_this()); |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 270 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 271 | 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] | 272 | (long long)client->mClientId, client->mClientPid, client->mClientUid, |
| 273 | client->mClientName.c_str(), client->mClientOpPackageName.c_str()); |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 274 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 275 | { |
| 276 | std::scoped_lock lock{sCookie2ClientLock}; |
| 277 | sCookie2Client.emplace(std::make_pair(client->mClientId, client)); |
| 278 | } |
| 279 | |
| 280 | AIBinder_linkToDeath(binder.get(), mDeathRecipient.get(), |
| 281 | reinterpret_cast<void*>(client->mClientId)); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 282 | |
| 283 | // Adds the new client to the map. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 284 | mRegisteredCallbacks.insert((uintptr_t)binder.get()); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 285 | mClientIdToClientMap[client->mClientId] = client; |
| 286 | |
| 287 | *outClient = client; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 288 | |
| 289 | return OK; |
| 290 | } |
| 291 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 292 | status_t TranscodingClientManager::removeClient(ClientIdType clientId) { |
| 293 | ALOGD("Removing client id %lld", (long long)clientId); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 294 | std::scoped_lock lock{mLock}; |
| 295 | |
| 296 | // Checks if the client is valid. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 297 | auto it = mClientIdToClientMap.find(clientId); |
| 298 | if (it == mClientIdToClientMap.end()) { |
| 299 | ALOGE("Client id %lld does not exist", (long long)clientId); |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 300 | return IMediaTranscodingService::ERROR_INVALID_OPERATION; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 303 | SpAIBinder binder = it->second->mClientBinder; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 304 | |
| 305 | // Check if the client still live. If alive, unlink the death. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 306 | if (binder.get() != nullptr) { |
| 307 | AIBinder_unlinkToDeath(binder.get(), mDeathRecipient.get(), |
| 308 | reinterpret_cast<void*>(it->second->mClientId)); |
| 309 | } |
| 310 | |
| 311 | { |
| 312 | std::scoped_lock lock{sCookie2ClientLock}; |
| 313 | sCookie2Client.erase(it->second->mClientId); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Erase the entry. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 317 | mClientIdToClientMap.erase(it); |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 318 | mRegisteredCallbacks.erase((uintptr_t)binder.get()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 319 | |
| 320 | return OK; |
| 321 | } |
| 322 | |
| 323 | size_t TranscodingClientManager::getNumOfClients() const { |
| 324 | std::scoped_lock lock{mLock}; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 325 | return mClientIdToClientMap.size(); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 326 | } |
| 327 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 328 | } // namespace android |