blob: 519ed67b0271d9a1349d35524a5698c333c3d235 [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;
84 return getDrmManagerService()->setDrmServiceListener(uniqueId, this);
85}
86
aimitakeshi27ed8ad2010-07-29 10:12:27 +090087status_t DrmManagerClientImpl::installDrmEngine(int uniqueId, const String8& drmEngineFile) {
88 status_t status = DRM_ERROR_UNKNOWN;
89 if (EMPTY_STRING != drmEngineFile) {
90 status = getDrmManagerService()->installDrmEngine(uniqueId, drmEngineFile);
91 }
92 return status;
93}
94
95DrmConstraints* DrmManagerClientImpl::getConstraints(
96 int uniqueId, const String8* path, const int action) {
97 DrmConstraints *drmConstraints = NULL;
98 if ((NULL != path) && (EMPTY_STRING != *path)) {
99 drmConstraints = getDrmManagerService()->getConstraints(uniqueId, path, action);
100 }
101 return drmConstraints;
102}
103
104bool DrmManagerClientImpl::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
105 bool retCode = false;
106 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
107 retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
108 }
109 return retCode;
110}
111
112DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
113 DrmInfoStatus *drmInfoStatus = NULL;
114 if (NULL != drmInfo) {
115 drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
116 }
117 return drmInfoStatus;
118}
119
120DrmInfo* DrmManagerClientImpl::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
121 DrmInfo* drmInfo = NULL;
122 if (NULL != drmInfoRequest) {
123 drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
124 }
125 return drmInfo;
126}
127
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900128status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900129 const String8& rightsPath, const String8& contentPath) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900130 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900131 if (EMPTY_STRING != contentPath) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900132 status = getDrmManagerService()->saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900133 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900134 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900135}
136
137String8 DrmManagerClientImpl::getOriginalMimeType(int uniqueId, const String8& path) {
138 String8 mimeType = EMPTY_STRING;
139 if (EMPTY_STRING != path) {
140 mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path);
141 }
142 return mimeType;
143}
144
145int DrmManagerClientImpl::getDrmObjectType(
146 int uniqueId, const String8& path, const String8& mimeType) {
147 int drmOjectType = DrmObjectType::UNKNOWN;
148 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
149 drmOjectType = getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
150 }
151 return drmOjectType;
152}
153
154int DrmManagerClientImpl::checkRightsStatus(
155 int uniqueId, const String8& path, int action) {
156 int rightsStatus = RightsStatus::RIGHTS_INVALID;
157 if (EMPTY_STRING != path) {
158 rightsStatus = getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
159 }
160 return rightsStatus;
161}
162
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900163status_t DrmManagerClientImpl::consumeRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900164 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900165 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900166 if (NULL != decryptHandle) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900167 status = getDrmManagerService()->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900168 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900169 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900170}
171
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900172status_t DrmManagerClientImpl::setPlaybackStatus(
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800173 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
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()->setPlaybackStatus(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900177 uniqueId, decryptHandle, playbackStatus, position);
178 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900179 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900180}
181
182bool DrmManagerClientImpl::validateAction(
183 int uniqueId, const String8& path, int action, const ActionDescription& description) {
184 bool retCode = false;
185 if (EMPTY_STRING != path) {
186 retCode = getDrmManagerService()->validateAction(uniqueId, path, action, description);
187 }
188 return retCode;
189}
190
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900191status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
192 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900193 if (EMPTY_STRING != path) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900194 status = getDrmManagerService()->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900195 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900196 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900197}
198
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900199status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
200 return getDrmManagerService()->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900201}
202
203int DrmManagerClientImpl::openConvertSession(int uniqueId, const String8& mimeType) {
204 int retCode = INVALID_VALUE;
205 if (EMPTY_STRING != mimeType) {
206 retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
207 }
208 return retCode;
209}
210
211DrmConvertedStatus* DrmManagerClientImpl::convertData(
212 int uniqueId, int convertId, const DrmBuffer* inputData) {
213 DrmConvertedStatus* drmConvertedStatus = NULL;
214 if (NULL != inputData) {
215 drmConvertedStatus = getDrmManagerService()->convertData(uniqueId, convertId, inputData);
216 }
217 return drmConvertedStatus;
218}
219
220DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(int uniqueId, int convertId) {
221 return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
222}
223
224status_t DrmManagerClientImpl::getAllSupportInfo(
225 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
226 status_t status = DRM_ERROR_UNKNOWN;
227 if ((NULL != drmSupportInfoArray) && (NULL != length)) {
228 status = getDrmManagerService()->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
229 }
230 return status;
231}
232
233DecryptHandle* DrmManagerClientImpl::openDecryptSession(
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800234 int uniqueId, int fd, off64_t offset, off64_t length) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900235 return getDrmManagerService()->openDecryptSession(uniqueId, fd, offset, length);
236}
237
Takeshi Aimie943f842010-10-08 23:05:49 +0900238DecryptHandle* DrmManagerClientImpl::openDecryptSession(int uniqueId, const char* uri) {
239 DecryptHandle* handle = NULL;
240 if (NULL != uri) {
241 handle = getDrmManagerService()->openDecryptSession(uniqueId, uri);
242 }
243 return handle;
244}
245
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900246status_t DrmManagerClientImpl::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900247 status_t status = DRM_ERROR_UNKNOWN;
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900248 if (NULL != decryptHandle) {
249 status = getDrmManagerService()->closeDecryptSession( uniqueId, decryptHandle);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900250 }
251 return status;
252}
253
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900254status_t DrmManagerClientImpl::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
255 int decryptUnitId, const DrmBuffer* headerInfo) {
256 status_t status = DRM_ERROR_UNKNOWN;
257 if ((NULL != decryptHandle) && (NULL != headerInfo)) {
258 status = getDrmManagerService()->initializeDecryptUnit(
259 uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900260 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900261 return status;
262}
263
264status_t DrmManagerClientImpl::decrypt(int uniqueId, DecryptHandle* decryptHandle,
265 int decryptUnitId, const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
266 status_t status = DRM_ERROR_UNKNOWN;
267 if ((NULL != decryptHandle) && (NULL != encBuffer)
268 && (NULL != decBuffer) && (NULL != *decBuffer)) {
269 status = getDrmManagerService()->decrypt(
270 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
271 }
272 return status;
273}
274
275status_t DrmManagerClientImpl::finalizeDecryptUnit(
276 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
277 status_t status = DRM_ERROR_UNKNOWN;
278 if (NULL != decryptHandle) {
279 status
280 = getDrmManagerService()->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
281 }
282 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900283}
284
285ssize_t DrmManagerClientImpl::pread(int uniqueId, DecryptHandle* decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800286 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900287 ssize_t retCode = INVALID_VALUE;
288 if ((NULL != decryptHandle) && (NULL != buffer) && (0 < numBytes)) {
289 retCode = getDrmManagerService()->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
290 }
291 return retCode;
292}
293
294status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
295 if (NULL != mOnInfoListener.get()) {
296 Mutex::Autolock _l(mLock);
297 sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
298 listener->onInfo(event);
299 }
300 return DRM_NO_ERROR;
301}
302