Eric Laurent | 27ef4d8 | 2016-10-14 15:46:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 17 | #define LOG_TAG "audiohalservice" |
Eric Laurent | 27ef4d8 | 2016-10-14 15:46:06 -0700 | [diff] [blame] | 18 | |
Mikhail Naganov | 9a88b5b | 2021-06-17 19:55:16 +0000 | [diff] [blame] | 19 | #include <signal.h> |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Jasmine Cha | d90c474 | 2022-02-15 16:31:13 +0800 | [diff] [blame] | 23 | #include <android/binder_process.h> |
Haynes Mathew George | a57f41d | 2018-02-21 14:45:20 -0800 | [diff] [blame] | 24 | #include <binder/ProcessState.h> |
Mikhail Naganov | 317a9ed | 2018-09-20 14:25:01 -0700 | [diff] [blame] | 25 | #include <cutils/properties.h> |
Josh Wu | 3f8f599 | 2022-01-18 03:01:19 -0800 | [diff] [blame] | 26 | #include <dlfcn.h> |
Mikhail Naganov | 5697a9b | 2017-12-28 13:29:59 -0800 | [diff] [blame] | 27 | #include <hidl/HidlTransportSupport.h> |
| 28 | #include <hidl/LegacySupport.h> |
Mikhail Naganov | 317a9ed | 2018-09-20 14:25:01 -0700 | [diff] [blame] | 29 | #include <hwbinder/ProcessState.h> |
Eric Laurent | 27ef4d8 | 2016-10-14 15:46:06 -0700 | [diff] [blame] | 30 | |
Kevin Rocard | 91a73b4 | 2018-03-01 18:56:58 -0800 | [diff] [blame] | 31 | using namespace android::hardware; |
Mikhail Naganov | 3c25646 | 2017-02-08 16:34:22 -0800 | [diff] [blame] | 32 | using android::OK; |
| 33 | |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 34 | using InterfacesList = std::vector<std::string>; |
| 35 | |
Kevin Rocard | 5261dce | 2019-06-18 12:40:48 -0700 | [diff] [blame] | 36 | /** Try to register the provided factories in the provided order. |
| 37 | * If any registers successfully, do not register any other and return true. |
| 38 | * If all fail, return false. |
| 39 | */ |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 40 | template <class Iter> |
| 41 | static bool registerPassthroughServiceImplementations(Iter first, Iter last) { |
| 42 | for (; first != last; ++first) { |
| 43 | if (registerPassthroughServiceImplementation(*first) == OK) { |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | return false; |
Kevin Rocard | 5261dce | 2019-06-18 12:40:48 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Josh Wu | 3f8f599 | 2022-01-18 03:01:19 -0800 | [diff] [blame] | 50 | static bool registerExternalServiceImplementation(const std::string& libName, |
| 51 | const std::string& funcName) { |
| 52 | constexpr int dlMode = RTLD_LAZY; |
| 53 | void* handle = nullptr; |
| 54 | dlerror(); // clear |
| 55 | auto libPath = libName + ".so"; |
| 56 | handle = dlopen(libPath.c_str(), dlMode); |
| 57 | if (handle == nullptr) { |
| 58 | const char* error = dlerror(); |
| 59 | ALOGE("Failed to dlopen %s: %s", libPath.c_str(), |
| 60 | error != nullptr ? error : "unknown error"); |
| 61 | return false; |
| 62 | } |
| 63 | binder_status_t (*factoryFunction)(); |
| 64 | *(void**)(&factoryFunction) = dlsym(handle, funcName.c_str()); |
| 65 | if (!factoryFunction) { |
| 66 | const char* error = dlerror(); |
| 67 | ALOGE("Factory function %s not found in libName %s: %s", funcName.c_str(), libPath.c_str(), |
| 68 | error != nullptr ? error : "unknown error"); |
| 69 | dlclose(handle); |
| 70 | return false; |
| 71 | } |
| 72 | return ((*factoryFunction)() == STATUS_OK); |
| 73 | } |
| 74 | |
Eric Laurent | 27ef4d8 | 2016-10-14 15:46:06 -0700 | [diff] [blame] | 75 | int main(int /* argc */, char* /* argv */ []) { |
Mikhail Naganov | 9a88b5b | 2021-06-17 19:55:16 +0000 | [diff] [blame] | 76 | signal(SIGPIPE, SIG_IGN); |
| 77 | |
Mikhail Naganov | 317a9ed | 2018-09-20 14:25:01 -0700 | [diff] [blame] | 78 | ::android::ProcessState::initWithDriver("/dev/vndbinder"); |
Haynes Mathew George | a57f41d | 2018-02-21 14:45:20 -0800 | [diff] [blame] | 79 | // start a threadpool for vndbinder interactions |
Mikhail Naganov | 317a9ed | 2018-09-20 14:25:01 -0700 | [diff] [blame] | 80 | ::android::ProcessState::self()->startThreadPool(); |
| 81 | |
Jasmine Cha | d90c474 | 2022-02-15 16:31:13 +0800 | [diff] [blame] | 82 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 83 | ABinderProcess_startThreadPool(); |
| 84 | |
Mikhail Naganov | 317a9ed | 2018-09-20 14:25:01 -0700 | [diff] [blame] | 85 | const int32_t defaultValue = -1; |
| 86 | int32_t value = |
| 87 | property_get_int32("persist.vendor.audio.service.hwbinder.size_kbyte", defaultValue); |
| 88 | if (value != defaultValue) { |
| 89 | ALOGD("Configuring hwbinder with mmap size %d KBytes", value); |
| 90 | ProcessState::initWithMmapSize(static_cast<size_t>(value) * 1024); |
| 91 | } |
Martijn Coenen | 0282237 | 2016-12-29 14:03:41 +0100 | [diff] [blame] | 92 | configureRpcThreadpool(16, true /*callerWillJoin*/); |
Kevin Rocard | 91a73b4 | 2018-03-01 18:56:58 -0800 | [diff] [blame] | 93 | |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 94 | // Automatic formatting tries to compact the lines, making them less readable |
Kevin Rocard | c69e3e9 | 2019-06-18 15:39:17 -0700 | [diff] [blame] | 95 | // clang-format off |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 96 | const std::vector<InterfacesList> mandatoryInterfaces = { |
| 97 | { |
| 98 | "Audio Core API", |
Mikhail Naganov | 8140f56 | 2022-01-15 01:15:12 +0000 | [diff] [blame] | 99 | "android.hardware.audio@7.1::IDevicesFactory", |
Mikhail Naganov | b444350 | 2021-02-05 00:10:40 +0000 | [diff] [blame] | 100 | "android.hardware.audio@7.0::IDevicesFactory", |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 101 | "android.hardware.audio@6.0::IDevicesFactory", |
| 102 | "android.hardware.audio@5.0::IDevicesFactory", |
| 103 | "android.hardware.audio@4.0::IDevicesFactory", |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 104 | }, |
| 105 | { |
| 106 | "Audio Effect API", |
Mikhail Naganov | b444350 | 2021-02-05 00:10:40 +0000 | [diff] [blame] | 107 | "android.hardware.audio.effect@7.0::IEffectsFactory", |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 108 | "android.hardware.audio.effect@6.0::IEffectsFactory", |
| 109 | "android.hardware.audio.effect@5.0::IEffectsFactory", |
| 110 | "android.hardware.audio.effect@4.0::IEffectsFactory", |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 111 | } |
| 112 | }; |
Kevin Rocard | 91a73b4 | 2018-03-01 18:56:58 -0800 | [diff] [blame] | 113 | |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 114 | const std::vector<InterfacesList> optionalInterfaces = { |
| 115 | { |
| 116 | "Soundtrigger API", |
| 117 | "android.hardware.soundtrigger@2.3::ISoundTriggerHw", |
| 118 | "android.hardware.soundtrigger@2.2::ISoundTriggerHw", |
| 119 | "android.hardware.soundtrigger@2.1::ISoundTriggerHw", |
| 120 | "android.hardware.soundtrigger@2.0::ISoundTriggerHw", |
| 121 | }, |
| 122 | { |
| 123 | "Bluetooth Audio API", |
Alice Kuo | 95ba6a2 | 2021-11-04 19:28:10 +0800 | [diff] [blame] | 124 | "android.hardware.bluetooth.audio@2.2::IBluetoothAudioProvidersFactory", |
Grzegorz Kolodziejczyk | b5f2d23 | 2019-10-24 12:31:20 +0200 | [diff] [blame] | 125 | "android.hardware.bluetooth.audio@2.1::IBluetoothAudioProvidersFactory", |
| 126 | "android.hardware.bluetooth.audio@2.0::IBluetoothAudioProvidersFactory", |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 127 | }, |
| 128 | // remove the old HIDL when Bluetooth Audio Hal V2 has offloading supported |
| 129 | { |
| 130 | "Bluetooth Audio Offload API", |
Mikhail Naganov | 2801b03 | 2020-01-31 14:48:23 -0800 | [diff] [blame] | 131 | "android.hardware.bluetooth.a2dp@1.0::IBluetoothAudioOffload" |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 132 | } |
| 133 | }; |
Josh Wu | 3f8f599 | 2022-01-18 03:01:19 -0800 | [diff] [blame] | 134 | |
| 135 | const std::vector<std::pair<std::string,std::string>> optionalInterfaceSharedLibs = { |
| 136 | { |
| 137 | "android.hardware.bluetooth.audio-impl", |
| 138 | "createIBluetoothAudioProviderFactory", |
| 139 | }, |
| 140 | }; |
Kevin Rocard | c69e3e9 | 2019-06-18 15:39:17 -0700 | [diff] [blame] | 141 | // clang-format on |
Kevin Rocard | 91a73b4 | 2018-03-01 18:56:58 -0800 | [diff] [blame] | 142 | |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 143 | for (const auto& listIter : mandatoryInterfaces) { |
| 144 | auto iter = listIter.begin(); |
| 145 | const std::string& interfaceFamilyName = *iter++; |
| 146 | LOG_ALWAYS_FATAL_IF(!registerPassthroughServiceImplementations(iter, listIter.end()), |
| 147 | "Could not register %s", interfaceFamilyName.c_str()); |
| 148 | } |
Kevin Rocard | 91a73b4 | 2018-03-01 18:56:58 -0800 | [diff] [blame] | 149 | |
Mikhail Naganov | f3e4d21 | 2020-01-29 15:32:24 -0800 | [diff] [blame] | 150 | for (const auto& listIter : optionalInterfaces) { |
| 151 | auto iter = listIter.begin(); |
| 152 | const std::string& interfaceFamilyName = *iter++; |
| 153 | ALOGW_IF(!registerPassthroughServiceImplementations(iter, listIter.end()), |
| 154 | "Could not register %s", interfaceFamilyName.c_str()); |
| 155 | } |
Aniket Kumar Lata | c5a5203 | 2018-01-31 20:17:30 -0800 | [diff] [blame] | 156 | |
Josh Wu | 3f8f599 | 2022-01-18 03:01:19 -0800 | [diff] [blame] | 157 | for (const auto& interfacePair : optionalInterfaceSharedLibs) { |
| 158 | const std::string& libraryName = interfacePair.first; |
| 159 | const std::string& interfaceLoaderFuncName = interfacePair.second; |
| 160 | if (registerExternalServiceImplementation(libraryName, interfaceLoaderFuncName)) { |
| 161 | ALOGI("%s() from %s success", interfaceLoaderFuncName.c_str(), libraryName.c_str()); |
| 162 | } else { |
| 163 | ALOGW("%s() from %s failed", interfaceLoaderFuncName.c_str(), libraryName.c_str()); |
| 164 | } |
| 165 | } |
| 166 | |
Martijn Coenen | 0282237 | 2016-12-29 14:03:41 +0100 | [diff] [blame] | 167 | joinRpcThreadpool(); |
Eric Laurent | 27ef4d8 | 2016-10-14 15:46:06 -0700 | [diff] [blame] | 168 | } |