blob: f00b1a06b7e4fc52400a9e42bcb439c5d3dff5ae [file] [log] [blame]
Shunkai Yaodca65ce2022-12-02 05:35:41 +00001/*
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 Naganovf83b9742023-04-24 13:06:04 -070017#include <memory>
18
Shunkai Yaodca65ce2022-12-02 05:35:41 +000019#define LOG_TAG "DevicesFactoryHalAidl"
20//#define LOG_NDEBUG 0
21
Shunkai Yao51202502022-12-12 06:11:46 +000022#include <aidl/android/hardware/audio/core/IModule.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000023#include <android/binder_manager.h>
Mikhail Naganovfab697c2023-01-11 19:33:13 +000024#include <binder/IServiceManager.h>
Mikhail Naganovf83b9742023-04-24 13:06:04 -070025#include <media/AidlConversionNdkCpp.h>
26#include <media/AidlConversionUtil.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000027#include <utils/Log.h>
28
Shunkai Yao51202502022-12-12 06:11:46 +000029#include "DeviceHalAidl.h"
Shunkai Yaodca65ce2022-12-02 05:35:41 +000030#include "DevicesFactoryHalAidl.h"
31
Mikhail Naganovf83b9742023-04-24 13:06:04 -070032using aidl::android::aidl_utils::statusTFromBinderStatus;
33using aidl::android::hardware::audio::core::IConfig;
34using aidl::android::hardware::audio::core::IModule;
35using aidl::android::hardware::audio::core::SurroundSoundConfig;
36using aidl::android::media::audio::common::AudioHalEngineConfig;
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070037using aidl::android::media::audio::IHalAdapterVendorExtension;
38using android::detail::AudioHalVersionInfo;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000039
40namespace android {
41
Mikhail Naganovf83b9742023-04-24 13:06:04 -070042namespace {
43
44ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily>
45ndk2cpp_SurroundSoundConfigFormatFamily(const SurroundSoundConfig::SurroundFormatFamily& ndk) {
46 media::SurroundSoundConfig::SurroundFormatFamily cpp;
47 cpp.primaryFormat = VALUE_OR_RETURN(ndk2cpp_AudioFormatDescription(ndk.primaryFormat));
48 cpp.subFormats = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
49 media::audio::common::AudioFormatDescription>>(ndk.subFormats,
50 ndk2cpp_AudioFormatDescription));
51 return cpp;
52}
53
54ConversionResult<media::SurroundSoundConfig>
55ndk2cpp_SurroundSoundConfig(const SurroundSoundConfig& ndk) {
56 media::SurroundSoundConfig cpp;
57 cpp.formatFamilies = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
58 media::SurroundSoundConfig::SurroundFormatFamily>>(ndk.formatFamilies,
59 ndk2cpp_SurroundSoundConfigFormatFamily));
60 return cpp;
61}
62
63} // namespace
64
65DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config)
66 : mConfig(std::move(config)) {
67}
68
69status_t DevicesFactoryHalAidl::getDeviceNames(std::vector<std::string> *names) {
70 if (names == nullptr) {
71 return BAD_VALUE;
72 }
73 AServiceManager_forEachDeclaredInstance(IModule::descriptor, static_cast<void*>(names),
74 [](const char* instance, void* context) {
75 if (strcmp(instance, "default") == 0) instance = "primary";
76 static_cast<decltype(names)>(context)->push_back(instance);
77 });
78 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000079}
80
Shunkai Yaodca65ce2022-12-02 05:35:41 +000081// Opens a device with the specified name. To close the device, it is
82// necessary to release references to the returned object.
83status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
84 if (name == nullptr || device == nullptr) {
85 return BAD_VALUE;
86 }
Mikhail Naganov31d46652023-01-10 18:29:25 +000087 std::shared_ptr<IModule> service;
Mikhail Naganov0ee6cb92023-05-15 13:21:11 -070088 if (strcmp(name, "primary") == 0) name = "default";
89 auto serviceName = std::string(IModule::descriptor) + "/" + name;
90 service = IModule::fromBinder(
91 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
92 if (service == nullptr) {
93 ALOGE("%s fromBinder %s failed", __func__, serviceName.c_str());
94 return NO_INIT;
Shunkai Yao51202502022-12-12 06:11:46 +000095 }
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070096 *device = sp<DeviceHalAidl>::make(name, service, getVendorExtension());
Shunkai Yao51202502022-12-12 06:11:46 +000097 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000098}
99
100status_t DevicesFactoryHalAidl::getHalPids(std::vector<pid_t> *pids) {
101 if (pids == nullptr) {
102 return BAD_VALUE;
103 }
Mikhail Naganovfab697c2023-01-11 19:33:13 +0000104 // The functionality for retrieving debug infos of services is not exposed via the NDK.
105 sp<IServiceManager> sm = defaultServiceManager();
106 if (sm == nullptr) {
107 return NO_INIT;
108 }
109 std::set<pid_t> pidsSet;
110 const auto moduleServiceName = std::string(IModule::descriptor) + "/";
111 auto debugInfos = sm->getServiceDebugInfo();
112 for (const auto& info : debugInfos) {
113 if (info.pid > 0 &&
114 info.name.size() > moduleServiceName.size() && // '>' as there must be instance name
115 info.name.substr(0, moduleServiceName.size()) == moduleServiceName) {
116 pidsSet.insert(info.pid);
117 }
118 }
119 *pids = {pidsSet.begin(), pidsSet.end()};
120 return NO_ERROR;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000121}
122
123status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000124 // Dynamic registration of module instances is not supported. The functionality
125 // in the audio server which is related to this callback can be removed together
126 // with HIDL support.
127 ALOG_ASSERT(callback != nullptr);
128 if (callback != nullptr) {
129 callback->onNewDevicesAvailable();
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000130 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000131 return NO_ERROR;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000132}
133
134AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const {
135 int32_t versionNumber = 0;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700136 if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) {
137 ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str());
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000138 }
139 // AIDL does not have minor version, fill 0 for all versions
140 return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber);
141}
142
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700143status_t DevicesFactoryHalAidl::getSurroundSoundConfig(media::SurroundSoundConfig *config) {
144 SurroundSoundConfig ndkConfig;
145 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getSurroundSoundConfig(&ndkConfig)));
146 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_SurroundSoundConfig(ndkConfig));
147 return OK;
148}
149
150status_t DevicesFactoryHalAidl::getEngineConfig(
151 media::audio::common::AudioHalEngineConfig *config) {
152 AudioHalEngineConfig ndkConfig;
153 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getEngineConfig(&ndkConfig)));
154 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_AudioHalEngineConfig(ndkConfig));
155 return OK;
156}
157
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700158std::shared_ptr<IHalAdapterVendorExtension> DevicesFactoryHalAidl::getVendorExtension() {
159 if (!mVendorExt.has_value()) {
160 auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
161 if (AServiceManager_isDeclared(serviceName.c_str())) {
162 mVendorExt = std::shared_ptr<IHalAdapterVendorExtension>(
163 IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
164 AServiceManager_waitForService(serviceName.c_str()))));
165 } else {
166 mVendorExt = nullptr;
167 }
168 }
169 return mVendorExt.value();
170}
171
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000172// Main entry-point to the shared library.
173extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Shunkai Yao51202502022-12-12 06:11:46 +0000174 auto serviceName = std::string(IConfig::descriptor) + "/default";
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000175 auto service = IConfig::fromBinder(
Shunkai Yao51202502022-12-12 06:11:46 +0000176 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
177 if (!service) {
178 ALOGE("%s binder service %s not exist", __func__, serviceName.c_str());
179 return nullptr;
180 }
181 return new DevicesFactoryHalAidl(service);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000182}
183
184} // namespace android