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