xref: /aosp_15_r20/frameworks/av/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngine.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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 __FWDLOCKENGINE_H__
18 #define __FWDLOCKENGINE_H__
19 
20 #include <DrmEngineBase.h>
21 #include <drm/DrmConstraints.h>
22 #include <drm/DrmRights.h>
23 #include <drm/DrmInfo.h>
24 #include <drm/DrmInfoStatus.h>
25 #include <drm/DrmConvertedStatus.h>
26 #include <drm/DrmInfoRequest.h>
27 #include <drm/DrmSupportInfo.h>
28 #include <drm/DrmInfoEvent.h>
29 
30 #include "SessionMap.h"
31 #include "FwdLockConv.h"
32 
33 namespace android {
34 
35 /**
36  * Forward Lock Engine class.
37  */
38 class FwdLockEngine : public android::DrmEngineBase {
39 
40 public:
41     FwdLockEngine();
42     virtual ~FwdLockEngine();
43 
44 protected:
45 /**
46  * Get constraint information associated with input content.
47  *
48  * @param uniqueId Unique identifier for a session
49  * @param path Path of the protected content
50  * @param action Actions defined such as,
51  *     Action::DEFAULT, Action::PLAY, etc
52  * @return DrmConstraints
53  *     key-value pairs of constraint are embedded in it
54  * @note
55  *     In case of error, return NULL
56  */
57 DrmConstraints* onGetConstraints(int uniqueId, const String8* path, int action);
58 
59 /**
60  * Get metadata information associated with input content.
61  *
62  * @param uniqueId Unique identifier for a session
63  * @param path Path of the protected content
64  * @return DrmMetadata
65  *      For Forward Lock engine, it returns an empty object
66  * @note
67  *     In case of error, returns NULL
68  */
69 DrmMetadata* onGetMetadata(int uniqueId, const String8* path);
70 
71 /**
72  * Initialize plug-in.
73  *
74  * @param uniqueId Unique identifier for a session
75  * @return status_t
76  *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
77  */
78 status_t onInitialize(int uniqueId);
79 
80 /**
81  * Register a callback to be invoked when the caller required to
82  * receive necessary information.
83  *
84  * @param uniqueId Unique identifier for a session
85  * @param infoListener Listener
86  * @return status_t
87  *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
88  */
89 status_t onSetOnInfoListener(int uniqueId, const IDrmEngine::OnInfoListener* infoListener);
90 
91 /**
92  * Terminate the plug-in and release resources bound to it.
93  *
94  * @param uniqueId Unique identifier for a session
95  * @return status_t
96  *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
97  */
98 status_t onTerminate(int uniqueId);
99 
100 /**
101  * Get whether the given content can be handled by this plugin or not.
102  *
103  * @param uniqueId Unique identifier for a session
104  * @param path Path to the protected object
105  * @return bool
106  *      Returns true if this plugin can handle , false in case of not able to handle
107  */
108 bool onCanHandle(int uniqueId, const String8& path);
109 
110 /**
111  * Processes the given DRM information as appropriate for its type.
112  * Not used for Forward Lock Engine.
113  *
114  * @param uniqueId Unique identifier for a session
115  * @param drmInfo Information that needs to be processed
116  * @return DrmInfoStatus
117  *      instance as a result of processing given input
118  */
119 DrmInfoStatus* onProcessDrmInfo(int uniqueId, const DrmInfo* drmInfo);
120 
121 /**
122  * Save DRM rights to specified rights path
123  * and make association with content path.
124  *
125  * @param uniqueId Unique identifier for a session
126  * @param drmRights DrmRights to be saved
127  * @param rightsPath File path where rights to be saved
128  * @param contentPath File path where content was saved
129  * @return status_t
130  *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
131  */
132 status_t onSaveRights(int uniqueId,
133                       const DrmRights& drmRights,
134                       const String8& rightsPath,
135                       const String8& contentPath);
136 
137 /**
138  * Retrieves necessary information for registration, unregistration or rights
139  * acquisition information.
140  *
141  * @param uniqueId Unique identifier for a session
142  * @param drmInfoRequest Request information to retrieve drmInfo
143  * @return DrmInfo
144  *      instance as a result of processing given input
145  */
146 DrmInfo* onAcquireDrmInfo(int uniqueId,
147                           const DrmInfoRequest* drmInfoRequest);
148 
149 /**
150  * Retrieves the mime type embedded inside the original content.
151  *
152  * @param uniqueId Unique identifier for a session
153  * @param path Path of the conent or null.
154  * @param fd descriptor of the protected content as a file source
155  * @return String8
156  *       Returns mime-type of the original content, such as "video/mpeg"
157  */
158 String8 onGetOriginalMimeType(int uniqueId, const String8& path, int fd);
159 
160 /**
161  * Retrieves the type of the protected object (content, rights, etc..)
162  * using specified path or mimetype. At least one parameter should be non null
163  * to retrieve DRM object type.
164  *
165  * @param uniqueId Unique identifier for a session
166  * @param path Path of the content or null.
167  * @param mimeType Mime type of the content or null.
168  * @return type of the DRM content,
169  *     such as DrmObjectType::CONTENT, DrmObjectType::RIGHTS_OBJECT
170  */
171 int onGetDrmObjectType(int uniqueId,
172                        const String8& path,
173                        const String8& mimeType);
174 
175 /**
176  * Check whether the given content has valid rights or not.
177  *
178  * @param uniqueId Unique identifier for a session
179  * @param path Path of the protected content
180  * @param action Action to perform (Action::DEFAULT, Action::PLAY, etc)
181  * @return the status of the rights for the protected content,
182  *     such as RightsStatus::RIGHTS_VALID, RightsStatus::RIGHTS_EXPIRED, etc.
183  */
184 int onCheckRightsStatus(int uniqueId,
185                         const String8& path,
186                         int action);
187 
188 /**
189  * Consumes the rights for a content.
190  * If the reserve parameter is true the rights are reserved until the same
191  * application calls this api again with the reserve parameter set to false.
192  *
193  * @param uniqueId Unique identifier for a session
194  * @param decryptHandle Handle for the decryption session
195  * @param action Action to perform. (Action::DEFAULT, Action::PLAY, etc)
196  * @param reserve True if the rights should be reserved.
197  * @return status_t
198  *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
199  */
200 status_t onConsumeRights(int uniqueId,
201                          sp<DecryptHandle>& decryptHandle,
202                          int action,
203                          bool reserve);
204 
205 /**
206  * Informs the DRM Engine about the playback actions performed on the DRM files.
207  *
208  * @param uniqueId Unique identifier for a session
209  * @param decryptHandle Handle for the decryption session
210  * @param playbackStatus Playback action (Playback::START, Playback::STOP, Playback::PAUSE)
211  * @param position Position in the file (in milliseconds) where the start occurs.
212  *     Only valid together with Playback::START.
213  * @return status_t
214  *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
215  */
216 #ifdef USE_64BIT_DRM_API
217 status_t onSetPlaybackStatus(int uniqueId,
218                              sp<DecryptHandle>& decryptHandle,
219                              int playbackStatus,
220                              int64_t position);
221 #else
222 status_t onSetPlaybackStatus(int uniqueId,
223                              sp<DecryptHandle>& decryptHandle,
224                              int playbackStatus,
225                              int position);
226 #endif
227 
228 /**
229  *  Validates whether an action on the DRM content is allowed or not.
230  *
231  * @param uniqueId Unique identifier for a session
232  * @param path Path of the protected content
233  * @param action Action to validate (Action::PLAY, Action::TRANSFER, etc)
234  * @param description Detailed description of the action
235  * @return true if the action is allowed.
236  */
237 bool onValidateAction(int uniqueId,
238                       const String8& path,
239                       int action,
240                       const ActionDescription& description);
241 
242 /**
243  * Removes the rights associated with the given protected content.
244  * Not used for Forward Lock Engine.
245  *
246  * @param uniqueId Unique identifier for a session
247  * @param path Path of the protected content
248  * @return status_t
249  *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
250  */
251 status_t onRemoveRights(int uniqueId, const String8& path);
252 
253 /**
254  * Removes all the rights information of each plug-in associated with
255  * DRM framework. Does nothing for Forward Lock Engine.
256  *
257  * @param uniqueId Unique identifier for a session
258  * @return status_t
259  *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
260  */
261 status_t onRemoveAllRights(int uniqueId);
262 
263 /**
264  * Starts the Forward Lock file conversion session.
265  * Each time the application tries to download a new DRM file
266  * which needs to be converted, then the application has to
267  * begin with calling this API. The convertId is used as the conversion session key
268  * and must not be the same for different convert sessions.
269  *
270  * @param uniqueId Unique identifier for a session
271  * @param convertId Handle for the convert session
272  * @return status_t
273  *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
274  */
275 status_t onOpenConvertSession(int uniqueId, int convertId);
276 
277 /**
278  * Accepts and converts the input data which is part of DRM file.
279  * The resultant converted data and the status is returned in the DrmConvertedInfo
280  * object. This method will be called each time there is a new block
281  * of data received by the application.
282  *
283  * @param uniqueId Unique identifier for a session
284  * @param convertId Handle for the convert session
285  * @param inputData Input Data which need to be converted
286  * @return Return object contains the status of the data conversion,
287  *       the output converted data and offset. In this case the
288  *      application will ignore the offset information.
289  */
290 DrmConvertedStatus* onConvertData(int uniqueId,
291                                   int convertId,
292                                   const DrmBuffer* inputData);
293 
294 /**
295  * Closes the convert session in case of data supply completed or error occurred.
296  * Upon successful conversion of the complete data, it returns signature calculated over
297  * the entire data used over a conversion session. This signature must be copied to the offset
298  * mentioned in the DrmConvertedStatus. Signature is used for data integrity protection.
299  *
300  * @param uniqueId Unique identifier for a session
301  * @param convertId Handle for the convert session
302  * @return Return object contains the status of the data conversion,
303  *      the header and body signature data. It also informs
304  *      the application about the file offset at which this
305  *      signature data should be written.
306  */
307 DrmConvertedStatus* onCloseConvertSession(int uniqueId, int convertId);
308 
309 /**
310  * Returns the information about the Drm Engine capabilities which includes
311  * supported MimeTypes and file suffixes.
312  *
313  * @param uniqueId Unique identifier for a session
314  * @return DrmSupportInfo
315  *      instance which holds the capabilities of a plug-in
316  */
317 DrmSupportInfo* onGetSupportInfo(int uniqueId);
318 
319 /**
320  * Open the decrypt session to decrypt the given protected content.
321  *
322  * @param uniqueId Unique identifier for a session
323  * @param decryptHandle Handle for the current decryption session
324  * @param fd File descriptor of the protected content to be decrypted
325  * @param offset Start position of the content
326  * @param length The length of the protected content
327  * @return
328  *     DRM_ERROR_CANNOT_HANDLE for failure and DRM_NO_ERROR for success
329  */
330 #ifdef USE_64BIT_DRM_API
331 status_t onOpenDecryptSession(int uniqueId,
332                               sp<DecryptHandle>& decryptHandle,
333                               int fd, off64_t offset, off64_t length);
334 #else
335 status_t onOpenDecryptSession(int uniqueId,
336                               sp<DecryptHandle>& decryptHandle,
337                               int fd, int offset, int length);
338 #endif
339 
340 /**
341  * Open the decrypt session to decrypt the given protected content.
342  *
343  * @param uniqueId Unique identifier for a session
344  * @param decryptHandle Handle for the current decryption session
345  * @param uri Path of the protected content to be decrypted
346  * @return
347  *     DRM_ERROR_CANNOT_HANDLE for failure and DRM_NO_ERROR for success
348  */
349 status_t onOpenDecryptSession(int uniqueId,
350                               sp<DecryptHandle>& decryptHandle,
351                               const char* uri);
352 
353 /**
354  * Close the decrypt session for the given handle.
355  *
356  * @param uniqueId Unique identifier for a session
357  * @param decryptHandle Handle for the decryption session
358  * @return status_t
359  *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
360  */
361 status_t onCloseDecryptSession(int uniqueId,
362                                sp<DecryptHandle>& decryptHandle);
363 
364 /**
365  * Initialize decryption for the given unit of the protected content.
366  *
367  * @param uniqueId Unique identifier for a session
368  * @param decryptHandle Handle for the decryption session
369  * @param decryptUnitId ID which specifies decryption unit, such as track ID
370  * @param headerInfo Information for initializing decryption of this decrypUnit
371  * @return
372  *     DRM_ERROR_CANNOT_HANDLE for failure and DRM_NO_ERROR for success
373  */
374 status_t onInitializeDecryptUnit(int uniqueId,
375                                  sp<DecryptHandle>& decryptHandle,
376                                  int decryptUnitId,
377                                  const DrmBuffer* headerInfo);
378 
379 /**
380  * Decrypt the protected content buffers for the given unit.
381  * This method will be called any number of times, based on number of
382  * encrypted streams received from application.
383  *
384  * @param uniqueId Unique identifier for a session
385  * @param decryptHandle Handle for the decryption session
386  * @param decryptUnitId ID which specifies decryption unit, such as track ID
387  * @param encBuffer Encrypted data block
388  * @param decBuffer Decrypted data block
389  * @return status_t
390  *     Returns the error code for this API
391  *     DRM_NO_ERROR for success, and one of DRM_ERROR_UNKNOWN, DRM_ERROR_LICENSE_EXPIRED
392  *     DRM_ERROR_SESSION_NOT_OPENED, DRM_ERROR_DECRYPT_UNIT_NOT_INITIALIZED,
393  *     DRM_ERROR_DECRYPT for failure.
394  */
395 status_t onDecrypt(int uniqueId,
396                    sp<DecryptHandle>& decryptHandle,
397                    int decryptUnitId,
398                    const DrmBuffer* encBuffer,
399                    DrmBuffer** decBuffer);
400 
401 /**
402  * Decrypt the protected content buffers for the given unit.
403  * This method will be called any number of times, based on number of
404  * encrypted streams received from application.
405  *
406  * @param uniqueId Unique identifier for a session
407  * @param decryptId Handle for the decryption session
408  * @param decryptUnitId ID Specifies decryption unit, such as track ID
409  * @param encBuffer Encrypted data block
410  * @param decBuffer Decrypted data block
411  * @param IV Optional buffer
412  * @return status_t
413  *     Returns the error code for this API
414  *     DRM_NO_ERROR for success, and one of DRM_ERROR_UNKNOWN, DRM_ERROR_LICENSE_EXPIRED
415  *     DRM_ERROR_SESSION_NOT_OPENED, DRM_ERROR_DECRYPT_UNIT_NOT_INITIALIZED,
416  *     DRM_ERROR_DECRYPT for failure.
417  */
418 status_t onDecrypt(int uniqueId, sp<DecryptHandle>& decryptHandle,
419                    int decryptUnitId, const DrmBuffer* encBuffer,
420                    DrmBuffer** decBuffer, DrmBuffer* IV);
421 
422 /**
423  * Finalize decryption for the given unit of the protected content.
424  *
425  * @param uniqueId Unique identifier for a session
426  * @param decryptHandle Handle for the decryption session
427  * @param decryptUnitId ID Specifies decryption unit, such as track ID
428  * @return
429  *     DRM_ERROR_CANNOT_HANDLE for failure and DRM_NO_ERROR for success
430  */
431 status_t onFinalizeDecryptUnit(int uniqueId,
432                                sp<DecryptHandle>& decryptHandle,
433                                int decryptUnitId);
434 
435 /**
436  * Reads the specified number of bytes from an open DRM file.
437  *
438  * @param uniqueId Unique identifier for a session
439  * @param decryptHandle Handle for the decryption session
440  * @param buffer Reference to the buffer that should receive the read data.
441  * @param numBytes Number of bytes to read.
442  *
443  * @return Number of bytes read.
444  * @retval -1 Failure.
445  */
446 ssize_t onRead(int uniqueId,
447                sp<DecryptHandle>& decryptHandle,
448                void* pBuffer,
449                int numBytes);
450 
451 /**
452  * Updates the file position within an open DRM file.
453  *
454  * @param uniqueId Unique identifier for a session
455  * @param decryptHandle Handle for the decryption session
456  * @param offset Offset with which to update the file position.
457  * @param whence One of SEEK_SET, SEEK_CUR, and SEEK_END.
458  *           These constants are defined in unistd.h.
459  *
460  * @return New file position.
461  * @retval ((off_t)-1) Failure.
462  */
463 #ifdef USE_64BIT_DRM_API
464 off64_t onLseek(int uniqueId,
465                 sp<DecryptHandle>& decryptHandle,
466                 off64_t offset,
467                 int whence);
468 #else
469 off_t onLseek(int uniqueId,
470               sp<DecryptHandle>& decryptHandle,
471               off_t offset,
472               int whence);
473 #endif
474 
475 /**
476  * Reads the specified number of bytes from an open DRM file.
477  *
478  * @param uniqueId Unique identifier for a session
479  * @param decryptHandle Handle for the decryption session
480  * @param buffer Reference to the buffer that should receive the read data.
481  * @param numBytes Number of bytes to read.
482  * @param offset Offset with which to update the file position.
483  *
484  * @return Number of bytes read. Returns -1 for Failure.
485  */
486 #ifdef USE_64BIT_DRM_API
487 ssize_t onPread(int uniqueId,
488                 sp<DecryptHandle>& decryptHandle,
489                 void* buffer,
490                 ssize_t numBytes,
491                 off64_t offset);
492 #else
493 ssize_t onPread(int uniqueId,
494                 sp<DecryptHandle>& decryptHandle,
495                 void* buffer,
496                 ssize_t numBytes,
497                 off_t offset);
498 #endif
499 
500 private:
501 
502     static const String8 Description;
503     static const String8 FileSuffixes[];
504     static const String8 MimeTypes[];
505     static bool IsFileSuffixSupported(const String8& suffix);
506     static bool IsMimeTypeSupported(const String8& mime);
507     static void AddSupportedMimeTypes(DrmSupportInfo *info);
508     static void AddSupportedFileSuffixes(DrmSupportInfo *info);
509 
510 /**
511  * Session Class for Forward Lock Conversion. An object of this class is created
512  * for every conversion.
513  */
514 class ConvertSession {
515     public :
516         int uniqueId;
517         FwdLockConv_Output_t output;
518 
ConvertSession()519         ConvertSession() {
520             uniqueId = 0;
521             memset(&output, 0, sizeof(FwdLockConv_Output_t));
522         }
523 
~ConvertSession()524         virtual ~ConvertSession() {}
525 };
526 
527 /**
528  * Session Class for Forward Lock decoder. An object of this class is created
529  * for every decoding session.
530  */
531 class DecodeSession {
532     public :
533         int fileDesc;
534         off_t offset;
535 
DecodeSession()536         DecodeSession() {
537             fileDesc = -1;
538             offset = 0;
539         }
540 
DecodeSession(int fd)541         explicit DecodeSession(int fd) {
542             fileDesc = fd;
543             offset = 0;
544         }
545 
~DecodeSession()546         virtual ~DecodeSession() {}
547 };
548 
549 /**
550  * Session Map Tables for Conversion and Decoding of forward lock files.
551  */
552 SessionMap<ConvertSession*> convertSessionMap;
553 SessionMap<DecodeSession*> decodeSessionMap;
554 
555 /**
556  * Converts the error code from Forward Lock Converter to DrmConvertStatus error code.
557  *
558  * @param Forward Lock Converter error code
559  *
560  * @return Status code from DrmConvertStatus.
561  */
562 static int getConvertedStatus(FwdLockConv_Status_t status);
563 };
564 
565 };
566 
567 #endif /* __FWDLOCKENGINE_H__ */
568