blob: 0e86aacc6f09f3d20d548b2888f2b6d4e9ceb982 [file] [log] [blame]
Andreas Hubercf0db312012-04-03 14:15:05 -07001/*
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 Tinkerac7d8fe2015-04-10 04:10:10 -070017#include <media/stagefright/MediaErrors.h>
Andreas Hubercf0db312012-04-03 14:15:05 -070018#include <utils/Errors.h>
Jeff Tinkerac7d8fe2015-04-10 04:10:10 -070019#include <utils/Vector.h>
Andreas Hubercf0db312012-04-03 14:15:05 -070020
21#ifndef CRYPTO_API_H_
22
23#define CRYPTO_API_H_
24
25namespace android {
26
Andreas Huber383190e2012-04-19 12:55:36 -070027struct AString;
Andreas Hubercf0db312012-04-03 14:15:05 -070028struct CryptoPlugin;
29
30struct 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
40private:
41 CryptoFactory(const CryptoFactory &);
42 CryptoFactory &operator=(const CryptoFactory &);
43};
44
45struct CryptoPlugin {
46 enum Mode {
47 kMode_Unencrypted = 0,
48 kMode_AES_CTR = 1,
Jeff Tinkere47077f2015-12-18 11:52:06 -080049 kMode_AES_WV = 2,
50 kMode_AES_CBC = 3,
Andreas Hubercf0db312012-04-03 14:15:05 -070051 };
52
53 struct SubSample {
Jeff Tinker03a05712014-07-01 14:54:45 -070054 uint32_t mNumBytesOfClearData;
55 uint32_t mNumBytesOfEncryptedData;
Andreas Hubercf0db312012-04-03 14:15:05 -070056 };
57
Jeff Tinkere47077f2015-12-18 11:52:06 -080058 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 Hubercf0db312012-04-03 14:15:05 -070068 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 Tinkerc3959f52014-08-28 18:00:58 -070077 // 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 Tinker9a498ef2015-03-31 15:47:24 -070081
82 virtual void notifyResolution(uint32_t /* width */, uint32_t /* height */) {}
Jeff Tinkerc3959f52014-08-28 18:00:58 -070083
Jeff Tinkerac7d8fe2015-04-10 04:10:10 -070084 // 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 Huber383190e2012-04-19 12:55:36 -070094 // 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 Wonge0daeb32012-07-10 19:57:26 -0700100 // 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 Hubercf0db312012-04-03 14:15:05 -0700103 bool secure,
104 const uint8_t key[16],
105 const uint8_t iv[16],
106 Mode mode,
Jeff Tinkere47077f2015-12-18 11:52:06 -0800107 const Pattern &pattern,
Andreas Hubercf0db312012-04-03 14:15:05 -0700108 const void *srcPtr,
109 const SubSample *subSamples, size_t numSubSamples,
Andreas Huber383190e2012-04-19 12:55:36 -0700110 void *dstPtr,
111 AString *errorDetailMsg) = 0;
Andreas Hubercf0db312012-04-03 14:15:05 -0700112
113private:
114 CryptoPlugin(const CryptoPlugin &);
115 CryptoPlugin &operator=(const CryptoPlugin &);
116};
117
118} // namespace android
119
120extern "C" {
121 extern android::CryptoFactory *createCryptoFactory();
122}
123
124#endif // CRYPTO_API_H_