blob: dccd23dfc5eb3ebb90541619aa149702e8257e03 [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) {
Henrik B Andersson13f7fe72012-10-26 15:15:15 +020045 srand(time(NULL));
46 memset(mUniqueIdArray, 0, sizeof(bool) * kMaxNumUniqueIds);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090047}
48
49DrmManager::~DrmManager() {
50
51}
52
Gloria Wang8f001512011-07-21 15:10:22 -070053int DrmManager::addUniqueId(bool isNative) {
Gloria Wang6b610a32011-03-04 14:45:03 -080054 Mutex::Autolock _l(mLock);
Takeshi Aimi2272ee22010-09-20 23:40:41 +090055
Henrik B Andersson13f7fe72012-10-26 15:15:15 +020056 int uniqueId = -1;
57 int random = rand();
Takeshi Aimi2272ee22010-09-20 23:40:41 +090058
Henrik B Andersson13f7fe72012-10-26 15:15:15 +020059 for (size_t index = 0; index < kMaxNumUniqueIds; ++index) {
60 int temp = (random + index) % kMaxNumUniqueIds;
61 if (!mUniqueIdArray[temp]) {
62 uniqueId = temp;
63 mUniqueIdArray[uniqueId] = true;
Gloria Wang8f001512011-07-21 15:10:22 -070064
Henrik B Andersson13f7fe72012-10-26 15:15:15 +020065 if (isNative) {
66 // set a flag to differentiate DrmManagerClient
67 // created from native side and java side
68 uniqueId |= 0x1000;
Takeshi Aimi2272ee22010-09-20 23:40:41 +090069 }
Henrik B Andersson13f7fe72012-10-26 15:15:15 +020070 break;
Takeshi Aimi2272ee22010-09-20 23:40:41 +090071 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +090072 }
Gloria Wang8f001512011-07-21 15:10:22 -070073
Henrik B Andersson13f7fe72012-10-26 15:15:15 +020074 // -1 indicates that no unique id can be allocated.
75 return uniqueId;
Takeshi Aimi2272ee22010-09-20 23:40:41 +090076}
77
78void DrmManager::removeUniqueId(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -080079 Mutex::Autolock _l(mLock);
Henrik B Andersson13f7fe72012-10-26 15:15:15 +020080 if (uniqueId & 0x1000) {
81 // clear the flag for the native side.
82 uniqueId &= ~(0x1000);
83 }
84
85 if (uniqueId >= 0 && uniqueId < kMaxNumUniqueIds) {
86 mUniqueIdArray[uniqueId] = false;
Takeshi Aimi2272ee22010-09-20 23:40:41 +090087 }
88}
89
Takeshi Aimie943f842010-10-08 23:05:49 +090090status_t DrmManager::loadPlugIns() {
Edwin Wong5f6f4e42011-09-21 19:18:30 -070091
92 String8 vendorPluginDirPath("/vendor/lib/drm");
93 loadPlugIns(vendorPluginDirPath);
94
James Dong785ee062011-12-14 10:57:05 -080095 String8 pluginDirPath("/system/lib/drm");
96 loadPlugIns(pluginDirPath);
Edwin Wong5f6f4e42011-09-21 19:18:30 -070097 return DRM_NO_ERROR;
98
aimitakeshi27ed8ad2010-07-29 10:12:27 +090099}
100
Takeshi Aimie943f842010-10-08 23:05:49 +0900101status_t DrmManager::loadPlugIns(const String8& plugInDirPath) {
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700102 mPlugInManager.loadPlugIns(plugInDirPath);
103 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
104 for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
105 String8 plugInPath = plugInPathList[i];
106 DrmSupportInfo* info = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
107 if (NULL != info) {
108 if (mSupportInfoToPlugInIdMap.indexOfKey(*info) < 0) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900109 mSupportInfoToPlugInIdMap.add(*info, plugInPath);
110 }
Edwin Wong5f6f4e42011-09-21 19:18:30 -0700111 delete info;
Takeshi Aimie943f842010-10-08 23:05:49 +0900112 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900113 }
Takeshi Aimie943f842010-10-08 23:05:49 +0900114 return DRM_NO_ERROR;
115}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900116
Takeshi Aimie943f842010-10-08 23:05:49 +0900117status_t DrmManager::unloadPlugIns() {
Gloria Wang6b610a32011-03-04 14:45:03 -0800118 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900119 mConvertSessionMap.clear();
120 mDecryptSessionMap.clear();
121 mPlugInManager.unloadPlugIns();
122 mSupportInfoToPlugInIdMap.clear();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900123 return DRM_NO_ERROR;
124}
125
126status_t DrmManager::setDrmServiceListener(
127 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
Gloria Wang0e0a5f92011-03-11 14:07:21 -0800128 Mutex::Autolock _l(mListenerLock);
Takeshi Aimic618b5a2010-11-30 16:27:42 +0900129 if (NULL != drmServiceListener.get()) {
130 mServiceListeners.add(uniqueId, drmServiceListener);
131 } else {
132 mServiceListeners.removeItem(uniqueId);
133 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900134 return DRM_NO_ERROR;
135}
136
Takeshi Aimie943f842010-10-08 23:05:49 +0900137void DrmManager::addClient(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800138 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900139 if (!mSupportInfoToPlugInIdMap.isEmpty()) {
140 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
141 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
142 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
143 rDrmEngine.initialize(uniqueId);
144 rDrmEngine.setOnInfoListener(uniqueId, this);
145 }
146 }
147}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900148
Takeshi Aimie943f842010-10-08 23:05:49 +0900149void DrmManager::removeClient(int uniqueId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800150 Mutex::Autolock _l(mLock);
Takeshi Aimie943f842010-10-08 23:05:49 +0900151 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900152 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
153 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
154 rDrmEngine.terminate(uniqueId);
155 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900156}
157
158DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800159 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900160 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
161 if (EMPTY_STRING != plugInId) {
162 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
163 return rDrmEngine.getConstraints(uniqueId, path, action);
164 }
165 return NULL;
166}
167
Takeshi Aimi34738462010-11-16 13:56:11 +0900168DrmMetadata* DrmManager::getMetadata(int uniqueId, const String8* path) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800169 Mutex::Autolock _l(mLock);
Takeshi Aimi34738462010-11-16 13:56:11 +0900170 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
171 if (EMPTY_STRING != plugInId) {
172 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
173 return rDrmEngine.getMetadata(uniqueId, path);
174 }
175 return NULL;
176}
177
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900178bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800179 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900180 const String8 plugInId = getSupportedPlugInId(mimeType);
181 bool result = (EMPTY_STRING != plugInId) ? true : false;
182
Takeshi Aimie943f842010-10-08 23:05:49 +0900183 if (0 < path.length()) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900184 if (result) {
185 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
186 result = rDrmEngine.canHandle(uniqueId, path);
187 } else {
Gloria Wang7f89d092011-03-02 12:33:00 -0800188 String8 extension = path.getPathExtension();
189 if (String8("") != extension) {
190 result = canHandle(uniqueId, path);
191 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900192 }
193 }
194 return result;
195}
196
197DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800198 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900199 const String8 plugInId = getSupportedPlugInId(drmInfo->getMimeType());
200 if (EMPTY_STRING != plugInId) {
201 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
202 return rDrmEngine.processDrmInfo(uniqueId, drmInfo);
203 }
204 return NULL;
205}
206
207bool DrmManager::canHandle(int uniqueId, const String8& path) {
208 bool result = false;
209 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
210
211 for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
212 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]);
213 result = rDrmEngine.canHandle(uniqueId, path);
214
215 if (result) {
216 break;
217 }
218 }
219 return result;
220}
221
222DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800223 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900224 const String8 plugInId = getSupportedPlugInId(drmInfoRequest->getMimeType());
225 if (EMPTY_STRING != plugInId) {
226 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
227 return rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest);
228 }
229 return NULL;
230}
231
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900232status_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900233 const String8& rightsPath, const String8& contentPath) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800234 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900235 const String8 plugInId = getSupportedPlugInId(drmRights.getMimeType());
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900236 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900237 if (EMPTY_STRING != plugInId) {
238 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900239 result = rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900240 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900241 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900242}
243
James Dongbf5b3b22012-07-30 17:57:39 -0700244String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800245 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900246 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
247 if (EMPTY_STRING != plugInId) {
248 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dongbf5b3b22012-07-30 17:57:39 -0700249 return rDrmEngine.getOriginalMimeType(uniqueId, path, fd);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900250 }
251 return EMPTY_STRING;
252}
253
254int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800255 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900256 const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
257 if (EMPTY_STRING != plugInId) {
258 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
259 return rDrmEngine.getDrmObjectType(uniqueId, path, mimeType);
260 }
261 return DrmObjectType::UNKNOWN;
262}
263
264int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800265 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900266 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
267 if (EMPTY_STRING != plugInId) {
268 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
269 return rDrmEngine.checkRightsStatus(uniqueId, path, action);
270 }
271 return RightsStatus::RIGHTS_INVALID;
272}
273
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900274status_t DrmManager::consumeRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900275 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900276 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800277 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900278 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
279 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900280 result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900281 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900282 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900283}
284
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900285status_t DrmManager::setPlaybackStatus(
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800286 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900287 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800288 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900289 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
290 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900291 result = drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900292 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900293 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900294}
295
296bool DrmManager::validateAction(
297 int uniqueId, const String8& path, int action, const ActionDescription& description) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800298 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900299 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
300 if (EMPTY_STRING != plugInId) {
301 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
302 return rDrmEngine.validateAction(uniqueId, path, action, description);
303 }
304 return false;
305}
306
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900307status_t DrmManager::removeRights(int uniqueId, const String8& path) {
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);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900310 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900311 if (EMPTY_STRING != plugInId) {
312 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900313 result = rDrmEngine.removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900314 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900315 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900316}
317
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900318status_t DrmManager::removeAllRights(int uniqueId) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900319 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900320 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900321 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
322 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900323 result = rDrmEngine.removeAllRights(uniqueId);
324 if (DRM_NO_ERROR != result) {
325 break;
326 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900327 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900328 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900329}
330
331int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800332 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900333 int convertId = -1;
334
335 const String8 plugInId = getSupportedPlugInId(mimeType);
336 if (EMPTY_STRING != plugInId) {
337 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
338
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900339 if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900340 ++mConvertId;
341 convertId = mConvertId;
342 mConvertSessionMap.add(convertId, &rDrmEngine);
343 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900344 }
345 return convertId;
346}
347
348DrmConvertedStatus* DrmManager::convertData(
349 int uniqueId, int convertId, const DrmBuffer* inputData) {
350 DrmConvertedStatus *drmConvertedStatus = NULL;
351
Gloria Wang6b610a32011-03-04 14:45:03 -0800352 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900353 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
354 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
355 drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
356 }
357 return drmConvertedStatus;
358}
359
360DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800361 Mutex::Autolock _l(mConvertLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900362 DrmConvertedStatus *drmConvertedStatus = NULL;
363
364 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
365 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
366 drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId);
367 mConvertSessionMap.removeItem(convertId);
368 }
369 return drmConvertedStatus;
370}
371
372status_t DrmManager::getAllSupportInfo(
373 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
Gloria Wang6b610a32011-03-04 14:45:03 -0800374 Mutex::Autolock _l(mLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900375 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
376 int size = plugInPathList.size();
377 int validPlugins = 0;
378
379 if (0 < size) {
380 Vector<DrmSupportInfo> drmSupportInfoList;
381
382 for (int i = 0; i < size; ++i) {
383 String8 plugInPath = plugInPathList[i];
384 DrmSupportInfo* drmSupportInfo
Takeshi Aimie943f842010-10-08 23:05:49 +0900385 = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900386 if (NULL != drmSupportInfo) {
387 drmSupportInfoList.add(*drmSupportInfo);
388 delete drmSupportInfo; drmSupportInfo = NULL;
389 }
390 }
391
392 validPlugins = drmSupportInfoList.size();
393 if (0 < validPlugins) {
394 *drmSupportInfoArray = new DrmSupportInfo[validPlugins];
395 for (int i = 0; i < validPlugins; ++i) {
396 (*drmSupportInfoArray)[i] = drmSupportInfoList[i];
397 }
398 }
399 }
400 *length = validPlugins;
401 return DRM_NO_ERROR;
402}
403
James Dong9d2f3862012-01-10 08:24:37 -0800404DecryptHandle* DrmManager::openDecryptSession(
405 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
406
Takeshi Aimie943f842010-10-08 23:05:49 +0900407 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900408 status_t result = DRM_ERROR_CANNOT_HANDLE;
409 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
410
411 DecryptHandle* handle = new DecryptHandle();
412 if (NULL != handle) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900413 handle->decryptId = mDecryptSessionId + 1;
414
415 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
416 String8 plugInId = plugInIdList.itemAt(index);
417 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dong9d2f3862012-01-10 08:24:37 -0800418 result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length, mime);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900419
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900420 if (DRM_NO_ERROR == result) {
421 ++mDecryptSessionId;
422 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900423 break;
424 }
425 }
426 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900427 if (DRM_NO_ERROR != result) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900428 delete handle; handle = NULL;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900429 }
Takeshi Aimie943f842010-10-08 23:05:49 +0900430 return handle;
431}
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900432
James Dong9d2f3862012-01-10 08:24:37 -0800433DecryptHandle* DrmManager::openDecryptSession(
434 int uniqueId, const char* uri, const char* mime) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900435 Mutex::Autolock _l(mDecryptLock);
436 status_t result = DRM_ERROR_CANNOT_HANDLE;
437 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
438
439 DecryptHandle* handle = new DecryptHandle();
440 if (NULL != handle) {
441 handle->decryptId = mDecryptSessionId + 1;
442
443 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
444 String8 plugInId = plugInIdList.itemAt(index);
445 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
James Dong9d2f3862012-01-10 08:24:37 -0800446 result = rDrmEngine.openDecryptSession(uniqueId, handle, uri, mime);
Takeshi Aimie943f842010-10-08 23:05:49 +0900447
448 if (DRM_NO_ERROR == result) {
449 ++mDecryptSessionId;
450 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
451 break;
452 }
453 }
454 }
455 if (DRM_NO_ERROR != result) {
456 delete handle; handle = NULL;
Steve Block3856b092011-10-20 11:56:00 +0100457 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
Takeshi Aimie943f842010-10-08 23:05:49 +0900458 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900459 return handle;
460}
461
Kei Takahashicba7b322012-01-18 17:10:19 +0900462DecryptHandle* DrmManager::openDecryptSession(
463 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
464 Mutex::Autolock _l(mDecryptLock);
465 status_t result = DRM_ERROR_CANNOT_HANDLE;
466 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
467
468 DecryptHandle* handle = new DecryptHandle();
469 if (NULL != handle) {
470 handle->decryptId = mDecryptSessionId + 1;
471
472 for (size_t index = 0; index < plugInIdList.size(); index++) {
473 String8 plugInId = plugInIdList.itemAt(index);
474 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
475 result = rDrmEngine.openDecryptSession(uniqueId, handle, buf, mimeType);
476
477 if (DRM_NO_ERROR == result) {
478 ++mDecryptSessionId;
479 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
480 break;
481 }
482 }
483 }
484 if (DRM_NO_ERROR != result) {
485 delete handle;
486 handle = NULL;
487 ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
488 }
489 return handle;
490}
491
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900492status_t DrmManager::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900493 Mutex::Autolock _l(mDecryptLock);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900494 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900495 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
496 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900497 result = drmEngine->closeDecryptSession(uniqueId, decryptHandle);
498 if (DRM_NO_ERROR == result) {
499 mDecryptSessionMap.removeItem(decryptHandle->decryptId);
500 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900501 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900502 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900503}
504
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900505status_t DrmManager::initializeDecryptUnit(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900506 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, const DrmBuffer* headerInfo) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900507 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800508 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900509 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
510 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900511 result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900512 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900513 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900514}
515
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900516status_t DrmManager::decrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
517 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
518 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800519
520 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900521 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
522 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900523 result = drmEngine->decrypt(
524 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900525 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900526 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900527}
528
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900529status_t DrmManager::finalizeDecryptUnit(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900530 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900531 status_t result = DRM_ERROR_UNKNOWN;
Gloria Wang6b610a32011-03-04 14:45:03 -0800532 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900533 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
534 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900535 result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900536 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900537 return result;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900538}
539
540ssize_t DrmManager::pread(int uniqueId, DecryptHandle* decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800541 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900542 ssize_t result = DECRYPT_FILE_ERROR;
543
Gloria Wang6b610a32011-03-04 14:45:03 -0800544 Mutex::Autolock _l(mDecryptLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900545 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
546 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
547 result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
548 }
549 return result;
550}
551
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900552String8 DrmManager::getSupportedPlugInId(
553 int uniqueId, const String8& path, const String8& mimeType) {
554 String8 plugInId("");
555
556 if (EMPTY_STRING != mimeType) {
557 plugInId = getSupportedPlugInId(mimeType);
558 } else {
559 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
560 }
561 return plugInId;
562}
563
564String8 DrmManager::getSupportedPlugInId(const String8& mimeType) {
565 String8 plugInId("");
566
567 if (EMPTY_STRING != mimeType) {
568 for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
569 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
570
571 if (drmSupportInfo.isSupportedMimeType(mimeType)) {
572 plugInId = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
573 break;
574 }
575 }
576 }
577 return plugInId;
578}
579
580String8 DrmManager::getSupportedPlugInIdFromPath(int uniqueId, const String8& path) {
581 String8 plugInId("");
582 const String8 fileSuffix = path.getPathExtension();
583
584 for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
585 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
586
587 if (drmSupportInfo.isSupportedFileSuffix(fileSuffix)) {
588 String8 key = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
589 IDrmEngine& drmEngine = mPlugInManager.getPlugIn(key);
590
591 if (drmEngine.canHandle(uniqueId, path)) {
592 plugInId = key;
593 break;
594 }
595 }
596 }
597 return plugInId;
598}
599
600void DrmManager::onInfo(const DrmInfoEvent& event) {
Gloria Wang0e0a5f92011-03-11 14:07:21 -0800601 Mutex::Autolock _l(mListenerLock);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900602 for (unsigned int index = 0; index < mServiceListeners.size(); index++) {
603 int uniqueId = mServiceListeners.keyAt(index);
604
605 if (uniqueId == event.getUniqueId()) {
606 sp<IDrmServiceListener> serviceListener = mServiceListeners.valueFor(uniqueId);
607 serviceListener->notify(event);
608 }
609 }
610}
611