libaudiohal@aidl: Provide an example IHalAdapterVendorExtension
This service is necessary for any vendor because all vendor
HAL implementations have some vendor-specific parameters.
An example implementation is added, which is used by Cuttlefish.
Updated the framework client code to handle service crashes
and rebind automatically. This allows to avoid restarting
the audioserver when the parameter parser service crashes.
Bug: 321651892
Test: atest audiorouting_tests
Change-Id: Iabb983c11e581bd4d44798d2a3af6683f326546c
diff --git a/media/libaudiohal/impl/DevicesFactoryHalAidl.cpp b/media/libaudiohal/impl/DevicesFactoryHalAidl.cpp
index 3dbc14a..01fc7fb 100644
--- a/media/libaudiohal/impl/DevicesFactoryHalAidl.cpp
+++ b/media/libaudiohal/impl/DevicesFactoryHalAidl.cpp
@@ -17,12 +17,14 @@
#include <algorithm>
#include <map>
#include <memory>
+#include <mutex>
#include <string>
#define LOG_TAG "DevicesFactoryHalAidl"
//#define LOG_NDEBUG 0
#include <aidl/android/hardware/audio/core/IModule.h>
+#include <aidl/android/media/audio/BnHalAdapterVendorExtension.h>
#include <android/binder_manager.h>
#include <media/AidlConversionNdkCpp.h>
#include <media/AidlConversionUtil.h>
@@ -35,6 +37,7 @@
using aidl::android::hardware::audio::core::IConfig;
using aidl::android::hardware::audio::core::IModule;
using aidl::android::hardware::audio::core::SurroundSoundConfig;
+using aidl::android::hardware::audio::core::VendorParameter;
using aidl::android::media::audio::common::AudioHalEngineConfig;
using aidl::android::media::audio::IHalAdapterVendorExtension;
using android::detail::AudioHalVersionInfo;
@@ -62,10 +65,84 @@
return cpp;
}
+class HalAdapterVendorExtensionWrapper :
+ public ::aidl::android::media::audio::BnHalAdapterVendorExtension {
+ private:
+ template<typename F>
+ ndk::ScopedAStatus callWithRetryOnCrash(F method) {
+ ndk::ScopedAStatus status = ndk::ScopedAStatus::ok();
+ for (auto service = getService(); service != nullptr; service = getService(true)) {
+ status = method(service);
+ if (status.getStatus() != STATUS_DEAD_OBJECT) break;
+ }
+ return status;
+ }
+
+ ndk::ScopedAStatus parseVendorParameterIds(ParameterScope in_scope,
+ const std::string& in_rawKeys,
+ std::vector<std::string>* _aidl_return) override {
+ return callWithRetryOnCrash([&](auto service) {
+ return service->parseVendorParameterIds(in_scope, in_rawKeys, _aidl_return);
+ });
+ }
+
+ ndk::ScopedAStatus parseVendorParameters(
+ ParameterScope in_scope, const std::string& in_rawKeysAndValues,
+ std::vector<VendorParameter>* out_syncParameters,
+ std::vector<VendorParameter>* out_asyncParameters) override {
+ return callWithRetryOnCrash([&](auto service) {
+ return service->parseVendorParameters(in_scope, in_rawKeysAndValues,
+ out_syncParameters, out_asyncParameters);
+ });
+ }
+
+ ndk::ScopedAStatus parseBluetoothA2dpReconfigureOffload(
+ const std::string& in_rawValue, std::vector<VendorParameter>* _aidl_return) override {
+ return callWithRetryOnCrash([&](auto service) {
+ return service->parseBluetoothA2dpReconfigureOffload(in_rawValue, _aidl_return);
+ });
+ }
+
+ ndk::ScopedAStatus parseBluetoothLeReconfigureOffload(const std::string& in_rawValue,
+ std::vector<VendorParameter>* _aidl_return) override {
+ return callWithRetryOnCrash([&](auto service) {
+ return service->parseBluetoothLeReconfigureOffload(in_rawValue, _aidl_return);
+ });
+ }
+
+ ndk::ScopedAStatus processVendorParameters(ParameterScope in_scope,
+ const std::vector<VendorParameter>& in_parameters,
+ std::string* _aidl_return) override {
+ return callWithRetryOnCrash([&](auto service) {
+ return service->processVendorParameters(in_scope, in_parameters, _aidl_return);
+ });
+ }
+
+ std::shared_ptr<IHalAdapterVendorExtension> getService(bool reset = false) {
+ std::lock_guard l(mLock);
+ if (reset || !mVendorExt.has_value()) {
+ auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
+ if (AServiceManager_isDeclared(serviceName.c_str())) {
+ mVendorExt = std::shared_ptr<IHalAdapterVendorExtension>(
+ IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
+ AServiceManager_waitForService(serviceName.c_str()))));
+ } else {
+ mVendorExt = nullptr;
+ }
+ }
+ return mVendorExt.value();
+ }
+
+ std::mutex mLock;
+ std::optional<std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>>
+ mVendorExt GUARDED_BY(mLock);
+};
+
} // namespace
DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config)
- : mConfig(std::move(config)) {
+ : mConfig(std::move(config)),
+ mVendorExt(ndk::SharedRefBase::make<HalAdapterVendorExtensionWrapper>()) {
}
status_t DevicesFactoryHalAidl::getDeviceNames(std::vector<std::string> *names) {
@@ -110,7 +187,7 @@
ALOGE("%s fromBinder %s failed", __func__, serviceName.c_str());
return NO_INIT;
}
- *device = sp<DeviceHalAidl>::make(name, service, getVendorExtension());
+ *device = sp<DeviceHalAidl>::make(name, service, mVendorExt);
return OK;
}
@@ -149,20 +226,6 @@
return OK;
}
-std::shared_ptr<IHalAdapterVendorExtension> DevicesFactoryHalAidl::getVendorExtension() {
- if (!mVendorExt.has_value()) {
- auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
- if (AServiceManager_isDeclared(serviceName.c_str())) {
- mVendorExt = std::shared_ptr<IHalAdapterVendorExtension>(
- IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
- AServiceManager_waitForService(serviceName.c_str()))));
- } else {
- mVendorExt = nullptr;
- }
- }
- return mVendorExt.value();
-}
-
// Main entry-point to the shared library.
extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
auto serviceName = std::string(IConfig::descriptor) + "/default";