Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 1 | /* |
| 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 Naganov | 1f8cf3d | 2023-08-04 14:42:14 -0700 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | #include <map> |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 19 | #include <memory> |
Mikhail Naganov | e501100 | 2024-01-26 10:57:19 -0800 | [diff] [blame] | 20 | #include <mutex> |
Mikhail Naganov | 1f8cf3d | 2023-08-04 14:42:14 -0700 | [diff] [blame] | 21 | #include <string> |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 22 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 23 | #define LOG_TAG "DevicesFactoryHalAidl" |
| 24 | //#define LOG_NDEBUG 0 |
| 25 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 26 | #include <aidl/android/hardware/audio/core/IModule.h> |
Mikhail Naganov | e501100 | 2024-01-26 10:57:19 -0800 | [diff] [blame] | 27 | #include <aidl/android/media/audio/BnHalAdapterVendorExtension.h> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 28 | #include <android/binder_manager.h> |
Mikhail Naganov | 69dac99 | 2024-02-14 18:53:29 +0000 | [diff] [blame] | 29 | #include <cutils/properties.h> |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 30 | #include <media/AidlConversionNdkCpp.h> |
| 31 | #include <media/AidlConversionUtil.h> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 32 | #include <utils/Log.h> |
| 33 | |
Mikhail Naganov | d8f2441 | 2024-03-20 18:15:42 -0700 | [diff] [blame] | 34 | #include "AidlUtils.h" |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 35 | #include "DeviceHalAidl.h" |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 36 | #include "DevicesFactoryHalAidl.h" |
| 37 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 38 | using aidl::android::aidl_utils::statusTFromBinderStatus; |
| 39 | using aidl::android::hardware::audio::core::IConfig; |
| 40 | using aidl::android::hardware::audio::core::IModule; |
| 41 | using aidl::android::hardware::audio::core::SurroundSoundConfig; |
Mikhail Naganov | e501100 | 2024-01-26 10:57:19 -0800 | [diff] [blame] | 42 | using aidl::android::hardware::audio::core::VendorParameter; |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 43 | using aidl::android::media::audio::common::AudioHalEngineConfig; |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 44 | using aidl::android::media::audio::IHalAdapterVendorExtension; |
| 45 | using android::detail::AudioHalVersionInfo; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 46 | |
| 47 | namespace android { |
| 48 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily> |
| 52 | ndk2cpp_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 | |
| 61 | ConversionResult<media::SurroundSoundConfig> |
| 62 | ndk2cpp_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 Naganov | e501100 | 2024-01-26 10:57:19 -0800 | [diff] [blame] | 70 | class 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 Naganov | 69dac99 | 2024-02-14 18:53:29 +0000 | [diff] [blame] | 126 | if (property_get_bool("ro.audio.ihaladaptervendorextension_enabled", false)) { |
| 127 | auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default"; |
Mikhail Naganov | e501100 | 2024-01-26 10:57:19 -0800 | [diff] [blame] | 128 | 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 Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 143 | } // namespace |
| 144 | |
| 145 | DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config) |
Mikhail Naganov | e501100 | 2024-01-26 10:57:19 -0800 | [diff] [blame] | 146 | : mConfig(std::move(config)), |
| 147 | mVendorExt(ndk::SharedRefBase::make<HalAdapterVendorExtensionWrapper>()) { |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | status_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 Naganov | 1f8cf3d | 2023-08-04 14:42:14 -0700 | [diff] [blame] | 159 | 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 Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 174 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 177 | // Opens a device with the specified name. To close the device, it is |
| 178 | // necessary to release references to the returned object. |
| 179 | status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { |
| 180 | if (name == nullptr || device == nullptr) { |
| 181 | return BAD_VALUE; |
| 182 | } |
Mikhail Naganov | 0ee6cb9 | 2023-05-15 13:21:11 -0700 | [diff] [blame] | 183 | if (strcmp(name, "primary") == 0) name = "default"; |
Mikhail Naganov | d8f2441 | 2024-03-20 18:15:42 -0700 | [diff] [blame] | 184 | *device = sp<DeviceHalAidl>::make(name, getServiceInstance<IModule>(name), mVendorExt); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 185 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 188 | status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 189 | // 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 Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 195 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 196 | return NO_ERROR; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const { |
| 200 | int32_t versionNumber = 0; |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 201 | if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) { |
| 202 | ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str()); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 203 | } |
| 204 | // AIDL does not have minor version, fill 0 for all versions |
| 205 | return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber); |
| 206 | } |
| 207 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 208 | status_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 | |
| 215 | status_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 Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 223 | // Main entry-point to the shared library. |
| 224 | extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() { |
Mikhail Naganov | d8f2441 | 2024-03-20 18:15:42 -0700 | [diff] [blame] | 225 | return new DevicesFactoryHalAidl(getServiceInstance<IConfig>("default")); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | } // namespace android |