blob: c8cce9698a876c1fbf0f9565528f01c1b6ee47c9 [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;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000037using ::android::detail::AudioHalVersionInfo;
38
39namespace android {
40
Mikhail Naganovf83b9742023-04-24 13:06:04 -070041namespace {
42
43ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily>
44ndk2cpp_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
53ConversionResult<media::SurroundSoundConfig>
54ndk2cpp_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
64DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config)
65 : mConfig(std::move(config)) {
66}
67
68status_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 Yaodca65ce2022-12-02 05:35:41 +000078}
79
Shunkai Yaodca65ce2022-12-02 05:35:41 +000080// Opens a device with the specified name. To close the device, it is
81// necessary to release references to the returned object.
82status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
83 if (name == nullptr || device == nullptr) {
84 return BAD_VALUE;
85 }
Mikhail Naganov31d46652023-01-10 18:29:25 +000086 std::shared_ptr<IModule> service;
Mikhail Naganov0ee6cb92023-05-15 13:21:11 -070087 if (strcmp(name, "primary") == 0) name = "default";
88 auto serviceName = std::string(IModule::descriptor) + "/" + name;
89 service = IModule::fromBinder(
90 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
91 if (service == nullptr) {
92 ALOGE("%s fromBinder %s failed", __func__, serviceName.c_str());
93 return NO_INIT;
Shunkai Yao51202502022-12-12 06:11:46 +000094 }
Mikhail Naganov5b1eed12023-01-25 11:29:11 -080095 *device = sp<DeviceHalAidl>::make(name, service);
Shunkai Yao51202502022-12-12 06:11:46 +000096 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000097}
98
99status_t DevicesFactoryHalAidl::getHalPids(std::vector<pid_t> *pids) {
100 if (pids == nullptr) {
101 return BAD_VALUE;
102 }
Mikhail Naganovfab697c2023-01-11 19:33:13 +0000103 // The functionality for retrieving debug infos of services is not exposed via the NDK.
104 sp<IServiceManager> sm = defaultServiceManager();
105 if (sm == nullptr) {
106 return NO_INIT;
107 }
108 std::set<pid_t> pidsSet;
109 const auto moduleServiceName = std::string(IModule::descriptor) + "/";
110 auto debugInfos = sm->getServiceDebugInfo();
111 for (const auto& info : debugInfos) {
112 if (info.pid > 0 &&
113 info.name.size() > moduleServiceName.size() && // '>' as there must be instance name
114 info.name.substr(0, moduleServiceName.size()) == moduleServiceName) {
115 pidsSet.insert(info.pid);
116 }
117 }
118 *pids = {pidsSet.begin(), pidsSet.end()};
119 return NO_ERROR;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000120}
121
122status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000123 // Dynamic registration of module instances is not supported. The functionality
124 // in the audio server which is related to this callback can be removed together
125 // with HIDL support.
126 ALOG_ASSERT(callback != nullptr);
127 if (callback != nullptr) {
128 callback->onNewDevicesAvailable();
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000129 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000130 return NO_ERROR;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000131}
132
133AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const {
134 int32_t versionNumber = 0;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700135 if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) {
136 ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str());
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000137 }
138 // AIDL does not have minor version, fill 0 for all versions
139 return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber);
140}
141
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700142status_t DevicesFactoryHalAidl::getSurroundSoundConfig(media::SurroundSoundConfig *config) {
143 SurroundSoundConfig ndkConfig;
144 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getSurroundSoundConfig(&ndkConfig)));
145 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_SurroundSoundConfig(ndkConfig));
146 return OK;
147}
148
149status_t DevicesFactoryHalAidl::getEngineConfig(
150 media::audio::common::AudioHalEngineConfig *config) {
151 AudioHalEngineConfig ndkConfig;
152 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getEngineConfig(&ndkConfig)));
153 *config = VALUE_OR_RETURN_STATUS(ndk2cpp_AudioHalEngineConfig(ndkConfig));
154 return OK;
155}
156
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000157// Main entry-point to the shared library.
158extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Shunkai Yao51202502022-12-12 06:11:46 +0000159 auto serviceName = std::string(IConfig::descriptor) + "/default";
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000160 auto service = IConfig::fromBinder(
Shunkai Yao51202502022-12-12 06:11:46 +0000161 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
162 if (!service) {
163 ALOGE("%s binder service %s not exist", __func__, serviceName.c_str());
164 return nullptr;
165 }
166 return new DevicesFactoryHalAidl(service);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000167}
168
169} // namespace android