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> |
| 36 | #include <iterator> |
| 37 | #include <mutex> |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 38 | #include <vector> |
| 39 | |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 40 | |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 41 | using namespace ::android::hardware::drm; |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 42 | using ::android::hardware::hidl_vec; |
| 43 | using ::android::hardware::Return; |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 44 | |
| 45 | namespace android { |
| 46 | |
| 47 | struct ICrypto; |
| 48 | struct IDrm; |
| 49 | |
| 50 | namespace DrmUtils { |
| 51 | |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 52 | // Log APIs |
| 53 | class LogBuffer { |
| 54 | public: |
| 55 | static const int MAX_CAPACITY = 100; |
| 56 | void addLog(const ::V1_4::LogMessage &log); |
| 57 | Vector<::V1_4::LogMessage> getLogs(); |
| 58 | |
| 59 | private: |
| 60 | std::deque<::V1_4::LogMessage> mBuffer; |
| 61 | std::mutex mMutex; |
| 62 | }; |
| 63 | |
| 64 | extern LogBuffer gLogBuf; |
| 65 | |
| 66 | static inline int formatBuffer(char *buf, size_t size, const char *msg) { |
| 67 | return snprintf(buf, size, "%s", msg); |
| 68 | } |
| 69 | |
| 70 | template <typename First, typename... Args> |
| 71 | static inline int formatBuffer(char *buf, size_t size, const char *fmt, First first, Args... args) { |
| 72 | return snprintf(buf, size, fmt, first, args...); |
| 73 | } |
| 74 | |
| 75 | template <typename... Args> |
| 76 | void LogToBuffer(android_LogPriority level, const char *fmt, Args... args) { |
| 77 | const int LOG_BUF_SIZE = 256; |
| 78 | char buf[LOG_BUF_SIZE]; |
| 79 | int len = formatBuffer(buf, LOG_BUF_SIZE, fmt, args...); |
| 80 | if (len <= 0) { |
| 81 | return; |
| 82 | } |
| 83 | __android_log_write(level, LOG_TAG, buf); |
| 84 | if (level >= ANDROID_LOG_INFO) { |
| 85 | int64_t epochTimeMs = |
| 86 | std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1); |
| 87 | gLogBuf.addLog({epochTimeMs, static_cast<::V1_4::LogPriority>(level), buf}); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | #ifndef LOG2BE |
| 92 | #define LOG2BE(...) LogToBuffer(ANDROID_LOG_ERROR, __VA_ARGS__) |
| 93 | #define LOG2BW(...) LogToBuffer(ANDROID_LOG_WARN, __VA_ARGS__) |
| 94 | #define LOG2BI(...) LogToBuffer(ANDROID_LOG_INFO, __VA_ARGS__) |
| 95 | #define LOG2BD(...) LogToBuffer(ANDROID_LOG_DEBUG, __VA_ARGS__) |
| 96 | #define LOG2BV(...) LogToBuffer(ANDROID_LOG_VERBOSE, __VA_ARGS__) |
| 97 | #endif |
| 98 | |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 99 | bool UseDrmService(); |
| 100 | |
| 101 | sp<IDrm> MakeDrm(status_t *pstatus = nullptr); |
| 102 | |
| 103 | sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr); |
| 104 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 105 | template<typename BA, typename PARCEL> |
| 106 | void WriteByteArray(PARCEL &obj, const BA &vec) { |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 107 | obj.writeInt32(vec.size()); |
| 108 | if (vec.size()) { |
| 109 | obj.write(vec.data(), vec.size()); |
| 110 | } |
| 111 | } |
| 112 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 113 | template<typename ET, typename BA, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 114 | void WriteEventToParcel( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 115 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 116 | ET eventType, |
| 117 | const BA &sessionId, |
| 118 | const BA &data) { |
| 119 | WriteByteArray(obj, sessionId); |
| 120 | WriteByteArray(obj, data); |
| 121 | obj.writeInt32(eventType); |
| 122 | } |
| 123 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 124 | template<typename BA, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 125 | void WriteExpirationUpdateToParcel( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 126 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 127 | const BA &sessionId, |
| 128 | int64_t expiryTimeInMS) { |
| 129 | WriteByteArray(obj, sessionId); |
| 130 | obj.writeInt64(expiryTimeInMS); |
| 131 | } |
| 132 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 133 | template<typename BA, typename KSL, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 134 | void WriteKeysChange( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 135 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 136 | const BA &sessionId, |
| 137 | const KSL &keyStatusList, |
| 138 | bool hasNewUsableKey) { |
| 139 | WriteByteArray(obj, sessionId); |
| 140 | obj.writeInt32(keyStatusList.size()); |
| 141 | for (const auto &keyStatus : keyStatusList) { |
| 142 | WriteByteArray(obj, keyStatus.keyId); |
| 143 | obj.writeInt32(keyStatus.type); |
| 144 | } |
| 145 | obj.writeInt32(hasNewUsableKey); |
| 146 | } |
| 147 | |
Robert Shih | c0d1d0e | 2019-11-24 13:21:04 -0800 | [diff] [blame] | 148 | 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] | 149 | |
| 150 | std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16], |
| 151 | const char *appPackageName); |
| 152 | |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 153 | std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]); |
| 154 | |
| 155 | std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16], |
| 156 | const void *initData, size_t initDataSize); |
| 157 | |
Robert Shih | 5944a0b | 2021-02-10 04:26:33 -0800 | [diff] [blame] | 158 | status_t toStatusT_1_4(::V1_4::Status status); |
| 159 | |
| 160 | template<typename S> |
| 161 | inline status_t toStatusT(S status) { |
| 162 | auto err = static_cast<::V1_4::Status>(status); |
| 163 | return toStatusT_1_4(err); |
| 164 | } |
| 165 | |
| 166 | template<typename T> |
| 167 | inline status_t toStatusT(const android::hardware::Return<T> &status) { |
| 168 | auto t = static_cast<T>(status); |
| 169 | auto err = static_cast<::V1_4::Status>(t); |
| 170 | return toStatusT_1_4(err); |
| 171 | } |
| 172 | |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 173 | template<typename T, typename U> |
| 174 | status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) { |
| 175 | sp<T> plugin = T::castFrom(obj); |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 176 | if (obj == NULL) { |
| 177 | LOG2BW("%s obj is null", U::descriptor); |
| 178 | } else if (plugin == NULL) { |
| 179 | LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor); |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | ::V1_4::Status err{}; |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 183 | std::vector<::V1_4::LogMessage> pluginLogs; |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 184 | ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&]( |
| 185 | ::V1_4::Status status, |
| 186 | hidl_vec<::V1_4::LogMessage> hLogs) { |
Robert Shih | faae26a | 2021-02-20 00:01:19 -0800 | [diff] [blame] | 187 | if (::V1_4::Status::OK != status) { |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 188 | err = status; |
| 189 | return; |
| 190 | } |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 191 | pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size()); |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 192 | }; |
| 193 | |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 194 | Return<void> hResult; |
| 195 | if (plugin != NULL) { |
| 196 | hResult = plugin->getLogMessages(cb); |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 197 | } |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 198 | if (!hResult.isOk()) { |
| 199 | LOG2BW("%s::getLogMessages remote call failed", T::descriptor); |
| 200 | } |
| 201 | |
| 202 | auto allLogs(gLogBuf.getLogs()); |
| 203 | std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs)); |
| 204 | std::sort(allLogs.begin(), allLogs.end(), |
| 205 | [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) { |
| 206 | return a.timeMs < b.timeMs; |
| 207 | }); |
| 208 | |
| 209 | logs.appendVector(allLogs); |
| 210 | return OK; |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 213 | std::string GetExceptionMessage(status_t err, const char *msg, |
| 214 | const Vector<::V1_4::LogMessage> &logs); |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 215 | |
| 216 | template<typename T> |
| 217 | 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] | 218 | Vector<::V1_4::LogMessage> logs; |
Robert Shih | 8635cb1 | 2021-02-26 07:57:55 -0800 | [diff] [blame] | 219 | iface->getLogMessages(logs); |
| 220 | return GetExceptionMessage(err, msg, logs); |
Robert Shih | 0beba05 | 2021-02-14 00:47:00 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 223 | } // namespace DrmUtils |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 224 | } // namespace android |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 225 | #endif // ANDROID_DRMUTILS_H |