blob: 3028b783fc6ad70743f8d6af788cd4ddbf901a5c [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>
Sohail Nagaraj0cc3da82022-11-30 10:22:37 +053025#include <mediadrm/DrmStatus.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070026#include <utils/Errors.h> // for status_t
Robert Shih0beba052021-02-14 00:47:00 -080027#include <utils/Log.h>
28#include <utils/String8.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070029#include <utils/StrongPointer.h>
Robert Shih0beba052021-02-14 00:47:00 -080030#include <utils/Vector.h>
Robert Shih8635cb12021-02-26 07:57:55 -080031#include <algorithm>
32#include <chrono>
33#include <cstddef>
34#include <cstdint>
Robert Shih2616a632021-09-13 17:45:38 -070035#include <cstring>
Robert Shih0beba052021-02-14 00:47:00 -080036#include <ctime>
Robert Shih8635cb12021-02-26 07:57:55 -080037#include <deque>
Robert Shihbd790122021-03-01 20:45:31 -080038#include <endian.h>
Robert Shih8635cb12021-02-26 07:57:55 -080039#include <iterator>
40#include <mutex>
Robert Shihbd790122021-03-01 20:45:31 -080041#include <string>
Robert Shih10fe9432019-11-09 08:26:49 -080042#include <vector>
Kyle Zhang6605add2022-01-13 17:51:23 +000043#include <aidl/android/hardware/drm/LogMessage.h>
44#include <aidl/android/hardware/drm/Status.h>
Kyle Zhang994023a2022-02-09 05:32:12 +000045#include <aidl/android/hardware/drm/IDrmFactory.h>
Robert Shih9afca952021-02-12 01:36:06 -080046
Robert Shih10fe9432019-11-09 08:26:49 -080047using namespace ::android::hardware::drm;
Robert Shih9afca952021-02-12 01:36:06 -080048using ::android::hardware::hidl_vec;
49using ::android::hardware::Return;
Robert Shih28c2ed32019-10-27 22:55:12 -070050
Kyle Zhang6605add2022-01-13 17:51:23 +000051using ::aidl::android::hardware::drm::LogPriority;
52using ::aidl::android::hardware::drm::LogMessage;
Kyle Zhang994023a2022-02-09 05:32:12 +000053using ::aidl::android::hardware::drm::Uuid;
Kyle Zhang6605add2022-01-13 17:51:23 +000054using StatusAidl = ::aidl::android::hardware::drm::Status;
Kyle Zhang994023a2022-02-09 05:32:12 +000055using IDrmFactoryAidl = ::aidl::android::hardware::drm::IDrmFactory;
Kyle Zhang6605add2022-01-13 17:51:23 +000056
Robert Shih28c2ed32019-10-27 22:55:12 -070057namespace android {
58
59struct ICrypto;
60struct IDrm;
61
62namespace DrmUtils {
63
Robert Shih8635cb12021-02-26 07:57:55 -080064// Log APIs
65class LogBuffer {
66 public:
67 static const int MAX_CAPACITY = 100;
68 void addLog(const ::V1_4::LogMessage &log);
69 Vector<::V1_4::LogMessage> getLogs();
70
71 private:
72 std::deque<::V1_4::LogMessage> mBuffer;
73 std::mutex mMutex;
74};
75
76extern LogBuffer gLogBuf;
77
78static inline int formatBuffer(char *buf, size_t size, const char *msg) {
79 return snprintf(buf, size, "%s", msg);
80}
81
82template <typename First, typename... Args>
83static inline int formatBuffer(char *buf, size_t size, const char *fmt, First first, Args... args) {
84 return snprintf(buf, size, fmt, first, args...);
85}
86
87template <typename... Args>
88void LogToBuffer(android_LogPriority level, const char *fmt, Args... args) {
89 const int LOG_BUF_SIZE = 256;
90 char buf[LOG_BUF_SIZE];
91 int len = formatBuffer(buf, LOG_BUF_SIZE, fmt, args...);
92 if (len <= 0) {
93 return;
94 }
95 __android_log_write(level, LOG_TAG, buf);
96 if (level >= ANDROID_LOG_INFO) {
97 int64_t epochTimeMs =
98 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
99 gLogBuf.addLog({epochTimeMs, static_cast<::V1_4::LogPriority>(level), buf});
100 }
101}
102
Robert Shihbd790122021-03-01 20:45:31 -0800103template <typename... Args>
104void LogToBuffer(android_LogPriority level, const uint8_t uuid[16], const char *fmt, Args... args) {
Robert Shih2616a632021-09-13 17:45:38 -0700105 uint64_t uuid2[2] = {};
106 std::memcpy(uuid2, uuid, sizeof(uuid2));
Robert Shihbd790122021-03-01 20:45:31 -0800107 std::string uuidFmt("uuid=[%lx %lx] ");
108 uuidFmt += fmt;
109 LogToBuffer(level, uuidFmt.c_str(), htobe64(uuid2[0]), htobe64(uuid2[1]), args...);
110}
111
Robert Shih8635cb12021-02-26 07:57:55 -0800112#ifndef LOG2BE
113#define LOG2BE(...) LogToBuffer(ANDROID_LOG_ERROR, __VA_ARGS__)
114#define LOG2BW(...) LogToBuffer(ANDROID_LOG_WARN, __VA_ARGS__)
115#define LOG2BI(...) LogToBuffer(ANDROID_LOG_INFO, __VA_ARGS__)
116#define LOG2BD(...) LogToBuffer(ANDROID_LOG_DEBUG, __VA_ARGS__)
117#define LOG2BV(...) LogToBuffer(ANDROID_LOG_VERBOSE, __VA_ARGS__)
118#endif
119
Robert Shih28c2ed32019-10-27 22:55:12 -0700120bool UseDrmService();
121
122sp<IDrm> MakeDrm(status_t *pstatus = nullptr);
123
124sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
125
Robert Shihd3f9ba72019-11-20 17:25:26 -0800126template<typename BA, typename PARCEL>
127void WriteByteArray(PARCEL &obj, const BA &vec) {
Robert Shih61e1c762019-10-31 21:26:58 -0700128 obj.writeInt32(vec.size());
129 if (vec.size()) {
130 obj.write(vec.data(), vec.size());
131 }
132}
133
Robert Shihd3f9ba72019-11-20 17:25:26 -0800134template<typename ET, typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700135void WriteEventToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800136 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700137 ET eventType,
138 const BA &sessionId,
139 const BA &data) {
140 WriteByteArray(obj, sessionId);
141 WriteByteArray(obj, data);
142 obj.writeInt32(eventType);
143}
144
Robert Shihd3f9ba72019-11-20 17:25:26 -0800145template<typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700146void WriteExpirationUpdateToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800147 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700148 const BA &sessionId,
149 int64_t expiryTimeInMS) {
150 WriteByteArray(obj, sessionId);
151 obj.writeInt64(expiryTimeInMS);
152}
153
Robert Shihd3f9ba72019-11-20 17:25:26 -0800154template<typename BA, typename KSL, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700155void WriteKeysChange(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800156 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700157 const BA &sessionId,
158 const KSL &keyStatusList,
159 bool hasNewUsableKey) {
160 WriteByteArray(obj, sessionId);
161 obj.writeInt32(keyStatusList.size());
162 for (const auto &keyStatus : keyStatusList) {
163 WriteByteArray(obj, keyStatus.keyId);
164 obj.writeInt32(keyStatus.type);
165 }
166 obj.writeInt32(hasNewUsableKey);
167}
168
Kyle Zhang994023a2022-02-09 05:32:12 +0000169inline Uuid toAidlUuid(const uint8_t uuid[16]) {
170 Uuid uuidAidl;
171 for (int i = 0; i < 16; ++i) uuidAidl.uuid[i] = uuid[i];
172 return uuidAidl;
173}
174
175std::vector<std::shared_ptr<IDrmFactoryAidl>> makeDrmFactoriesAidl();
176
Robert Shihc0d1d0e2019-11-24 13:21:04 -0800177std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
Robert Shih5ff3ad62019-11-09 08:26:49 -0800178
179std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
180 const char *appPackageName);
181
Robert Shih10fe9432019-11-09 08:26:49 -0800182std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
183
184std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
185 const void *initData, size_t initDataSize);
186
Robert Shih5944a0b2021-02-10 04:26:33 -0800187status_t toStatusT_1_4(::V1_4::Status status);
188
189template<typename S>
190inline status_t toStatusT(S status) {
191 auto err = static_cast<::V1_4::Status>(status);
192 return toStatusT_1_4(err);
193}
194
195template<typename T>
196inline status_t toStatusT(const android::hardware::Return<T> &status) {
197 auto t = static_cast<T>(status);
198 auto err = static_cast<::V1_4::Status>(t);
199 return toStatusT_1_4(err);
200}
201
Robert Shih89b232b2022-12-13 09:22:42 -0800202DrmStatus statusAidlToDrmStatus(::ndk::ScopedAStatus& statusAidl);
Kyle Zhang6605add2022-01-13 17:51:23 +0000203
204template<typename T, typename U>
205status_t GetLogMessagesAidl(const std::shared_ptr<U> &obj, Vector<::V1_4::LogMessage> &logs) {
206 std::shared_ptr<T> plugin = obj;
207 if (obj == NULL) {
208 LOG2BW("%s obj is null", U::descriptor);
209 } else if (plugin == NULL) {
210 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
211 }
212
213 std::vector<LogMessage> pluginLogsAidl;
214 if (plugin != NULL) {
215 if(!plugin->getLogMessages(&pluginLogsAidl).isOk()) {
216 LOG2BW("%s::getLogMessages remote call failed", T::descriptor);
217 }
218 }
219
220 std::vector<::V1_4::LogMessage> pluginLogs;
221 for (LogMessage log : pluginLogsAidl) {
222 ::V1_4::LogMessage logHidl;
223 logHidl.timeMs = log.timeMs;
224 // skip negative convert check as count of enum elements is 7
225 logHidl.priority = static_cast<::V1_4::LogPriority>((int32_t)log.priority);
226 logHidl.message = log.message;
227 pluginLogs.push_back(logHidl);
228 }
229
230 auto allLogs(gLogBuf.getLogs());
231 LOG2BD("framework logs size %zu; plugin logs size %zu",
232 allLogs.size(), pluginLogs.size());
233 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
234 std::sort(allLogs.begin(), allLogs.end(),
235 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
236 return a.timeMs < b.timeMs;
237 });
238
239 logs.appendVector(allLogs);
240 return OK;
241}
242
Robert Shih9afca952021-02-12 01:36:06 -0800243template<typename T, typename U>
244status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) {
245 sp<T> plugin = T::castFrom(obj);
Robert Shih8635cb12021-02-26 07:57:55 -0800246 if (obj == NULL) {
247 LOG2BW("%s obj is null", U::descriptor);
248 } else if (plugin == NULL) {
249 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
Robert Shih9afca952021-02-12 01:36:06 -0800250 }
251
252 ::V1_4::Status err{};
Robert Shih8635cb12021-02-26 07:57:55 -0800253 std::vector<::V1_4::LogMessage> pluginLogs;
Robert Shih9afca952021-02-12 01:36:06 -0800254 ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&](
255 ::V1_4::Status status,
256 hidl_vec<::V1_4::LogMessage> hLogs) {
Robert Shihfaae26a2021-02-20 00:01:19 -0800257 if (::V1_4::Status::OK != status) {
Robert Shih9afca952021-02-12 01:36:06 -0800258 err = status;
259 return;
260 }
Robert Shih8635cb12021-02-26 07:57:55 -0800261 pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size());
Robert Shih9afca952021-02-12 01:36:06 -0800262 };
263
Robert Shih8635cb12021-02-26 07:57:55 -0800264 Return<void> hResult;
265 if (plugin != NULL) {
266 hResult = plugin->getLogMessages(cb);
Robert Shih9afca952021-02-12 01:36:06 -0800267 }
Robert Shih8635cb12021-02-26 07:57:55 -0800268 if (!hResult.isOk()) {
Robert Shihbd790122021-03-01 20:45:31 -0800269 LOG2BW("%s::getLogMessages remote call failed %s",
270 T::descriptor, hResult.description().c_str());
Robert Shih8635cb12021-02-26 07:57:55 -0800271 }
272
273 auto allLogs(gLogBuf.getLogs());
Robert Shih87aed812021-04-05 11:42:31 -0700274 LOG2BD("framework logs size %zu; plugin logs size %zu",
Robert Shihbd790122021-03-01 20:45:31 -0800275 allLogs.size(), pluginLogs.size());
Robert Shih8635cb12021-02-26 07:57:55 -0800276 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
277 std::sort(allLogs.begin(), allLogs.end(),
278 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
279 return a.timeMs < b.timeMs;
280 });
281
282 logs.appendVector(allLogs);
283 return OK;
Robert Shih9afca952021-02-12 01:36:06 -0800284}
285
Robert Shihae5c6db2022-12-15 10:38:24 -0800286std::string GetExceptionMessage(const DrmStatus & err, const char *defaultMsg,
Robert Shih8635cb12021-02-26 07:57:55 -0800287 const Vector<::V1_4::LogMessage> &logs);
Robert Shih0beba052021-02-14 00:47:00 -0800288
289template<typename T>
Robert Shihae5c6db2022-12-15 10:38:24 -0800290std::string GetExceptionMessage(const DrmStatus &err, const char *defaultMsg, const sp<T> &iface) {
Robert Shih0beba052021-02-14 00:47:00 -0800291 Vector<::V1_4::LogMessage> logs;
Robert Shih8635cb12021-02-26 07:57:55 -0800292 iface->getLogMessages(logs);
Robert Shihae5c6db2022-12-15 10:38:24 -0800293 return GetExceptionMessage(err, defaultMsg, logs);
Robert Shih0beba052021-02-14 00:47:00 -0800294}
295
Robert Shih28c2ed32019-10-27 22:55:12 -0700296} // namespace DrmUtils
Robert Shih28c2ed32019-10-27 22:55:12 -0700297} // namespace android
Robert Shih28c2ed32019-10-27 22:55:12 -0700298#endif // ANDROID_DRMUTILS_H