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> |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 26 | #include <media/TranscodingUidPolicy.h> |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 27 | #include <private/android_filesystem_config.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 28 | #include <utils/Log.h> |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 29 | #include <utils/String16.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 32 | static_assert(sizeof(ClientIdType) == sizeof(void*), "ClientIdType should be pointer-sized"); |
| 33 | |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 34 | static constexpr const char* MEDIA_PROVIDER_PKG_NAME = "com.google.android.providers.media.module"; |
| 35 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 36 | using ::aidl::android::media::BnTranscodingClient; |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 37 | using ::aidl::android::media::IMediaTranscodingService; // For service error codes |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 38 | using ::aidl::android::media::TranscodingJobParcel; |
| 39 | using ::aidl::android::media::TranscodingRequestParcel; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 40 | using Status = ::ndk::ScopedAStatus; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 41 | using ::ndk::SpAIBinder; |
| 42 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 43 | //static |
| 44 | std::atomic<ClientIdType> TranscodingClientManager::sCookieCounter = 0; |
| 45 | //static |
| 46 | std::mutex TranscodingClientManager::sCookie2ClientLock; |
| 47 | //static |
| 48 | std::map<ClientIdType, std::shared_ptr<TranscodingClientManager::ClientImpl>> |
| 49 | TranscodingClientManager::sCookie2Client; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 52 | // Convenience methods for constructing binder::Status objects for error returns |
| 53 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 54 | Status::fromServiceSpecificErrorWithMessage( \ |
| 55 | errorCode, \ |
| 56 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__)) |
| 57 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 58 | /** |
| 59 | * ClientImpl implements a single client and contains all its information. |
| 60 | */ |
| 61 | struct TranscodingClientManager::ClientImpl : public BnTranscodingClient { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 62 | /* The remote client callback that this ClientInfo is associated with. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 63 | * Once the ClientInfo is created, we hold an SpAIBinder so that the binder |
| 64 | * object doesn't get created again, otherwise the binder object pointer |
| 65 | * may not be unique. |
| 66 | */ |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 67 | SpAIBinder mClientBinder; |
| 68 | std::shared_ptr<ITranscodingClientCallback> mClientCallback; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 69 | /* A unique id assigned to the client by the service. This number is used |
| 70 | * by the service for indexing. Here we use the binder object's pointer |
| 71 | * (casted to int64t_t) as the client id. |
| 72 | */ |
| 73 | ClientIdType mClientId; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 74 | std::string mClientName; |
| 75 | std::string mClientOpPackageName; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 76 | |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 77 | // Next jobId to assign. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 78 | std::atomic<int32_t> mNextJobId; |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 79 | // Whether this client has been unregistered already. |
| 80 | std::atomic<bool> mAbandoned; |
| 81 | // Weak pointer to the client manager for this client. |
| 82 | std::weak_ptr<TranscodingClientManager> mOwner; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 83 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 84 | ClientImpl(const std::shared_ptr<ITranscodingClientCallback>& callback, |
hkuang | 08b38d0 | 2020-04-17 14:29:33 -0700 | [diff] [blame] | 85 | const std::string& clientName, const std::string& opPackageName, |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 86 | const std::weak_ptr<TranscodingClientManager>& owner); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 87 | |
| 88 | Status submitRequest(const TranscodingRequestParcel& /*in_request*/, |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 89 | TranscodingJobParcel* /*out_job*/, bool* /*_aidl_return*/) override; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 90 | |
| 91 | Status cancelJob(int32_t /*in_jobId*/, bool* /*_aidl_return*/) override; |
| 92 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 93 | Status getJobWithId(int32_t /*in_jobId*/, TranscodingJobParcel* /*out_job*/, |
| 94 | bool* /*_aidl_return*/) override; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 95 | |
| 96 | Status unregister() override; |
| 97 | }; |
| 98 | |
| 99 | TranscodingClientManager::ClientImpl::ClientImpl( |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 100 | const std::shared_ptr<ITranscodingClientCallback>& callback, const std::string& clientName, |
| 101 | const std::string& opPackageName, const std::weak_ptr<TranscodingClientManager>& owner) |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 102 | : mClientBinder((callback != nullptr) ? callback->asBinder() : nullptr), |
| 103 | mClientCallback(callback), |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 104 | mClientId(sCookieCounter.fetch_add(1, std::memory_order_relaxed)), |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 105 | mClientName(clientName), |
| 106 | mClientOpPackageName(opPackageName), |
| 107 | mNextJobId(0), |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 108 | mAbandoned(false), |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 109 | mOwner(owner) {} |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 110 | |
| 111 | Status TranscodingClientManager::ClientImpl::submitRequest( |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 112 | const TranscodingRequestParcel& in_request, TranscodingJobParcel* out_job, |
| 113 | bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 114 | *_aidl_return = false; |
| 115 | |
| 116 | std::shared_ptr<TranscodingClientManager> owner; |
| 117 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 118 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 119 | } |
| 120 | |
hkuang | 72d105f | 2020-05-21 10:48:55 -0700 | [diff] [blame] | 121 | if (in_request.sourceFilePath.empty() || in_request.destinationFilePath.empty()) { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 122 | return Status::ok(); |
| 123 | } |
| 124 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 125 | int32_t callingPid = AIBinder_getCallingPid(); |
| 126 | int32_t callingUid = AIBinder_getCallingUid(); |
| 127 | int32_t in_clientUid = in_request.clientUid; |
| 128 | int32_t in_clientPid = in_request.clientPid; |
| 129 | |
| 130 | // Check if we can trust clientUid. Only privilege caller could forward the |
| 131 | // uid on app client's behalf. |
| 132 | if (in_clientUid == IMediaTranscodingService::USE_CALLING_UID) { |
| 133 | in_clientUid = callingUid; |
| 134 | } else if (in_clientUid < 0) { |
| 135 | return Status::ok(); |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 136 | } else if (in_clientUid != callingUid && !owner->isTrustedCallingUid(callingUid)) { |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 137 | ALOGE("MediaTranscodingService::registerClient rejected (clientPid %d, clientUid %d) " |
| 138 | "(don't trust callingUid %d)", |
| 139 | in_clientPid, in_clientUid, callingUid); |
| 140 | return STATUS_ERROR_FMT( |
| 141 | IMediaTranscodingService::ERROR_PERMISSION_DENIED, |
| 142 | "MediaTranscodingService::registerClient rejected (clientPid %d, clientUid %d) " |
| 143 | "(don't trust callingUid %d)", |
| 144 | in_clientPid, in_clientUid, callingUid); |
| 145 | } |
| 146 | |
| 147 | // Check if we can trust clientPid. Only privilege caller could forward the |
| 148 | // pid on app client's behalf. |
| 149 | if (in_clientPid == IMediaTranscodingService::USE_CALLING_PID) { |
| 150 | in_clientPid = callingPid; |
| 151 | } else if (in_clientPid < 0) { |
| 152 | return Status::ok(); |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 153 | } else if (in_clientPid != callingPid && !owner->isTrustedCallingUid(callingUid)) { |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 154 | ALOGE("MediaTranscodingService::registerClient rejected (clientPid %d, clientUid %d) " |
| 155 | "(don't trust callingUid %d)", |
| 156 | in_clientPid, in_clientUid, callingUid); |
| 157 | return STATUS_ERROR_FMT( |
| 158 | IMediaTranscodingService::ERROR_PERMISSION_DENIED, |
| 159 | "MediaTranscodingService::registerClient rejected (clientPid %d, clientUid %d) " |
| 160 | "(don't trust callingUid %d)", |
| 161 | in_clientPid, in_clientUid, callingUid); |
| 162 | } |
| 163 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 164 | int32_t jobId = mNextJobId.fetch_add(1); |
| 165 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 166 | *_aidl_return = owner->mJobScheduler->submit(mClientId, jobId, in_clientUid, in_request, |
| 167 | mClientCallback); |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 168 | |
| 169 | if (*_aidl_return) { |
| 170 | out_job->jobId = jobId; |
| 171 | |
| 172 | // TODO(chz): is some of this coming from JobScheduler? |
| 173 | *(TranscodingRequest*)&out_job->request = in_request; |
| 174 | out_job->awaitNumberOfJobs = 0; |
| 175 | } |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 176 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 177 | return Status::ok(); |
| 178 | } |
| 179 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 180 | Status TranscodingClientManager::ClientImpl::cancelJob(int32_t in_jobId, bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 181 | *_aidl_return = false; |
| 182 | |
| 183 | std::shared_ptr<TranscodingClientManager> owner; |
| 184 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 185 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 186 | } |
| 187 | |
| 188 | if (in_jobId < 0) { |
| 189 | return Status::ok(); |
| 190 | } |
| 191 | |
| 192 | *_aidl_return = owner->mJobScheduler->cancel(mClientId, in_jobId); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 193 | return Status::ok(); |
| 194 | } |
| 195 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 196 | Status TranscodingClientManager::ClientImpl::getJobWithId(int32_t in_jobId, |
| 197 | TranscodingJobParcel* out_job, |
| 198 | bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 199 | *_aidl_return = false; |
| 200 | |
| 201 | std::shared_ptr<TranscodingClientManager> owner; |
| 202 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 203 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 204 | } |
| 205 | |
| 206 | if (in_jobId < 0) { |
| 207 | return Status::ok(); |
| 208 | } |
| 209 | |
| 210 | *_aidl_return = owner->mJobScheduler->getJob(mClientId, in_jobId, &out_job->request); |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 211 | |
| 212 | if (*_aidl_return) { |
| 213 | out_job->jobId = in_jobId; |
| 214 | out_job->awaitNumberOfJobs = 0; |
| 215 | } |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 216 | return Status::ok(); |
| 217 | } |
| 218 | |
| 219 | Status TranscodingClientManager::ClientImpl::unregister() { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 220 | bool abandoned = mAbandoned.exchange(true); |
| 221 | |
| 222 | std::shared_ptr<TranscodingClientManager> owner; |
| 223 | if (abandoned || (owner = mOwner.lock()) == nullptr) { |
| 224 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 225 | } |
| 226 | |
| 227 | // Use jobId == -1 to cancel all realtime jobs for this client with the scheduler. |
| 228 | owner->mJobScheduler->cancel(mClientId, -1); |
| 229 | owner->removeClient(mClientId); |
| 230 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 231 | return Status::ok(); |
| 232 | } |
| 233 | |
| 234 | /////////////////////////////////////////////////////////////////////////////// |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 235 | |
| 236 | // static |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 237 | void TranscodingClientManager::BinderDiedCallback(void* cookie) { |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 238 | ClientIdType clientId = reinterpret_cast<ClientIdType>(cookie); |
| 239 | |
| 240 | ALOGD("Client %lld is dead", (long long)clientId); |
| 241 | |
| 242 | std::shared_ptr<ClientImpl> client; |
| 243 | |
| 244 | { |
| 245 | std::scoped_lock lock{sCookie2ClientLock}; |
| 246 | |
| 247 | auto it = sCookie2Client.find(clientId); |
| 248 | if (it != sCookie2Client.end()) { |
| 249 | client = it->second; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (client != nullptr) { |
| 254 | client->unregister(); |
| 255 | } |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 258 | TranscodingClientManager::TranscodingClientManager( |
| 259 | const std::shared_ptr<SchedulerClientInterface>& scheduler) |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 260 | : mDeathRecipient(AIBinder_DeathRecipient_new(BinderDiedCallback)), |
| 261 | mJobScheduler(scheduler), |
| 262 | mMediaProviderUid(-1) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 263 | ALOGD("TranscodingClientManager started"); |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 264 | uid_t mpuid; |
| 265 | if (TranscodingUidPolicy::getUidForPackage(String16(MEDIA_PROVIDER_PKG_NAME), mpuid) == |
| 266 | NO_ERROR) { |
| 267 | ALOGI("Found MediaProvider uid: %d", mpuid); |
| 268 | mMediaProviderUid = mpuid; |
| 269 | } else { |
| 270 | ALOGW("Couldn't get uid for MediaProvider."); |
| 271 | } |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | TranscodingClientManager::~TranscodingClientManager() { |
| 275 | ALOGD("TranscodingClientManager exited"); |
| 276 | } |
| 277 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 278 | void TranscodingClientManager::dumpAllClients(int fd, const Vector<String16>& args __unused) { |
| 279 | String8 result; |
| 280 | |
| 281 | const size_t SIZE = 256; |
| 282 | char buffer[SIZE]; |
hkuang | 08b38d0 | 2020-04-17 14:29:33 -0700 | [diff] [blame] | 283 | std::scoped_lock lock{mLock}; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 284 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 285 | if (mClientIdToClientMap.size() > 0) { |
Chong Zhang | 0579c6f | 2020-10-05 12:03:34 -0700 | [diff] [blame] | 286 | snprintf(buffer, SIZE, "\n========== Dumping all clients =========\n"); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 287 | result.append(buffer); |
| 288 | } |
| 289 | |
Chong Zhang | 0579c6f | 2020-10-05 12:03:34 -0700 | [diff] [blame] | 290 | snprintf(buffer, SIZE, " Total num of Clients: %zu\n", mClientIdToClientMap.size()); |
| 291 | result.append(buffer); |
| 292 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 293 | for (const auto& iter : mClientIdToClientMap) { |
Chong Zhang | 0579c6f | 2020-10-05 12:03:34 -0700 | [diff] [blame] | 294 | snprintf(buffer, SIZE, " Client %lld: pkg: %s\n", (long long)iter.first, |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 295 | iter.second->mClientName.c_str()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 296 | result.append(buffer); |
| 297 | } |
| 298 | |
| 299 | write(fd, result.string(), result.size()); |
| 300 | } |
| 301 | |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 302 | bool TranscodingClientManager::isTrustedCallingUid(uid_t uid) { |
| 303 | if (uid > 0 && uid == mMediaProviderUid) { |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | switch (uid) { |
| 308 | case AID_ROOT: // root user |
| 309 | case AID_SYSTEM: |
| 310 | case AID_SHELL: |
| 311 | case AID_MEDIA: // mediaserver |
| 312 | return true; |
| 313 | default: |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 318 | status_t TranscodingClientManager::addClient( |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 319 | const std::shared_ptr<ITranscodingClientCallback>& callback, const std::string& clientName, |
| 320 | const std::string& opPackageName, std::shared_ptr<ITranscodingClient>* outClient) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 321 | // Validate the client. |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 322 | if (callback == nullptr || clientName.empty() || opPackageName.empty()) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 323 | ALOGE("Invalid client"); |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 324 | return IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 327 | SpAIBinder binder = callback->asBinder(); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 328 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 329 | std::scoped_lock lock{mLock}; |
| 330 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 331 | // Checks if the client already registers. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 332 | if (mRegisteredCallbacks.count((uintptr_t)binder.get()) > 0) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 333 | return IMediaTranscodingService::ERROR_ALREADY_EXISTS; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 336 | // Creates the client (with the id assigned by ClientImpl). |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 337 | std::shared_ptr<ClientImpl> client = ::ndk::SharedRefBase::make<ClientImpl>( |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 338 | callback, clientName, opPackageName, shared_from_this()); |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 339 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 340 | ALOGD("Adding client id %lld, name %s, package %s", (long long)client->mClientId, |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 341 | client->mClientName.c_str(), client->mClientOpPackageName.c_str()); |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 342 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 343 | { |
| 344 | std::scoped_lock lock{sCookie2ClientLock}; |
| 345 | sCookie2Client.emplace(std::make_pair(client->mClientId, client)); |
| 346 | } |
| 347 | |
| 348 | AIBinder_linkToDeath(binder.get(), mDeathRecipient.get(), |
| 349 | reinterpret_cast<void*>(client->mClientId)); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 350 | |
| 351 | // Adds the new client to the map. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 352 | mRegisteredCallbacks.insert((uintptr_t)binder.get()); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 353 | mClientIdToClientMap[client->mClientId] = client; |
| 354 | |
| 355 | *outClient = client; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 356 | |
| 357 | return OK; |
| 358 | } |
| 359 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 360 | status_t TranscodingClientManager::removeClient(ClientIdType clientId) { |
| 361 | ALOGD("Removing client id %lld", (long long)clientId); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 362 | std::scoped_lock lock{mLock}; |
| 363 | |
| 364 | // Checks if the client is valid. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 365 | auto it = mClientIdToClientMap.find(clientId); |
| 366 | if (it == mClientIdToClientMap.end()) { |
| 367 | ALOGE("Client id %lld does not exist", (long long)clientId); |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 368 | return IMediaTranscodingService::ERROR_INVALID_OPERATION; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 371 | SpAIBinder binder = it->second->mClientBinder; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 372 | |
| 373 | // Check if the client still live. If alive, unlink the death. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 374 | if (binder.get() != nullptr) { |
| 375 | AIBinder_unlinkToDeath(binder.get(), mDeathRecipient.get(), |
| 376 | reinterpret_cast<void*>(it->second->mClientId)); |
| 377 | } |
| 378 | |
| 379 | { |
| 380 | std::scoped_lock lock{sCookie2ClientLock}; |
| 381 | sCookie2Client.erase(it->second->mClientId); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // Erase the entry. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 385 | mClientIdToClientMap.erase(it); |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 386 | mRegisteredCallbacks.erase((uintptr_t)binder.get()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 387 | |
| 388 | return OK; |
| 389 | } |
| 390 | |
| 391 | size_t TranscodingClientManager::getNumOfClients() const { |
| 392 | std::scoped_lock lock{mLock}; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 393 | return mClientIdToClientMap.size(); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 394 | } |
| 395 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 396 | } // namespace android |