blob: 3a765918aebdead758e94ac4fae92e882a907709 [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//#define LOG_NDEBUG 0
18#define LOG_TAG "DrmUtils"
19
Robert Shih10fe9432019-11-09 08:26:49 -080020#include <android/hardware/drm/1.0/ICryptoFactory.h>
21#include <android/hardware/drm/1.0/ICryptoPlugin.h>
22#include <android/hardware/drm/1.1/ICryptoFactory.h>
23#include <android/hardware/drm/1.2/ICryptoFactory.h>
24#include <android/hidl/manager/1.0/IServiceManager.h>
25#include <hidl/HidlSupport.h>
26
27#include <utils/Errors.h>
28#include <utils/Log.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070029#include <utils/String16.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070030#include <cutils/properties.h>
31
32#include <mediadrm/CryptoHal.h>
33#include <mediadrm/DrmHal.h>
34#include <mediadrm/DrmUtils.h>
35#include <mediadrm/ICrypto.h>
36#include <mediadrm/IDrm.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070037
Robert Shih10fe9432019-11-09 08:26:49 -080038using HServiceManager = ::android::hidl::manager::V1_0::IServiceManager;
39using ::android::hardware::hidl_array;
40using ::android::hardware::hidl_string;
41using ::android::hardware::hidl_vec;
42using namespace ::android::hardware::drm;
43
Robert Shih28c2ed32019-10-27 22:55:12 -070044namespace android {
45namespace DrmUtils {
46
47namespace {
Robert Shih6571bf62019-11-10 15:03:01 -080048
49template<typename Hal>
50Hal *MakeObject(status_t *pstatus) {
Robert Shih28c2ed32019-10-27 22:55:12 -070051 status_t err = OK;
52 status_t &status = pstatus ? *pstatus : err;
Robert Shih6571bf62019-11-10 15:03:01 -080053 auto obj = new Hal();
Robert Shih28c2ed32019-10-27 22:55:12 -070054 status = obj->initCheck();
55 if (status != OK && status != NO_INIT) {
56 return NULL;
57 }
58 return obj;
59}
60
Robert Shih10fe9432019-11-09 08:26:49 -080061template <typename Hal, typename V>
62void MakeCryptoFactories(const uint8_t uuid[16], V &cryptoFactories) {
63 sp<HServiceManager> serviceManager = HServiceManager::getService();
64 if (serviceManager == nullptr) {
65 ALOGE("Failed to get service manager");
66 exit(-1);
67 }
68
69 serviceManager->listByInterface(Hal::descriptor, [&](const hidl_vec<hidl_string> &registered) {
70 for (const auto &instance : registered) {
71 auto factory = Hal::getService(instance);
72 if (factory != nullptr) {
73 ALOGI("found %s %s", Hal::descriptor, instance.c_str());
74 if (factory->isCryptoSchemeSupported(uuid)) {
75 cryptoFactories.push_back(factory);
76 }
77 }
78 }
79 });
80}
81
82hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) {
83 hidl_vec<uint8_t> vec(size);
84 if (ptr != nullptr) {
85 memcpy(vec.data(), ptr, size);
86 }
87 return vec;
88}
89
90hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) {
91 if (ptr == nullptr) {
92 return hidl_array<uint8_t, 16>();
93 }
94 return hidl_array<uint8_t, 16>(ptr);
95}
96
97sp<::V1_0::ICryptoPlugin> MakeCryptoPlugin(const sp<::V1_0::ICryptoFactory> &factory,
98 const uint8_t uuid[16], const void *initData,
99 size_t initDataSize) {
100 sp<::V1_0::ICryptoPlugin> plugin;
101 factory->createPlugin(toHidlArray16(uuid), toHidlVec(initData, initDataSize),
102 [&](::V1_0::Status status, const sp<::V1_0::ICryptoPlugin> &hPlugin) {
103 if (status != ::V1_0::Status::OK) {
104 return;
105 }
106 plugin = hPlugin;
107 });
108 return plugin;
109}
110
Robert Shih28c2ed32019-10-27 22:55:12 -0700111} // namespace
112
113bool UseDrmService() {
Robert Shih17c6d822019-11-07 11:31:43 -0800114 return property_get_bool("mediadrm.use_mediadrmserver", true);
Robert Shih28c2ed32019-10-27 22:55:12 -0700115}
116
117sp<IDrm> MakeDrm(status_t *pstatus) {
Robert Shih6571bf62019-11-10 15:03:01 -0800118 return MakeObject<DrmHal>(pstatus);
Robert Shih28c2ed32019-10-27 22:55:12 -0700119}
120
121sp<ICrypto> MakeCrypto(status_t *pstatus) {
Robert Shih6571bf62019-11-10 15:03:01 -0800122 return MakeObject<CryptoHal>(pstatus);
Robert Shih28c2ed32019-10-27 22:55:12 -0700123}
124
Robert Shih10fe9432019-11-09 08:26:49 -0800125std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]) {
126 std::vector<sp<::V1_0::ICryptoFactory>> cryptoFactories;
127 MakeCryptoFactories<::V1_0::ICryptoFactory>(uuid, cryptoFactories);
128 MakeCryptoFactories<::V1_1::ICryptoFactory>(uuid, cryptoFactories);
129 MakeCryptoFactories<::V1_2::ICryptoFactory>(uuid, cryptoFactories);
130 return cryptoFactories;
131}
132
133std::vector<sp<ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16], const void *initData,
134 size_t initDataSize) {
135 std::vector<sp<ICryptoPlugin>> plugins;
136 for (const auto &factory : MakeCryptoFactories(uuid)) {
137 plugins.push_back(MakeCryptoPlugin(factory, uuid, initData, initDataSize));
138 }
139 return plugins;
140}
141
Robert Shih28c2ed32019-10-27 22:55:12 -0700142} // namespace DrmUtils
143} // namespace android