blob: 68b650f3bed6c736d0e73fe6c8e2aac379a0edf4 [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
Mikhail Naganovd8f24412024-03-20 18:15:42 -070034#include "AidlUtils.h"
Shunkai Yao51202502022-12-12 06:11:46 +000035#include "DeviceHalAidl.h"
Shunkai Yaodca65ce2022-12-02 05:35:41 +000036#include "DevicesFactoryHalAidl.h"
37
Mikhail Naganovf83b9742023-04-24 13:06:04 -070038using aidl::android::aidl_utils::statusTFromBinderStatus;
39using aidl::android::hardware::audio::core::IConfig;
40using aidl::android::hardware::audio::core::IModule;
41using aidl::android::hardware::audio::core::SurroundSoundConfig;
Mikhail Naganove5011002024-01-26 10:57:19 -080042using aidl::android::hardware::audio::core::VendorParameter;
Mikhail Naganovf83b9742023-04-24 13:06:04 -070043using aidl::android::media::audio::common::AudioHalEngineConfig;
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070044using aidl::android::media::audio::IHalAdapterVendorExtension;
45using android::detail::AudioHalVersionInfo;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000046
47namespace android {
48
Mikhail Naganovf83b9742023-04-24 13:06:04 -070049namespace {
50
51ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily>
52ndk2cpp_SurroundSoundConfigFormatFamily(const SurroundSoundConfig::SurroundFormatFamily& ndk) {
53 media::SurroundSoundConfig::SurroundFormatFamily cpp;
54 cpp.primaryFormat = VALUE_OR_RETURN(ndk2cpp_AudioFormatDescription(ndk.primaryFormat));
55 cpp.subFormats = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
56 media::audio::common::AudioFormatDescription>>(ndk.subFormats,
57 ndk2cpp_AudioFormatDescription));
58 return cpp;
59}
60
61ConversionResult<media::SurroundSoundConfig>
62ndk2cpp_SurroundSoundConfig(const SurroundSoundConfig& ndk) {
63 media::SurroundSoundConfig cpp;
64 cpp.formatFamilies = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
65 media::SurroundSoundConfig::SurroundFormatFamily>>(ndk.formatFamilies,
66 ndk2cpp_SurroundSoundConfigFormatFamily));
67 return cpp;
68}
69
Mikhail Naganove5011002024-01-26 10:57:19 -080070class HalAdapterVendorExtensionWrapper :
71 public ::aidl::android::media::audio::BnHalAdapterVendorExtension {
72 private:
73 template<typename F>
74 ndk::ScopedAStatus callWithRetryOnCrash(F method) {
75 ndk::ScopedAStatus status = ndk::ScopedAStatus::ok();
76 for (auto service = getService(); service != nullptr; service = getService(true)) {
77 status = method(service);
78 if (status.getStatus() != STATUS_DEAD_OBJECT) break;
79 }
80 return status;
81 }
82
83 ndk::ScopedAStatus parseVendorParameterIds(ParameterScope in_scope,
84 const std::string& in_rawKeys,
85 std::vector<std::string>* _aidl_return) override {
86 return callWithRetryOnCrash([&](auto service) {
87 return service->parseVendorParameterIds(in_scope, in_rawKeys, _aidl_return);
88 });
89 }
90
91 ndk::ScopedAStatus parseVendorParameters(
92 ParameterScope in_scope, const std::string& in_rawKeysAndValues,
93 std::vector<VendorParameter>* out_syncParameters,
94 std::vector<VendorParameter>* out_asyncParameters) override {
95 return callWithRetryOnCrash([&](auto service) {
96 return service->parseVendorParameters(in_scope, in_rawKeysAndValues,
97 out_syncParameters, out_asyncParameters);
98 });
99 }
100
101 ndk::ScopedAStatus parseBluetoothA2dpReconfigureOffload(
102 const std::string& in_rawValue, std::vector<VendorParameter>* _aidl_return) override {
103 return callWithRetryOnCrash([&](auto service) {
104 return service->parseBluetoothA2dpReconfigureOffload(in_rawValue, _aidl_return);
105 });
106 }
107
108 ndk::ScopedAStatus parseBluetoothLeReconfigureOffload(const std::string& in_rawValue,
109 std::vector<VendorParameter>* _aidl_return) override {
110 return callWithRetryOnCrash([&](auto service) {
111 return service->parseBluetoothLeReconfigureOffload(in_rawValue, _aidl_return);
112 });
113 }
114
115 ndk::ScopedAStatus processVendorParameters(ParameterScope in_scope,
116 const std::vector<VendorParameter>& in_parameters,
117 std::string* _aidl_return) override {
118 return callWithRetryOnCrash([&](auto service) {
119 return service->processVendorParameters(in_scope, in_parameters, _aidl_return);
120 });
121 }
122
123 std::shared_ptr<IHalAdapterVendorExtension> getService(bool reset = false) {
124 std::lock_guard l(mLock);
125 if (reset || !mVendorExt.has_value()) {
Mikhail Naganov69dac992024-02-14 18:53:29 +0000126 if (property_get_bool("ro.audio.ihaladaptervendorextension_enabled", false)) {
127 auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
Mikhail Naganove5011002024-01-26 10:57:19 -0800128 mVendorExt = std::shared_ptr<IHalAdapterVendorExtension>(
129 IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
130 AServiceManager_waitForService(serviceName.c_str()))));
131 } else {
132 mVendorExt = nullptr;
133 }
134 }
135 return mVendorExt.value();
136 }
137
138 std::mutex mLock;
139 std::optional<std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>>
140 mVendorExt GUARDED_BY(mLock);
141};
142
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700143} // namespace
144
145DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config)
Mikhail Naganove5011002024-01-26 10:57:19 -0800146 : mConfig(std::move(config)),
147 mVendorExt(ndk::SharedRefBase::make<HalAdapterVendorExtensionWrapper>()) {
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700148}
149
150status_t DevicesFactoryHalAidl::getDeviceNames(std::vector<std::string> *names) {
151 if (names == nullptr) {
152 return BAD_VALUE;
153 }
154 AServiceManager_forEachDeclaredInstance(IModule::descriptor, static_cast<void*>(names),
155 [](const char* instance, void* context) {
156 if (strcmp(instance, "default") == 0) instance = "primary";
157 static_cast<decltype(names)>(context)->push_back(instance);
158 });
Mikhail Naganov1f8cf3d2023-08-04 14:42:14 -0700159 std::sort(names->begin(), names->end(), [](const std::string& lhs,
160 const std::string& rhs) {
161 // This order corresponds to the canonical order of modules as specified in
162 // the reference 'audio_policy_configuration_7_0.xml' file.
163 static const std::map<std::string, int> kPriorities{
164 { "primary", 0 }, { "a2dp", 1 }, { "usb", 2 }, { "r_submix", 3 },
165 { "bluetooth", 4 }, { "hearing_aid", 5 }, { "msd", 6 }, { "stub", 7 }
166 };
167 auto lhsIt = kPriorities.find(lhs);
168 auto rhsIt = kPriorities.find(rhs);
169 if (lhsIt != kPriorities.end() && rhsIt != kPriorities.end()) {
170 return lhsIt->second < rhsIt->second;
171 }
172 return lhsIt != kPriorities.end();
173 });
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700174 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000175}
176
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000177// Opens a device with the specified name. To close the device, it is
178// necessary to release references to the returned object.
179status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
180 if (name == nullptr || device == nullptr) {
181 return BAD_VALUE;
182 }
Mikhail Naganov0ee6cb92023-05-15 13:21:11 -0700183 if (strcmp(name, "primary") == 0) name = "default";
Mikhail Naganovd8f24412024-03-20 18:15:42 -0700184 *device = sp<DeviceHalAidl>::make(name, getServiceInstance<IModule>(name), mVendorExt);
Shunkai Yao51202502022-12-12 06:11:46 +0000185 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000186}
187
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000188status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000189 // Dynamic registration of module instances is not supported. The functionality
190 // in the audio server which is related to this callback can be removed together
191 // with HIDL support.
192 ALOG_ASSERT(callback != nullptr);
193 if (callback != nullptr) {
194 callback->onNewDevicesAvailable();
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000195 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000196 return NO_ERROR;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000197}
198
199AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const {
200 int32_t versionNumber = 0;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700201 if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) {
202 ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str());
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000203 }
204 // AIDL does not have minor version, fill 0 for all versions
205 return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber);
206}
207
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700208status_t DevicesFactoryHalAidl::getSurroundSoundConfig(media::SurroundSoundConfig *config) {
209 SurroundSoundConfig ndkConfig;
210 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getSurroundSoundConfig(&ndkConfig)));
211 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_SurroundSoundConfig(ndkConfig));
212 return OK;
213}
214
215status_t DevicesFactoryHalAidl::getEngineConfig(
216 media::audio::common::AudioHalEngineConfig *config) {
217 AudioHalEngineConfig ndkConfig;
218 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getEngineConfig(&ndkConfig)));
219 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_AudioHalEngineConfig(ndkConfig));
220 return OK;
221}
222
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000223// Main entry-point to the shared library.
224extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Mikhail Naganovd8f24412024-03-20 18:15:42 -0700225 return new DevicesFactoryHalAidl(getServiceInstance<IConfig>("default"));
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000226}
227
228} // namespace android