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 
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>
25 #include <utils/Errors.h>
26 #include <utils/threads.h>
27 
28 #include "IDrmEngine.h"
29 #include "PlugInManager.h"
30 #include "IDrmServiceListener.h"
31 
32 #include <array>
33 #include <cstddef>
34 #include <map>
35 #include <set>
36 #include <string>
37 #include <utility>
38 #include <vector>
39 
40 namespace android {
41 
42 class IDrmManager;
43 class DrmRegistrationInfo;
44 class DrmUnregistrationInfo;
45 class DrmRightsAcquisitionInfo;
46 class DrmConstraints;
47 class DrmMetadata;
48 class DrmRights;
49 class DrmInfo;
50 class DrmInfoStatus;
51 class DrmConvertedStatus;
52 class DrmInfoRequest;
53 class DrmSupportInfo;
54 class ActionDescription;
55 
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 
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  */
88 class DrmManager : public AHandler, public IDrmEngine::OnInfoListener {
89 public:
90     DrmManager();
91     virtual ~DrmManager();
92 
93 public:
94     int addUniqueId(bool isNative);
95 
96     void removeUniqueId(int uniqueId);
97 
98     void addClient(int uniqueId);
99 
100     void removeClient(int uniqueId);
101 
102     status_t loadPlugIns();
103 
104     status_t loadPlugIns(const String8& plugInDirPath);
105 
106     status_t unloadPlugIns();
107 
108     status_t setDrmServiceListener(
109             int uniqueId, const sp<IDrmServiceListener>& drmServiceListener);
110 
111     DrmConstraints* getConstraints(int uniqueId, const String8* path, const int action);
112 
113     DrmMetadata* getMetadata(int uniqueId, const String8* path);
114 
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 
121     status_t saveRights(int uniqueId, const DrmRights& drmRights,
122             const String8& rightsPath, const String8& contentPath);
123 
124     String8 getOriginalMimeType(int uniqueId, const String8& path, int fd);
125 
126     int getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType);
127 
128     int checkRightsStatus(int uniqueId, const String8& path, int action);
129 
130     status_t consumeRights(int uniqueId, sp<DecryptHandle>& decryptHandle, int action,
131             bool reserve);
132 
133     status_t setPlaybackStatus(
134             int uniqueId, sp<DecryptHandle>& decryptHandle, int playbackStatus, int64_t position);
135 
136     bool validateAction(
137             int uniqueId, const String8& path, int action, const ActionDescription& description);
138 
139     status_t removeRights(int uniqueId, const String8& path);
140 
141     status_t removeAllRights(int uniqueId);
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 
151     sp<DecryptHandle> openDecryptSession(
152             int uniqueId, int fd, off64_t offset, off64_t length, const char* mime);
153 
154     sp<DecryptHandle> openDecryptSession(int uniqueId, const char* uri, const char* mime);
155 
156     sp<DecryptHandle> openDecryptSession(int uniqueId, const DrmBuffer& buf,
157             const String8& mimeType);
158 
159     status_t closeDecryptSession(int uniqueId, sp<DecryptHandle>& decryptHandle);
160 
161     status_t initializeDecryptUnit(int uniqueId, sp<DecryptHandle>& decryptHandle,
162             int decryptUnitId, const DrmBuffer* headerInfo);
163 
164     status_t decrypt(int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId,
165             const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV);
166 
167     status_t finalizeDecryptUnit(int uniqueId, sp<DecryptHandle>& decryptHandle,
168             int decryptUnitId);
169 
170     ssize_t pread(int uniqueId, sp<DecryptHandle>& decryptHandle,
171             void* buffer, ssize_t numBytes, off64_t offset);
172 
173     void onInfo(const DrmInfoEvent& event);
174 
175     void initMetricsLooper();
176 
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 
184     bool canHandle(int uniqueId, const String8& path);
185 
186     void onMessageReceived(const sp<AMessage> &msg);
187 
188     int64_t getMetricsFlushPeriodUs();
189 
190     void recordEngineMetrics(const char func[],
191             const String8& plugInId, const String8& mimeType = String8(""));
192 
193     void flushEngineMetrics();
194 
195 private:
196     enum {
197         kMaxNumUniqueIds = 0x1000,
198         kWhatFlushMetrics = 'metr',
199     };
200 
201     bool mUniqueIdArray[kMaxNumUniqueIds];
202     static const String8 EMPTY_STRING;
203     static const std::map<const char*, size_t> kMethodIdMap;
204 
205     int mDecryptSessionId;
206     int mConvertId;
207     Mutex mLock;
208     Mutex mListenerLock;
209     Mutex mDecryptLock;
210     Mutex mConvertLock;
211     Mutex mMetricsLock;
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;
217 
218     std::map<std::pair<uid_t, std::string>, DrmManagerMetrics> mPluginMetrics;
219     sp<ALooper> mMetricsLooper;
220 };
221 
222 };
223 
224 #endif /* __DRM_MANAGER_H__ */
225 
226