blob: 3abf3d3352909f570f3058ddd8d10fa8d4656379 [file] [log] [blame]
aimitakeshid074e302010-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 Aimidc549d62010-09-20 23:40:41 +090017//#define LOG_NDEBUG 0
aimitakeshid074e302010-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 Aimidc9186562010-11-16 13:56:11 +090026#include <drm/DrmMetadata.h>
aimitakeshid074e302010-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 Wanga17d4542011-07-21 15:10:22 -070052int DrmManager::addUniqueId(bool isNative) {
Gloria Wang2ef2d492011-03-04 14:45:03 -080053 Mutex::Autolock _l(mLock);
Takeshi Aimidc549d62010-09-20 23:40:41 +090054
Gloria Wanga17d4542011-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 Aimidc549d62010-09-20 23:40:41 +090061
Gloria Wanga17d4542011-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 Aimidc549d62010-09-20 23:40:41 +090076 }
77 }
Gloria Wanga17d4542011-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 Aimidc549d62010-09-20 23:40:41 +090084 }
Gloria Wanga17d4542011-07-21 15:10:22 -070085
86 mUniqueIdVector.push(temp);
87 return temp;
Takeshi Aimidc549d62010-09-20 23:40:41 +090088}
89
90void DrmManager::removeUniqueId(int uniqueId) {
Gloria Wang2ef2d492011-03-04 14:45:03 -080091 Mutex::Autolock _l(mLock);
Takeshi Aimidc549d62010-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 Aimic7b3ccc2010-10-08 23:05:49 +0900100status_t DrmManager::loadPlugIns() {
Edwin Wongaab6fe22011-09-21 19:18:30 -0700101
102 String8 vendorPluginDirPath("/vendor/lib/drm");
103 loadPlugIns(vendorPluginDirPath);
104
James Dongd70c64d2011-12-14 10:57:05 -0800105 String8 pluginDirPath("/system/lib/drm");
106 loadPlugIns(pluginDirPath);
Edwin Wongaab6fe22011-09-21 19:18:30 -0700107 return DRM_NO_ERROR;
108
aimitakeshid074e302010-07-29 10:12:27 +0900109}
110
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900111status_t DrmManager::loadPlugIns(const String8& plugInDirPath) {
Edwin Wongaab6fe22011-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 Aimic7b3ccc2010-10-08 23:05:49 +0900119 mSupportInfoToPlugInIdMap.add(*info, plugInPath);
120 }
Edwin Wongaab6fe22011-09-21 19:18:30 -0700121 delete info;
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900122 }
aimitakeshid074e302010-07-29 10:12:27 +0900123 }
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900124 return DRM_NO_ERROR;
125}
aimitakeshid074e302010-07-29 10:12:27 +0900126
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900127status_t DrmManager::unloadPlugIns() {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800128 Mutex::Autolock _l(mLock);
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900129 mConvertSessionMap.clear();
130 mDecryptSessionMap.clear();
131 mPlugInManager.unloadPlugIns();
132 mSupportInfoToPlugInIdMap.clear();
aimitakeshid074e302010-07-29 10:12:27 +0900133 return DRM_NO_ERROR;
134}
135
136status_t DrmManager::setDrmServiceListener(
137 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
Gloria Wang192b5912011-03-11 14:07:21 -0800138 Mutex::Autolock _l(mListenerLock);
Takeshi Aimif05913a2010-11-30 16:27:42 +0900139 if (NULL != drmServiceListener.get()) {
140 mServiceListeners.add(uniqueId, drmServiceListener);
141 } else {
142 mServiceListeners.removeItem(uniqueId);
143 }
aimitakeshid074e302010-07-29 10:12:27 +0900144 return DRM_NO_ERROR;
145}
146
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900147void DrmManager::addClient(int uniqueId) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800148 Mutex::Autolock _l(mLock);
Takeshi Aimic7b3ccc2010-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}
aimitakeshid074e302010-07-29 10:12:27 +0900158
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900159void DrmManager::removeClient(int uniqueId) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800160 Mutex::Autolock _l(mLock);
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900161 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
aimitakeshid074e302010-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 }
aimitakeshid074e302010-07-29 10:12:27 +0900166}
167
168DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800169 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-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 Aimidc9186562010-11-16 13:56:11 +0900178DrmMetadata* DrmManager::getMetadata(int uniqueId, const String8* path) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800179 Mutex::Autolock _l(mLock);
Takeshi Aimidc9186562010-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
aimitakeshid074e302010-07-29 10:12:27 +0900188status_t DrmManager::installDrmEngine(int uniqueId, const String8& absolutePath) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800189 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900190 mPlugInManager.loadPlugIn(absolutePath);
191
192 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(absolutePath);
193 rDrmEngine.initialize(uniqueId);
194 rDrmEngine.setOnInfoListener(uniqueId, this);
195
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900196 DrmSupportInfo* info = rDrmEngine.getSupportInfo(0);
aimitakeshid074e302010-07-29 10:12:27 +0900197 mSupportInfoToPlugInIdMap.add(*info, absolutePath);
James Dong3f0341c2011-04-24 12:38:35 -0700198 delete info;
aimitakeshid074e302010-07-29 10:12:27 +0900199
200 return DRM_NO_ERROR;
201}
202
203bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800204 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900205 const String8 plugInId = getSupportedPlugInId(mimeType);
206 bool result = (EMPTY_STRING != plugInId) ? true : false;
207
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900208 if (0 < path.length()) {
aimitakeshid074e302010-07-29 10:12:27 +0900209 if (result) {
210 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
211 result = rDrmEngine.canHandle(uniqueId, path);
212 } else {
Gloria Wang289401b2011-03-02 12:33:00 -0800213 String8 extension = path.getPathExtension();
214 if (String8("") != extension) {
215 result = canHandle(uniqueId, path);
216 }
aimitakeshid074e302010-07-29 10:12:27 +0900217 }
218 }
219 return result;
220}
221
222DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800223 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900224 const String8 plugInId = getSupportedPlugInId(drmInfo->getMimeType());
225 if (EMPTY_STRING != plugInId) {
226 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
227 return rDrmEngine.processDrmInfo(uniqueId, drmInfo);
228 }
229 return NULL;
230}
231
232bool DrmManager::canHandle(int uniqueId, const String8& path) {
233 bool result = false;
234 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
235
236 for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
237 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]);
238 result = rDrmEngine.canHandle(uniqueId, path);
239
240 if (result) {
241 break;
242 }
243 }
244 return result;
245}
246
247DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800248 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900249 const String8 plugInId = getSupportedPlugInId(drmInfoRequest->getMimeType());
250 if (EMPTY_STRING != plugInId) {
251 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
252 return rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest);
253 }
254 return NULL;
255}
256
Takeshi Aimidc549d62010-09-20 23:40:41 +0900257status_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshid074e302010-07-29 10:12:27 +0900258 const String8& rightsPath, const String8& contentPath) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800259 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900260 const String8 plugInId = getSupportedPlugInId(drmRights.getMimeType());
Takeshi Aimidc549d62010-09-20 23:40:41 +0900261 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900262 if (EMPTY_STRING != plugInId) {
263 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900264 result = rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshid074e302010-07-29 10:12:27 +0900265 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900266 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900267}
268
269String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800270 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900271 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
272 if (EMPTY_STRING != plugInId) {
273 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
274 return rDrmEngine.getOriginalMimeType(uniqueId, path);
275 }
276 return EMPTY_STRING;
277}
278
279int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800280 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900281 const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
282 if (EMPTY_STRING != plugInId) {
283 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
284 return rDrmEngine.getDrmObjectType(uniqueId, path, mimeType);
285 }
286 return DrmObjectType::UNKNOWN;
287}
288
289int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800290 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900291 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
292 if (EMPTY_STRING != plugInId) {
293 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
294 return rDrmEngine.checkRightsStatus(uniqueId, path, action);
295 }
296 return RightsStatus::RIGHTS_INVALID;
297}
298
Takeshi Aimidc549d62010-09-20 23:40:41 +0900299status_t DrmManager::consumeRights(
aimitakeshid074e302010-07-29 10:12:27 +0900300 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900301 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang2ef2d492011-03-04 14:45:03 -0800302 Mutex::Autolock _l(mDecryptLock);
aimitakeshid074e302010-07-29 10:12:27 +0900303 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
304 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900305 result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshid074e302010-07-29 10:12:27 +0900306 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900307 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900308}
309
Takeshi Aimidc549d62010-09-20 23:40:41 +0900310status_t DrmManager::setPlaybackStatus(
Gloria Wang5fc3edb2010-11-19 15:19:36 -0800311 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900312 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang2ef2d492011-03-04 14:45:03 -0800313 Mutex::Autolock _l(mDecryptLock);
aimitakeshid074e302010-07-29 10:12:27 +0900314 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
315 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900316 result = drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshid074e302010-07-29 10:12:27 +0900317 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900318 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900319}
320
321bool DrmManager::validateAction(
322 int uniqueId, const String8& path, int action, const ActionDescription& description) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800323 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900324 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
325 if (EMPTY_STRING != plugInId) {
326 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
327 return rDrmEngine.validateAction(uniqueId, path, action, description);
328 }
329 return false;
330}
331
Takeshi Aimidc549d62010-09-20 23:40:41 +0900332status_t DrmManager::removeRights(int uniqueId, const String8& path) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800333 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900334 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900335 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900336 if (EMPTY_STRING != plugInId) {
337 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900338 result = rDrmEngine.removeRights(uniqueId, path);
aimitakeshid074e302010-07-29 10:12:27 +0900339 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900340 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900341}
342
Takeshi Aimidc549d62010-09-20 23:40:41 +0900343status_t DrmManager::removeAllRights(int uniqueId) {
aimitakeshid074e302010-07-29 10:12:27 +0900344 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Takeshi Aimidc549d62010-09-20 23:40:41 +0900345 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900346 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
347 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
Takeshi Aimidc549d62010-09-20 23:40:41 +0900348 result = rDrmEngine.removeAllRights(uniqueId);
349 if (DRM_NO_ERROR != result) {
350 break;
351 }
aimitakeshid074e302010-07-29 10:12:27 +0900352 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900353 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900354}
355
356int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800357 Mutex::Autolock _l(mConvertLock);
aimitakeshid074e302010-07-29 10:12:27 +0900358 int convertId = -1;
359
360 const String8 plugInId = getSupportedPlugInId(mimeType);
361 if (EMPTY_STRING != plugInId) {
362 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
363
Takeshi Aimidc549d62010-09-20 23:40:41 +0900364 if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900365 ++mConvertId;
366 convertId = mConvertId;
367 mConvertSessionMap.add(convertId, &rDrmEngine);
368 }
aimitakeshid074e302010-07-29 10:12:27 +0900369 }
370 return convertId;
371}
372
373DrmConvertedStatus* DrmManager::convertData(
374 int uniqueId, int convertId, const DrmBuffer* inputData) {
375 DrmConvertedStatus *drmConvertedStatus = NULL;
376
Gloria Wang2ef2d492011-03-04 14:45:03 -0800377 Mutex::Autolock _l(mConvertLock);
aimitakeshid074e302010-07-29 10:12:27 +0900378 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
379 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
380 drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
381 }
382 return drmConvertedStatus;
383}
384
385DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800386 Mutex::Autolock _l(mConvertLock);
aimitakeshid074e302010-07-29 10:12:27 +0900387 DrmConvertedStatus *drmConvertedStatus = NULL;
388
389 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
390 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
391 drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId);
392 mConvertSessionMap.removeItem(convertId);
393 }
394 return drmConvertedStatus;
395}
396
397status_t DrmManager::getAllSupportInfo(
398 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
Gloria Wang2ef2d492011-03-04 14:45:03 -0800399 Mutex::Autolock _l(mLock);
aimitakeshid074e302010-07-29 10:12:27 +0900400 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
401 int size = plugInPathList.size();
402 int validPlugins = 0;
403
404 if (0 < size) {
405 Vector<DrmSupportInfo> drmSupportInfoList;
406
407 for (int i = 0; i < size; ++i) {
408 String8 plugInPath = plugInPathList[i];
409 DrmSupportInfo* drmSupportInfo
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900410 = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
aimitakeshid074e302010-07-29 10:12:27 +0900411 if (NULL != drmSupportInfo) {
412 drmSupportInfoList.add(*drmSupportInfo);
413 delete drmSupportInfo; drmSupportInfo = NULL;
414 }
415 }
416
417 validPlugins = drmSupportInfoList.size();
418 if (0 < validPlugins) {
419 *drmSupportInfoArray = new DrmSupportInfo[validPlugins];
420 for (int i = 0; i < validPlugins; ++i) {
421 (*drmSupportInfoArray)[i] = drmSupportInfoList[i];
422 }
423 }
424 }
425 *length = validPlugins;
426 return DRM_NO_ERROR;
427}
428
Gloria Wang5fc3edb2010-11-19 15:19:36 -0800429DecryptHandle* DrmManager::openDecryptSession(int uniqueId, int fd, off64_t offset, off64_t length) {
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900430 Mutex::Autolock _l(mDecryptLock);
aimitakeshid074e302010-07-29 10:12:27 +0900431 status_t result = DRM_ERROR_CANNOT_HANDLE;
432 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
433
434 DecryptHandle* handle = new DecryptHandle();
435 if (NULL != handle) {
aimitakeshid074e302010-07-29 10:12:27 +0900436 handle->decryptId = mDecryptSessionId + 1;
437
438 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
439 String8 plugInId = plugInIdList.itemAt(index);
440 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
441 result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length);
442
aimitakeshid074e302010-07-29 10:12:27 +0900443 if (DRM_NO_ERROR == result) {
444 ++mDecryptSessionId;
445 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
aimitakeshid074e302010-07-29 10:12:27 +0900446 break;
447 }
448 }
449 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900450 if (DRM_NO_ERROR != result) {
aimitakeshid074e302010-07-29 10:12:27 +0900451 delete handle; handle = NULL;
aimitakeshid074e302010-07-29 10:12:27 +0900452 }
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900453 return handle;
454}
aimitakeshid074e302010-07-29 10:12:27 +0900455
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900456DecryptHandle* DrmManager::openDecryptSession(int uniqueId, const char* uri) {
457 Mutex::Autolock _l(mDecryptLock);
458 status_t result = DRM_ERROR_CANNOT_HANDLE;
459 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
460
461 DecryptHandle* handle = new DecryptHandle();
462 if (NULL != handle) {
463 handle->decryptId = mDecryptSessionId + 1;
464
465 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
466 String8 plugInId = plugInIdList.itemAt(index);
467 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
468 result = rDrmEngine.openDecryptSession(uniqueId, handle, uri);
469
470 if (DRM_NO_ERROR == result) {
471 ++mDecryptSessionId;
472 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
473 break;
474 }
475 }
476 }
477 if (DRM_NO_ERROR != result) {
478 delete handle; handle = NULL;
Steve Block71f2cf12011-10-20 11:56:00 +0100479 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900480 }
aimitakeshid074e302010-07-29 10:12:27 +0900481 return handle;
482}
483
Takeshi Aimidc549d62010-09-20 23:40:41 +0900484status_t DrmManager::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900485 Mutex::Autolock _l(mDecryptLock);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900486 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900487 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
488 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900489 result = drmEngine->closeDecryptSession(uniqueId, decryptHandle);
490 if (DRM_NO_ERROR == result) {
491 mDecryptSessionMap.removeItem(decryptHandle->decryptId);
492 }
aimitakeshid074e302010-07-29 10:12:27 +0900493 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900494 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900495}
496
Takeshi Aimidc549d62010-09-20 23:40:41 +0900497status_t DrmManager::initializeDecryptUnit(
aimitakeshid074e302010-07-29 10:12:27 +0900498 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, const DrmBuffer* headerInfo) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900499 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang2ef2d492011-03-04 14:45:03 -0800500 Mutex::Autolock _l(mDecryptLock);
aimitakeshid074e302010-07-29 10:12:27 +0900501 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
502 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900503 result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshid074e302010-07-29 10:12:27 +0900504 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900505 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900506}
507
Takeshi Aimidc549d62010-09-20 23:40:41 +0900508status_t DrmManager::decrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
509 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
510 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang2ef2d492011-03-04 14:45:03 -0800511
512 Mutex::Autolock _l(mDecryptLock);
aimitakeshid074e302010-07-29 10:12:27 +0900513 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
514 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900515 result = drmEngine->decrypt(
516 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshid074e302010-07-29 10:12:27 +0900517 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900518 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900519}
520
Takeshi Aimidc549d62010-09-20 23:40:41 +0900521status_t DrmManager::finalizeDecryptUnit(
aimitakeshid074e302010-07-29 10:12:27 +0900522 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900523 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang2ef2d492011-03-04 14:45:03 -0800524 Mutex::Autolock _l(mDecryptLock);
aimitakeshid074e302010-07-29 10:12:27 +0900525 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
526 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900527 result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshid074e302010-07-29 10:12:27 +0900528 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900529 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900530}
531
532ssize_t DrmManager::pread(int uniqueId, DecryptHandle* decryptHandle,
Gloria Wang5fc3edb2010-11-19 15:19:36 -0800533 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshid074e302010-07-29 10:12:27 +0900534 ssize_t result = DECRYPT_FILE_ERROR;
535
Gloria Wang2ef2d492011-03-04 14:45:03 -0800536 Mutex::Autolock _l(mDecryptLock);
aimitakeshid074e302010-07-29 10:12:27 +0900537 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
538 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
539 result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
540 }
541 return result;
542}
543
aimitakeshid074e302010-07-29 10:12:27 +0900544String8 DrmManager::getSupportedPlugInId(
545 int uniqueId, const String8& path, const String8& mimeType) {
546 String8 plugInId("");
547
548 if (EMPTY_STRING != mimeType) {
549 plugInId = getSupportedPlugInId(mimeType);
550 } else {
551 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
552 }
553 return plugInId;
554}
555
556String8 DrmManager::getSupportedPlugInId(const String8& mimeType) {
557 String8 plugInId("");
558
559 if (EMPTY_STRING != mimeType) {
560 for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
561 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
562
563 if (drmSupportInfo.isSupportedMimeType(mimeType)) {
564 plugInId = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
565 break;
566 }
567 }
568 }
569 return plugInId;
570}
571
572String8 DrmManager::getSupportedPlugInIdFromPath(int uniqueId, const String8& path) {
573 String8 plugInId("");
574 const String8 fileSuffix = path.getPathExtension();
575
576 for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
577 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
578
579 if (drmSupportInfo.isSupportedFileSuffix(fileSuffix)) {
580 String8 key = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
581 IDrmEngine& drmEngine = mPlugInManager.getPlugIn(key);
582
583 if (drmEngine.canHandle(uniqueId, path)) {
584 plugInId = key;
585 break;
586 }
587 }
588 }
589 return plugInId;
590}
591
592void DrmManager::onInfo(const DrmInfoEvent& event) {
Gloria Wang192b5912011-03-11 14:07:21 -0800593 Mutex::Autolock _l(mListenerLock);
aimitakeshid074e302010-07-29 10:12:27 +0900594 for (unsigned int index = 0; index < mServiceListeners.size(); index++) {
595 int uniqueId = mServiceListeners.keyAt(index);
596
597 if (uniqueId == event.getUniqueId()) {
598 sp<IDrmServiceListener> serviceListener = mServiceListeners.valueFor(uniqueId);
599 serviceListener->notify(event);
600 }
601 }
602}
603