blob: d32228b69db54bae5718937d79e7b8afa404ff11 [file] [log] [blame]
aimitakeshi27ed8ad2010-07-29 10:12:27 +09001/*
2 * Copyright (C) 2010 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
Takeshi Aimi2272ee22010-09-20 23:40:41 +090017//#define LOG_NDEBUG 0
aimitakeshi27ed8ad2010-07-29 10:12:27 +090018#define LOG_TAG "DrmManager(Native)"
aimitakeshi27ed8ad2010-07-29 10:12:27 +090019
Robert Shih7bcf7922020-02-07 15:01:57 -080020#include <cutils/properties.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090021#include <utils/String8.h>
Robert Shih7bcf7922020-02-07 15:01:57 -080022#include <utils/Log.h>
Robert Shihec056ae2019-08-17 01:54:04 -070023
24#include <binder/IPCThreadState.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090025#include <drm/DrmInfo.h>
Robert Shihec056ae2019-08-17 01:54:04 -070026
aimitakeshi27ed8ad2010-07-29 10:12:27 +090027#include <drm/DrmInfoEvent.h>
28#include <drm/DrmRights.h>
29#include <drm/DrmConstraints.h>
Takeshi Aimi34738462010-11-16 13:56:11 +090030#include <drm/DrmMetadata.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090031#include <drm/DrmInfoStatus.h>
32#include <drm/DrmInfoRequest.h>
33#include <drm/DrmSupportInfo.h>
34#include <drm/DrmConvertedStatus.h>
Ray Essickf27e9872019-12-07 06:28:46 -080035#include <media/MediaMetricsItem.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090036#include <IDrmEngine.h>
37
38#include "DrmManager.h"
39#include "ReadWriteUtils.h"
40
Robert Shih7bcf7922020-02-07 15:01:57 -080041#include <algorithm>
Tomasz Wasilczyk259940c2023-08-23 03:29:56 +000042#include <filesystem>
Robert Shih7bcf7922020-02-07 15:01:57 -080043
Chih-Hung Hsieh92c6b822016-05-17 15:20:14 -070044#define DECRYPT_FILE_ERROR (-1)
aimitakeshi27ed8ad2010-07-29 10:12:27 +090045
46using namespace android;
47
48const String8 DrmManager::EMPTY_STRING("");
49
Robert Shih7bcf7922020-02-07 15:01:57 -080050const std::map<const char*, size_t> DrmManager::kMethodIdMap {
51 {"getConstraints" , DrmManagerMethodId::GET_CONSTRAINTS },
52 {"getMetadata" , DrmManagerMethodId::GET_METADATA },
53 {"canHandle" , DrmManagerMethodId::CAN_HANDLE },
54 {"processDrmInfo" , DrmManagerMethodId::PROCESS_DRM_INFO },
55 {"acquireDrmInfo" , DrmManagerMethodId::ACQUIRE_DRM_INFO },
56 {"saveRights" , DrmManagerMethodId::SAVE_RIGHTS },
57 {"getOriginalMimeType", DrmManagerMethodId::GET_ORIGINAL_MIME_TYPE},
58 {"getDrmObjectType" , DrmManagerMethodId::GET_DRM_OBJECT_TYPE },
59 {"checkRightsStatus" , DrmManagerMethodId::CHECK_RIGHTS_STATUS },
60 {"removeRights" , DrmManagerMethodId::REMOVE_RIGHTS },
61 {"removeAllRights" , DrmManagerMethodId::REMOVE_ALL_RIGHTS },
62 {"openConvertSession" , DrmManagerMethodId::OPEN_CONVERT_SESSION },
63 {"openDecryptSession" , DrmManagerMethodId::OPEN_DECRYPT_SESSION }
64};
65
aimitakeshi27ed8ad2010-07-29 10:12:27 +090066DrmManager::DrmManager() :
67 mDecryptSessionId(0),
68 mConvertId(0) {
Henrik B Andersson13f7fe72012-10-26 15:15:15 +020069 srand(time(NULL));
70 memset(mUniqueIdArray, 0, sizeof(bool) * kMaxNumUniqueIds);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090071}
72
73DrmManager::~DrmManager() {
Robert Shih7bcf7922020-02-07 15:01:57 -080074 if (mMetricsLooper != NULL) {
75 mMetricsLooper->stop();
76 }
77 flushEngineMetrics();
aimitakeshi27ed8ad2010-07-29 10:12:27 +090078}
79
Robert Shih7bcf7922020-02-07 15:01:57 -080080void DrmManager::initMetricsLooper() {
81 if (mMetricsLooper != NULL) {
82 return;
83 }
84 mMetricsLooper = new ALooper;
85 mMetricsLooper->setName("DrmManagerMetricsLooper");
86 mMetricsLooper->start();
87 mMetricsLooper->registerHandler(this);
Robert Shihec056ae2019-08-17 01:54:04 -070088
Robert Shih7bcf7922020-02-07 15:01:57 -080089 sp<AMessage> msg = new AMessage(kWhatFlushMetrics, this);
90 msg->post(getMetricsFlushPeriodUs());
91}
Robert Shihec056ae2019-08-17 01:54:04 -070092
Robert Shih7bcf7922020-02-07 15:01:57 -080093void DrmManager::onMessageReceived(const sp<AMessage> &msg) {
94 switch (msg->what()) {
95 case kWhatFlushMetrics:
96 {
97 flushEngineMetrics();
98 msg->post(getMetricsFlushPeriodUs());
99 break;
100 }
101 default:
102 {
Alistair Delva5721cc42020-09-13 22:50:55 -0700103 ALOGW("Unrecognized message type: %u", msg->what());
Robert Shih7bcf7922020-02-07 15:01:57 -0800104 }
105 }
106}
107
108int64_t DrmManager::getMetricsFlushPeriodUs() {
Alistair Delva5721cc42020-09-13 22:50:55 -0700109 return 1000 * 1000 * std::max(1ll, (long long)property_get_int64("drmmanager.metrics.period", 86400));
Robert Shih7bcf7922020-02-07 15:01:57 -0800110}
111
112void DrmManager::recordEngineMetrics(
113 const char func[], const String8& plugInId8, const String8& mimeType) {
114 IDrmEngine& engine = mPlugInManager.getPlugIn(plugInId8);
Robert Shihec056ae2019-08-17 01:54:04 -0700115 std::unique_ptr<DrmSupportInfo> info(engine.getSupportInfo(0));
Robert Shih7bcf7922020-02-07 15:01:57 -0800116
117 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Tomasz Wasilczyk259940c2023-08-23 03:29:56 +0000118 std::string plugInId = std::filesystem::path(plugInId8.c_str()).stem();
Robert Shih7bcf7922020-02-07 15:01:57 -0800119 ALOGV("%d calling %s %s", callingUid, plugInId.c_str(), func);
120
121 Mutex::Autolock _l(mMetricsLock);
122 auto& metrics = mPluginMetrics[std::make_pair(callingUid, plugInId)];
123 if (metrics.mPluginId.empty()) {
124 metrics.mPluginId = plugInId;
125 metrics.mCallingUid = callingUid;
126 if (NULL != info) {
127 metrics.mDescription = info->getDescription().c_str();
128 }
Robert Shihec056ae2019-08-17 01:54:04 -0700129 }
130
Tomasz Wasilczykfd9ffd12023-08-14 17:56:22 +0000131 if (!mimeType.empty()) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800132 metrics.mMimeTypes.insert(mimeType.c_str());
Robert Shihec056ae2019-08-17 01:54:04 -0700133 } else if (NULL != info) {
134 DrmSupportInfo::MimeTypeIterator mimeIter = info->getMimeTypeIterator();
Robert Shihec056ae2019-08-17 01:54:04 -0700135 while (mimeIter.hasNext()) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800136 metrics.mMimeTypes.insert(mimeIter.next().c_str());
Robert Shihec056ae2019-08-17 01:54:04 -0700137 }
Robert Shihec056ae2019-08-17 01:54:04 -0700138 }
139
Robert Shih7bcf7922020-02-07 15:01:57 -0800140 size_t methodId = kMethodIdMap.at(func);
141 if (methodId < metrics.mMethodCounts.size()) {
142 metrics.mMethodCounts[methodId]++;
Robert Shihec056ae2019-08-17 01:54:04 -0700143 }
144}
145
Robert Shih7bcf7922020-02-07 15:01:57 -0800146void DrmManager::flushEngineMetrics() {
147 using namespace std::string_literals;
148 Mutex::Autolock _l(mMetricsLock);
149 for (auto kv : mPluginMetrics) {
150 DrmManagerMetrics& metrics = kv.second;
151 std::unique_ptr<mediametrics::Item> item(mediametrics::Item::create("drmmanager"));
152 item->setUid(metrics.mCallingUid);
153 item->setCString("plugin_id", metrics.mPluginId.c_str());
154 item->setCString("description", metrics.mDescription.c_str());
155
156 std::vector<std::string> mimeTypes(metrics.mMimeTypes.begin(), metrics.mMimeTypes.end());
157 std::string mimeTypesStr(mimeTypes.empty() ? "" : mimeTypes[0]);
158 for (size_t i = 1; i < mimeTypes.size() ; i++) {
159 mimeTypesStr.append(",").append(mimeTypes[i]);
160 }
161 item->setCString("mime_types", mimeTypesStr.c_str());
162
163 for (size_t i = 0; i < metrics.mMethodCounts.size() ; i++) {
164 item->setInt64(("method"s + std::to_string(i)).c_str(), metrics.mMethodCounts[i]);
165 }
166
167 if (!item->selfrecord()) {
168 ALOGE("Failed to record metrics");
169 }
170 }
171 mPluginMetrics.clear();
172}
173
Gloria Wang8f001512011-07-21 15:10:22 -0700174int DrmManager::addUniqueId(bool isNative) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800175 Mutex::Autolock _l(mLock);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900176
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200177 int uniqueId = -1;
178 int random = rand();
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900179
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200180 for (size_t index = 0; index < kMaxNumUniqueIds; ++index) {
181 int temp = (random + index) % kMaxNumUniqueIds;
182 if (!mUniqueIdArray[temp]) {
183 uniqueId = temp;
184 mUniqueIdArray[uniqueId] = true;
Gloria Wang8f001512011-07-21 15:10:22 -0700185
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200186 if (isNative) {
187 // set a flag to differentiate DrmManagerClient
188 // created from native side and java side
189 uniqueId |= 0x1000;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900190 }
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200191 break;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900192 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900193 }
Gloria Wang8f001512011-07-21 15:10:22 -0700194
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200195 // -1 indicates that no unique id can be allocated.
196 return uniqueId;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900197}
198
199void DrmManager::removeUniqueId(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800200 Mutex::Autolock _l(mLock);
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200201 if (uniqueId & 0x1000) {
202 // clear the flag for the native side.
203 uniqueId &= ~(0x1000);
204 }
205
206 if (uniqueId >= 0 && uniqueId < kMaxNumUniqueIds) {
207 mUniqueIdArray[uniqueId] = false;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900208 }
209}
210
Takeshi Aimie943f842010-10-08 23:05:49 +0900211status_t DrmManager::loadPlugIns() {
Robert Shih7ba9c992022-04-20 15:33:51 -0700212#if __LP64__
213 String8 pluginDirPath("/system/lib64/drm");
214#else
James Dong785ee062011-12-14 10:57:05 -0800215 String8 pluginDirPath("/system/lib/drm");
Robert Shih7ba9c992022-04-20 15:33:51 -0700216#endif
James Dong785ee062011-12-14 10:57:05 -0800217 loadPlugIns(pluginDirPath);
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700218 return DRM_NO_ERROR;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900219}
220
Takeshi Aimie943f842010-10-08 23:05:49 +0900221status_t DrmManager::loadPlugIns(const String8& plugInDirPath) {
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700222 mPlugInManager.loadPlugIns(plugInDirPath);
223 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700224 for (size_t i = 0; i < plugInPathList.size(); ++i) {
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700225 String8 plugInPath = plugInPathList[i];
226 DrmSupportInfo* info = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
227 if (NULL != info) {
228 if (mSupportInfoToPlugInIdMap.indexOfKey(*info) < 0) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900229 mSupportInfoToPlugInIdMap.add(*info, plugInPath);
230 }
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700231 delete info;
Takeshi Aimie943f842010-10-08 23:05:49 +0900232 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900233 }
Takeshi Aimie943f842010-10-08 23:05:49 +0900234 return DRM_NO_ERROR;
235}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900236
Takeshi Aimie943f842010-10-08 23:05:49 +0900237status_t DrmManager::unloadPlugIns() {
Gloria Wang6b610a32011-03-04 14:45:03 -0800238 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900239 mConvertSessionMap.clear();
240 mDecryptSessionMap.clear();
241 mPlugInManager.unloadPlugIns();
242 mSupportInfoToPlugInIdMap.clear();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900243 return DRM_NO_ERROR;
244}
245
246status_t DrmManager::setDrmServiceListener(
247 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
Gloria Wang0e0a5f92011-03-11 14:07:21 -0800248 Mutex::Autolock _l(mListenerLock);
Takeshi Aimic618b5a2010-11-30 16:27:42 +0900249 if (NULL != drmServiceListener.get()) {
250 mServiceListeners.add(uniqueId, drmServiceListener);
251 } else {
252 mServiceListeners.removeItem(uniqueId);
253 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900254 return DRM_NO_ERROR;
255}
256
Takeshi Aimie943f842010-10-08 23:05:49 +0900257void DrmManager::addClient(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800258 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900259 if (!mSupportInfoToPlugInIdMap.isEmpty()) {
260 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700261 for (size_t index = 0; index < plugInIdList.size(); index++) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900262 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
263 rDrmEngine.initialize(uniqueId);
264 rDrmEngine.setOnInfoListener(uniqueId, this);
265 }
266 }
267}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900268
Takeshi Aimie943f842010-10-08 23:05:49 +0900269void DrmManager::removeClient(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800270 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900271 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700272 for (size_t index = 0; index < plugInIdList.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900273 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
274 rDrmEngine.terminate(uniqueId);
275 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900276}
277
278DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800279 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700280 DrmConstraints *constraints = NULL;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900281 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
282 if (EMPTY_STRING != plugInId) {
283 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700284 constraints = rDrmEngine.getConstraints(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900285 }
Robert Shihec056ae2019-08-17 01:54:04 -0700286 if (NULL != constraints) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800287 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700288 }
289 return constraints;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900290}
291
Takeshi Aimi34738462010-11-16 13:56:11 +0900292DrmMetadata* DrmManager::getMetadata(int uniqueId, const String8* path) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800293 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700294 DrmMetadata *meta = NULL;
Takeshi Aimi34738462010-11-16 13:56:11 +0900295 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
296 if (EMPTY_STRING != plugInId) {
297 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700298 meta = rDrmEngine.getMetadata(uniqueId, path);
Takeshi Aimi34738462010-11-16 13:56:11 +0900299 }
Robert Shihec056ae2019-08-17 01:54:04 -0700300 if (NULL != meta) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800301 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700302 }
303 return meta;
Takeshi Aimi34738462010-11-16 13:56:11 +0900304}
305
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900306bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800307 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900308 const String8 plugInId = getSupportedPlugInId(mimeType);
309 bool result = (EMPTY_STRING != plugInId) ? true : false;
310
Robert Shihec056ae2019-08-17 01:54:04 -0700311 if (result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800312 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700313 }
314
Takeshi Aimie943f842010-10-08 23:05:49 +0900315 if (0 < path.length()) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900316 if (result) {
317 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
318 result = rDrmEngine.canHandle(uniqueId, path);
319 } else {
Tomasz Wasilczyk259940c2023-08-23 03:29:56 +0000320 const auto extension = std::filesystem::path(path.c_str()).extension();
321 if (!extension.empty()) {
Gloria Wang7f89d092011-03-02 12:33:00 -0800322 result = canHandle(uniqueId, path);
323 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900324 }
325 }
326 return result;
327}
328
329DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800330 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700331 DrmInfoStatus *infoStatus = NULL;
332 const String8 mimeType = drmInfo->getMimeType();
333 const String8 plugInId = getSupportedPlugInId(mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900334 if (EMPTY_STRING != plugInId) {
335 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700336 infoStatus = rDrmEngine.processDrmInfo(uniqueId, drmInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900337 }
Robert Shihec056ae2019-08-17 01:54:04 -0700338 if (NULL != infoStatus) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800339 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700340 }
341 return infoStatus;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900342}
343
344bool DrmManager::canHandle(int uniqueId, const String8& path) {
345 bool result = false;
346 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
347
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700348 for (size_t i = 0; i < plugInPathList.size(); ++i) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900349 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]);
350 result = rDrmEngine.canHandle(uniqueId, path);
351
352 if (result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800353 recordEngineMetrics(__func__, plugInPathList[i]);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900354 break;
355 }
356 }
357 return result;
358}
359
360DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800361 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700362 DrmInfo *info = NULL;
363 const String8 mimeType = drmInfoRequest->getMimeType();
364 const String8 plugInId = getSupportedPlugInId(mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900365 if (EMPTY_STRING != plugInId) {
366 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700367 info = rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900368 }
Robert Shihec056ae2019-08-17 01:54:04 -0700369 if (NULL != info) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800370 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700371 }
372 return info;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900373}
374
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900375status_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900376 const String8& rightsPath, const String8& contentPath) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800377 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700378 const String8 mimeType = drmRights.getMimeType();
379 const String8 plugInId = getSupportedPlugInId(mimeType);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900380 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900381 if (EMPTY_STRING != plugInId) {
382 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900383 result = rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900384 }
Robert Shihec056ae2019-08-17 01:54:04 -0700385 if (DRM_NO_ERROR == result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800386 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700387 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900388 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900389}
390
James Dongbf5b3b22012-07-30 17:57:39 -0700391String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800392 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700393 String8 mimeType(EMPTY_STRING);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900394 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
395 if (EMPTY_STRING != plugInId) {
396 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700397 mimeType = rDrmEngine.getOriginalMimeType(uniqueId, path, fd);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900398 }
Tomasz Wasilczykfd9ffd12023-08-14 17:56:22 +0000399 if (!mimeType.empty()) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800400 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700401 }
402 return mimeType;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900403}
404
405int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800406 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700407 int type = DrmObjectType::UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900408 const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
409 if (EMPTY_STRING != plugInId) {
410 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700411 type = rDrmEngine.getDrmObjectType(uniqueId, path, mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900412 }
Robert Shihec056ae2019-08-17 01:54:04 -0700413 if (DrmObjectType::UNKNOWN != type) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800414 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700415 }
416 return type;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900417}
418
419int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800420 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700421 int rightsStatus = RightsStatus::RIGHTS_INVALID;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900422 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
423 if (EMPTY_STRING != plugInId) {
424 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700425 rightsStatus = rDrmEngine.checkRightsStatus(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900426 }
Robert Shihec056ae2019-08-17 01:54:04 -0700427 if (RightsStatus::RIGHTS_INVALID != rightsStatus) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800428 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700429 }
430 return rightsStatus;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900431}
432
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900433status_t DrmManager::consumeRights(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700434 int uniqueId, sp<DecryptHandle>& decryptHandle, int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900435 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800436 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900437 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
438 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900439 result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900440 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900441 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900442}
443
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900444status_t DrmManager::setPlaybackStatus(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700445 int uniqueId, sp<DecryptHandle>& decryptHandle, int playbackStatus, int64_t position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900446 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800447 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900448 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
449 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900450 result = drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900451 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900452 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900453}
454
455bool DrmManager::validateAction(
456 int uniqueId, const String8& path, int action, const ActionDescription& description) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800457 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900458 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
459 if (EMPTY_STRING != plugInId) {
460 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
461 return rDrmEngine.validateAction(uniqueId, path, action, description);
462 }
463 return false;
464}
465
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900466status_t DrmManager::removeRights(int uniqueId, const String8& path) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800467 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900468 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900469 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900470 if (EMPTY_STRING != plugInId) {
471 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900472 result = rDrmEngine.removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900473 }
Robert Shihec056ae2019-08-17 01:54:04 -0700474 if (DRM_NO_ERROR == result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800475 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700476 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900477 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900478}
479
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900480status_t DrmManager::removeAllRights(int uniqueId) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900481 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900482 status_t result = DRM_ERROR_UNKNOWN;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700483 for (size_t index = 0; index < plugInIdList.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900484 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900485 result = rDrmEngine.removeAllRights(uniqueId);
486 if (DRM_NO_ERROR != result) {
487 break;
488 }
Robert Shih7bcf7922020-02-07 15:01:57 -0800489 recordEngineMetrics(__func__, plugInIdList[index]);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900490 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900491 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900492}
493
494int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800495 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900496 int convertId = -1;
497
498 const String8 plugInId = getSupportedPlugInId(mimeType);
499 if (EMPTY_STRING != plugInId) {
500 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
501
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900502 if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900503 ++mConvertId;
504 convertId = mConvertId;
505 mConvertSessionMap.add(convertId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800506 recordEngineMetrics(__func__, plugInId, mimeType);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900507 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900508 }
509 return convertId;
510}
511
512DrmConvertedStatus* DrmManager::convertData(
513 int uniqueId, int convertId, const DrmBuffer* inputData) {
514 DrmConvertedStatus *drmConvertedStatus = NULL;
515
Gloria Wang6b610a32011-03-04 14:45:03 -0800516 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900517 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
518 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
519 drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
520 }
521 return drmConvertedStatus;
522}
523
524DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800525 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900526 DrmConvertedStatus *drmConvertedStatus = NULL;
527
528 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
529 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
530 drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId);
531 mConvertSessionMap.removeItem(convertId);
532 }
533 return drmConvertedStatus;
534}
535
536status_t DrmManager::getAllSupportInfo(
Aurimas Liutikasb2231172016-02-12 16:57:08 -0800537 int /* uniqueId */, int* length, DrmSupportInfo** drmSupportInfoArray) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800538 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900539 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
540 int size = plugInPathList.size();
541 int validPlugins = 0;
542
543 if (0 < size) {
544 Vector<DrmSupportInfo> drmSupportInfoList;
545
546 for (int i = 0; i < size; ++i) {
547 String8 plugInPath = plugInPathList[i];
548 DrmSupportInfo* drmSupportInfo
Takeshi Aimie943f842010-10-08 23:05:49 +0900549 = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900550 if (NULL != drmSupportInfo) {
551 drmSupportInfoList.add(*drmSupportInfo);
552 delete drmSupportInfo; drmSupportInfo = NULL;
553 }
554 }
555
556 validPlugins = drmSupportInfoList.size();
557 if (0 < validPlugins) {
558 *drmSupportInfoArray = new DrmSupportInfo[validPlugins];
559 for (int i = 0; i < validPlugins; ++i) {
560 (*drmSupportInfoArray)[i] = drmSupportInfoList[i];
561 }
562 }
563 }
564 *length = validPlugins;
565 return DRM_NO_ERROR;
566}
567
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700568sp<DecryptHandle> DrmManager::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800569 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
570
Takeshi Aimie943f842010-10-08 23:05:49 +0900571 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900572 status_t result = DRM_ERROR_CANNOT_HANDLE;
573 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
574
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700575 sp<DecryptHandle> handle = new DecryptHandle();
576 if (NULL != handle.get()) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900577 handle->decryptId = mDecryptSessionId + 1;
578
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700579 for (size_t index = 0; index < plugInIdList.size(); index++) {
Chih-Hung Hsieh8c0164c2016-08-09 14:20:59 -0700580 const String8& plugInId = plugInIdList.itemAt(index);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900581 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dong9d2f3862012-01-10 08:24:37 -0800582 result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length, mime);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900583
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900584 if (DRM_NO_ERROR == result) {
585 ++mDecryptSessionId;
586 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800587 recordEngineMetrics(__func__, plugInId, String8(mime));
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900588 break;
589 }
590 }
591 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900592 if (DRM_NO_ERROR != result) {
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700593 handle.clear();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900594 }
Takeshi Aimie943f842010-10-08 23:05:49 +0900595 return handle;
596}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900597
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700598sp<DecryptHandle> DrmManager::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800599 int uniqueId, const char* uri, const char* mime) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900600 Mutex::Autolock _l(mDecryptLock);
601 status_t result = DRM_ERROR_CANNOT_HANDLE;
602 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
603
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700604 sp<DecryptHandle> handle = new DecryptHandle();
605 if (NULL != handle.get()) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900606 handle->decryptId = mDecryptSessionId + 1;
607
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700608 for (size_t index = 0; index < plugInIdList.size(); index++) {
Chih-Hung Hsieh8c0164c2016-08-09 14:20:59 -0700609 const String8& plugInId = plugInIdList.itemAt(index);
Takeshi Aimie943f842010-10-08 23:05:49 +0900610 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dong9d2f3862012-01-10 08:24:37 -0800611 result = rDrmEngine.openDecryptSession(uniqueId, handle, uri, mime);
Takeshi Aimie943f842010-10-08 23:05:49 +0900612
613 if (DRM_NO_ERROR == result) {
614 ++mDecryptSessionId;
615 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800616 recordEngineMetrics(__func__, plugInId, String8(mime));
Takeshi Aimie943f842010-10-08 23:05:49 +0900617 break;
618 }
619 }
620 }
621 if (DRM_NO_ERROR != result) {
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700622 handle.clear();
Steve Block3856b092011-10-20 11:56:00 +0100623 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
Takeshi Aimie943f842010-10-08 23:05:49 +0900624 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900625 return handle;
626}
627
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700628sp<DecryptHandle> DrmManager::openDecryptSession(
Kei Takahashicba7b322012-01-18 17:10:19 +0900629 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
630 Mutex::Autolock _l(mDecryptLock);
631 status_t result = DRM_ERROR_CANNOT_HANDLE;
632 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
633
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700634 sp<DecryptHandle> handle = new DecryptHandle();
635 if (NULL != handle.get()) {
Kei Takahashicba7b322012-01-18 17:10:19 +0900636 handle->decryptId = mDecryptSessionId + 1;
637
638 for (size_t index = 0; index < plugInIdList.size(); index++) {
Chih-Hung Hsieh8c0164c2016-08-09 14:20:59 -0700639 const String8& plugInId = plugInIdList.itemAt(index);
Kei Takahashicba7b322012-01-18 17:10:19 +0900640 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
641 result = rDrmEngine.openDecryptSession(uniqueId, handle, buf, mimeType);
642
643 if (DRM_NO_ERROR == result) {
644 ++mDecryptSessionId;
645 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800646 recordEngineMetrics(__func__, plugInId, mimeType);
Kei Takahashicba7b322012-01-18 17:10:19 +0900647 break;
648 }
649 }
650 }
651 if (DRM_NO_ERROR != result) {
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700652 handle.clear();
Kei Takahashicba7b322012-01-18 17:10:19 +0900653 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
654 }
655 return handle;
656}
657
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700658status_t DrmManager::closeDecryptSession(int uniqueId, sp<DecryptHandle>& decryptHandle) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900659 Mutex::Autolock _l(mDecryptLock);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900660 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900661 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
662 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900663 result = drmEngine->closeDecryptSession(uniqueId, decryptHandle);
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700664 if (DRM_NO_ERROR == result && NULL != decryptHandle.get()) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900665 mDecryptSessionMap.removeItem(decryptHandle->decryptId);
666 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900667 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900668 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900669}
670
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900671status_t DrmManager::initializeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700672 int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId,
673 const DrmBuffer* headerInfo) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900674 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800675 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900676 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
677 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900678 result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900679 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900680 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900681}
682
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700683status_t DrmManager::decrypt(int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId,
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900684 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
685 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800686
687 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900688 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
689 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900690 result = drmEngine->decrypt(
691 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900692 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900693 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900694}
695
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900696status_t DrmManager::finalizeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700697 int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900698 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800699 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900700 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
701 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900702 result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900703 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900704 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900705}
706
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700707ssize_t DrmManager::pread(int uniqueId, sp<DecryptHandle>& decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800708 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900709 ssize_t result = DECRYPT_FILE_ERROR;
710
Gloria Wang6b610a32011-03-04 14:45:03 -0800711 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900712 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
713 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
714 result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
715 }
716 return result;
717}
718
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900719String8 DrmManager::getSupportedPlugInId(
720 int uniqueId, const String8& path, const String8& mimeType) {
721 String8 plugInId("");
722
723 if (EMPTY_STRING != mimeType) {
724 plugInId = getSupportedPlugInId(mimeType);
725 } else {
726 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
727 }
728 return plugInId;
729}
730
731String8 DrmManager::getSupportedPlugInId(const String8& mimeType) {
732 String8 plugInId("");
733
734 if (EMPTY_STRING != mimeType) {
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700735 for (size_t index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900736 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
737
738 if (drmSupportInfo.isSupportedMimeType(mimeType)) {
739 plugInId = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
740 break;
741 }
742 }
743 }
744 return plugInId;
745}
746
747String8 DrmManager::getSupportedPlugInIdFromPath(int uniqueId, const String8& path) {
748 String8 plugInId("");
Tomasz Wasilczyk259940c2023-08-23 03:29:56 +0000749 const String8 fileSuffix(std::filesystem::path(path.c_str()).extension().c_str());
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900750
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700751 for (size_t index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900752 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
753
754 if (drmSupportInfo.isSupportedFileSuffix(fileSuffix)) {
755 String8 key = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
756 IDrmEngine& drmEngine = mPlugInManager.getPlugIn(key);
757
758 if (drmEngine.canHandle(uniqueId, path)) {
759 plugInId = key;
760 break;
761 }
762 }
763 }
764 return plugInId;
765}
766
767void DrmManager::onInfo(const DrmInfoEvent& event) {
Gloria Wang0e0a5f92011-03-11 14:07:21 -0800768 Mutex::Autolock _l(mListenerLock);
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700769 for (size_t index = 0; index < mServiceListeners.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900770 int uniqueId = mServiceListeners.keyAt(index);
771
772 if (uniqueId == event.getUniqueId()) {
773 sp<IDrmServiceListener> serviceListener = mServiceListeners.valueFor(uniqueId);
774 serviceListener->notify(event);
775 }
776 }
777}
778