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; |
| 37 | |
| 38 | namespace { |
| 39 | void ResourceManagerServiceDied(void* cookie) { |
| 40 | auto thiz = static_cast<DrmSessionManager*>(cookie); |
| 41 | thiz->binderDied(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | using ::ndk::ScopedAStatus; |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 46 | |
| 47 | static String8 GetSessionIdString(const Vector<uint8_t> &sessionId) { |
| 48 | String8 sessionIdStr; |
| 49 | for (size_t i = 0; i < sessionId.size(); ++i) { |
| 50 | sessionIdStr.appendFormat("%u ", sessionId[i]); |
| 51 | } |
| 52 | return sessionIdStr; |
| 53 | } |
| 54 | |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 55 | template <typename Byte = uint8_t> |
| 56 | static std::vector<Byte> toStdVec(const Vector<uint8_t> &vector) { |
| 57 | auto v = reinterpret_cast<const Byte *>(vector.array()); |
| 58 | std::vector<Byte> vec(v, v + vector.size()); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 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 | |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 74 | static std::shared_ptr<IResourceManagerService> getResourceManagerService() { |
| 75 | ::ndk::SpAIBinder binder(AServiceManager_getService("media.resource_manager")); |
| 76 | return IResourceManagerService::fromBinder(binder); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 79 | bool isEqualSessionId(const Vector<uint8_t> &sessionId1, const Vector<uint8_t> &sessionId2) { |
| 80 | if (sessionId1.size() != sessionId2.size()) { |
| 81 | return false; |
| 82 | } |
| 83 | for (size_t i = 0; i < sessionId1.size(); ++i) { |
| 84 | if (sessionId1[i] != sessionId2[i]) { |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 91 | sp<DrmSessionManager> DrmSessionManager::Instance() { |
Robert Shih | fc62baf | 2020-03-13 21:49:06 -0700 | [diff] [blame] | 92 | static sp<DrmSessionManager> drmSessionManager = new DrmSessionManager(); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 93 | drmSessionManager->init(); |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 94 | return drmSessionManager; |
| 95 | } |
| 96 | |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 97 | DrmSessionManager::DrmSessionManager() |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 98 | : DrmSessionManager(getResourceManagerService()) { |
| 99 | } |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 100 | |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 101 | DrmSessionManager::DrmSessionManager(const std::shared_ptr<IResourceManagerService> &service) |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 102 | : mService(service), |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 103 | mInitialized(false), |
| 104 | mDeathRecipient(AIBinder_DeathRecipient_new(ResourceManagerServiceDied)) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 105 | if (mService == NULL) { |
| 106 | ALOGE("Failed to init ResourceManagerService"); |
| 107 | } |
| 108 | } |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 109 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 110 | DrmSessionManager::~DrmSessionManager() { |
| 111 | if (mService != NULL) { |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 112 | AIBinder_unlinkToDeath(mService->asBinder().get(), mDeathRecipient.get(), this); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 115 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 116 | void DrmSessionManager::init() { |
| 117 | Mutex::Autolock lock(mLock); |
| 118 | if (mInitialized) { |
| 119 | return; |
| 120 | } |
| 121 | mInitialized = true; |
| 122 | if (mService != NULL) { |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 123 | AIBinder_linkToDeath(mService->asBinder().get(), mDeathRecipient.get(), this); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | void DrmSessionManager::addSession(int pid, |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 128 | const std::shared_ptr<IResourceManagerClient>& drm, const Vector<uint8_t> &sessionId) { |
| 129 | uid_t uid = AIBinder_getCallingUid(); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 130 | ALOGV("addSession(pid %d, uid %d, drm %p, sessionId %s)", pid, uid, drm.get(), |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 131 | GetSessionIdString(sessionId).string()); |
| 132 | |
| 133 | Mutex::Autolock lock(mLock); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 134 | if (mService == NULL) { |
| 135 | return; |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 136 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 137 | |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 138 | static int64_t clientId = 0; |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 139 | mSessionMap[toStdVec(sessionId)] = (SessionInfo){pid, uid, clientId}; |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 140 | mService->addResource(pid, uid, clientId++, drm, toResourceVec(sessionId, INT64_MAX)); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void DrmSessionManager::useSession(const Vector<uint8_t> &sessionId) { |
| 144 | ALOGV("useSession(%s)", GetSessionIdString(sessionId).string()); |
| 145 | |
| 146 | Mutex::Autolock lock(mLock); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 147 | auto it = mSessionMap.find(toStdVec(sessionId)); |
| 148 | if (mService == NULL || it == mSessionMap.end()) { |
| 149 | return; |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 150 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 151 | |
| 152 | auto info = it->second; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 153 | mService->addResource(info.pid, info.uid, info.clientId, NULL, toResourceVec(sessionId, -1)); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void DrmSessionManager::removeSession(const Vector<uint8_t> &sessionId) { |
| 157 | ALOGV("removeSession(%s)", GetSessionIdString(sessionId).string()); |
| 158 | |
| 159 | Mutex::Autolock lock(mLock); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 160 | auto it = mSessionMap.find(toStdVec(sessionId)); |
| 161 | if (mService == NULL || it == mSessionMap.end()) { |
| 162 | return; |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 163 | } |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 164 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 165 | auto info = it->second; |
Robert Shih | fc62baf | 2020-03-13 21:49:06 -0700 | [diff] [blame] | 166 | // removeClient instead of removeSession because each client has only one session |
| 167 | mService->removeClient(info.pid, info.clientId); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 168 | mSessionMap.erase(it); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | bool DrmSessionManager::reclaimSession(int callingPid) { |
| 172 | ALOGV("reclaimSession(%d)", callingPid); |
| 173 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 174 | // unlock early because reclaimResource might callback into removeSession |
| 175 | mLock.lock(); |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 176 | std::shared_ptr<IResourceManagerService> service(mService); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 177 | mLock.unlock(); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 178 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 179 | if (service == NULL) { |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 183 | // cannot update mSessionMap because we do not know which sessionId is reclaimed; |
| 184 | // we rely on IResourceManagerClient to removeSession in reclaimResource |
| 185 | Vector<uint8_t> dummy; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 186 | bool success; |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 187 | ScopedAStatus status = service->reclaimResource(callingPid, toResourceVec(dummy, INT64_MAX), &success); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 188 | return status.isOk() && success; |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 191 | size_t DrmSessionManager::getSessionCount() const { |
| 192 | Mutex::Autolock lock(mLock); |
| 193 | return mSessionMap.size(); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 196 | bool DrmSessionManager::containsSession(const Vector<uint8_t>& sessionId) const { |
| 197 | Mutex::Autolock lock(mLock); |
| 198 | return mSessionMap.count(toStdVec(sessionId)); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 201 | void DrmSessionManager::binderDied() { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 202 | ALOGW("ResourceManagerService died."); |
| 203 | Mutex::Autolock lock(mLock); |
Robert Shih | 0f3a8a0 | 2019-11-14 15:43:39 -0800 | [diff] [blame] | 204 | mService.reset(); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | } // namespace android |