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