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 | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame^] | 17 | #include <memory> |
| 18 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 19 | #define LOG_TAG "DevicesFactoryHalAidl" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 22 | #include <aidl/android/hardware/audio/core/IModule.h> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 23 | #include <android/binder_manager.h> |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 24 | #include <binder/IServiceManager.h> |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame^] | 25 | #include <media/AidlConversionNdkCpp.h> |
| 26 | #include <media/AidlConversionUtil.h> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 27 | #include <utils/Log.h> |
| 28 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 29 | #include "DeviceHalAidl.h" |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 30 | #include "DevicesFactoryHalAidl.h" |
| 31 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame^] | 32 | using aidl::android::aidl_utils::statusTFromBinderStatus; |
| 33 | using aidl::android::hardware::audio::core::IConfig; |
| 34 | using aidl::android::hardware::audio::core::IModule; |
| 35 | using aidl::android::hardware::audio::core::SurroundSoundConfig; |
| 36 | using aidl::android::media::audio::common::AudioHalEngineConfig; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 37 | using ::android::detail::AudioHalVersionInfo; |
| 38 | |
| 39 | namespace android { |
| 40 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame^] | 41 | namespace { |
| 42 | |
| 43 | ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily> |
| 44 | ndk2cpp_SurroundSoundConfigFormatFamily(const SurroundSoundConfig::SurroundFormatFamily& ndk) { |
| 45 | media::SurroundSoundConfig::SurroundFormatFamily cpp; |
| 46 | cpp.primaryFormat = VALUE_OR_RETURN(ndk2cpp_AudioFormatDescription(ndk.primaryFormat)); |
| 47 | cpp.subFormats = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector< |
| 48 | media::audio::common::AudioFormatDescription>>(ndk.subFormats, |
| 49 | ndk2cpp_AudioFormatDescription)); |
| 50 | return cpp; |
| 51 | } |
| 52 | |
| 53 | ConversionResult<media::SurroundSoundConfig> |
| 54 | ndk2cpp_SurroundSoundConfig(const SurroundSoundConfig& ndk) { |
| 55 | media::SurroundSoundConfig cpp; |
| 56 | cpp.formatFamilies = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector< |
| 57 | media::SurroundSoundConfig::SurroundFormatFamily>>(ndk.formatFamilies, |
| 58 | ndk2cpp_SurroundSoundConfigFormatFamily)); |
| 59 | return cpp; |
| 60 | } |
| 61 | |
| 62 | } // namespace |
| 63 | |
| 64 | DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config) |
| 65 | : mConfig(std::move(config)) { |
| 66 | } |
| 67 | |
| 68 | status_t DevicesFactoryHalAidl::getDeviceNames(std::vector<std::string> *names) { |
| 69 | if (names == nullptr) { |
| 70 | return BAD_VALUE; |
| 71 | } |
| 72 | AServiceManager_forEachDeclaredInstance(IModule::descriptor, static_cast<void*>(names), |
| 73 | [](const char* instance, void* context) { |
| 74 | if (strcmp(instance, "default") == 0) instance = "primary"; |
| 75 | static_cast<decltype(names)>(context)->push_back(instance); |
| 76 | }); |
| 77 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 80 | // Opens a device with the specified name. To close the device, it is |
| 81 | // necessary to release references to the returned object. |
| 82 | status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { |
| 83 | if (name == nullptr || device == nullptr) { |
| 84 | return BAD_VALUE; |
| 85 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 86 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame^] | 87 | // FIXME: Remove this call and the check for the supported module names |
| 88 | // after implementing retrieval of module names on the framework side. |
| 89 | // Currently it is still using the legacy XML config. |
| 90 | std::vector<std::string> deviceNames; |
| 91 | if (status_t status = getDeviceNames(&deviceNames); status != OK) { |
| 92 | return status; |
| 93 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 94 | std::shared_ptr<IModule> service; |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame^] | 95 | if (std::find(deviceNames.begin(), deviceNames.end(), name) != deviceNames.end()) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 96 | if (strcmp(name, "primary") == 0) name = "default"; |
| 97 | auto serviceName = std::string(IModule::descriptor) + "/" + name; |
| 98 | service = IModule::fromBinder( |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 99 | ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str()))); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 100 | ALOGE_IF(service == nullptr, "%s fromBinder %s failed", __func__, serviceName.c_str()); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 101 | } |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame^] | 102 | // If the service is a nullptr, the device object will not be really functional, |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 103 | // but will not crash either. |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 104 | *device = sp<DeviceHalAidl>::make(name, service); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 105 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | status_t DevicesFactoryHalAidl::getHalPids(std::vector<pid_t> *pids) { |
| 109 | if (pids == nullptr) { |
| 110 | return BAD_VALUE; |
| 111 | } |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 112 | // The functionality for retrieving debug infos of services is not exposed via the NDK. |
| 113 | sp<IServiceManager> sm = defaultServiceManager(); |
| 114 | if (sm == nullptr) { |
| 115 | return NO_INIT; |
| 116 | } |
| 117 | std::set<pid_t> pidsSet; |
| 118 | const auto moduleServiceName = std::string(IModule::descriptor) + "/"; |
| 119 | auto debugInfos = sm->getServiceDebugInfo(); |
| 120 | for (const auto& info : debugInfos) { |
| 121 | if (info.pid > 0 && |
| 122 | info.name.size() > moduleServiceName.size() && // '>' as there must be instance name |
| 123 | info.name.substr(0, moduleServiceName.size()) == moduleServiceName) { |
| 124 | pidsSet.insert(info.pid); |
| 125 | } |
| 126 | } |
| 127 | *pids = {pidsSet.begin(), pidsSet.end()}; |
| 128 | return NO_ERROR; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 132 | // Dynamic registration of module instances is not supported. The functionality |
| 133 | // in the audio server which is related to this callback can be removed together |
| 134 | // with HIDL support. |
| 135 | ALOG_ASSERT(callback != nullptr); |
| 136 | if (callback != nullptr) { |
| 137 | callback->onNewDevicesAvailable(); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 138 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 139 | return NO_ERROR; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const { |
| 143 | int32_t versionNumber = 0; |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame^] | 144 | if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) { |
| 145 | ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str()); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 146 | } |
| 147 | // AIDL does not have minor version, fill 0 for all versions |
| 148 | return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber); |
| 149 | } |
| 150 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame^] | 151 | status_t DevicesFactoryHalAidl::getSurroundSoundConfig(media::SurroundSoundConfig *config) { |
| 152 | SurroundSoundConfig ndkConfig; |
| 153 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getSurroundSoundConfig(&ndkConfig))); |
| 154 | *config = VALUE_OR_RETURN_STATUS(ndk2cpp_SurroundSoundConfig(ndkConfig)); |
| 155 | return OK; |
| 156 | } |
| 157 | |
| 158 | status_t DevicesFactoryHalAidl::getEngineConfig( |
| 159 | media::audio::common::AudioHalEngineConfig *config) { |
| 160 | AudioHalEngineConfig ndkConfig; |
| 161 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getEngineConfig(&ndkConfig))); |
| 162 | *config = VALUE_OR_RETURN_STATUS(ndk2cpp_AudioHalEngineConfig(ndkConfig)); |
| 163 | return OK; |
| 164 | } |
| 165 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 166 | // Main entry-point to the shared library. |
| 167 | extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 168 | auto serviceName = std::string(IConfig::descriptor) + "/default"; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 169 | auto service = IConfig::fromBinder( |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 170 | ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str()))); |
| 171 | if (!service) { |
| 172 | ALOGE("%s binder service %s not exist", __func__, serviceName.c_str()); |
| 173 | return nullptr; |
| 174 | } |
| 175 | return new DevicesFactoryHalAidl(service); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | } // namespace android |