| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "DrmSessionManager" |
| 19 | #include <utils/Log.h> |
| 20 | |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 21 | #include <aidl/android/media/IResourceManagerClient.h> |
| 22 | #include <aidl/android/media/IResourceManagerService.h> |
| 23 | #include <aidl/android/media/MediaResourceParcel.h> |
| 24 | #include <android/binder_ibinder.h> |
| 25 | #include <android/binder_manager.h> |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
| Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 27 | #include <mediadrm/DrmUtils.h> |
| Jeff Tinker | 7d2c6e8 | 2018-02-16 16:14:59 -0800 | [diff] [blame] | 28 | #include <mediadrm/DrmSessionManager.h> |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <utils/String8.h> |
| 31 | |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 34 | namespace android { |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 35 | |
| 36 | using aidl::android::media::MediaResourceParcel; |
| Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 37 | using aidl::android::media::ClientInfoParcel; |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 38 | |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 39 | using ::ndk::ScopedAStatus; |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 40 | |
| 41 | static String8 GetSessionIdString(const Vector<uint8_t> &sessionId) { |
| 42 | String8 sessionIdStr; |
| 43 | for (size_t i = 0; i < sessionId.size(); ++i) { |
| 44 | sessionIdStr.appendFormat("%u ", sessionId[i]); |
| 45 | } |
| 46 | return sessionIdStr; |
| 47 | } |
| 48 | |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 49 | template <typename Byte = uint8_t> |
| 50 | static std::vector<Byte> toStdVec(const Vector<uint8_t> &vector) { |
| 51 | auto v = reinterpret_cast<const Byte *>(vector.array()); |
| 52 | std::vector<Byte> vec(v, v + vector.size()); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 53 | return vec; |
| 54 | } |
| 55 | |
| Girish | 323711f | 2023-05-24 21:47:34 +0000 | [diff] [blame] | 56 | static Vector<uint8_t> toAndroidVec(const std::vector<uint8_t>& array) { |
| 57 | Vector<uint8_t> vec; |
| 58 | vec.appendArray(array.data(), array.size()); |
| 59 | return vec; |
| 60 | } |
| 61 | |
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 62 | static std::vector<MediaResourceParcel> toResourceVec( |
| 63 | const Vector<uint8_t> &sessionId, int64_t value) { |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 64 | using Type = aidl::android::media::MediaResourceType; |
| 65 | using SubType = aidl::android::media::MediaResourceSubType; |
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 66 | std::vector<MediaResourceParcel> resources; |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 67 | MediaResourceParcel resource{ |
| 68 | Type::kDrmSession, SubType::kUnspecifiedSubType, |
| Jooyung Han | 3d564ff | 2020-02-22 00:46:06 +0900 | [diff] [blame] | 69 | toStdVec<>(sessionId), value}; |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 70 | resources.push_back(resource); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 71 | return resources; |
| 72 | } |
| 73 | |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 74 | bool isEqualSessionId(const Vector<uint8_t> &sessionId1, const Vector<uint8_t> &sessionId2) { |
| 75 | if (sessionId1.size() != sessionId2.size()) { |
| 76 | return false; |
| 77 | } |
| 78 | for (size_t i = 0; i < sessionId1.size(); ++i) { |
| 79 | if (sessionId1[i] != sessionId2[i]) { |
| 80 | return false; |
| 81 | } |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 86 | sp<DrmSessionManager> DrmSessionManager::Instance() { |
| Robert Shih | fc62baf | 2020-03-13 21:49:06 -0700 | [diff] [blame] | 87 | static sp<DrmSessionManager> drmSessionManager = new DrmSessionManager(); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 88 | drmSessionManager->init(); |
| Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 89 | return drmSessionManager; |
| 90 | } |
| 91 | |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 92 | DrmSessionManager::DrmSessionManager() |
| Girish | 323711f | 2023-05-24 21:47:34 +0000 | [diff] [blame] | 93 | : DrmSessionManager(nullptr) { |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 94 | } |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 95 | |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 96 | DrmSessionManager::DrmSessionManager(const std::shared_ptr<IResourceManagerService> &service) |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 97 | : mService(service), |
| Girish | 323711f | 2023-05-24 21:47:34 +0000 | [diff] [blame] | 98 | mDeathRecipient(::ndk::ScopedAIBinder_DeathRecipient( |
| 99 | AIBinder_DeathRecipient_new(ResourceManagerServiceDied))) { |
| 100 | // Setting callback notification when DeathRecipient gets deleted. |
| 101 | AIBinder_DeathRecipient_setOnUnlinked(mDeathRecipient.get(), BinderUnlinkedCallback); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 102 | } |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 103 | |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 104 | DrmSessionManager::~DrmSessionManager() { |
| 105 | if (mService != NULL) { |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 106 | AIBinder_unlinkToDeath(mService->asBinder().get(), mDeathRecipient.get(), this); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 109 | |
| Girish | 323711f | 2023-05-24 21:47:34 +0000 | [diff] [blame] | 110 | status_t DrmSessionManager::init() { |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 111 | Mutex::Autolock lock(mLock); |
| Girish | 323711f | 2023-05-24 21:47:34 +0000 | [diff] [blame] | 112 | getResourceManagerService_l(); |
| 113 | if (mService == nullptr) { |
| 114 | ALOGE("Failed to init ResourceManagerService"); |
| 115 | return DEAD_OBJECT; |
| 116 | } |
| 117 | |
| 118 | return OK; |
| 119 | } |
| 120 | |
| 121 | void DrmSessionManager::getResourceManagerService_l() { |
| 122 | if (mService != nullptr) { |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 123 | return; |
| 124 | } |
| Girish | 323711f | 2023-05-24 21:47:34 +0000 | [diff] [blame] | 125 | |
| 126 | // Get binder interface to resource manager. |
| 127 | ::ndk::SpAIBinder binder(AServiceManager_waitForService("media.resource_manager")); |
| 128 | mService = IResourceManagerService::fromBinder(binder); |
| 129 | if (mService == nullptr) { |
| 130 | ALOGE("Failed to get ResourceManagerService"); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | // Create the context that is passed as cookie to the binder death notification. |
| 135 | // The context gets deleted at BinderUnlinkedCallback. |
| 136 | BinderDiedContext* context = new BinderDiedContext{ |
| 137 | .mDrmSessionManager = wp<DrmSessionManager>::fromExisting(this)}; |
| 138 | // Register for the callbacks by linking to death notification. |
| 139 | AIBinder_linkToDeath(mService->asBinder().get(), mDeathRecipient.get(), context); |
| 140 | |
| 141 | // If the RM was restarted, re-register all the resources. |
| 142 | if (mBinderDied) { |
| 143 | reRegisterAllResources_l(); |
| 144 | mBinderDied = false; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void DrmSessionManager::reRegisterAllResources_l() { |
| 149 | if (mSessionMap.empty()) { |
| 150 | // Nothing to register. |
| 151 | ALOGV("No resources to add"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | if (mService == nullptr) { |
| 156 | ALOGW("Service isn't available"); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | // Go through the session map and re-register all the resources for those sessions. |
| 161 | for (SessionInfoMap::const_iterator iter = mSessionMap.begin(); |
| 162 | iter != mSessionMap.end(); ++iter) { |
| 163 | ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(iter->second.pid), |
| 164 | .uid = static_cast<int32_t>(iter->second.uid), |
| 165 | .id = iter->second.clientId}; |
| 166 | mService->addResource(clientInfo, iter->second.drm, |
| 167 | toResourceVec(toAndroidVec(iter->first), iter->second.resourceValue)); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | void DrmSessionManager::addSession(int pid, |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 172 | const std::shared_ptr<IResourceManagerClient>& drm, const Vector<uint8_t> &sessionId) { |
| 173 | uid_t uid = AIBinder_getCallingUid(); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 174 | ALOGV("addSession(pid %d, uid %d, drm %p, sessionId %s)", pid, uid, drm.get(), |
| Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 175 | GetSessionIdString(sessionId).c_str()); |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 176 | |
| 177 | Mutex::Autolock lock(mLock); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 178 | if (mService == NULL) { |
| 179 | return; |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 180 | } |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 181 | |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 182 | static int64_t clientId = 0; |
| Girish | 323711f | 2023-05-24 21:47:34 +0000 | [diff] [blame] | 183 | mSessionMap[toStdVec(sessionId)] = (SessionInfo){pid, uid, clientId, drm, INT64_MAX}; |
| Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 184 | ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid), |
| 185 | .uid = static_cast<int32_t>(uid), |
| 186 | .id = clientId++}; |
| 187 | mService->addResource(clientInfo, drm, toResourceVec(sessionId, INT64_MAX)); |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void DrmSessionManager::useSession(const Vector<uint8_t> &sessionId) { |
| Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 191 | ALOGV("useSession(%s)", GetSessionIdString(sessionId).c_str()); |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 192 | |
| 193 | Mutex::Autolock lock(mLock); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 194 | auto it = mSessionMap.find(toStdVec(sessionId)); |
| 195 | if (mService == NULL || it == mSessionMap.end()) { |
| 196 | return; |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 197 | } |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 198 | |
| 199 | auto info = it->second; |
| Girish | 323711f | 2023-05-24 21:47:34 +0000 | [diff] [blame] | 200 | info.resourceValue = -1; |
| Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 201 | ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(info.pid), |
| 202 | .uid = static_cast<int32_t>(info.uid), |
| 203 | .id = info.clientId}; |
| 204 | mService->addResource(clientInfo, NULL, toResourceVec(sessionId, -1)); |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void DrmSessionManager::removeSession(const Vector<uint8_t> &sessionId) { |
| Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 208 | ALOGV("removeSession(%s)", GetSessionIdString(sessionId).c_str()); |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 209 | |
| 210 | Mutex::Autolock lock(mLock); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 211 | auto it = mSessionMap.find(toStdVec(sessionId)); |
| 212 | if (mService == NULL || it == mSessionMap.end()) { |
| 213 | return; |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 214 | } |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 215 | |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 216 | auto info = it->second; |
| Robert Shih | fc62baf | 2020-03-13 21:49:06 -0700 | [diff] [blame] | 217 | // removeClient instead of removeSession because each client has only one session |
| Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 218 | ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(info.pid), |
| 219 | .uid = static_cast<int32_t>(info.uid), |
| 220 | .id = info.clientId}; |
| 221 | mService->removeClient(clientInfo); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 222 | mSessionMap.erase(it); |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | bool DrmSessionManager::reclaimSession(int callingPid) { |
| 226 | ALOGV("reclaimSession(%d)", callingPid); |
| 227 | |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 228 | // unlock early because reclaimResource might callback into removeSession |
| 229 | mLock.lock(); |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 230 | std::shared_ptr<IResourceManagerService> service(mService); |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 231 | mLock.unlock(); |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 232 | |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 233 | if (service == NULL) { |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 234 | return false; |
| 235 | } |
| 236 | |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 237 | // cannot update mSessionMap because we do not know which sessionId is reclaimed; |
| 238 | // we rely on IResourceManagerClient to removeSession in reclaimResource |
| Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 239 | Vector<uint8_t> placeHolder; |
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 240 | bool success; |
| Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 241 | uid_t uid = AIBinder_getCallingUid(); |
| 242 | ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(callingPid), |
| 243 | .uid = static_cast<int32_t>(uid)}; |
| 244 | ScopedAStatus status = service->reclaimResource( |
| 245 | clientInfo, toResourceVec(placeHolder, INT64_MAX), &success); |
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 246 | return status.isOk() && success; |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 249 | size_t DrmSessionManager::getSessionCount() const { |
| 250 | Mutex::Autolock lock(mLock); |
| 251 | return mSessionMap.size(); |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 254 | bool DrmSessionManager::containsSession(const Vector<uint8_t>& sessionId) const { |
| 255 | Mutex::Autolock lock(mLock); |
| 256 | return mSessionMap.count(toStdVec(sessionId)); |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 259 | void DrmSessionManager::binderDied() { |
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 260 | ALOGW("ResourceManagerService died."); |
| 261 | Mutex::Autolock lock(mLock); |
| Girish | 323711f | 2023-05-24 21:47:34 +0000 | [diff] [blame] | 262 | mService = nullptr; |
| 263 | mBinderDied = true; |
| 264 | // start an async operation that will reconnect with the RM and |
| 265 | // re-registers all the resources. |
| 266 | mGetServiceFuture = std::async(std::launch::async, [this] { getResourceManagerService(); }); |
| 267 | } |
| 268 | |
| 269 | void DrmSessionManager::ResourceManagerServiceDied(void* cookie) { |
| 270 | BinderDiedContext* context = reinterpret_cast<BinderDiedContext*>(cookie); |
| 271 | |
| 272 | // Validate the context and check if the DrmSessionManager object is still in scope. |
| 273 | if (context != nullptr) { |
| 274 | sp<DrmSessionManager> thiz = context->mDrmSessionManager.promote(); |
| 275 | if (thiz != nullptr) { |
| 276 | thiz->binderDied(); |
| 277 | } else { |
| 278 | ALOGI("DrmSessionManager is out of scope already"); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void DrmSessionManager::BinderUnlinkedCallback(void* cookie) { |
| 284 | BinderDiedContext* context = reinterpret_cast<BinderDiedContext*>(cookie); |
| 285 | // Since we don't need the context anymore, we are deleting it now. |
| 286 | delete context; |
| Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | } // namespace android |