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> |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 25 | #include <utils/Errors.h> // for status_t |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | #include <utils/String8.h> |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 28 | #include <utils/StrongPointer.h> |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 29 | #include <utils/Vector.h> |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 30 | #include <algorithm> |
| 31 | #include <chrono> |
| 32 | #include <cstddef> |
| 33 | #include <cstdint> |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 34 | #include <ctime> |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 35 | #include <deque> |
Robert Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 36 | #include <endian.h> |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 37 | #include <iterator> |
| 38 | #include <mutex> |
Robert Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 39 | #include <string> |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 40 | #include <vector> |
| 41 | |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 42 | |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 43 | using namespace ::android::hardware::drm; |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 44 | using ::android::hardware::hidl_vec; |
| 45 | using ::android::hardware::Return; |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 46 | |
| 47 | namespace android { |
| 48 | |
| 49 | struct ICrypto; |
| 50 | struct IDrm; |
| 51 | |
| 52 | namespace DrmUtils { |
| 53 | |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 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 | |
| 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> |
| 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> |
| 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 | |
Robert Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 93 | template <typename... 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 | |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 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 | |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 109 | bool UseDrmService(); |
| 110 | |
| 111 | sp<IDrm> MakeDrm(status_t *pstatus = nullptr); |
| 112 | |
| 113 | sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr); |
| 114 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 115 | template<typename BA, typename PARCEL> |
| 116 | void WriteByteArray(PARCEL &obj, const BA &vec) { |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 117 | obj.writeInt32(vec.size()); |
| 118 | if (vec.size()) { |
| 119 | obj.write(vec.data(), vec.size()); |
| 120 | } |
| 121 | } |
| 122 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 123 | template<typename ET, typename BA, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 124 | void WriteEventToParcel( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 125 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 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 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 134 | template<typename BA, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 135 | void WriteExpirationUpdateToParcel( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 136 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 137 | const BA &sessionId, |
| 138 | int64_t expiryTimeInMS) { |
| 139 | WriteByteArray(obj, sessionId); |
| 140 | obj.writeInt64(expiryTimeInMS); |
| 141 | } |
| 142 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 143 | template<typename BA, typename KSL, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 144 | void WriteKeysChange( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 145 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 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 | |
Robert Shih | c0d1d0e | 2019-11-24 13:21:04 -0800 | [diff] [blame] | 158 | 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] | 159 | |
| 160 | std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16], |
| 161 | const char *appPackageName); |
| 162 | |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 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 | |
Robert Shih | 5944a0b | 2021-02-10 04:26:33 -0800 | [diff] [blame] | 168 | status_t toStatusT_1_4(::V1_4::Status status); |
| 169 | |
| 170 | template<typename S> |
| 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> |
| 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 | |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 183 | template<typename T, typename U> |
| 184 | status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) { |
| 185 | sp<T> plugin = T::castFrom(obj); |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 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); |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | ::V1_4::Status err{}; |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 193 | std::vector<::V1_4::LogMessage> pluginLogs; |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 194 | ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&]( |
| 195 | ::V1_4::Status status, |
| 196 | hidl_vec<::V1_4::LogMessage> hLogs) { |
Robert Shih | faae26a | 2021-02-20 00:01:19 -0800 | [diff] [blame] | 197 | if (::V1_4::Status::OK != status) { |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 198 | err = status; |
| 199 | return; |
| 200 | } |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 201 | pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size()); |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 202 | }; |
| 203 | |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 204 | Return<void> hResult; |
| 205 | if (plugin != NULL) { |
| 206 | hResult = plugin->getLogMessages(cb); |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 207 | } |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 208 | if (!hResult.isOk()) { |
Robert Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 209 | LOG2BW("%s::getLogMessages remote call failed %s", |
| 210 | T::descriptor, hResult.description().c_str()); |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | auto allLogs(gLogBuf.getLogs()); |
Robert Shih | 87aed81 | 2021-04-05 11:42:31 -0700 | [diff] [blame] | 214 | LOG2BD("framework logs size %zu; plugin logs size %zu", |
Robert Shih | bd79012 | 2021-03-01 20:45:31 -0800 | [diff] [blame] | 215 | allLogs.size(), pluginLogs.size()); |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 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; |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 226 | std::string GetExceptionMessage(status_t err, const char *msg, |
| 227 | const Vector<::V1_4::LogMessage> &logs); |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 228 | |
| 229 | template<typename T> |
| 230 | std::string GetExceptionMessage(status_t err, const char *msg, const sp<T> &iface) { |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 231 | Vector<::V1_4::LogMessage> logs; |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 232 | iface->getLogMessages(logs); |
| 233 | return GetExceptionMessage(err, msg, logs); |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 236 | } // namespace DrmUtils |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 237 | } // namespace android |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 238 | #endif // ANDROID_DRMUTILS_H |