DrmManagerClientImpl.cpp revision c618b5af98dec06d49374a61a5a94016f9fec2d3
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,
85            (NULL != infoListener.get()) ? this : NULL);
86}
87
88status_t DrmManagerClientImpl::installDrmEngine(int uniqueId, const String8& drmEngineFile) {
89    status_t status = DRM_ERROR_UNKNOWN;
90    if (EMPTY_STRING != drmEngineFile) {
91        status = getDrmManagerService()->installDrmEngine(uniqueId, drmEngineFile);
92    }
93    return status;
94}
95
96DrmConstraints* DrmManagerClientImpl::getConstraints(
97        int uniqueId, const String8* path, const int action) {
98    DrmConstraints *drmConstraints = NULL;
99    if ((NULL != path) && (EMPTY_STRING != *path)) {
100        drmConstraints = getDrmManagerService()->getConstraints(uniqueId, path, action);
101    }
102    return drmConstraints;
103}
104
105DrmMetadata* DrmManagerClientImpl::getMetadata(int uniqueId, const String8* path) {
106    DrmMetadata *drmMetadata = NULL;
107    if ((NULL != path) && (EMPTY_STRING != *path)) {
108        drmMetadata = getDrmManagerService()->getMetadata(uniqueId, path);
109    }
110    return drmMetadata;
111}
112
113bool DrmManagerClientImpl::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
114    bool retCode = false;
115    if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
116        retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
117    }
118    return retCode;
119}
120
121DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
122    DrmInfoStatus *drmInfoStatus = NULL;
123    if (NULL != drmInfo) {
124        drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
125    }
126    return drmInfoStatus;
127}
128
129DrmInfo* DrmManagerClientImpl::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
130    DrmInfo* drmInfo = NULL;
131    if (NULL != drmInfoRequest) {
132        drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
133    }
134    return drmInfo;
135}
136
137status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
138            const String8& rightsPath, const String8& contentPath) {
139    status_t status = DRM_ERROR_UNKNOWN;
140    if (EMPTY_STRING != contentPath) {
141        status = getDrmManagerService()->saveRights(uniqueId, drmRights, rightsPath, contentPath);
142    }
143    return status;
144}
145
146String8 DrmManagerClientImpl::getOriginalMimeType(int uniqueId, const String8& path) {
147    String8 mimeType = EMPTY_STRING;
148    if (EMPTY_STRING != path) {
149        mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path);
150    }
151    return mimeType;
152}
153
154int DrmManagerClientImpl::getDrmObjectType(
155            int uniqueId, const String8& path, const String8& mimeType) {
156    int drmOjectType = DrmObjectType::UNKNOWN;
157    if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
158         drmOjectType = getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
159    }
160    return drmOjectType;
161}
162
163int DrmManagerClientImpl::checkRightsStatus(
164            int uniqueId, const String8& path, int action) {
165    int rightsStatus = RightsStatus::RIGHTS_INVALID;
166    if (EMPTY_STRING != path) {
167        rightsStatus = getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
168    }
169    return rightsStatus;
170}
171
172status_t DrmManagerClientImpl::consumeRights(
173            int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
174    status_t status = DRM_ERROR_UNKNOWN;
175    if (NULL != decryptHandle) {
176        status = getDrmManagerService()->consumeRights(uniqueId, decryptHandle, action, reserve);
177    }
178    return status;
179}
180
181status_t DrmManagerClientImpl::setPlaybackStatus(
182            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
183    status_t status = DRM_ERROR_UNKNOWN;
184    if (NULL != decryptHandle) {
185        status = getDrmManagerService()->setPlaybackStatus(
186                uniqueId, decryptHandle, playbackStatus, position);
187    }
188    return status;
189}
190
191bool DrmManagerClientImpl::validateAction(
192            int uniqueId, const String8& path, int action, const ActionDescription& description) {
193    bool retCode = false;
194    if (EMPTY_STRING != path) {
195        retCode = getDrmManagerService()->validateAction(uniqueId, path, action, description);
196    }
197    return retCode;
198}
199
200status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
201    status_t status = DRM_ERROR_UNKNOWN;
202    if (EMPTY_STRING != path) {
203        status = getDrmManagerService()->removeRights(uniqueId, path);
204    }
205    return status;
206}
207
208status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
209    return getDrmManagerService()->removeAllRights(uniqueId);
210}
211
212int DrmManagerClientImpl::openConvertSession(int uniqueId, const String8& mimeType) {
213    int retCode = INVALID_VALUE;
214    if (EMPTY_STRING != mimeType) {
215        retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
216    }
217    return retCode;
218}
219
220DrmConvertedStatus* DrmManagerClientImpl::convertData(
221            int uniqueId, int convertId, const DrmBuffer* inputData) {
222    DrmConvertedStatus* drmConvertedStatus = NULL;
223    if (NULL != inputData) {
224         drmConvertedStatus = getDrmManagerService()->convertData(uniqueId, convertId, inputData);
225    }
226    return drmConvertedStatus;
227}
228
229DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(int uniqueId, int convertId) {
230    return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
231}
232
233status_t DrmManagerClientImpl::getAllSupportInfo(
234            int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
235    status_t status = DRM_ERROR_UNKNOWN;
236    if ((NULL != drmSupportInfoArray) && (NULL != length)) {
237        status = getDrmManagerService()->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
238    }
239    return status;
240}
241
242DecryptHandle* DrmManagerClientImpl::openDecryptSession(
243            int uniqueId, int fd, int offset, int length) {
244    return getDrmManagerService()->openDecryptSession(uniqueId, fd, offset, length);
245}
246
247DecryptHandle* DrmManagerClientImpl::openDecryptSession(int uniqueId, const char* uri) {
248    DecryptHandle* handle = NULL;
249    if (NULL != uri) {
250        handle = getDrmManagerService()->openDecryptSession(uniqueId, uri);
251    }
252    return handle;
253}
254
255status_t DrmManagerClientImpl::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
256    status_t status = DRM_ERROR_UNKNOWN;
257    if (NULL != decryptHandle) {
258        status = getDrmManagerService()->closeDecryptSession( uniqueId, decryptHandle);
259    }
260    return status;
261}
262
263status_t DrmManagerClientImpl::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
264            int decryptUnitId, const DrmBuffer* headerInfo) {
265    status_t status = DRM_ERROR_UNKNOWN;
266    if ((NULL != decryptHandle) && (NULL != headerInfo)) {
267        status = getDrmManagerService()->initializeDecryptUnit(
268                uniqueId, decryptHandle, decryptUnitId, headerInfo);
269    }
270    return status;
271}
272
273status_t DrmManagerClientImpl::decrypt(int uniqueId, DecryptHandle* decryptHandle,
274            int decryptUnitId, const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
275    status_t status = DRM_ERROR_UNKNOWN;
276    if ((NULL != decryptHandle) && (NULL != encBuffer)
277        && (NULL != decBuffer) && (NULL != *decBuffer)) {
278        status = getDrmManagerService()->decrypt(
279                uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
280    }
281    return status;
282}
283
284status_t DrmManagerClientImpl::finalizeDecryptUnit(
285            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
286    status_t status = DRM_ERROR_UNKNOWN;
287    if (NULL != decryptHandle) {
288        status
289            = getDrmManagerService()->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
290    }
291    return status;
292}
293
294ssize_t DrmManagerClientImpl::pread(int uniqueId, DecryptHandle* decryptHandle,
295            void* buffer, ssize_t numBytes, off_t offset) {
296    ssize_t retCode = INVALID_VALUE;
297    if ((NULL != decryptHandle) && (NULL != buffer) && (0 < numBytes)) {
298        retCode = getDrmManagerService()->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
299    }
300    return retCode;
301}
302
303status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
304    if (NULL != mOnInfoListener.get()) {
305        Mutex::Autolock _l(mLock);
306        sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
307        listener->onInfo(event);
308    }
309    return DRM_NO_ERROR;
310}
311
312