1 /*
2 * Copyright (C) 2019 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 ANDROID_DRMUTILS_H
18 #define ANDROID_DRMUTILS_H
19
20 #include <android/hardware/drm/1.0/ICryptoFactory.h>
21 #include <android/hardware/drm/1.0/IDrmFactory.h>
22 #include <android/hardware/drm/1.4/IDrmPlugin.h>
23 #include <android/hardware/drm/1.4/types.h>
24 #include <media/stagefright/MediaErrors.h>
25 #include <utils/Errors.h> // for status_t
26 #include <utils/Log.h>
27 #include <utils/String8.h>
28 #include <utils/StrongPointer.h>
29 #include <utils/Vector.h>
30 #include <algorithm>
31 #include <chrono>
32 #include <cstddef>
33 #include <cstdint>
34 #include <ctime>
35 #include <deque>
36 #include <endian.h>
37 #include <iterator>
38 #include <mutex>
39 #include <string>
40 #include <vector>
41
42
43 using namespace ::android::hardware::drm;
44 using ::android::hardware::hidl_vec;
45 using ::android::hardware::Return;
46
47 namespace android {
48
49 struct ICrypto;
50 struct IDrm;
51
52 namespace DrmUtils {
53
54 // Log APIs
55 class LogBuffer {
56 public:
57 static const int MAX_CAPACITY = 100;
58 void addLog(const ::V1_4::LogMessage &log);
59 Vector<::V1_4::LogMessage> getLogs();
60
61 private:
62 std::deque<::V1_4::LogMessage> mBuffer;
63 std::mutex mMutex;
64 };
65
66 extern LogBuffer gLogBuf;
67
formatBuffer(char * buf,size_t size,const char * msg)68 static inline int formatBuffer(char *buf, size_t size, const char *msg) {
69 return snprintf(buf, size, "%s", msg);
70 }
71
72 template <typename First, typename... Args>
formatBuffer(char * buf,size_t size,const char * fmt,First first,Args...args)73 static inline int formatBuffer(char *buf, size_t size, const char *fmt, First first, Args... args) {
74 return snprintf(buf, size, fmt, first, args...);
75 }
76
77 template <typename... Args>
LogToBuffer(android_LogPriority level,const char * fmt,Args...args)78 void LogToBuffer(android_LogPriority level, const char *fmt, Args... args) {
79 const int LOG_BUF_SIZE = 256;
80 char buf[LOG_BUF_SIZE];
81 int len = formatBuffer(buf, LOG_BUF_SIZE, fmt, args...);
82 if (len <= 0) {
83 return;
84 }
85 __android_log_write(level, LOG_TAG, buf);
86 if (level >= ANDROID_LOG_INFO) {
87 int64_t epochTimeMs =
88 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
89 gLogBuf.addLog({epochTimeMs, static_cast<::V1_4::LogPriority>(level), buf});
90 }
91 }
92
93 template <typename... Args>
LogToBuffer(android_LogPriority level,const uint8_t uuid[16],const char * fmt,Args...args)94 void LogToBuffer(android_LogPriority level, const uint8_t uuid[16], const char *fmt, Args... args) {
95 const uint64_t* uuid2 = reinterpret_cast<const uint64_t*>(uuid);
96 std::string uuidFmt("uuid=[%lx %lx] ");
97 uuidFmt += fmt;
98 LogToBuffer(level, uuidFmt.c_str(), htobe64(uuid2[0]), htobe64(uuid2[1]), args...);
99 }
100
101 #ifndef LOG2BE
102 #define LOG2BE(...) LogToBuffer(ANDROID_LOG_ERROR, __VA_ARGS__)
103 #define LOG2BW(...) LogToBuffer(ANDROID_LOG_WARN, __VA_ARGS__)
104 #define LOG2BI(...) LogToBuffer(ANDROID_LOG_INFO, __VA_ARGS__)
105 #define LOG2BD(...) LogToBuffer(ANDROID_LOG_DEBUG, __VA_ARGS__)
106 #define LOG2BV(...) LogToBuffer(ANDROID_LOG_VERBOSE, __VA_ARGS__)
107 #endif
108
109 bool UseDrmService();
110
111 sp<IDrm> MakeDrm(status_t *pstatus = nullptr);
112
113 sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
114
115 template<typename BA, typename PARCEL>
WriteByteArray(PARCEL & obj,const BA & vec)116 void WriteByteArray(PARCEL &obj, const BA &vec) {
117 obj.writeInt32(vec.size());
118 if (vec.size()) {
119 obj.write(vec.data(), vec.size());
120 }
121 }
122
123 template<typename ET, typename BA, typename PARCEL>
WriteEventToParcel(PARCEL & obj,ET eventType,const BA & sessionId,const BA & data)124 void WriteEventToParcel(
125 PARCEL &obj,
126 ET eventType,
127 const BA &sessionId,
128 const BA &data) {
129 WriteByteArray(obj, sessionId);
130 WriteByteArray(obj, data);
131 obj.writeInt32(eventType);
132 }
133
134 template<typename BA, typename PARCEL>
WriteExpirationUpdateToParcel(PARCEL & obj,const BA & sessionId,int64_t expiryTimeInMS)135 void WriteExpirationUpdateToParcel(
136 PARCEL &obj,
137 const BA &sessionId,
138 int64_t expiryTimeInMS) {
139 WriteByteArray(obj, sessionId);
140 obj.writeInt64(expiryTimeInMS);
141 }
142
143 template<typename BA, typename KSL, typename PARCEL>
WriteKeysChange(PARCEL & obj,const BA & sessionId,const KSL & keyStatusList,bool hasNewUsableKey)144 void WriteKeysChange(
145 PARCEL &obj,
146 const BA &sessionId,
147 const KSL &keyStatusList,
148 bool hasNewUsableKey) {
149 WriteByteArray(obj, sessionId);
150 obj.writeInt32(keyStatusList.size());
151 for (const auto &keyStatus : keyStatusList) {
152 WriteByteArray(obj, keyStatus.keyId);
153 obj.writeInt32(keyStatus.type);
154 }
155 obj.writeInt32(hasNewUsableKey);
156 }
157
158 std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
159
160 std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
161 const char *appPackageName);
162
163 std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
164
165 std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
166 const void *initData, size_t initDataSize);
167
168 status_t toStatusT_1_4(::V1_4::Status status);
169
170 template<typename S>
toStatusT(S status)171 inline status_t toStatusT(S status) {
172 auto err = static_cast<::V1_4::Status>(status);
173 return toStatusT_1_4(err);
174 }
175
176 template<typename T>
toStatusT(const android::hardware::Return<T> & status)177 inline status_t toStatusT(const android::hardware::Return<T> &status) {
178 auto t = static_cast<T>(status);
179 auto err = static_cast<::V1_4::Status>(t);
180 return toStatusT_1_4(err);
181 }
182
183 template<typename T, typename U>
GetLogMessages(const sp<U> & obj,Vector<::V1_4::LogMessage> & logs)184 status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) {
185 sp<T> plugin = T::castFrom(obj);
186 if (obj == NULL) {
187 LOG2BW("%s obj is null", U::descriptor);
188 } else if (plugin == NULL) {
189 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
190 }
191
192 ::V1_4::Status err{};
193 std::vector<::V1_4::LogMessage> pluginLogs;
194 ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&](
195 ::V1_4::Status status,
196 hidl_vec<::V1_4::LogMessage> hLogs) {
197 if (::V1_4::Status::OK != status) {
198 err = status;
199 return;
200 }
201 pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size());
202 };
203
204 Return<void> hResult;
205 if (plugin != NULL) {
206 hResult = plugin->getLogMessages(cb);
207 }
208 if (!hResult.isOk()) {
209 LOG2BW("%s::getLogMessages remote call failed %s",
210 T::descriptor, hResult.description().c_str());
211 }
212
213 auto allLogs(gLogBuf.getLogs());
214 LOG2BD("framework logs size %zu; plugin logs size %zu",
215 allLogs.size(), pluginLogs.size());
216 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
217 std::sort(allLogs.begin(), allLogs.end(),
218 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
219 return a.timeMs < b.timeMs;
220 });
221
222 logs.appendVector(allLogs);
223 return OK;
224 }
225
226 std::string GetExceptionMessage(status_t err, const char *msg,
227 const Vector<::V1_4::LogMessage> &logs);
228
229 template<typename T>
GetExceptionMessage(status_t err,const char * msg,const sp<T> & iface)230 std::string GetExceptionMessage(status_t err, const char *msg, const sp<T> &iface) {
231 Vector<::V1_4::LogMessage> logs;
232 iface->getLogMessages(logs);
233 return GetExceptionMessage(err, msg, logs);
234 }
235
236 } // namespace DrmUtils
237 } // namespace android
238 #endif // ANDROID_DRMUTILS_H
239