blob: 01fc7fbb6b34971e16aa891a0f240f0556a628ab [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 Naganovf83b9742023-04-24 13:06:04 -070029#include <media/AidlConversionNdkCpp.h>
30#include <media/AidlConversionUtil.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000031#include <utils/Log.h>
32
Shunkai Yao51202502022-12-12 06:11:46 +000033#include "DeviceHalAidl.h"
Shunkai Yaodca65ce2022-12-02 05:35:41 +000034#include "DevicesFactoryHalAidl.h"
35
Mikhail Naganovf83b9742023-04-24 13:06:04 -070036using aidl::android::aidl_utils::statusTFromBinderStatus;
37using aidl::android::hardware::audio::core::IConfig;
38using aidl::android::hardware::audio::core::IModule;
39using aidl::android::hardware::audio::core::SurroundSoundConfig;
Mikhail Naganove5011002024-01-26 10:57:19 -080040using aidl::android::hardware::audio::core::VendorParameter;
Mikhail Naganovf83b9742023-04-24 13:06:04 -070041using aidl::android::media::audio::common::AudioHalEngineConfig;
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070042using aidl::android::media::audio::IHalAdapterVendorExtension;
43using android::detail::AudioHalVersionInfo;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000044
45namespace android {
46
Mikhail Naganovf83b9742023-04-24 13:06:04 -070047namespace {
48
49ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily>
50ndk2cpp_SurroundSoundConfigFormatFamily(const SurroundSoundConfig::SurroundFormatFamily& ndk) {
51 media::SurroundSoundConfig::SurroundFormatFamily cpp;
52 cpp.primaryFormat = VALUE_OR_RETURN(ndk2cpp_AudioFormatDescription(ndk.primaryFormat));
53 cpp.subFormats = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
54 media::audio::common::AudioFormatDescription>>(ndk.subFormats,
55 ndk2cpp_AudioFormatDescription));
56 return cpp;
57}
58
59ConversionResult<media::SurroundSoundConfig>
60ndk2cpp_SurroundSoundConfig(const SurroundSoundConfig& ndk) {
61 media::SurroundSoundConfig cpp;
62 cpp.formatFamilies = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
63 media::SurroundSoundConfig::SurroundFormatFamily>>(ndk.formatFamilies,
64 ndk2cpp_SurroundSoundConfigFormatFamily));
65 return cpp;
66}
67
Mikhail Naganove5011002024-01-26 10:57:19 -080068class HalAdapterVendorExtensionWrapper :
69 public ::aidl::android::media::audio::BnHalAdapterVendorExtension {
70 private:
71 template<typename F>
72 ndk::ScopedAStatus callWithRetryOnCrash(F method) {
73 ndk::ScopedAStatus status = ndk::ScopedAStatus::ok();
74 for (auto service = getService(); service != nullptr; service = getService(true)) {
75 status = method(service);
76 if (status.getStatus() != STATUS_DEAD_OBJECT) break;
77 }
78 return status;
79 }
80
81 ndk::ScopedAStatus parseVendorParameterIds(ParameterScope in_scope,
82 const std::string& in_rawKeys,
83 std::vector<std::string>* _aidl_return) override {
84 return callWithRetryOnCrash([&](auto service) {
85 return service->parseVendorParameterIds(in_scope, in_rawKeys, _aidl_return);
86 });
87 }
88
89 ndk::ScopedAStatus parseVendorParameters(
90 ParameterScope in_scope, const std::string& in_rawKeysAndValues,
91 std::vector<VendorParameter>* out_syncParameters,
92 std::vector<VendorParameter>* out_asyncParameters) override {
93 return callWithRetryOnCrash([&](auto service) {
94 return service->parseVendorParameters(in_scope, in_rawKeysAndValues,
95 out_syncParameters, out_asyncParameters);
96 });
97 }
98
99 ndk::ScopedAStatus parseBluetoothA2dpReconfigureOffload(
100 const std::string& in_rawValue, std::vector<VendorParameter>* _aidl_return) override {
101 return callWithRetryOnCrash([&](auto service) {
102 return service->parseBluetoothA2dpReconfigureOffload(in_rawValue, _aidl_return);
103 });
104 }
105
106 ndk::ScopedAStatus parseBluetoothLeReconfigureOffload(const std::string& in_rawValue,
107 std::vector<VendorParameter>* _aidl_return) override {
108 return callWithRetryOnCrash([&](auto service) {
109 return service->parseBluetoothLeReconfigureOffload(in_rawValue, _aidl_return);
110 });
111 }
112
113 ndk::ScopedAStatus processVendorParameters(ParameterScope in_scope,
114 const std::vector<VendorParameter>& in_parameters,
115 std::string* _aidl_return) override {
116 return callWithRetryOnCrash([&](auto service) {
117 return service->processVendorParameters(in_scope, in_parameters, _aidl_return);
118 });
119 }
120
121 std::shared_ptr<IHalAdapterVendorExtension> getService(bool reset = false) {
122 std::lock_guard l(mLock);
123 if (reset || !mVendorExt.has_value()) {
124 auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
125 if (AServiceManager_isDeclared(serviceName.c_str())) {
126 mVendorExt = std::shared_ptr<IHalAdapterVendorExtension>(
127 IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
128 AServiceManager_waitForService(serviceName.c_str()))));
129 } else {
130 mVendorExt = nullptr;
131 }
132 }
133 return mVendorExt.value();
134 }
135
136 std::mutex mLock;
137 std::optional<std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>>
138 mVendorExt GUARDED_BY(mLock);
139};
140
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700141} // namespace
142
143DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config)
Mikhail Naganove5011002024-01-26 10:57:19 -0800144 : mConfig(std::move(config)),
145 mVendorExt(ndk::SharedRefBase::make<HalAdapterVendorExtensionWrapper>()) {
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700146}
147
148status_t DevicesFactoryHalAidl::getDeviceNames(std::vector<std::string> *names) {
149 if (names == nullptr) {
150 return BAD_VALUE;
151 }
152 AServiceManager_forEachDeclaredInstance(IModule::descriptor, static_cast<void*>(names),
153 [](const char* instance, void* context) {
154 if (strcmp(instance, "default") == 0) instance = "primary";
155 static_cast<decltype(names)>(context)->push_back(instance);
156 });
Mikhail Naganov1f8cf3d2023-08-04 14:42:14 -0700157 std::sort(names->begin(), names->end(), [](const std::string& lhs,
158 const std::string& rhs) {
159 // This order corresponds to the canonical order of modules as specified in
160 // the reference 'audio_policy_configuration_7_0.xml' file.
161 static const std::map<std::string, int> kPriorities{
162 { "primary", 0 }, { "a2dp", 1 }, { "usb", 2 }, { "r_submix", 3 },
163 { "bluetooth", 4 }, { "hearing_aid", 5 }, { "msd", 6 }, { "stub", 7 }
164 };
165 auto lhsIt = kPriorities.find(lhs);
166 auto rhsIt = kPriorities.find(rhs);
167 if (lhsIt != kPriorities.end() && rhsIt != kPriorities.end()) {
168 return lhsIt->second < rhsIt->second;
169 }
170 return lhsIt != kPriorities.end();
171 });
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700172 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000173}
174
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000175// Opens a device with the specified name. To close the device, it is
176// necessary to release references to the returned object.
177status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
178 if (name == nullptr || device == nullptr) {
179 return BAD_VALUE;
180 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000181 std::shared_ptr<IModule> service;
Mikhail Naganov0ee6cb92023-05-15 13:21:11 -0700182 if (strcmp(name, "primary") == 0) name = "default";
183 auto serviceName = std::string(IModule::descriptor) + "/" + name;
184 service = IModule::fromBinder(
185 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
186 if (service == nullptr) {
187 ALOGE("%s fromBinder %s failed", __func__, serviceName.c_str());
188 return NO_INIT;
Shunkai Yao51202502022-12-12 06:11:46 +0000189 }
Mikhail Naganove5011002024-01-26 10:57:19 -0800190 *device = sp<DeviceHalAidl>::make(name, service, mVendorExt);
Shunkai Yao51202502022-12-12 06:11:46 +0000191 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000192}
193
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000194status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000195 // Dynamic registration of module instances is not supported. The functionality
196 // in the audio server which is related to this callback can be removed together
197 // with HIDL support.
198 ALOG_ASSERT(callback != nullptr);
199 if (callback != nullptr) {
200 callback->onNewDevicesAvailable();
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000201 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000202 return NO_ERROR;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000203}
204
205AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const {
206 int32_t versionNumber = 0;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700207 if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) {
208 ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str());
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000209 }
210 // AIDL does not have minor version, fill 0 for all versions
211 return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber);
212}
213
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700214status_t DevicesFactoryHalAidl::getSurroundSoundConfig(media::SurroundSoundConfig *config) {
215 SurroundSoundConfig ndkConfig;
216 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getSurroundSoundConfig(&ndkConfig)));
217 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_SurroundSoundConfig(ndkConfig));
218 return OK;
219}
220
221status_t DevicesFactoryHalAidl::getEngineConfig(
222 media::audio::common::AudioHalEngineConfig *config) {
223 AudioHalEngineConfig ndkConfig;
224 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getEngineConfig(&ndkConfig)));
225 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_AudioHalEngineConfig(ndkConfig));
226 return OK;
227}
228
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000229// Main entry-point to the shared library.
230extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Shunkai Yao51202502022-12-12 06:11:46 +0000231 auto serviceName = std::string(IConfig::descriptor) + "/default";
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000232 auto service = IConfig::fromBinder(
Shunkai Yao51202502022-12-12 06:11:46 +0000233 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
234 if (!service) {
235 ALOGE("%s binder service %s not exist", __func__, serviceName.c_str());
236 return nullptr;
237 }
238 return new DevicesFactoryHalAidl(service);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000239}
240
241} // namespace android