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 | |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 21 | #include <binder/IPCThreadState.h> |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 22 | #include <binder/IProcessInfoService.h> |
| 23 | #include <binder/IServiceManager.h> |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 24 | #include <cutils/properties.h> |
| 25 | #include <media/IResourceManagerClient.h> |
| 26 | #include <media/MediaResource.h> |
Jeff Tinker | 7d2c6e8 | 2018-02-16 16:14:59 -0800 | [diff] [blame] | 27 | #include <mediadrm/DrmSessionManager.h> |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | #include <utils/String8.h> |
| 30 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| 33 | #include "ResourceManagerService.h" |
| 34 | |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 35 | namespace android { |
| 36 | |
| 37 | static String8 GetSessionIdString(const Vector<uint8_t> &sessionId) { |
| 38 | String8 sessionIdStr; |
| 39 | for (size_t i = 0; i < sessionId.size(); ++i) { |
| 40 | sessionIdStr.appendFormat("%u ", sessionId[i]); |
| 41 | } |
| 42 | return sessionIdStr; |
| 43 | } |
| 44 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 45 | static std::vector<uint8_t> toStdVec(const Vector<uint8_t> &vector) { |
| 46 | const uint8_t *v = vector.array(); |
| 47 | std::vector<uint8_t> vec(v, v + vector.size()); |
| 48 | return vec; |
| 49 | } |
| 50 | |
| 51 | static uint64_t toClientId(const sp<IResourceManagerClient>& drm) { |
| 52 | return reinterpret_cast<int64_t>(drm.get()); |
| 53 | } |
| 54 | |
| 55 | static Vector<MediaResource> toResourceVec(const Vector<uint8_t> &sessionId) { |
| 56 | Vector<MediaResource> resources; |
| 57 | // use UINT64_MAX to decrement through addition overflow |
| 58 | resources.push_back(MediaResource(MediaResource::kDrmSession, toStdVec(sessionId), UINT64_MAX)); |
| 59 | return resources; |
| 60 | } |
| 61 | |
| 62 | static sp<IResourceManagerService> getResourceManagerService() { |
| 63 | if (property_get_bool("persist.device_config.media_native.mediadrmserver", 1)) { |
| 64 | return new ResourceManagerService(); |
| 65 | } |
| 66 | sp<IServiceManager> sm = defaultServiceManager(); |
| 67 | if (sm == NULL) { |
| 68 | return NULL; |
| 69 | } |
| 70 | sp<IBinder> binder = sm->getService(String16("media.resource_manager")); |
| 71 | return interface_cast<IResourceManagerService>(binder); |
| 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() { |
| 87 | static sp<DrmSessionManager> drmSessionManager = new DrmSessionManager(); |
Robert Shih | 79673cf | 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() |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 93 | : DrmSessionManager(getResourceManagerService()) { |
| 94 | } |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 95 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 96 | DrmSessionManager::DrmSessionManager(const sp<IResourceManagerService> &service) |
| 97 | : mService(service), |
| 98 | mInitialized(false) { |
| 99 | if (mService == NULL) { |
| 100 | ALOGE("Failed to init ResourceManagerService"); |
| 101 | } |
| 102 | } |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 103 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 104 | DrmSessionManager::~DrmSessionManager() { |
| 105 | if (mService != NULL) { |
| 106 | IInterface::asBinder(mService)->unlinkToDeath(this); |
| 107 | } |
| 108 | } |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 109 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 110 | void DrmSessionManager::init() { |
| 111 | Mutex::Autolock lock(mLock); |
| 112 | if (mInitialized) { |
| 113 | return; |
| 114 | } |
| 115 | mInitialized = true; |
| 116 | if (mService != NULL) { |
| 117 | IInterface::asBinder(mService)->linkToDeath(this); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void DrmSessionManager::addSession(int pid, |
| 122 | const sp<IResourceManagerClient>& drm, const Vector<uint8_t> &sessionId) { |
| 123 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 124 | 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] | 125 | GetSessionIdString(sessionId).string()); |
| 126 | |
| 127 | Mutex::Autolock lock(mLock); |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 128 | if (mService == NULL) { |
| 129 | return; |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 130 | } |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 131 | |
| 132 | int64_t clientId = toClientId(drm); |
| 133 | mSessionMap[toStdVec(sessionId)] = (SessionInfo){pid, uid, clientId}; |
| 134 | mService->addResource(pid, uid, clientId, drm, toResourceVec(sessionId)); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void DrmSessionManager::useSession(const Vector<uint8_t> &sessionId) { |
| 138 | ALOGV("useSession(%s)", GetSessionIdString(sessionId).string()); |
| 139 | |
| 140 | Mutex::Autolock lock(mLock); |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 141 | auto it = mSessionMap.find(toStdVec(sessionId)); |
| 142 | if (mService == NULL || it == mSessionMap.end()) { |
| 143 | return; |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 144 | } |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 145 | |
| 146 | auto info = it->second; |
| 147 | mService->addResource(info.pid, info.uid, info.clientId, NULL, toResourceVec(sessionId)); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void DrmSessionManager::removeSession(const Vector<uint8_t> &sessionId) { |
| 151 | ALOGV("removeSession(%s)", GetSessionIdString(sessionId).string()); |
| 152 | |
| 153 | Mutex::Autolock lock(mLock); |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 154 | auto it = mSessionMap.find(toStdVec(sessionId)); |
| 155 | if (mService == NULL || it == mSessionMap.end()) { |
| 156 | return; |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 157 | } |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 158 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 159 | auto info = it->second; |
| 160 | mService->removeResource(info.pid, info.clientId, toResourceVec(sessionId)); |
| 161 | mSessionMap.erase(it); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | bool DrmSessionManager::reclaimSession(int callingPid) { |
| 165 | ALOGV("reclaimSession(%d)", callingPid); |
| 166 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 167 | // unlock early because reclaimResource might callback into removeSession |
| 168 | mLock.lock(); |
| 169 | sp<IResourceManagerService> service(mService); |
| 170 | mLock.unlock(); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 171 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 172 | if (service == NULL) { |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 176 | // cannot update mSessionMap because we do not know which sessionId is reclaimed; |
| 177 | // we rely on IResourceManagerClient to removeSession in reclaimResource |
| 178 | Vector<uint8_t> dummy; |
| 179 | return service->reclaimResource(callingPid, toResourceVec(dummy)); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 182 | size_t DrmSessionManager::getSessionCount() const { |
| 183 | Mutex::Autolock lock(mLock); |
| 184 | return mSessionMap.size(); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 187 | bool DrmSessionManager::containsSession(const Vector<uint8_t>& sessionId) const { |
| 188 | Mutex::Autolock lock(mLock); |
| 189 | return mSessionMap.count(toStdVec(sessionId)); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Robert Shih | 79673cf | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 192 | void DrmSessionManager::binderDied(const wp<IBinder>& /*who*/) { |
| 193 | ALOGW("ResourceManagerService died."); |
| 194 | Mutex::Autolock lock(mLock); |
| 195 | mService.clear(); |
Ronghua Wu | 10305cc | 2015-02-22 07:55:32 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | } // namespace android |