blob: e70e6e118bd6d0796105e503bdb87155cee8f256 [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:
38 DrmManagerClientImpl();
39
40public:
41 static DrmManagerClientImpl* create(int* pUniqueId);
42
43 static void remove(int uniqueId);
44
45 virtual ~DrmManagerClientImpl();
46
47public:
48 /**
49 * Initialize DRM Manager
50 * load available plug-ins from default plugInDirPath
51 *
52 * @param[in] uniqueId Unique identifier for a session
53 * @return status_t
54 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
55 */
56 status_t loadPlugIns(int uniqueId);
57
58 /**
59 * Finalize DRM Manager
60 * release resources associated with each plug-in
61 * unload all plug-ins and etc.
62 *
63 * @param[in] uniqueId Unique identifier for a session
64 * @return status_t
65 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
66 */
67 status_t unloadPlugIns(int uniqueId);
68
69 /**
70 * Register a callback to be invoked when the caller required to
71 * receive necessary information
72 *
73 * @param[in] uniqueId Unique identifier for a session
74 * @param[in] infoListener Listener
75 * @return status_t
76 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
77 */
78 status_t setOnInfoListener(
79 int uniqueId, const sp<DrmManagerClient::OnInfoListener>& infoListener);
80
81 /**
82 * Get constraint information associated with input content
83 *
84 * @param[in] uniqueId Unique identifier for a session
85 * @param[in] path Path of the protected content
86 * @param[in] action Actions defined such as,
87 * Action::DEFAULT, Action::PLAY, etc
88 * @return DrmConstraints
89 * key-value pairs of constraint are embedded in it
90 * @note
91 * In case of error, return NULL
92 */
93 DrmConstraints* getConstraints(int uniqueId, const String8* path, const int action);
94
95 /**
96 * Check whether the given mimetype or path can be handled
97 *
98 * @param[in] uniqueId Unique identifier for a session
99 * @param[in] path Path of the content needs to be handled
100 * @param[in] mimetype Mimetype of the content needs to be handled
101 * @return
102 * True if DrmManager can handle given path or mime type.
103 */
104 bool canHandle(int uniqueId, const String8& path, const String8& mimeType);
105
106 /**
107 * Executes given drm information based on its type
108 *
109 * @param[in] uniqueId Unique identifier for a session
110 * @param[in] drmInfo Information needs to be processed
111 * @return DrmInfoStatus
112 * instance as a result of processing given input
113 */
114 DrmInfoStatus* processDrmInfo(int uniqueId, const DrmInfo* drmInfo);
115
116 /**
117 * Retrieves necessary information for registration, unregistration or rights
118 * acquisition information.
119 *
120 * @param[in] uniqueId Unique identifier for a session
121 * @param[in] drmInfoRequest Request information to retrieve drmInfo
122 * @return DrmInfo
123 * instance as a result of processing given input
124 */
125 DrmInfo* acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest);
126
127 /**
128 * Save DRM rights to specified rights path
129 * and make association with content path
130 *
131 * @param[in] uniqueId Unique identifier for a session
132 * @param[in] drmRights DrmRights to be saved
133 * @param[in] rightsPath File path where rights to be saved
134 * @param[in] contentPath File path where content was saved
135 */
136 void saveRights(int uniqueId, const DrmRights& drmRights,
137 const String8& rightsPath, const String8& contentPath);
138
139 /**
140 * Retrieves the mime type embedded inside the original content
141 *
142 * @param[in] uniqueId Unique identifier for a session
143 * @param[in] path the path of the protected content
144 * @return String8
145 * Returns mime-type of the original content, such as "video/mpeg"
146 */
147 String8 getOriginalMimeType(int uniqueId, const String8& path);
148
149 /**
150 * Retrieves the type of the protected object (content, rights, etc..)
151 * using specified path or mimetype. At least one parameter should be non null
152 * to retrieve DRM object type
153 *
154 * @param[in] uniqueId Unique identifier for a session
155 * @param[in] path Path of the content or null.
156 * @param[in] mimeType Mime type of the content or null.
157 * @return type of the DRM content,
158 * such as DrmObjectType::CONTENT, DrmObjectType::RIGHTS_OBJECT
159 */
160 int getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType);
161
162 /**
163 * Check whether the given content has valid rights or not
164 *
165 * @param[in] uniqueId Unique identifier for a session
166 * @param[in] path Path of the protected content
167 * @param[in] action Action to perform (Action::DEFAULT, Action::PLAY, etc)
168 * @return the status of the rights for the protected content,
169 * such as RightsStatus::RIGHTS_VALID, RightsStatus::RIGHTS_EXPIRED, etc.
170 */
171 int checkRightsStatus(int uniqueId, const String8& path, int action);
172
173 /**
174 * Consumes the rights for a content.
175 * If the reserve parameter is true the rights is reserved until the same
176 * application calls this api again with the reserve parameter set to false.
177 *
178 * @param[in] uniqueId Unique identifier for a session
179 * @param[in] decryptHandle Handle for the decryption session
180 * @param[in] action Action to perform. (Action::DEFAULT, Action::PLAY, etc)
181 * @param[in] reserve True if the rights should be reserved.
182 */
183 void consumeRights(int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve);
184
185 /**
186 * Informs the DRM engine about the playback actions performed on the DRM files.
187 *
188 * @param[in] uniqueId Unique identifier for a session
189 * @param[in] decryptHandle Handle for the decryption session
190 * @param[in] playbackStatus Playback action (Playback::START, Playback::STOP, Playback::PAUSE)
191 * @param[in] position Position in the file (in milliseconds) where the start occurs.
192 * Only valid together with Playback::START.
193 */
194 void setPlaybackStatus(
195 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position);
196
197 /**
198 * Validates whether an action on the DRM content is allowed or not.
199 *
200 * @param[in] uniqueId Unique identifier for a session
201 * @param[in] path Path of the protected content
202 * @param[in] action Action to validate (Action::PLAY, Action::TRANSFER, etc)
203 * @param[in] description Detailed description of the action
204 * @return true if the action is allowed.
205 */
206 bool validateAction(
207 int uniqueId, const String8& path, int action, const ActionDescription& description);
208
209 /**
210 * Removes the rights associated with the given protected content
211 *
212 * @param[in] uniqueId Unique identifier for a session
213 * @param[in] path Path of the protected content
214 */
215 void removeRights(int uniqueId, const String8& path);
216
217 /**
218 * Removes all the rights information of each plug-in associated with
219 * DRM framework. Will be used in master reset
220 *
221 * @param[in] uniqueId Unique identifier for a session
222 */
223 void removeAllRights(int uniqueId);
224
225 /**
226 * This API is for Forward Lock based DRM scheme.
227 * Each time the application tries to download a new DRM file
228 * which needs to be converted, then the application has to
229 * begin with calling this API.
230 *
231 * @param[in] uniqueId Unique identifier for a session
232 * @param[in] mimeType Description/MIME type of the input data packet
233 * @return Return handle for the convert session
234 */
235 int openConvertSession(int uniqueId, const String8& mimeType);
236
237 /**
238 * Accepts and converts the input data which is part of DRM file.
239 * The resultant converted data and the status is returned in the DrmConvertedInfo
240 * object. This method will be called each time there are new block
241 * of data received by the application.
242 *
243 * @param[in] uniqueId Unique identifier for a session
244 * @param[in] convertId Handle for the convert session
245 * @param[in] inputData Input Data which need to be converted
246 * @return Return object contains the status of the data conversion,
247 * the output converted data and offset. In this case the
248 * application will ignore the offset information.
249 */
250 DrmConvertedStatus* convertData(int uniqueId, int convertId, const DrmBuffer* inputData);
251
252 /**
253 * Informs the Drm Agent when there is no more data which need to be converted
254 * or when an error occurs. Upon successful conversion of the complete data,
255 * the agent will inform that where the header and body signature
256 * should be added. This signature appending is needed to integrity
257 * protect the converted file.
258 *
259 * @param[in] uniqueId Unique identifier for a session
260 * @param[in] convertId Handle for the convert session
261 * @return Return object contains the status of the data conversion,
262 * the header and body signature data. It also informs
263 * the application on which offset these signature data
264 * should be appended.
265 */
266 DrmConvertedStatus* closeConvertSession(int uniqueId, int convertId);
267
268 /**
269 * Retrieves all DrmSupportInfo instance that native DRM framework can handle.
270 * This interface is meant to be used by JNI layer
271 *
272 * @param[in] uniqueId Unique identifier for a session
273 * @param[out] length Number of elements in drmSupportInfoArray
274 * @param[out] drmSupportInfoArray Array contains all DrmSupportInfo
275 * that native DRM framework can handle
276 * @return status_t
277 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
278 */
279 status_t getAllSupportInfo(int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray);
280
281 /**
282 * Open the decrypt session to decrypt the given protected content
283 *
284 * @param[in] uniqueId Unique identifier for a session
285 * @param[in] fd File descriptor of the protected content to be decrypted
286 * @param[in] offset Start position of the content
287 * @param[in] length The length of the protected content
288 * @return
289 * Handle for the decryption session
290 */
291 DecryptHandle* openDecryptSession(int uniqueId, int fd, int offset, int length);
292
293 /**
294 * Close the decrypt session for the given handle
295 *
296 * @param[in] uniqueId Unique identifier for a session
297 * @param[in] decryptHandle Handle for the decryption session
298 */
299 void closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle);
300
301 /**
302 * Initialize decryption for the given unit of the protected content
303 *
304 * @param[in] uniqueId Unique identifier for a session
305 * @param[in] decryptHandle Handle for the decryption session
306 * @param[in] decryptUnitId ID which specifies decryption unit, such as track ID
307 * @param[in] headerInfo Information for initializing decryption of this decrypUnit
308 */
309 void initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
310 int decryptUnitId, const DrmBuffer* headerInfo);
311
312 /**
313 * Decrypt the protected content buffers for the given unit
314 * This method will be called any number of times, based on number of
315 * encrypted streams received from application.
316 *
317 * @param[in] uniqueId Unique identifier for a session
318 * @param[in] decryptHandle Handle for the decryption session
319 * @param[in] decryptUnitId ID which specifies decryption unit, such as track ID
320 * @param[in] encBuffer Encrypted data block
321 * @param[out] decBuffer Decrypted data block
322 * @return status_t
323 * Returns the error code for this API
324 * DRM_NO_ERROR for success, and one of DRM_ERROR_UNKNOWN, DRM_ERROR_LICENSE_EXPIRED
325 * DRM_ERROR_SESSION_NOT_OPENED, DRM_ERROR_DECRYPT_UNIT_NOT_INITIALIZED,
326 * DRM_ERROR_DECRYPT for failure.
327 */
328 status_t decrypt(int uniqueId, DecryptHandle* decryptHandle,
329 int decryptUnitId, const DrmBuffer* encBuffer, DrmBuffer** decBuffer);
330
331 /**
332 * Finalize decryption for the given unit of the protected content
333 *
334 * @param[in] uniqueId Unique identifier for a session
335 * @param[in] decryptHandle Handle for the decryption session
336 * @param[in] decryptUnitId ID which specifies decryption unit, such as track ID
337 */
338 void finalizeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId);
339
340 /**
341 * Reads the specified number of bytes from an open DRM file.
342 *
343 * @param[in] uniqueId Unique identifier for a session
344 * @param[in] decryptHandle Handle for the decryption session
345 * @param[out] buffer Reference to the buffer that should receive the read data.
346 * @param[in] numBytes Number of bytes to read.
347 * @param[in] offset Offset with which to update the file position.
348 *
349 * @return Number of bytes read. Returns -1 for Failure.
350 */
351 ssize_t pread(int uniqueId, DecryptHandle* decryptHandle,
352 void* buffer, ssize_t numBytes, off_t offset);
353
354 /**
355 * Notify the event to the registered listener
356 *
357 * @param[in] event The event to be notified
358 * @return status_t
359 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
360 */
361 status_t notify(const DrmInfoEvent& event);
362
363private:
364 /**
365 * Initialize DRM Manager
366 * load available plug-ins from plugInDirPath
367 *
368 * @param[in] uniqueId Unique identifier for a session
369 * @param[in] plugInDirPath Directory from where to load plug-ins
370 * @return status_t
371 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
372 */
373 status_t loadPlugIns(int uniqueId, const String8& plugInDirPath);
374
375 /**
376 * Install new DRM Engine Plug-in at the runtime
377 *
378 * @param[in] uniqueId Unique identifier for a session
379 * @param[in] drmEngine Shared Object(so) File in which DRM Engine defined
380 * @return status_t
381 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
382 */
383 status_t installDrmEngine(int uniqueId, const String8& drmEngineFile);
384
385private:
386 Mutex mLock;
387 sp<DrmManagerClient::OnInfoListener> mOnInfoListener;
388
389private:
390 static Mutex mMutex;
391 static Vector<int> mUniqueIdVector;
392 static sp<IDrmManagerService> mDrmManagerService;
393 static const sp<IDrmManagerService>& getDrmManagerService();
394 static const String8 EMPTY_STRING;
395};
396
397};
398
399#endif /* __DRM_MANAGER_CLIENT_IMPL_H__ */
400