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