blob: 3e3257f95687683ea523f66cc27bfa4595e87468 [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,
49
50 // Neither key nor iv are being used in this mode.
51 // Each subsample is encrypted w/ an iv of all zeroes.
52 kMode_AES_WV = 2, // FIX constant
53 };
54
55 struct SubSample {
Jeff Tinker03a05712014-07-01 14:54:45 -070056 uint32_t mNumBytesOfClearData;
57 uint32_t mNumBytesOfEncryptedData;
Andreas Hubercf0db312012-04-03 14:15:05 -070058 };
59
60 CryptoPlugin() {}
61 virtual ~CryptoPlugin() {}
62
63 // If this method returns false, a non-secure decoder will be used to
64 // decode the data after decryption. The decrypt API below will have
65 // to support insecure decryption of the data (secure = false) for
66 // media data of the given mime type.
67 virtual bool requiresSecureDecoderComponent(const char *mime) const = 0;
68
Jeff Tinkerc3959f52014-08-28 18:00:58 -070069 // To implement resolution constraints, the crypto plugin needs to know
70 // the resolution of the video being decrypted. The media player should
71 // call this method when the resolution is determined and any time it
72 // is subsequently changed.
Jeff Tinker9a498ef2015-03-31 15:47:24 -070073
74 virtual void notifyResolution(uint32_t /* width */, uint32_t /* height */) {}
Jeff Tinkerc3959f52014-08-28 18:00:58 -070075
Jeff Tinkerac7d8fe2015-04-10 04:10:10 -070076 // A MediaDrm session may be associated with a MediaCrypto session. The
77 // associated MediaDrm session is used to load decryption keys
78 // into the crypto/drm plugin. The keys are then referenced by key-id
79 // in the 'key' parameter to the decrypt() method.
80 // Should return NO_ERROR on success, ERROR_DRM_SESSION_NOT_OPENED if
81 // the session is not opened and a code from MediaErrors.h otherwise.
82 virtual status_t setMediaDrmSession(const Vector<uint8_t> & /*sessionId */) {
83 return ERROR_UNSUPPORTED;
84 }
85
Andreas Huber383190e2012-04-19 12:55:36 -070086 // If the error returned falls into the range
87 // ERROR_DRM_VENDOR_MIN..ERROR_DRM_VENDOR_MAX, errorDetailMsg should be
88 // filled in with an appropriate string.
89 // At the java level these special errors will then trigger a
90 // MediaCodec.CryptoException that gives clients access to both
91 // the error code and the errorDetailMsg.
Edwin Wonge0daeb32012-07-10 19:57:26 -070092 // Returns a non-negative result to indicate the number of bytes written
93 // to the dstPtr, or a negative result to indicate an error.
94 virtual ssize_t decrypt(
Andreas Hubercf0db312012-04-03 14:15:05 -070095 bool secure,
96 const uint8_t key[16],
97 const uint8_t iv[16],
98 Mode mode,
99 const void *srcPtr,
100 const SubSample *subSamples, size_t numSubSamples,
Andreas Huber383190e2012-04-19 12:55:36 -0700101 void *dstPtr,
102 AString *errorDetailMsg) = 0;
Andreas Hubercf0db312012-04-03 14:15:05 -0700103
104private:
105 CryptoPlugin(const CryptoPlugin &);
106 CryptoPlugin &operator=(const CryptoPlugin &);
107};
108
109} // namespace android
110
111extern "C" {
112 extern android::CryptoFactory *createCryptoFactory();
113}
114
115#endif // CRYPTO_API_H_