blob: 96a3e60350973fee71fb3643e6887444d37fc7ae [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 Naganov1f8cf3d2023-08-04 14:42:14 -070017#include <algorithm>
18#include <map>
Mikhail Naganovf83b9742023-04-24 13:06:04 -070019#include <memory>
Mikhail Naganov1f8cf3d2023-08-04 14:42:14 -070020#include <string>
Mikhail Naganovf83b9742023-04-24 13:06:04 -070021
Shunkai Yaodca65ce2022-12-02 05:35:41 +000022#define LOG_TAG "DevicesFactoryHalAidl"
23//#define LOG_NDEBUG 0
24
Shunkai Yao51202502022-12-12 06:11:46 +000025#include <aidl/android/hardware/audio/core/IModule.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000026#include <android/binder_manager.h>
Mikhail Naganovf83b9742023-04-24 13:06:04 -070027#include <media/AidlConversionNdkCpp.h>
28#include <media/AidlConversionUtil.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000029#include <utils/Log.h>
30
Shunkai Yao51202502022-12-12 06:11:46 +000031#include "DeviceHalAidl.h"
Shunkai Yaodca65ce2022-12-02 05:35:41 +000032#include "DevicesFactoryHalAidl.h"
33
Mikhail Naganovf83b9742023-04-24 13:06:04 -070034using aidl::android::aidl_utils::statusTFromBinderStatus;
35using aidl::android::hardware::audio::core::IConfig;
36using aidl::android::hardware::audio::core::IModule;
37using aidl::android::hardware::audio::core::SurroundSoundConfig;
38using aidl::android::media::audio::common::AudioHalEngineConfig;
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070039using aidl::android::media::audio::IHalAdapterVendorExtension;
40using android::detail::AudioHalVersionInfo;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000041
42namespace android {
43
Mikhail Naganovf83b9742023-04-24 13:06:04 -070044namespace {
45
46ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily>
47ndk2cpp_SurroundSoundConfigFormatFamily(const SurroundSoundConfig::SurroundFormatFamily& ndk) {
48 media::SurroundSoundConfig::SurroundFormatFamily cpp;
49 cpp.primaryFormat = VALUE_OR_RETURN(ndk2cpp_AudioFormatDescription(ndk.primaryFormat));
50 cpp.subFormats = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
51 media::audio::common::AudioFormatDescription>>(ndk.subFormats,
52 ndk2cpp_AudioFormatDescription));
53 return cpp;
54}
55
56ConversionResult<media::SurroundSoundConfig>
57ndk2cpp_SurroundSoundConfig(const SurroundSoundConfig& ndk) {
58 media::SurroundSoundConfig cpp;
59 cpp.formatFamilies = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
60 media::SurroundSoundConfig::SurroundFormatFamily>>(ndk.formatFamilies,
61 ndk2cpp_SurroundSoundConfigFormatFamily));
62 return cpp;
63}
64
65} // namespace
66
67DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config)
68 : mConfig(std::move(config)) {
69}
70
71status_t DevicesFactoryHalAidl::getDeviceNames(std::vector<std::string> *names) {
72 if (names == nullptr) {
73 return BAD_VALUE;
74 }
75 AServiceManager_forEachDeclaredInstance(IModule::descriptor, static_cast<void*>(names),
76 [](const char* instance, void* context) {
77 if (strcmp(instance, "default") == 0) instance = "primary";
78 static_cast<decltype(names)>(context)->push_back(instance);
79 });
Mikhail Naganov1f8cf3d2023-08-04 14:42:14 -070080 std::sort(names->begin(), names->end(), [](const std::string& lhs,
81 const std::string& rhs) {
82 // This order corresponds to the canonical order of modules as specified in
83 // the reference 'audio_policy_configuration_7_0.xml' file.
84 static const std::map<std::string, int> kPriorities{
85 { "primary", 0 }, { "a2dp", 1 }, { "usb", 2 }, { "r_submix", 3 },
86 { "bluetooth", 4 }, { "hearing_aid", 5 }, { "msd", 6 }, { "stub", 7 }
87 };
88 auto lhsIt = kPriorities.find(lhs);
89 auto rhsIt = kPriorities.find(rhs);
90 if (lhsIt != kPriorities.end() && rhsIt != kPriorities.end()) {
91 return lhsIt->second < rhsIt->second;
92 }
93 return lhsIt != kPriorities.end();
94 });
Mikhail Naganovf83b9742023-04-24 13:06:04 -070095 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000096}
97
Shunkai Yaodca65ce2022-12-02 05:35:41 +000098// Opens a device with the specified name. To close the device, it is
99// necessary to release references to the returned object.
100status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
101 if (name == nullptr || device == nullptr) {
102 return BAD_VALUE;
103 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000104 std::shared_ptr<IModule> service;
Mikhail Naganov0ee6cb92023-05-15 13:21:11 -0700105 if (strcmp(name, "primary") == 0) name = "default";
106 auto serviceName = std::string(IModule::descriptor) + "/" + name;
107 service = IModule::fromBinder(
108 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
109 if (service == nullptr) {
110 ALOGE("%s fromBinder %s failed", __func__, serviceName.c_str());
111 return NO_INIT;
Shunkai Yao51202502022-12-12 06:11:46 +0000112 }
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700113 *device = sp<DeviceHalAidl>::make(name, service, getVendorExtension());
Shunkai Yao51202502022-12-12 06:11:46 +0000114 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000115}
116
117status_t DevicesFactoryHalAidl::getHalPids(std::vector<pid_t> *pids) {
118 if (pids == nullptr) {
119 return BAD_VALUE;
120 }
Mikhail Naganovf5c7fdb2023-12-06 16:28:39 -0800121 // Retrieval of HAL pids requires "list services" permission which is not granted
122 // to the audio server. This job is performed by AudioService (in Java) instead.
123 return PERMISSION_DENIED;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000124}
125
126status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000127 // Dynamic registration of module instances is not supported. The functionality
128 // in the audio server which is related to this callback can be removed together
129 // with HIDL support.
130 ALOG_ASSERT(callback != nullptr);
131 if (callback != nullptr) {
132 callback->onNewDevicesAvailable();
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000133 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000134 return NO_ERROR;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000135}
136
137AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const {
138 int32_t versionNumber = 0;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700139 if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) {
140 ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str());
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000141 }
142 // AIDL does not have minor version, fill 0 for all versions
143 return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber);
144}
145
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700146status_t DevicesFactoryHalAidl::getSurroundSoundConfig(media::SurroundSoundConfig *config) {
147 SurroundSoundConfig ndkConfig;
148 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getSurroundSoundConfig(&ndkConfig)));
149 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_SurroundSoundConfig(ndkConfig));
150 return OK;
151}
152
153status_t DevicesFactoryHalAidl::getEngineConfig(
154 media::audio::common::AudioHalEngineConfig *config) {
155 AudioHalEngineConfig ndkConfig;
156 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getEngineConfig(&ndkConfig)));
157 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_AudioHalEngineConfig(ndkConfig));
158 return OK;
159}
160
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700161std::shared_ptr<IHalAdapterVendorExtension> DevicesFactoryHalAidl::getVendorExtension() {
162 if (!mVendorExt.has_value()) {
163 auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
164 if (AServiceManager_isDeclared(serviceName.c_str())) {
165 mVendorExt = std::shared_ptr<IHalAdapterVendorExtension>(
166 IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
167 AServiceManager_waitForService(serviceName.c_str()))));
168 } else {
169 mVendorExt = nullptr;
170 }
171 }
172 return mVendorExt.value();
173}
174
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000175// Main entry-point to the shared library.
176extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Shunkai Yao51202502022-12-12 06:11:46 +0000177 auto serviceName = std::string(IConfig::descriptor) + "/default";
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000178 auto service = IConfig::fromBinder(
Shunkai Yao51202502022-12-12 06:11:46 +0000179 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
180 if (!service) {
181 ALOGE("%s binder service %s not exist", __func__, serviceName.c_str());
182 return nullptr;
183 }
184 return new DevicesFactoryHalAidl(service);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000185}
186
187} // namespace android