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