aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame^] | 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 | |
| 27 | using namespace android; |
| 28 | |
| 29 | #define INVALID_VALUE -1 |
| 30 | |
| 31 | Mutex DrmManagerClientImpl::mMutex; |
| 32 | Vector<int> DrmManagerClientImpl::mUniqueIdVector; |
| 33 | sp<IDrmManagerService> DrmManagerClientImpl::mDrmManagerService; |
| 34 | const String8 DrmManagerClientImpl::EMPTY_STRING(""); |
| 35 | |
| 36 | DrmManagerClientImpl* 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 | |
| 63 | void 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 | |
| 72 | DrmManagerClientImpl::DrmManagerClientImpl() { |
| 73 | |
| 74 | } |
| 75 | |
| 76 | DrmManagerClientImpl::~DrmManagerClientImpl() { |
| 77 | |
| 78 | } |
| 79 | |
| 80 | const 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 | |
| 103 | status_t DrmManagerClientImpl::loadPlugIns(int uniqueId) { |
| 104 | return getDrmManagerService()->loadPlugIns(uniqueId); |
| 105 | } |
| 106 | |
| 107 | status_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 | |
| 115 | status_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 | |
| 122 | status_t DrmManagerClientImpl::unloadPlugIns(int uniqueId) { |
| 123 | return getDrmManagerService()->unloadPlugIns(uniqueId); |
| 124 | } |
| 125 | |
| 126 | status_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 | |
| 134 | DrmConstraints* 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 | |
| 143 | bool 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 | |
| 151 | DrmInfoStatus* 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 | |
| 159 | DrmInfo* 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 | |
| 167 | void 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 | |
| 174 | String8 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 | |
| 182 | int 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 | |
| 191 | int 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 | |
| 200 | void 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 | |
| 207 | void 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 | |
| 215 | bool 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 | |
| 224 | void DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) { |
| 225 | if (EMPTY_STRING != path) { |
| 226 | getDrmManagerService()->removeRights(uniqueId, path); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void DrmManagerClientImpl::removeAllRights(int uniqueId) { |
| 231 | getDrmManagerService()->removeAllRights(uniqueId); |
| 232 | } |
| 233 | |
| 234 | int 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 | |
| 242 | DrmConvertedStatus* 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 | |
| 251 | DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(int uniqueId, int convertId) { |
| 252 | return getDrmManagerService()->closeConvertSession(uniqueId, convertId); |
| 253 | } |
| 254 | |
| 255 | status_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 | |
| 264 | DecryptHandle* 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 | |
| 270 | void DrmManagerClientImpl::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) { |
| 271 | if (NULL != decryptHandle) { |
| 272 | getDrmManagerService()->closeDecryptSession( uniqueId, decryptHandle); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void 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 | |
| 284 | status_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 | |
| 295 | void DrmManagerClientImpl::finalizeDecryptUnit( |
| 296 | int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) { |
| 297 | if (NULL != decryptHandle) { |
| 298 | getDrmManagerService()->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | ssize_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 | |
| 311 | status_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 | |