blob: 10b72079a91b415bd9d7630444c7c11134ed0fee [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>
Robert Shih28c2ed32019-10-27 22:55:12 -070025#include <utils/Errors.h> // for status_t
Robert Shih9afca952021-02-12 01:36:06 -080026#include <utils/Vector.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070027#include <utils/StrongPointer.h>
Robert Shih10fe9432019-11-09 08:26:49 -080028#include <vector>
29
Robert Shih9afca952021-02-12 01:36:06 -080030
Robert Shih10fe9432019-11-09 08:26:49 -080031using namespace ::android::hardware::drm;
Robert Shih9afca952021-02-12 01:36:06 -080032using ::android::hardware::hidl_vec;
33using ::android::hardware::Return;
Robert Shih28c2ed32019-10-27 22:55:12 -070034
35namespace android {
36
37struct ICrypto;
38struct IDrm;
39
40namespace DrmUtils {
41
42bool UseDrmService();
43
44sp<IDrm> MakeDrm(status_t *pstatus = nullptr);
45
46sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
47
Robert Shihd3f9ba72019-11-20 17:25:26 -080048template<typename BA, typename PARCEL>
49void WriteByteArray(PARCEL &obj, const BA &vec) {
Robert Shih61e1c762019-10-31 21:26:58 -070050 obj.writeInt32(vec.size());
51 if (vec.size()) {
52 obj.write(vec.data(), vec.size());
53 }
54}
55
Robert Shihd3f9ba72019-11-20 17:25:26 -080056template<typename ET, typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -070057void WriteEventToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -080058 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -070059 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 Shihd3f9ba72019-11-20 17:25:26 -080067template<typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -070068void WriteExpirationUpdateToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -080069 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -070070 const BA &sessionId,
71 int64_t expiryTimeInMS) {
72 WriteByteArray(obj, sessionId);
73 obj.writeInt64(expiryTimeInMS);
74}
75
Robert Shihd3f9ba72019-11-20 17:25:26 -080076template<typename BA, typename KSL, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -070077void WriteKeysChange(
Robert Shihd3f9ba72019-11-20 17:25:26 -080078 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -070079 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 Shihc0d1d0e2019-11-24 13:21:04 -080091std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
Robert Shih5ff3ad62019-11-09 08:26:49 -080092
93std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
94 const char *appPackageName);
95
Robert Shih10fe9432019-11-09 08:26:49 -080096std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
97
98std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
99 const void *initData, size_t initDataSize);
100
Robert Shih5944a0b2021-02-10 04:26:33 -0800101status_t toStatusT_1_4(::V1_4::Status status);
102
103template<typename S>
104inline status_t toStatusT(S status) {
105 auto err = static_cast<::V1_4::Status>(status);
106 return toStatusT_1_4(err);
107}
108
109template<typename T>
110inline 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 Shih9afca952021-02-12 01:36:06 -0800116template<typename T, typename U>
117status_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 Shih28c2ed32019-10-27 22:55:12 -0700141} // namespace DrmUtils
Robert Shih28c2ed32019-10-27 22:55:12 -0700142} // namespace android
Robert Shih28c2ed32019-10-27 22:55:12 -0700143#endif // ANDROID_DRMUTILS_H