blob: f3d931577856965b24c53f2d5b27a3d2521fdf03 [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
17#ifndef __DRM_MANAGER_CLIENT_IMPL_H__
18#define __DRM_MANAGER_CLIENT_IMPL_H__
19
20#include <binder/IMemory.h>
21#include <utils/threads.h>
22#include <drm/DrmManagerClient.h>
23
24#include "IDrmManagerService.h"
25
26namespace android {
27
28class DrmInfoEvent;
29/**
30 * This is implementation class for DrmManagerClient class.
31 *
32 * Only the JNI layer creates an instance of this class to delegate
33 * functionality to Native later.
34 *
35 */
36class DrmManagerClientImpl : public BnDrmServiceListener {
37private:
Takeshi Aimie943f842010-10-08 23:05:49 +090038 DrmManagerClientImpl() { }
aimitakeshi27ed8ad2010-07-29 10:12:27 +090039
40public:
Gloria Wang8f001512011-07-21 15:10:22 -070041 static DrmManagerClientImpl* create(int* pUniqueId, bool isNative);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090042
43 static void remove(int uniqueId);
44
Takeshi Aimie943f842010-10-08 23:05:49 +090045 virtual ~DrmManagerClientImpl() { }
aimitakeshi27ed8ad2010-07-29 10:12:27 +090046
47public:
48 /**
Takeshi Aimie943f842010-10-08 23:05:49 +090049 * Adds the client respective to given unique id.
aimitakeshi27ed8ad2010-07-29 10:12:27 +090050 *
51 * @param[in] uniqueId Unique identifier for a session
aimitakeshi27ed8ad2010-07-29 10:12:27 +090052 */
Takeshi Aimie943f842010-10-08 23:05:49 +090053 void addClient(int uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090054
55 /**
Takeshi Aimie943f842010-10-08 23:05:49 +090056 * Removes the client respective to given unique id.
aimitakeshi27ed8ad2010-07-29 10:12:27 +090057 *
58 * @param[in] uniqueId Unique identifier for a session
aimitakeshi27ed8ad2010-07-29 10:12:27 +090059 */
Takeshi Aimie943f842010-10-08 23:05:49 +090060 void removeClient(int uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090061
62 /**
63 * Register a callback to be invoked when the caller required to
64 * receive necessary information
65 *
66 * @param[in] uniqueId Unique identifier for a session
67 * @param[in] infoListener Listener
68 * @return status_t
69 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
70 */
71 status_t setOnInfoListener(
72 int uniqueId, const sp<DrmManagerClient::OnInfoListener>& infoListener);
73
74 /**
75 * Get constraint information associated with input content
76 *
77 * @param[in] uniqueId Unique identifier for a session
78 * @param[in] path Path of the protected content
79 * @param[in] action Actions defined such as,
80 * Action::DEFAULT, Action::PLAY, etc
81 * @return DrmConstraints
82 * key-value pairs of constraint are embedded in it
83 * @note
84 * In case of error, return NULL
85 */
86 DrmConstraints* getConstraints(int uniqueId, const String8* path, const int action);
87
88 /**
Takeshi Aimi34738462010-11-16 13:56:11 +090089 * Get metadata information associated with input content.
90 *
91 * @param[in] uniqueId Unique identifier for a session
92 * @param[in] path Path of the protected content
93 * @return DrmMetadata
94 * key-value pairs of metadata are embedded in it
95 * @note
96 * In case of error, return NULL
97 */
98 DrmMetadata* getMetadata(int uniqueId, const String8* path);
99
100 /**
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900101 * Check whether the given mimetype or path can be handled
102 *
103 * @param[in] uniqueId Unique identifier for a session
104 * @param[in] path Path of the content needs to be handled
105 * @param[in] mimetype Mimetype of the content needs to be handled
106 * @return
107 * True if DrmManager can handle given path or mime type.
108 */
109 bool canHandle(int uniqueId, const String8& path, const String8& mimeType);
110
111 /**
112 * Executes given drm information based on its type
113 *
114 * @param[in] uniqueId Unique identifier for a session
115 * @param[in] drmInfo Information needs to be processed
116 * @return DrmInfoStatus
117 * instance as a result of processing given input
118 */
119 DrmInfoStatus* processDrmInfo(int uniqueId, const DrmInfo* drmInfo);
120
121 /**
122 * Retrieves necessary information for registration, unregistration or rights
123 * acquisition information.
124 *
125 * @param[in] uniqueId Unique identifier for a session
126 * @param[in] drmInfoRequest Request information to retrieve drmInfo
127 * @return DrmInfo
128 * instance as a result of processing given input
129 */
130 DrmInfo* acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest);
131
132 /**
133 * Save DRM rights to specified rights path
134 * and make association with content path
135 *
136 * @param[in] uniqueId Unique identifier for a session
137 * @param[in] drmRights DrmRights to be saved
138 * @param[in] rightsPath File path where rights to be saved
139 * @param[in] contentPath File path where content was saved
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900140 * @return status_t
141 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900142 */
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900143 status_t saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900144 const String8& rightsPath, const String8& contentPath);
145
146 /**
147 * Retrieves the mime type embedded inside the original content
148 *
149 * @param[in] uniqueId Unique identifier for a session
150 * @param[in] path the path of the protected content
151 * @return String8
152 * Returns mime-type of the original content, such as "video/mpeg"
153 */
154 String8 getOriginalMimeType(int uniqueId, const String8& path);
155
156 /**
157 * Retrieves the type of the protected object (content, rights, etc..)
158 * using specified path or mimetype. At least one parameter should be non null
159 * to retrieve DRM object type
160 *
161 * @param[in] uniqueId Unique identifier for a session
162 * @param[in] path Path of the content or null.
163 * @param[in] mimeType Mime type of the content or null.
164 * @return type of the DRM content,
165 * such as DrmObjectType::CONTENT, DrmObjectType::RIGHTS_OBJECT
166 */
167 int getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType);
168
169 /**
170 * Check whether the given content has valid rights or not
171 *
172 * @param[in] uniqueId Unique identifier for a session
173 * @param[in] path Path of the protected content
174 * @param[in] action Action to perform (Action::DEFAULT, Action::PLAY, etc)
175 * @return the status of the rights for the protected content,
176 * such as RightsStatus::RIGHTS_VALID, RightsStatus::RIGHTS_EXPIRED, etc.
177 */
178 int checkRightsStatus(int uniqueId, const String8& path, int action);
179
180 /**
181 * Consumes the rights for a content.
182 * If the reserve parameter is true the rights is reserved until the same
183 * application calls this api again with the reserve parameter set to false.
184 *
185 * @param[in] uniqueId Unique identifier for a session
186 * @param[in] decryptHandle Handle for the decryption session
187 * @param[in] action Action to perform. (Action::DEFAULT, Action::PLAY, etc)
188 * @param[in] reserve True if the rights should be reserved.
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900189 * @return status_t
190 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900191 */
Gloria Wangb5ce3612011-02-24 16:40:57 -0800192 status_t consumeRights(int uniqueId, sp<DecryptHandle> &decryptHandle, int action, bool reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900193
194 /**
195 * Informs the DRM engine about the playback actions performed on the DRM files.
196 *
197 * @param[in] uniqueId Unique identifier for a session
198 * @param[in] decryptHandle Handle for the decryption session
199 * @param[in] playbackStatus Playback action (Playback::START, Playback::STOP, Playback::PAUSE)
200 * @param[in] position Position in the file (in milliseconds) where the start occurs.
201 * Only valid together with Playback::START.
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900202 * @return status_t
203 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900204 */
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900205 status_t setPlaybackStatus(
Gloria Wangb5ce3612011-02-24 16:40:57 -0800206 int uniqueId, sp<DecryptHandle> &decryptHandle, int playbackStatus, int64_t position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900207
208 /**
209 * Validates whether an action on the DRM content is allowed or not.
210 *
211 * @param[in] uniqueId Unique identifier for a session
212 * @param[in] path Path of the protected content
213 * @param[in] action Action to validate (Action::PLAY, Action::TRANSFER, etc)
214 * @param[in] description Detailed description of the action
215 * @return true if the action is allowed.
216 */
217 bool validateAction(
218 int uniqueId, const String8& path, int action, const ActionDescription& description);
219
220 /**
221 * Removes the rights associated with the given protected content
222 *
223 * @param[in] uniqueId Unique identifier for a session
224 * @param[in] path Path of the protected content
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900225 * @return status_t
226 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900227 */
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900228 status_t removeRights(int uniqueId, const String8& path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900229
230 /**
231 * Removes all the rights information of each plug-in associated with
232 * DRM framework. Will be used in master reset
233 *
234 * @param[in] uniqueId Unique identifier for a session
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900235 * @return status_t
236 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900237 */
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900238 status_t removeAllRights(int uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900239
240 /**
241 * This API is for Forward Lock based DRM scheme.
242 * Each time the application tries to download a new DRM file
243 * which needs to be converted, then the application has to
244 * begin with calling this API.
245 *
246 * @param[in] uniqueId Unique identifier for a session
247 * @param[in] mimeType Description/MIME type of the input data packet
248 * @return Return handle for the convert session
249 */
250 int openConvertSession(int uniqueId, const String8& mimeType);
251
252 /**
253 * Accepts and converts the input data which is part of DRM file.
254 * The resultant converted data and the status is returned in the DrmConvertedInfo
255 * object. This method will be called each time there are new block
256 * of data received by the application.
257 *
258 * @param[in] uniqueId Unique identifier for a session
259 * @param[in] convertId Handle for the convert session
260 * @param[in] inputData Input Data which need to be converted
261 * @return Return object contains the status of the data conversion,
262 * the output converted data and offset. In this case the
263 * application will ignore the offset information.
264 */
265 DrmConvertedStatus* convertData(int uniqueId, int convertId, const DrmBuffer* inputData);
266
267 /**
268 * Informs the Drm Agent when there is no more data which need to be converted
269 * or when an error occurs. Upon successful conversion of the complete data,
270 * the agent will inform that where the header and body signature
271 * should be added. This signature appending is needed to integrity
272 * protect the converted file.
273 *
274 * @param[in] uniqueId Unique identifier for a session
275 * @param[in] convertId Handle for the convert session
276 * @return Return object contains the status of the data conversion,
277 * the header and body signature data. It also informs
278 * the application on which offset these signature data
279 * should be appended.
280 */
281 DrmConvertedStatus* closeConvertSession(int uniqueId, int convertId);
282
283 /**
284 * Retrieves all DrmSupportInfo instance that native DRM framework can handle.
285 * This interface is meant to be used by JNI layer
286 *
287 * @param[in] uniqueId Unique identifier for a session
288 * @param[out] length Number of elements in drmSupportInfoArray
289 * @param[out] drmSupportInfoArray Array contains all DrmSupportInfo
290 * that native DRM framework can handle
291 * @return status_t
292 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
293 */
294 status_t getAllSupportInfo(int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray);
295
296 /**
297 * Open the decrypt session to decrypt the given protected content
298 *
299 * @param[in] uniqueId Unique identifier for a session
300 * @param[in] fd File descriptor of the protected content to be decrypted
301 * @param[in] offset Start position of the content
302 * @param[in] length The length of the protected content
James Dong9d2f3862012-01-10 08:24:37 -0800303 * @param[in] mime The mime type of the protected content if it is not NULL or empty
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900304 * @return
305 * Handle for the decryption session
306 */
James Dong9d2f3862012-01-10 08:24:37 -0800307 sp<DecryptHandle> openDecryptSession(
308 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900309
310 /**
Takeshi Aimie943f842010-10-08 23:05:49 +0900311 * Open the decrypt session to decrypt the given protected content
312 *
313 * @param[in] uniqueId Unique identifier for a session
314 * @param[in] uri Path of the protected content to be decrypted
James Dong9d2f3862012-01-10 08:24:37 -0800315 * @param[in] mime The mime type of the protected content if it is not NULL or empty
Takeshi Aimie943f842010-10-08 23:05:49 +0900316 * @return
317 * Handle for the decryption session
318 */
James Dong9d2f3862012-01-10 08:24:37 -0800319 sp<DecryptHandle> openDecryptSession(
320 int uniqueId, const char* uri, const char* mime);
Takeshi Aimie943f842010-10-08 23:05:49 +0900321
322 /**
Kei Takahashicba7b322012-01-18 17:10:19 +0900323 * Open the decrypt session to decrypt the given protected content
324 *
325 * @param[in] uniqueId Unique identifier for a session
326 * @param[in] buf Data to initiate decrypt session
327 * @param[in] mimeType Mime type of the protected content
328 * @return
329 * Handle for the decryption session
330 */
331 sp<DecryptHandle> openDecryptSession(int uniqueId, const DrmBuffer& buf,
332 const String8& mimeType);
333
334 /**
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900335 * Close the decrypt session for the given handle
336 *
337 * @param[in] uniqueId Unique identifier for a session
338 * @param[in] decryptHandle Handle for the decryption session
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900339 * @return status_t
340 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900341 */
Gloria Wangb5ce3612011-02-24 16:40:57 -0800342 status_t closeDecryptSession(int uniqueId, sp<DecryptHandle> &decryptHandle);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900343
344 /**
345 * Initialize decryption for the given unit of the protected content
346 *
347 * @param[in] uniqueId Unique identifier for a session
348 * @param[in] decryptHandle Handle for the decryption session
349 * @param[in] decryptUnitId ID which specifies decryption unit, such as track ID
350 * @param[in] headerInfo Information for initializing decryption of this decrypUnit
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900351 * @return status_t
352 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900353 */
Gloria Wangb5ce3612011-02-24 16:40:57 -0800354 status_t initializeDecryptUnit(int uniqueId, sp<DecryptHandle> &decryptHandle,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900355 int decryptUnitId, const DrmBuffer* headerInfo);
356
357 /**
358 * Decrypt the protected content buffers for the given unit
359 * This method will be called any number of times, based on number of
360 * encrypted streams received from application.
361 *
362 * @param[in] uniqueId Unique identifier for a session
363 * @param[in] decryptHandle Handle for the decryption session
364 * @param[in] decryptUnitId ID which specifies decryption unit, such as track ID
365 * @param[in] encBuffer Encrypted data block
366 * @param[out] decBuffer Decrypted data block
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900367 * @param[in] IV Optional buffer
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900368 * @return status_t
369 * Returns the error code for this API
370 * DRM_NO_ERROR for success, and one of DRM_ERROR_UNKNOWN, DRM_ERROR_LICENSE_EXPIRED
371 * DRM_ERROR_SESSION_NOT_OPENED, DRM_ERROR_DECRYPT_UNIT_NOT_INITIALIZED,
372 * DRM_ERROR_DECRYPT for failure.
373 */
Gloria Wangb5ce3612011-02-24 16:40:57 -0800374 status_t decrypt(int uniqueId, sp<DecryptHandle> &decryptHandle, int decryptUnitId,
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900375 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900376
377 /**
378 * Finalize decryption for the given unit of the protected content
379 *
380 * @param[in] uniqueId Unique identifier for a session
381 * @param[in] decryptHandle Handle for the decryption session
382 * @param[in] decryptUnitId ID which specifies decryption unit, such as track ID
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900383 * @return status_t
384 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900385 */
Gloria Wangb5ce3612011-02-24 16:40:57 -0800386 status_t finalizeDecryptUnit(int uniqueId, sp<DecryptHandle> &decryptHandle, int decryptUnitId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900387
388 /**
389 * Reads the specified number of bytes from an open DRM file.
390 *
391 * @param[in] uniqueId Unique identifier for a session
392 * @param[in] decryptHandle Handle for the decryption session
393 * @param[out] buffer Reference to the buffer that should receive the read data.
394 * @param[in] numBytes Number of bytes to read.
395 * @param[in] offset Offset with which to update the file position.
396 *
397 * @return Number of bytes read. Returns -1 for Failure.
398 */
Gloria Wangb5ce3612011-02-24 16:40:57 -0800399 ssize_t pread(int uniqueId, sp<DecryptHandle> &decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800400 void* buffer, ssize_t numBytes, off64_t offset);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900401
402 /**
403 * Notify the event to the registered listener
404 *
405 * @param[in] event The event to be notified
406 * @return status_t
407 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
408 */
409 status_t notify(const DrmInfoEvent& event);
410
411private:
412 /**
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900413 * Install new DRM Engine Plug-in at the runtime
414 *
415 * @param[in] uniqueId Unique identifier for a session
416 * @param[in] drmEngine Shared Object(so) File in which DRM Engine defined
417 * @return status_t
418 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
419 */
420 status_t installDrmEngine(int uniqueId, const String8& drmEngineFile);
421
422private:
423 Mutex mLock;
424 sp<DrmManagerClient::OnInfoListener> mOnInfoListener;
425
Gloria Wang8d2577b2011-03-15 10:52:28 -0700426 class DeathNotifier: public IBinder::DeathRecipient {
427 public:
428 DeathNotifier() {}
429 virtual ~DeathNotifier();
430 virtual void binderDied(const wp<IBinder>& who);
431 };
432
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900433private:
Gloria Wang8d2577b2011-03-15 10:52:28 -0700434 static Mutex sMutex;
435 static sp<DeathNotifier> sDeathNotifier;
436 static sp<IDrmManagerService> sDrmManagerService;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900437 static const sp<IDrmManagerService>& getDrmManagerService();
438 static const String8 EMPTY_STRING;
439};
440
441};
442
443#endif /* __DRM_MANAGER_CLIENT_IMPL_H__ */
444