blob: 94cf74379f4e03afc7a7a0e4c789f8755100d846 [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 Nagaraj17d54db2022-12-29 10:00:59 +053025#include <mediadrm/DrmMetricsLogger.h>
Sohail Nagaraj0cc3da82022-11-30 10:22:37 +053026#include <mediadrm/DrmStatus.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070027#include <utils/Errors.h> // for status_t
Robert Shih0beba052021-02-14 00:47:00 -080028#include <utils/Log.h>
29#include <utils/String8.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070030#include <utils/StrongPointer.h>
Robert Shih0beba052021-02-14 00:47:00 -080031#include <utils/Vector.h>
Robert Shih8635cb12021-02-26 07:57:55 -080032#include <algorithm>
33#include <chrono>
34#include <cstddef>
35#include <cstdint>
Robert Shih2616a632021-09-13 17:45:38 -070036#include <cstring>
Robert Shih0beba052021-02-14 00:47:00 -080037#include <ctime>
Robert Shih8635cb12021-02-26 07:57:55 -080038#include <deque>
Robert Shihbd790122021-03-01 20:45:31 -080039#include <endian.h>
Robert Shih8635cb12021-02-26 07:57:55 -080040#include <iterator>
41#include <mutex>
Robert Shihbd790122021-03-01 20:45:31 -080042#include <string>
Robert Shih10fe9432019-11-09 08:26:49 -080043#include <vector>
Kyle Zhang6605add2022-01-13 17:51:23 +000044#include <aidl/android/hardware/drm/LogMessage.h>
45#include <aidl/android/hardware/drm/Status.h>
Kyle Zhang994023a2022-02-09 05:32:12 +000046#include <aidl/android/hardware/drm/IDrmFactory.h>
Robert Shih9afca952021-02-12 01:36:06 -080047
Robert Shih10fe9432019-11-09 08:26:49 -080048using namespace ::android::hardware::drm;
Robert Shih9afca952021-02-12 01:36:06 -080049using ::android::hardware::hidl_vec;
50using ::android::hardware::Return;
Robert Shih28c2ed32019-10-27 22:55:12 -070051
Kyle Zhang6605add2022-01-13 17:51:23 +000052using ::aidl::android::hardware::drm::LogPriority;
53using ::aidl::android::hardware::drm::LogMessage;
Kyle Zhang994023a2022-02-09 05:32:12 +000054using ::aidl::android::hardware::drm::Uuid;
Kyle Zhang6605add2022-01-13 17:51:23 +000055using StatusAidl = ::aidl::android::hardware::drm::Status;
Kyle Zhang994023a2022-02-09 05:32:12 +000056using IDrmFactoryAidl = ::aidl::android::hardware::drm::IDrmFactory;
Kyle Zhang6605add2022-01-13 17:51:23 +000057
Robert Shih28c2ed32019-10-27 22:55:12 -070058namespace android {
59
60struct ICrypto;
61struct IDrm;
62
63namespace DrmUtils {
64
Robert Shih8635cb12021-02-26 07:57:55 -080065// Log APIs
66class LogBuffer {
67 public:
68 static const int MAX_CAPACITY = 100;
69 void addLog(const ::V1_4::LogMessage &log);
70 Vector<::V1_4::LogMessage> getLogs();
71
72 private:
73 std::deque<::V1_4::LogMessage> mBuffer;
74 std::mutex mMutex;
75};
76
77extern LogBuffer gLogBuf;
78
79static inline int formatBuffer(char *buf, size_t size, const char *msg) {
80 return snprintf(buf, size, "%s", msg);
81}
82
83template <typename First, typename... Args>
84static inline int formatBuffer(char *buf, size_t size, const char *fmt, First first, Args... args) {
85 return snprintf(buf, size, fmt, first, args...);
86}
87
88template <typename... Args>
89void LogToBuffer(android_LogPriority level, const char *fmt, Args... args) {
90 const int LOG_BUF_SIZE = 256;
91 char buf[LOG_BUF_SIZE];
92 int len = formatBuffer(buf, LOG_BUF_SIZE, fmt, args...);
93 if (len <= 0) {
94 return;
95 }
96 __android_log_write(level, LOG_TAG, buf);
97 if (level >= ANDROID_LOG_INFO) {
98 int64_t epochTimeMs =
99 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
100 gLogBuf.addLog({epochTimeMs, static_cast<::V1_4::LogPriority>(level), buf});
101 }
102}
103
Robert Shihbd790122021-03-01 20:45:31 -0800104template <typename... Args>
105void LogToBuffer(android_LogPriority level, const uint8_t uuid[16], const char *fmt, Args... args) {
Robert Shih2616a632021-09-13 17:45:38 -0700106 uint64_t uuid2[2] = {};
107 std::memcpy(uuid2, uuid, sizeof(uuid2));
Robert Shihbd790122021-03-01 20:45:31 -0800108 std::string uuidFmt("uuid=[%lx %lx] ");
109 uuidFmt += fmt;
110 LogToBuffer(level, uuidFmt.c_str(), htobe64(uuid2[0]), htobe64(uuid2[1]), args...);
111}
112
Robert Shih8635cb12021-02-26 07:57:55 -0800113#ifndef LOG2BE
114#define LOG2BE(...) LogToBuffer(ANDROID_LOG_ERROR, __VA_ARGS__)
115#define LOG2BW(...) LogToBuffer(ANDROID_LOG_WARN, __VA_ARGS__)
116#define LOG2BI(...) LogToBuffer(ANDROID_LOG_INFO, __VA_ARGS__)
117#define LOG2BD(...) LogToBuffer(ANDROID_LOG_DEBUG, __VA_ARGS__)
118#define LOG2BV(...) LogToBuffer(ANDROID_LOG_VERBOSE, __VA_ARGS__)
119#endif
120
Robert Shih28c2ed32019-10-27 22:55:12 -0700121bool UseDrmService();
122
Sohail Nagaraj17d54db2022-12-29 10:00:59 +0530123sp<IDrm> MakeDrm(IDrmFrontend frontend = IDRM_JNI, status_t* pstatus = nullptr);
Robert Shih28c2ed32019-10-27 22:55:12 -0700124
125sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
126
Robert Shihd3f9ba72019-11-20 17:25:26 -0800127template<typename BA, typename PARCEL>
128void WriteByteArray(PARCEL &obj, const BA &vec) {
Robert Shih61e1c762019-10-31 21:26:58 -0700129 obj.writeInt32(vec.size());
130 if (vec.size()) {
131 obj.write(vec.data(), vec.size());
132 }
133}
134
Robert Shihd3f9ba72019-11-20 17:25:26 -0800135template<typename ET, typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700136void WriteEventToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800137 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700138 ET eventType,
139 const BA &sessionId,
140 const BA &data) {
141 WriteByteArray(obj, sessionId);
142 WriteByteArray(obj, data);
143 obj.writeInt32(eventType);
144}
145
Robert Shihd3f9ba72019-11-20 17:25:26 -0800146template<typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700147void WriteExpirationUpdateToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800148 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700149 const BA &sessionId,
150 int64_t expiryTimeInMS) {
151 WriteByteArray(obj, sessionId);
152 obj.writeInt64(expiryTimeInMS);
153}
154
Robert Shihd3f9ba72019-11-20 17:25:26 -0800155template<typename BA, typename KSL, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700156void WriteKeysChange(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800157 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700158 const BA &sessionId,
159 const KSL &keyStatusList,
160 bool hasNewUsableKey) {
161 WriteByteArray(obj, sessionId);
162 obj.writeInt32(keyStatusList.size());
163 for (const auto &keyStatus : keyStatusList) {
164 WriteByteArray(obj, keyStatus.keyId);
165 obj.writeInt32(keyStatus.type);
166 }
167 obj.writeInt32(hasNewUsableKey);
168}
169
Kyle Zhang994023a2022-02-09 05:32:12 +0000170inline Uuid toAidlUuid(const uint8_t uuid[16]) {
171 Uuid uuidAidl;
172 for (int i = 0; i < 16; ++i) uuidAidl.uuid[i] = uuid[i];
173 return uuidAidl;
174}
175
176std::vector<std::shared_ptr<IDrmFactoryAidl>> makeDrmFactoriesAidl();
177
Robert Shihc0d1d0e2019-11-24 13:21:04 -0800178std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
Robert Shih5ff3ad62019-11-09 08:26:49 -0800179
180std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
181 const char *appPackageName);
182
Robert Shih10fe9432019-11-09 08:26:49 -0800183std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
184
185std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
186 const void *initData, size_t initDataSize);
187
Robert Shih5944a0b2021-02-10 04:26:33 -0800188status_t toStatusT_1_4(::V1_4::Status status);
189
190template<typename S>
191inline status_t toStatusT(S status) {
192 auto err = static_cast<::V1_4::Status>(status);
193 return toStatusT_1_4(err);
194}
195
196template<typename T>
197inline status_t toStatusT(const android::hardware::Return<T> &status) {
198 auto t = static_cast<T>(status);
199 auto err = static_cast<::V1_4::Status>(t);
200 return toStatusT_1_4(err);
201}
202
Robert Shih89b232b2022-12-13 09:22:42 -0800203DrmStatus statusAidlToDrmStatus(::ndk::ScopedAStatus& statusAidl);
Kyle Zhang6605add2022-01-13 17:51:23 +0000204
205template<typename T, typename U>
206status_t GetLogMessagesAidl(const std::shared_ptr<U> &obj, Vector<::V1_4::LogMessage> &logs) {
207 std::shared_ptr<T> plugin = obj;
208 if (obj == NULL) {
209 LOG2BW("%s obj is null", U::descriptor);
210 } else if (plugin == NULL) {
211 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
212 }
213
214 std::vector<LogMessage> pluginLogsAidl;
215 if (plugin != NULL) {
216 if(!plugin->getLogMessages(&pluginLogsAidl).isOk()) {
217 LOG2BW("%s::getLogMessages remote call failed", T::descriptor);
218 }
219 }
220
221 std::vector<::V1_4::LogMessage> pluginLogs;
222 for (LogMessage log : pluginLogsAidl) {
223 ::V1_4::LogMessage logHidl;
224 logHidl.timeMs = log.timeMs;
225 // skip negative convert check as count of enum elements is 7
226 logHidl.priority = static_cast<::V1_4::LogPriority>((int32_t)log.priority);
227 logHidl.message = log.message;
228 pluginLogs.push_back(logHidl);
229 }
230
231 auto allLogs(gLogBuf.getLogs());
232 LOG2BD("framework logs size %zu; plugin logs size %zu",
233 allLogs.size(), pluginLogs.size());
234 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
235 std::sort(allLogs.begin(), allLogs.end(),
236 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
237 return a.timeMs < b.timeMs;
238 });
239
240 logs.appendVector(allLogs);
241 return OK;
242}
243
Robert Shih9afca952021-02-12 01:36:06 -0800244template<typename T, typename U>
245status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) {
246 sp<T> plugin = T::castFrom(obj);
Robert Shih8635cb12021-02-26 07:57:55 -0800247 if (obj == NULL) {
248 LOG2BW("%s obj is null", U::descriptor);
249 } else if (plugin == NULL) {
250 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
Robert Shih9afca952021-02-12 01:36:06 -0800251 }
252
253 ::V1_4::Status err{};
Robert Shih8635cb12021-02-26 07:57:55 -0800254 std::vector<::V1_4::LogMessage> pluginLogs;
Robert Shih9afca952021-02-12 01:36:06 -0800255 ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&](
256 ::V1_4::Status status,
257 hidl_vec<::V1_4::LogMessage> hLogs) {
Robert Shihfaae26a2021-02-20 00:01:19 -0800258 if (::V1_4::Status::OK != status) {
Robert Shih9afca952021-02-12 01:36:06 -0800259 err = status;
260 return;
261 }
Robert Shih8635cb12021-02-26 07:57:55 -0800262 pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size());
Robert Shih9afca952021-02-12 01:36:06 -0800263 };
264
Robert Shih8635cb12021-02-26 07:57:55 -0800265 Return<void> hResult;
266 if (plugin != NULL) {
267 hResult = plugin->getLogMessages(cb);
Robert Shih9afca952021-02-12 01:36:06 -0800268 }
Robert Shih8635cb12021-02-26 07:57:55 -0800269 if (!hResult.isOk()) {
Robert Shihbd790122021-03-01 20:45:31 -0800270 LOG2BW("%s::getLogMessages remote call failed %s",
271 T::descriptor, hResult.description().c_str());
Robert Shih8635cb12021-02-26 07:57:55 -0800272 }
273
274 auto allLogs(gLogBuf.getLogs());
Robert Shih87aed812021-04-05 11:42:31 -0700275 LOG2BD("framework logs size %zu; plugin logs size %zu",
Robert Shihbd790122021-03-01 20:45:31 -0800276 allLogs.size(), pluginLogs.size());
Robert Shih8635cb12021-02-26 07:57:55 -0800277 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
278 std::sort(allLogs.begin(), allLogs.end(),
279 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
280 return a.timeMs < b.timeMs;
281 });
282
283 logs.appendVector(allLogs);
284 return OK;
Robert Shih9afca952021-02-12 01:36:06 -0800285}
286
Robert Shihae5c6db2022-12-15 10:38:24 -0800287std::string GetExceptionMessage(const DrmStatus & err, const char *defaultMsg,
Robert Shih8635cb12021-02-26 07:57:55 -0800288 const Vector<::V1_4::LogMessage> &logs);
Robert Shih0beba052021-02-14 00:47:00 -0800289
290template<typename T>
Robert Shihae5c6db2022-12-15 10:38:24 -0800291std::string GetExceptionMessage(const DrmStatus &err, const char *defaultMsg, const sp<T> &iface) {
Robert Shih0beba052021-02-14 00:47:00 -0800292 Vector<::V1_4::LogMessage> logs;
Robert Shih4d181fb2022-12-16 11:11:05 -0800293 if (iface != NULL) {
294 iface->getLogMessages(logs);
295 }
Robert Shihae5c6db2022-12-15 10:38:24 -0800296 return GetExceptionMessage(err, defaultMsg, logs);
Robert Shih0beba052021-02-14 00:47:00 -0800297}
298
Robert Shih28c2ed32019-10-27 22:55:12 -0700299} // namespace DrmUtils
Robert Shih28c2ed32019-10-27 22:55:12 -0700300} // namespace android
Robert Shih28c2ed32019-10-27 22:55:12 -0700301#endif // ANDROID_DRMUTILS_H