DrmEngineBase.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_ENGINE_BASE_H__ 18#define __DRM_ENGINE_BASE_H__ 19 20#include <drm/drm_framework_common.h> 21#include "IDrmEngine.h" 22 23namespace android { 24 25/** 26 * This class is an interface for plug-in developers 27 * 28 * Responsibility of this class is control the sequence of actual plug-in. 29 * All each plug-in developer has to do is implement onXXX() type virtual interfaces. 30 */ 31class DrmEngineBase : public IDrmEngine { 32public: 33 DrmEngineBase(); 34 virtual ~DrmEngineBase(); 35 36public: 37 DrmConstraints* getConstraints(int uniqueId, const String8* path, int action); 38 39 status_t initialize(int uniqueId); 40 41 status_t setOnInfoListener(int uniqueId, const IDrmEngine::OnInfoListener* infoListener); 42 43 status_t terminate(int uniqueId); 44 45 bool canHandle(int uniqueId, const String8& path); 46 47 DrmInfoStatus* processDrmInfo(int uniqueId, const DrmInfo* drmInfo); 48 49 status_t saveRights(int uniqueId, const DrmRights& drmRights, 50 const String8& rightsPath, const String8& contentPath); 51 52 DrmInfo* acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest); 53 54 String8 getOriginalMimeType(int uniqueId, const String8& path); 55 56 int getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType); 57 58 int checkRightsStatus(int uniqueId, const String8& path, int action); 59 60 status_t consumeRights(int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve); 61 62 status_t setPlaybackStatus( 63 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position); 64 65 bool validateAction( 66 int uniqueId, const String8& path, int action, const ActionDescription& description); 67 68 status_t removeRights(int uniqueId, const String8& path); 69 70 status_t removeAllRights(int uniqueId); 71 72 status_t openConvertSession(int uniqueId, int convertId); 73 74 DrmConvertedStatus* convertData(int uniqueId, int convertId, const DrmBuffer* inputData); 75 76 DrmConvertedStatus* closeConvertSession(int uniqueId, int convertId); 77 78 DrmSupportInfo* getSupportInfo(int uniqueId); 79 80 status_t openDecryptSession( 81 int uniqueId, DecryptHandle* decryptHandle, int fd, int offset, int length); 82 83 status_t closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle); 84 85 status_t initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle, 86 int decryptUnitId, const DrmBuffer* headerInfo); 87 88 status_t decrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, 89 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV); 90 91 status_t finalizeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId); 92 93 ssize_t pread(int uniqueId, DecryptHandle* decryptHandle, 94 void* buffer, ssize_t numBytes, off_t offset); 95 96protected: 97 ///////////////////////////////////////////////////// 98 // Interface for plug-in developers // 99 // each plug-in has to implement following method // 100 ///////////////////////////////////////////////////// 101 /** 102 * Get constraint information associated with input content 103 * 104 * @param[in] uniqueId Unique identifier for a session 105 * @param[in] path Path of the protected content 106 * @param[in] action Actions defined such as, 107 * Action::DEFAULT, Action::PLAY, etc 108 * @return DrmConstraints 109 * key-value pairs of constraint are embedded in it 110 * @note 111 * In case of error, return NULL 112 */ 113 virtual DrmConstraints* onGetConstraints( 114 int uniqueId, const String8* path, int action) = 0; 115 116 /** 117 * Initialize plug-in 118 * 119 * @param[in] uniqueId Unique identifier for a session 120 * @return status_t 121 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 122 */ 123 virtual status_t onInitialize(int uniqueId) = 0; 124 125 /** 126 * Register a callback to be invoked when the caller required to 127 * receive necessary information 128 * 129 * @param[in] uniqueId Unique identifier for a session 130 * @param[in] infoListener Listener 131 * @return status_t 132 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 133 */ 134 virtual status_t onSetOnInfoListener( 135 int uniqueId, const IDrmEngine::OnInfoListener* infoListener) = 0; 136 137 /** 138 * Terminate the plug-in 139 * and release resource bound to plug-in 140 * 141 * @param[in] uniqueId Unique identifier for a session 142 * @return status_t 143 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 144 */ 145 virtual status_t onTerminate(int uniqueId) = 0; 146 147 /** 148 * Get whether the given content can be handled by this plugin or not 149 * 150 * @param[in] uniqueId Unique identifier for a session 151 * @param[in] path Path the protected object 152 * @return bool 153 * Returns true if this plugin can handle , false in case of not able to handle 154 */ 155 virtual bool onCanHandle(int uniqueId, const String8& path) = 0; 156 157 /** 158 * Executes given drm information based on its type 159 * 160 * @param[in] uniqueId Unique identifier for a session 161 * @param[in] drmInfo Information needs to be processed 162 * @return DrmInfoStatus 163 * instance as a result of processing given input 164 */ 165 virtual DrmInfoStatus* onProcessDrmInfo(int uniqueId, const DrmInfo* drmInfo) = 0; 166 167 /** 168 * Save DRM rights to specified rights path 169 * and make association with content path 170 * 171 * @param[in] uniqueId Unique identifier for a session 172 * @param[in] drmRights DrmRights to be saved 173 * @param[in] rightsPath File path where rights to be saved 174 * @param[in] contentPath File path where content was saved 175 * @return status_t 176 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 177 */ 178 virtual status_t onSaveRights(int uniqueId, const DrmRights& drmRights, 179 const String8& rightspath, const String8& contentPath) = 0; 180 181 /** 182 * Retrieves necessary information for registration, unregistration or rights 183 * acquisition information. 184 * 185 * @param[in] uniqueId Unique identifier for a session 186 * @param[in] drmInfoRequest Request information to retrieve drmInfo 187 * @return DrmInfo 188 * instance as a result of processing given input 189 */ 190 virtual DrmInfo* onAcquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInforequest) = 0; 191 192 /** 193 * Retrieves the mime type embedded inside the original content 194 * 195 * @param[in] uniqueId Unique identifier for a session 196 * @param[in] path Path of the protected content 197 * @return String8 198 * Returns mime-type of the original content, such as "video/mpeg" 199 */ 200 virtual String8 onGetOriginalMimeType(int uniqueId, const String8& path) = 0; 201 202 /** 203 * Retrieves the type of the protected object (content, rights, etc..) 204 * using specified path or mimetype. At least one parameter should be non null 205 * to retrieve DRM object type 206 * 207 * @param[in] uniqueId Unique identifier for a session 208 * @param[in] path Path of the content or null. 209 * @param[in] mimeType Mime type of the content or null. 210 * @return type of the DRM content, 211 * such as DrmObjectType::CONTENT, DrmObjectType::RIGHTS_OBJECT 212 */ 213 virtual int onGetDrmObjectType( 214 int uniqueId, const String8& path, const String8& mimeType) = 0; 215 216 /** 217 * Check whether the given content has valid rights or not 218 * 219 * @param[in] uniqueId Unique identifier for a session 220 * @param[in] path Path of the protected content 221 * @param[in] action Action to perform (Action::DEFAULT, Action::PLAY, etc) 222 * @return the status of the rights for the protected content, 223 * such as RightsStatus::RIGHTS_VALID, RightsStatus::RIGHTS_EXPIRED, etc. 224 */ 225 virtual int onCheckRightsStatus(int uniqueId, const String8& path, int action) = 0; 226 227 /** 228 * Consumes the rights for a content. 229 * If the reserve parameter is true the rights is reserved until the same 230 * application calls this api again with the reserve parameter set to false. 231 * 232 * @param[in] uniqueId Unique identifier for a session 233 * @param[in] decryptHandle Handle for the decryption session 234 * @param[in] action Action to perform. (Action::DEFAULT, Action::PLAY, etc) 235 * @param[in] reserve True if the rights should be reserved. 236 * @return status_t 237 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 238 */ 239 virtual status_t onConsumeRights(int uniqueId, DecryptHandle* decryptHandle, 240 int action, bool reserve) = 0; 241 242 /** 243 * Informs the DRM Engine about the playback actions performed on the DRM files. 244 * 245 * @param[in] uniqueId Unique identifier for a session 246 * @param[in] decryptHandle Handle for the decryption session 247 * @param[in] playbackStatus Playback action (Playback::START, Playback::STOP, Playback::PAUSE) 248 * @param[in] position Position in the file (in milliseconds) where the start occurs. 249 * Only valid together with Playback::START. 250 * @return status_t 251 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 252 */ 253 virtual status_t onSetPlaybackStatus( 254 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) = 0; 255 256 /** 257 * Validates whether an action on the DRM content is allowed or not. 258 * 259 * @param[in] uniqueId Unique identifier for a session 260 * @param[in] path Path of the protected content 261 * @param[in] action Action to validate (Action::PLAY, Action::TRANSFER, etc) 262 * @param[in] description Detailed description of the action 263 * @return true if the action is allowed. 264 */ 265 virtual bool onValidateAction(int uniqueId, const String8& path, 266 int action, const ActionDescription& description) = 0; 267 268 /** 269 * Removes the rights associated with the given protected content 270 * 271 * @param[in] uniqueId Unique identifier for a session 272 * @param[in] path Path of the protected content 273 * @return status_t 274 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 275 */ 276 virtual status_t onRemoveRights(int uniqueId, const String8& path) = 0; 277 278 /** 279 * Removes all the rights information of each plug-in associated with 280 * DRM framework. Will be used in master reset 281 * 282 * @param[in] uniqueId Unique identifier for a session 283 * @return status_t 284 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 285 */ 286 virtual status_t onRemoveAllRights(int uniqueId) = 0; 287 288 /** 289 * This API is for Forward Lock based DRM scheme. 290 * Each time the application tries to download a new DRM file 291 * which needs to be converted, then the application has to 292 * begin with calling this API. 293 * 294 * @param[in] uniqueId Unique identifier for a session 295 * @param[in] convertId Handle for the convert session 296 * @return status_t 297 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 298 */ 299 virtual status_t onOpenConvertSession(int uniqueId, int convertId) = 0; 300 301 /** 302 * Accepts and converts the input data which is part of DRM file. 303 * The resultant converted data and the status is returned in the DrmConvertedInfo 304 * object. This method will be called each time there are new block 305 * of data received by the application. 306 * 307 * @param[in] uniqueId Unique identifier for a session 308 * @param[in] convertId Handle for the convert session 309 * @param[in] inputData Input Data which need to be converted 310 * @return Return object contains the status of the data conversion, 311 * the output converted data and offset. In this case the 312 * application will ignore the offset information. 313 */ 314 virtual DrmConvertedStatus* onConvertData( 315 int uniqueId, int convertId, const DrmBuffer* inputData) = 0; 316 317 /** 318 * Informs the Drm Agent when there is no more data which need to be converted 319 * or when an error occurs. Upon successful conversion of the complete data, 320 * the agent will inform that where the header and body signature 321 * should be added. This signature appending is needed to integrity 322 * protect the converted file. 323 * 324 * @param[in] uniqueId Unique identifier for a session 325 * @param[in] convertId Handle for the convert session 326 * @return Return object contains the status of the data conversion, 327 * the header and body signature data. It also informs 328 * the application on which offset these signature data 329 * should be appended. 330 */ 331 virtual DrmConvertedStatus* onCloseConvertSession(int uniqueId, int convertId) = 0; 332 333 /** 334 * Returns the information about the Drm Engine capabilities which includes 335 * supported MimeTypes and file suffixes. 336 * 337 * @param[in] uniqueId Unique identifier for a session 338 * @return DrmSupportInfo 339 * instance which holds the capabilities of a plug-in 340 */ 341 virtual DrmSupportInfo* onGetSupportInfo(int uniqueId) = 0; 342 343 /** 344 * Open the decrypt session to decrypt the given protected content 345 * 346 * @param[in] uniqueId Unique identifier for a session 347 * @param[in] decryptHandle Handle for the current decryption session 348 * @param[in] fd File descriptor of the protected content to be decrypted 349 * @param[in] offset Start position of the content 350 * @param[in] length The length of the protected content 351 * @return 352 * DRM_ERROR_CANNOT_HANDLE for failure and DRM_NO_ERROR for success 353 */ 354 virtual status_t onOpenDecryptSession( 355 int uniqueId, DecryptHandle* decryptHandle, int fd, int offset, int length) = 0; 356 357 /** 358 * Close the decrypt session for the given handle 359 * 360 * @param[in] uniqueId Unique identifier for a session 361 * @param[in] decryptHandle Handle for the decryption session 362 * @return status_t 363 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 364 */ 365 virtual status_t onCloseDecryptSession(int uniqueId, DecryptHandle* decryptHandle) = 0; 366 367 /** 368 * Initialize decryption for the given unit of the protected content 369 * 370 * @param[in] uniqueId Unique identifier for a session 371 * @param[in] decryptId Handle for the decryption session 372 * @param[in] decryptUnitId ID Specifies decryption unit, such as track ID 373 * @param[in] headerInfo Information for initializing decryption of this decrypUnit 374 * @return status_t 375 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 376 */ 377 virtual status_t onInitializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle, 378 int decryptUnitId, const DrmBuffer* headerInfo) = 0; 379 380 /** 381 * Decrypt the protected content buffers for the given unit 382 * This method will be called any number of times, based on number of 383 * encrypted streams received from application. 384 * 385 * @param[in] uniqueId Unique identifier for a session 386 * @param[in] decryptId Handle for the decryption session 387 * @param[in] decryptUnitId ID Specifies decryption unit, such as track ID 388 * @param[in] encBuffer Encrypted data block 389 * @param[out] decBuffer Decrypted data block 390 * @param[in] IV Optional buffer 391 * @return status_t 392 * Returns the error code for this API 393 * DRM_NO_ERROR for success, and one of DRM_ERROR_UNKNOWN, DRM_ERROR_LICENSE_EXPIRED 394 * DRM_ERROR_SESSION_NOT_OPENED, DRM_ERROR_DECRYPT_UNIT_NOT_INITIALIZED, 395 * DRM_ERROR_DECRYPT for failure. 396 */ 397 virtual status_t onDecrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, 398 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) = 0; 399 400 /** 401 * Finalize decryption for the given unit of the protected content 402 * 403 * @param[in] uniqueId Unique identifier for a session 404 * @param[in] decryptHandle Handle for the decryption session 405 * @param[in] decryptUnitId ID Specifies decryption unit, such as track ID 406 * @return status_t 407 * Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure 408 */ 409 virtual status_t onFinalizeDecryptUnit( 410 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) = 0; 411 412 /** 413 * Reads the specified number of bytes from an open DRM file. 414 * 415 * @param[in] uniqueId Unique identifier for a session 416 * @param[in] decryptHandle Handle for the decryption session 417 * @param[out] buffer Reference to the buffer that should receive the read data. 418 * @param[in] numBytes Number of bytes to read. 419 * @param[in] offset Offset with which to update the file position. 420 * 421 * @return Number of bytes read. Returns -1 for Failure. 422 */ 423 virtual ssize_t onPread(int uniqueId, DecryptHandle* decryptHandle, 424 void* buffer, ssize_t numBytes, off_t offset) = 0; 425}; 426 427}; 428 429#endif /* __DRM_ENGINE_BASE_H__ */ 430 431