blob: b1228d4f56b0b1030bab2edd609accab25895fc3 [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 "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 Wang8d2577b2011-03-15 10:52:28 -070031Mutex DrmManagerClientImpl::sMutex;
32sp<IDrmManagerService> DrmManagerClientImpl::sDrmManagerService;
33sp<DrmManagerClientImpl::DeathNotifier> DrmManagerClientImpl::sDeathNotifier;
aimitakeshi27ed8ad2010-07-29 10:12:27 +090034const String8 DrmManagerClientImpl::EMPTY_STRING("");
35
Gloria Wang8f001512011-07-21 15:10:22 -070036DrmManagerClientImpl* DrmManagerClientImpl::create(
37 int* pUniqueId, bool isNative) {
38 *pUniqueId = getDrmManagerService()->addUniqueId(isNative);
39
aimitakeshi27ed8ad2010-07-29 10:12:27 +090040 return new DrmManagerClientImpl();
41}
42
43void DrmManagerClientImpl::remove(int uniqueId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +090044 getDrmManagerService()->removeUniqueId(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090045}
46
aimitakeshi27ed8ad2010-07-29 10:12:27 +090047const sp<IDrmManagerService>& DrmManagerClientImpl::getDrmManagerService() {
Gloria Wang8d2577b2011-03-15 10:52:28 -070048 Mutex::Autolock lock(sMutex);
49 if (NULL == sDrmManagerService.get()) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +090050 sp<IServiceManager> sm = defaultServiceManager();
51 sp<IBinder> binder;
52 do {
53 binder = sm->getService(String16("drm.drmManager"));
54 if (binder != 0) {
55 break;
56 }
Steve Block5ff1dd52012-01-05 23:22:43 +000057 ALOGW("DrmManagerService not published, waiting...");
aimitakeshi27ed8ad2010-07-29 10:12:27 +090058 struct timespec reqt;
59 reqt.tv_sec = 0;
60 reqt.tv_nsec = 500000000; //0.5 sec
61 nanosleep(&reqt, NULL);
62 } while (true);
Gloria Wang8d2577b2011-03-15 10:52:28 -070063 if (NULL == sDeathNotifier.get()) {
64 sDeathNotifier = new DeathNotifier();
65 }
66 binder->linkToDeath(sDeathNotifier);
67 sDrmManagerService = interface_cast<IDrmManagerService>(binder);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090068 }
Gloria Wang8d2577b2011-03-15 10:52:28 -070069 return sDrmManagerService;
aimitakeshi27ed8ad2010-07-29 10:12:27 +090070}
71
Takeshi Aimie943f842010-10-08 23:05:49 +090072void DrmManagerClientImpl::addClient(int uniqueId) {
73 getDrmManagerService()->addClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090074}
75
Takeshi Aimie943f842010-10-08 23:05:49 +090076void DrmManagerClientImpl::removeClient(int uniqueId) {
77 getDrmManagerService()->removeClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090078}
79
80status_t DrmManagerClientImpl::setOnInfoListener(
Gloria Wangb5ce3612011-02-24 16:40:57 -080081 int uniqueId,
82 const sp<DrmManagerClient::OnInfoListener>& infoListener) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +090083 Mutex::Autolock _l(mLock);
84 mOnInfoListener = infoListener;
Takeshi Aimic618b5a2010-11-30 16:27:42 +090085 return getDrmManagerService()->setDrmServiceListener(uniqueId,
86 (NULL != infoListener.get()) ? this : NULL);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090087}
88
Gloria Wangb5ce3612011-02-24 16:40:57 -080089status_t DrmManagerClientImpl::installDrmEngine(
90 int uniqueId, const String8& drmEngineFile) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +090091 status_t status = DRM_ERROR_UNKNOWN;
92 if (EMPTY_STRING != drmEngineFile) {
93 status = getDrmManagerService()->installDrmEngine(uniqueId, drmEngineFile);
94 }
95 return status;
96}
97
98DrmConstraints* DrmManagerClientImpl::getConstraints(
99 int uniqueId, const String8* path, const int action) {
100 DrmConstraints *drmConstraints = NULL;
101 if ((NULL != path) && (EMPTY_STRING != *path)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800102 drmConstraints =
103 getDrmManagerService()->getConstraints(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900104 }
105 return drmConstraints;
106}
107
Takeshi Aimi34738462010-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
Gloria Wangb5ce3612011-02-24 16:40:57 -0800116bool DrmManagerClientImpl::canHandle(
117 int uniqueId, const String8& path, const String8& mimeType) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900118 bool retCode = false;
119 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
120 retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
121 }
122 return retCode;
123}
124
Gloria Wangb5ce3612011-02-24 16:40:57 -0800125DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(
126 int uniqueId, const DrmInfo* drmInfo) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900127 DrmInfoStatus *drmInfoStatus = NULL;
128 if (NULL != drmInfo) {
129 drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
130 }
131 return drmInfoStatus;
132}
133
Gloria Wangb5ce3612011-02-24 16:40:57 -0800134DrmInfo* DrmManagerClientImpl::acquireDrmInfo(
135 int uniqueId, const DrmInfoRequest* drmInfoRequest) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900136 DrmInfo* drmInfo = NULL;
137 if (NULL != drmInfoRequest) {
138 drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
139 }
140 return drmInfo;
141}
142
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900143status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900144 const String8& rightsPath, const String8& contentPath) {
Gloria Wang8d2577b2011-03-15 10:52:28 -0700145 return getDrmManagerService()->saveRights(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800146 uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900147}
148
Gloria Wangb5ce3612011-02-24 16:40:57 -0800149String8 DrmManagerClientImpl::getOriginalMimeType(
150 int uniqueId, const String8& path) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900151 String8 mimeType = EMPTY_STRING;
152 if (EMPTY_STRING != path) {
153 mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path);
154 }
155 return mimeType;
156}
157
158int DrmManagerClientImpl::getDrmObjectType(
159 int uniqueId, const String8& path, const String8& mimeType) {
160 int drmOjectType = DrmObjectType::UNKNOWN;
161 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800162 drmOjectType =
163 getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900164 }
165 return drmOjectType;
166}
167
168int DrmManagerClientImpl::checkRightsStatus(
169 int uniqueId, const String8& path, int action) {
170 int rightsStatus = RightsStatus::RIGHTS_INVALID;
171 if (EMPTY_STRING != path) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800172 rightsStatus =
173 getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900174 }
175 return rightsStatus;
176}
177
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900178status_t DrmManagerClientImpl::consumeRights(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800179 int uniqueId, sp<DecryptHandle> &decryptHandle,
180 int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900181 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800182 if (NULL != decryptHandle.get()) {
183 status = getDrmManagerService()->consumeRights(
184 uniqueId, decryptHandle.get(), action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900185 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900186 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900187}
188
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900189status_t DrmManagerClientImpl::setPlaybackStatus(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800190 int uniqueId, sp<DecryptHandle> &decryptHandle,
191 int playbackStatus, int64_t position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900192 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800193 if (NULL != decryptHandle.get()) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900194 status = getDrmManagerService()->setPlaybackStatus(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800195 uniqueId, decryptHandle.get(), playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900196 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900197 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900198}
199
200bool DrmManagerClientImpl::validateAction(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800201 int uniqueId, const String8& path,
202 int action, const ActionDescription& description) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900203 bool retCode = false;
204 if (EMPTY_STRING != path) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800205 retCode = getDrmManagerService()->validateAction(
206 uniqueId, path, action, description);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900207 }
208 return retCode;
209}
210
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900211status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
212 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900213 if (EMPTY_STRING != path) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900214 status = getDrmManagerService()->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900215 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900216 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900217}
218
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900219status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
220 return getDrmManagerService()->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900221}
222
Gloria Wangb5ce3612011-02-24 16:40:57 -0800223int DrmManagerClientImpl::openConvertSession(
224 int uniqueId, const String8& mimeType) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900225 int retCode = INVALID_VALUE;
226 if (EMPTY_STRING != mimeType) {
227 retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
228 }
229 return retCode;
230}
231
232DrmConvertedStatus* DrmManagerClientImpl::convertData(
233 int uniqueId, int convertId, const DrmBuffer* inputData) {
234 DrmConvertedStatus* drmConvertedStatus = NULL;
235 if (NULL != inputData) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800236 drmConvertedStatus =
237 getDrmManagerService()->convertData(uniqueId, convertId, inputData);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900238 }
239 return drmConvertedStatus;
240}
241
Gloria Wangb5ce3612011-02-24 16:40:57 -0800242DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(
243 int uniqueId, int convertId) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900244 return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
245}
246
247status_t DrmManagerClientImpl::getAllSupportInfo(
248 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
249 status_t status = DRM_ERROR_UNKNOWN;
250 if ((NULL != drmSupportInfoArray) && (NULL != length)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800251 status = getDrmManagerService()->getAllSupportInfo(
252 uniqueId, length, drmSupportInfoArray);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900253 }
254 return status;
255}
256
Gloria Wangb5ce3612011-02-24 16:40:57 -0800257sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800258 int uniqueId, int fd, off64_t offset,
259 off64_t length, const char* mime) {
260
261 return getDrmManagerService()->openDecryptSession(
262 uniqueId, fd, offset, length, mime);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900263}
264
Gloria Wangb5ce3612011-02-24 16:40:57 -0800265sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800266 int uniqueId, const char* uri, const char* mime) {
267
Takeshi Aimie943f842010-10-08 23:05:49 +0900268 DecryptHandle* handle = NULL;
269 if (NULL != uri) {
James Dong9d2f3862012-01-10 08:24:37 -0800270 handle = getDrmManagerService()->openDecryptSession(uniqueId, uri, mime);
Takeshi Aimie943f842010-10-08 23:05:49 +0900271 }
272 return handle;
273}
274
Kei Takahashicba7b322012-01-18 17:10:19 +0900275sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
276 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
277 return getDrmManagerService()->openDecryptSession(uniqueId, buf, mimeType);
278}
279
Gloria Wangb5ce3612011-02-24 16:40:57 -0800280status_t DrmManagerClientImpl::closeDecryptSession(
281 int uniqueId, sp<DecryptHandle> &decryptHandle) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900282 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800283 if (NULL != decryptHandle.get()) {
284 status = getDrmManagerService()->closeDecryptSession(
285 uniqueId, decryptHandle.get());
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900286 }
287 return status;
288}
289
Gloria Wangb5ce3612011-02-24 16:40:57 -0800290status_t DrmManagerClientImpl::initializeDecryptUnit(
291 int uniqueId, sp<DecryptHandle> &decryptHandle,
292 int decryptUnitId, const DrmBuffer* headerInfo) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900293 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800294 if ((NULL != decryptHandle.get()) && (NULL != headerInfo)) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900295 status = getDrmManagerService()->initializeDecryptUnit(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800296 uniqueId, decryptHandle.get(), decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900297 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900298 return status;
299}
300
Gloria Wangb5ce3612011-02-24 16:40:57 -0800301status_t DrmManagerClientImpl::decrypt(
302 int uniqueId, sp<DecryptHandle> &decryptHandle,
303 int decryptUnitId, const DrmBuffer* encBuffer,
304 DrmBuffer** decBuffer, DrmBuffer* IV) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900305 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800306 if ((NULL != decryptHandle.get()) && (NULL != encBuffer)
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900307 && (NULL != decBuffer) && (NULL != *decBuffer)) {
308 status = getDrmManagerService()->decrypt(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800309 uniqueId, decryptHandle.get(), decryptUnitId,
310 encBuffer, decBuffer, IV);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900311 }
312 return status;
313}
314
315status_t DrmManagerClientImpl::finalizeDecryptUnit(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800316 int uniqueId, sp<DecryptHandle> &decryptHandle, int decryptUnitId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900317 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800318 if (NULL != decryptHandle.get()) {
319 status = getDrmManagerService()->finalizeDecryptUnit(
320 uniqueId, decryptHandle.get(), decryptUnitId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900321 }
322 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900323}
324
Gloria Wangb5ce3612011-02-24 16:40:57 -0800325ssize_t DrmManagerClientImpl::pread(int uniqueId, sp<DecryptHandle> &decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800326 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900327 ssize_t retCode = INVALID_VALUE;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800328 if ((NULL != decryptHandle.get()) && (NULL != buffer) && (0 < numBytes)) {
329 retCode = getDrmManagerService()->pread(
330 uniqueId, decryptHandle.get(), buffer, numBytes, offset);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900331 }
332 return retCode;
333}
334
335status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
336 if (NULL != mOnInfoListener.get()) {
337 Mutex::Autolock _l(mLock);
338 sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
339 listener->onInfo(event);
340 }
341 return DRM_NO_ERROR;
342}
343
Gloria Wang8d2577b2011-03-15 10:52:28 -0700344DrmManagerClientImpl::DeathNotifier::~DeathNotifier() {
345 Mutex::Autolock lock(sMutex);
346 if (NULL != sDrmManagerService.get()) {
347 sDrmManagerService->asBinder()->unlinkToDeath(this);
348 }
349}
350
351void DrmManagerClientImpl::DeathNotifier::binderDied(const wp<IBinder>& who) {
352 Mutex::Autolock lock(sMutex);
353 DrmManagerClientImpl::sDrmManagerService.clear();
Steve Block5ff1dd52012-01-05 23:22:43 +0000354 ALOGW("DrmManager server died!");
Gloria Wang8d2577b2011-03-15 10:52:28 -0700355}
356