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