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