blob: 347afa60be7715c37182d5d9045952e5ab2b7a10 [file] [log] [blame]
Shunkai Yaodca65ce2022-12-02 05:35:41 +00001/*
2 * Copyright (C) 2022 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
Mikhail Naganov1f8cf3d2023-08-04 14:42:14 -070017#include <algorithm>
18#include <map>
Mikhail Naganovf83b9742023-04-24 13:06:04 -070019#include <memory>
Mikhail Naganove5011002024-01-26 10:57:19 -080020#include <mutex>
Mikhail Naganov1f8cf3d2023-08-04 14:42:14 -070021#include <string>
Mikhail Naganovf83b9742023-04-24 13:06:04 -070022
Shunkai Yaodca65ce2022-12-02 05:35:41 +000023#define LOG_TAG "DevicesFactoryHalAidl"
24//#define LOG_NDEBUG 0
25
Shunkai Yao51202502022-12-12 06:11:46 +000026#include <aidl/android/hardware/audio/core/IModule.h>
Mikhail Naganove5011002024-01-26 10:57:19 -080027#include <aidl/android/media/audio/BnHalAdapterVendorExtension.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000028#include <android/binder_manager.h>
Mikhail Naganov69dac992024-02-14 18:53:29 +000029#include <cutils/properties.h>
Mikhail Naganovf83b9742023-04-24 13:06:04 -070030#include <media/AidlConversionNdkCpp.h>
31#include <media/AidlConversionUtil.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000032#include <utils/Log.h>
33
Shunkai Yao51202502022-12-12 06:11:46 +000034#include "DeviceHalAidl.h"
Shunkai Yaodca65ce2022-12-02 05:35:41 +000035#include "DevicesFactoryHalAidl.h"
36
Mikhail Naganovf83b9742023-04-24 13:06:04 -070037using aidl::android::aidl_utils::statusTFromBinderStatus;
38using aidl::android::hardware::audio::core::IConfig;
39using aidl::android::hardware::audio::core::IModule;
40using aidl::android::hardware::audio::core::SurroundSoundConfig;
Mikhail Naganove5011002024-01-26 10:57:19 -080041using aidl::android::hardware::audio::core::VendorParameter;
Mikhail Naganovf83b9742023-04-24 13:06:04 -070042using aidl::android::media::audio::common::AudioHalEngineConfig;
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070043using aidl::android::media::audio::IHalAdapterVendorExtension;
44using android::detail::AudioHalVersionInfo;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000045
46namespace android {
47
Mikhail Naganovf83b9742023-04-24 13:06:04 -070048namespace {
49
50ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily>
51ndk2cpp_SurroundSoundConfigFormatFamily(const SurroundSoundConfig::SurroundFormatFamily& ndk) {
52 media::SurroundSoundConfig::SurroundFormatFamily cpp;
53 cpp.primaryFormat = VALUE_OR_RETURN(ndk2cpp_AudioFormatDescription(ndk.primaryFormat));
54 cpp.subFormats = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
55 media::audio::common::AudioFormatDescription>>(ndk.subFormats,
56 ndk2cpp_AudioFormatDescription));
57 return cpp;
58}
59
60ConversionResult<media::SurroundSoundConfig>
61ndk2cpp_SurroundSoundConfig(const SurroundSoundConfig& ndk) {
62 media::SurroundSoundConfig cpp;
63 cpp.formatFamilies = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
64 media::SurroundSoundConfig::SurroundFormatFamily>>(ndk.formatFamilies,
65 ndk2cpp_SurroundSoundConfigFormatFamily));
66 return cpp;
67}
68
Mikhail Naganove5011002024-01-26 10:57:19 -080069class HalAdapterVendorExtensionWrapper :
70 public ::aidl::android::media::audio::BnHalAdapterVendorExtension {
71 private:
72 template<typename F>
73 ndk::ScopedAStatus callWithRetryOnCrash(F method) {
74 ndk::ScopedAStatus status = ndk::ScopedAStatus::ok();
75 for (auto service = getService(); service != nullptr; service = getService(true)) {
76 status = method(service);
77 if (status.getStatus() != STATUS_DEAD_OBJECT) break;
78 }
79 return status;
80 }
81
82 ndk::ScopedAStatus parseVendorParameterIds(ParameterScope in_scope,
83 const std::string& in_rawKeys,
84 std::vector<std::string>* _aidl_return) override {
85 return callWithRetryOnCrash([&](auto service) {
86 return service->parseVendorParameterIds(in_scope, in_rawKeys, _aidl_return);
87 });
88 }
89
90 ndk::ScopedAStatus parseVendorParameters(
91 ParameterScope in_scope, const std::string& in_rawKeysAndValues,
92 std::vector<VendorParameter>* out_syncParameters,
93 std::vector<VendorParameter>* out_asyncParameters) override {
94 return callWithRetryOnCrash([&](auto service) {
95 return service->parseVendorParameters(in_scope, in_rawKeysAndValues,
96 out_syncParameters, out_asyncParameters);
97 });
98 }
99
100 ndk::ScopedAStatus parseBluetoothA2dpReconfigureOffload(
101 const std::string& in_rawValue, std::vector<VendorParameter>* _aidl_return) override {
102 return callWithRetryOnCrash([&](auto service) {
103 return service->parseBluetoothA2dpReconfigureOffload(in_rawValue, _aidl_return);
104 });
105 }
106
107 ndk::ScopedAStatus parseBluetoothLeReconfigureOffload(const std::string& in_rawValue,
108 std::vector<VendorParameter>* _aidl_return) override {
109 return callWithRetryOnCrash([&](auto service) {
110 return service->parseBluetoothLeReconfigureOffload(in_rawValue, _aidl_return);
111 });
112 }
113
114 ndk::ScopedAStatus processVendorParameters(ParameterScope in_scope,
115 const std::vector<VendorParameter>& in_parameters,
116 std::string* _aidl_return) override {
117 return callWithRetryOnCrash([&](auto service) {
118 return service->processVendorParameters(in_scope, in_parameters, _aidl_return);
119 });
120 }
121
122 std::shared_ptr<IHalAdapterVendorExtension> getService(bool reset = false) {
123 std::lock_guard l(mLock);
124 if (reset || !mVendorExt.has_value()) {
Mikhail Naganov69dac992024-02-14 18:53:29 +0000125 if (property_get_bool("ro.audio.ihaladaptervendorextension_enabled", false)) {
126 auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
Mikhail Naganove5011002024-01-26 10:57:19 -0800127 mVendorExt = std::shared_ptr<IHalAdapterVendorExtension>(
128 IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
129 AServiceManager_waitForService(serviceName.c_str()))));
130 } else {
131 mVendorExt = nullptr;
132 }
133 }
134 return mVendorExt.value();
135 }
136
137 std::mutex mLock;
138 std::optional<std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>>
139 mVendorExt GUARDED_BY(mLock);
140};
141
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700142} // namespace
143
144DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config)
Mikhail Naganove5011002024-01-26 10:57:19 -0800145 : mConfig(std::move(config)),
146 mVendorExt(ndk::SharedRefBase::make<HalAdapterVendorExtensionWrapper>()) {
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700147}
148
149status_t DevicesFactoryHalAidl::getDeviceNames(std::vector<std::string> *names) {
150 if (names == nullptr) {
151 return BAD_VALUE;
152 }
153 AServiceManager_forEachDeclaredInstance(IModule::descriptor, static_cast<void*>(names),
154 [](const char* instance, void* context) {
155 if (strcmp(instance, "default") == 0) instance = "primary";
156 static_cast<decltype(names)>(context)->push_back(instance);
157 });
Mikhail Naganov1f8cf3d2023-08-04 14:42:14 -0700158 std::sort(names->begin(), names->end(), [](const std::string& lhs,
159 const std::string& rhs) {
160 // This order corresponds to the canonical order of modules as specified in
161 // the reference 'audio_policy_configuration_7_0.xml' file.
162 static const std::map<std::string, int> kPriorities{
163 { "primary", 0 }, { "a2dp", 1 }, { "usb", 2 }, { "r_submix", 3 },
164 { "bluetooth", 4 }, { "hearing_aid", 5 }, { "msd", 6 }, { "stub", 7 }
165 };
166 auto lhsIt = kPriorities.find(lhs);
167 auto rhsIt = kPriorities.find(rhs);
168 if (lhsIt != kPriorities.end() && rhsIt != kPriorities.end()) {
169 return lhsIt->second < rhsIt->second;
170 }
171 return lhsIt != kPriorities.end();
172 });
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700173 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000174}
175
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000176// Opens a device with the specified name. To close the device, it is
177// necessary to release references to the returned object.
178status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
179 if (name == nullptr || device == nullptr) {
180 return BAD_VALUE;
181 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000182 std::shared_ptr<IModule> service;
Mikhail Naganov0ee6cb92023-05-15 13:21:11 -0700183 if (strcmp(name, "primary") == 0) name = "default";
184 auto serviceName = std::string(IModule::descriptor) + "/" + name;
185 service = IModule::fromBinder(
186 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
187 if (service == nullptr) {
188 ALOGE("%s fromBinder %s failed", __func__, serviceName.c_str());
189 return NO_INIT;
Shunkai Yao51202502022-12-12 06:11:46 +0000190 }
Mikhail Naganove5011002024-01-26 10:57:19 -0800191 *device = sp<DeviceHalAidl>::make(name, service, mVendorExt);
Shunkai Yao51202502022-12-12 06:11:46 +0000192 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000193}
194
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000195status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000196 // Dynamic registration of module instances is not supported. The functionality
197 // in the audio server which is related to this callback can be removed together
198 // with HIDL support.
199 ALOG_ASSERT(callback != nullptr);
200 if (callback != nullptr) {
201 callback->onNewDevicesAvailable();
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000202 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000203 return NO_ERROR;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000204}
205
206AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const {
207 int32_t versionNumber = 0;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700208 if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) {
209 ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str());
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000210 }
211 // AIDL does not have minor version, fill 0 for all versions
212 return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber);
213}
214
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700215status_t DevicesFactoryHalAidl::getSurroundSoundConfig(media::SurroundSoundConfig *config) {
216 SurroundSoundConfig ndkConfig;
217 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getSurroundSoundConfig(&ndkConfig)));
218 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_SurroundSoundConfig(ndkConfig));
219 return OK;
220}
221
222status_t DevicesFactoryHalAidl::getEngineConfig(
223 media::audio::common::AudioHalEngineConfig *config) {
224 AudioHalEngineConfig ndkConfig;
225 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getEngineConfig(&ndkConfig)));
226 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_AudioHalEngineConfig(ndkConfig));
227 return OK;
228}
229
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000230// Main entry-point to the shared library.
231extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Shunkai Yao51202502022-12-12 06:11:46 +0000232 auto serviceName = std::string(IConfig::descriptor) + "/default";
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000233 auto service = IConfig::fromBinder(
Shunkai Yao51202502022-12-12 06:11:46 +0000234 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
235 if (!service) {
236 ALOGE("%s binder service %s not exist", __func__, serviceName.c_str());
237 return nullptr;
238 }
239 return new DevicesFactoryHalAidl(service);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000240}
241
242} // namespace android