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