Andreas Huber | cf0db31 | 2012-04-03 14:15:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Jeff Tinker | ac7d8fe | 2015-04-10 04:10:10 -0700 | [diff] [blame] | 17 | #include <media/stagefright/MediaErrors.h> |
Andreas Huber | cf0db31 | 2012-04-03 14:15:05 -0700 | [diff] [blame] | 18 | #include <utils/Errors.h> |
Jeff Tinker | ac7d8fe | 2015-04-10 04:10:10 -0700 | [diff] [blame] | 19 | #include <utils/Vector.h> |
Andreas Huber | cf0db31 | 2012-04-03 14:15:05 -0700 | [diff] [blame] | 20 | |
| 21 | #ifndef CRYPTO_API_H_ |
| 22 | |
| 23 | #define CRYPTO_API_H_ |
| 24 | |
| 25 | namespace android { |
| 26 | |
Andreas Huber | 383190e | 2012-04-19 12:55:36 -0700 | [diff] [blame] | 27 | struct AString; |
Andreas Huber | cf0db31 | 2012-04-03 14:15:05 -0700 | [diff] [blame] | 28 | struct CryptoPlugin; |
| 29 | |
| 30 | struct CryptoFactory { |
| 31 | CryptoFactory() {} |
| 32 | virtual ~CryptoFactory() {} |
| 33 | |
| 34 | virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) const = 0; |
| 35 | |
| 36 | virtual status_t createPlugin( |
| 37 | const uint8_t uuid[16], const void *data, size_t size, |
| 38 | CryptoPlugin **plugin) = 0; |
| 39 | |
| 40 | private: |
| 41 | CryptoFactory(const CryptoFactory &); |
| 42 | CryptoFactory &operator=(const CryptoFactory &); |
| 43 | }; |
| 44 | |
| 45 | struct CryptoPlugin { |
| 46 | enum Mode { |
| 47 | kMode_Unencrypted = 0, |
| 48 | kMode_AES_CTR = 1, |
Jeff Tinker | e47077f | 2015-12-18 11:52:06 -0800 | [diff] [blame] | 49 | kMode_AES_WV = 2, |
| 50 | kMode_AES_CBC = 3, |
Andreas Huber | cf0db31 | 2012-04-03 14:15:05 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | struct SubSample { |
Jeff Tinker | 03a0571 | 2014-07-01 14:54:45 -0700 | [diff] [blame] | 54 | uint32_t mNumBytesOfClearData; |
| 55 | uint32_t mNumBytesOfEncryptedData; |
Andreas Huber | cf0db31 | 2012-04-03 14:15:05 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Jeff Tinker | e47077f | 2015-12-18 11:52:06 -0800 | [diff] [blame] | 58 | struct Pattern { |
| 59 | // Number of blocks to be encrypted in the pattern. If zero, pattern |
| 60 | // encryption is inoperative. |
| 61 | uint32_t mEncryptBlocks; |
| 62 | |
| 63 | // Number of blocks to be skipped (left clear) in the pattern. If zero, |
| 64 | // pattern encryption is inoperative. |
| 65 | uint32_t mSkipBlocks; |
| 66 | }; |
| 67 | |
Andreas Huber | cf0db31 | 2012-04-03 14:15:05 -0700 | [diff] [blame] | 68 | CryptoPlugin() {} |
| 69 | virtual ~CryptoPlugin() {} |
| 70 | |
| 71 | // If this method returns false, a non-secure decoder will be used to |
| 72 | // decode the data after decryption. The decrypt API below will have |
| 73 | // to support insecure decryption of the data (secure = false) for |
| 74 | // media data of the given mime type. |
| 75 | virtual bool requiresSecureDecoderComponent(const char *mime) const = 0; |
| 76 | |
Jeff Tinker | c3959f5 | 2014-08-28 18:00:58 -0700 | [diff] [blame] | 77 | // To implement resolution constraints, the crypto plugin needs to know |
| 78 | // the resolution of the video being decrypted. The media player should |
| 79 | // call this method when the resolution is determined and any time it |
| 80 | // is subsequently changed. |
Jeff Tinker | 9a498ef | 2015-03-31 15:47:24 -0700 | [diff] [blame] | 81 | |
| 82 | virtual void notifyResolution(uint32_t /* width */, uint32_t /* height */) {} |
Jeff Tinker | c3959f5 | 2014-08-28 18:00:58 -0700 | [diff] [blame] | 83 | |
Jeff Tinker | ac7d8fe | 2015-04-10 04:10:10 -0700 | [diff] [blame] | 84 | // A MediaDrm session may be associated with a MediaCrypto session. The |
| 85 | // associated MediaDrm session is used to load decryption keys |
| 86 | // into the crypto/drm plugin. The keys are then referenced by key-id |
| 87 | // in the 'key' parameter to the decrypt() method. |
| 88 | // Should return NO_ERROR on success, ERROR_DRM_SESSION_NOT_OPENED if |
| 89 | // the session is not opened and a code from MediaErrors.h otherwise. |
| 90 | virtual status_t setMediaDrmSession(const Vector<uint8_t> & /*sessionId */) { |
| 91 | return ERROR_UNSUPPORTED; |
| 92 | } |
| 93 | |
Andreas Huber | 383190e | 2012-04-19 12:55:36 -0700 | [diff] [blame] | 94 | // If the error returned falls into the range |
| 95 | // ERROR_DRM_VENDOR_MIN..ERROR_DRM_VENDOR_MAX, errorDetailMsg should be |
| 96 | // filled in with an appropriate string. |
| 97 | // At the java level these special errors will then trigger a |
| 98 | // MediaCodec.CryptoException that gives clients access to both |
| 99 | // the error code and the errorDetailMsg. |
Edwin Wong | e0daeb3 | 2012-07-10 19:57:26 -0700 | [diff] [blame] | 100 | // Returns a non-negative result to indicate the number of bytes written |
| 101 | // to the dstPtr, or a negative result to indicate an error. |
| 102 | virtual ssize_t decrypt( |
Andreas Huber | cf0db31 | 2012-04-03 14:15:05 -0700 | [diff] [blame] | 103 | bool secure, |
| 104 | const uint8_t key[16], |
| 105 | const uint8_t iv[16], |
| 106 | Mode mode, |
Jeff Tinker | e47077f | 2015-12-18 11:52:06 -0800 | [diff] [blame] | 107 | const Pattern &pattern, |
Andreas Huber | cf0db31 | 2012-04-03 14:15:05 -0700 | [diff] [blame] | 108 | const void *srcPtr, |
| 109 | const SubSample *subSamples, size_t numSubSamples, |
Andreas Huber | 383190e | 2012-04-19 12:55:36 -0700 | [diff] [blame] | 110 | void *dstPtr, |
| 111 | AString *errorDetailMsg) = 0; |
Andreas Huber | cf0db31 | 2012-04-03 14:15:05 -0700 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | CryptoPlugin(const CryptoPlugin &); |
| 115 | CryptoPlugin &operator=(const CryptoPlugin &); |
| 116 | }; |
| 117 | |
| 118 | } // namespace android |
| 119 | |
| 120 | extern "C" { |
| 121 | extern android::CryptoFactory *createCryptoFactory(); |
| 122 | } |
| 123 | |
| 124 | #endif // CRYPTO_API_H_ |