summaryrefslogtreecommitdiffstats
path: root/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngine.h
blob: 34804cf6160a1a1d1207bc8fb434e872a47a4160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __FWDLOCKENGINE_H__
#define __FWDLOCKENGINE_H__

#include <DrmEngineBase.h>
#include <DrmConstraints.h>
#include <DrmRights.h>
#include <DrmInfo.h>
#include <DrmInfoStatus.h>
#include <DrmConvertedStatus.h>
#include <DrmInfoRequest.h>
#include <DrmSupportInfo.h>
#include <DrmInfoEvent.h>

#include "SessionMap.h"
#include "FwdLockConv.h"

namespace android {

/**
 * Forward Lock Engine class.
 */
class FwdLockEngine : public android::DrmEngineBase {

public:
    FwdLockEngine();
    virtual ~FwdLockEngine();

protected:
/**
 * Get constraint information associated with input content.
 *
 * @param uniqueId Unique identifier for a session
 * @param path Path of the protected content
 * @param action Actions defined such as,
 *     Action::DEFAULT, Action::PLAY, etc
 * @return DrmConstraints
 *     key-value pairs of constraint are embedded in it
 * @note
 *     In case of error, return NULL
 */
DrmConstraints* onGetConstraints(int uniqueId, const String8* path, int action);

/**
 * Get metadata information associated with input content.
 *
 * @param uniqueId Unique identifier for a session
 * @param path Path of the protected content
 * @return DrmMetadata
 *      For Forward Lock engine, it returns an empty object
 * @note
 *     In case of error, returns NULL
 */
DrmMetadata* onGetMetadata(int uniqueId, const String8* path);

/**
 * Initialize plug-in.
 *
 * @param uniqueId Unique identifier for a session
 * @return status_t
 *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
 */
status_t onInitialize(int uniqueId);

/**
 * Register a callback to be invoked when the caller required to
 * receive necessary information.
 *
 * @param uniqueId Unique identifier for a session
 * @param infoListener Listener
 * @return status_t
 *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
 */
status_t onSetOnInfoListener(int uniqueId, const IDrmEngine::OnInfoListener* infoListener);

/**
 * Terminate the plug-in and release resources bound to it.
 *
 * @param uniqueId Unique identifier for a session
 * @return status_t
 *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
 */
status_t onTerminate(int uniqueId);

/**
 * Get whether the given content can be handled by this plugin or not.
 *
 * @param uniqueId Unique identifier for a session
 * @param path Path to the protected object
 * @return bool
 *      Returns true if this plugin can handle , false in case of not able to handle
 */
bool onCanHandle(int uniqueId, const String8& path);

/**
 * Processes the given DRM information as appropriate for its type.
 * Not used for Forward Lock Engine.
 *
 * @param uniqueId Unique identifier for a session
 * @param drmInfo Information that needs to be processed
 * @return DrmInfoStatus
 *      instance as a result of processing given input
 */
DrmInfoStatus* onProcessDrmInfo(int uniqueId, const DrmInfo* drmInfo);

/**
 * Save DRM rights to specified rights path
 * and make association with content path.
 *
 * @param uniqueId Unique identifier for a session
 * @param drmRights DrmRights to be saved
 * @param rightsPath File path where rights to be saved
 * @param contentPath File path where content was saved
 * @return status_t
 *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
 */
status_t onSaveRights(int uniqueId,
                      const DrmRights& drmRights,
                      const String8& rightsPath,
                      const String8& contentPath);

/**
 * Retrieves necessary information for registration, unregistration or rights
 * acquisition information.
 *
 * @param uniqueId Unique identifier for a session
 * @param drmInfoRequest Request information to retrieve drmInfo
 * @return DrmInfo
 *      instance as a result of processing given input
 */
DrmInfo* onAcquireDrmInfo(int uniqueId,
                          const DrmInfoRequest* drmInfoRequest);

/**
 * Retrieves the mime type embedded inside the original content.
 *
 * @param uniqueId Unique identifier for a session
 * @param path Path of the protected content
 * @return String8
 *       Returns mime-type of the original content, such as "video/mpeg"
 */
String8 onGetOriginalMimeType(int uniqueId, const String8& path);

/**
 * Retrieves the type of the protected object (content, rights, etc..)
 * using specified path or mimetype. At least one parameter should be non null
 * to retrieve DRM object type.
 *
 * @param uniqueId Unique identifier for a session
 * @param path Path of the content or null.
 * @param mimeType Mime type of the content or null.
 * @return type of the DRM content,
 *     such as DrmObjectType::CONTENT, DrmObjectType::RIGHTS_OBJECT
 */
int onGetDrmObjectType(int uniqueId,
                       const String8& path,
                       const String8& mimeType);

/**
 * Check whether the given content has valid rights or not.
 *
 * @param uniqueId Unique identifier for a session
 * @param path Path of the protected content
 * @param action Action to perform (Action::DEFAULT, Action::PLAY, etc)
 * @return the status of the rights for the protected content,
 *     such as RightsStatus::RIGHTS_VALID, RightsStatus::RIGHTS_EXPIRED, etc.
 */
int onCheckRightsStatus(int uniqueId,
                        const String8& path,
                        int action);

/**
 * Consumes the rights for a content.
 * If the reserve parameter is true the rights are reserved until the same
 * application calls this api again with the reserve parameter set to false.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the decryption session
 * @param action Action to perform. (Action::DEFAULT, Action::PLAY, etc)
 * @param reserve True if the rights should be reserved.
 * @return status_t
 *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
 */
status_t onConsumeRights(int uniqueId,
                         DecryptHandle* decryptHandle,
                         int action,
                         bool reserve);

/**
 * Informs the DRM Engine about the playback actions performed on the DRM files.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the decryption session
 * @param playbackStatus Playback action (Playback::START, Playback::STOP, Playback::PAUSE)
 * @param position Position in the file (in milliseconds) where the start occurs.
 *     Only valid together with Playback::START.
 * @return status_t
 *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
 */
#ifdef USE_64BIT_DRM_API
status_t onSetPlaybackStatus(int uniqueId,
                             DecryptHandle* decryptHandle,
                             int playbackStatus,
                             int64_t position);
#else
status_t onSetPlaybackStatus(int uniqueId,
                             DecryptHandle* decryptHandle,
                             int playbackStatus,
                             int position);
#endif

/**
 *  Validates whether an action on the DRM content is allowed or not.
 *
 * @param uniqueId Unique identifier for a session
 * @param path Path of the protected content
 * @param action Action to validate (Action::PLAY, Action::TRANSFER, etc)
 * @param description Detailed description of the action
 * @return true if the action is allowed.
 */
bool onValidateAction(int uniqueId,
                      const String8& path,
                      int action,
                      const ActionDescription& description);

/**
 * Removes the rights associated with the given protected content.
 * Not used for Forward Lock Engine.
 *
 * @param uniqueId Unique identifier for a session
 * @param path Path of the protected content
 * @return status_t
 *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
 */
status_t onRemoveRights(int uniqueId, const String8& path);

/**
 * Removes all the rights information of each plug-in associated with
 * DRM framework. Will be used in master reset but does nothing for
 * Forward Lock Engine.
 *
 * @param uniqueId Unique identifier for a session
 * @return status_t
 *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
 */
status_t onRemoveAllRights(int uniqueId);

/**
 * Starts the Forward Lock file conversion session.
 * Each time the application tries to download a new DRM file
 * which needs to be converted, then the application has to
 * begin with calling this API. The convertId is used as the conversion session key
 * and must not be the same for different convert sessions.
 *
 * @param uniqueId Unique identifier for a session
 * @param convertId Handle for the convert session
 * @return status_t
 *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
 */
status_t onOpenConvertSession(int uniqueId, int convertId);

/**
 * Accepts and converts the input data which is part of DRM file.
 * The resultant converted data and the status is returned in the DrmConvertedInfo
 * object. This method will be called each time there is a new block
 * of data received by the application.
 *
 * @param uniqueId Unique identifier for a session
 * @param convertId Handle for the convert session
 * @param inputData Input Data which need to be converted
 * @return Return object contains the status of the data conversion,
 *       the output converted data and offset. In this case the
 *      application will ignore the offset information.
 */
DrmConvertedStatus* onConvertData(int uniqueId,
                                  int convertId,
                                  const DrmBuffer* inputData);

/**
 * Closes the convert session in case of data supply completed or error occurred.
 * Upon successful conversion of the complete data, it returns signature calculated over
 * the entire data used over a conversion session. This signature must be copied to the offset
 * mentioned in the DrmConvertedStatus. Signature is used for data integrity protection.
 *
 * @param uniqueId Unique identifier for a session
 * @param convertId Handle for the convert session
 * @return Return object contains the status of the data conversion,
 *      the header and body signature data. It also informs
 *      the application about the file offset at which this
 *      signature data should be written.
 */
DrmConvertedStatus* onCloseConvertSession(int uniqueId, int convertId);

/**
 * Returns the information about the Drm Engine capabilities which includes
 * supported MimeTypes and file suffixes.
 *
 * @param uniqueId Unique identifier for a session
 * @return DrmSupportInfo
 *      instance which holds the capabilities of a plug-in
 */
DrmSupportInfo* onGetSupportInfo(int uniqueId);

/**
 * Open the decrypt session to decrypt the given protected content.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the current decryption session
 * @param fd File descriptor of the protected content to be decrypted
 * @param offset Start position of the content
 * @param length The length of the protected content
 * @return
 *     DRM_ERROR_CANNOT_HANDLE for failure and DRM_NO_ERROR for success
 */
#ifdef USE_64BIT_DRM_API
status_t onOpenDecryptSession(int uniqueId,
                              DecryptHandle* decryptHandle,
                              int fd, off64_t offset, off64_t length);
#else
status_t onOpenDecryptSession(int uniqueId,
                              DecryptHandle* decryptHandle,
                              int fd, int offset, int length);
#endif

/**
 * Open the decrypt session to decrypt the given protected content.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the current decryption session
 * @param uri Path of the protected content to be decrypted
 * @return
 *     DRM_ERROR_CANNOT_HANDLE for failure and DRM_NO_ERROR for success
 */
status_t onOpenDecryptSession(int uniqueId,
                              DecryptHandle* decryptHandle,
                              const char* uri);

/**
 * Close the decrypt session for the given handle.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the decryption session
 * @return status_t
 *     Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
 */
status_t onCloseDecryptSession(int uniqueId,
                               DecryptHandle* decryptHandle);

/**
 * Initialize decryption for the given unit of the protected content.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the decryption session
 * @param decryptUnitId ID which specifies decryption unit, such as track ID
 * @param headerInfo Information for initializing decryption of this decrypUnit
 * @return
 *     DRM_ERROR_CANNOT_HANDLE for failure and DRM_NO_ERROR for success
 */
status_t onInitializeDecryptUnit(int uniqueId,
                                 DecryptHandle* decryptHandle,
                                 int decryptUnitId,
                                 const DrmBuffer* headerInfo);

/**
 * Decrypt the protected content buffers for the given unit.
 * This method will be called any number of times, based on number of
 * encrypted streams received from application.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the decryption session
 * @param decryptUnitId ID which specifies decryption unit, such as track ID
 * @param encBuffer Encrypted data block
 * @param decBuffer Decrypted data block
 * @return status_t
 *     Returns the error code for this API
 *     DRM_NO_ERROR for success, and one of DRM_ERROR_UNKNOWN, DRM_ERROR_LICENSE_EXPIRED
 *     DRM_ERROR_SESSION_NOT_OPENED, DRM_ERROR_DECRYPT_UNIT_NOT_INITIALIZED,
 *     DRM_ERROR_DECRYPT for failure.
 */
status_t onDecrypt(int uniqueId,
                   DecryptHandle* decryptHandle,
                   int decryptUnitId,
                   const DrmBuffer* encBuffer,
                   DrmBuffer** decBuffer);

/**
 * Decrypt the protected content buffers for the given unit.
 * This method will be called any number of times, based on number of
 * encrypted streams received from application.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptId Handle for the decryption session
 * @param decryptUnitId ID Specifies decryption unit, such as track ID
 * @param encBuffer Encrypted data block
 * @param decBuffer Decrypted data block
 * @param IV Optional buffer
 * @return status_t
 *     Returns the error code for this API
 *     DRM_NO_ERROR for success, and one of DRM_ERROR_UNKNOWN, DRM_ERROR_LICENSE_EXPIRED
 *     DRM_ERROR_SESSION_NOT_OPENED, DRM_ERROR_DECRYPT_UNIT_NOT_INITIALIZED,
 *     DRM_ERROR_DECRYPT for failure.
 */
status_t onDecrypt(int uniqueId, DecryptHandle* decryptHandle,
                   int decryptUnitId, const DrmBuffer* encBuffer,
                   DrmBuffer** decBuffer, DrmBuffer* IV);

/**
 * Finalize decryption for the given unit of the protected content.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the decryption session
 * @param decryptUnitId ID Specifies decryption unit, such as track ID
 * @return
 *     DRM_ERROR_CANNOT_HANDLE for failure and DRM_NO_ERROR for success
 */
status_t onFinalizeDecryptUnit(int uniqueId,
                               DecryptHandle* decryptHandle,
                               int decryptUnitId);

/**
 * Reads the specified number of bytes from an open DRM file.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the decryption session
 * @param buffer Reference to the buffer that should receive the read data.
 * @param numBytes Number of bytes to read.
 *
 * @return Number of bytes read.
 * @retval -1 Failure.
 */
ssize_t onRead(int uniqueId,
               DecryptHandle* decryptHandle,
               void* pBuffer,
               int numBytes);

/**
 * Updates the file position within an open DRM file.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the decryption session
 * @param offset Offset with which to update the file position.
 * @param whence One of SEEK_SET, SEEK_CUR, and SEEK_END.
 *           These constants are defined in unistd.h.
 *
 * @return New file position.
 * @retval ((off_t)-1) Failure.
 */
#ifdef USE_64BIT_DRM_API
off64_t onLseek(int uniqueId,
                DecryptHandle* decryptHandle,
                off64_t offset,
                int whence);
#else
off_t onLseek(int uniqueId,
              DecryptHandle* decryptHandle,
              off_t offset,
              int whence);
#endif

/**
 * Reads the specified number of bytes from an open DRM file.
 *
 * @param uniqueId Unique identifier for a session
 * @param decryptHandle Handle for the decryption session
 * @param buffer Reference to the buffer that should receive the read data.
 * @param numBytes Number of bytes to read.
 * @param offset Offset with which to update the file position.
 *
 * @return Number of bytes read. Returns -1 for Failure.
 */
#ifdef USE_64BIT_DRM_API
ssize_t onPread(int uniqueId,
                DecryptHandle* decryptHandle,
                void* buffer,
                ssize_t numBytes,
                off64_t offset);
#else
ssize_t onPread(int uniqueId,
                DecryptHandle* decryptHandle,
                void* buffer,
                ssize_t numBytes,
                off_t offset);
#endif

private:

/**
 * Session Class for Forward Lock Conversion. An object of this class is created
 * for every conversion.
 */
class ConvertSession {
    public :
        int uniqueId;
        FwdLockConv_Output_t output;

        ConvertSession() {
            uniqueId = 0;
            memset(&output, 0, sizeof(FwdLockConv_Output_t));
        }

        virtual ~ConvertSession() {}
};

/**
 * Session Class for Forward Lock decoder. An object of this class is created
 * for every decoding session.
 */
class DecodeSession {
    public :
        int fileDesc;
        off_t offset;

        DecodeSession() {
            fileDesc = -1;
            offset = 0;
        }

        DecodeSession(int fd) {
            fileDesc = fd;
            offset = 0;
        }

        virtual ~DecodeSession() {}
};

/**
 * Session Map Tables for Conversion and Decoding of forward lock files.
 */
SessionMap<ConvertSession*> convertSessionMap;
SessionMap<DecodeSession*> decodeSessionMap;

/**
 * Converts the error code from Forward Lock Converter to DrmConvertStatus error code.
 *
 * @param Forward Lock Converter error code
 *
 * @return Status code from DrmConvertStatus.
 */
static int getConvertedStatus(FwdLockConv_Status_t status);
};

};

#endif /* __FWDLOCKENGINE_H__ */