1 /*
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
17 //#define LOG_NDEBUG 0
18 #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
27 using namespace android;
28
29 #define INVALID_VALUE -1
30
31 Mutex DrmManagerClientImpl::sMutex;
32 sp<IDrmManagerService> DrmManagerClientImpl::sDrmManagerService;
33 sp<DrmManagerClientImpl::DeathNotifier> DrmManagerClientImpl::sDeathNotifier;
34 const String8 DrmManagerClientImpl::EMPTY_STRING("");
35
create(int * pUniqueId,bool isNative)36 DrmManagerClientImpl* DrmManagerClientImpl::create(
37 int* pUniqueId, bool isNative) {
38 *pUniqueId = getDrmManagerService()->addUniqueId(isNative);
39
40 return new DrmManagerClientImpl();
41 }
42
remove(int uniqueId)43 void DrmManagerClientImpl::remove(int uniqueId) {
44 getDrmManagerService()->removeUniqueId(uniqueId);
45 }
46
getDrmManagerService()47 const sp<IDrmManagerService>& DrmManagerClientImpl::getDrmManagerService() {
48 Mutex::Autolock lock(sMutex);
49 if (NULL == sDrmManagerService.get()) {
50 sp<IServiceManager> sm = defaultServiceManager();
51 sp<IBinder> binder;
52 do {
53 binder = sm->getService(String16("drm.drmManager"));
54 if (binder != 0) {
55 break;
56 }
57 ALOGW("DrmManagerService not published, waiting...");
58 struct timespec reqt;
59 reqt.tv_sec = 0;
60 reqt.tv_nsec = 500000000; //0.5 sec
61 nanosleep(&reqt, NULL);
62 } while (true);
63 if (NULL == sDeathNotifier.get()) {
64 sDeathNotifier = new DeathNotifier();
65 }
66 binder->linkToDeath(sDeathNotifier);
67 sDrmManagerService = interface_cast<IDrmManagerService>(binder);
68 }
69 return sDrmManagerService;
70 }
71
addClient(int uniqueId)72 void DrmManagerClientImpl::addClient(int uniqueId) {
73 getDrmManagerService()->addClient(uniqueId);
74 }
75
removeClient(int uniqueId)76 void DrmManagerClientImpl::removeClient(int uniqueId) {
77 getDrmManagerService()->removeClient(uniqueId);
78 }
79
setOnInfoListener(int uniqueId,const sp<DrmManagerClient::OnInfoListener> & infoListener)80 status_t DrmManagerClientImpl::setOnInfoListener(
81 int uniqueId,
82 const sp<DrmManagerClient::OnInfoListener>& infoListener) {
83 Mutex::Autolock _l(mLock);
84 mOnInfoListener = infoListener;
85 return getDrmManagerService()->setDrmServiceListener(uniqueId,
86 (NULL != infoListener.get()) ? this : NULL);
87 }
88
installDrmEngine(int uniqueId,const String8 & drmEngineFile)89 status_t DrmManagerClientImpl::installDrmEngine(
90 int uniqueId, const String8& drmEngineFile) {
91 status_t status = DRM_ERROR_UNKNOWN;
92 if (EMPTY_STRING != drmEngineFile) {
93 status = getDrmManagerService()->installDrmEngine(uniqueId, drmEngineFile);
94 }
95 return status;
96 }
97
getConstraints(int uniqueId,const String8 * path,const int action)98 DrmConstraints* DrmManagerClientImpl::getConstraints(
99 int uniqueId, const String8* path, const int action) {
100 DrmConstraints *drmConstraints = NULL;
101 if ((NULL != path) && (EMPTY_STRING != *path)) {
102 drmConstraints =
103 getDrmManagerService()->getConstraints(uniqueId, path, action);
104 }
105 return drmConstraints;
106 }
107
getMetadata(int uniqueId,const String8 * path)108 DrmMetadata* 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
canHandle(int uniqueId,const String8 & path,const String8 & mimeType)116 bool DrmManagerClientImpl::canHandle(
117 int uniqueId, const String8& path, const String8& mimeType) {
118 bool retCode = false;
119 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
120 retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
121 }
122 return retCode;
123 }
124
processDrmInfo(int uniqueId,const DrmInfo * drmInfo)125 DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(
126 int uniqueId, const DrmInfo* drmInfo) {
127 DrmInfoStatus *drmInfoStatus = NULL;
128 if (NULL != drmInfo) {
129 drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
130 }
131 return drmInfoStatus;
132 }
133
acquireDrmInfo(int uniqueId,const DrmInfoRequest * drmInfoRequest)134 DrmInfo* DrmManagerClientImpl::acquireDrmInfo(
135 int uniqueId, const DrmInfoRequest* drmInfoRequest) {
136 DrmInfo* drmInfo = NULL;
137 if (NULL != drmInfoRequest) {
138 drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
139 }
140 return drmInfo;
141 }
142
saveRights(int uniqueId,const DrmRights & drmRights,const String8 & rightsPath,const String8 & contentPath)143 status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
144 const String8& rightsPath, const String8& contentPath) {
145 return getDrmManagerService()->saveRights(
146 uniqueId, drmRights, rightsPath, contentPath);
147 }
148
getOriginalMimeType(int uniqueId,const String8 & path)149 String8 DrmManagerClientImpl::getOriginalMimeType(
150 int uniqueId, const String8& path) {
151 String8 mimeType = EMPTY_STRING;
152 if (EMPTY_STRING != path) {
153 mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path);
154 }
155 return mimeType;
156 }
157
getDrmObjectType(int uniqueId,const String8 & path,const String8 & mimeType)158 int DrmManagerClientImpl::getDrmObjectType(
159 int uniqueId, const String8& path, const String8& mimeType) {
160 int drmOjectType = DrmObjectType::UNKNOWN;
161 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
162 drmOjectType =
163 getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
164 }
165 return drmOjectType;
166 }
167
checkRightsStatus(int uniqueId,const String8 & path,int action)168 int DrmManagerClientImpl::checkRightsStatus(
169 int uniqueId, const String8& path, int action) {
170 int rightsStatus = RightsStatus::RIGHTS_INVALID;
171 if (EMPTY_STRING != path) {
172 rightsStatus =
173 getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
174 }
175 return rightsStatus;
176 }
177
consumeRights(int uniqueId,sp<DecryptHandle> & decryptHandle,int action,bool reserve)178 status_t DrmManagerClientImpl::consumeRights(
179 int uniqueId, sp<DecryptHandle> &decryptHandle,
180 int action, bool reserve) {
181 status_t status = DRM_ERROR_UNKNOWN;
182 if (NULL != decryptHandle.get()) {
183 status = getDrmManagerService()->consumeRights(
184 uniqueId, decryptHandle.get(), action, reserve);
185 }
186 return status;
187 }
188
setPlaybackStatus(int uniqueId,sp<DecryptHandle> & decryptHandle,int playbackStatus,int64_t position)189 status_t DrmManagerClientImpl::setPlaybackStatus(
190 int uniqueId, sp<DecryptHandle> &decryptHandle,
191 int playbackStatus, int64_t position) {
192 status_t status = DRM_ERROR_UNKNOWN;
193 if (NULL != decryptHandle.get()) {
194 status = getDrmManagerService()->setPlaybackStatus(
195 uniqueId, decryptHandle.get(), playbackStatus, position);
196 }
197 return status;
198 }
199
validateAction(int uniqueId,const String8 & path,int action,const ActionDescription & description)200 bool DrmManagerClientImpl::validateAction(
201 int uniqueId, const String8& path,
202 int action, const ActionDescription& description) {
203 bool retCode = false;
204 if (EMPTY_STRING != path) {
205 retCode = getDrmManagerService()->validateAction(
206 uniqueId, path, action, description);
207 }
208 return retCode;
209 }
210
removeRights(int uniqueId,const String8 & path)211 status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
212 status_t status = DRM_ERROR_UNKNOWN;
213 if (EMPTY_STRING != path) {
214 status = getDrmManagerService()->removeRights(uniqueId, path);
215 }
216 return status;
217 }
218
removeAllRights(int uniqueId)219 status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
220 return getDrmManagerService()->removeAllRights(uniqueId);
221 }
222
openConvertSession(int uniqueId,const String8 & mimeType)223 int DrmManagerClientImpl::openConvertSession(
224 int uniqueId, const String8& mimeType) {
225 int retCode = INVALID_VALUE;
226 if (EMPTY_STRING != mimeType) {
227 retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
228 }
229 return retCode;
230 }
231
convertData(int uniqueId,int convertId,const DrmBuffer * inputData)232 DrmConvertedStatus* DrmManagerClientImpl::convertData(
233 int uniqueId, int convertId, const DrmBuffer* inputData) {
234 DrmConvertedStatus* drmConvertedStatus = NULL;
235 if (NULL != inputData) {
236 drmConvertedStatus =
237 getDrmManagerService()->convertData(uniqueId, convertId, inputData);
238 }
239 return drmConvertedStatus;
240 }
241
closeConvertSession(int uniqueId,int convertId)242 DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(
243 int uniqueId, int convertId) {
244 return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
245 }
246
getAllSupportInfo(int uniqueId,int * length,DrmSupportInfo ** drmSupportInfoArray)247 status_t DrmManagerClientImpl::getAllSupportInfo(
248 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
249 status_t status = DRM_ERROR_UNKNOWN;
250 if ((NULL != drmSupportInfoArray) && (NULL != length)) {
251 status = getDrmManagerService()->getAllSupportInfo(
252 uniqueId, length, drmSupportInfoArray);
253 }
254 return status;
255 }
256
openDecryptSession(int uniqueId,int fd,off64_t offset,off64_t length,const char * mime)257 sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
258 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);
263 }
264
openDecryptSession(int uniqueId,const char * uri,const char * mime)265 sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
266 int uniqueId, const char* uri, const char* mime) {
267
268 DecryptHandle* handle = NULL;
269 if (NULL != uri) {
270 handle = getDrmManagerService()->openDecryptSession(uniqueId, uri, mime);
271 }
272 return handle;
273 }
274
closeDecryptSession(int uniqueId,sp<DecryptHandle> & decryptHandle)275 status_t DrmManagerClientImpl::closeDecryptSession(
276 int uniqueId, sp<DecryptHandle> &decryptHandle) {
277 status_t status = DRM_ERROR_UNKNOWN;
278 if (NULL != decryptHandle.get()) {
279 status = getDrmManagerService()->closeDecryptSession(
280 uniqueId, decryptHandle.get());
281 }
282 return status;
283 }
284
initializeDecryptUnit(int uniqueId,sp<DecryptHandle> & decryptHandle,int decryptUnitId,const DrmBuffer * headerInfo)285 status_t DrmManagerClientImpl::initializeDecryptUnit(
286 int uniqueId, sp<DecryptHandle> &decryptHandle,
287 int decryptUnitId, const DrmBuffer* headerInfo) {
288 status_t status = DRM_ERROR_UNKNOWN;
289 if ((NULL != decryptHandle.get()) && (NULL != headerInfo)) {
290 status = getDrmManagerService()->initializeDecryptUnit(
291 uniqueId, decryptHandle.get(), decryptUnitId, headerInfo);
292 }
293 return status;
294 }
295
decrypt(int uniqueId,sp<DecryptHandle> & decryptHandle,int decryptUnitId,const DrmBuffer * encBuffer,DrmBuffer ** decBuffer,DrmBuffer * IV)296 status_t DrmManagerClientImpl::decrypt(
297 int uniqueId, sp<DecryptHandle> &decryptHandle,
298 int decryptUnitId, const DrmBuffer* encBuffer,
299 DrmBuffer** decBuffer, DrmBuffer* IV) {
300 status_t status = DRM_ERROR_UNKNOWN;
301 if ((NULL != decryptHandle.get()) && (NULL != encBuffer)
302 && (NULL != decBuffer) && (NULL != *decBuffer)) {
303 status = getDrmManagerService()->decrypt(
304 uniqueId, decryptHandle.get(), decryptUnitId,
305 encBuffer, decBuffer, IV);
306 }
307 return status;
308 }
309
finalizeDecryptUnit(int uniqueId,sp<DecryptHandle> & decryptHandle,int decryptUnitId)310 status_t DrmManagerClientImpl::finalizeDecryptUnit(
311 int uniqueId, sp<DecryptHandle> &decryptHandle, int decryptUnitId) {
312 status_t status = DRM_ERROR_UNKNOWN;
313 if (NULL != decryptHandle.get()) {
314 status = getDrmManagerService()->finalizeDecryptUnit(
315 uniqueId, decryptHandle.get(), decryptUnitId);
316 }
317 return status;
318 }
319
pread(int uniqueId,sp<DecryptHandle> & decryptHandle,void * buffer,ssize_t numBytes,off64_t offset)320 ssize_t DrmManagerClientImpl::pread(int uniqueId, sp<DecryptHandle> &decryptHandle,
321 void* buffer, ssize_t numBytes, off64_t offset) {
322 ssize_t retCode = INVALID_VALUE;
323 if ((NULL != decryptHandle.get()) && (NULL != buffer) && (0 < numBytes)) {
324 retCode = getDrmManagerService()->pread(
325 uniqueId, decryptHandle.get(), buffer, numBytes, offset);
326 }
327 return retCode;
328 }
329
notify(const DrmInfoEvent & event)330 status_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
~DeathNotifier()339 DrmManagerClientImpl::DeathNotifier::~DeathNotifier() {
340 Mutex::Autolock lock(sMutex);
341 if (NULL != sDrmManagerService.get()) {
342 sDrmManagerService->asBinder()->unlinkToDeath(this);
343 }
344 }
345
binderDied(const wp<IBinder> & who)346 void DrmManagerClientImpl::DeathNotifier::binderDied(const wp<IBinder>& who) {
347 Mutex::Autolock lock(sMutex);
348 DrmManagerClientImpl::sDrmManagerService.clear();
349 ALOGW("DrmManager server died!");
350 }
351
352