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 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 49 | const 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 | |
Takeshi Aimi | e943f84 | 2010-10-08 23:05:49 +0900 | [diff] [blame] | 72 | void DrmManagerClientImpl::addClient(int uniqueId) { |
| 73 | getDrmManagerService()->addClient(uniqueId); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 74 | } |
| 75 | |
Takeshi Aimi | e943f84 | 2010-10-08 23:05:49 +0900 | [diff] [blame] | 76 | void DrmManagerClientImpl::removeClient(int uniqueId) { |
| 77 | getDrmManagerService()->removeClient(uniqueId); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | status_t DrmManagerClientImpl::setOnInfoListener( |
| 81 | int uniqueId, const sp<DrmManagerClient::OnInfoListener>& infoListener) { |
| 82 | Mutex::Autolock _l(mLock); |
| 83 | mOnInfoListener = infoListener; |
Takeshi Aimi | c618b5a | 2010-11-30 16:27:42 +0900 | [diff] [blame^] | 84 | return getDrmManagerService()->setDrmServiceListener(uniqueId, |
| 85 | (NULL != infoListener.get()) ? this : NULL); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 86 | } |
| 87 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 88 | status_t DrmManagerClientImpl::installDrmEngine(int uniqueId, const String8& drmEngineFile) { |
| 89 | status_t status = DRM_ERROR_UNKNOWN; |
| 90 | if (EMPTY_STRING != drmEngineFile) { |
| 91 | status = getDrmManagerService()->installDrmEngine(uniqueId, drmEngineFile); |
| 92 | } |
| 93 | return status; |
| 94 | } |
| 95 | |
| 96 | DrmConstraints* DrmManagerClientImpl::getConstraints( |
| 97 | int uniqueId, const String8* path, const int action) { |
| 98 | DrmConstraints *drmConstraints = NULL; |
| 99 | if ((NULL != path) && (EMPTY_STRING != *path)) { |
| 100 | drmConstraints = getDrmManagerService()->getConstraints(uniqueId, path, action); |
| 101 | } |
| 102 | return drmConstraints; |
| 103 | } |
| 104 | |
Takeshi Aimi | 3473846 | 2010-11-16 13:56:11 +0900 | [diff] [blame] | 105 | DrmMetadata* DrmManagerClientImpl::getMetadata(int uniqueId, const String8* path) { |
| 106 | DrmMetadata *drmMetadata = NULL; |
| 107 | if ((NULL != path) && (EMPTY_STRING != *path)) { |
| 108 | drmMetadata = getDrmManagerService()->getMetadata(uniqueId, path); |
| 109 | } |
| 110 | return drmMetadata; |
| 111 | } |
| 112 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 113 | bool DrmManagerClientImpl::canHandle(int uniqueId, const String8& path, const String8& mimeType) { |
| 114 | bool retCode = false; |
| 115 | if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) { |
| 116 | retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType); |
| 117 | } |
| 118 | return retCode; |
| 119 | } |
| 120 | |
| 121 | DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) { |
| 122 | DrmInfoStatus *drmInfoStatus = NULL; |
| 123 | if (NULL != drmInfo) { |
| 124 | drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo); |
| 125 | } |
| 126 | return drmInfoStatus; |
| 127 | } |
| 128 | |
| 129 | DrmInfo* DrmManagerClientImpl::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) { |
| 130 | DrmInfo* drmInfo = NULL; |
| 131 | if (NULL != drmInfoRequest) { |
| 132 | drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest); |
| 133 | } |
| 134 | return drmInfo; |
| 135 | } |
| 136 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 137 | status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights, |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 138 | const String8& rightsPath, const String8& contentPath) { |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 139 | status_t status = DRM_ERROR_UNKNOWN; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 140 | if (EMPTY_STRING != contentPath) { |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 141 | status = getDrmManagerService()->saveRights(uniqueId, drmRights, rightsPath, contentPath); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 142 | } |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 143 | return status; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | String8 DrmManagerClientImpl::getOriginalMimeType(int uniqueId, const String8& path) { |
| 147 | String8 mimeType = EMPTY_STRING; |
| 148 | if (EMPTY_STRING != path) { |
| 149 | mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path); |
| 150 | } |
| 151 | return mimeType; |
| 152 | } |
| 153 | |
| 154 | int DrmManagerClientImpl::getDrmObjectType( |
| 155 | int uniqueId, const String8& path, const String8& mimeType) { |
| 156 | int drmOjectType = DrmObjectType::UNKNOWN; |
| 157 | if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) { |
| 158 | drmOjectType = getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType); |
| 159 | } |
| 160 | return drmOjectType; |
| 161 | } |
| 162 | |
| 163 | int DrmManagerClientImpl::checkRightsStatus( |
| 164 | int uniqueId, const String8& path, int action) { |
| 165 | int rightsStatus = RightsStatus::RIGHTS_INVALID; |
| 166 | if (EMPTY_STRING != path) { |
| 167 | rightsStatus = getDrmManagerService()->checkRightsStatus(uniqueId, path, action); |
| 168 | } |
| 169 | return rightsStatus; |
| 170 | } |
| 171 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 172 | status_t DrmManagerClientImpl::consumeRights( |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 173 | int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) { |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 174 | status_t status = DRM_ERROR_UNKNOWN; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 175 | if (NULL != decryptHandle) { |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 176 | status = getDrmManagerService()->consumeRights(uniqueId, decryptHandle, action, reserve); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 177 | } |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 178 | return status; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 179 | } |
| 180 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 181 | status_t DrmManagerClientImpl::setPlaybackStatus( |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 182 | int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) { |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 183 | status_t status = DRM_ERROR_UNKNOWN; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 184 | if (NULL != decryptHandle) { |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 185 | status = getDrmManagerService()->setPlaybackStatus( |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 186 | uniqueId, decryptHandle, playbackStatus, position); |
| 187 | } |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 188 | return status; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | bool DrmManagerClientImpl::validateAction( |
| 192 | int uniqueId, const String8& path, int action, const ActionDescription& description) { |
| 193 | bool retCode = false; |
| 194 | if (EMPTY_STRING != path) { |
| 195 | retCode = getDrmManagerService()->validateAction(uniqueId, path, action, description); |
| 196 | } |
| 197 | return retCode; |
| 198 | } |
| 199 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 200 | status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) { |
| 201 | status_t status = DRM_ERROR_UNKNOWN; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 202 | if (EMPTY_STRING != path) { |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 203 | status = getDrmManagerService()->removeRights(uniqueId, path); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 204 | } |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 205 | return status; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 206 | } |
| 207 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 208 | status_t DrmManagerClientImpl::removeAllRights(int uniqueId) { |
| 209 | return getDrmManagerService()->removeAllRights(uniqueId); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | int DrmManagerClientImpl::openConvertSession(int uniqueId, const String8& mimeType) { |
| 213 | int retCode = INVALID_VALUE; |
| 214 | if (EMPTY_STRING != mimeType) { |
| 215 | retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType); |
| 216 | } |
| 217 | return retCode; |
| 218 | } |
| 219 | |
| 220 | DrmConvertedStatus* DrmManagerClientImpl::convertData( |
| 221 | int uniqueId, int convertId, const DrmBuffer* inputData) { |
| 222 | DrmConvertedStatus* drmConvertedStatus = NULL; |
| 223 | if (NULL != inputData) { |
| 224 | drmConvertedStatus = getDrmManagerService()->convertData(uniqueId, convertId, inputData); |
| 225 | } |
| 226 | return drmConvertedStatus; |
| 227 | } |
| 228 | |
| 229 | DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(int uniqueId, int convertId) { |
| 230 | return getDrmManagerService()->closeConvertSession(uniqueId, convertId); |
| 231 | } |
| 232 | |
| 233 | status_t DrmManagerClientImpl::getAllSupportInfo( |
| 234 | int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) { |
| 235 | status_t status = DRM_ERROR_UNKNOWN; |
| 236 | if ((NULL != drmSupportInfoArray) && (NULL != length)) { |
| 237 | status = getDrmManagerService()->getAllSupportInfo(uniqueId, length, drmSupportInfoArray); |
| 238 | } |
| 239 | return status; |
| 240 | } |
| 241 | |
| 242 | DecryptHandle* DrmManagerClientImpl::openDecryptSession( |
| 243 | int uniqueId, int fd, int offset, int length) { |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 244 | return getDrmManagerService()->openDecryptSession(uniqueId, fd, offset, length); |
| 245 | } |
| 246 | |
Takeshi Aimi | e943f84 | 2010-10-08 23:05:49 +0900 | [diff] [blame] | 247 | DecryptHandle* DrmManagerClientImpl::openDecryptSession(int uniqueId, const char* uri) { |
| 248 | DecryptHandle* handle = NULL; |
| 249 | if (NULL != uri) { |
| 250 | handle = getDrmManagerService()->openDecryptSession(uniqueId, uri); |
| 251 | } |
| 252 | return handle; |
| 253 | } |
| 254 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 255 | status_t DrmManagerClientImpl::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) { |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 256 | status_t status = DRM_ERROR_UNKNOWN; |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 257 | if (NULL != decryptHandle) { |
| 258 | status = getDrmManagerService()->closeDecryptSession( uniqueId, decryptHandle); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 259 | } |
| 260 | return status; |
| 261 | } |
| 262 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 263 | status_t DrmManagerClientImpl::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle, |
| 264 | int decryptUnitId, const DrmBuffer* headerInfo) { |
| 265 | status_t status = DRM_ERROR_UNKNOWN; |
| 266 | if ((NULL != decryptHandle) && (NULL != headerInfo)) { |
| 267 | status = getDrmManagerService()->initializeDecryptUnit( |
| 268 | uniqueId, decryptHandle, decryptUnitId, headerInfo); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 269 | } |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 270 | return status; |
| 271 | } |
| 272 | |
| 273 | status_t DrmManagerClientImpl::decrypt(int uniqueId, DecryptHandle* decryptHandle, |
| 274 | int decryptUnitId, const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) { |
| 275 | status_t status = DRM_ERROR_UNKNOWN; |
| 276 | if ((NULL != decryptHandle) && (NULL != encBuffer) |
| 277 | && (NULL != decBuffer) && (NULL != *decBuffer)) { |
| 278 | status = getDrmManagerService()->decrypt( |
| 279 | uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV); |
| 280 | } |
| 281 | return status; |
| 282 | } |
| 283 | |
| 284 | status_t DrmManagerClientImpl::finalizeDecryptUnit( |
| 285 | int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) { |
| 286 | status_t status = DRM_ERROR_UNKNOWN; |
| 287 | if (NULL != decryptHandle) { |
| 288 | status |
| 289 | = getDrmManagerService()->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId); |
| 290 | } |
| 291 | return status; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | ssize_t DrmManagerClientImpl::pread(int uniqueId, DecryptHandle* decryptHandle, |
| 295 | void* buffer, ssize_t numBytes, off_t offset) { |
| 296 | ssize_t retCode = INVALID_VALUE; |
| 297 | if ((NULL != decryptHandle) && (NULL != buffer) && (0 < numBytes)) { |
| 298 | retCode = getDrmManagerService()->pread(uniqueId, decryptHandle, buffer, numBytes, offset); |
| 299 | } |
| 300 | return retCode; |
| 301 | } |
| 302 | |
| 303 | status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) { |
| 304 | if (NULL != mOnInfoListener.get()) { |
| 305 | Mutex::Autolock _l(mLock); |
| 306 | sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener; |
| 307 | listener->onInfo(event); |
| 308 | } |
| 309 | return DRM_NO_ERROR; |
| 310 | } |
| 311 | |