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 CLEARKEY_CAS_PLUGIN_H_
18 #define CLEARKEY_CAS_PLUGIN_H_
19 
20 #include <media/cas/CasAPI.h>
21 #include <media/cas/DescramblerAPI.h>
22 #include <utils/Mutex.h>
23 #include <utils/StrongPointer.h>
24 
25 extern "C" {
26       android::CasFactory *createCasFactory();
27       android::DescramblerFactory *createDescramblerFactory();
28 }
29 
30 namespace android {
31 namespace clearkeycas {
32 
33 class KeyFetcher;
34 class ClearKeyCasSession;
35 
36 class ClearKeyCasFactory : public CasFactory {
37 public:
ClearKeyCasFactory()38     ClearKeyCasFactory() {}
~ClearKeyCasFactory()39     virtual ~ClearKeyCasFactory() {}
40 
41     virtual bool isSystemIdSupported(
42             int32_t CA_system_id) const override;
43     virtual status_t queryPlugins(
44             std::vector<CasPluginDescriptor> *descriptors) const override;
45     virtual status_t createPlugin(
46             int32_t CA_system_id,
47             void *appData,
48             CasPluginCallback callback,
49             CasPlugin **plugin) override;
50     virtual status_t createPlugin(
51             int32_t CA_system_id,
52             void *appData,
53             CasPluginCallbackExt callback,
54             CasPlugin **plugin) override;
55 };
56 
57 class ClearKeyDescramblerFactory : public DescramblerFactory {
58 public:
ClearKeyDescramblerFactory()59     ClearKeyDescramblerFactory() {}
~ClearKeyDescramblerFactory()60     virtual ~ClearKeyDescramblerFactory() {}
61 
62     virtual bool isSystemIdSupported(
63             int32_t CA_system_id) const override;
64     virtual status_t createPlugin(
65             int32_t CA_system_id, DescramblerPlugin **plugin) override;
66 };
67 
68 class ClearKeyCasPlugin : public CasPlugin {
69 public:
70     ClearKeyCasPlugin(void *appData, CasPluginCallback callback);
71     ClearKeyCasPlugin(void *appData, CasPluginCallbackExt callback);
72     virtual ~ClearKeyCasPlugin();
73 
74     virtual status_t setStatusCallback(
75             CasPluginStatusCallback callback) override;
76 
77     virtual status_t setPrivateData(
78             const CasData &data) override;
79 
80     virtual status_t openSession(CasSessionId *sessionId) override;
81 
82     virtual status_t openSession(uint32_t intent, uint32_t mode,
83                                      CasSessionId *sessionId) override;
84 
85     virtual status_t closeSession(
86             const CasSessionId &sessionId) override;
87 
88     virtual status_t setSessionPrivateData(
89             const CasSessionId &sessionId,
90             const CasData &data) override;
91 
92     virtual status_t processEcm(
93             const CasSessionId &sessionId, const CasEcm &ecm) override;
94 
95     virtual status_t processEmm(const CasEmm &emm) override;
96 
97     virtual status_t sendEvent(
98             int32_t event, int32_t arg, const CasData &eventData) override;
99 
100     virtual status_t sendSessionEvent(
101             const CasSessionId &sessionId,
102             int32_t event, int32_t arg, const CasData &eventData) override;
103 
104     virtual status_t provision(const String8 &str) override;
105 
106     virtual status_t refreshEntitlements(
107             int32_t refreshType, const CasData &refreshData) override;
108 
109 private:
110     Mutex mKeyFetcherLock;
111     std::unique_ptr<KeyFetcher> mKeyFetcher;
112     CasPluginCallback mCallback;
113     CasPluginCallbackExt mCallbackExt;
114     CasPluginStatusCallback mStatusCallback;
115     void* mAppData;
116 };
117 
118 class ClearKeyDescramblerPlugin : public DescramblerPlugin {
119 public:
ClearKeyDescramblerPlugin()120     ClearKeyDescramblerPlugin() {}
~ClearKeyDescramblerPlugin()121     virtual ~ClearKeyDescramblerPlugin() {};
122 
123     virtual bool requiresSecureDecoderComponent(
124             const char *mime) const override;
125 
126     virtual status_t setMediaCasSession(
127             const CasSessionId &sessionId) override;
128 
129     virtual ssize_t descramble(
130             bool secure,
131             ScramblingControl scramblingControl,
132             size_t numSubSamples,
133             const SubSample *subSamples,
134             const void *srcPtr,
135             int32_t srcOffset,
136             void *dstPtr,
137             int32_t dstOffset,
138             AString *errorDetailMsg) override;
139 
140 private:
141     std::shared_ptr<ClearKeyCasSession> mCASSession;
142 
143     String8 subSamplesToString(
144             SubSample const *subSamples,
145             size_t numSubSamples) const;
146     String8 arrayToString(uint8_t const *array, size_t len) const;
147 };
148 
149 } // namespace clearkeycas
150 } // namespace android
151 
152 #endif // CLEARKEY_CAS_PLUGIN_H_
153