blob: ec0b8781dddaf40a9c8953136516e98bafcf99e5 [file] [log] [blame]
Robert Shih28c2ed32019-10-27 22:55:12 -07001/*
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
Robert Shih10fe9432019-11-09 08:26:49 -080020#include <android/hardware/drm/1.0/ICryptoFactory.h>
Robert Shih5ff3ad62019-11-09 08:26:49 -080021#include <android/hardware/drm/1.0/IDrmFactory.h>
Robert Shih9afca952021-02-12 01:36:06 -080022#include <android/hardware/drm/1.4/IDrmPlugin.h>
Robert Shih5944a0b2021-02-10 04:26:33 -080023#include <android/hardware/drm/1.4/types.h>
Robert Shih9afca952021-02-12 01:36:06 -080024#include <media/stagefright/MediaErrors.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070025#include <utils/Errors.h> // for status_t
Robert Shih0beba052021-02-14 00:47:00 -080026#include <utils/Log.h>
27#include <utils/String8.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070028#include <utils/StrongPointer.h>
Robert Shih0beba052021-02-14 00:47:00 -080029#include <utils/Vector.h>
Robert Shih8635cb12021-02-26 07:57:55 -080030#include <algorithm>
31#include <chrono>
32#include <cstddef>
33#include <cstdint>
Robert Shih0beba052021-02-14 00:47:00 -080034#include <ctime>
Robert Shih8635cb12021-02-26 07:57:55 -080035#include <deque>
Robert Shihbd790122021-03-01 20:45:31 -080036#include <endian.h>
Robert Shih8635cb12021-02-26 07:57:55 -080037#include <iterator>
38#include <mutex>
Robert Shihbd790122021-03-01 20:45:31 -080039#include <string>
Robert Shih10fe9432019-11-09 08:26:49 -080040#include <vector>
41
Robert Shih9afca952021-02-12 01:36:06 -080042
Robert Shih10fe9432019-11-09 08:26:49 -080043using namespace ::android::hardware::drm;
Robert Shih9afca952021-02-12 01:36:06 -080044using ::android::hardware::hidl_vec;
45using ::android::hardware::Return;
Robert Shih28c2ed32019-10-27 22:55:12 -070046
47namespace android {
48
49struct ICrypto;
50struct IDrm;
51
52namespace DrmUtils {
53
Robert Shih8635cb12021-02-26 07:57:55 -080054// Log APIs
55class 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
66extern LogBuffer gLogBuf;
67
68static inline int formatBuffer(char *buf, size_t size, const char *msg) {
69 return snprintf(buf, size, "%s", msg);
70}
71
72template <typename First, typename... Args>
73static 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
77template <typename... Args>
78void 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
Robert Shihbd790122021-03-01 20:45:31 -080093template <typename... Args>
94void 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
Robert Shih8635cb12021-02-26 07:57:55 -0800101#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
Robert Shih28c2ed32019-10-27 22:55:12 -0700109bool UseDrmService();
110
111sp<IDrm> MakeDrm(status_t *pstatus = nullptr);
112
113sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
114
Robert Shihd3f9ba72019-11-20 17:25:26 -0800115template<typename BA, typename PARCEL>
116void WriteByteArray(PARCEL &obj, const BA &vec) {
Robert Shih61e1c762019-10-31 21:26:58 -0700117 obj.writeInt32(vec.size());
118 if (vec.size()) {
119 obj.write(vec.data(), vec.size());
120 }
121}
122
Robert Shihd3f9ba72019-11-20 17:25:26 -0800123template<typename ET, typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700124void WriteEventToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800125 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700126 ET eventType,
127 const BA &sessionId,
128 const BA &data) {
129 WriteByteArray(obj, sessionId);
130 WriteByteArray(obj, data);
131 obj.writeInt32(eventType);
132}
133
Robert Shihd3f9ba72019-11-20 17:25:26 -0800134template<typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700135void WriteExpirationUpdateToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800136 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700137 const BA &sessionId,
138 int64_t expiryTimeInMS) {
139 WriteByteArray(obj, sessionId);
140 obj.writeInt64(expiryTimeInMS);
141}
142
Robert Shihd3f9ba72019-11-20 17:25:26 -0800143template<typename BA, typename KSL, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700144void WriteKeysChange(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800145 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700146 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
Robert Shihc0d1d0e2019-11-24 13:21:04 -0800158std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
Robert Shih5ff3ad62019-11-09 08:26:49 -0800159
160std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
161 const char *appPackageName);
162
Robert Shih10fe9432019-11-09 08:26:49 -0800163std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
164
165std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
166 const void *initData, size_t initDataSize);
167
Robert Shih5944a0b2021-02-10 04:26:33 -0800168status_t toStatusT_1_4(::V1_4::Status status);
169
170template<typename S>
171inline status_t toStatusT(S status) {
172 auto err = static_cast<::V1_4::Status>(status);
173 return toStatusT_1_4(err);
174}
175
176template<typename T>
177inline 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
Robert Shih9afca952021-02-12 01:36:06 -0800183template<typename T, typename U>
184status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) {
185 sp<T> plugin = T::castFrom(obj);
Robert Shih8635cb12021-02-26 07:57:55 -0800186 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);
Robert Shih9afca952021-02-12 01:36:06 -0800190 }
191
192 ::V1_4::Status err{};
Robert Shih8635cb12021-02-26 07:57:55 -0800193 std::vector<::V1_4::LogMessage> pluginLogs;
Robert Shih9afca952021-02-12 01:36:06 -0800194 ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&](
195 ::V1_4::Status status,
196 hidl_vec<::V1_4::LogMessage> hLogs) {
Robert Shihfaae26a2021-02-20 00:01:19 -0800197 if (::V1_4::Status::OK != status) {
Robert Shih9afca952021-02-12 01:36:06 -0800198 err = status;
199 return;
200 }
Robert Shih8635cb12021-02-26 07:57:55 -0800201 pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size());
Robert Shih9afca952021-02-12 01:36:06 -0800202 };
203
Robert Shih8635cb12021-02-26 07:57:55 -0800204 Return<void> hResult;
205 if (plugin != NULL) {
206 hResult = plugin->getLogMessages(cb);
Robert Shih9afca952021-02-12 01:36:06 -0800207 }
Robert Shih8635cb12021-02-26 07:57:55 -0800208 if (!hResult.isOk()) {
Robert Shihbd790122021-03-01 20:45:31 -0800209 LOG2BW("%s::getLogMessages remote call failed %s",
210 T::descriptor, hResult.description().c_str());
Robert Shih8635cb12021-02-26 07:57:55 -0800211 }
212
213 auto allLogs(gLogBuf.getLogs());
Robert Shih87aed812021-04-05 11:42:31 -0700214 LOG2BD("framework logs size %zu; plugin logs size %zu",
Robert Shihbd790122021-03-01 20:45:31 -0800215 allLogs.size(), pluginLogs.size());
Robert Shih8635cb12021-02-26 07:57:55 -0800216 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;
Robert Shih9afca952021-02-12 01:36:06 -0800224}
225
Robert Shih8635cb12021-02-26 07:57:55 -0800226std::string GetExceptionMessage(status_t err, const char *msg,
227 const Vector<::V1_4::LogMessage> &logs);
Robert Shih0beba052021-02-14 00:47:00 -0800228
229template<typename T>
230std::string GetExceptionMessage(status_t err, const char *msg, const sp<T> &iface) {
Robert Shih0beba052021-02-14 00:47:00 -0800231 Vector<::V1_4::LogMessage> logs;
Robert Shih8635cb12021-02-26 07:57:55 -0800232 iface->getLogMessages(logs);
233 return GetExceptionMessage(err, msg, logs);
Robert Shih0beba052021-02-14 00:47:00 -0800234}
235
Robert Shih28c2ed32019-10-27 22:55:12 -0700236} // namespace DrmUtils
Robert Shih28c2ed32019-10-27 22:55:12 -0700237} // namespace android
Robert Shih28c2ed32019-10-27 22:55:12 -0700238#endif // ANDROID_DRMUTILS_H