blob: 74e3223bce017ec1a2a64270a58c7523057a4970 [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() {
James Dong785ee062011-12-14 10:57:05 -0800211 String8 pluginDirPath("/system/lib/drm");
212 loadPlugIns(pluginDirPath);
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700213 return DRM_NO_ERROR;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900214}
215
Takeshi Aimie943f842010-10-08 23:05:49 +0900216status_t DrmManager::loadPlugIns(const String8& plugInDirPath) {
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700217 mPlugInManager.loadPlugIns(plugInDirPath);
218 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700219 for (size_t i = 0; i < plugInPathList.size(); ++i) {
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700220 String8 plugInPath = plugInPathList[i];
221 DrmSupportInfo* info = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
222 if (NULL != info) {
223 if (mSupportInfoToPlugInIdMap.indexOfKey(*info) < 0) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900224 mSupportInfoToPlugInIdMap.add(*info, plugInPath);
225 }
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700226 delete info;
Takeshi Aimie943f842010-10-08 23:05:49 +0900227 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900228 }
Takeshi Aimie943f842010-10-08 23:05:49 +0900229 return DRM_NO_ERROR;
230}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900231
Takeshi Aimie943f842010-10-08 23:05:49 +0900232status_t DrmManager::unloadPlugIns() {
Gloria Wang6b610a32011-03-04 14:45:03 -0800233 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900234 mConvertSessionMap.clear();
235 mDecryptSessionMap.clear();
236 mPlugInManager.unloadPlugIns();
237 mSupportInfoToPlugInIdMap.clear();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900238 return DRM_NO_ERROR;
239}
240
241status_t DrmManager::setDrmServiceListener(
242 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
Gloria Wang0e0a5f92011-03-11 14:07:21 -0800243 Mutex::Autolock _l(mListenerLock);
Takeshi Aimic618b5a2010-11-30 16:27:42 +0900244 if (NULL != drmServiceListener.get()) {
245 mServiceListeners.add(uniqueId, drmServiceListener);
246 } else {
247 mServiceListeners.removeItem(uniqueId);
248 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900249 return DRM_NO_ERROR;
250}
251
Takeshi Aimie943f842010-10-08 23:05:49 +0900252void DrmManager::addClient(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800253 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900254 if (!mSupportInfoToPlugInIdMap.isEmpty()) {
255 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700256 for (size_t index = 0; index < plugInIdList.size(); index++) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900257 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
258 rDrmEngine.initialize(uniqueId);
259 rDrmEngine.setOnInfoListener(uniqueId, this);
260 }
261 }
262}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900263
Takeshi Aimie943f842010-10-08 23:05:49 +0900264void DrmManager::removeClient(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800265 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900266 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700267 for (size_t index = 0; index < plugInIdList.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900268 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
269 rDrmEngine.terminate(uniqueId);
270 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900271}
272
273DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800274 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700275 DrmConstraints *constraints = NULL;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900276 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
277 if (EMPTY_STRING != plugInId) {
278 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700279 constraints = rDrmEngine.getConstraints(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900280 }
Robert Shihec056ae2019-08-17 01:54:04 -0700281 if (NULL != constraints) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800282 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700283 }
284 return constraints;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900285}
286
Takeshi Aimi34738462010-11-16 13:56:11 +0900287DrmMetadata* DrmManager::getMetadata(int uniqueId, const String8* path) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800288 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700289 DrmMetadata *meta = NULL;
Takeshi Aimi34738462010-11-16 13:56:11 +0900290 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
291 if (EMPTY_STRING != plugInId) {
292 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700293 meta = rDrmEngine.getMetadata(uniqueId, path);
Takeshi Aimi34738462010-11-16 13:56:11 +0900294 }
Robert Shihec056ae2019-08-17 01:54:04 -0700295 if (NULL != meta) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800296 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700297 }
298 return meta;
Takeshi Aimi34738462010-11-16 13:56:11 +0900299}
300
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900301bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800302 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900303 const String8 plugInId = getSupportedPlugInId(mimeType);
304 bool result = (EMPTY_STRING != plugInId) ? true : false;
305
Robert Shihec056ae2019-08-17 01:54:04 -0700306 if (result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800307 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700308 }
309
Takeshi Aimie943f842010-10-08 23:05:49 +0900310 if (0 < path.length()) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900311 if (result) {
312 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
313 result = rDrmEngine.canHandle(uniqueId, path);
314 } else {
Gloria Wang7f89d092011-03-02 12:33:00 -0800315 String8 extension = path.getPathExtension();
316 if (String8("") != extension) {
317 result = canHandle(uniqueId, path);
318 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900319 }
320 }
321 return result;
322}
323
324DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800325 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700326 DrmInfoStatus *infoStatus = NULL;
327 const String8 mimeType = drmInfo->getMimeType();
328 const String8 plugInId = getSupportedPlugInId(mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900329 if (EMPTY_STRING != plugInId) {
330 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700331 infoStatus = rDrmEngine.processDrmInfo(uniqueId, drmInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900332 }
Robert Shihec056ae2019-08-17 01:54:04 -0700333 if (NULL != infoStatus) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800334 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700335 }
336 return infoStatus;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900337}
338
339bool DrmManager::canHandle(int uniqueId, const String8& path) {
340 bool result = false;
341 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
342
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700343 for (size_t i = 0; i < plugInPathList.size(); ++i) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900344 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]);
345 result = rDrmEngine.canHandle(uniqueId, path);
346
347 if (result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800348 recordEngineMetrics(__func__, plugInPathList[i]);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900349 break;
350 }
351 }
352 return result;
353}
354
355DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800356 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700357 DrmInfo *info = NULL;
358 const String8 mimeType = drmInfoRequest->getMimeType();
359 const String8 plugInId = getSupportedPlugInId(mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900360 if (EMPTY_STRING != plugInId) {
361 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700362 info = rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900363 }
Robert Shihec056ae2019-08-17 01:54:04 -0700364 if (NULL != info) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800365 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700366 }
367 return info;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900368}
369
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900370status_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900371 const String8& rightsPath, const String8& contentPath) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800372 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700373 const String8 mimeType = drmRights.getMimeType();
374 const String8 plugInId = getSupportedPlugInId(mimeType);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900375 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900376 if (EMPTY_STRING != plugInId) {
377 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900378 result = rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900379 }
Robert Shihec056ae2019-08-17 01:54:04 -0700380 if (DRM_NO_ERROR == result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800381 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700382 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900383 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900384}
385
James Dongbf5b3b22012-07-30 17:57:39 -0700386String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800387 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700388 String8 mimeType(EMPTY_STRING);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900389 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
390 if (EMPTY_STRING != plugInId) {
391 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700392 mimeType = rDrmEngine.getOriginalMimeType(uniqueId, path, fd);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900393 }
Robert Shihec056ae2019-08-17 01:54:04 -0700394 if (!mimeType.isEmpty()) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800395 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700396 }
397 return mimeType;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900398}
399
400int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800401 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700402 int type = DrmObjectType::UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900403 const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
404 if (EMPTY_STRING != plugInId) {
405 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700406 type = rDrmEngine.getDrmObjectType(uniqueId, path, mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900407 }
Robert Shihec056ae2019-08-17 01:54:04 -0700408 if (DrmObjectType::UNKNOWN != type) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800409 recordEngineMetrics(__func__, plugInId, mimeType);
Robert Shihec056ae2019-08-17 01:54:04 -0700410 }
411 return type;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900412}
413
414int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800415 Mutex::Autolock _l(mLock);
Robert Shihec056ae2019-08-17 01:54:04 -0700416 int rightsStatus = RightsStatus::RIGHTS_INVALID;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900417 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
418 if (EMPTY_STRING != plugInId) {
419 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700420 rightsStatus = rDrmEngine.checkRightsStatus(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900421 }
Robert Shihec056ae2019-08-17 01:54:04 -0700422 if (RightsStatus::RIGHTS_INVALID != rightsStatus) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800423 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700424 }
425 return rightsStatus;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900426}
427
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900428status_t DrmManager::consumeRights(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700429 int uniqueId, sp<DecryptHandle>& decryptHandle, int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900430 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800431 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900432 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
433 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900434 result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900435 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900436 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900437}
438
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900439status_t DrmManager::setPlaybackStatus(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700440 int uniqueId, sp<DecryptHandle>& decryptHandle, int playbackStatus, int64_t position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900441 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800442 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900443 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
444 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900445 result = drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900446 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900447 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900448}
449
450bool DrmManager::validateAction(
451 int uniqueId, const String8& path, int action, const ActionDescription& description) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800452 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900453 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
454 if (EMPTY_STRING != plugInId) {
455 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
456 return rDrmEngine.validateAction(uniqueId, path, action, description);
457 }
458 return false;
459}
460
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900461status_t DrmManager::removeRights(int uniqueId, const String8& path) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800462 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900463 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900464 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900465 if (EMPTY_STRING != plugInId) {
466 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900467 result = rDrmEngine.removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900468 }
Robert Shihec056ae2019-08-17 01:54:04 -0700469 if (DRM_NO_ERROR == result) {
Robert Shih7bcf7922020-02-07 15:01:57 -0800470 recordEngineMetrics(__func__, plugInId);
Robert Shihec056ae2019-08-17 01:54:04 -0700471 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900472 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900473}
474
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900475status_t DrmManager::removeAllRights(int uniqueId) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900476 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900477 status_t result = DRM_ERROR_UNKNOWN;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700478 for (size_t index = 0; index < plugInIdList.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900479 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900480 result = rDrmEngine.removeAllRights(uniqueId);
481 if (DRM_NO_ERROR != result) {
482 break;
483 }
Robert Shih7bcf7922020-02-07 15:01:57 -0800484 recordEngineMetrics(__func__, plugInIdList[index]);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900485 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900486 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900487}
488
489int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800490 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900491 int convertId = -1;
492
493 const String8 plugInId = getSupportedPlugInId(mimeType);
494 if (EMPTY_STRING != plugInId) {
495 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
496
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900497 if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900498 ++mConvertId;
499 convertId = mConvertId;
500 mConvertSessionMap.add(convertId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800501 recordEngineMetrics(__func__, plugInId, mimeType);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900502 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900503 }
504 return convertId;
505}
506
507DrmConvertedStatus* DrmManager::convertData(
508 int uniqueId, int convertId, const DrmBuffer* inputData) {
509 DrmConvertedStatus *drmConvertedStatus = NULL;
510
Gloria Wang6b610a32011-03-04 14:45:03 -0800511 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900512 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
513 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
514 drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
515 }
516 return drmConvertedStatus;
517}
518
519DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800520 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900521 DrmConvertedStatus *drmConvertedStatus = NULL;
522
523 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
524 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
525 drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId);
526 mConvertSessionMap.removeItem(convertId);
527 }
528 return drmConvertedStatus;
529}
530
531status_t DrmManager::getAllSupportInfo(
Aurimas Liutikasb2231172016-02-12 16:57:08 -0800532 int /* uniqueId */, int* length, DrmSupportInfo** drmSupportInfoArray) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800533 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900534 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
535 int size = plugInPathList.size();
536 int validPlugins = 0;
537
538 if (0 < size) {
539 Vector<DrmSupportInfo> drmSupportInfoList;
540
541 for (int i = 0; i < size; ++i) {
542 String8 plugInPath = plugInPathList[i];
543 DrmSupportInfo* drmSupportInfo
Takeshi Aimie943f842010-10-08 23:05:49 +0900544 = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900545 if (NULL != drmSupportInfo) {
546 drmSupportInfoList.add(*drmSupportInfo);
547 delete drmSupportInfo; drmSupportInfo = NULL;
548 }
549 }
550
551 validPlugins = drmSupportInfoList.size();
552 if (0 < validPlugins) {
553 *drmSupportInfoArray = new DrmSupportInfo[validPlugins];
554 for (int i = 0; i < validPlugins; ++i) {
555 (*drmSupportInfoArray)[i] = drmSupportInfoList[i];
556 }
557 }
558 }
559 *length = validPlugins;
560 return DRM_NO_ERROR;
561}
562
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700563sp<DecryptHandle> DrmManager::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800564 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
565
Takeshi Aimie943f842010-10-08 23:05:49 +0900566 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900567 status_t result = DRM_ERROR_CANNOT_HANDLE;
568 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
569
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700570 sp<DecryptHandle> handle = new DecryptHandle();
571 if (NULL != handle.get()) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900572 handle->decryptId = mDecryptSessionId + 1;
573
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700574 for (size_t index = 0; index < plugInIdList.size(); index++) {
Chih-Hung Hsieh8c0164c2016-08-09 14:20:59 -0700575 const String8& plugInId = plugInIdList.itemAt(index);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900576 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dong9d2f3862012-01-10 08:24:37 -0800577 result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length, mime);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900578
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900579 if (DRM_NO_ERROR == result) {
580 ++mDecryptSessionId;
581 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800582 recordEngineMetrics(__func__, plugInId, String8(mime));
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900583 break;
584 }
585 }
586 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900587 if (DRM_NO_ERROR != result) {
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700588 handle.clear();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900589 }
Takeshi Aimie943f842010-10-08 23:05:49 +0900590 return handle;
591}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900592
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700593sp<DecryptHandle> DrmManager::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800594 int uniqueId, const char* uri, const char* mime) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900595 Mutex::Autolock _l(mDecryptLock);
596 status_t result = DRM_ERROR_CANNOT_HANDLE;
597 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
598
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700599 sp<DecryptHandle> handle = new DecryptHandle();
600 if (NULL != handle.get()) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900601 handle->decryptId = mDecryptSessionId + 1;
602
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700603 for (size_t index = 0; index < plugInIdList.size(); index++) {
Chih-Hung Hsieh8c0164c2016-08-09 14:20:59 -0700604 const String8& plugInId = plugInIdList.itemAt(index);
Takeshi Aimie943f842010-10-08 23:05:49 +0900605 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dong9d2f3862012-01-10 08:24:37 -0800606 result = rDrmEngine.openDecryptSession(uniqueId, handle, uri, mime);
Takeshi Aimie943f842010-10-08 23:05:49 +0900607
608 if (DRM_NO_ERROR == result) {
609 ++mDecryptSessionId;
610 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800611 recordEngineMetrics(__func__, plugInId, String8(mime));
Takeshi Aimie943f842010-10-08 23:05:49 +0900612 break;
613 }
614 }
615 }
616 if (DRM_NO_ERROR != result) {
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700617 handle.clear();
Steve Block3856b092011-10-20 11:56:00 +0100618 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
Takeshi Aimie943f842010-10-08 23:05:49 +0900619 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900620 return handle;
621}
622
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700623sp<DecryptHandle> DrmManager::openDecryptSession(
Kei Takahashicba7b322012-01-18 17:10:19 +0900624 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
625 Mutex::Autolock _l(mDecryptLock);
626 status_t result = DRM_ERROR_CANNOT_HANDLE;
627 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
628
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700629 sp<DecryptHandle> handle = new DecryptHandle();
630 if (NULL != handle.get()) {
Kei Takahashicba7b322012-01-18 17:10:19 +0900631 handle->decryptId = mDecryptSessionId + 1;
632
633 for (size_t index = 0; index < plugInIdList.size(); index++) {
Chih-Hung Hsieh8c0164c2016-08-09 14:20:59 -0700634 const String8& plugInId = plugInIdList.itemAt(index);
Kei Takahashicba7b322012-01-18 17:10:19 +0900635 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
636 result = rDrmEngine.openDecryptSession(uniqueId, handle, buf, mimeType);
637
638 if (DRM_NO_ERROR == result) {
639 ++mDecryptSessionId;
640 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
Robert Shih7bcf7922020-02-07 15:01:57 -0800641 recordEngineMetrics(__func__, plugInId, mimeType);
Kei Takahashicba7b322012-01-18 17:10:19 +0900642 break;
643 }
644 }
645 }
646 if (DRM_NO_ERROR != result) {
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700647 handle.clear();
Kei Takahashicba7b322012-01-18 17:10:19 +0900648 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
649 }
650 return handle;
651}
652
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700653status_t DrmManager::closeDecryptSession(int uniqueId, sp<DecryptHandle>& decryptHandle) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900654 Mutex::Autolock _l(mDecryptLock);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900655 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900656 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
657 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900658 result = drmEngine->closeDecryptSession(uniqueId, decryptHandle);
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700659 if (DRM_NO_ERROR == result && NULL != decryptHandle.get()) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900660 mDecryptSessionMap.removeItem(decryptHandle->decryptId);
661 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900662 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900663 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900664}
665
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900666status_t DrmManager::initializeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700667 int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId,
668 const DrmBuffer* headerInfo) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900669 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800670 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900671 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
672 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900673 result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900674 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900675 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900676}
677
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700678status_t DrmManager::decrypt(int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId,
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900679 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
680 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800681
682 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900683 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
684 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900685 result = drmEngine->decrypt(
686 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900687 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900688 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900689}
690
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900691status_t DrmManager::finalizeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700692 int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900693 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800694 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900695 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
696 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900697 result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900698 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900699 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900700}
701
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700702ssize_t DrmManager::pread(int uniqueId, sp<DecryptHandle>& decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800703 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900704 ssize_t result = DECRYPT_FILE_ERROR;
705
Gloria Wang6b610a32011-03-04 14:45:03 -0800706 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900707 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
708 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
709 result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
710 }
711 return result;
712}
713
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900714String8 DrmManager::getSupportedPlugInId(
715 int uniqueId, const String8& path, const String8& mimeType) {
716 String8 plugInId("");
717
718 if (EMPTY_STRING != mimeType) {
719 plugInId = getSupportedPlugInId(mimeType);
720 } else {
721 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
722 }
723 return plugInId;
724}
725
726String8 DrmManager::getSupportedPlugInId(const String8& mimeType) {
727 String8 plugInId("");
728
729 if (EMPTY_STRING != mimeType) {
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700730 for (size_t index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900731 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
732
733 if (drmSupportInfo.isSupportedMimeType(mimeType)) {
734 plugInId = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
735 break;
736 }
737 }
738 }
739 return plugInId;
740}
741
742String8 DrmManager::getSupportedPlugInIdFromPath(int uniqueId, const String8& path) {
743 String8 plugInId("");
744 const String8 fileSuffix = path.getPathExtension();
745
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700746 for (size_t index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900747 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
748
749 if (drmSupportInfo.isSupportedFileSuffix(fileSuffix)) {
750 String8 key = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
751 IDrmEngine& drmEngine = mPlugInManager.getPlugIn(key);
752
753 if (drmEngine.canHandle(uniqueId, path)) {
754 plugInId = key;
755 break;
756 }
757 }
758 }
759 return plugInId;
760}
761
762void DrmManager::onInfo(const DrmInfoEvent& event) {
Gloria Wang0e0a5f92011-03-11 14:07:21 -0800763 Mutex::Autolock _l(mListenerLock);
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700764 for (size_t index = 0; index < mServiceListeners.size(); index++) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900765 int uniqueId = mServiceListeners.keyAt(index);
766
767 if (uniqueId == event.getUniqueId()) {
768 sp<IDrmServiceListener> serviceListener = mServiceListeners.valueFor(uniqueId);
769 serviceListener->notify(event);
770 }
771 }
772}
773