/drm/libdrmframework/DrmManagerClientImpl.cpp

http://github.com/CyanogenMod/android_frameworks_base · C++ · 347 lines · 288 code · 43 blank · 16 comment · 77 complexity · c8f4064940fbedb15be98143b69ba303 MD5 · raw file

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