blob: eea312b5edb3fdeba40163bc99d1378d2726293a [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(
81 int uniqueId, const sp<DrmManagerClient::OnInfoListener>& infoListener) {
82 Mutex::Autolock _l(mLock);
83 mOnInfoListener = infoListener;
Takeshi Aimic618b5a2010-11-30 16:27:42 +090084 return getDrmManagerService()->setDrmServiceListener(uniqueId,
85 (NULL != infoListener.get()) ? this : NULL);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090086}
87
aimitakeshi27ed8ad2010-07-29 10:12:27 +090088status_t DrmManagerClientImpl::installDrmEngine(int uniqueId, const String8& drmEngineFile) {
89 status_t status = DRM_ERROR_UNKNOWN;
90 if (EMPTY_STRING != drmEngineFile) {
91 status = getDrmManagerService()->installDrmEngine(uniqueId, drmEngineFile);
92 }
93 return status;
94}
95
96DrmConstraints* DrmManagerClientImpl::getConstraints(
97 int uniqueId, const String8* path, const int action) {
98 DrmConstraints *drmConstraints = NULL;
99 if ((NULL != path) && (EMPTY_STRING != *path)) {
100 drmConstraints = getDrmManagerService()->getConstraints(uniqueId, path, action);
101 }
102 return drmConstraints;
103}
104
Takeshi Aimi34738462010-11-16 13:56:11 +0900105DrmMetadata* DrmManagerClientImpl::getMetadata(int uniqueId, const String8* path) {
106 DrmMetadata *drmMetadata = NULL;
107 if ((NULL != path) && (EMPTY_STRING != *path)) {
108 drmMetadata = getDrmManagerService()->getMetadata(uniqueId, path);
109 }
110 return drmMetadata;
111}
112
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900113bool DrmManagerClientImpl::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
114 bool retCode = false;
115 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
116 retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
117 }
118 return retCode;
119}
120
121DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
122 DrmInfoStatus *drmInfoStatus = NULL;
123 if (NULL != drmInfo) {
124 drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
125 }
126 return drmInfoStatus;
127}
128
129DrmInfo* DrmManagerClientImpl::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
130 DrmInfo* drmInfo = NULL;
131 if (NULL != drmInfoRequest) {
132 drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
133 }
134 return drmInfo;
135}
136
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900137status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900138 const String8& rightsPath, const String8& contentPath) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900139 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900140 if (EMPTY_STRING != contentPath) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900141 status = getDrmManagerService()->saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900142 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900143 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900144}
145
146String8 DrmManagerClientImpl::getOriginalMimeType(int uniqueId, const String8& path) {
147 String8 mimeType = EMPTY_STRING;
148 if (EMPTY_STRING != path) {
149 mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path);
150 }
151 return mimeType;
152}
153
154int DrmManagerClientImpl::getDrmObjectType(
155 int uniqueId, const String8& path, const String8& mimeType) {
156 int drmOjectType = DrmObjectType::UNKNOWN;
157 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
158 drmOjectType = getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
159 }
160 return drmOjectType;
161}
162
163int DrmManagerClientImpl::checkRightsStatus(
164 int uniqueId, const String8& path, int action) {
165 int rightsStatus = RightsStatus::RIGHTS_INVALID;
166 if (EMPTY_STRING != path) {
167 rightsStatus = getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
168 }
169 return rightsStatus;
170}
171
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900172status_t DrmManagerClientImpl::consumeRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900173 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900174 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900175 if (NULL != decryptHandle) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900176 status = getDrmManagerService()->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900177 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900178 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900179}
180
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900181status_t DrmManagerClientImpl::setPlaybackStatus(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900182 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900183 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900184 if (NULL != decryptHandle) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900185 status = getDrmManagerService()->setPlaybackStatus(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900186 uniqueId, decryptHandle, playbackStatus, position);
187 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900188 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900189}
190
191bool DrmManagerClientImpl::validateAction(
192 int uniqueId, const String8& path, int action, const ActionDescription& description) {
193 bool retCode = false;
194 if (EMPTY_STRING != path) {
195 retCode = getDrmManagerService()->validateAction(uniqueId, path, action, description);
196 }
197 return retCode;
198}
199
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900200status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
201 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900202 if (EMPTY_STRING != path) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900203 status = getDrmManagerService()->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900204 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900205 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900206}
207
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900208status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
209 return getDrmManagerService()->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900210}
211
212int DrmManagerClientImpl::openConvertSession(int uniqueId, const String8& mimeType) {
213 int retCode = INVALID_VALUE;
214 if (EMPTY_STRING != mimeType) {
215 retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
216 }
217 return retCode;
218}
219
220DrmConvertedStatus* DrmManagerClientImpl::convertData(
221 int uniqueId, int convertId, const DrmBuffer* inputData) {
222 DrmConvertedStatus* drmConvertedStatus = NULL;
223 if (NULL != inputData) {
224 drmConvertedStatus = getDrmManagerService()->convertData(uniqueId, convertId, inputData);
225 }
226 return drmConvertedStatus;
227}
228
229DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(int uniqueId, int convertId) {
230 return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
231}
232
233status_t DrmManagerClientImpl::getAllSupportInfo(
234 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
235 status_t status = DRM_ERROR_UNKNOWN;
236 if ((NULL != drmSupportInfoArray) && (NULL != length)) {
237 status = getDrmManagerService()->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
238 }
239 return status;
240}
241
242DecryptHandle* DrmManagerClientImpl::openDecryptSession(
243 int uniqueId, int fd, int offset, int length) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900244 return getDrmManagerService()->openDecryptSession(uniqueId, fd, offset, length);
245}
246
Takeshi Aimie943f842010-10-08 23:05:49 +0900247DecryptHandle* DrmManagerClientImpl::openDecryptSession(int uniqueId, const char* uri) {
248 DecryptHandle* handle = NULL;
249 if (NULL != uri) {
250 handle = getDrmManagerService()->openDecryptSession(uniqueId, uri);
251 }
252 return handle;
253}
254
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900255status_t DrmManagerClientImpl::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900256 status_t status = DRM_ERROR_UNKNOWN;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900257 if (NULL != decryptHandle) {
258 status = getDrmManagerService()->closeDecryptSession( uniqueId, decryptHandle);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900259 }
260 return status;
261}
262
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900263status_t DrmManagerClientImpl::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
264 int decryptUnitId, const DrmBuffer* headerInfo) {
265 status_t status = DRM_ERROR_UNKNOWN;
266 if ((NULL != decryptHandle) && (NULL != headerInfo)) {
267 status = getDrmManagerService()->initializeDecryptUnit(
268 uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900269 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900270 return status;
271}
272
273status_t DrmManagerClientImpl::decrypt(int uniqueId, DecryptHandle* decryptHandle,
274 int decryptUnitId, const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
275 status_t status = DRM_ERROR_UNKNOWN;
276 if ((NULL != decryptHandle) && (NULL != encBuffer)
277 && (NULL != decBuffer) && (NULL != *decBuffer)) {
278 status = getDrmManagerService()->decrypt(
279 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
280 }
281 return status;
282}
283
284status_t DrmManagerClientImpl::finalizeDecryptUnit(
285 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
286 status_t status = DRM_ERROR_UNKNOWN;
287 if (NULL != decryptHandle) {
288 status
289 = getDrmManagerService()->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
290 }
291 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900292}
293
294ssize_t DrmManagerClientImpl::pread(int uniqueId, DecryptHandle* decryptHandle,
295 void* buffer, ssize_t numBytes, off_t offset) {
296 ssize_t retCode = INVALID_VALUE;
297 if ((NULL != decryptHandle) && (NULL != buffer) && (0 < numBytes)) {
298 retCode = getDrmManagerService()->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
299 }
300 return retCode;
301}
302
303status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
304 if (NULL != mOnInfoListener.get()) {
305 Mutex::Autolock _l(mLock);
306 sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
307 listener->onInfo(event);
308 }
309 return DRM_NO_ERROR;
310}
311