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