libaudiohal@aidl: Make IHalAdapterVendorExtension mandatory

Since IHalAdapterVendorExtension is not stable, it can not be
included into the framework manifest, and thus `isDeclared`
check will never succeed (due to a bug, it could be included
previously, but it is now fixed).

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
and also added to GSI system_ext.

Bug: 321651892
Test: atest audiorouting_tests
Change-Id: Ib529250349da289cecb26bb965f1bfc8e37dc893
diff --git a/media/libaudiohal/impl/DevicesFactoryHalAidl.cpp b/media/libaudiohal/impl/DevicesFactoryHalAidl.cpp
index 3dbc14a..24df32d 100644
--- a/media/libaudiohal/impl/DevicesFactoryHalAidl.cpp
+++ b/media/libaudiohal/impl/DevicesFactoryHalAidl.cpp
@@ -23,6 +23,7 @@
 //#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 +36,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 +64,86 @@
     return cpp;
 }
 
+class HalAdapterVendorExtensionWrapper :
+            public ::aidl::android::media::audio::BnHalAdapterVendorExtension {
+  private:
+    ndk::ScopedAStatus parseVendorParameterIds(ParameterScope in_scope,
+                                               const std::string& in_rawKeys,
+                                               std::vector<std::string>* _aidl_return) override {
+        ndk::ScopedAStatus status;
+        while (true) {
+            status = get()->parseVendorParameterIds(in_scope, in_rawKeys, _aidl_return);
+            if (status.getStatus() != STATUS_DEAD_OBJECT) break;
+            mVendorExt.reset();
+        }
+        return status;
+    }
+
+    ndk::ScopedAStatus parseVendorParameters(
+            ParameterScope in_scope, const std::string& in_rawKeysAndValues,
+            std::vector<VendorParameter>* out_syncParameters,
+            std::vector<VendorParameter>* out_asyncParameters) override {
+        ndk::ScopedAStatus status;
+        while (true) {
+            status = get()->parseVendorParameters(in_scope, in_rawKeysAndValues,
+                    out_syncParameters, out_asyncParameters);
+            if (status.getStatus() != STATUS_DEAD_OBJECT) break;
+            mVendorExt.reset();
+        }
+        return status;
+    }
+
+    ndk::ScopedAStatus parseBluetoothA2dpReconfigureOffload(
+            const std::string& in_rawValue, std::vector<VendorParameter>* _aidl_return) override {
+        ndk::ScopedAStatus status;
+        while (true) {
+            status = get()->parseBluetoothA2dpReconfigureOffload(in_rawValue, _aidl_return);
+            if (status.getStatus() != STATUS_DEAD_OBJECT) break;
+            mVendorExt.reset();
+        }
+        return status;
+    }
+
+    ndk::ScopedAStatus parseBluetoothLeReconfigureOffload(const std::string& in_rawValue,
+            std::vector<VendorParameter>* _aidl_return) override {
+        ndk::ScopedAStatus status;
+        while (true) {
+            status = get()->parseBluetoothLeReconfigureOffload(in_rawValue, _aidl_return);
+            if (status.getStatus() != STATUS_DEAD_OBJECT) break;
+            mVendorExt.reset();
+        }
+        return status;
+    }
+
+    ndk::ScopedAStatus processVendorParameters(ParameterScope in_scope,
+                                               const std::vector<VendorParameter>& in_parameters,
+                                               std::string* _aidl_return) override {
+        ndk::ScopedAStatus status;
+        while (true) {
+            status = get()->processVendorParameters(in_scope, in_parameters, _aidl_return);
+            if (status.getStatus() != STATUS_DEAD_OBJECT) break;
+            mVendorExt.reset();
+        }
+        return status;
+    }
+
+    std::shared_ptr<IHalAdapterVendorExtension> get() {
+        if (!mVendorExt) {
+            auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
+            mVendorExt = IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
+                            AServiceManager_waitForService(serviceName.c_str())));
+        }
+        return mVendorExt;
+    }
+
+    std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> mVendorExt;
+};
+
 }  // 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 +188,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 +227,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";