blob: 9c7fed398fb6bfd58073e1c0498190048acc67d7 [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 "DrmManagerClientImpl(Native)"
19#include <utils/Log.h>
20
21#include <utils/String8.h>
22#include <utils/Vector.h>
23#include <binder/IServiceManager.h>
24
25#include "DrmManagerClientImpl.h"
26
27using namespace android;
28
29#define INVALID_VALUE -1
30
Gloria Wang9e1e9a32011-03-23 22:35:13 -070031Mutex DrmManagerClientImpl::sMutex;
32sp<IDrmManagerService> DrmManagerClientImpl::sDrmManagerService;
33sp<DrmManagerClientImpl::DeathNotifier> DrmManagerClientImpl::sDeathNotifier;
aimitakeshid074e302010-07-29 10:12:27 +090034const String8 DrmManagerClientImpl::EMPTY_STRING("");
35
36DrmManagerClientImpl* DrmManagerClientImpl::create(int* pUniqueId) {
37 if (0 == *pUniqueId) {
Takeshi Aimidc549d62010-09-20 23:40:41 +090038 int uniqueId = getDrmManagerService()->addUniqueId(*pUniqueId);
aimitakeshid074e302010-07-29 10:12:27 +090039 *pUniqueId = uniqueId;
Takeshi Aimidc549d62010-09-20 23:40:41 +090040 } else {
41 getDrmManagerService()->addUniqueId(*pUniqueId);
aimitakeshid074e302010-07-29 10:12:27 +090042 }
aimitakeshid074e302010-07-29 10:12:27 +090043 return new DrmManagerClientImpl();
44}
45
46void DrmManagerClientImpl::remove(int uniqueId) {
Takeshi Aimidc549d62010-09-20 23:40:41 +090047 getDrmManagerService()->removeUniqueId(uniqueId);
aimitakeshid074e302010-07-29 10:12:27 +090048}
49
aimitakeshid074e302010-07-29 10:12:27 +090050const sp<IDrmManagerService>& DrmManagerClientImpl::getDrmManagerService() {
Gloria Wang9e1e9a32011-03-23 22:35:13 -070051 Mutex::Autolock lock(sMutex);
52 if (NULL == sDrmManagerService.get()) {
aimitakeshid074e302010-07-29 10:12:27 +090053 sp<IServiceManager> sm = defaultServiceManager();
54 sp<IBinder> binder;
55 do {
56 binder = sm->getService(String16("drm.drmManager"));
57 if (binder != 0) {
58 break;
59 }
60 LOGW("DrmManagerService not published, waiting...");
61 struct timespec reqt;
62 reqt.tv_sec = 0;
63 reqt.tv_nsec = 500000000; //0.5 sec
64 nanosleep(&reqt, NULL);
65 } while (true);
Gloria Wang9e1e9a32011-03-23 22:35:13 -070066 if (NULL == sDeathNotifier.get()) {
67 sDeathNotifier = new DeathNotifier();
68 }
69 binder->linkToDeath(sDeathNotifier);
70 sDrmManagerService = interface_cast<IDrmManagerService>(binder);
aimitakeshid074e302010-07-29 10:12:27 +090071 }
Gloria Wang9e1e9a32011-03-23 22:35:13 -070072 return sDrmManagerService;
aimitakeshid074e302010-07-29 10:12:27 +090073}
74
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +090075void DrmManagerClientImpl::addClient(int uniqueId) {
76 getDrmManagerService()->addClient(uniqueId);
aimitakeshid074e302010-07-29 10:12:27 +090077}
78
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +090079void DrmManagerClientImpl::removeClient(int uniqueId) {
80 getDrmManagerService()->removeClient(uniqueId);
aimitakeshid074e302010-07-29 10:12:27 +090081}
82
83status_t DrmManagerClientImpl::setOnInfoListener(
84 int uniqueId, const sp<DrmManagerClient::OnInfoListener>& infoListener) {
85 Mutex::Autolock _l(mLock);
86 mOnInfoListener = infoListener;
Takeshi Aimif05913a2010-11-30 16:27:42 +090087 return getDrmManagerService()->setDrmServiceListener(uniqueId,
88 (NULL != infoListener.get()) ? this : NULL);
aimitakeshid074e302010-07-29 10:12:27 +090089}
90
aimitakeshid074e302010-07-29 10:12:27 +090091status_t DrmManagerClientImpl::installDrmEngine(int uniqueId, const String8& drmEngineFile) {
92 status_t status = DRM_ERROR_UNKNOWN;
93 if (EMPTY_STRING != drmEngineFile) {
94 status = getDrmManagerService()->installDrmEngine(uniqueId, drmEngineFile);
95 }
96 return status;
97}
98
99DrmConstraints* DrmManagerClientImpl::getConstraints(
100 int uniqueId, const String8* path, const int action) {
101 DrmConstraints *drmConstraints = NULL;
102 if ((NULL != path) && (EMPTY_STRING != *path)) {
103 drmConstraints = getDrmManagerService()->getConstraints(uniqueId, path, action);
104 }
105 return drmConstraints;
106}
107
Takeshi Aimidc9186562010-11-16 13:56:11 +0900108DrmMetadata* DrmManagerClientImpl::getMetadata(int uniqueId, const String8* path) {
109 DrmMetadata *drmMetadata = NULL;
110 if ((NULL != path) && (EMPTY_STRING != *path)) {
111 drmMetadata = getDrmManagerService()->getMetadata(uniqueId, path);
112 }
113 return drmMetadata;
114}
115
aimitakeshid074e302010-07-29 10:12:27 +0900116bool DrmManagerClientImpl::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
117 bool retCode = false;
118 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
119 retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
120 }
121 return retCode;
122}
123
124DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
125 DrmInfoStatus *drmInfoStatus = NULL;
126 if (NULL != drmInfo) {
127 drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
128 }
129 return drmInfoStatus;
130}
131
132DrmInfo* DrmManagerClientImpl::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
133 DrmInfo* drmInfo = NULL;
134 if (NULL != drmInfoRequest) {
135 drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
136 }
137 return drmInfo;
138}
139
Takeshi Aimidc549d62010-09-20 23:40:41 +0900140status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshid074e302010-07-29 10:12:27 +0900141 const String8& rightsPath, const String8& contentPath) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900142 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900143 if (EMPTY_STRING != contentPath) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900144 status = getDrmManagerService()->saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshid074e302010-07-29 10:12:27 +0900145 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900146 return status;
aimitakeshid074e302010-07-29 10:12:27 +0900147}
148
149String8 DrmManagerClientImpl::getOriginalMimeType(int uniqueId, const String8& path) {
150 String8 mimeType = EMPTY_STRING;
151 if (EMPTY_STRING != path) {
152 mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path);
153 }
154 return mimeType;
155}
156
157int DrmManagerClientImpl::getDrmObjectType(
158 int uniqueId, const String8& path, const String8& mimeType) {
159 int drmOjectType = DrmObjectType::UNKNOWN;
160 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
161 drmOjectType = getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
162 }
163 return drmOjectType;
164}
165
166int DrmManagerClientImpl::checkRightsStatus(
167 int uniqueId, const String8& path, int action) {
168 int rightsStatus = RightsStatus::RIGHTS_INVALID;
169 if (EMPTY_STRING != path) {
170 rightsStatus = getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
171 }
172 return rightsStatus;
173}
174
Takeshi Aimidc549d62010-09-20 23:40:41 +0900175status_t DrmManagerClientImpl::consumeRights(
aimitakeshid074e302010-07-29 10:12:27 +0900176 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900177 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900178 if (NULL != decryptHandle) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900179 status = getDrmManagerService()->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshid074e302010-07-29 10:12:27 +0900180 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900181 return status;
aimitakeshid074e302010-07-29 10:12:27 +0900182}
183
Takeshi Aimidc549d62010-09-20 23:40:41 +0900184status_t DrmManagerClientImpl::setPlaybackStatus(
Gloria Wang5fc3edb2010-11-19 15:19:36 -0800185 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900186 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900187 if (NULL != decryptHandle) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900188 status = getDrmManagerService()->setPlaybackStatus(
aimitakeshid074e302010-07-29 10:12:27 +0900189 uniqueId, decryptHandle, playbackStatus, position);
190 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900191 return status;
aimitakeshid074e302010-07-29 10:12:27 +0900192}
193
194bool DrmManagerClientImpl::validateAction(
195 int uniqueId, const String8& path, int action, const ActionDescription& description) {
196 bool retCode = false;
197 if (EMPTY_STRING != path) {
198 retCode = getDrmManagerService()->validateAction(uniqueId, path, action, description);
199 }
200 return retCode;
201}
202
Takeshi Aimidc549d62010-09-20 23:40:41 +0900203status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
204 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900205 if (EMPTY_STRING != path) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900206 status = getDrmManagerService()->removeRights(uniqueId, path);
aimitakeshid074e302010-07-29 10:12:27 +0900207 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900208 return status;
aimitakeshid074e302010-07-29 10:12:27 +0900209}
210
Takeshi Aimidc549d62010-09-20 23:40:41 +0900211status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
212 return getDrmManagerService()->removeAllRights(uniqueId);
aimitakeshid074e302010-07-29 10:12:27 +0900213}
214
215int DrmManagerClientImpl::openConvertSession(int uniqueId, const String8& mimeType) {
216 int retCode = INVALID_VALUE;
217 if (EMPTY_STRING != mimeType) {
218 retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
219 }
220 return retCode;
221}
222
223DrmConvertedStatus* DrmManagerClientImpl::convertData(
224 int uniqueId, int convertId, const DrmBuffer* inputData) {
225 DrmConvertedStatus* drmConvertedStatus = NULL;
226 if (NULL != inputData) {
227 drmConvertedStatus = getDrmManagerService()->convertData(uniqueId, convertId, inputData);
228 }
229 return drmConvertedStatus;
230}
231
232DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(int uniqueId, int convertId) {
233 return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
234}
235
236status_t DrmManagerClientImpl::getAllSupportInfo(
237 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
238 status_t status = DRM_ERROR_UNKNOWN;
239 if ((NULL != drmSupportInfoArray) && (NULL != length)) {
240 status = getDrmManagerService()->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
241 }
242 return status;
243}
244
245DecryptHandle* DrmManagerClientImpl::openDecryptSession(
Gloria Wang5fc3edb2010-11-19 15:19:36 -0800246 int uniqueId, int fd, off64_t offset, off64_t length) {
aimitakeshid074e302010-07-29 10:12:27 +0900247 return getDrmManagerService()->openDecryptSession(uniqueId, fd, offset, length);
248}
249
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900250DecryptHandle* DrmManagerClientImpl::openDecryptSession(int uniqueId, const char* uri) {
251 DecryptHandle* handle = NULL;
252 if (NULL != uri) {
253 handle = getDrmManagerService()->openDecryptSession(uniqueId, uri);
254 }
255 return handle;
256}
257
Takeshi Aimidc549d62010-09-20 23:40:41 +0900258status_t DrmManagerClientImpl::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
aimitakeshid074e302010-07-29 10:12:27 +0900259 status_t status = DRM_ERROR_UNKNOWN;
Takeshi Aimidc549d62010-09-20 23:40:41 +0900260 if (NULL != decryptHandle) {
261 status = getDrmManagerService()->closeDecryptSession( uniqueId, decryptHandle);
aimitakeshid074e302010-07-29 10:12:27 +0900262 }
263 return status;
264}
265
Takeshi Aimidc549d62010-09-20 23:40:41 +0900266status_t DrmManagerClientImpl::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
267 int decryptUnitId, const DrmBuffer* headerInfo) {
268 status_t status = DRM_ERROR_UNKNOWN;
269 if ((NULL != decryptHandle) && (NULL != headerInfo)) {
270 status = getDrmManagerService()->initializeDecryptUnit(
271 uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshid074e302010-07-29 10:12:27 +0900272 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900273 return status;
274}
275
276status_t DrmManagerClientImpl::decrypt(int uniqueId, DecryptHandle* decryptHandle,
277 int decryptUnitId, const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
278 status_t status = DRM_ERROR_UNKNOWN;
279 if ((NULL != decryptHandle) && (NULL != encBuffer)
280 && (NULL != decBuffer) && (NULL != *decBuffer)) {
281 status = getDrmManagerService()->decrypt(
282 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
283 }
284 return status;
285}
286
287status_t DrmManagerClientImpl::finalizeDecryptUnit(
288 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
289 status_t status = DRM_ERROR_UNKNOWN;
290 if (NULL != decryptHandle) {
291 status
292 = getDrmManagerService()->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
293 }
294 return status;
aimitakeshid074e302010-07-29 10:12:27 +0900295}
296
297ssize_t DrmManagerClientImpl::pread(int uniqueId, DecryptHandle* decryptHandle,
Gloria Wang5fc3edb2010-11-19 15:19:36 -0800298 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshid074e302010-07-29 10:12:27 +0900299 ssize_t retCode = INVALID_VALUE;
300 if ((NULL != decryptHandle) && (NULL != buffer) && (0 < numBytes)) {
301 retCode = getDrmManagerService()->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
302 }
303 return retCode;
304}
305
306status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
307 if (NULL != mOnInfoListener.get()) {
308 Mutex::Autolock _l(mLock);
309 sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
310 listener->onInfo(event);
311 }
312 return DRM_NO_ERROR;
313}
314
Gloria Wang9e1e9a32011-03-23 22:35:13 -0700315DrmManagerClientImpl::DeathNotifier::~DeathNotifier() {
316 Mutex::Autolock lock(sMutex);
317 if (NULL != sDrmManagerService.get()) {
318 sDrmManagerService->asBinder()->unlinkToDeath(this);
319 }
320}
321
322void DrmManagerClientImpl::DeathNotifier::binderDied(const wp<IBinder>& who) {
323 Mutex::Autolock lock(sMutex);
324 DrmManagerClientImpl::sDrmManagerService.clear();
325 LOGW("DrmManager server died!");
326}
327