Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 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 | |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 20 | #include <android/hardware/drm/1.0/ICryptoFactory.h> |
Robert Shih | 5ff3ad6 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 21 | #include <android/hardware/drm/1.0/IDrmFactory.h> |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 22 | #include <android/hardware/drm/1.4/IDrmPlugin.h> |
Robert Shih | 5944a0b | 2021-02-10 04:26:33 -0800 | [diff] [blame] | 23 | #include <android/hardware/drm/1.4/types.h> |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 24 | #include <media/stagefright/MediaErrors.h> |
Sohail Nagaraj | 17d54db | 2022-12-29 10:00:59 +0530 | [diff] [blame^] | 25 | #include <mediadrm/DrmMetricsLogger.h> |
Sohail Nagaraj | 0cc3da8 | 2022-11-30 10:22:37 +0530 | [diff] [blame] | 26 | #include <mediadrm/DrmStatus.h> |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 27 | #include <utils/Errors.h> // for status_t |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 28 | #include <utils/Log.h> |
| 29 | #include <utils/String8.h> |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 30 | #include <utils/StrongPointer.h> |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 31 | #include <utils/Vector.h> |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | #include <chrono> |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
Robert Shih | 2616a63 | 2021-09-13 17:45:38 -0700 | [diff] [blame] | 36 | #include <cstring> |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 37 | #include <ctime> |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 38 | #include <deque> |
Robert Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 39 | #include <endian.h> |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 40 | #include <iterator> |
| 41 | #include <mutex> |
Robert Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 42 | #include <string> |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 43 | #include <vector> |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 44 | #include <aidl/android/hardware/drm/LogMessage.h> |
| 45 | #include <aidl/android/hardware/drm/Status.h> |
Kyle Zhang | 994023a | 2022-02-09 05:32:12 +0000 | [diff] [blame] | 46 | #include <aidl/android/hardware/drm/IDrmFactory.h> |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 47 | |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 48 | using namespace ::android::hardware::drm; |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 49 | using ::android::hardware::hidl_vec; |
| 50 | using ::android::hardware::Return; |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 51 | |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 52 | using ::aidl::android::hardware::drm::LogPriority; |
| 53 | using ::aidl::android::hardware::drm::LogMessage; |
Kyle Zhang | 994023a | 2022-02-09 05:32:12 +0000 | [diff] [blame] | 54 | using ::aidl::android::hardware::drm::Uuid; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 55 | using StatusAidl = ::aidl::android::hardware::drm::Status; |
Kyle Zhang | 994023a | 2022-02-09 05:32:12 +0000 | [diff] [blame] | 56 | using IDrmFactoryAidl = ::aidl::android::hardware::drm::IDrmFactory; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 57 | |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 58 | namespace android { |
| 59 | |
| 60 | struct ICrypto; |
| 61 | struct IDrm; |
| 62 | |
| 63 | namespace DrmUtils { |
| 64 | |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 65 | // Log APIs |
| 66 | class 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 | |
| 77 | extern LogBuffer gLogBuf; |
| 78 | |
| 79 | static inline int formatBuffer(char *buf, size_t size, const char *msg) { |
| 80 | return snprintf(buf, size, "%s", msg); |
| 81 | } |
| 82 | |
| 83 | template <typename First, typename... Args> |
| 84 | static 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 | |
| 88 | template <typename... Args> |
| 89 | void 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 Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 104 | template <typename... Args> |
| 105 | void LogToBuffer(android_LogPriority level, const uint8_t uuid[16], const char *fmt, Args... args) { |
Robert Shih | 2616a63 | 2021-09-13 17:45:38 -0700 | [diff] [blame] | 106 | uint64_t uuid2[2] = {}; |
| 107 | std::memcpy(uuid2, uuid, sizeof(uuid2)); |
Robert Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 108 | 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 Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 113 | #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 Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 121 | bool UseDrmService(); |
| 122 | |
Sohail Nagaraj | 17d54db | 2022-12-29 10:00:59 +0530 | [diff] [blame^] | 123 | sp<IDrm> MakeDrm(IDrmFrontend frontend = IDRM_JNI, status_t* pstatus = nullptr); |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 124 | |
| 125 | sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr); |
| 126 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 127 | template<typename BA, typename PARCEL> |
| 128 | void WriteByteArray(PARCEL &obj, const BA &vec) { |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 129 | obj.writeInt32(vec.size()); |
| 130 | if (vec.size()) { |
| 131 | obj.write(vec.data(), vec.size()); |
| 132 | } |
| 133 | } |
| 134 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 135 | template<typename ET, typename BA, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 136 | void WriteEventToParcel( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 137 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 138 | 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 Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 146 | template<typename BA, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 147 | void WriteExpirationUpdateToParcel( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 148 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 149 | const BA &sessionId, |
| 150 | int64_t expiryTimeInMS) { |
| 151 | WriteByteArray(obj, sessionId); |
| 152 | obj.writeInt64(expiryTimeInMS); |
| 153 | } |
| 154 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 155 | template<typename BA, typename KSL, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 156 | void WriteKeysChange( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 157 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 158 | 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 Zhang | 994023a | 2022-02-09 05:32:12 +0000 | [diff] [blame] | 170 | inline 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 | |
| 176 | std::vector<std::shared_ptr<IDrmFactoryAidl>> makeDrmFactoriesAidl(); |
| 177 | |
Robert Shih | c0d1d0e | 2019-11-24 13:21:04 -0800 | [diff] [blame] | 178 | std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr); |
Robert Shih | 5ff3ad6 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 179 | |
| 180 | std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16], |
| 181 | const char *appPackageName); |
| 182 | |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 183 | std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]); |
| 184 | |
| 185 | std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16], |
| 186 | const void *initData, size_t initDataSize); |
| 187 | |
Robert Shih | 5944a0b | 2021-02-10 04:26:33 -0800 | [diff] [blame] | 188 | status_t toStatusT_1_4(::V1_4::Status status); |
| 189 | |
| 190 | template<typename S> |
| 191 | inline status_t toStatusT(S status) { |
| 192 | auto err = static_cast<::V1_4::Status>(status); |
| 193 | return toStatusT_1_4(err); |
| 194 | } |
| 195 | |
| 196 | template<typename T> |
| 197 | inline 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 Shih | 89b232b | 2022-12-13 09:22:42 -0800 | [diff] [blame] | 203 | DrmStatus statusAidlToDrmStatus(::ndk::ScopedAStatus& statusAidl); |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 204 | |
| 205 | template<typename T, typename U> |
| 206 | status_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 Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 244 | template<typename T, typename U> |
| 245 | status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) { |
| 246 | sp<T> plugin = T::castFrom(obj); |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 247 | 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 Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | ::V1_4::Status err{}; |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 254 | std::vector<::V1_4::LogMessage> pluginLogs; |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 255 | ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&]( |
| 256 | ::V1_4::Status status, |
| 257 | hidl_vec<::V1_4::LogMessage> hLogs) { |
Robert Shih | faae26a | 2021-02-20 00:01:19 -0800 | [diff] [blame] | 258 | if (::V1_4::Status::OK != status) { |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 259 | err = status; |
| 260 | return; |
| 261 | } |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 262 | pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size()); |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 263 | }; |
| 264 | |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 265 | Return<void> hResult; |
| 266 | if (plugin != NULL) { |
| 267 | hResult = plugin->getLogMessages(cb); |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 268 | } |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 269 | if (!hResult.isOk()) { |
Robert Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 270 | LOG2BW("%s::getLogMessages remote call failed %s", |
| 271 | T::descriptor, hResult.description().c_str()); |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | auto allLogs(gLogBuf.getLogs()); |
Robert Shih | 87aed81 | 2021-04-05 11:42:31 -0700 | [diff] [blame] | 275 | LOG2BD("framework logs size %zu; plugin logs size %zu", |
Robert Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 276 | allLogs.size(), pluginLogs.size()); |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 277 | 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 Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Robert Shih | ae5c6db | 2022-12-15 10:38:24 -0800 | [diff] [blame] | 287 | std::string GetExceptionMessage(const DrmStatus & err, const char *defaultMsg, |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 288 | const Vector<::V1_4::LogMessage> &logs); |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 289 | |
| 290 | template<typename T> |
Robert Shih | ae5c6db | 2022-12-15 10:38:24 -0800 | [diff] [blame] | 291 | std::string GetExceptionMessage(const DrmStatus &err, const char *defaultMsg, const sp<T> &iface) { |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 292 | Vector<::V1_4::LogMessage> logs; |
Robert Shih | 4d181fb | 2022-12-16 11:11:05 -0800 | [diff] [blame] | 293 | if (iface != NULL) { |
| 294 | iface->getLogMessages(logs); |
| 295 | } |
Robert Shih | ae5c6db | 2022-12-15 10:38:24 -0800 | [diff] [blame] | 296 | return GetExceptionMessage(err, defaultMsg, logs); |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 299 | } // namespace DrmUtils |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 300 | } // namespace android |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 301 | #endif // ANDROID_DRMUTILS_H |