Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 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 CRYPTO_HAL_H_ |
| 18 | |
| 19 | #define CRYPTO_HAL_H_ |
| 20 | |
| 21 | #include <android/hardware/drm/1.0/ICryptoFactory.h> |
| 22 | #include <android/hardware/drm/1.0/ICryptoPlugin.h> |
Jeff Tinker | e307dc4 | 2018-02-11 19:53:54 +0000 | [diff] [blame] | 23 | #include <android/hardware/drm/1.1/ICryptoFactory.h> |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 24 | #include <android/hardware/drm/1.2/ICryptoPlugin.h> |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 25 | |
Jeff Tinker | 7d2c6e8 | 2018-02-16 16:14:59 -0800 | [diff] [blame] | 26 | #include <mediadrm/ICrypto.h> |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 27 | #include <utils/KeyedVector.h> |
| 28 | #include <utils/threads.h> |
| 29 | |
Jeff Tinker | e307dc4 | 2018-02-11 19:53:54 +0000 | [diff] [blame] | 30 | namespace drm = ::android::hardware::drm; |
| 31 | using drm::V1_0::ICryptoFactory; |
| 32 | using drm::V1_0::ICryptoPlugin; |
| 33 | using drm::V1_0::SharedBuffer; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 34 | |
Jeff Tinker | 3cb5316 | 2017-02-16 12:06:32 -0800 | [diff] [blame] | 35 | class IMemoryHeap; |
| 36 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 37 | namespace android { |
| 38 | |
Robert Shih | 9c930d0 | 2019-07-16 15:44:13 -0700 | [diff] [blame^] | 39 | struct CryptoHal : public ICrypto { |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 40 | CryptoHal(); |
| 41 | virtual ~CryptoHal(); |
| 42 | |
| 43 | virtual status_t initCheck() const; |
| 44 | |
| 45 | virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]); |
| 46 | |
| 47 | virtual status_t createPlugin( |
| 48 | const uint8_t uuid[16], const void *data, size_t size); |
| 49 | |
| 50 | virtual status_t destroyPlugin(); |
| 51 | |
| 52 | virtual bool requiresSecureDecoderComponent( |
| 53 | const char *mime) const; |
| 54 | |
| 55 | virtual void notifyResolution(uint32_t width, uint32_t height); |
| 56 | |
| 57 | virtual status_t setMediaDrmSession(const Vector<uint8_t> &sessionId); |
| 58 | |
| 59 | virtual ssize_t decrypt(const uint8_t key[16], const uint8_t iv[16], |
| 60 | CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern, |
Chong Zhang | 6dcab2b | 2017-03-28 14:18:27 -0700 | [diff] [blame] | 61 | const ICrypto::SourceBuffer &source, size_t offset, |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 62 | const CryptoPlugin::SubSample *subSamples, size_t numSubSamples, |
| 63 | const ICrypto::DestinationBuffer &destination, |
| 64 | AString *errorDetailMsg); |
| 65 | |
Chong Zhang | 6dcab2b | 2017-03-28 14:18:27 -0700 | [diff] [blame] | 66 | virtual int32_t setHeap(const sp<IMemoryHeap>& heap) { |
| 67 | return setHeapBase(heap); |
| 68 | } |
| 69 | virtual void unsetHeap(int32_t seqNum) { clearHeapBase(seqNum); } |
Chong Zhang | d07c927 | 2017-03-28 11:02:06 -0700 | [diff] [blame] | 70 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 71 | private: |
| 72 | mutable Mutex mLock; |
| 73 | |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 74 | const Vector<sp<ICryptoFactory>> mFactories; |
| 75 | sp<ICryptoPlugin> mPlugin; |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 76 | sp<drm::V1_2::ICryptoPlugin> mPluginV1_2; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * mInitCheck is: |
| 80 | * NO_INIT if a plugin hasn't been created yet |
| 81 | * ERROR_UNSUPPORTED if a plugin can't be created for the uuid |
| 82 | * OK after a plugin has been created and mPlugin is valid |
| 83 | */ |
| 84 | status_t mInitCheck; |
| 85 | |
Jeff Tinker | b1f8c80 | 2018-04-19 16:23:21 -0700 | [diff] [blame] | 86 | struct HeapBase { |
| 87 | HeapBase() : mBufferId(0), mSize(0) {} |
| 88 | HeapBase(uint32_t bufferId, size_t size) : |
| 89 | mBufferId(bufferId), mSize(size) {} |
| 90 | |
| 91 | uint32_t getBufferId() const {return mBufferId;} |
| 92 | size_t getSize() const {return mSize;} |
| 93 | |
| 94 | private: |
| 95 | uint32_t mBufferId; |
| 96 | size_t mSize; |
| 97 | }; |
| 98 | |
| 99 | KeyedVector<int32_t, HeapBase> mHeapBases; |
Chong Zhang | 6dcab2b | 2017-03-28 14:18:27 -0700 | [diff] [blame] | 100 | int32_t mHeapSeqNum; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 101 | |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 102 | Vector<sp<ICryptoFactory>> makeCryptoFactories(); |
| 103 | sp<ICryptoPlugin> makeCryptoPlugin(const sp<ICryptoFactory>& factory, |
| 104 | const uint8_t uuid[16], const void *initData, size_t size); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 105 | |
Chong Zhang | 6dcab2b | 2017-03-28 14:18:27 -0700 | [diff] [blame] | 106 | int32_t setHeapBase(const sp<IMemoryHeap>& heap); |
| 107 | void clearHeapBase(int32_t seqNum); |
Jeff Tinker | 3cb5316 | 2017-02-16 12:06:32 -0800 | [diff] [blame] | 108 | |
Chong Zhang | 6dcab2b | 2017-03-28 14:18:27 -0700 | [diff] [blame] | 109 | status_t toSharedBuffer(const sp<IMemory>& memory, int32_t seqNum, ::SharedBuffer* buffer); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 110 | |
| 111 | DISALLOW_EVIL_CONSTRUCTORS(CryptoHal); |
| 112 | }; |
| 113 | |
| 114 | } // namespace android |
| 115 | |
| 116 | #endif // CRYPTO_HAL_H_ |