blob: 8e424677cfa37b0e97b17cdcb98c7d9c2c0b8dc4 [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)"
19#include "utils/Log.h"
20
21#include <utils/String8.h>
22#include <drm/DrmInfo.h>
23#include <drm/DrmInfoEvent.h>
24#include <drm/DrmRights.h>
25#include <drm/DrmConstraints.h>
Takeshi Aimi34738462010-11-16 13:56:11 +090026#include <drm/DrmMetadata.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090027#include <drm/DrmInfoStatus.h>
28#include <drm/DrmInfoRequest.h>
29#include <drm/DrmSupportInfo.h>
30#include <drm/DrmConvertedStatus.h>
31#include <IDrmEngine.h>
32
33#include "DrmManager.h"
34#include "ReadWriteUtils.h"
35
36#define DECRYPT_FILE_ERROR -1
37
38using namespace android;
39
40const String8 DrmManager::EMPTY_STRING("");
41
42DrmManager::DrmManager() :
43 mDecryptSessionId(0),
44 mConvertId(0) {
45
46}
47
48DrmManager::~DrmManager() {
49
50}
51
Gloria Wang8f001512011-07-21 15:10:22 -070052int DrmManager::addUniqueId(bool isNative) {
Gloria Wang6b610a32011-03-04 14:45:03 -080053 Mutex::Autolock _l(mLock);
Takeshi Aimi2272ee22010-09-20 23:40:41 +090054
Gloria Wang8f001512011-07-21 15:10:22 -070055 int temp = 0;
56 bool foundUniqueId = false;
57 const int size = mUniqueIdVector.size();
58 const int uniqueIdRange = 0xfff;
59 int maxLoopTimes = (uniqueIdRange - 1) / 2;
60 srand(time(NULL));
Takeshi Aimi2272ee22010-09-20 23:40:41 +090061
Gloria Wang8f001512011-07-21 15:10:22 -070062 while (!foundUniqueId) {
63 temp = rand() & uniqueIdRange;
64
65 if (isNative) {
66 // set a flag to differentiate DrmManagerClient
67 // created from native side and java side
68 temp |= 0x1000;
69 }
70
71 int index = 0;
72 for (; index < size; ++index) {
73 if (mUniqueIdVector.itemAt(index) == temp) {
74 foundUniqueId = false;
75 break;
Takeshi Aimi2272ee22010-09-20 23:40:41 +090076 }
77 }
Gloria Wang8f001512011-07-21 15:10:22 -070078 if (index == size) {
79 foundUniqueId = true;
80 }
81
82 maxLoopTimes --;
83 LOG_FATAL_IF(maxLoopTimes <= 0, "cannot find an unique ID for this session");
Takeshi Aimi2272ee22010-09-20 23:40:41 +090084 }
Gloria Wang8f001512011-07-21 15:10:22 -070085
86 mUniqueIdVector.push(temp);
87 return temp;
Takeshi Aimi2272ee22010-09-20 23:40:41 +090088}
89
90void DrmManager::removeUniqueId(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -080091 Mutex::Autolock _l(mLock);
Takeshi Aimi2272ee22010-09-20 23:40:41 +090092 for (unsigned int i = 0; i < mUniqueIdVector.size(); i++) {
93 if (uniqueId == mUniqueIdVector.itemAt(i)) {
94 mUniqueIdVector.removeAt(i);
95 break;
96 }
97 }
98}
99
Takeshi Aimie943f842010-10-08 23:05:49 +0900100status_t DrmManager::loadPlugIns() {
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700101
102 String8 vendorPluginDirPath("/vendor/lib/drm");
103 loadPlugIns(vendorPluginDirPath);
104
James Dong785ee062011-12-14 10:57:05 -0800105 String8 pluginDirPath("/system/lib/drm");
106 loadPlugIns(pluginDirPath);
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700107 return DRM_NO_ERROR;
108
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900109}
110
Takeshi Aimie943f842010-10-08 23:05:49 +0900111status_t DrmManager::loadPlugIns(const String8& plugInDirPath) {
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700112 mPlugInManager.loadPlugIns(plugInDirPath);
113 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
114 for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
115 String8 plugInPath = plugInPathList[i];
116 DrmSupportInfo* info = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
117 if (NULL != info) {
118 if (mSupportInfoToPlugInIdMap.indexOfKey(*info) < 0) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900119 mSupportInfoToPlugInIdMap.add(*info, plugInPath);
120 }
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700121 delete info;
Takeshi Aimie943f842010-10-08 23:05:49 +0900122 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900123 }
Takeshi Aimie943f842010-10-08 23:05:49 +0900124 return DRM_NO_ERROR;
125}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900126
Takeshi Aimie943f842010-10-08 23:05:49 +0900127status_t DrmManager::unloadPlugIns() {
Gloria Wang6b610a32011-03-04 14:45:03 -0800128 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900129 mConvertSessionMap.clear();
130 mDecryptSessionMap.clear();
131 mPlugInManager.unloadPlugIns();
132 mSupportInfoToPlugInIdMap.clear();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900133 return DRM_NO_ERROR;
134}
135
136status_t DrmManager::setDrmServiceListener(
137 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
Gloria Wang0e0a5f92011-03-11 14:07:21 -0800138 Mutex::Autolock _l(mListenerLock);
Takeshi Aimic618b5a2010-11-30 16:27:42 +0900139 if (NULL != drmServiceListener.get()) {
140 mServiceListeners.add(uniqueId, drmServiceListener);
141 } else {
142 mServiceListeners.removeItem(uniqueId);
143 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900144 return DRM_NO_ERROR;
145}
146
Takeshi Aimie943f842010-10-08 23:05:49 +0900147void DrmManager::addClient(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800148 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900149 if (!mSupportInfoToPlugInIdMap.isEmpty()) {
150 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
151 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
152 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
153 rDrmEngine.initialize(uniqueId);
154 rDrmEngine.setOnInfoListener(uniqueId, this);
155 }
156 }
157}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900158
Takeshi Aimie943f842010-10-08 23:05:49 +0900159void DrmManager::removeClient(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800160 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900161 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900162 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
163 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
164 rDrmEngine.terminate(uniqueId);
165 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900166}
167
168DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800169 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900170 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
171 if (EMPTY_STRING != plugInId) {
172 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
173 return rDrmEngine.getConstraints(uniqueId, path, action);
174 }
175 return NULL;
176}
177
Takeshi Aimi34738462010-11-16 13:56:11 +0900178DrmMetadata* DrmManager::getMetadata(int uniqueId, const String8* path) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800179 Mutex::Autolock _l(mLock);
Takeshi Aimi34738462010-11-16 13:56:11 +0900180 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
181 if (EMPTY_STRING != plugInId) {
182 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
183 return rDrmEngine.getMetadata(uniqueId, path);
184 }
185 return NULL;
186}
187
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900188bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800189 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900190 const String8 plugInId = getSupportedPlugInId(mimeType);
191 bool result = (EMPTY_STRING != plugInId) ? true : false;
192
Takeshi Aimie943f842010-10-08 23:05:49 +0900193 if (0 < path.length()) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900194 if (result) {
195 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
196 result = rDrmEngine.canHandle(uniqueId, path);
197 } else {
Gloria Wang7f89d092011-03-02 12:33:00 -0800198 String8 extension = path.getPathExtension();
199 if (String8("") != extension) {
200 result = canHandle(uniqueId, path);
201 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900202 }
203 }
204 return result;
205}
206
207DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800208 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900209 const String8 plugInId = getSupportedPlugInId(drmInfo->getMimeType());
210 if (EMPTY_STRING != plugInId) {
211 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
212 return rDrmEngine.processDrmInfo(uniqueId, drmInfo);
213 }
214 return NULL;
215}
216
217bool DrmManager::canHandle(int uniqueId, const String8& path) {
218 bool result = false;
219 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
220
221 for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
222 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]);
223 result = rDrmEngine.canHandle(uniqueId, path);
224
225 if (result) {
226 break;
227 }
228 }
229 return result;
230}
231
232DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800233 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900234 const String8 plugInId = getSupportedPlugInId(drmInfoRequest->getMimeType());
235 if (EMPTY_STRING != plugInId) {
236 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
237 return rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest);
238 }
239 return NULL;
240}
241
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900242status_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900243 const String8& rightsPath, const String8& contentPath) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800244 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900245 const String8 plugInId = getSupportedPlugInId(drmRights.getMimeType());
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900246 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900247 if (EMPTY_STRING != plugInId) {
248 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900249 result = rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900250 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900251 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900252}
253
254String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800255 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900256 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
257 if (EMPTY_STRING != plugInId) {
258 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
259 return rDrmEngine.getOriginalMimeType(uniqueId, path);
260 }
261 return EMPTY_STRING;
262}
263
264int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800265 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900266 const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
267 if (EMPTY_STRING != plugInId) {
268 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
269 return rDrmEngine.getDrmObjectType(uniqueId, path, mimeType);
270 }
271 return DrmObjectType::UNKNOWN;
272}
273
274int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800275 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900276 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
277 if (EMPTY_STRING != plugInId) {
278 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
279 return rDrmEngine.checkRightsStatus(uniqueId, path, action);
280 }
281 return RightsStatus::RIGHTS_INVALID;
282}
283
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900284status_t DrmManager::consumeRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900285 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900286 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800287 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900288 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
289 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900290 result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900291 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900292 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900293}
294
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900295status_t DrmManager::setPlaybackStatus(
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800296 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900297 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800298 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900299 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
300 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900301 result = drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900302 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900303 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900304}
305
306bool DrmManager::validateAction(
307 int uniqueId, const String8& path, int action, const ActionDescription& description) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800308 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900309 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
310 if (EMPTY_STRING != plugInId) {
311 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
312 return rDrmEngine.validateAction(uniqueId, path, action, description);
313 }
314 return false;
315}
316
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900317status_t DrmManager::removeRights(int uniqueId, const String8& path) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800318 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900319 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900320 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900321 if (EMPTY_STRING != plugInId) {
322 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900323 result = rDrmEngine.removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900324 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900325 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900326}
327
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900328status_t DrmManager::removeAllRights(int uniqueId) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900329 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900330 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900331 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
332 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900333 result = rDrmEngine.removeAllRights(uniqueId);
334 if (DRM_NO_ERROR != result) {
335 break;
336 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900337 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900338 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900339}
340
341int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800342 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900343 int convertId = -1;
344
345 const String8 plugInId = getSupportedPlugInId(mimeType);
346 if (EMPTY_STRING != plugInId) {
347 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
348
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900349 if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900350 ++mConvertId;
351 convertId = mConvertId;
352 mConvertSessionMap.add(convertId, &rDrmEngine);
353 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900354 }
355 return convertId;
356}
357
358DrmConvertedStatus* DrmManager::convertData(
359 int uniqueId, int convertId, const DrmBuffer* inputData) {
360 DrmConvertedStatus *drmConvertedStatus = NULL;
361
Gloria Wang6b610a32011-03-04 14:45:03 -0800362 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900363 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
364 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
365 drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
366 }
367 return drmConvertedStatus;
368}
369
370DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800371 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900372 DrmConvertedStatus *drmConvertedStatus = NULL;
373
374 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
375 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
376 drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId);
377 mConvertSessionMap.removeItem(convertId);
378 }
379 return drmConvertedStatus;
380}
381
382status_t DrmManager::getAllSupportInfo(
383 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800384 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900385 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
386 int size = plugInPathList.size();
387 int validPlugins = 0;
388
389 if (0 < size) {
390 Vector<DrmSupportInfo> drmSupportInfoList;
391
392 for (int i = 0; i < size; ++i) {
393 String8 plugInPath = plugInPathList[i];
394 DrmSupportInfo* drmSupportInfo
Takeshi Aimie943f842010-10-08 23:05:49 +0900395 = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900396 if (NULL != drmSupportInfo) {
397 drmSupportInfoList.add(*drmSupportInfo);
398 delete drmSupportInfo; drmSupportInfo = NULL;
399 }
400 }
401
402 validPlugins = drmSupportInfoList.size();
403 if (0 < validPlugins) {
404 *drmSupportInfoArray = new DrmSupportInfo[validPlugins];
405 for (int i = 0; i < validPlugins; ++i) {
406 (*drmSupportInfoArray)[i] = drmSupportInfoList[i];
407 }
408 }
409 }
410 *length = validPlugins;
411 return DRM_NO_ERROR;
412}
413
James Dong9d2f3862012-01-10 08:24:37 -0800414DecryptHandle* DrmManager::openDecryptSession(
415 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
416
Takeshi Aimie943f842010-10-08 23:05:49 +0900417 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900418 status_t result = DRM_ERROR_CANNOT_HANDLE;
419 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
420
421 DecryptHandle* handle = new DecryptHandle();
422 if (NULL != handle) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900423 handle->decryptId = mDecryptSessionId + 1;
424
425 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
426 String8 plugInId = plugInIdList.itemAt(index);
427 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dong9d2f3862012-01-10 08:24:37 -0800428 result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length, mime);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900429
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900430 if (DRM_NO_ERROR == result) {
431 ++mDecryptSessionId;
432 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900433 break;
434 }
435 }
436 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900437 if (DRM_NO_ERROR != result) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900438 delete handle; handle = NULL;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900439 }
Takeshi Aimie943f842010-10-08 23:05:49 +0900440 return handle;
441}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900442
James Dong9d2f3862012-01-10 08:24:37 -0800443DecryptHandle* DrmManager::openDecryptSession(
444 int uniqueId, const char* uri, const char* mime) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900445 Mutex::Autolock _l(mDecryptLock);
446 status_t result = DRM_ERROR_CANNOT_HANDLE;
447 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
448
449 DecryptHandle* handle = new DecryptHandle();
450 if (NULL != handle) {
451 handle->decryptId = mDecryptSessionId + 1;
452
453 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
454 String8 plugInId = plugInIdList.itemAt(index);
455 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dong9d2f3862012-01-10 08:24:37 -0800456 result = rDrmEngine.openDecryptSession(uniqueId, handle, uri, mime);
Takeshi Aimie943f842010-10-08 23:05:49 +0900457
458 if (DRM_NO_ERROR == result) {
459 ++mDecryptSessionId;
460 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
461 break;
462 }
463 }
464 }
465 if (DRM_NO_ERROR != result) {
466 delete handle; handle = NULL;
Steve Block3856b092011-10-20 11:56:00 +0100467 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
Takeshi Aimie943f842010-10-08 23:05:49 +0900468 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900469 return handle;
470}
471
Kei Takahashicba7b322012-01-18 17:10:19 +0900472DecryptHandle* DrmManager::openDecryptSession(
473 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
474 Mutex::Autolock _l(mDecryptLock);
475 status_t result = DRM_ERROR_CANNOT_HANDLE;
476 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
477
478 DecryptHandle* handle = new DecryptHandle();
479 if (NULL != handle) {
480 handle->decryptId = mDecryptSessionId + 1;
481
482 for (size_t index = 0; index < plugInIdList.size(); index++) {
483 String8 plugInId = plugInIdList.itemAt(index);
484 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
485 result = rDrmEngine.openDecryptSession(uniqueId, handle, buf, mimeType);
486
487 if (DRM_NO_ERROR == result) {
488 ++mDecryptSessionId;
489 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
490 break;
491 }
492 }
493 }
494 if (DRM_NO_ERROR != result) {
495 delete handle;
496 handle = NULL;
497 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
498 }
499 return handle;
500}
501
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900502status_t DrmManager::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900503 Mutex::Autolock _l(mDecryptLock);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900504 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900505 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
506 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900507 result = drmEngine->closeDecryptSession(uniqueId, decryptHandle);
508 if (DRM_NO_ERROR == result) {
509 mDecryptSessionMap.removeItem(decryptHandle->decryptId);
510 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900511 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900512 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900513}
514
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900515status_t DrmManager::initializeDecryptUnit(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900516 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, const DrmBuffer* headerInfo) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900517 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800518 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900519 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
520 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900521 result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900522 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900523 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900524}
525
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900526status_t DrmManager::decrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
527 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
528 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800529
530 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900531 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
532 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900533 result = drmEngine->decrypt(
534 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900535 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900536 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900537}
538
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900539status_t DrmManager::finalizeDecryptUnit(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900540 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900541 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800542 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900543 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
544 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900545 result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900546 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900547 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900548}
549
550ssize_t DrmManager::pread(int uniqueId, DecryptHandle* decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800551 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900552 ssize_t result = DECRYPT_FILE_ERROR;
553
Gloria Wang6b610a32011-03-04 14:45:03 -0800554 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900555 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
556 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
557 result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
558 }
559 return result;
560}
561
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900562String8 DrmManager::getSupportedPlugInId(
563 int uniqueId, const String8& path, const String8& mimeType) {
564 String8 plugInId("");
565
566 if (EMPTY_STRING != mimeType) {
567 plugInId = getSupportedPlugInId(mimeType);
568 } else {
569 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
570 }
571 return plugInId;
572}
573
574String8 DrmManager::getSupportedPlugInId(const String8& mimeType) {
575 String8 plugInId("");
576
577 if (EMPTY_STRING != mimeType) {
578 for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
579 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
580
581 if (drmSupportInfo.isSupportedMimeType(mimeType)) {
582 plugInId = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
583 break;
584 }
585 }
586 }
587 return plugInId;
588}
589
590String8 DrmManager::getSupportedPlugInIdFromPath(int uniqueId, const String8& path) {
591 String8 plugInId("");
592 const String8 fileSuffix = path.getPathExtension();
593
594 for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
595 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
596
597 if (drmSupportInfo.isSupportedFileSuffix(fileSuffix)) {
598 String8 key = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
599 IDrmEngine& drmEngine = mPlugInManager.getPlugIn(key);
600
601 if (drmEngine.canHandle(uniqueId, path)) {
602 plugInId = key;
603 break;
604 }
605 }
606 }
607 return plugInId;
608}
609
610void DrmManager::onInfo(const DrmInfoEvent& event) {
Gloria Wang0e0a5f92011-03-11 14:07:21 -0800611 Mutex::Autolock _l(mListenerLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900612 for (unsigned int index = 0; index < mServiceListeners.size(); index++) {
613 int uniqueId = mServiceListeners.keyAt(index);
614
615 if (uniqueId == event.getUniqueId()) {
616 sp<IDrmServiceListener> serviceListener = mServiceListeners.valueFor(uniqueId);
617 serviceListener->notify(event);
618 }
619 }
620}
621