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> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 26 | #include <android/binder_manager.h> |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 27 | #include <binder/IServiceManager.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; |
| 39 | using aidl::android::media::audio::common::AudioHalEngineConfig; |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 40 | using aidl::android::media::audio::IHalAdapterVendorExtension; |
| 41 | using android::detail::AudioHalVersionInfo; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 42 | |
| 43 | namespace android { |
| 44 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily> |
| 48 | ndk2cpp_SurroundSoundConfigFormatFamily(const SurroundSoundConfig::SurroundFormatFamily& ndk) { |
| 49 | media::SurroundSoundConfig::SurroundFormatFamily cpp; |
| 50 | cpp.primaryFormat = VALUE_OR_RETURN(ndk2cpp_AudioFormatDescription(ndk.primaryFormat)); |
| 51 | cpp.subFormats = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector< |
| 52 | media::audio::common::AudioFormatDescription>>(ndk.subFormats, |
| 53 | ndk2cpp_AudioFormatDescription)); |
| 54 | return cpp; |
| 55 | } |
| 56 | |
| 57 | ConversionResult<media::SurroundSoundConfig> |
| 58 | ndk2cpp_SurroundSoundConfig(const SurroundSoundConfig& ndk) { |
| 59 | media::SurroundSoundConfig cpp; |
| 60 | cpp.formatFamilies = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector< |
| 61 | media::SurroundSoundConfig::SurroundFormatFamily>>(ndk.formatFamilies, |
| 62 | ndk2cpp_SurroundSoundConfigFormatFamily)); |
| 63 | return cpp; |
| 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config) |
| 69 | : mConfig(std::move(config)) { |
| 70 | } |
| 71 | |
| 72 | status_t DevicesFactoryHalAidl::getDeviceNames(std::vector<std::string> *names) { |
| 73 | if (names == nullptr) { |
| 74 | return BAD_VALUE; |
| 75 | } |
| 76 | AServiceManager_forEachDeclaredInstance(IModule::descriptor, static_cast<void*>(names), |
| 77 | [](const char* instance, void* context) { |
| 78 | if (strcmp(instance, "default") == 0) instance = "primary"; |
| 79 | static_cast<decltype(names)>(context)->push_back(instance); |
| 80 | }); |
Mikhail Naganov | 1f8cf3d | 2023-08-04 14:42:14 -0700 | [diff] [blame^] | 81 | std::sort(names->begin(), names->end(), [](const std::string& lhs, |
| 82 | const std::string& rhs) { |
| 83 | // This order corresponds to the canonical order of modules as specified in |
| 84 | // the reference 'audio_policy_configuration_7_0.xml' file. |
| 85 | static const std::map<std::string, int> kPriorities{ |
| 86 | { "primary", 0 }, { "a2dp", 1 }, { "usb", 2 }, { "r_submix", 3 }, |
| 87 | { "bluetooth", 4 }, { "hearing_aid", 5 }, { "msd", 6 }, { "stub", 7 } |
| 88 | }; |
| 89 | auto lhsIt = kPriorities.find(lhs); |
| 90 | auto rhsIt = kPriorities.find(rhs); |
| 91 | if (lhsIt != kPriorities.end() && rhsIt != kPriorities.end()) { |
| 92 | return lhsIt->second < rhsIt->second; |
| 93 | } |
| 94 | return lhsIt != kPriorities.end(); |
| 95 | }); |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 96 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 99 | // Opens a device with the specified name. To close the device, it is |
| 100 | // necessary to release references to the returned object. |
| 101 | status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { |
| 102 | if (name == nullptr || device == nullptr) { |
| 103 | return BAD_VALUE; |
| 104 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 105 | std::shared_ptr<IModule> service; |
Mikhail Naganov | 0ee6cb9 | 2023-05-15 13:21:11 -0700 | [diff] [blame] | 106 | if (strcmp(name, "primary") == 0) name = "default"; |
| 107 | auto serviceName = std::string(IModule::descriptor) + "/" + name; |
| 108 | service = IModule::fromBinder( |
| 109 | ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str()))); |
| 110 | if (service == nullptr) { |
| 111 | ALOGE("%s fromBinder %s failed", __func__, serviceName.c_str()); |
| 112 | return NO_INIT; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 113 | } |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 114 | *device = sp<DeviceHalAidl>::make(name, service, getVendorExtension()); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 115 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | status_t DevicesFactoryHalAidl::getHalPids(std::vector<pid_t> *pids) { |
| 119 | if (pids == nullptr) { |
| 120 | return BAD_VALUE; |
| 121 | } |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 122 | // The functionality for retrieving debug infos of services is not exposed via the NDK. |
| 123 | sp<IServiceManager> sm = defaultServiceManager(); |
| 124 | if (sm == nullptr) { |
| 125 | return NO_INIT; |
| 126 | } |
| 127 | std::set<pid_t> pidsSet; |
| 128 | const auto moduleServiceName = std::string(IModule::descriptor) + "/"; |
| 129 | auto debugInfos = sm->getServiceDebugInfo(); |
| 130 | for (const auto& info : debugInfos) { |
| 131 | if (info.pid > 0 && |
| 132 | info.name.size() > moduleServiceName.size() && // '>' as there must be instance name |
| 133 | info.name.substr(0, moduleServiceName.size()) == moduleServiceName) { |
| 134 | pidsSet.insert(info.pid); |
| 135 | } |
| 136 | } |
| 137 | *pids = {pidsSet.begin(), pidsSet.end()}; |
| 138 | return NO_ERROR; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 142 | // Dynamic registration of module instances is not supported. The functionality |
| 143 | // in the audio server which is related to this callback can be removed together |
| 144 | // with HIDL support. |
| 145 | ALOG_ASSERT(callback != nullptr); |
| 146 | if (callback != nullptr) { |
| 147 | callback->onNewDevicesAvailable(); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 148 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 149 | return NO_ERROR; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const { |
| 153 | int32_t versionNumber = 0; |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 154 | if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) { |
| 155 | ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str()); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 156 | } |
| 157 | // AIDL does not have minor version, fill 0 for all versions |
| 158 | return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber); |
| 159 | } |
| 160 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 161 | status_t DevicesFactoryHalAidl::getSurroundSoundConfig(media::SurroundSoundConfig *config) { |
| 162 | SurroundSoundConfig ndkConfig; |
| 163 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getSurroundSoundConfig(&ndkConfig))); |
| 164 | *config = VALUE_OR_RETURN_STATUS(ndk2cpp_SurroundSoundConfig(ndkConfig)); |
| 165 | return OK; |
| 166 | } |
| 167 | |
| 168 | status_t DevicesFactoryHalAidl::getEngineConfig( |
| 169 | media::audio::common::AudioHalEngineConfig *config) { |
| 170 | AudioHalEngineConfig ndkConfig; |
| 171 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getEngineConfig(&ndkConfig))); |
| 172 | *config = VALUE_OR_RETURN_STATUS(ndk2cpp_AudioHalEngineConfig(ndkConfig)); |
| 173 | return OK; |
| 174 | } |
| 175 | |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 176 | std::shared_ptr<IHalAdapterVendorExtension> DevicesFactoryHalAidl::getVendorExtension() { |
| 177 | if (!mVendorExt.has_value()) { |
| 178 | auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default"; |
| 179 | if (AServiceManager_isDeclared(serviceName.c_str())) { |
| 180 | mVendorExt = std::shared_ptr<IHalAdapterVendorExtension>( |
| 181 | IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder( |
| 182 | AServiceManager_waitForService(serviceName.c_str())))); |
| 183 | } else { |
| 184 | mVendorExt = nullptr; |
| 185 | } |
| 186 | } |
| 187 | return mVendorExt.value(); |
| 188 | } |
| 189 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 190 | // Main entry-point to the shared library. |
| 191 | extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 192 | auto serviceName = std::string(IConfig::descriptor) + "/default"; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 193 | auto service = IConfig::fromBinder( |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 194 | ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str()))); |
| 195 | if (!service) { |
| 196 | ALOGE("%s binder service %s not exist", __func__, serviceName.c_str()); |
| 197 | return nullptr; |
| 198 | } |
| 199 | return new DevicesFactoryHalAidl(service); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | } // namespace android |