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 | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 29 | #include <media/AidlConversionNdkCpp.h> |
| 30 | #include <media/AidlConversionUtil.h> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 31 | #include <utils/Log.h> |
| 32 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 33 | #include "DeviceHalAidl.h" |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 34 | #include "DevicesFactoryHalAidl.h" |
| 35 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 36 | using aidl::android::aidl_utils::statusTFromBinderStatus; |
| 37 | using aidl::android::hardware::audio::core::IConfig; |
| 38 | using aidl::android::hardware::audio::core::IModule; |
| 39 | using aidl::android::hardware::audio::core::SurroundSoundConfig; |
Mikhail Naganov | e501100 | 2024-01-26 10:57:19 -0800 | [diff] [blame] | 40 | using aidl::android::hardware::audio::core::VendorParameter; |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 41 | using aidl::android::media::audio::common::AudioHalEngineConfig; |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 42 | using aidl::android::media::audio::IHalAdapterVendorExtension; |
| 43 | using android::detail::AudioHalVersionInfo; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 44 | |
| 45 | namespace android { |
| 46 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 47 | namespace { |
| 48 | |
| 49 | ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily> |
| 50 | ndk2cpp_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 | |
| 59 | ConversionResult<media::SurroundSoundConfig> |
| 60 | ndk2cpp_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 Naganov | e501100 | 2024-01-26 10:57:19 -0800 | [diff] [blame] | 68 | class 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 Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 141 | } // namespace |
| 142 | |
| 143 | DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config) |
Mikhail Naganov | e501100 | 2024-01-26 10:57:19 -0800 | [diff] [blame] | 144 | : mConfig(std::move(config)), |
| 145 | mVendorExt(ndk::SharedRefBase::make<HalAdapterVendorExtensionWrapper>()) { |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | status_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 Naganov | 1f8cf3d | 2023-08-04 14:42:14 -0700 | [diff] [blame] | 157 | 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 Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 172 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 175 | // Opens a device with the specified name. To close the device, it is |
| 176 | // necessary to release references to the returned object. |
| 177 | status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { |
| 178 | if (name == nullptr || device == nullptr) { |
| 179 | return BAD_VALUE; |
| 180 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 181 | std::shared_ptr<IModule> service; |
Mikhail Naganov | 0ee6cb9 | 2023-05-15 13:21:11 -0700 | [diff] [blame] | 182 | 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 Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 189 | } |
Mikhail Naganov | e501100 | 2024-01-26 10:57:19 -0800 | [diff] [blame] | 190 | *device = sp<DeviceHalAidl>::make(name, service, mVendorExt); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 191 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 194 | status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 195 | // 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 Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 201 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 202 | return NO_ERROR; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const { |
| 206 | int32_t versionNumber = 0; |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 207 | if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) { |
| 208 | ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str()); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 209 | } |
| 210 | // AIDL does not have minor version, fill 0 for all versions |
| 211 | return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber); |
| 212 | } |
| 213 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 214 | status_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 | |
| 221 | status_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 Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 229 | // Main entry-point to the shared library. |
| 230 | extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 231 | auto serviceName = std::string(IConfig::descriptor) + "/default"; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 232 | auto service = IConfig::fromBinder( |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 233 | 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 Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | } // namespace android |