DrmManagerClientImpl.cpp revision 2272ee27d9022d173b6eab45c409b3c3f57f30ec
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
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
144status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
145            const String8& rightsPath, const String8& contentPath) {
146    status_t status = DRM_ERROR_UNKNOWN;
147    if (EMPTY_STRING != contentPath) {
148        status = getDrmManagerService()->saveRights(uniqueId, drmRights, rightsPath, contentPath);
149    }
150    return status;
151}
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
179status_t DrmManagerClientImpl::consumeRights(
180            int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
181    status_t status = DRM_ERROR_UNKNOWN;
182    if (NULL != decryptHandle) {
183        status = getDrmManagerService()->consumeRights(uniqueId, decryptHandle, action, reserve);
184    }
185    return status;
186}
187
188status_t DrmManagerClientImpl::setPlaybackStatus(
189            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
190    status_t status = DRM_ERROR_UNKNOWN;
191    if (NULL != decryptHandle) {
192        status = getDrmManagerService()->setPlaybackStatus(
193                uniqueId, decryptHandle, playbackStatus, position);
194    }
195    return status;
196}
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
207status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
208    status_t status = DRM_ERROR_UNKNOWN;
209    if (EMPTY_STRING != path) {
210        status = getDrmManagerService()->removeRights(uniqueId, path);
211    }
212    return status;
213}
214
215status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
216    return getDrmManagerService()->removeAllRights(uniqueId);
217}
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) {
251    return getDrmManagerService()->openDecryptSession(uniqueId, fd, offset, length);
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