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 | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame^] | 26 | #include <utils/Vector.h> |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 27 | #include <utils/StrongPointer.h> |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame^] | 30 | |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 31 | using namespace ::android::hardware::drm; |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame^] | 32 | using ::android::hardware::hidl_vec; |
| 33 | using ::android::hardware::Return; |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | struct ICrypto; |
| 38 | struct IDrm; |
| 39 | |
| 40 | namespace DrmUtils { |
| 41 | |
| 42 | bool UseDrmService(); |
| 43 | |
| 44 | sp<IDrm> MakeDrm(status_t *pstatus = nullptr); |
| 45 | |
| 46 | sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr); |
| 47 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 48 | template<typename BA, typename PARCEL> |
| 49 | void WriteByteArray(PARCEL &obj, const BA &vec) { |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 50 | obj.writeInt32(vec.size()); |
| 51 | if (vec.size()) { |
| 52 | obj.write(vec.data(), vec.size()); |
| 53 | } |
| 54 | } |
| 55 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 56 | template<typename ET, typename BA, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 57 | void WriteEventToParcel( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 58 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 59 | ET eventType, |
| 60 | const BA &sessionId, |
| 61 | const BA &data) { |
| 62 | WriteByteArray(obj, sessionId); |
| 63 | WriteByteArray(obj, data); |
| 64 | obj.writeInt32(eventType); |
| 65 | } |
| 66 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 67 | template<typename BA, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 68 | void WriteExpirationUpdateToParcel( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 69 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 70 | const BA &sessionId, |
| 71 | int64_t expiryTimeInMS) { |
| 72 | WriteByteArray(obj, sessionId); |
| 73 | obj.writeInt64(expiryTimeInMS); |
| 74 | } |
| 75 | |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 76 | template<typename BA, typename KSL, typename PARCEL> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 77 | void WriteKeysChange( |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 78 | PARCEL &obj, |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 79 | const BA &sessionId, |
| 80 | const KSL &keyStatusList, |
| 81 | bool hasNewUsableKey) { |
| 82 | WriteByteArray(obj, sessionId); |
| 83 | obj.writeInt32(keyStatusList.size()); |
| 84 | for (const auto &keyStatus : keyStatusList) { |
| 85 | WriteByteArray(obj, keyStatus.keyId); |
| 86 | obj.writeInt32(keyStatus.type); |
| 87 | } |
| 88 | obj.writeInt32(hasNewUsableKey); |
| 89 | } |
| 90 | |
Robert Shih | c0d1d0e | 2019-11-24 13:21:04 -0800 | [diff] [blame] | 91 | 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] | 92 | |
| 93 | std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16], |
| 94 | const char *appPackageName); |
| 95 | |
Robert Shih | 10fe943 | 2019-11-09 08:26:49 -0800 | [diff] [blame] | 96 | std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]); |
| 97 | |
| 98 | std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16], |
| 99 | const void *initData, size_t initDataSize); |
| 100 | |
Robert Shih | 5944a0b | 2021-02-10 04:26:33 -0800 | [diff] [blame] | 101 | status_t toStatusT_1_4(::V1_4::Status status); |
| 102 | |
| 103 | template<typename S> |
| 104 | inline status_t toStatusT(S status) { |
| 105 | auto err = static_cast<::V1_4::Status>(status); |
| 106 | return toStatusT_1_4(err); |
| 107 | } |
| 108 | |
| 109 | template<typename T> |
| 110 | inline status_t toStatusT(const android::hardware::Return<T> &status) { |
| 111 | auto t = static_cast<T>(status); |
| 112 | auto err = static_cast<::V1_4::Status>(t); |
| 113 | return toStatusT_1_4(err); |
| 114 | } |
| 115 | |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame^] | 116 | template<typename T, typename U> |
| 117 | status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) { |
| 118 | sp<T> plugin = T::castFrom(obj); |
| 119 | if (plugin == NULL) { |
| 120 | return ERROR_UNSUPPORTED; |
| 121 | } |
| 122 | |
| 123 | ::V1_4::Status err{}; |
| 124 | ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&]( |
| 125 | ::V1_4::Status status, |
| 126 | hidl_vec<::V1_4::LogMessage> hLogs) { |
| 127 | if (::V1_4::Status::OK == status) { |
| 128 | err = status; |
| 129 | return; |
| 130 | } |
| 131 | logs.appendArray(hLogs.data(), hLogs.size()); |
| 132 | }; |
| 133 | |
| 134 | Return<void> hResult = plugin->getLogMessages(cb); |
| 135 | if (!hResult.isOk()) { |
| 136 | return DEAD_OBJECT; |
| 137 | } |
| 138 | return toStatusT(err); |
| 139 | } |
| 140 | |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 141 | } // namespace DrmUtils |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 142 | } // namespace android |
Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame] | 143 | #endif // ANDROID_DRMUTILS_H |