aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 __DRM_MANAGER_H__ |
| 18 | #define __DRM_MANAGER_H__ |
| 19 | |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 20 | #include <drm/drm_framework_common.h> |
| 21 | #include <media/stagefright/foundation/AHandler.h> |
| 22 | #include <media/stagefright/foundation/ALooper.h> |
| 23 | #include <media/stagefright/foundation/AMessage.h> |
| 24 | #include <sys/types.h> |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 25 | #include <utils/Errors.h> |
| 26 | #include <utils/threads.h> |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 27 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 28 | #include "IDrmEngine.h" |
| 29 | #include "PlugInManager.h" |
| 30 | #include "IDrmServiceListener.h" |
| 31 | |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 32 | #include <array> |
| 33 | #include <cstddef> |
| 34 | #include <map> |
| 35 | #include <set> |
| 36 | #include <string> |
| 37 | #include <utility> |
| 38 | #include <vector> |
| 39 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 40 | namespace android { |
| 41 | |
| 42 | class IDrmManager; |
| 43 | class DrmRegistrationInfo; |
| 44 | class DrmUnregistrationInfo; |
| 45 | class DrmRightsAcquisitionInfo; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 46 | class DrmConstraints; |
Takeshi Aimi | 3473846 | 2010-11-16 13:56:11 +0900 | [diff] [blame] | 47 | class DrmMetadata; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 48 | class DrmRights; |
| 49 | class DrmInfo; |
| 50 | class DrmInfoStatus; |
| 51 | class DrmConvertedStatus; |
| 52 | class DrmInfoRequest; |
| 53 | class DrmSupportInfo; |
| 54 | class ActionDescription; |
| 55 | |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 56 | enum DrmManagerMethodId { |
| 57 | GET_CONSTRAINTS, |
| 58 | GET_METADATA, |
| 59 | CAN_HANDLE, |
| 60 | PROCESS_DRM_INFO, |
| 61 | ACQUIRE_DRM_INFO, |
| 62 | SAVE_RIGHTS, |
| 63 | GET_ORIGINAL_MIME_TYPE, |
| 64 | GET_DRM_OBJECT_TYPE, |
| 65 | CHECK_RIGHTS_STATUS, |
| 66 | REMOVE_RIGHTS, |
| 67 | REMOVE_ALL_RIGHTS, |
| 68 | OPEN_CONVERT_SESSION, |
| 69 | OPEN_DECRYPT_SESSION, |
| 70 | NUM_METHODS, |
| 71 | }; |
| 72 | |
| 73 | struct DrmManagerMetrics { |
| 74 | std::string mPluginId; |
| 75 | std::string mDescription; |
| 76 | std::set<std::string> mMimeTypes; |
| 77 | std::array<int64_t, DrmManagerMethodId::NUM_METHODS> mMethodCounts{}; |
| 78 | uid_t mCallingUid; |
| 79 | }; |
| 80 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 81 | /** |
| 82 | * This is implementation class for DRM Manager. This class delegates the |
| 83 | * functionality to corresponding DRM Engine. |
| 84 | * |
| 85 | * The DrmManagerService class creates an instance of this class. |
| 86 | * |
| 87 | */ |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 88 | class DrmManager : public AHandler, public IDrmEngine::OnInfoListener { |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 89 | public: |
| 90 | DrmManager(); |
| 91 | virtual ~DrmManager(); |
| 92 | |
| 93 | public: |
Gloria Wang | 8f00151 | 2011-07-21 15:10:22 -0700 | [diff] [blame] | 94 | int addUniqueId(bool isNative); |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 95 | |
| 96 | void removeUniqueId(int uniqueId); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 97 | |
Takeshi Aimi | e943f84 | 2010-10-08 23:05:49 +0900 | [diff] [blame] | 98 | void addClient(int uniqueId); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 99 | |
Takeshi Aimi | e943f84 | 2010-10-08 23:05:49 +0900 | [diff] [blame] | 100 | void removeClient(int uniqueId); |
| 101 | |
| 102 | status_t loadPlugIns(); |
| 103 | |
| 104 | status_t loadPlugIns(const String8& plugInDirPath); |
| 105 | |
| 106 | status_t unloadPlugIns(); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 107 | |
| 108 | status_t setDrmServiceListener( |
| 109 | int uniqueId, const sp<IDrmServiceListener>& drmServiceListener); |
| 110 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 111 | DrmConstraints* getConstraints(int uniqueId, const String8* path, const int action); |
| 112 | |
Takeshi Aimi | 3473846 | 2010-11-16 13:56:11 +0900 | [diff] [blame] | 113 | DrmMetadata* getMetadata(int uniqueId, const String8* path); |
| 114 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 115 | bool canHandle(int uniqueId, const String8& path, const String8& mimeType); |
| 116 | |
| 117 | DrmInfoStatus* processDrmInfo(int uniqueId, const DrmInfo* drmInfo); |
| 118 | |
| 119 | DrmInfo* acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest); |
| 120 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 121 | status_t saveRights(int uniqueId, const DrmRights& drmRights, |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 122 | const String8& rightsPath, const String8& contentPath); |
| 123 | |
James Dong | bf5b3b2 | 2012-07-30 17:57:39 -0700 | [diff] [blame] | 124 | String8 getOriginalMimeType(int uniqueId, const String8& path, int fd); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 125 | |
| 126 | int getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType); |
| 127 | |
| 128 | int checkRightsStatus(int uniqueId, const String8& path, int action); |
| 129 | |
Jeff Tinker | 5d49bef | 2018-10-03 23:01:09 -0700 | [diff] [blame] | 130 | status_t consumeRights(int uniqueId, sp<DecryptHandle>& decryptHandle, int action, |
| 131 | bool reserve); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 132 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 133 | status_t setPlaybackStatus( |
Jeff Tinker | 5d49bef | 2018-10-03 23:01:09 -0700 | [diff] [blame] | 134 | int uniqueId, sp<DecryptHandle>& decryptHandle, int playbackStatus, int64_t position); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 135 | |
| 136 | bool validateAction( |
| 137 | int uniqueId, const String8& path, int action, const ActionDescription& description); |
| 138 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 139 | status_t removeRights(int uniqueId, const String8& path); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 140 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 141 | status_t removeAllRights(int uniqueId); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 142 | |
| 143 | int openConvertSession(int uniqueId, const String8& mimeType); |
| 144 | |
| 145 | DrmConvertedStatus* convertData(int uniqueId, int convertId, const DrmBuffer* inputData); |
| 146 | |
| 147 | DrmConvertedStatus* closeConvertSession(int uniqueId, int convertId); |
| 148 | |
| 149 | status_t getAllSupportInfo(int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray); |
| 150 | |
Jeff Tinker | 5d49bef | 2018-10-03 23:01:09 -0700 | [diff] [blame] | 151 | sp<DecryptHandle> openDecryptSession( |
James Dong | 9d2f386 | 2012-01-10 08:24:37 -0800 | [diff] [blame] | 152 | int uniqueId, int fd, off64_t offset, off64_t length, const char* mime); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 153 | |
Jeff Tinker | 5d49bef | 2018-10-03 23:01:09 -0700 | [diff] [blame] | 154 | sp<DecryptHandle> openDecryptSession(int uniqueId, const char* uri, const char* mime); |
Takeshi Aimi | e943f84 | 2010-10-08 23:05:49 +0900 | [diff] [blame] | 155 | |
Jeff Tinker | 5d49bef | 2018-10-03 23:01:09 -0700 | [diff] [blame] | 156 | sp<DecryptHandle> openDecryptSession(int uniqueId, const DrmBuffer& buf, |
Kei Takahashi | cba7b32 | 2012-01-18 17:10:19 +0900 | [diff] [blame] | 157 | const String8& mimeType); |
| 158 | |
Jeff Tinker | 5d49bef | 2018-10-03 23:01:09 -0700 | [diff] [blame] | 159 | status_t closeDecryptSession(int uniqueId, sp<DecryptHandle>& decryptHandle); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 160 | |
Jeff Tinker | 5d49bef | 2018-10-03 23:01:09 -0700 | [diff] [blame] | 161 | status_t initializeDecryptUnit(int uniqueId, sp<DecryptHandle>& decryptHandle, |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 162 | int decryptUnitId, const DrmBuffer* headerInfo); |
| 163 | |
Jeff Tinker | 5d49bef | 2018-10-03 23:01:09 -0700 | [diff] [blame] | 164 | status_t decrypt(int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId, |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 165 | const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 166 | |
Jeff Tinker | 5d49bef | 2018-10-03 23:01:09 -0700 | [diff] [blame] | 167 | status_t finalizeDecryptUnit(int uniqueId, sp<DecryptHandle>& decryptHandle, |
| 168 | int decryptUnitId); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 169 | |
Jeff Tinker | 5d49bef | 2018-10-03 23:01:09 -0700 | [diff] [blame] | 170 | ssize_t pread(int uniqueId, sp<DecryptHandle>& decryptHandle, |
Gloria Wang | a2cd44c | 2010-11-19 15:19:36 -0800 | [diff] [blame] | 171 | void* buffer, ssize_t numBytes, off64_t offset); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 172 | |
| 173 | void onInfo(const DrmInfoEvent& event); |
| 174 | |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 175 | void initMetricsLooper(); |
| 176 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 177 | private: |
| 178 | String8 getSupportedPlugInId(int uniqueId, const String8& path, const String8& mimeType); |
| 179 | |
| 180 | String8 getSupportedPlugInId(const String8& mimeType); |
| 181 | |
| 182 | String8 getSupportedPlugInIdFromPath(int uniqueId, const String8& path); |
| 183 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 184 | bool canHandle(int uniqueId, const String8& path); |
| 185 | |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 186 | void onMessageReceived(const sp<AMessage> &msg); |
| 187 | |
| 188 | int64_t getMetricsFlushPeriodUs(); |
| 189 | |
| 190 | void recordEngineMetrics(const char func[], |
Robert Shih | ec056ae | 2019-08-17 01:54:04 -0700 | [diff] [blame] | 191 | const String8& plugInId, const String8& mimeType = String8("")); |
| 192 | |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 193 | void flushEngineMetrics(); |
| 194 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 195 | private: |
Henrik B Andersson | 13f7fe7 | 2012-10-26 15:15:15 +0200 | [diff] [blame] | 196 | enum { |
| 197 | kMaxNumUniqueIds = 0x1000, |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 198 | kWhatFlushMetrics = 'metr', |
Henrik B Andersson | 13f7fe7 | 2012-10-26 15:15:15 +0200 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | bool mUniqueIdArray[kMaxNumUniqueIds]; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 202 | static const String8 EMPTY_STRING; |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 203 | static const std::map<const char*, size_t> kMethodIdMap; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 204 | |
| 205 | int mDecryptSessionId; |
| 206 | int mConvertId; |
| 207 | Mutex mLock; |
Gloria Wang | 0e0a5f9 | 2011-03-11 14:07:21 -0800 | [diff] [blame] | 208 | Mutex mListenerLock; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 209 | Mutex mDecryptLock; |
| 210 | Mutex mConvertLock; |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 211 | Mutex mMetricsLock; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 212 | TPlugInManager<IDrmEngine> mPlugInManager; |
| 213 | KeyedVector< DrmSupportInfo, String8 > mSupportInfoToPlugInIdMap; |
| 214 | KeyedVector< int, IDrmEngine*> mConvertSessionMap; |
| 215 | KeyedVector< int, sp<IDrmServiceListener> > mServiceListeners; |
| 216 | KeyedVector< int, IDrmEngine*> mDecryptSessionMap; |
Robert Shih | 7bcf792 | 2020-02-07 15:01:57 -0800 | [diff] [blame] | 217 | |
| 218 | std::map<std::pair<uid_t, std::string>, DrmManagerMetrics> mPluginMetrics; |
| 219 | sp<ALooper> mMetricsLooper; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | }; |
| 223 | |
| 224 | #endif /* __DRM_MANAGER_H__ */ |
| 225 | |