blob: 24df32dbf6eacf6473dba7c05f519d9c77b06eb9 [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 Naganov1f8cf3d2023-08-04 14:42:14 -070020#include <string>
Mikhail Naganovf83b9742023-04-24 13:06:04 -070021
Shunkai Yaodca65ce2022-12-02 05:35:41 +000022#define LOG_TAG "DevicesFactoryHalAidl"
23//#define LOG_NDEBUG 0
24
Shunkai Yao51202502022-12-12 06:11:46 +000025#include <aidl/android/hardware/audio/core/IModule.h>
Mikhail Naganovc77d1842024-01-26 10:57:19 -080026#include <aidl/android/media/audio/BnHalAdapterVendorExtension.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000027#include <android/binder_manager.h>
Mikhail Naganovf83b9742023-04-24 13:06:04 -070028#include <media/AidlConversionNdkCpp.h>
29#include <media/AidlConversionUtil.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000030#include <utils/Log.h>
31
Shunkai Yao51202502022-12-12 06:11:46 +000032#include "DeviceHalAidl.h"
Shunkai Yaodca65ce2022-12-02 05:35:41 +000033#include "DevicesFactoryHalAidl.h"
34
Mikhail Naganovf83b9742023-04-24 13:06:04 -070035using aidl::android::aidl_utils::statusTFromBinderStatus;
36using aidl::android::hardware::audio::core::IConfig;
37using aidl::android::hardware::audio::core::IModule;
38using aidl::android::hardware::audio::core::SurroundSoundConfig;
Mikhail Naganovc77d1842024-01-26 10:57:19 -080039using aidl::android::hardware::audio::core::VendorParameter;
Mikhail Naganovf83b9742023-04-24 13:06:04 -070040using aidl::android::media::audio::common::AudioHalEngineConfig;
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070041using aidl::android::media::audio::IHalAdapterVendorExtension;
42using android::detail::AudioHalVersionInfo;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000043
44namespace android {
45
Mikhail Naganovf83b9742023-04-24 13:06:04 -070046namespace {
47
48ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily>
49ndk2cpp_SurroundSoundConfigFormatFamily(const SurroundSoundConfig::SurroundFormatFamily& ndk) {
50 media::SurroundSoundConfig::SurroundFormatFamily cpp;
51 cpp.primaryFormat = VALUE_OR_RETURN(ndk2cpp_AudioFormatDescription(ndk.primaryFormat));
52 cpp.subFormats = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
53 media::audio::common::AudioFormatDescription>>(ndk.subFormats,
54 ndk2cpp_AudioFormatDescription));
55 return cpp;
56}
57
58ConversionResult<media::SurroundSoundConfig>
59ndk2cpp_SurroundSoundConfig(const SurroundSoundConfig& ndk) {
60 media::SurroundSoundConfig cpp;
61 cpp.formatFamilies = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
62 media::SurroundSoundConfig::SurroundFormatFamily>>(ndk.formatFamilies,
63 ndk2cpp_SurroundSoundConfigFormatFamily));
64 return cpp;
65}
66
Mikhail Naganovc77d1842024-01-26 10:57:19 -080067class HalAdapterVendorExtensionWrapper :
68 public ::aidl::android::media::audio::BnHalAdapterVendorExtension {
69 private:
70 ndk::ScopedAStatus parseVendorParameterIds(ParameterScope in_scope,
71 const std::string& in_rawKeys,
72 std::vector<std::string>* _aidl_return) override {
73 ndk::ScopedAStatus status;
74 while (true) {
75 status = get()->parseVendorParameterIds(in_scope, in_rawKeys, _aidl_return);
76 if (status.getStatus() != STATUS_DEAD_OBJECT) break;
77 mVendorExt.reset();
78 }
79 return status;
80 }
81
82 ndk::ScopedAStatus parseVendorParameters(
83 ParameterScope in_scope, const std::string& in_rawKeysAndValues,
84 std::vector<VendorParameter>* out_syncParameters,
85 std::vector<VendorParameter>* out_asyncParameters) override {
86 ndk::ScopedAStatus status;
87 while (true) {
88 status = get()->parseVendorParameters(in_scope, in_rawKeysAndValues,
89 out_syncParameters, out_asyncParameters);
90 if (status.getStatus() != STATUS_DEAD_OBJECT) break;
91 mVendorExt.reset();
92 }
93 return status;
94 }
95
96 ndk::ScopedAStatus parseBluetoothA2dpReconfigureOffload(
97 const std::string& in_rawValue, std::vector<VendorParameter>* _aidl_return) override {
98 ndk::ScopedAStatus status;
99 while (true) {
100 status = get()->parseBluetoothA2dpReconfigureOffload(in_rawValue, _aidl_return);
101 if (status.getStatus() != STATUS_DEAD_OBJECT) break;
102 mVendorExt.reset();
103 }
104 return status;
105 }
106
107 ndk::ScopedAStatus parseBluetoothLeReconfigureOffload(const std::string& in_rawValue,
108 std::vector<VendorParameter>* _aidl_return) override {
109 ndk::ScopedAStatus status;
110 while (true) {
111 status = get()->parseBluetoothLeReconfigureOffload(in_rawValue, _aidl_return);
112 if (status.getStatus() != STATUS_DEAD_OBJECT) break;
113 mVendorExt.reset();
114 }
115 return status;
116 }
117
118 ndk::ScopedAStatus processVendorParameters(ParameterScope in_scope,
119 const std::vector<VendorParameter>& in_parameters,
120 std::string* _aidl_return) override {
121 ndk::ScopedAStatus status;
122 while (true) {
123 status = get()->processVendorParameters(in_scope, in_parameters, _aidl_return);
124 if (status.getStatus() != STATUS_DEAD_OBJECT) break;
125 mVendorExt.reset();
126 }
127 return status;
128 }
129
130 std::shared_ptr<IHalAdapterVendorExtension> get() {
131 if (!mVendorExt) {
132 auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
133 mVendorExt = IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
134 AServiceManager_waitForService(serviceName.c_str())));
135 }
136 return mVendorExt;
137 }
138
139 std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> mVendorExt;
140};
141
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700142} // namespace
143
144DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config)
Mikhail Naganovc77d1842024-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 Naganovc77d1842024-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