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