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