blob: e79ad756806d8a220f68e0553bc55a0fdcf25615 [file] [log] [blame]
Eric Laurent27ef4d82016-10-14 15:46:06 -07001/*
2 * Copyright (C) 2016 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 Naganov10548292016-10-31 10:39:47 -070017#define LOG_TAG "audiohalservice"
Eric Laurent27ef4d82016-10-14 15:46:06 -070018
Mikhail Naganov9a88b5b2021-06-17 19:55:16 +000019#include <signal.h>
Mikhail Naganovf3e4d212020-01-29 15:32:24 -080020#include <string>
21#include <vector>
22
Vlad Popaf4fe41c2022-12-14 11:57:26 +010023#include <SoundDoseFactory.h>
24#include <android-base/logging.h>
25#include <android/binder_ibinder_platform.h>
26#include <android/binder_manager.h>
Jasmine Chad90c4742022-02-15 16:31:13 +080027#include <android/binder_process.h>
Haynes Mathew Georgea57f41d2018-02-21 14:45:20 -080028#include <binder/ProcessState.h>
Mikhail Naganov317a9ed2018-09-20 14:25:01 -070029#include <cutils/properties.h>
Josh Wu3f8f5992022-01-18 03:01:19 -080030#include <dlfcn.h>
Mikhail Naganov5697a9b2017-12-28 13:29:59 -080031#include <hidl/HidlTransportSupport.h>
32#include <hidl/LegacySupport.h>
Mikhail Naganov317a9ed2018-09-20 14:25:01 -070033#include <hwbinder/ProcessState.h>
Eric Laurent27ef4d82016-10-14 15:46:06 -070034
Kevin Rocard91a73b42018-03-01 18:56:58 -080035using namespace android::hardware;
Mikhail Naganov3c256462017-02-08 16:34:22 -080036using android::OK;
37
Mikhail Naganovf3e4d212020-01-29 15:32:24 -080038using InterfacesList = std::vector<std::string>;
39
Vlad Popaf4fe41c2022-12-14 11:57:26 +010040using aidl::android::hardware::audio::sounddose::SoundDoseFactory;
41
Kevin Rocard5261dce2019-06-18 12:40:48 -070042/** Try to register the provided factories in the provided order.
43 * If any registers successfully, do not register any other and return true.
44 * If all fail, return false.
45 */
Mikhail Naganovf3e4d212020-01-29 15:32:24 -080046template <class Iter>
47static bool registerPassthroughServiceImplementations(Iter first, Iter last) {
48 for (; first != last; ++first) {
49 if (registerPassthroughServiceImplementation(*first) == OK) {
50 return true;
51 }
52 }
53 return false;
Kevin Rocard5261dce2019-06-18 12:40:48 -070054}
55
Josh Wu3f8f5992022-01-18 03:01:19 -080056static bool registerExternalServiceImplementation(const std::string& libName,
57 const std::string& funcName) {
58 constexpr int dlMode = RTLD_LAZY;
59 void* handle = nullptr;
60 dlerror(); // clear
61 auto libPath = libName + ".so";
62 handle = dlopen(libPath.c_str(), dlMode);
63 if (handle == nullptr) {
64 const char* error = dlerror();
65 ALOGE("Failed to dlopen %s: %s", libPath.c_str(),
66 error != nullptr ? error : "unknown error");
67 return false;
68 }
69 binder_status_t (*factoryFunction)();
70 *(void**)(&factoryFunction) = dlsym(handle, funcName.c_str());
71 if (!factoryFunction) {
72 const char* error = dlerror();
73 ALOGE("Factory function %s not found in libName %s: %s", funcName.c_str(), libPath.c_str(),
74 error != nullptr ? error : "unknown error");
75 dlclose(handle);
76 return false;
77 }
78 return ((*factoryFunction)() == STATUS_OK);
79}
80
Eric Laurent27ef4d82016-10-14 15:46:06 -070081int main(int /* argc */, char* /* argv */ []) {
Mikhail Naganov9a88b5b2021-06-17 19:55:16 +000082 signal(SIGPIPE, SIG_IGN);
83
Mikhail Naganov317a9ed2018-09-20 14:25:01 -070084 ::android::ProcessState::initWithDriver("/dev/vndbinder");
Haynes Mathew Georgea57f41d2018-02-21 14:45:20 -080085 // start a threadpool for vndbinder interactions
Mikhail Naganov317a9ed2018-09-20 14:25:01 -070086 ::android::ProcessState::self()->startThreadPool();
87
Jasmine Chad90c4742022-02-15 16:31:13 +080088 ABinderProcess_setThreadPoolMaxThreadCount(1);
89 ABinderProcess_startThreadPool();
90
Mikhail Naganov317a9ed2018-09-20 14:25:01 -070091 const int32_t defaultValue = -1;
92 int32_t value =
93 property_get_int32("persist.vendor.audio.service.hwbinder.size_kbyte", defaultValue);
94 if (value != defaultValue) {
95 ALOGD("Configuring hwbinder with mmap size %d KBytes", value);
96 ProcessState::initWithMmapSize(static_cast<size_t>(value) * 1024);
97 }
Martijn Coenen02822372016-12-29 14:03:41 +010098 configureRpcThreadpool(16, true /*callerWillJoin*/);
Kevin Rocard91a73b42018-03-01 18:56:58 -080099
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800100 // Automatic formatting tries to compact the lines, making them less readable
Kevin Rocardc69e3e92019-06-18 15:39:17 -0700101 // clang-format off
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800102 const std::vector<InterfacesList> mandatoryInterfaces = {
103 {
104 "Audio Core API",
Mikhail Naganov8140f562022-01-15 01:15:12 +0000105 "android.hardware.audio@7.1::IDevicesFactory",
Mikhail Naganovb4443502021-02-05 00:10:40 +0000106 "android.hardware.audio@7.0::IDevicesFactory",
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800107 "android.hardware.audio@6.0::IDevicesFactory",
108 "android.hardware.audio@5.0::IDevicesFactory",
109 "android.hardware.audio@4.0::IDevicesFactory",
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800110 },
111 {
112 "Audio Effect API",
Mikhail Naganovb4443502021-02-05 00:10:40 +0000113 "android.hardware.audio.effect@7.0::IEffectsFactory",
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800114 "android.hardware.audio.effect@6.0::IEffectsFactory",
115 "android.hardware.audio.effect@5.0::IEffectsFactory",
116 "android.hardware.audio.effect@4.0::IEffectsFactory",
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800117 }
118 };
Kevin Rocard91a73b42018-03-01 18:56:58 -0800119
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800120 const std::vector<InterfacesList> optionalInterfaces = {
121 {
122 "Soundtrigger API",
123 "android.hardware.soundtrigger@2.3::ISoundTriggerHw",
124 "android.hardware.soundtrigger@2.2::ISoundTriggerHw",
125 "android.hardware.soundtrigger@2.1::ISoundTriggerHw",
126 "android.hardware.soundtrigger@2.0::ISoundTriggerHw",
127 },
128 {
129 "Bluetooth Audio API",
Alice Kuo95ba6a22021-11-04 19:28:10 +0800130 "android.hardware.bluetooth.audio@2.2::IBluetoothAudioProvidersFactory",
Grzegorz Kolodziejczykb5f2d232019-10-24 12:31:20 +0200131 "android.hardware.bluetooth.audio@2.1::IBluetoothAudioProvidersFactory",
132 "android.hardware.bluetooth.audio@2.0::IBluetoothAudioProvidersFactory",
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800133 },
134 // remove the old HIDL when Bluetooth Audio Hal V2 has offloading supported
135 {
136 "Bluetooth Audio Offload API",
Mikhail Naganov2801b032020-01-31 14:48:23 -0800137 "android.hardware.bluetooth.a2dp@1.0::IBluetoothAudioOffload"
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800138 }
139 };
Josh Wu3f8f5992022-01-18 03:01:19 -0800140
141 const std::vector<std::pair<std::string,std::string>> optionalInterfaceSharedLibs = {
142 {
143 "android.hardware.bluetooth.audio-impl",
144 "createIBluetoothAudioProviderFactory",
145 },
146 };
Kevin Rocardc69e3e92019-06-18 15:39:17 -0700147 // clang-format on
Kevin Rocard91a73b42018-03-01 18:56:58 -0800148
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800149 for (const auto& listIter : mandatoryInterfaces) {
150 auto iter = listIter.begin();
151 const std::string& interfaceFamilyName = *iter++;
152 LOG_ALWAYS_FATAL_IF(!registerPassthroughServiceImplementations(iter, listIter.end()),
153 "Could not register %s", interfaceFamilyName.c_str());
154 }
Kevin Rocard91a73b42018-03-01 18:56:58 -0800155
Mikhail Naganovf3e4d212020-01-29 15:32:24 -0800156 for (const auto& listIter : optionalInterfaces) {
157 auto iter = listIter.begin();
158 const std::string& interfaceFamilyName = *iter++;
159 ALOGW_IF(!registerPassthroughServiceImplementations(iter, listIter.end()),
160 "Could not register %s", interfaceFamilyName.c_str());
161 }
Aniket Kumar Latac5a52032018-01-31 20:17:30 -0800162
Josh Wu3f8f5992022-01-18 03:01:19 -0800163 for (const auto& interfacePair : optionalInterfaceSharedLibs) {
164 const std::string& libraryName = interfacePair.first;
165 const std::string& interfaceLoaderFuncName = interfacePair.second;
166 if (registerExternalServiceImplementation(libraryName, interfaceLoaderFuncName)) {
167 ALOGI("%s() from %s success", interfaceLoaderFuncName.c_str(), libraryName.c_str());
168 } else {
169 ALOGW("%s() from %s failed", interfaceLoaderFuncName.c_str(), libraryName.c_str());
170 }
171 }
172
Vlad Popaf4fe41c2022-12-14 11:57:26 +0100173 // Register ISoundDoseFactory interface as a workaround for using the audio AIDL HAL
174 auto soundDoseDefault = ndk::SharedRefBase::make<SoundDoseFactory>();
175 const std::string soundDoseDefaultName =
176 std::string() + SoundDoseFactory::descriptor + "/default";
177 binder_status_t status = AServiceManager_addService(soundDoseDefault->asBinder().get(),
178 soundDoseDefaultName.c_str());
179 CHECK_EQ(STATUS_OK, status);
180
Martijn Coenen02822372016-12-29 14:03:41 +0100181 joinRpcThreadpool();
Eric Laurent27ef4d82016-10-14 15:46:06 -0700182}