blob: 3dbc14ad8e6c0babe99bcb56a286d46b2d93d331 [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)
Dan Shi1eaa2242024-02-01 19:32:34 +000068 : mConfig(std::move(config)) {
Mikhail Naganovf83b9742023-04-24 13:06:04 -070069}
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 }
Dan Shi1eaa2242024-02-01 19:32:34 +0000113 *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
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000117status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000118 // Dynamic registration of module instances is not supported. The functionality
119 // in the audio server which is related to this callback can be removed together
120 // with HIDL support.
121 ALOG_ASSERT(callback != nullptr);
122 if (callback != nullptr) {
123 callback->onNewDevicesAvailable();
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000124 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000125 return NO_ERROR;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000126}
127
128AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const {
129 int32_t versionNumber = 0;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700130 if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) {
131 ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str());
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000132 }
133 // AIDL does not have minor version, fill 0 for all versions
134 return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber);
135}
136
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700137status_t DevicesFactoryHalAidl::getSurroundSoundConfig(media::SurroundSoundConfig *config) {
138 SurroundSoundConfig ndkConfig;
139 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getSurroundSoundConfig(&ndkConfig)));
140 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_SurroundSoundConfig(ndkConfig));
141 return OK;
142}
143
144status_t DevicesFactoryHalAidl::getEngineConfig(
145 media::audio::common::AudioHalEngineConfig *config) {
146 AudioHalEngineConfig ndkConfig;
147 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getEngineConfig(&ndkConfig)));
148 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_AudioHalEngineConfig(ndkConfig));
149 return OK;
150}
151
Dan Shi1eaa2242024-02-01 19:32:34 +0000152std::shared_ptr<IHalAdapterVendorExtension> DevicesFactoryHalAidl::getVendorExtension() {
153 if (!mVendorExt.has_value()) {
154 auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
155 if (AServiceManager_isDeclared(serviceName.c_str())) {
156 mVendorExt = std::shared_ptr<IHalAdapterVendorExtension>(
157 IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
158 AServiceManager_waitForService(serviceName.c_str()))));
159 } else {
160 mVendorExt = nullptr;
161 }
162 }
163 return mVendorExt.value();
164}
165
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000166// Main entry-point to the shared library.
167extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Shunkai Yao51202502022-12-12 06:11:46 +0000168 auto serviceName = std::string(IConfig::descriptor) + "/default";
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000169 auto service = IConfig::fromBinder(
Shunkai Yao51202502022-12-12 06:11:46 +0000170 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 Yaodca65ce2022-12-02 05:35:41 +0000176}
177
178} // namespace android