blob: e6ae220fd819fb487ad11138b395ee4a5f6143aa [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
31Mutex DrmManagerClientImpl::mMutex;
aimitakeshi27ed8ad2010-07-29 10:12:27 +090032sp<IDrmManagerService> DrmManagerClientImpl::mDrmManagerService;
33const String8 DrmManagerClientImpl::EMPTY_STRING("");
34
35DrmManagerClientImpl* DrmManagerClientImpl::create(int* pUniqueId) {
36 if (0 == *pUniqueId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +090037 int uniqueId = getDrmManagerService()->addUniqueId(*pUniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090038 *pUniqueId = uniqueId;
Takeshi Aimi2272ee22010-09-20 23:40:41 +090039 } else {
40 getDrmManagerService()->addUniqueId(*pUniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090041 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +090042 return new DrmManagerClientImpl();
43}
44
45void DrmManagerClientImpl::remove(int uniqueId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +090046 getDrmManagerService()->removeUniqueId(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090047}
48
aimitakeshi27ed8ad2010-07-29 10:12:27 +090049const sp<IDrmManagerService>& DrmManagerClientImpl::getDrmManagerService() {
50 mMutex.lock();
51 if (NULL == mDrmManagerService.get()) {
52 sp<IServiceManager> sm = defaultServiceManager();
53 sp<IBinder> binder;
54 do {
55 binder = sm->getService(String16("drm.drmManager"));
56 if (binder != 0) {
57 break;
58 }
59 LOGW("DrmManagerService not published, waiting...");
60 struct timespec reqt;
61 reqt.tv_sec = 0;
62 reqt.tv_nsec = 500000000; //0.5 sec
63 nanosleep(&reqt, NULL);
64 } while (true);
65
66 mDrmManagerService = interface_cast<IDrmManagerService>(binder);
67 }
68 mMutex.unlock();
69 return mDrmManagerService;
70}
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) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900145 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900146 if (EMPTY_STRING != contentPath) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800147 status = getDrmManagerService()->saveRights(
148 uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900149 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900150 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900151}
152
Gloria Wangb5ce3612011-02-24 16:40:57 -0800153String8 DrmManagerClientImpl::getOriginalMimeType(
154 int uniqueId, const String8& path) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900155 String8 mimeType = EMPTY_STRING;
156 if (EMPTY_STRING != path) {
157 mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path);
158 }
159 return mimeType;
160}
161
162int DrmManagerClientImpl::getDrmObjectType(
163 int uniqueId, const String8& path, const String8& mimeType) {
164 int drmOjectType = DrmObjectType::UNKNOWN;
165 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800166 drmOjectType =
167 getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900168 }
169 return drmOjectType;
170}
171
172int DrmManagerClientImpl::checkRightsStatus(
173 int uniqueId, const String8& path, int action) {
174 int rightsStatus = RightsStatus::RIGHTS_INVALID;
175 if (EMPTY_STRING != path) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800176 rightsStatus =
177 getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900178 }
179 return rightsStatus;
180}
181
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900182status_t DrmManagerClientImpl::consumeRights(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800183 int uniqueId, sp<DecryptHandle> &decryptHandle,
184 int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900185 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800186 if (NULL != decryptHandle.get()) {
187 status = getDrmManagerService()->consumeRights(
188 uniqueId, decryptHandle.get(), action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900189 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900190 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900191}
192
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900193status_t DrmManagerClientImpl::setPlaybackStatus(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800194 int uniqueId, sp<DecryptHandle> &decryptHandle,
195 int playbackStatus, int64_t position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900196 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800197 if (NULL != decryptHandle.get()) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900198 status = getDrmManagerService()->setPlaybackStatus(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800199 uniqueId, decryptHandle.get(), playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900200 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900201 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900202}
203
204bool DrmManagerClientImpl::validateAction(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800205 int uniqueId, const String8& path,
206 int action, const ActionDescription& description) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900207 bool retCode = false;
208 if (EMPTY_STRING != path) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800209 retCode = getDrmManagerService()->validateAction(
210 uniqueId, path, action, description);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900211 }
212 return retCode;
213}
214
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900215status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
216 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900217 if (EMPTY_STRING != path) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900218 status = getDrmManagerService()->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900219 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900220 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900221}
222
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900223status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
224 return getDrmManagerService()->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900225}
226
Gloria Wangb5ce3612011-02-24 16:40:57 -0800227int DrmManagerClientImpl::openConvertSession(
228 int uniqueId, const String8& mimeType) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900229 int retCode = INVALID_VALUE;
230 if (EMPTY_STRING != mimeType) {
231 retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
232 }
233 return retCode;
234}
235
236DrmConvertedStatus* DrmManagerClientImpl::convertData(
237 int uniqueId, int convertId, const DrmBuffer* inputData) {
238 DrmConvertedStatus* drmConvertedStatus = NULL;
239 if (NULL != inputData) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800240 drmConvertedStatus =
241 getDrmManagerService()->convertData(uniqueId, convertId, inputData);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900242 }
243 return drmConvertedStatus;
244}
245
Gloria Wangb5ce3612011-02-24 16:40:57 -0800246DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(
247 int uniqueId, int convertId) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900248 return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
249}
250
251status_t DrmManagerClientImpl::getAllSupportInfo(
252 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
253 status_t status = DRM_ERROR_UNKNOWN;
254 if ((NULL != drmSupportInfoArray) && (NULL != length)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800255 status = getDrmManagerService()->getAllSupportInfo(
256 uniqueId, length, drmSupportInfoArray);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900257 }
258 return status;
259}
260
Gloria Wangb5ce3612011-02-24 16:40:57 -0800261sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800262 int uniqueId, int fd, off64_t offset, off64_t length) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900263 return getDrmManagerService()->openDecryptSession(uniqueId, fd, offset, length);
264}
265
Gloria Wangb5ce3612011-02-24 16:40:57 -0800266sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
267 int uniqueId, const char* uri) {
Takeshi Aimie943f842010-10-08 23:05:49 +0900268 DecryptHandle* handle = NULL;
269 if (NULL != uri) {
270 handle = getDrmManagerService()->openDecryptSession(uniqueId, uri);
271 }
272 return handle;
273}
274
Gloria Wangb5ce3612011-02-24 16:40:57 -0800275status_t DrmManagerClientImpl::closeDecryptSession(
276 int uniqueId, sp<DecryptHandle> &decryptHandle) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900277 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800278 if (NULL != decryptHandle.get()) {
279 status = getDrmManagerService()->closeDecryptSession(
280 uniqueId, decryptHandle.get());
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900281 }
282 return status;
283}
284
Gloria Wangb5ce3612011-02-24 16:40:57 -0800285status_t DrmManagerClientImpl::initializeDecryptUnit(
286 int uniqueId, sp<DecryptHandle> &decryptHandle,
287 int decryptUnitId, const DrmBuffer* headerInfo) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900288 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800289 if ((NULL != decryptHandle.get()) && (NULL != headerInfo)) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900290 status = getDrmManagerService()->initializeDecryptUnit(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800291 uniqueId, decryptHandle.get(), decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900292 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900293 return status;
294}
295
Gloria Wangb5ce3612011-02-24 16:40:57 -0800296status_t DrmManagerClientImpl::decrypt(
297 int uniqueId, sp<DecryptHandle> &decryptHandle,
298 int decryptUnitId, const DrmBuffer* encBuffer,
299 DrmBuffer** decBuffer, DrmBuffer* IV) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900300 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800301 if ((NULL != decryptHandle.get()) && (NULL != encBuffer)
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900302 && (NULL != decBuffer) && (NULL != *decBuffer)) {
303 status = getDrmManagerService()->decrypt(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800304 uniqueId, decryptHandle.get(), decryptUnitId,
305 encBuffer, decBuffer, IV);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900306 }
307 return status;
308}
309
310status_t DrmManagerClientImpl::finalizeDecryptUnit(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800311 int uniqueId, sp<DecryptHandle> &decryptHandle, int decryptUnitId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900312 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800313 if (NULL != decryptHandle.get()) {
314 status = getDrmManagerService()->finalizeDecryptUnit(
315 uniqueId, decryptHandle.get(), decryptUnitId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900316 }
317 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900318}
319
Gloria Wangb5ce3612011-02-24 16:40:57 -0800320ssize_t DrmManagerClientImpl::pread(int uniqueId, sp<DecryptHandle> &decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800321 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900322 ssize_t retCode = INVALID_VALUE;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800323 if ((NULL != decryptHandle.get()) && (NULL != buffer) && (0 < numBytes)) {
324 retCode = getDrmManagerService()->pread(
325 uniqueId, decryptHandle.get(), buffer, numBytes, offset);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900326 }
327 return retCode;
328}
329
330status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
331 if (NULL != mOnInfoListener.get()) {
332 Mutex::Autolock _l(mLock);
333 sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
334 listener->onInfo(event);
335 }
336 return DRM_NO_ERROR;
337}
338