Loading media/libaudiohal/Android.bp +8 −1 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ cc_library_shared { srcs: [ "DevicesFactoryHalInterface.cpp", "EffectsFactoryHalInterface.cpp", "FactoryHalHidl.cpp", ], cflags: [ Loading @@ -12,11 +13,17 @@ cc_library_shared { "-Werror", ], shared_libs: [ required: [ "libaudiohal@2.0", "libaudiohal@4.0", "libaudiohal@5.0", "libaudiohal@6.0", ], shared_libs: [ "libdl", "libhidlbase", "liblog", "libutils", ], Loading media/libaudiohal/DevicesFactoryHalInterface.cpp +3 −4 Original line number Diff line number Diff line Loading @@ -14,16 +14,15 @@ * limitations under the License. */ #include <libaudiohal/FactoryHalHidl.h> #include <media/audiohal/DevicesFactoryHalInterface.h> #include <media/audiohal/FactoryHalHidl.h> namespace android { // static sp<DevicesFactoryHalInterface> DevicesFactoryHalInterface::create() { return createPreferedImpl<DevicesFactoryHalInterface>(); return createPreferredImpl<DevicesFactoryHalInterface>( "android.hardware.audio", "IDevicesFactory"); } } // namespace android media/libaudiohal/EffectsFactoryHalInterface.cpp +3 −3 Original line number Diff line number Diff line Loading @@ -14,15 +14,15 @@ * limitations under the License. */ #include <libaudiohal/FactoryHalHidl.h> #include <media/audiohal/EffectsFactoryHalInterface.h> #include <media/audiohal/FactoryHalHidl.h> namespace android { // static sp<EffectsFactoryHalInterface> EffectsFactoryHalInterface::create() { return createPreferedImpl<EffectsFactoryHalInterface>(); return createPreferredImpl<EffectsFactoryHalInterface>( "android.hardware.audio.effect", "IEffectsFactory"); } // static Loading media/libaudiohal/FactoryHalHidl.cpp 0 → 100644 +107 −0 Original line number Diff line number Diff line /* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "FactoryHalHidl" #include <media/audiohal/FactoryHalHidl.h> #include <dlfcn.h> #include <android/hidl/manager/1.0/IServiceManager.h> #include <hidl/ServiceManagement.h> #include <hidl/Status.h> #include <utils/Log.h> namespace android::detail { namespace { /** Supported HAL versions, in order of preference. */ const char* sAudioHALVersions[] = { "6.0", "5.0", "4.0", "2.0", nullptr }; bool createHalService(const std::string& version, const std::string& interface, void** rawInterface) { const std::string libName = "libaudiohal@" + version + ".so"; const std::string factoryFunctionName = "create" + interface; constexpr int dlMode = RTLD_LAZY; void* handle = nullptr; dlerror(); // clear handle = dlopen(libName.c_str(), dlMode); if (handle == nullptr) { const char* error = dlerror(); ALOGE("Failed to dlopen %s: %s", libName.c_str(), error != nullptr ? error : "unknown error"); return false; } void* (*factoryFunction)(); *(void **)(&factoryFunction) = dlsym(handle, factoryFunctionName.c_str()); if (!factoryFunction) { const char* error = dlerror(); ALOGE("Factory function %s not found in library %s: %s", factoryFunctionName.c_str(), libName.c_str(), error != nullptr ? error : "unknown error"); dlclose(handle); return false; } *rawInterface = (*factoryFunction)(); ALOGW_IF(!*rawInterface, "Factory function %s from %s returned nullptr", factoryFunctionName.c_str(), libName.c_str()); return true; } bool hasHalService(const std::string& package, const std::string& version, const std::string& interface) { using ::android::hidl::manager::V1_0::IServiceManager; sp<IServiceManager> sm = ::android::hardware::defaultServiceManager(); if (!sm) { ALOGE("Failed to obtain HIDL ServiceManager"); return false; } // Since audio HAL doesn't support multiple clients, avoid instantiating // the interface right away. Instead, query the transport type for it. using ::android::hardware::Return; using Transport = IServiceManager::Transport; const std::string fqName = package + "@" + version + "::" + interface; const std::string instance = "default"; Return<Transport> transport = sm->getTransport(fqName, instance); if (!transport.isOk()) { ALOGE("Failed to obtain transport type for %s/%s: %s", fqName.c_str(), instance.c_str(), transport.description().c_str()); return false; } return transport != Transport::EMPTY; } } // namespace void* createPreferredImpl(const std::string& package, const std::string& interface) { for (auto version = detail::sAudioHALVersions; version != nullptr; ++version) { void* rawInterface = nullptr; if (hasHalService(package, *version, interface) && createHalService(*version, interface, &rawInterface)) { return rawInterface; } } return nullptr; } } // namespace android::detail media/libaudiohal/impl/Android.bp +1 −2 Original line number Diff line number Diff line Loading @@ -16,12 +16,11 @@ cc_defaults { "StreamHalHidl.cpp", ], export_include_dirs: ["include"], cflags: [ "-Wall", "-Wextra", "-Werror", "-fvisibility=hidden", ], shared_libs: [ "android.hardware.audio.common-util", Loading Loading
media/libaudiohal/Android.bp +8 −1 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ cc_library_shared { srcs: [ "DevicesFactoryHalInterface.cpp", "EffectsFactoryHalInterface.cpp", "FactoryHalHidl.cpp", ], cflags: [ Loading @@ -12,11 +13,17 @@ cc_library_shared { "-Werror", ], shared_libs: [ required: [ "libaudiohal@2.0", "libaudiohal@4.0", "libaudiohal@5.0", "libaudiohal@6.0", ], shared_libs: [ "libdl", "libhidlbase", "liblog", "libutils", ], Loading
media/libaudiohal/DevicesFactoryHalInterface.cpp +3 −4 Original line number Diff line number Diff line Loading @@ -14,16 +14,15 @@ * limitations under the License. */ #include <libaudiohal/FactoryHalHidl.h> #include <media/audiohal/DevicesFactoryHalInterface.h> #include <media/audiohal/FactoryHalHidl.h> namespace android { // static sp<DevicesFactoryHalInterface> DevicesFactoryHalInterface::create() { return createPreferedImpl<DevicesFactoryHalInterface>(); return createPreferredImpl<DevicesFactoryHalInterface>( "android.hardware.audio", "IDevicesFactory"); } } // namespace android
media/libaudiohal/EffectsFactoryHalInterface.cpp +3 −3 Original line number Diff line number Diff line Loading @@ -14,15 +14,15 @@ * limitations under the License. */ #include <libaudiohal/FactoryHalHidl.h> #include <media/audiohal/EffectsFactoryHalInterface.h> #include <media/audiohal/FactoryHalHidl.h> namespace android { // static sp<EffectsFactoryHalInterface> EffectsFactoryHalInterface::create() { return createPreferedImpl<EffectsFactoryHalInterface>(); return createPreferredImpl<EffectsFactoryHalInterface>( "android.hardware.audio.effect", "IEffectsFactory"); } // static Loading
media/libaudiohal/FactoryHalHidl.cpp 0 → 100644 +107 −0 Original line number Diff line number Diff line /* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "FactoryHalHidl" #include <media/audiohal/FactoryHalHidl.h> #include <dlfcn.h> #include <android/hidl/manager/1.0/IServiceManager.h> #include <hidl/ServiceManagement.h> #include <hidl/Status.h> #include <utils/Log.h> namespace android::detail { namespace { /** Supported HAL versions, in order of preference. */ const char* sAudioHALVersions[] = { "6.0", "5.0", "4.0", "2.0", nullptr }; bool createHalService(const std::string& version, const std::string& interface, void** rawInterface) { const std::string libName = "libaudiohal@" + version + ".so"; const std::string factoryFunctionName = "create" + interface; constexpr int dlMode = RTLD_LAZY; void* handle = nullptr; dlerror(); // clear handle = dlopen(libName.c_str(), dlMode); if (handle == nullptr) { const char* error = dlerror(); ALOGE("Failed to dlopen %s: %s", libName.c_str(), error != nullptr ? error : "unknown error"); return false; } void* (*factoryFunction)(); *(void **)(&factoryFunction) = dlsym(handle, factoryFunctionName.c_str()); if (!factoryFunction) { const char* error = dlerror(); ALOGE("Factory function %s not found in library %s: %s", factoryFunctionName.c_str(), libName.c_str(), error != nullptr ? error : "unknown error"); dlclose(handle); return false; } *rawInterface = (*factoryFunction)(); ALOGW_IF(!*rawInterface, "Factory function %s from %s returned nullptr", factoryFunctionName.c_str(), libName.c_str()); return true; } bool hasHalService(const std::string& package, const std::string& version, const std::string& interface) { using ::android::hidl::manager::V1_0::IServiceManager; sp<IServiceManager> sm = ::android::hardware::defaultServiceManager(); if (!sm) { ALOGE("Failed to obtain HIDL ServiceManager"); return false; } // Since audio HAL doesn't support multiple clients, avoid instantiating // the interface right away. Instead, query the transport type for it. using ::android::hardware::Return; using Transport = IServiceManager::Transport; const std::string fqName = package + "@" + version + "::" + interface; const std::string instance = "default"; Return<Transport> transport = sm->getTransport(fqName, instance); if (!transport.isOk()) { ALOGE("Failed to obtain transport type for %s/%s: %s", fqName.c_str(), instance.c_str(), transport.description().c_str()); return false; } return transport != Transport::EMPTY; } } // namespace void* createPreferredImpl(const std::string& package, const std::string& interface) { for (auto version = detail::sAudioHALVersions; version != nullptr; ++version) { void* rawInterface = nullptr; if (hasHalService(package, *version, interface) && createHalService(*version, interface, &rawInterface)) { return rawInterface; } } return nullptr; } } // namespace android::detail
media/libaudiohal/impl/Android.bp +1 −2 Original line number Diff line number Diff line Loading @@ -16,12 +16,11 @@ cc_defaults { "StreamHalHidl.cpp", ], export_include_dirs: ["include"], cflags: [ "-Wall", "-Wextra", "-Werror", "-fvisibility=hidden", ], shared_libs: [ "android.hardware.audio.common-util", Loading