blob: 272adcdfaccd0bfe76a3892a3e750fc686a1925a [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
49DrmManagerClientImpl::DrmManagerClientImpl() {
50
51}
52
53DrmManagerClientImpl::~DrmManagerClientImpl() {
54
55}
56
57const sp<IDrmManagerService>& DrmManagerClientImpl::getDrmManagerService() {
58 mMutex.lock();
59 if (NULL == mDrmManagerService.get()) {
60 sp<IServiceManager> sm = defaultServiceManager();
61 sp<IBinder> binder;
62 do {
63 binder = sm->getService(String16("drm.drmManager"));
64 if (binder != 0) {
65 break;
66 }
67 LOGW("DrmManagerService not published, waiting...");
68 struct timespec reqt;
69 reqt.tv_sec = 0;
70 reqt.tv_nsec = 500000000; //0.5 sec
71 nanosleep(&reqt, NULL);
72 } while (true);
73
74 mDrmManagerService = interface_cast<IDrmManagerService>(binder);
75 }
76 mMutex.unlock();
77 return mDrmManagerService;
78}
79
80status_t DrmManagerClientImpl::loadPlugIns(int uniqueId) {
81 return getDrmManagerService()->loadPlugIns(uniqueId);
82}
83
84status_t DrmManagerClientImpl::loadPlugIns(int uniqueId, const String8& plugInDirPath) {
85 status_t status = DRM_ERROR_UNKNOWN;
86 if (EMPTY_STRING != plugInDirPath) {
87 status = getDrmManagerService()->loadPlugIns(uniqueId, plugInDirPath);
88 }
89 return status;
90}
91
92status_t DrmManagerClientImpl::setOnInfoListener(
93 int uniqueId, const sp<DrmManagerClient::OnInfoListener>& infoListener) {
94 Mutex::Autolock _l(mLock);
95 mOnInfoListener = infoListener;
96 return getDrmManagerService()->setDrmServiceListener(uniqueId, this);
97}
98
99status_t DrmManagerClientImpl::unloadPlugIns(int uniqueId) {
100 return getDrmManagerService()->unloadPlugIns(uniqueId);
101}
102
103status_t DrmManagerClientImpl::installDrmEngine(int uniqueId, const String8& drmEngineFile) {
104 status_t status = DRM_ERROR_UNKNOWN;
105 if (EMPTY_STRING != drmEngineFile) {
106 status = getDrmManagerService()->installDrmEngine(uniqueId, drmEngineFile);
107 }
108 return status;
109}
110
111DrmConstraints* DrmManagerClientImpl::getConstraints(
112 int uniqueId, const String8* path, const int action) {
113 DrmConstraints *drmConstraints = NULL;
114 if ((NULL != path) && (EMPTY_STRING != *path)) {
115 drmConstraints = getDrmManagerService()->getConstraints(uniqueId, path, action);
116 }
117 return drmConstraints;
118}
119
120bool DrmManagerClientImpl::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
121 bool retCode = false;
122 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
123 retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
124 }
125 return retCode;
126}
127
128DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
129 DrmInfoStatus *drmInfoStatus = NULL;
130 if (NULL != drmInfo) {
131 drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
132 }
133 return drmInfoStatus;
134}
135
136DrmInfo* DrmManagerClientImpl::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
137 DrmInfo* drmInfo = NULL;
138 if (NULL != drmInfoRequest) {
139 drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
140 }
141 return drmInfo;
142}
143
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900144status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900145 const String8& rightsPath, const String8& contentPath) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900146 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900147 if (EMPTY_STRING != contentPath) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900148 status = getDrmManagerService()->saveRights(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
153String8 DrmManagerClientImpl::getOriginalMimeType(int uniqueId, const String8& path) {
154 String8 mimeType = EMPTY_STRING;
155 if (EMPTY_STRING != path) {
156 mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path);
157 }
158 return mimeType;
159}
160
161int DrmManagerClientImpl::getDrmObjectType(
162 int uniqueId, const String8& path, const String8& mimeType) {
163 int drmOjectType = DrmObjectType::UNKNOWN;
164 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
165 drmOjectType = getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
166 }
167 return drmOjectType;
168}
169
170int DrmManagerClientImpl::checkRightsStatus(
171 int uniqueId, const String8& path, int action) {
172 int rightsStatus = RightsStatus::RIGHTS_INVALID;
173 if (EMPTY_STRING != path) {
174 rightsStatus = getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
175 }
176 return rightsStatus;
177}
178
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900179status_t DrmManagerClientImpl::consumeRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900180 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900181 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900182 if (NULL != decryptHandle) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900183 status = getDrmManagerService()->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900184 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900185 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900186}
187
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900188status_t DrmManagerClientImpl::setPlaybackStatus(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900189 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900190 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900191 if (NULL != decryptHandle) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900192 status = getDrmManagerService()->setPlaybackStatus(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900193 uniqueId, decryptHandle, playbackStatus, position);
194 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900195 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900196}
197
198bool DrmManagerClientImpl::validateAction(
199 int uniqueId, const String8& path, int action, const ActionDescription& description) {
200 bool retCode = false;
201 if (EMPTY_STRING != path) {
202 retCode = getDrmManagerService()->validateAction(uniqueId, path, action, description);
203 }
204 return retCode;
205}
206
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900207status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
208 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900209 if (EMPTY_STRING != path) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900210 status = getDrmManagerService()->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900211 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900212 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900213}
214
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900215status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
216 return getDrmManagerService()->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900217}
218
219int DrmManagerClientImpl::openConvertSession(int uniqueId, const String8& mimeType) {
220 int retCode = INVALID_VALUE;
221 if (EMPTY_STRING != mimeType) {
222 retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
223 }
224 return retCode;
225}
226
227DrmConvertedStatus* DrmManagerClientImpl::convertData(
228 int uniqueId, int convertId, const DrmBuffer* inputData) {
229 DrmConvertedStatus* drmConvertedStatus = NULL;
230 if (NULL != inputData) {
231 drmConvertedStatus = getDrmManagerService()->convertData(uniqueId, convertId, inputData);
232 }
233 return drmConvertedStatus;
234}
235
236DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(int uniqueId, int convertId) {
237 return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
238}
239
240status_t DrmManagerClientImpl::getAllSupportInfo(
241 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
242 status_t status = DRM_ERROR_UNKNOWN;
243 if ((NULL != drmSupportInfoArray) && (NULL != length)) {
244 status = getDrmManagerService()->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
245 }
246 return status;
247}
248
249DecryptHandle* DrmManagerClientImpl::openDecryptSession(
250 int uniqueId, int fd, int offset, int length) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900251 return getDrmManagerService()->openDecryptSession(uniqueId, fd, offset, length);
252}
253
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900254status_t DrmManagerClientImpl::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900255 status_t status = DRM_ERROR_UNKNOWN;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900256 if (NULL != decryptHandle) {
257 status = getDrmManagerService()->closeDecryptSession( uniqueId, decryptHandle);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900258 }
259 return status;
260}
261
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900262status_t DrmManagerClientImpl::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
263 int decryptUnitId, const DrmBuffer* headerInfo) {
264 status_t status = DRM_ERROR_UNKNOWN;
265 if ((NULL != decryptHandle) && (NULL != headerInfo)) {
266 status = getDrmManagerService()->initializeDecryptUnit(
267 uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900268 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900269 return status;
270}
271
272status_t DrmManagerClientImpl::decrypt(int uniqueId, DecryptHandle* decryptHandle,
273 int decryptUnitId, const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
274 status_t status = DRM_ERROR_UNKNOWN;
275 if ((NULL != decryptHandle) && (NULL != encBuffer)
276 && (NULL != decBuffer) && (NULL != *decBuffer)) {
277 status = getDrmManagerService()->decrypt(
278 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
279 }
280 return status;
281}
282
283status_t DrmManagerClientImpl::finalizeDecryptUnit(
284 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
285 status_t status = DRM_ERROR_UNKNOWN;
286 if (NULL != decryptHandle) {
287 status
288 = getDrmManagerService()->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
289 }
290 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900291}
292
293ssize_t DrmManagerClientImpl::pread(int uniqueId, DecryptHandle* decryptHandle,
294 void* buffer, ssize_t numBytes, off_t offset) {
295 ssize_t retCode = INVALID_VALUE;
296 if ((NULL != decryptHandle) && (NULL != buffer) && (0 < numBytes)) {
297 retCode = getDrmManagerService()->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
298 }
299 return retCode;
300}
301
302status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
303 if (NULL != mOnInfoListener.get()) {
304 Mutex::Autolock _l(mLock);
305 sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
306 listener->onInfo(event);
307 }
308 return DRM_NO_ERROR;
309}
310