1/*
2 * Copyright (C) 2017 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 DESCRAMBLER_API_H_
18#define DESCRAMBLER_API_H_
19
20#include <media/stagefright/MediaErrors.h>
21#include <media/cas/CasAPI.h>
22
23namespace android {
24
25struct AString;
26struct DescramblerPlugin;
27
28struct DescramblerFactory {
29    DescramblerFactory() {}
30    virtual ~DescramblerFactory() {}
31
32    // Determine if the plugin can handle the CA scheme identified by CA_system_id.
33    virtual bool isSystemIdSupported(
34            int32_t CA_system_id) const = 0;
35
36    // Construct a new instance of a DescramblerPlugin given a CA_system_id
37    virtual status_t createPlugin(
38            int32_t CA_system_id, DescramblerPlugin **plugin) = 0;
39
40private:
41    DescramblerFactory(const DescramblerFactory &);
42    DescramblerFactory &operator=(const DescramblerFactory &);
43};
44
45struct DescramblerPlugin {
46    enum ScramblingControl {
47        kScrambling_Unscrambled = 0,
48        kScrambling_Reserved    = 1,
49        kScrambling_EvenKey     = 2,
50        kScrambling_OddKey      = 3,
51    };
52
53    struct SubSample {
54        uint32_t mNumBytesOfClearData;
55        uint32_t mNumBytesOfEncryptedData;
56    };
57
58    DescramblerPlugin() {}
59    virtual ~DescramblerPlugin() {}
60
61    // If this method returns false, a non-secure decoder will be used to
62    // decode the data after decryption. The decrypt API below will have
63    // to support insecure decryption of the data (secure = false) for
64    // media data of the given mime type.
65    virtual bool requiresSecureDecoderComponent(const char *mime) const = 0;
66
67    // A MediaCas session may be associated with a MediaCrypto session.  The
68    // associated MediaCas session is used to load decryption keys
69    // into the crypto/cas plugin.  The keys are then referenced by key-id
70    // in the 'key' parameter to the decrypt() method.
71    // Should return NO_ERROR on success, ERROR_DRM_SESSION_NOT_OPENED if
72    // the session is not opened and a code from MediaErrors.h otherwise.
73    virtual status_t setMediaCasSession(const CasSessionId& sessionId) = 0;
74
75    // If the error returned falls into the range
76    // ERROR_DRM_VENDOR_MIN..ERROR_DRM_VENDOR_MAX, errorDetailMsg should be
77    // filled in with an appropriate string.
78    // At the java level these special errors will then trigger a
79    // MediaCodec.CryptoException that gives clients access to both
80    // the error code and the errorDetailMsg.
81    // Returns a non-negative result to indicate the number of bytes written
82    // to the dstPtr, or a negative result to indicate an error.
83    virtual ssize_t descramble(
84            bool secure,
85            ScramblingControl scramblingControl,
86            size_t numSubSamples,
87            const SubSample *subSamples,
88            const void *srcPtr,
89            int32_t srcOffset,
90            void *dstPtr,
91            int32_t dstOffset,
92            AString *errorDetailMsg) = 0;
93
94private:
95    DescramblerPlugin(const DescramblerPlugin &);
96    DescramblerPlugin &operator=(const DescramblerPlugin &);
97};
98
99}  // namespace android
100
101extern "C" {
102    extern android::DescramblerFactory *createDescramblerFactory();
103}
104
105#endif  // DESCRAMBLER_API_H_
106