DrmManagerClientImpl.h revision 2272ee27d9022d173b6eab45c409b3c3f57f30ec
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_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     * @return status_t
136     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
137     */
138    status_t saveRights(int uniqueId, const DrmRights& drmRights,
139            const String8& rightsPath, const String8& contentPath);
140
141    /**
142     * Retrieves the mime type embedded inside the original content
143     *
144     * @param[in] uniqueId Unique identifier for a session
145     * @param[in] path the path of the protected content
146     * @return String8
147     *     Returns mime-type of the original content, such as "video/mpeg"
148     */
149    String8 getOriginalMimeType(int uniqueId, const String8& path);
150
151    /**
152     * Retrieves the type of the protected object (content, rights, etc..)
153     * using specified path or mimetype. At least one parameter should be non null
154     * to retrieve DRM object type
155     *
156     * @param[in] uniqueId Unique identifier for a session
157     * @param[in] path Path of the content or null.
158     * @param[in] mimeType Mime type of the content or null.
159     * @return type of the DRM content,
160     *     such as DrmObjectType::CONTENT, DrmObjectType::RIGHTS_OBJECT
161     */
162    int getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType);
163
164    /**
165     * Check whether the given content has valid rights or not
166     *
167     * @param[in] uniqueId Unique identifier for a session
168     * @param[in] path Path of the protected content
169     * @param[in] action Action to perform (Action::DEFAULT, Action::PLAY, etc)
170     * @return the status of the rights for the protected content,
171     *     such as RightsStatus::RIGHTS_VALID, RightsStatus::RIGHTS_EXPIRED, etc.
172     */
173    int checkRightsStatus(int uniqueId, const String8& path, int action);
174
175    /**
176     * Consumes the rights for a content.
177     * If the reserve parameter is true the rights is reserved until the same
178     * application calls this api again with the reserve parameter set to false.
179     *
180     * @param[in] uniqueId Unique identifier for a session
181     * @param[in] decryptHandle Handle for the decryption session
182     * @param[in] action Action to perform. (Action::DEFAULT, Action::PLAY, etc)
183     * @param[in] reserve True if the rights should be reserved.
184     * @return status_t
185     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
186     */
187    status_t consumeRights(int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve);
188
189    /**
190     * Informs the DRM engine about the playback actions performed on the DRM files.
191     *
192     * @param[in] uniqueId Unique identifier for a session
193     * @param[in] decryptHandle Handle for the decryption session
194     * @param[in] playbackStatus Playback action (Playback::START, Playback::STOP, Playback::PAUSE)
195     * @param[in] position Position in the file (in milliseconds) where the start occurs.
196     *     Only valid together with Playback::START.
197     * @return status_t
198     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
199     */
200    status_t setPlaybackStatus(
201            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position);
202
203    /**
204     * Validates whether an action on the DRM content is allowed or not.
205     *
206     * @param[in] uniqueId Unique identifier for a session
207     * @param[in] path Path of the protected content
208     * @param[in] action Action to validate (Action::PLAY, Action::TRANSFER, etc)
209     * @param[in] description Detailed description of the action
210     * @return true if the action is allowed.
211     */
212    bool validateAction(
213        int uniqueId, const String8& path, int action, const ActionDescription& description);
214
215    /**
216     * Removes the rights associated with the given protected content
217     *
218     * @param[in] uniqueId Unique identifier for a session
219     * @param[in] path Path of the protected content
220     * @return status_t
221     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
222     */
223    status_t removeRights(int uniqueId, const String8& path);
224
225    /**
226     * Removes all the rights information of each plug-in associated with
227     * DRM framework. Will be used in master reset
228     *
229     * @param[in] uniqueId Unique identifier for a session
230     * @return status_t
231     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
232     */
233    status_t removeAllRights(int uniqueId);
234
235    /**
236     * This API is for Forward Lock based DRM scheme.
237     * Each time the application tries to download a new DRM file
238     * which needs to be converted, then the application has to
239     * begin with calling this API.
240     *
241     * @param[in] uniqueId Unique identifier for a session
242     * @param[in] mimeType Description/MIME type of the input data packet
243     * @return Return handle for the convert session
244     */
245    int openConvertSession(int uniqueId, const String8& mimeType);
246
247    /**
248     * Accepts and converts the input data which is part of DRM file.
249     * The resultant converted data and the status is returned in the DrmConvertedInfo
250     * object. This method will be called each time there are new block
251     * of data received by the application.
252     *
253     * @param[in] uniqueId Unique identifier for a session
254     * @param[in] convertId Handle for the convert session
255     * @param[in] inputData Input Data which need to be converted
256     * @return Return object contains the status of the data conversion,
257     *     the output converted data and offset. In this case the
258     *     application will ignore the offset information.
259     */
260    DrmConvertedStatus* convertData(int uniqueId, int convertId, const DrmBuffer* inputData);
261
262    /**
263     * Informs the Drm Agent when there is no more data which need to be converted
264     * or when an error occurs. Upon successful conversion of the complete data,
265     * the agent will inform that where the header and body signature
266     * should be added. This signature appending is needed to integrity
267     * protect the converted file.
268     *
269     * @param[in] uniqueId Unique identifier for a session
270     * @param[in] convertId Handle for the convert session
271     * @return Return object contains the status of the data conversion,
272     *     the header and body signature data. It also informs
273     *     the application on which offset these signature data
274     *     should be appended.
275     */
276    DrmConvertedStatus* closeConvertSession(int uniqueId, int convertId);
277
278    /**
279     * Retrieves all DrmSupportInfo instance that native DRM framework can handle.
280     * This interface is meant to be used by JNI layer
281     *
282     * @param[in] uniqueId Unique identifier for a session
283     * @param[out] length Number of elements in drmSupportInfoArray
284     * @param[out] drmSupportInfoArray Array contains all DrmSupportInfo
285     *             that native DRM framework can handle
286     * @return status_t
287     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
288     */
289    status_t getAllSupportInfo(int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray);
290
291    /**
292     * Open the decrypt session to decrypt the given protected content
293     *
294     * @param[in] uniqueId Unique identifier for a session
295     * @param[in] fd File descriptor of the protected content to be decrypted
296     * @param[in] offset Start position of the content
297     * @param[in] length The length of the protected content
298     * @return
299     *     Handle for the decryption session
300     */
301    DecryptHandle* openDecryptSession(int uniqueId, int fd, int offset, int length);
302
303    /**
304     * Close the decrypt session for the given handle
305     *
306     * @param[in] uniqueId Unique identifier for a session
307     * @param[in] decryptHandle Handle for the decryption session
308     * @return status_t
309     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
310     */
311    status_t closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle);
312
313    /**
314     * Initialize decryption for the given unit of the protected content
315     *
316     * @param[in] uniqueId Unique identifier for a session
317     * @param[in] decryptHandle Handle for the decryption session
318     * @param[in] decryptUnitId ID which specifies decryption unit, such as track ID
319     * @param[in] headerInfo Information for initializing decryption of this decrypUnit
320     * @return status_t
321     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
322     */
323    status_t initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
324            int decryptUnitId, const DrmBuffer* headerInfo);
325
326    /**
327     * Decrypt the protected content buffers for the given unit
328     * This method will be called any number of times, based on number of
329     * encrypted streams received from application.
330     *
331     * @param[in] uniqueId Unique identifier for a session
332     * @param[in] decryptHandle Handle for the decryption session
333     * @param[in] decryptUnitId ID which specifies decryption unit, such as track ID
334     * @param[in] encBuffer Encrypted data block
335     * @param[out] decBuffer Decrypted data block
336     * @param[in] IV Optional buffer
337     * @return status_t
338     *     Returns the error code for this API
339     *     DRM_NO_ERROR for success, and one of DRM_ERROR_UNKNOWN, DRM_ERROR_LICENSE_EXPIRED
340     *     DRM_ERROR_SESSION_NOT_OPENED, DRM_ERROR_DECRYPT_UNIT_NOT_INITIALIZED,
341     *     DRM_ERROR_DECRYPT for failure.
342     */
343    status_t decrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
344            const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV);
345
346    /**
347     * Finalize decryption for the given unit of the protected content
348     *
349     * @param[in] uniqueId Unique identifier for a session
350     * @param[in] decryptHandle Handle for the decryption session
351     * @param[in] decryptUnitId ID which specifies decryption unit, such as track ID
352     * @return status_t
353     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
354     */
355    status_t finalizeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId);
356
357    /**
358     * Reads the specified number of bytes from an open DRM file.
359     *
360     * @param[in] uniqueId Unique identifier for a session
361     * @param[in] decryptHandle Handle for the decryption session
362     * @param[out] buffer Reference to the buffer that should receive the read data.
363     * @param[in] numBytes Number of bytes to read.
364     * @param[in] offset Offset with which to update the file position.
365     *
366     * @return Number of bytes read. Returns -1 for Failure.
367     */
368    ssize_t pread(int uniqueId, DecryptHandle* decryptHandle,
369            void* buffer, ssize_t numBytes, off_t offset);
370
371    /**
372     * Notify the event to the registered listener
373     *
374     * @param[in] event The event to be notified
375     * @return status_t
376     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
377     */
378    status_t notify(const DrmInfoEvent& event);
379
380private:
381    /**
382     * Initialize DRM Manager
383     *     load available plug-ins from plugInDirPath
384     *
385     * @param[in] uniqueId Unique identifier for a session
386     * @param[in] plugInDirPath Directory from where to load plug-ins
387     * @return status_t
388     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
389     */
390    status_t loadPlugIns(int uniqueId, const String8& plugInDirPath);
391
392    /**
393     * Install new DRM Engine Plug-in at the runtime
394     *
395     * @param[in] uniqueId Unique identifier for a session
396     * @param[in] drmEngine Shared Object(so) File in which DRM Engine defined
397     * @return status_t
398     *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
399     */
400    status_t installDrmEngine(int uniqueId, const String8& drmEngineFile);
401
402private:
403    Mutex mLock;
404    sp<DrmManagerClient::OnInfoListener> mOnInfoListener;
405
406private:
407    static Mutex mMutex;
408    static sp<IDrmManagerService> mDrmManagerService;
409    static const sp<IDrmManagerService>& getDrmManagerService();
410    static const String8 EMPTY_STRING;
411};
412
413};
414
415#endif /* __DRM_MANAGER_CLIENT_IMPL_H__ */
416
417