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