1 /*
2  * Copyright (C) 2021 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 CRYPTO_HAL_AIDL_H_
18 #define CRYPTO_HAL_AIDL_H_
19 
20 #include <aidl/android/hardware/drm/ICryptoPlugin.h>
21 #include <aidl/android/hardware/drm/IDrmFactory.h>
22 #include <mediadrm/ICrypto.h>
23 #include <utils/KeyedVector.h>
24 #include <utils/threads.h>
25 
26 using IDrmFactoryAidl = ::aidl::android::hardware::drm::IDrmFactory;
27 using ICryptoPluginAidl = ::aidl::android::hardware::drm::ICryptoPlugin;
28 using ::aidl::android::hardware::drm::Uuid;
29 
30 // -------Hidl interface related-----------------
31 // TODO: replace before removing hidl interface
32 using ::android::hardware::drm::V1_0::DestinationBuffer;
33 using ::android::hardware::drm::V1_0::SharedBuffer;
34 
35 using ::android::hardware::HidlMemory;
36 
37 // -------Hidl interface related end-------------
38 
39 class IMemoryHeap;
40 
41 namespace android {
42 
43 struct CryptoHalAidl : public ICrypto {
44     CryptoHalAidl();
45     virtual ~CryptoHalAidl();
46     virtual status_t initCheck() const;
47     virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]);
48     virtual status_t createPlugin(const uint8_t uuid[16], const void* data, size_t size);
49     virtual status_t destroyPlugin();
50     virtual bool requiresSecureDecoderComponent(const char* mime) const;
51     virtual void notifyResolution(uint32_t width, uint32_t height);
52     virtual DrmStatus setMediaDrmSession(const Vector<uint8_t>& sessionId);
53     virtual ssize_t decrypt(const uint8_t key[16], const uint8_t iv[16], CryptoPlugin::Mode mode,
54                             const CryptoPlugin::Pattern& pattern, const ::SharedBuffer& source,
55                             size_t offset, const CryptoPlugin::SubSample* subSamples,
56                             size_t numSubSamples, const ::DestinationBuffer& destination,
57                             AString* errorDetailMsg);
58     virtual int32_t setHeap(const sp<HidlMemory>& heap);
59     virtual void unsetHeap(int32_t seqNum);
60     virtual status_t getLogMessages(Vector<drm::V1_4::LogMessage>& logs) const;
61 
62   private:
63     mutable Mutex mLock;
64 
65     const std::vector<std::shared_ptr<IDrmFactoryAidl>> mFactories;
66     std::shared_ptr<ICryptoPluginAidl> mPlugin;
67 
68     /**
69      * mInitCheck is:
70      *   NO_INIT if a plugin hasn't been created yet
71      *   ERROR_UNSUPPORTED if a plugin can't be created for the uuid
72      *   OK after a plugin has been created and mPlugin is valid
73      */
74     status_t mInitCheck;
75 
76     KeyedVector<int32_t, size_t> mHeapSizes;
77     int32_t mHeapSeqNum;
78 
79     std::shared_ptr<ICryptoPluginAidl> makeCryptoPlugin(
80             const std::shared_ptr<IDrmFactoryAidl>& factory, const Uuid& uuidAidl,
81             const std::vector<uint8_t> initData);
82 
83     status_t checkSharedBuffer(const ::SharedBuffer& buffer);
84     bool isCryptoSchemeSupportedInternal(const uint8_t uuid[16], int* factoryIdx);
85 
86     DISALLOW_EVIL_CONSTRUCTORS(CryptoHalAidl);
87 };
88 
89 }  // namespace android
90 
91 #endif // CRYPTO_HAL_AIDL_H_
92