blob: a2cac3f03dc7631fd9ed8e7f1fb23863c9dc78b7 [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();
56 sp<IBinder> binder = sm->getService(String16("drm.drmManager"));
57 if (binder == NULL) {
58 // Do NOT retry; IServiceManager already waits for ~5 seconds
59 // in getService if a service doesn't yet exist.
Tian Tan9ddff082021-07-13 21:17:24 +000060 return sDrmManagerService;
61 }
Gloria Wang8d2577b2011-03-15 10:52:28 -070062 if (NULL == sDeathNotifier.get()) {
63 sDeathNotifier = new DeathNotifier();
64 }
65 binder->linkToDeath(sDeathNotifier);
66 sDrmManagerService = interface_cast<IDrmManagerService>(binder);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090067 }
Gloria Wang8d2577b2011-03-15 10:52:28 -070068 return sDrmManagerService;
aimitakeshi27ed8ad2010-07-29 10:12:27 +090069}
70
Takeshi Aimie943f842010-10-08 23:05:49 +090071void DrmManagerClientImpl::addClient(int uniqueId) {
72 getDrmManagerService()->addClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090073}
74
Takeshi Aimie943f842010-10-08 23:05:49 +090075void DrmManagerClientImpl::removeClient(int uniqueId) {
76 getDrmManagerService()->removeClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090077}
78
79status_t DrmManagerClientImpl::setOnInfoListener(
Gloria Wangb5ce3612011-02-24 16:40:57 -080080 int uniqueId,
81 const sp<DrmManagerClient::OnInfoListener>& infoListener) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +090082 Mutex::Autolock _l(mLock);
83 mOnInfoListener = infoListener;
Takeshi Aimic618b5a2010-11-30 16:27:42 +090084 return getDrmManagerService()->setDrmServiceListener(uniqueId,
85 (NULL != infoListener.get()) ? this : NULL);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090086}
87
aimitakeshi27ed8ad2010-07-29 10:12:27 +090088DrmConstraints* DrmManagerClientImpl::getConstraints(
89 int uniqueId, const String8* path, const int action) {
90 DrmConstraints *drmConstraints = NULL;
91 if ((NULL != path) && (EMPTY_STRING != *path)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -080092 drmConstraints =
93 getDrmManagerService()->getConstraints(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090094 }
95 return drmConstraints;
96}
97
Takeshi Aimi34738462010-11-16 13:56:11 +090098DrmMetadata* DrmManagerClientImpl::getMetadata(int uniqueId, const String8* path) {
99 DrmMetadata *drmMetadata = NULL;
100 if ((NULL != path) && (EMPTY_STRING != *path)) {
101 drmMetadata = getDrmManagerService()->getMetadata(uniqueId, path);
102 }
103 return drmMetadata;
104}
105
Gloria Wangb5ce3612011-02-24 16:40:57 -0800106bool DrmManagerClientImpl::canHandle(
107 int uniqueId, const String8& path, const String8& mimeType) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900108 bool retCode = false;
109 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
110 retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
111 }
112 return retCode;
113}
114
Gloria Wangb5ce3612011-02-24 16:40:57 -0800115DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(
116 int uniqueId, const DrmInfo* drmInfo) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900117 DrmInfoStatus *drmInfoStatus = NULL;
118 if (NULL != drmInfo) {
119 drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
120 }
121 return drmInfoStatus;
122}
123
Gloria Wangb5ce3612011-02-24 16:40:57 -0800124DrmInfo* DrmManagerClientImpl::acquireDrmInfo(
125 int uniqueId, const DrmInfoRequest* drmInfoRequest) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900126 DrmInfo* drmInfo = NULL;
127 if (NULL != drmInfoRequest) {
128 drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
129 }
130 return drmInfo;
131}
132
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900133status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900134 const String8& rightsPath, const String8& contentPath) {
Gloria Wang8d2577b2011-03-15 10:52:28 -0700135 return getDrmManagerService()->saveRights(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800136 uniqueId, drmRights, rightsPath, contentPath);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900137}
138
Gloria Wangb5ce3612011-02-24 16:40:57 -0800139String8 DrmManagerClientImpl::getOriginalMimeType(
James Dongbf5b3b22012-07-30 17:57:39 -0700140 int uniqueId, const String8& path, int fd) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900141 String8 mimeType = EMPTY_STRING;
142 if (EMPTY_STRING != path) {
James Dongbf5b3b22012-07-30 17:57:39 -0700143 mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path, fd);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900144 }
145 return mimeType;
146}
147
148int DrmManagerClientImpl::getDrmObjectType(
149 int uniqueId, const String8& path, const String8& mimeType) {
150 int drmOjectType = DrmObjectType::UNKNOWN;
151 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800152 drmOjectType =
153 getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900154 }
155 return drmOjectType;
156}
157
158int DrmManagerClientImpl::checkRightsStatus(
159 int uniqueId, const String8& path, int action) {
160 int rightsStatus = RightsStatus::RIGHTS_INVALID;
161 if (EMPTY_STRING != path) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800162 rightsStatus =
163 getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900164 }
165 return rightsStatus;
166}
167
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900168status_t DrmManagerClientImpl::consumeRights(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800169 int uniqueId, sp<DecryptHandle> &decryptHandle,
170 int action, bool reserve) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900171 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800172 if (NULL != decryptHandle.get()) {
173 status = getDrmManagerService()->consumeRights(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700174 uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900175 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900176 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900177}
178
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900179status_t DrmManagerClientImpl::setPlaybackStatus(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800180 int uniqueId, sp<DecryptHandle> &decryptHandle,
181 int playbackStatus, int64_t position) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900182 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800183 if (NULL != decryptHandle.get()) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900184 status = getDrmManagerService()->setPlaybackStatus(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700185 uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900186 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900187 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900188}
189
190bool DrmManagerClientImpl::validateAction(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800191 int uniqueId, const String8& path,
192 int action, const ActionDescription& description) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900193 bool retCode = false;
194 if (EMPTY_STRING != path) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800195 retCode = getDrmManagerService()->validateAction(
196 uniqueId, path, action, description);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900197 }
198 return retCode;
199}
200
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900201status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
202 status_t status = DRM_ERROR_UNKNOWN;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900203 if (EMPTY_STRING != path) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900204 status = getDrmManagerService()->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900205 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900206 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900207}
208
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900209status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
210 return getDrmManagerService()->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900211}
212
Gloria Wangb5ce3612011-02-24 16:40:57 -0800213int DrmManagerClientImpl::openConvertSession(
214 int uniqueId, const String8& mimeType) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900215 int retCode = INVALID_VALUE;
216 if (EMPTY_STRING != mimeType) {
217 retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
218 }
219 return retCode;
220}
221
222DrmConvertedStatus* DrmManagerClientImpl::convertData(
223 int uniqueId, int convertId, const DrmBuffer* inputData) {
224 DrmConvertedStatus* drmConvertedStatus = NULL;
225 if (NULL != inputData) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800226 drmConvertedStatus =
227 getDrmManagerService()->convertData(uniqueId, convertId, inputData);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900228 }
229 return drmConvertedStatus;
230}
231
Gloria Wangb5ce3612011-02-24 16:40:57 -0800232DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(
233 int uniqueId, int convertId) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900234 return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
235}
236
237status_t DrmManagerClientImpl::getAllSupportInfo(
238 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
239 status_t status = DRM_ERROR_UNKNOWN;
240 if ((NULL != drmSupportInfoArray) && (NULL != length)) {
Gloria Wangb5ce3612011-02-24 16:40:57 -0800241 status = getDrmManagerService()->getAllSupportInfo(
242 uniqueId, length, drmSupportInfoArray);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900243 }
244 return status;
245}
246
Gloria Wangb5ce3612011-02-24 16:40:57 -0800247sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800248 int uniqueId, int fd, off64_t offset,
249 off64_t length, const char* mime) {
250
251 return getDrmManagerService()->openDecryptSession(
252 uniqueId, fd, offset, length, mime);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900253}
254
Gloria Wangb5ce3612011-02-24 16:40:57 -0800255sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800256 int uniqueId, const char* uri, const char* mime) {
257
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700258 sp<DecryptHandle> handle;
Takeshi Aimie943f842010-10-08 23:05:49 +0900259 if (NULL != uri) {
James Dong9d2f3862012-01-10 08:24:37 -0800260 handle = getDrmManagerService()->openDecryptSession(uniqueId, uri, mime);
Takeshi Aimie943f842010-10-08 23:05:49 +0900261 }
262 return handle;
263}
264
Kei Takahashicba7b322012-01-18 17:10:19 +0900265sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
266 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
267 return getDrmManagerService()->openDecryptSession(uniqueId, buf, mimeType);
268}
269
Gloria Wangb5ce3612011-02-24 16:40:57 -0800270status_t DrmManagerClientImpl::closeDecryptSession(
271 int uniqueId, sp<DecryptHandle> &decryptHandle) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900272 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800273 if (NULL != decryptHandle.get()) {
274 status = getDrmManagerService()->closeDecryptSession(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700275 uniqueId, decryptHandle);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900276 }
277 return status;
278}
279
Gloria Wangb5ce3612011-02-24 16:40:57 -0800280status_t DrmManagerClientImpl::initializeDecryptUnit(
281 int uniqueId, sp<DecryptHandle> &decryptHandle,
282 int decryptUnitId, const DrmBuffer* headerInfo) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900283 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800284 if ((NULL != decryptHandle.get()) && (NULL != headerInfo)) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900285 status = getDrmManagerService()->initializeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700286 uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900287 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900288 return status;
289}
290
Gloria Wangb5ce3612011-02-24 16:40:57 -0800291status_t DrmManagerClientImpl::decrypt(
292 int uniqueId, sp<DecryptHandle> &decryptHandle,
293 int decryptUnitId, const DrmBuffer* encBuffer,
294 DrmBuffer** decBuffer, DrmBuffer* IV) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900295 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800296 if ((NULL != decryptHandle.get()) && (NULL != encBuffer)
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900297 && (NULL != decBuffer) && (NULL != *decBuffer)) {
298 status = getDrmManagerService()->decrypt(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700299 uniqueId, decryptHandle, decryptUnitId,
Gloria Wangb5ce3612011-02-24 16:40:57 -0800300 encBuffer, decBuffer, IV);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900301 }
302 return status;
303}
304
305status_t DrmManagerClientImpl::finalizeDecryptUnit(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800306 int uniqueId, sp<DecryptHandle> &decryptHandle, int decryptUnitId) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900307 status_t status = DRM_ERROR_UNKNOWN;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800308 if (NULL != decryptHandle.get()) {
309 status = getDrmManagerService()->finalizeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700310 uniqueId, decryptHandle, decryptUnitId);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900311 }
312 return status;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900313}
314
Gloria Wangb5ce3612011-02-24 16:40:57 -0800315ssize_t DrmManagerClientImpl::pread(int uniqueId, sp<DecryptHandle> &decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800316 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900317 ssize_t retCode = INVALID_VALUE;
Gloria Wangb5ce3612011-02-24 16:40:57 -0800318 if ((NULL != decryptHandle.get()) && (NULL != buffer) && (0 < numBytes)) {
319 retCode = getDrmManagerService()->pread(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700320 uniqueId, decryptHandle, buffer, numBytes, offset);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900321 }
322 return retCode;
323}
324
325status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
326 if (NULL != mOnInfoListener.get()) {
327 Mutex::Autolock _l(mLock);
328 sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
329 listener->onInfo(event);
330 }
331 return DRM_NO_ERROR;
332}
333
Gloria Wang8d2577b2011-03-15 10:52:28 -0700334DrmManagerClientImpl::DeathNotifier::~DeathNotifier() {
335 Mutex::Autolock lock(sMutex);
336 if (NULL != sDrmManagerService.get()) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800337 IInterface::asBinder(sDrmManagerService)->unlinkToDeath(this);
Gloria Wang8d2577b2011-03-15 10:52:28 -0700338 }
339}
340
Aurimas Liutikasc52ca472016-02-12 13:10:19 -0800341void DrmManagerClientImpl::DeathNotifier::binderDied(
342 const wp<IBinder>& /* who */) {
Gloria Wang8d2577b2011-03-15 10:52:28 -0700343 Mutex::Autolock lock(sMutex);
344 DrmManagerClientImpl::sDrmManagerService.clear();
Steve Block5ff1dd52012-01-05 23:22:43 +0000345 ALOGW("DrmManager server died!");
Gloria Wang8d2577b2011-03-15 10:52:28 -0700346}
347