blob: f7989bd3661460c5b203e90dc07e493df860338e [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>
42
Chih-Hung Hsieh92c6b822016-05-17 15:20:14 -070043#define DECRYPT_FILE_ERROR (-1)
aimitakeshi27ed8ad2010-07-29 10:12:27 +090044
45using namespace android;
46
47const String8 DrmManager::EMPTY_STRING("");
48
Robert Shih7bcf7922020-02-07 15:01:57 -080049const std::map<const char*, size_t> DrmManager::kMethodIdMap {
50 {"getConstraints" , DrmManagerMethodId::GET_CONSTRAINTS },
51 {"getMetadata" , DrmManagerMethodId::GET_METADATA },
52 {"canHandle" , DrmManagerMethodId::CAN_HANDLE },
53 {"processDrmInfo" , DrmManagerMethodId::PROCESS_DRM_INFO },
54 {"acquireDrmInfo" , DrmManagerMethodId::ACQUIRE_DRM_INFO },
55 {"saveRights" , DrmManagerMethodId::SAVE_RIGHTS },
56 {"getOriginalMimeType", DrmManagerMethodId::GET_ORIGINAL_MIME_TYPE},
57 {"getDrmObjectType" , DrmManagerMethodId::GET_DRM_OBJECT_TYPE },
58 {"checkRightsStatus" , DrmManagerMethodId::CHECK_RIGHTS_STATUS },
59 {"removeRights" , DrmManagerMethodId::REMOVE_RIGHTS },
60 {"removeAllRights" , DrmManagerMethodId::REMOVE_ALL_RIGHTS },
61 {"openConvertSession" , DrmManagerMethodId::OPEN_CONVERT_SESSION },
62 {"openDecryptSession" , DrmManagerMethodId::OPEN_DECRYPT_SESSION }
63};
64
aimitakeshi27ed8ad2010-07-29 10:12:27 +090065DrmManager::DrmManager() :
66 mDecryptSessionId(0),
67 mConvertId(0) {
Henrik B Andersson13f7fe72012-10-26 15:15:15 +020068 srand(time(NULL));
69 memset(mUniqueIdArray, 0, sizeof(bool) * kMaxNumUniqueIds);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090070}
71
72DrmManager::~DrmManager() {
Robert Shih7bcf7922020-02-07 15:01:57 -080073 if (mMetricsLooper != NULL) {
74 mMetricsLooper->stop();
75 }
76 flushEngineMetrics();
aimitakeshi27ed8ad2010-07-29 10:12:27 +090077}
78
Robert Shih7bcf7922020-02-07 15:01:57 -080079void DrmManager::initMetricsLooper() {
80 if (mMetricsLooper != NULL) {
81 return;
82 }
83 mMetricsLooper = new ALooper;
84 mMetricsLooper->setName("DrmManagerMetricsLooper");
85 mMetricsLooper->start();
86 mMetricsLooper->registerHandler(this);
Robert Shihec056ae2019-08-17 01:54:04 -070087
Robert Shih7bcf7922020-02-07 15:01:57 -080088 sp<AMessage> msg = new AMessage(kWhatFlushMetrics, this);
89 msg->post(getMetricsFlushPeriodUs());
90}
Robert Shihec056ae2019-08-17 01:54:04 -070091
Robert Shih7bcf7922020-02-07 15:01:57 -080092void DrmManager::onMessageReceived(const sp<AMessage> &msg) {
93 switch (msg->what()) {
94 case kWhatFlushMetrics:
95 {
96 flushEngineMetrics();
97 msg->post(getMetricsFlushPeriodUs());
98 break;
99 }
100 default:
101 {
Alistair Delva5721cc42020-09-13 22:50:55 -0700102 ALOGW("Unrecognized message type: %u", msg->what());
Robert Shih7bcf7922020-02-07 15:01:57 -0800103 }
104 }
105}
106
107int64_t DrmManager::getMetricsFlushPeriodUs() {
Alistair Delva5721cc42020-09-13 22:50:55 -0700108 return 1000 * 1000 * std::max(1ll, (long long)property_get_int64("drmmanager.metrics.period", 86400));
Robert Shih7bcf7922020-02-07 15:01:57 -0800109}
110
111void DrmManager::recordEngineMetrics(
112 const char func[], const String8& plugInId8, const String8& mimeType) {
113 IDrmEngine& engine = mPlugInManager.getPlugIn(plugInId8);
Robert Shihec056ae2019-08-17 01:54:04 -0700114 std::unique_ptr<DrmSupportInfo> info(engine.getSupportInfo(0));
Robert Shih7bcf7922020-02-07 15:01:57 -0800115
116 uid_t callingUid = IPCThreadState::self()->getCallingUid();
117 std::string plugInId(plugInId8.getPathLeaf().getBasePath().c_str());
118 ALOGV("%d calling %s %s", callingUid, plugInId.c_str(), func);
119
120 Mutex::Autolock _l(mMetricsLock);
121 auto& metrics = mPluginMetrics[std::make_pair(callingUid, plugInId)];
122 if (metrics.mPluginId.empty()) {
123 metrics.mPluginId = plugInId;
124 metrics.mCallingUid = callingUid;
125 if (NULL != info) {
126 metrics.mDescription = info->getDescription().c_str();
127 }
Robert Shihec056ae2019-08-17 01:54:04 -0700128 }
129
130 if (!mimeType.isEmpty()) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800131 metrics.mMimeTypes.insert(mimeType.c_str());
Robert Shihec056ae2019-08-17 01:54:04 -0700132 } else if (NULL != info) {
133 DrmSupportInfo::MimeTypeIterator mimeIter = info->getMimeTypeIterator();
Robert Shihec056ae2019-08-17 01:54:04 -0700134 while (mimeIter.hasNext()) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800135 metrics.mMimeTypes.insert(mimeIter.next().c_str());
Robert Shihec056ae2019-08-17 01:54:04 -0700136 }
Robert Shihec056ae2019-08-17 01:54:04 -0700137 }
138
Robert Shih7bcf7922020-02-07 15:01:57 -0800139 size_t methodId = kMethodIdMap.at(func);
140 if (methodId < metrics.mMethodCounts.size()) {
141 metrics.mMethodCounts[methodId]++;
Robert Shihec056ae2019-08-17 01:54:04 -0700142 }
143}
144
Robert Shih7bcf7922020-02-07 15:01:57 -0800145void DrmManager::flushEngineMetrics() {
146 using namespace std::string_literals;
147 Mutex::Autolock _l(mMetricsLock);
148 for (auto kv : mPluginMetrics) {
149 DrmManagerMetrics& metrics = kv.second;
150 std::unique_ptr<mediametrics::Item> item(mediametrics::Item::create("drmmanager"));
151 item->setUid(metrics.mCallingUid);
152 item->setCString("plugin_id", metrics.mPluginId.c_str());
153 item->setCString("description", metrics.mDescription.c_str());
154
155 std::vector<std::string> mimeTypes(metrics.mMimeTypes.begin(), metrics.mMimeTypes.end());
156 std::string mimeTypesStr(mimeTypes.empty() ? "" : mimeTypes[0]);
157 for (size_t i = 1; i < mimeTypes.size() ; i++) {
158 mimeTypesStr.append(",").append(mimeTypes[i]);
159 }
160 item->setCString("mime_types", mimeTypesStr.c_str());
161
162 for (size_t i = 0; i < metrics.mMethodCounts.size() ; i++) {
163 item->setInt64(("method"s + std::to_string(i)).c_str(), metrics.mMethodCounts[i]);
164 }
165
166 if (!item->selfrecord()) {
167 ALOGE("Failed to record metrics");
168 }
169 }
170 mPluginMetrics.clear();
171}
172
Gloria Wang8f001512011-07-21 15:10:22 -0700173int DrmManager::addUniqueId(bool isNative) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800174 Mutex::Autolock _l(mLock);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900175
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200176 int uniqueId = -1;
177 int random = rand();
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900178
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200179 for (size_t index = 0; index < kMaxNumUniqueIds; ++index) {
180 int temp = (random + index) % kMaxNumUniqueIds;
181 if (!mUniqueIdArray[temp]) {
182 uniqueId = temp;
183 mUniqueIdArray[uniqueId] = true;
Gloria Wang8f001512011-07-21 15:10:22 -0700184
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200185 if (isNative) {
186 // set a flag to differentiate DrmManagerClient
187 // created from native side and java side
188 uniqueId |= 0x1000;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900189 }
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200190 break;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900191 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900192 }
Gloria Wang8f001512011-07-21 15:10:22 -0700193
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200194 // -1 indicates that no unique id can be allocated.
195 return uniqueId;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900196}
197
198void DrmManager::removeUniqueId(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800199 Mutex::Autolock _l(mLock);
Henrik B Andersson13f7fe72012-10-26 15:15:15 +0200200 if (uniqueId & 0x1000) {
201 // clear the flag for the native side.
202 uniqueId &= ~(0x1000);
203 }
204
205 if (uniqueId >= 0 && uniqueId < kMaxNumUniqueIds) {
206 mUniqueIdArray[uniqueId] = false;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900207 }
208}
209
Takeshi Aimie943f842010-10-08 23:05:49 +0900210status_t DrmManager::loadPlugIns() {
Robert Shih7ba9c992022-04-20 15:33:51 -0700211#if __LP64__
212 String8 pluginDirPath("/system/lib64/drm");
213#else
James Dong785ee062011-12-14 10:57:05 -0800214 String8 pluginDirPath("/system/lib/drm");
Robert Shih7ba9c992022-04-20 15:33:51 -0700215#endif
James Dong785ee062011-12-14 10:57:05 -0800216 loadPlugIns(pluginDirPath);
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700217 return DRM_NO_ERROR;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900218}
219
Takeshi Aimie943f842010-10-08 23:05:49 +0900220status_t DrmManager::loadPlugIns(const String8& plugInDirPath) {
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700221 mPlugInManager.loadPlugIns(plugInDirPath);
222 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700223 for (size_t i = 0; i < plugInPathList.size(); ++i) {
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700224 String8 plugInPath = plugInPathList[i];
225 DrmSupportInfo* info = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
226 if (NULL != info) {
227 if (mSupportInfoToPlugInIdMap.indexOfKey(*info) < 0) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900228 mSupportInfoToPlugInIdMap.add(*info, plugInPath);
229 }
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700230 delete info;
Takeshi Aimie943f842010-10-08 23:05:49 +0900231 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900232 }
Takeshi Aimie943f842010-10-08 23:05:49 +0900233 return DRM_NO_ERROR;
234}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900235
Takeshi Aimie943f842010-10-08 23:05:49 +0900236status_t DrmManager::unloadPlugIns() {
Gloria Wang6b610a32011-03-04 14:45:03 -0800237 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900238 mConvertSessionMap.clear();
239 mDecryptSessionMap.clear();
240 mPlugInManager.unloadPlugIns();
241 mSupportInfoToPlugInIdMap.clear();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900242 return DRM_NO_ERROR;
243}
244
245status_t DrmManager::setDrmServiceListener(
246 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
Gloria Wang0e0a5f92011-03-11 14:07:21 -0800247 Mutex::Autolock _l(mListenerLock);
Takeshi Aimic618b5a2010-11-30 16:27:42 +0900248 if (NULL != drmServiceListener.get()) {
249 mServiceListeners.add(uniqueId, drmServiceListener);
250 } else {
251 mServiceListeners.removeItem(uniqueId);
252 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900253 return DRM_NO_ERROR;
254}
255
Takeshi Aimie943f842010-10-08 23:05:49 +0900256void DrmManager::addClient(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800257 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900258 if (!mSupportInfoToPlugInIdMap.isEmpty()) {
259 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700260 for (size_t index = 0; index < plugInIdList.size(); index++) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900261 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
262 rDrmEngine.initialize(uniqueId);
263 rDrmEngine.setOnInfoListener(uniqueId, this);
264 }
265 }
266}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900267
Takeshi Aimie943f842010-10-08 23:05:49 +0900268void DrmManager::removeClient(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800269 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900270 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700271 for (size_t index = 0; index < plugInIdList.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900272 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
273 rDrmEngine.terminate(uniqueId);
274 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900275}
276
277DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800278 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700279 DrmConstraints *constraints = NULL;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900280 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
281 if (EMPTY_STRING != plugInId) {
282 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700283 constraints = rDrmEngine.getConstraints(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900284 }
Robert Shihec056ae2019-08-17 01:54:04 -0700285 if (NULL != constraints) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800286 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700287 }
288 return constraints;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900289}
290
Takeshi Aimi34738462010-11-16 13:56:11 +0900291DrmMetadata* DrmManager::getMetadata(int uniqueId, const String8* path) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800292 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700293 DrmMetadata *meta = NULL;
Takeshi Aimi34738462010-11-16 13:56:11 +0900294 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
295 if (EMPTY_STRING != plugInId) {
296 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700297 meta = rDrmEngine.getMetadata(uniqueId, path);
Takeshi Aimi34738462010-11-16 13:56:11 +0900298 }
Robert Shihec056ae2019-08-17 01:54:04 -0700299 if (NULL != meta) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800300 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700301 }
302 return meta;
Takeshi Aimi34738462010-11-16 13:56:11 +0900303}
304
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900305bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800306 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900307 const String8 plugInId = getSupportedPlugInId(mimeType);
308 bool result = (EMPTY_STRING != plugInId) ? true : false;
309
Robert Shihec056ae2019-08-17 01:54:04 -0700310 if (result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800311 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700312 }
313
Takeshi Aimie943f842010-10-08 23:05:49 +0900314 if (0 < path.length()) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900315 if (result) {
316 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
317 result = rDrmEngine.canHandle(uniqueId, path);
318 } else {
Gloria Wang7f89d092011-03-02 12:33:00 -0800319 String8 extension = path.getPathExtension();
320 if (String8("") != extension) {
321 result = canHandle(uniqueId, path);
322 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900323 }
324 }
325 return result;
326}
327
328DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800329 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700330 DrmInfoStatus *infoStatus = NULL;
331 const String8 mimeType = drmInfo->getMimeType();
332 const String8 plugInId = getSupportedPlugInId(mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900333 if (EMPTY_STRING != plugInId) {
334 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700335 infoStatus = rDrmEngine.processDrmInfo(uniqueId, drmInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900336 }
Robert Shihec056ae2019-08-17 01:54:04 -0700337 if (NULL != infoStatus) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800338 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700339 }
340 return infoStatus;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900341}
342
343bool DrmManager::canHandle(int uniqueId, const String8& path) {
344 bool result = false;
345 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
346
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700347 for (size_t i = 0; i < plugInPathList.size(); ++i) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900348 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]);
349 result = rDrmEngine.canHandle(uniqueId, path);
350
351 if (result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800352 recordEngineMetrics(__func__, plugInPathList[i]);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900353 break;
354 }
355 }
356 return result;
357}
358
359DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800360 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700361 DrmInfo *info = NULL;
362 const String8 mimeType = drmInfoRequest->getMimeType();
363 const String8 plugInId = getSupportedPlugInId(mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900364 if (EMPTY_STRING != plugInId) {
365 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700366 info = rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900367 }
Robert Shihec056ae2019-08-17 01:54:04 -0700368 if (NULL != info) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800369 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700370 }
371 return info;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900372}
373
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900374status_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900375 const String8& rightsPath, const String8& contentPath) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800376 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700377 const String8 mimeType = drmRights.getMimeType();
378 const String8 plugInId = getSupportedPlugInId(mimeType);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900379 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900380 if (EMPTY_STRING != plugInId) {
381 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900382 result = rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900383 }
Robert Shihec056ae2019-08-17 01:54:04 -0700384 if (DRM_NO_ERROR == result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800385 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700386 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900387 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900388}
389
James Dongbf5b3b22012-07-30 17:57:39 -0700390String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800391 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700392 String8 mimeType(EMPTY_STRING);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900393 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
394 if (EMPTY_STRING != plugInId) {
395 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700396 mimeType = rDrmEngine.getOriginalMimeType(uniqueId, path, fd);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900397 }
Robert Shihec056ae2019-08-17 01:54:04 -0700398 if (!mimeType.isEmpty()) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800399 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700400 }
401 return mimeType;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900402}
403
404int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800405 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700406 int type = DrmObjectType::UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900407 const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
408 if (EMPTY_STRING != plugInId) {
409 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700410 type = rDrmEngine.getDrmObjectType(uniqueId, path, mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900411 }
Robert Shihec056ae2019-08-17 01:54:04 -0700412 if (DrmObjectType::UNKNOWN != type) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800413 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700414 }
415 return type;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900416}
417
418int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800419 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700420 int rightsStatus = RightsStatus::RIGHTS_INVALID;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900421 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
422 if (EMPTY_STRING != plugInId) {
423 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700424 rightsStatus = rDrmEngine.checkRightsStatus(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900425 }
Robert Shihec056ae2019-08-17 01:54:04 -0700426 if (RightsStatus::RIGHTS_INVALID != rightsStatus) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800427 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700428 }
429 return rightsStatus;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900430}
431
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900432status_t DrmManager::consumeRights(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700433 int uniqueId, sp<DecryptHandle>& decryptHandle, int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900434 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800435 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900436 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
437 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900438 result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900439 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900440 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900441}
442
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900443status_t DrmManager::setPlaybackStatus(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700444 int uniqueId, sp<DecryptHandle>& decryptHandle, int playbackStatus, int64_t position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900445 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800446 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900447 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
448 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900449 result = drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900450 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900451 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900452}
453
454bool DrmManager::validateAction(
455 int uniqueId, const String8& path, int action, const ActionDescription& description) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800456 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900457 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
458 if (EMPTY_STRING != plugInId) {
459 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
460 return rDrmEngine.validateAction(uniqueId, path, action, description);
461 }
462 return false;
463}
464
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900465status_t DrmManager::removeRights(int uniqueId, const String8& path) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800466 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900467 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900468 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900469 if (EMPTY_STRING != plugInId) {
470 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900471 result = rDrmEngine.removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900472 }
Robert Shihec056ae2019-08-17 01:54:04 -0700473 if (DRM_NO_ERROR == result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800474 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700475 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900476 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900477}
478
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900479status_t DrmManager::removeAllRights(int uniqueId) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900480 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900481 status_t result = DRM_ERROR_UNKNOWN;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700482 for (size_t index = 0; index < plugInIdList.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900483 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900484 result = rDrmEngine.removeAllRights(uniqueId);
485 if (DRM_NO_ERROR != result) {
486 break;
487 }
Robert Shih7bcf7922020-02-07 15:01:57 -0800488 recordEngineMetrics(__func__, plugInIdList[index]);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900489 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900490 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900491}
492
493int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800494 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900495 int convertId = -1;
496
497 const String8 plugInId = getSupportedPlugInId(mimeType);
498 if (EMPTY_STRING != plugInId) {
499 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
500
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900501 if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900502 ++mConvertId;
503 convertId = mConvertId;
504 mConvertSessionMap.add(convertId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800505 recordEngineMetrics(__func__, plugInId, mimeType);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900506 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900507 }
508 return convertId;
509}
510
511DrmConvertedStatus* DrmManager::convertData(
512 int uniqueId, int convertId, const DrmBuffer* inputData) {
513 DrmConvertedStatus *drmConvertedStatus = NULL;
514
Gloria Wang6b610a32011-03-04 14:45:03 -0800515 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900516 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
517 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
518 drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
519 }
520 return drmConvertedStatus;
521}
522
523DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800524 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900525 DrmConvertedStatus *drmConvertedStatus = NULL;
526
527 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
528 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
529 drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId);
530 mConvertSessionMap.removeItem(convertId);
531 }
532 return drmConvertedStatus;
533}
534
535status_t DrmManager::getAllSupportInfo(
Aurimas Liutikasb2231172016-02-12 16:57:08 -0800536 int /* uniqueId */, int* length, DrmSupportInfo** drmSupportInfoArray) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800537 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900538 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
539 int size = plugInPathList.size();
540 int validPlugins = 0;
541
542 if (0 < size) {
543 Vector<DrmSupportInfo> drmSupportInfoList;
544
545 for (int i = 0; i < size; ++i) {
546 String8 plugInPath = plugInPathList[i];
547 DrmSupportInfo* drmSupportInfo
Takeshi Aimie943f842010-10-08 23:05:49 +0900548 = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900549 if (NULL != drmSupportInfo) {
550 drmSupportInfoList.add(*drmSupportInfo);
551 delete drmSupportInfo; drmSupportInfo = NULL;
552 }
553 }
554
555 validPlugins = drmSupportInfoList.size();
556 if (0 < validPlugins) {
557 *drmSupportInfoArray = new DrmSupportInfo[validPlugins];
558 for (int i = 0; i < validPlugins; ++i) {
559 (*drmSupportInfoArray)[i] = drmSupportInfoList[i];
560 }
561 }
562 }
563 *length = validPlugins;
564 return DRM_NO_ERROR;
565}
566
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700567sp<DecryptHandle> DrmManager::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800568 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
569
Takeshi Aimie943f842010-10-08 23:05:49 +0900570 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900571 status_t result = DRM_ERROR_CANNOT_HANDLE;
572 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
573
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700574 sp<DecryptHandle> handle = new DecryptHandle();
575 if (NULL != handle.get()) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900576 handle->decryptId = mDecryptSessionId + 1;
577
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700578 for (size_t index = 0; index < plugInIdList.size(); index++) {
Chih-Hung Hsieh8c0164c2016-08-09 14:20:59 -0700579 const String8& plugInId = plugInIdList.itemAt(index);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900580 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dong9d2f3862012-01-10 08:24:37 -0800581 result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length, mime);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900582
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900583 if (DRM_NO_ERROR == result) {
584 ++mDecryptSessionId;
585 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800586 recordEngineMetrics(__func__, plugInId, String8(mime));
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900587 break;
588 }
589 }
590 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900591 if (DRM_NO_ERROR != result) {
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700592 handle.clear();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900593 }
Takeshi Aimie943f842010-10-08 23:05:49 +0900594 return handle;
595}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900596
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700597sp<DecryptHandle> DrmManager::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800598 int uniqueId, const char* uri, const char* mime) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900599 Mutex::Autolock _l(mDecryptLock);
600 status_t result = DRM_ERROR_CANNOT_HANDLE;
601 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
602
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700603 sp<DecryptHandle> handle = new DecryptHandle();
604 if (NULL != handle.get()) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900605 handle->decryptId = mDecryptSessionId + 1;
606
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700607 for (size_t index = 0; index < plugInIdList.size(); index++) {
Chih-Hung Hsieh8c0164c2016-08-09 14:20:59 -0700608 const String8& plugInId = plugInIdList.itemAt(index);
Takeshi Aimie943f842010-10-08 23:05:49 +0900609 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dong9d2f3862012-01-10 08:24:37 -0800610 result = rDrmEngine.openDecryptSession(uniqueId, handle, uri, mime);
Takeshi Aimie943f842010-10-08 23:05:49 +0900611
612 if (DRM_NO_ERROR == result) {
613 ++mDecryptSessionId;
614 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800615 recordEngineMetrics(__func__, plugInId, String8(mime));
Takeshi Aimie943f842010-10-08 23:05:49 +0900616 break;
617 }
618 }
619 }
620 if (DRM_NO_ERROR != result) {
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700621 handle.clear();
Steve Block3856b092011-10-20 11:56:00 +0100622 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
Takeshi Aimie943f842010-10-08 23:05:49 +0900623 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900624 return handle;
625}
626
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700627sp<DecryptHandle> DrmManager::openDecryptSession(
Kei Takahashicba7b322012-01-18 17:10:19 +0900628 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
629 Mutex::Autolock _l(mDecryptLock);
630 status_t result = DRM_ERROR_CANNOT_HANDLE;
631 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
632
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700633 sp<DecryptHandle> handle = new DecryptHandle();
634 if (NULL != handle.get()) {
Kei Takahashicba7b322012-01-18 17:10:19 +0900635 handle->decryptId = mDecryptSessionId + 1;
636
637 for (size_t index = 0; index < plugInIdList.size(); index++) {
Chih-Hung Hsieh8c0164c2016-08-09 14:20:59 -0700638 const String8& plugInId = plugInIdList.itemAt(index);
Kei Takahashicba7b322012-01-18 17:10:19 +0900639 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
640 result = rDrmEngine.openDecryptSession(uniqueId, handle, buf, mimeType);
641
642 if (DRM_NO_ERROR == result) {
643 ++mDecryptSessionId;
644 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800645 recordEngineMetrics(__func__, plugInId, mimeType);
Kei Takahashicba7b322012-01-18 17:10:19 +0900646 break;
647 }
648 }
649 }
650 if (DRM_NO_ERROR != result) {
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700651 handle.clear();
Kei Takahashicba7b322012-01-18 17:10:19 +0900652 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
653 }
654 return handle;
655}
656
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700657status_t DrmManager::closeDecryptSession(int uniqueId, sp<DecryptHandle>& decryptHandle) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900658 Mutex::Autolock _l(mDecryptLock);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900659 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900660 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
661 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900662 result = drmEngine->closeDecryptSession(uniqueId, decryptHandle);
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700663 if (DRM_NO_ERROR == result && NULL != decryptHandle.get()) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900664 mDecryptSessionMap.removeItem(decryptHandle->decryptId);
665 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900666 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900667 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900668}
669
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900670status_t DrmManager::initializeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700671 int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId,
672 const DrmBuffer* headerInfo) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900673 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800674 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900675 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
676 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900677 result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900678 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900679 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900680}
681
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700682status_t DrmManager::decrypt(int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId,
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900683 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
684 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800685
686 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900687 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
688 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900689 result = drmEngine->decrypt(
690 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900691 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900692 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900693}
694
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900695status_t DrmManager::finalizeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700696 int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900697 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800698 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900699 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
700 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900701 result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900702 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900703 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900704}
705
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700706ssize_t DrmManager::pread(int uniqueId, sp<DecryptHandle>& decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800707 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900708 ssize_t result = DECRYPT_FILE_ERROR;
709
Gloria Wang6b610a32011-03-04 14:45:03 -0800710 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900711 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
712 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
713 result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
714 }
715 return result;
716}
717
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900718String8 DrmManager::getSupportedPlugInId(
719 int uniqueId, const String8& path, const String8& mimeType) {
720 String8 plugInId("");
721
722 if (EMPTY_STRING != mimeType) {
723 plugInId = getSupportedPlugInId(mimeType);
724 } else {
725 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
726 }
727 return plugInId;
728}
729
730String8 DrmManager::getSupportedPlugInId(const String8& mimeType) {
731 String8 plugInId("");
732
733 if (EMPTY_STRING != mimeType) {
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700734 for (size_t index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900735 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
736
737 if (drmSupportInfo.isSupportedMimeType(mimeType)) {
738 plugInId = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
739 break;
740 }
741 }
742 }
743 return plugInId;
744}
745
746String8 DrmManager::getSupportedPlugInIdFromPath(int uniqueId, const String8& path) {
747 String8 plugInId("");
748 const String8 fileSuffix = path.getPathExtension();
749
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700750 for (size_t index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900751 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
752
753 if (drmSupportInfo.isSupportedFileSuffix(fileSuffix)) {
754 String8 key = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
755 IDrmEngine& drmEngine = mPlugInManager.getPlugIn(key);
756
757 if (drmEngine.canHandle(uniqueId, path)) {
758 plugInId = key;
759 break;
760 }
761 }
762 }
763 return plugInId;
764}
765
766void DrmManager::onInfo(const DrmInfoEvent& event) {
Gloria Wang0e0a5f92011-03-11 14:07:21 -0800767 Mutex::Autolock _l(mListenerLock);
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700768 for (size_t index = 0; index < mServiceListeners.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900769 int uniqueId = mServiceListeners.keyAt(index);
770
771 if (uniqueId == event.getUniqueId()) {
772 sp<IDrmServiceListener> serviceListener = mServiceListeners.valueFor(uniqueId);
773 serviceListener->notify(event);
774 }
775 }
776}
777