DrmManagerClientImpl.cpp revision 3473846f64f5b28e1cbeb70ef5867073fc93159e
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
27using namespace android;
28
29#define INVALID_VALUE -1
30
31Mutex DrmManagerClientImpl::mMutex;
32sp<IDrmManagerService> DrmManagerClientImpl::mDrmManagerService;
33const String8 DrmManagerClientImpl::EMPTY_STRING("");
34
35DrmManagerClientImpl* DrmManagerClientImpl::create(int* pUniqueId) {
36    if (0 == *pUniqueId) {
37        int uniqueId = getDrmManagerService()->addUniqueId(*pUniqueId);
38        *pUniqueId = uniqueId;
39    } else {
40        getDrmManagerService()->addUniqueId(*pUniqueId);
41    }
42    return new DrmManagerClientImpl();
43}
44
45void DrmManagerClientImpl::remove(int uniqueId) {
46    getDrmManagerService()->removeUniqueId(uniqueId);
47}
48
49const 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
72void DrmManagerClientImpl::addClient(int uniqueId) {
73    getDrmManagerService()->addClient(uniqueId);
74}
75
76void DrmManagerClientImpl::removeClient(int uniqueId) {
77    getDrmManagerService()->removeClient(uniqueId);
78}
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
87status_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
104DrmMetadata* DrmManagerClientImpl::getMetadata(int uniqueId, const String8* path) {
105    DrmMetadata *drmMetadata = NULL;
106    if ((NULL != path) && (EMPTY_STRING != *path)) {
107        drmMetadata = getDrmManagerService()->getMetadata(uniqueId, path);
108    }
109    return drmMetadata;
110}
111
112bool DrmManagerClientImpl::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
113    bool retCode = false;
114    if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
115        retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
116    }
117    return retCode;
118}
119
120DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
121    DrmInfoStatus *drmInfoStatus = NULL;
122    if (NULL != drmInfo) {
123        drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
124    }
125    return drmInfoStatus;
126}
127
128DrmInfo* DrmManagerClientImpl::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
129    DrmInfo* drmInfo = NULL;
130    if (NULL != drmInfoRequest) {
131        drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
132    }
133    return drmInfo;
134}
135
136status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
137            const String8& rightsPath, const String8& contentPath) {
138    status_t status = DRM_ERROR_UNKNOWN;
139    if (EMPTY_STRING != contentPath) {
140        status = getDrmManagerService()->saveRights(uniqueId, drmRights, rightsPath, contentPath);
141    }
142    return status;
143}
144
145String8 DrmManagerClientImpl::getOriginalMimeType(int uniqueId, const String8& path) {
146    String8 mimeType = EMPTY_STRING;
147    if (EMPTY_STRING != path) {
148        mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path);
149    }
150    return mimeType;
151}
152
153int DrmManagerClientImpl::getDrmObjectType(
154            int uniqueId, const String8& path, const String8& mimeType) {
155    int drmOjectType = DrmObjectType::UNKNOWN;
156    if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
157         drmOjectType = getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
158    }
159    return drmOjectType;
160}
161
162int DrmManagerClientImpl::checkRightsStatus(
163            int uniqueId, const String8& path, int action) {
164    int rightsStatus = RightsStatus::RIGHTS_INVALID;
165    if (EMPTY_STRING != path) {
166        rightsStatus = getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
167    }
168    return rightsStatus;
169}
170
171status_t DrmManagerClientImpl::consumeRights(
172            int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
173    status_t status = DRM_ERROR_UNKNOWN;
174    if (NULL != decryptHandle) {
175        status = getDrmManagerService()->consumeRights(uniqueId, decryptHandle, action, reserve);
176    }
177    return status;
178}
179
180status_t DrmManagerClientImpl::setPlaybackStatus(
181            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
182    status_t status = DRM_ERROR_UNKNOWN;
183    if (NULL != decryptHandle) {
184        status = getDrmManagerService()->setPlaybackStatus(
185                uniqueId, decryptHandle, playbackStatus, position);
186    }
187    return status;
188}
189
190bool DrmManagerClientImpl::validateAction(
191            int uniqueId, const String8& path, int action, const ActionDescription& description) {
192    bool retCode = false;
193    if (EMPTY_STRING != path) {
194        retCode = getDrmManagerService()->validateAction(uniqueId, path, action, description);
195    }
196    return retCode;
197}
198
199status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
200    status_t status = DRM_ERROR_UNKNOWN;
201    if (EMPTY_STRING != path) {
202        status = getDrmManagerService()->removeRights(uniqueId, path);
203    }
204    return status;
205}
206
207status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
208    return getDrmManagerService()->removeAllRights(uniqueId);
209}
210
211int DrmManagerClientImpl::openConvertSession(int uniqueId, const String8& mimeType) {
212    int retCode = INVALID_VALUE;
213    if (EMPTY_STRING != mimeType) {
214        retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
215    }
216    return retCode;
217}
218
219DrmConvertedStatus* DrmManagerClientImpl::convertData(
220            int uniqueId, int convertId, const DrmBuffer* inputData) {
221    DrmConvertedStatus* drmConvertedStatus = NULL;
222    if (NULL != inputData) {
223         drmConvertedStatus = getDrmManagerService()->convertData(uniqueId, convertId, inputData);
224    }
225    return drmConvertedStatus;
226}
227
228DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(int uniqueId, int convertId) {
229    return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
230}
231
232status_t DrmManagerClientImpl::getAllSupportInfo(
233            int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
234    status_t status = DRM_ERROR_UNKNOWN;
235    if ((NULL != drmSupportInfoArray) && (NULL != length)) {
236        status = getDrmManagerService()->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
237    }
238    return status;
239}
240
241DecryptHandle* DrmManagerClientImpl::openDecryptSession(
242            int uniqueId, int fd, int offset, int length) {
243    return getDrmManagerService()->openDecryptSession(uniqueId, fd, offset, length);
244}
245
246DecryptHandle* DrmManagerClientImpl::openDecryptSession(int uniqueId, const char* uri) {
247    DecryptHandle* handle = NULL;
248    if (NULL != uri) {
249        handle = getDrmManagerService()->openDecryptSession(uniqueId, uri);
250    }
251    return handle;
252}
253
254status_t DrmManagerClientImpl::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
255    status_t status = DRM_ERROR_UNKNOWN;
256    if (NULL != decryptHandle) {
257        status = getDrmManagerService()->closeDecryptSession( uniqueId, decryptHandle);
258    }
259    return status;
260}
261
262status_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);
268    }
269    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;
291}
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
311