DrmManagerClientImpl.cpp revision e943f84129326ab885cc7a69dcfa17f766b72b89
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
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
128status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
129            const String8& rightsPath, const String8& contentPath) {
130    status_t status = DRM_ERROR_UNKNOWN;
131    if (EMPTY_STRING != contentPath) {
132        status = getDrmManagerService()->saveRights(uniqueId, drmRights, rightsPath, contentPath);
133    }
134    return status;
135}
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
163status_t DrmManagerClientImpl::consumeRights(
164            int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
165    status_t status = DRM_ERROR_UNKNOWN;
166    if (NULL != decryptHandle) {
167        status = getDrmManagerService()->consumeRights(uniqueId, decryptHandle, action, reserve);
168    }
169    return status;
170}
171
172status_t DrmManagerClientImpl::setPlaybackStatus(
173            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
174    status_t status = DRM_ERROR_UNKNOWN;
175    if (NULL != decryptHandle) {
176        status = getDrmManagerService()->setPlaybackStatus(
177                uniqueId, decryptHandle, playbackStatus, position);
178    }
179    return status;
180}
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
191status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
192    status_t status = DRM_ERROR_UNKNOWN;
193    if (EMPTY_STRING != path) {
194        status = getDrmManagerService()->removeRights(uniqueId, path);
195    }
196    return status;
197}
198
199status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
200    return getDrmManagerService()->removeAllRights(uniqueId);
201}
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(
234            int uniqueId, int fd, int offset, int length) {
235    return getDrmManagerService()->openDecryptSession(uniqueId, fd, offset, length);
236}
237
238DecryptHandle* 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
246status_t DrmManagerClientImpl::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
247    status_t status = DRM_ERROR_UNKNOWN;
248    if (NULL != decryptHandle) {
249        status = getDrmManagerService()->closeDecryptSession( uniqueId, decryptHandle);
250    }
251    return status;
252}
253
254status_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);
260    }
261    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;
283}
284
285ssize_t DrmManagerClientImpl::pread(int uniqueId, DecryptHandle* decryptHandle,
286            void* buffer, ssize_t numBytes, off_t offset) {
287    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
303