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> |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 23 | #include <android/permission_manager.h> |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 24 | #include <inttypes.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 25 | #include <media/TranscodingClientManager.h> |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 26 | #include <media/TranscodingRequest.h> |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 27 | #include <media/TranscodingUidPolicy.h> |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 30 | #include <utils/String16.h> |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 31 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 34 | static_assert(sizeof(ClientIdType) == sizeof(void*), "ClientIdType should be pointer-sized"); |
| 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 | |
Chong Zhang | ebd86d3 | 2021-03-29 11:30:56 -0700 | [diff] [blame] | 97 | Status addClientUid(int32_t /*in_sessionId*/, int32_t /*in_clientUid*/, |
| 98 | bool* /*_aidl_return*/) override; |
| 99 | |
Chong Zhang | 18ec532 | 2021-04-02 10:18:06 -0700 | [diff] [blame] | 100 | Status getClientUids(int32_t /*in_sessionId*/, |
| 101 | std::optional<std::vector<int32_t>>* /*_aidl_return*/) override; |
Chong Zhang | ebd86d3 | 2021-03-29 11:30:56 -0700 | [diff] [blame] | 102 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 103 | Status unregister() override; |
| 104 | }; |
| 105 | |
| 106 | TranscodingClientManager::ClientImpl::ClientImpl( |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 107 | const std::shared_ptr<ITranscodingClientCallback>& callback, const std::string& clientName, |
| 108 | const std::string& opPackageName, const std::weak_ptr<TranscodingClientManager>& owner) |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 109 | : mClientBinder((callback != nullptr) ? callback->asBinder() : nullptr), |
| 110 | mClientCallback(callback), |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 111 | mClientId(sCookieCounter.fetch_add(1, std::memory_order_relaxed)), |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 112 | mClientName(clientName), |
| 113 | mClientOpPackageName(opPackageName), |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 114 | mNextSessionId(0), |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 115 | mAbandoned(false), |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 116 | mOwner(owner) {} |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 117 | |
| 118 | Status TranscodingClientManager::ClientImpl::submitRequest( |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 119 | const TranscodingRequestParcel& in_request, TranscodingSessionParcel* out_session, |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 120 | bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 121 | *_aidl_return = false; |
| 122 | |
| 123 | std::shared_ptr<TranscodingClientManager> owner; |
| 124 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 125 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 126 | } |
| 127 | |
hkuang | 72d105f | 2020-05-21 10:48:55 -0700 | [diff] [blame] | 128 | if (in_request.sourceFilePath.empty() || in_request.destinationFilePath.empty()) { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 129 | return Status::ok(); |
| 130 | } |
| 131 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 132 | int32_t callingPid = AIBinder_getCallingPid(); |
| 133 | int32_t callingUid = AIBinder_getCallingUid(); |
| 134 | int32_t in_clientUid = in_request.clientUid; |
| 135 | int32_t in_clientPid = in_request.clientPid; |
| 136 | |
| 137 | // Check if we can trust clientUid. Only privilege caller could forward the |
| 138 | // uid on app client's behalf. |
| 139 | if (in_clientUid == IMediaTranscodingService::USE_CALLING_UID) { |
| 140 | in_clientUid = callingUid; |
| 141 | } else if (in_clientUid < 0) { |
| 142 | return Status::ok(); |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 143 | } else if (in_clientUid != callingUid && !owner->isTrustedCaller(callingPid, callingUid)) { |
Chong Zhang | 6ea8f8b | 2021-01-21 18:14:11 -0800 | [diff] [blame] | 144 | ALOGE("submitRequest rejected (clientPid %d, clientUid %d) " |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 145 | "(don't trust callingUid %d)", |
| 146 | in_clientPid, in_clientUid, callingUid); |
Chong Zhang | 8677f1f | 2021-01-21 20:37:35 +0000 | [diff] [blame] | 147 | return STATUS_ERROR_FMT(IMediaTranscodingService::ERROR_PERMISSION_DENIED, |
| 148 | "submitRequest rejected (clientPid %d, clientUid %d) " |
| 149 | "(don't trust callingUid %d)", |
| 150 | in_clientPid, in_clientUid, callingUid); |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Check if we can trust clientPid. Only privilege caller could forward the |
| 154 | // pid on app client's behalf. |
| 155 | if (in_clientPid == IMediaTranscodingService::USE_CALLING_PID) { |
| 156 | in_clientPid = callingPid; |
| 157 | } else if (in_clientPid < 0) { |
| 158 | return Status::ok(); |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 159 | } else if (in_clientPid != callingPid && !owner->isTrustedCaller(callingPid, callingUid)) { |
Chong Zhang | 6ea8f8b | 2021-01-21 18:14:11 -0800 | [diff] [blame] | 160 | ALOGE("submitRequest rejected (clientPid %d, clientUid %d) " |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 161 | "(don't trust callingUid %d)", |
| 162 | in_clientPid, in_clientUid, callingUid); |
Chong Zhang | 8677f1f | 2021-01-21 20:37:35 +0000 | [diff] [blame] | 163 | return STATUS_ERROR_FMT(IMediaTranscodingService::ERROR_PERMISSION_DENIED, |
| 164 | "submitRequest rejected (clientPid %d, clientUid %d) " |
| 165 | "(don't trust callingUid %d)", |
| 166 | in_clientPid, in_clientUid, callingUid); |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 169 | int32_t sessionId = mNextSessionId.fetch_add(1); |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 170 | |
Linus Nilsson | a99f404 | 2021-02-25 15:49:43 -0800 | [diff] [blame] | 171 | *_aidl_return = owner->mSessionController->submit(mClientId, sessionId, callingUid, |
| 172 | in_clientUid, in_request, mClientCallback); |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 173 | |
| 174 | if (*_aidl_return) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 175 | out_session->sessionId = sessionId; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 176 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 177 | // TODO(chz): is some of this coming from SessionController? |
| 178 | *(TranscodingRequest*)&out_session->request = in_request; |
| 179 | out_session->awaitNumberOfSessions = 0; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 180 | } |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 181 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 182 | return Status::ok(); |
| 183 | } |
| 184 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 185 | Status TranscodingClientManager::ClientImpl::cancelSession(int32_t in_sessionId, |
| 186 | bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 187 | *_aidl_return = false; |
| 188 | |
| 189 | std::shared_ptr<TranscodingClientManager> owner; |
| 190 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 191 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 192 | } |
| 193 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 194 | if (in_sessionId < 0) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 195 | return Status::ok(); |
| 196 | } |
| 197 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 198 | *_aidl_return = owner->mSessionController->cancel(mClientId, in_sessionId); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 199 | return Status::ok(); |
| 200 | } |
| 201 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 202 | Status TranscodingClientManager::ClientImpl::getSessionWithId(int32_t in_sessionId, |
| 203 | TranscodingSessionParcel* out_session, |
| 204 | bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 205 | *_aidl_return = false; |
| 206 | |
| 207 | std::shared_ptr<TranscodingClientManager> owner; |
| 208 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 209 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 210 | } |
| 211 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 212 | if (in_sessionId < 0) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 213 | return Status::ok(); |
| 214 | } |
| 215 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 216 | *_aidl_return = |
| 217 | owner->mSessionController->getSession(mClientId, in_sessionId, &out_session->request); |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 218 | |
| 219 | if (*_aidl_return) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 220 | out_session->sessionId = in_sessionId; |
| 221 | out_session->awaitNumberOfSessions = 0; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 222 | } |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 223 | return Status::ok(); |
| 224 | } |
| 225 | |
Chong Zhang | ebd86d3 | 2021-03-29 11:30:56 -0700 | [diff] [blame] | 226 | Status TranscodingClientManager::ClientImpl::addClientUid(int32_t in_sessionId, |
| 227 | int32_t in_clientUid, |
| 228 | bool* _aidl_return) { |
| 229 | *_aidl_return = false; |
| 230 | |
| 231 | std::shared_ptr<TranscodingClientManager> owner; |
| 232 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 233 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 234 | } |
| 235 | |
| 236 | if (in_sessionId < 0) { |
| 237 | return Status::ok(); |
| 238 | } |
| 239 | |
| 240 | int32_t callingPid = AIBinder_getCallingPid(); |
| 241 | int32_t callingUid = AIBinder_getCallingUid(); |
| 242 | |
| 243 | // Check if we can trust clientUid. Only privilege caller could add uid to existing sessions. |
| 244 | if (in_clientUid == IMediaTranscodingService::USE_CALLING_UID) { |
| 245 | in_clientUid = callingUid; |
| 246 | } else if (in_clientUid < 0) { |
| 247 | return Status::ok(); |
| 248 | } else if (in_clientUid != callingUid && !owner->isTrustedCaller(callingPid, callingUid)) { |
| 249 | ALOGE("addClientUid rejected (clientUid %d) " |
| 250 | "(don't trust callingUid %d)", |
| 251 | in_clientUid, callingUid); |
| 252 | return STATUS_ERROR_FMT(IMediaTranscodingService::ERROR_PERMISSION_DENIED, |
| 253 | "addClientUid rejected (clientUid %d) " |
| 254 | "(don't trust callingUid %d)", |
| 255 | in_clientUid, callingUid); |
| 256 | } |
| 257 | |
| 258 | *_aidl_return = owner->mSessionController->addClientUid(mClientId, in_sessionId, in_clientUid); |
| 259 | return Status::ok(); |
| 260 | } |
| 261 | |
Chong Zhang | 18ec532 | 2021-04-02 10:18:06 -0700 | [diff] [blame] | 262 | Status TranscodingClientManager::ClientImpl::getClientUids( |
| 263 | int32_t in_sessionId, std::optional<std::vector<int32_t>>* _aidl_return) { |
| 264 | *_aidl_return = std::nullopt; |
Chong Zhang | ebd86d3 | 2021-03-29 11:30:56 -0700 | [diff] [blame] | 265 | |
| 266 | std::shared_ptr<TranscodingClientManager> owner; |
| 267 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 268 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 269 | } |
| 270 | |
| 271 | if (in_sessionId < 0) { |
| 272 | return Status::ok(); |
| 273 | } |
| 274 | |
Chong Zhang | 18ec532 | 2021-04-02 10:18:06 -0700 | [diff] [blame] | 275 | std::vector<int32_t> result; |
| 276 | |
| 277 | if (owner->mSessionController->getClientUids(mClientId, in_sessionId, &result)) { |
| 278 | *_aidl_return = result; |
| 279 | } |
Chong Zhang | ebd86d3 | 2021-03-29 11:30:56 -0700 | [diff] [blame] | 280 | return Status::ok(); |
| 281 | } |
| 282 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 283 | Status TranscodingClientManager::ClientImpl::unregister() { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 284 | bool abandoned = mAbandoned.exchange(true); |
| 285 | |
| 286 | std::shared_ptr<TranscodingClientManager> owner; |
| 287 | if (abandoned || (owner = mOwner.lock()) == nullptr) { |
| 288 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 289 | } |
| 290 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 291 | // Use sessionId == -1 to cancel all realtime sessions for this client with the controller. |
| 292 | owner->mSessionController->cancel(mClientId, -1); |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 293 | owner->removeClient(mClientId); |
| 294 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 295 | return Status::ok(); |
| 296 | } |
| 297 | |
| 298 | /////////////////////////////////////////////////////////////////////////////// |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 299 | |
| 300 | // static |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 301 | void TranscodingClientManager::BinderDiedCallback(void* cookie) { |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 302 | ClientIdType clientId = reinterpret_cast<ClientIdType>(cookie); |
| 303 | |
| 304 | ALOGD("Client %lld is dead", (long long)clientId); |
| 305 | |
| 306 | std::shared_ptr<ClientImpl> client; |
| 307 | |
| 308 | { |
| 309 | std::scoped_lock lock{sCookie2ClientLock}; |
| 310 | |
| 311 | auto it = sCookie2Client.find(clientId); |
| 312 | if (it != sCookie2Client.end()) { |
| 313 | client = it->second; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (client != nullptr) { |
| 318 | client->unregister(); |
| 319 | } |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 322 | TranscodingClientManager::TranscodingClientManager( |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 323 | const std::shared_ptr<ControllerClientInterface>& controller) |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 324 | : mDeathRecipient(AIBinder_DeathRecipient_new(BinderDiedCallback)), |
Chong Zhang | 3fea286 | 2020-10-21 08:46:03 -0700 | [diff] [blame] | 325 | mSessionController(controller) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 326 | ALOGD("TranscodingClientManager started"); |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 327 | for (uid_t uid : {AID_ROOT, AID_SYSTEM, AID_SHELL, AID_MEDIA}) { |
| 328 | mTrustedUids.insert(uid); |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 329 | } |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | TranscodingClientManager::~TranscodingClientManager() { |
| 333 | ALOGD("TranscodingClientManager exited"); |
| 334 | } |
| 335 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 336 | void TranscodingClientManager::dumpAllClients(int fd, const Vector<String16>& args __unused) { |
| 337 | String8 result; |
| 338 | |
| 339 | const size_t SIZE = 256; |
| 340 | char buffer[SIZE]; |
hkuang | 08b38d0 | 2020-04-17 14:29:33 -0700 | [diff] [blame] | 341 | std::scoped_lock lock{mLock}; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 342 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 343 | if (mClientIdToClientMap.size() > 0) { |
Chong Zhang | 0579c6f | 2020-10-05 12:03:34 -0700 | [diff] [blame] | 344 | snprintf(buffer, SIZE, "\n========== Dumping all clients =========\n"); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 345 | result.append(buffer); |
| 346 | } |
| 347 | |
Chong Zhang | 0579c6f | 2020-10-05 12:03:34 -0700 | [diff] [blame] | 348 | snprintf(buffer, SIZE, " Total num of Clients: %zu\n", mClientIdToClientMap.size()); |
| 349 | result.append(buffer); |
| 350 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 351 | for (const auto& iter : mClientIdToClientMap) { |
Chong Zhang | 0579c6f | 2020-10-05 12:03:34 -0700 | [diff] [blame] | 352 | snprintf(buffer, SIZE, " Client %lld: pkg: %s\n", (long long)iter.first, |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 353 | iter.second->mClientName.c_str()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 354 | result.append(buffer); |
| 355 | } |
| 356 | |
Tomasz Wasilczyk | 03fc55f | 2023-08-11 17:05:05 +0000 | [diff] [blame] | 357 | write(fd, result.c_str(), result.size()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 360 | bool TranscodingClientManager::isTrustedCaller(pid_t pid, uid_t uid) { |
| 361 | if (uid > 0 && mTrustedUids.count(uid) > 0) { |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 362 | return true; |
| 363 | } |
| 364 | |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 365 | int32_t result; |
Chong Zhang | 5c504ee | 2021-01-21 18:53:19 -0800 | [diff] [blame] | 366 | if (__builtin_available(android __TRANSCODING_MIN_API__, *)) { |
Jiyong Park | ff99861 | 2021-01-15 16:01:38 +0900 | [diff] [blame] | 367 | if (APermissionManager_checkPermission("android.permission.WRITE_MEDIA_STORAGE", pid, uid, |
| 368 | &result) == PERMISSION_MANAGER_STATUS_OK && |
| 369 | result == PERMISSION_MANAGER_PERMISSION_GRANTED) { |
| 370 | mTrustedUids.insert(uid); |
| 371 | return true; |
| 372 | } |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 373 | } |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 374 | |
| 375 | return false; |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 378 | status_t TranscodingClientManager::addClient( |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 379 | const std::shared_ptr<ITranscodingClientCallback>& callback, const std::string& clientName, |
| 380 | const std::string& opPackageName, std::shared_ptr<ITranscodingClient>* outClient) { |
hkuang | 06e20a0 | 2021-02-16 17:24:42 -0800 | [diff] [blame] | 381 | int32_t callingPid = AIBinder_getCallingPid(); |
| 382 | int32_t callingUid = AIBinder_getCallingUid(); |
| 383 | |
| 384 | // Check if client has the permission |
| 385 | if (!isTrustedCaller(callingPid, callingUid)) { |
| 386 | ALOGE("addClient rejected (clientPid %d, clientUid %d)", callingPid, callingUid); |
| 387 | return IMediaTranscodingService::ERROR_PERMISSION_DENIED; |
| 388 | } |
| 389 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 390 | // Validate the client. |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 391 | if (callback == nullptr || clientName.empty() || opPackageName.empty()) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 392 | ALOGE("Invalid client"); |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 393 | return IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 396 | SpAIBinder binder = callback->asBinder(); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 397 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 398 | std::scoped_lock lock{mLock}; |
| 399 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 400 | // Checks if the client already registers. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 401 | if (mRegisteredCallbacks.count((uintptr_t)binder.get()) > 0) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 402 | return IMediaTranscodingService::ERROR_ALREADY_EXISTS; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 405 | // Creates the client (with the id assigned by ClientImpl). |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 406 | std::shared_ptr<ClientImpl> client = ::ndk::SharedRefBase::make<ClientImpl>( |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 407 | callback, clientName, opPackageName, shared_from_this()); |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 408 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 409 | 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] | 410 | client->mClientName.c_str(), client->mClientOpPackageName.c_str()); |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 411 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 412 | { |
| 413 | std::scoped_lock lock{sCookie2ClientLock}; |
| 414 | sCookie2Client.emplace(std::make_pair(client->mClientId, client)); |
| 415 | } |
| 416 | |
| 417 | AIBinder_linkToDeath(binder.get(), mDeathRecipient.get(), |
| 418 | reinterpret_cast<void*>(client->mClientId)); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 419 | |
| 420 | // Adds the new client to the map. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 421 | mRegisteredCallbacks.insert((uintptr_t)binder.get()); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 422 | mClientIdToClientMap[client->mClientId] = client; |
| 423 | |
| 424 | *outClient = client; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 425 | |
| 426 | return OK; |
| 427 | } |
| 428 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 429 | status_t TranscodingClientManager::removeClient(ClientIdType clientId) { |
| 430 | ALOGD("Removing client id %lld", (long long)clientId); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 431 | std::scoped_lock lock{mLock}; |
| 432 | |
| 433 | // Checks if the client is valid. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 434 | auto it = mClientIdToClientMap.find(clientId); |
| 435 | if (it == mClientIdToClientMap.end()) { |
| 436 | ALOGE("Client id %lld does not exist", (long long)clientId); |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 437 | return IMediaTranscodingService::ERROR_INVALID_OPERATION; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 440 | SpAIBinder binder = it->second->mClientBinder; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 441 | |
| 442 | // Check if the client still live. If alive, unlink the death. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 443 | if (binder.get() != nullptr) { |
| 444 | AIBinder_unlinkToDeath(binder.get(), mDeathRecipient.get(), |
| 445 | reinterpret_cast<void*>(it->second->mClientId)); |
| 446 | } |
| 447 | |
| 448 | { |
| 449 | std::scoped_lock lock{sCookie2ClientLock}; |
| 450 | sCookie2Client.erase(it->second->mClientId); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // Erase the entry. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 454 | mClientIdToClientMap.erase(it); |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 455 | mRegisteredCallbacks.erase((uintptr_t)binder.get()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 456 | |
| 457 | return OK; |
| 458 | } |
| 459 | |
| 460 | size_t TranscodingClientManager::getNumOfClients() const { |
| 461 | std::scoped_lock lock{mLock}; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 462 | return mClientIdToClientMap.size(); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 463 | } |
| 464 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 465 | } // namespace android |