blob: 6cb0a99129137c6f19a5c5db3aa293b9c60cdf9b [file] [log] [blame]
aimitakeshi27ed8ad2010-07-29 10:12:27 +09001/*
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 Aimi2272ee22010-09-20 23:40:41 +090017//#define LOG_NDEBUG 0
aimitakeshi27ed8ad2010-07-29 10:12:27 +090018#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>
Adam Lesinski927634a2014-06-04 15:14:03 -070024#include <cutils/properties.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090025
26#include "DrmManagerClientImpl.h"
Adam Lesinski927634a2014-06-04 15:14:03 -070027#include "NoOpDrmManagerClientImpl.h"
aimitakeshi27ed8ad2010-07-29 10:12:27 +090028
29using namespace android;
30
Chih-Hung Hsieh92c6b822016-05-17 15:20:14 -070031#define INVALID_VALUE (-1)
aimitakeshi27ed8ad2010-07-29 10:12:27 +090032
Gloria Wang8d2577b2011-03-15 10:52:28 -070033Mutex DrmManagerClientImpl::sMutex;
34sp<IDrmManagerService> DrmManagerClientImpl::sDrmManagerService;
35sp<DrmManagerClientImpl::DeathNotifier> DrmManagerClientImpl::sDeathNotifier;
aimitakeshi27ed8ad2010-07-29 10:12:27 +090036const String8 DrmManagerClientImpl::EMPTY_STRING("");
37
Gloria Wang8f001512011-07-21 15:10:22 -070038DrmManagerClientImpl* DrmManagerClientImpl::create(
39 int* pUniqueId, bool isNative) {
Adam Lesinski927634a2014-06-04 15:14:03 -070040 sp<IDrmManagerService> service = getDrmManagerService();
41 if (service != NULL) {
42 *pUniqueId = getDrmManagerService()->addUniqueId(isNative);
43 return new DrmManagerClientImpl();
44 }
45 return new NoOpDrmManagerClientImpl();
aimitakeshi27ed8ad2010-07-29 10:12:27 +090046}
47
48void DrmManagerClientImpl::remove(int uniqueId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +090049 getDrmManagerService()->removeUniqueId(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090050}
51
aimitakeshi27ed8ad2010-07-29 10:12:27 +090052const sp<IDrmManagerService>& DrmManagerClientImpl::getDrmManagerService() {
Gloria Wang8d2577b2011-03-15 10:52:28 -070053 Mutex::Autolock lock(sMutex);
54 if (NULL == sDrmManagerService.get()) {
Robert Shih0f5969b2021-07-02 18:08:13 -070055 sp<IServiceManager> sm = defaultServiceManager();
Robert Shih5450ceb2021-08-11 11:36:12 -070056 sp<IBinder> binder = sm->checkService(String16("drm.drmManager"));
Robert Shih0f5969b2021-07-02 18:08:13 -070057 if (binder == NULL) {
Tian Tan9ddff082021-07-13 21:17:24 +000058 return sDrmManagerService;
59 }
Gloria Wang8d2577b2011-03-15 10:52:28 -070060 if (NULL == sDeathNotifier.get()) {
61 sDeathNotifier = new DeathNotifier();
62 }
63 binder->linkToDeath(sDeathNotifier);
64 sDrmManagerService = interface_cast<IDrmManagerService>(binder);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090065 }
Gloria Wang8d2577b2011-03-15 10:52:28 -070066 return sDrmManagerService;
aimitakeshi27ed8ad2010-07-29 10:12:27 +090067}
68
Takeshi Aimie943f842010-10-08 23:05:49 +090069void DrmManagerClientImpl::addClient(int uniqueId) {
70 getDrmManagerService()->addClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090071}
72
Takeshi Aimie943f842010-10-08 23:05:49 +090073void DrmManagerClientImpl::removeClient(int uniqueId) {
74 getDrmManagerService()->removeClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090075}
76
77status_t DrmManagerClientImpl::setOnInfoListener(
Gloria Wangb5ce3612011-02-24 16:40:57 -080078 int uniqueId,
79 const sp<DrmManagerClient::OnInfoListener>& infoListener) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +090080 Mutex::Autolock _l(mLock);
81 mOnInfoListener = infoListener;
Takeshi Aimic618b5a2010-11-30 16:27:42 +090082 return getDrmManagerService()->setDrmServiceListener(uniqueId,
83 (NULL != infoListener.get()) ? this : NULL);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090084}
85
aimitakeshi27ed8ad2010-07-29 10:12:27 +090086DrmConstraints* DrmManagerClientImpl::getConstraints(
87 int uniqueId, const String8* path, const int action) {
88 DrmConstraints *drmConstraints = NULL;
89 if ((NULL != path) && (EMPTY_STRING != *path)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -080090 drmConstraints =
91 getDrmManagerService()->getConstraints(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090092 }
93 return drmConstraints;
94}
95
Takeshi Aimi34738462010-11-16 13:56:11 +090096DrmMetadata* DrmManagerClientImpl::getMetadata(int uniqueId, const String8* path) {
97 DrmMetadata *drmMetadata = NULL;
98 if ((NULL != path) && (EMPTY_STRING != *path)) {
99 drmMetadata = getDrmManagerService()->getMetadata(uniqueId, path);
100 }
101 return drmMetadata;
102}
103
Gloria Wangb5ce3612011-02-24 16:40:57 -0800104bool DrmManagerClientImpl::canHandle(
105 int uniqueId, const String8& path, const String8& mimeType) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900106 bool retCode = false;
107 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
108 retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
109 }
110 return retCode;
111}
112
Gloria Wangb5ce3612011-02-24 16:40:57 -0800113DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(
114 int uniqueId, const DrmInfo* drmInfo) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900115 DrmInfoStatus *drmInfoStatus = NULL;
116 if (NULL != drmInfo) {
117 drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
118 }
119 return drmInfoStatus;
120}
121
Gloria Wangb5ce3612011-02-24 16:40:57 -0800122DrmInfo* DrmManagerClientImpl::acquireDrmInfo(
123 int uniqueId, const DrmInfoRequest* drmInfoRequest) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900124 DrmInfo* drmInfo = NULL;
125 if (NULL != drmInfoRequest) {
126 drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
127 }
128 return drmInfo;
129}
130
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900131status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900132 const String8& rightsPath, const String8& contentPath) {
Gloria Wang8d2577b2011-03-15 10:52:28 -0700133 return getDrmManagerService()->saveRights(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800134 uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900135}
136
Gloria Wangb5ce3612011-02-24 16:40:57 -0800137String8 DrmManagerClientImpl::getOriginalMimeType(
James Dongbf5b3b22012-07-30 17:57:39 -0700138 int uniqueId, const String8& path, int fd) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900139 String8 mimeType = EMPTY_STRING;
140 if (EMPTY_STRING != path) {
James Dongbf5b3b22012-07-30 17:57:39 -0700141 mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path, fd);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900142 }
143 return mimeType;
144}
145
146int DrmManagerClientImpl::getDrmObjectType(
147 int uniqueId, const String8& path, const String8& mimeType) {
148 int drmOjectType = DrmObjectType::UNKNOWN;
149 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800150 drmOjectType =
151 getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900152 }
153 return drmOjectType;
154}
155
156int DrmManagerClientImpl::checkRightsStatus(
157 int uniqueId, const String8& path, int action) {
158 int rightsStatus = RightsStatus::RIGHTS_INVALID;
159 if (EMPTY_STRING != path) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800160 rightsStatus =
161 getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900162 }
163 return rightsStatus;
164}
165
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900166status_t DrmManagerClientImpl::consumeRights(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800167 int uniqueId, sp<DecryptHandle> &decryptHandle,
168 int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900169 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800170 if (NULL != decryptHandle.get()) {
171 status = getDrmManagerService()->consumeRights(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700172 uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900173 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900174 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900175}
176
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900177status_t DrmManagerClientImpl::setPlaybackStatus(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800178 int uniqueId, sp<DecryptHandle> &decryptHandle,
179 int playbackStatus, int64_t position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900180 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800181 if (NULL != decryptHandle.get()) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900182 status = getDrmManagerService()->setPlaybackStatus(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700183 uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900184 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900185 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900186}
187
188bool DrmManagerClientImpl::validateAction(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800189 int uniqueId, const String8& path,
190 int action, const ActionDescription& description) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900191 bool retCode = false;
192 if (EMPTY_STRING != path) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800193 retCode = getDrmManagerService()->validateAction(
194 uniqueId, path, action, description);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900195 }
196 return retCode;
197}
198
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900199status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
200 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900201 if (EMPTY_STRING != path) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900202 status = getDrmManagerService()->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900203 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900204 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900205}
206
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900207status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
208 return getDrmManagerService()->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900209}
210
Gloria Wangb5ce3612011-02-24 16:40:57 -0800211int DrmManagerClientImpl::openConvertSession(
212 int uniqueId, const String8& mimeType) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900213 int retCode = INVALID_VALUE;
214 if (EMPTY_STRING != mimeType) {
215 retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
216 }
217 return retCode;
218}
219
220DrmConvertedStatus* DrmManagerClientImpl::convertData(
221 int uniqueId, int convertId, const DrmBuffer* inputData) {
222 DrmConvertedStatus* drmConvertedStatus = NULL;
223 if (NULL != inputData) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800224 drmConvertedStatus =
225 getDrmManagerService()->convertData(uniqueId, convertId, inputData);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900226 }
227 return drmConvertedStatus;
228}
229
Gloria Wangb5ce3612011-02-24 16:40:57 -0800230DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(
231 int uniqueId, int convertId) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900232 return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
233}
234
235status_t DrmManagerClientImpl::getAllSupportInfo(
236 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
237 status_t status = DRM_ERROR_UNKNOWN;
238 if ((NULL != drmSupportInfoArray) && (NULL != length)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800239 status = getDrmManagerService()->getAllSupportInfo(
240 uniqueId, length, drmSupportInfoArray);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900241 }
242 return status;
243}
244
Gloria Wangb5ce3612011-02-24 16:40:57 -0800245sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800246 int uniqueId, int fd, off64_t offset,
247 off64_t length, const char* mime) {
248
249 return getDrmManagerService()->openDecryptSession(
250 uniqueId, fd, offset, length, mime);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900251}
252
Gloria Wangb5ce3612011-02-24 16:40:57 -0800253sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800254 int uniqueId, const char* uri, const char* mime) {
255
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700256 sp<DecryptHandle> handle;
Takeshi Aimie943f842010-10-08 23:05:49 +0900257 if (NULL != uri) {
James Dong9d2f3862012-01-10 08:24:37 -0800258 handle = getDrmManagerService()->openDecryptSession(uniqueId, uri, mime);
Takeshi Aimie943f842010-10-08 23:05:49 +0900259 }
260 return handle;
261}
262
Kei Takahashicba7b322012-01-18 17:10:19 +0900263sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
264 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
265 return getDrmManagerService()->openDecryptSession(uniqueId, buf, mimeType);
266}
267
Gloria Wangb5ce3612011-02-24 16:40:57 -0800268status_t DrmManagerClientImpl::closeDecryptSession(
269 int uniqueId, sp<DecryptHandle> &decryptHandle) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900270 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800271 if (NULL != decryptHandle.get()) {
272 status = getDrmManagerService()->closeDecryptSession(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700273 uniqueId, decryptHandle);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900274 }
275 return status;
276}
277
Gloria Wangb5ce3612011-02-24 16:40:57 -0800278status_t DrmManagerClientImpl::initializeDecryptUnit(
279 int uniqueId, sp<DecryptHandle> &decryptHandle,
280 int decryptUnitId, const DrmBuffer* headerInfo) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900281 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800282 if ((NULL != decryptHandle.get()) && (NULL != headerInfo)) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900283 status = getDrmManagerService()->initializeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700284 uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900285 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900286 return status;
287}
288
Gloria Wangb5ce3612011-02-24 16:40:57 -0800289status_t DrmManagerClientImpl::decrypt(
290 int uniqueId, sp<DecryptHandle> &decryptHandle,
291 int decryptUnitId, const DrmBuffer* encBuffer,
292 DrmBuffer** decBuffer, DrmBuffer* IV) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900293 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800294 if ((NULL != decryptHandle.get()) && (NULL != encBuffer)
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900295 && (NULL != decBuffer) && (NULL != *decBuffer)) {
296 status = getDrmManagerService()->decrypt(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700297 uniqueId, decryptHandle, decryptUnitId,
Gloria Wangb5ce3612011-02-24 16:40:57 -0800298 encBuffer, decBuffer, IV);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900299 }
300 return status;
301}
302
303status_t DrmManagerClientImpl::finalizeDecryptUnit(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800304 int uniqueId, sp<DecryptHandle> &decryptHandle, int decryptUnitId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900305 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800306 if (NULL != decryptHandle.get()) {
307 status = getDrmManagerService()->finalizeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700308 uniqueId, decryptHandle, decryptUnitId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900309 }
310 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900311}
312
Gloria Wangb5ce3612011-02-24 16:40:57 -0800313ssize_t DrmManagerClientImpl::pread(int uniqueId, sp<DecryptHandle> &decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800314 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900315 ssize_t retCode = INVALID_VALUE;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800316 if ((NULL != decryptHandle.get()) && (NULL != buffer) && (0 < numBytes)) {
317 retCode = getDrmManagerService()->pread(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700318 uniqueId, decryptHandle, buffer, numBytes, offset);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900319 }
320 return retCode;
321}
322
323status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
324 if (NULL != mOnInfoListener.get()) {
325 Mutex::Autolock _l(mLock);
326 sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
327 listener->onInfo(event);
328 }
329 return DRM_NO_ERROR;
330}
331
Gloria Wang8d2577b2011-03-15 10:52:28 -0700332DrmManagerClientImpl::DeathNotifier::~DeathNotifier() {
333 Mutex::Autolock lock(sMutex);
334 if (NULL != sDrmManagerService.get()) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800335 IInterface::asBinder(sDrmManagerService)->unlinkToDeath(this);
Gloria Wang8d2577b2011-03-15 10:52:28 -0700336 }
337}
338
Aurimas Liutikasc52ca472016-02-12 13:10:19 -0800339void DrmManagerClientImpl::DeathNotifier::binderDied(
340 const wp<IBinder>& /* who */) {
Gloria Wang8d2577b2011-03-15 10:52:28 -0700341 Mutex::Autolock lock(sMutex);
342 DrmManagerClientImpl::sDrmManagerService.clear();
Steve Block5ff1dd52012-01-05 23:22:43 +0000343 ALOGW("DrmManager server died!");
Gloria Wang8d2577b2011-03-15 10:52:28 -0700344}
345