audiopolicy: Load the engine library dynamically
Android provides 2 audio policy engines:
libaudiopolicyenginedefault and
libaudiopolicyengineconfigurable. This change makes the engine
to be loaded dynamically based on the configuration (currently
the engine name is hardcoded into AudioPolicyConfig). Dynamic
loading allows building and installing of both libraries without
any conflicts.
Technical changes:
- AudioPolicyManagerInterface renamed to EngineInterface
for clarity;
- For the purpose of dynamic loading, APM does not depend
anymore on the EngineInstance class. The class got removed
from the default AP engine, but left in the configurable engine
because it is also used by its plugins;
- Added EngineLibrary class to encapsulate dynamic loading
of the AP engine. The class name EngineInstance is repurposed
for a smart pointer to EngineInterface;
- services/audiopolicy/managerdefault/Android.mk converted
into Android.bp;
- Added engine loading failure test;
Bug: 132639720
Test: sanity tests for audio; audiopolicy_tests
Change-Id: I0581569a172f810e030aec879225e817bfa7851a
Merged-In: I0581569a172f810e030aec879225e817bfa7851a
diff --git a/services/audiopolicy/common/managerdefinitions/include/AudioPolicyConfig.h b/services/audiopolicy/common/managerdefinitions/include/AudioPolicyConfig.h
index 2264d8f..0776a8d 100644
--- a/services/audiopolicy/common/managerdefinitions/include/AudioPolicyConfig.h
+++ b/services/audiopolicy/common/managerdefinitions/include/AudioPolicyConfig.h
@@ -40,7 +40,8 @@
DeviceVector &availableOutputDevices,
DeviceVector &availableInputDevices,
sp<DeviceDescriptor> &defaultOutputDevice)
- : mHwModules(hwModules),
+ : mEngineLibraryNameSuffix(kDefaultEngineLibraryNameSuffix),
+ mHwModules(hwModules),
mAvailableOutputDevices(availableOutputDevices),
mAvailableInputDevices(availableInputDevices),
mDefaultOutputDevice(defaultOutputDevice),
@@ -55,6 +56,14 @@
mSource = file;
}
+ const std::string& getEngineLibraryNameSuffix() const {
+ return mEngineLibraryNameSuffix;
+ }
+
+ void setEngineLibraryNameSuffix(const std::string& suffix) {
+ mEngineLibraryNameSuffix = suffix;
+ }
+
void setHwModules(const HwModuleCollection &hwModules)
{
mHwModules = hwModules;
@@ -108,6 +117,7 @@
void setDefault(void)
{
mSource = "AudioPolicyConfig::setDefault";
+ mEngineLibraryNameSuffix = kDefaultEngineLibraryNameSuffix;
mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
mDefaultOutputDevice->addAudioProfile(AudioProfile::createFullDynamic());
sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
@@ -167,7 +177,10 @@
}
private:
+ static const constexpr char* const kDefaultEngineLibraryNameSuffix = "default";
+
std::string mSource;
+ std::string mEngineLibraryNameSuffix;
HwModuleCollection &mHwModules; /**< Collection of Module, with Profiles, i.e. Mix Ports. */
DeviceVector &mAvailableOutputDevices;
DeviceVector &mAvailableInputDevices;
diff --git a/services/audiopolicy/engine/common/include/EngineBase.h b/services/audiopolicy/engine/common/include/EngineBase.h
index cedc78f..fca9a60 100644
--- a/services/audiopolicy/engine/common/include/EngineBase.h
+++ b/services/audiopolicy/engine/common/include/EngineBase.h
@@ -17,18 +17,18 @@
#pragma once
#include <EngineConfig.h>
-#include <AudioPolicyManagerInterface.h>
+#include <EngineInterface.h>
#include <ProductStrategy.h>
#include <VolumeGroup.h>
namespace android {
namespace audio_policy {
-class EngineBase : public AudioPolicyManagerInterface
+class EngineBase : public EngineInterface
{
public:
///
- /// from AudioPolicyManagerInterface
+ /// from EngineInterface
///
android::status_t initCheck() override;
diff --git a/services/audiopolicy/engine/common/include/ProductStrategy.h b/services/audiopolicy/engine/common/include/ProductStrategy.h
index 1a2a198..c538f52 100644
--- a/services/audiopolicy/engine/common/include/ProductStrategy.h
+++ b/services/audiopolicy/engine/common/include/ProductStrategy.h
@@ -19,7 +19,6 @@
#include "VolumeGroup.h"
#include <system/audio.h>
-#include <AudioPolicyManagerInterface.h>
#include <utils/RefBase.h>
#include <HandleGenerator.h>
#include <string>
@@ -27,6 +26,7 @@
#include <map>
#include <utils/Errors.h>
#include <utils/String8.h>
+#include <media/AudioAttributes.h>
namespace android {
diff --git a/services/audiopolicy/engine/common/include/VolumeCurve.h b/services/audiopolicy/engine/common/include/VolumeCurve.h
index 54314e3..d3d0904 100644
--- a/services/audiopolicy/engine/common/include/VolumeCurve.h
+++ b/services/audiopolicy/engine/common/include/VolumeCurve.h
@@ -18,7 +18,6 @@
#include "IVolumeCurves.h"
#include <policy.h>
-#include <AudioPolicyManagerInterface.h>
#include <utils/RefBase.h>
#include <HandleGenerator.h>
#include <utils/String8.h>
diff --git a/services/audiopolicy/engine/common/include/VolumeGroup.h b/services/audiopolicy/engine/common/include/VolumeGroup.h
index c34b406..5378f64 100644
--- a/services/audiopolicy/engine/common/include/VolumeGroup.h
+++ b/services/audiopolicy/engine/common/include/VolumeGroup.h
@@ -16,7 +16,6 @@
#pragma once
-#include <AudioPolicyManagerInterface.h>
#include <VolumeCurve.h>
#include <system/audio.h>
#include <utils/RefBase.h>
diff --git a/services/audiopolicy/engine/common/src/ProductStrategy.cpp b/services/audiopolicy/engine/common/src/ProductStrategy.cpp
index f74f190..ac3e462 100644
--- a/services/audiopolicy/engine/common/src/ProductStrategy.cpp
+++ b/services/audiopolicy/engine/common/src/ProductStrategy.cpp
@@ -19,6 +19,7 @@
#include "ProductStrategy.h"
+#include <media/AudioProductStrategy.h>
#include <media/TypeConverter.h>
#include <utils/String8.h>
#include <cstdint>
diff --git a/services/audiopolicy/engine/config/src/EngineConfig.cpp b/services/audiopolicy/engine/config/src/EngineConfig.cpp
index 1ad7739..d47fbd2 100644
--- a/services/audiopolicy/engine/config/src/EngineConfig.cpp
+++ b/services/audiopolicy/engine/config/src/EngineConfig.cpp
@@ -32,9 +32,9 @@
#include <istream>
#include <cstdint>
+#include <stdarg.h>
#include <string>
-
namespace android {
using utilities::convertTo;
@@ -603,7 +603,39 @@
return NO_ERROR;
}
+namespace {
+
+class XmlErrorHandler {
+public:
+ XmlErrorHandler() {
+ xmlSetGenericErrorFunc(this, &xmlErrorHandler);
+ }
+ XmlErrorHandler(const XmlErrorHandler&) = delete;
+ XmlErrorHandler(XmlErrorHandler&&) = delete;
+ XmlErrorHandler& operator=(const XmlErrorHandler&) = delete;
+ XmlErrorHandler& operator=(XmlErrorHandler&&) = delete;
+ ~XmlErrorHandler() {
+ xmlSetGenericErrorFunc(NULL, NULL);
+ if (!mErrorMessage.empty()) {
+ ALOG(LOG_ERROR, "libxml2", "%s", mErrorMessage.c_str());
+ }
+ }
+ static void xmlErrorHandler(void* ctx, const char* msg, ...) {
+ char buffer[256];
+ va_list args;
+ va_start(args, msg);
+ vsnprintf(buffer, sizeof(buffer), msg, args);
+ va_end(args);
+ static_cast<XmlErrorHandler*>(ctx)->mErrorMessage += buffer;
+ }
+private:
+ std::string mErrorMessage;
+};
+
+} // namespace
+
ParsingResult parse(const char* path) {
+ XmlErrorHandler errorHandler;
xmlDocPtr doc;
doc = xmlParseFile(path);
if (doc == NULL) {
@@ -641,6 +673,7 @@
}
android::status_t parseLegacyVolumeFile(const char* path, VolumeGroups &volumeGroups) {
+ XmlErrorHandler errorHandler;
xmlDocPtr doc;
doc = xmlParseFile(path);
if (doc == NULL) {
diff --git a/services/audiopolicy/engine/interface/AudioPolicyManagerInterface.h b/services/audiopolicy/engine/interface/EngineInterface.h
similarity index 97%
rename from services/audiopolicy/engine/interface/AudioPolicyManagerInterface.h
rename to services/audiopolicy/engine/interface/EngineInterface.h
index b7fd031..0c58a7c 100644
--- a/services/audiopolicy/engine/interface/AudioPolicyManagerInterface.h
+++ b/services/audiopolicy/engine/interface/EngineInterface.h
@@ -38,7 +38,7 @@
/**
* This interface is dedicated to the policy manager that a Policy Engine shall implement.
*/
-class AudioPolicyManagerInterface
+class EngineInterface
{
public:
/**
@@ -295,7 +295,13 @@
virtual void dump(String8 *dst) const = 0;
protected:
- virtual ~AudioPolicyManagerInterface() {}
+ virtual ~EngineInterface() {}
};
+__attribute__((visibility("default")))
+extern "C" EngineInterface* createEngineInstance();
+
+__attribute__((visibility("default")))
+extern "C" void destroyEngineInstance(EngineInterface *engine);
+
} // namespace android
diff --git a/services/audiopolicy/engineconfigurable/include/AudioPolicyEngineInstance.h b/services/audiopolicy/engineconfigurable/include/AudioPolicyEngineInstance.h
index efc69da..f52de21 100644
--- a/services/audiopolicy/engineconfigurable/include/AudioPolicyEngineInstance.h
+++ b/services/audiopolicy/engineconfigurable/include/AudioPolicyEngineInstance.h
@@ -16,7 +16,7 @@
#pragma once
-class AudioPolicyManagerInterface;
+class EngineInterface;
class AudioPolicyPluginInterface;
namespace android {
@@ -69,7 +69,7 @@
* Compile time error will claim if invalid interface is requested.
*/
template <>
-AudioPolicyManagerInterface *EngineInstance::queryInterface() const;
+EngineInterface *EngineInstance::queryInterface() const;
template <>
AudioPolicyPluginInterface *EngineInstance::queryInterface() const;
diff --git a/services/audiopolicy/engineconfigurable/src/Engine.cpp b/services/audiopolicy/engineconfigurable/src/Engine.cpp
index cb45fcf..c37efca 100644
--- a/services/audiopolicy/engineconfigurable/src/Engine.cpp
+++ b/services/audiopolicy/engineconfigurable/src/Engine.cpp
@@ -361,7 +361,7 @@
}
template <>
-AudioPolicyManagerInterface *Engine::queryInterface()
+EngineInterface *Engine::queryInterface()
{
return this;
}
diff --git a/services/audiopolicy/engineconfigurable/src/Engine.h b/services/audiopolicy/engineconfigurable/src/Engine.h
index 4662e7e..3b371d8 100644
--- a/services/audiopolicy/engineconfigurable/src/Engine.h
+++ b/services/audiopolicy/engineconfigurable/src/Engine.h
@@ -17,7 +17,7 @@
#pragma once
#include "EngineBase.h"
-#include <AudioPolicyManagerInterface.h>
+#include <EngineInterface.h>
#include <AudioPolicyPluginInterface.h>
#include "Collection.h"
diff --git a/services/audiopolicy/engineconfigurable/src/EngineInstance.cpp b/services/audiopolicy/engineconfigurable/src/EngineInstance.cpp
index 2442590..b127796 100644
--- a/services/audiopolicy/engineconfigurable/src/EngineInstance.cpp
+++ b/services/audiopolicy/engineconfigurable/src/EngineInstance.cpp
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include <AudioPolicyManagerInterface.h>
+#include <EngineInterface.h>
#include <AudioPolicyPluginInterface.h>
#include "AudioPolicyEngineInstance.h"
#include "Engine.h"
@@ -45,9 +45,9 @@
}
template <>
-AudioPolicyManagerInterface *EngineInstance::queryInterface() const
+EngineInterface *EngineInstance::queryInterface() const
{
- return getEngine()->queryInterface<AudioPolicyManagerInterface>();
+ return getEngine()->queryInterface<EngineInterface>();
}
template <>
@@ -57,5 +57,16 @@
}
} // namespace audio_policy
+
+extern "C" EngineInterface* createEngineInstance()
+{
+ return audio_policy::EngineInstance::getInstance()->queryInterface<EngineInterface>();
+}
+
+extern "C" void destroyEngineInstance(EngineInterface*)
+{
+ // The engine is a singleton.
+}
+
} // namespace android
diff --git a/services/audiopolicy/enginedefault/Android.bp b/services/audiopolicy/enginedefault/Android.bp
index 7b42c6a..2b9cf09 100644
--- a/services/audiopolicy/enginedefault/Android.bp
+++ b/services/audiopolicy/enginedefault/Android.bp
@@ -1,16 +1,15 @@
cc_library_shared {
name: "libaudiopolicyenginedefault",
- export_include_dirs: ["include"],
srcs: [
"src/Engine.cpp",
"src/EngineInstance.cpp",
],
cflags: [
+ "-fvisibility=hidden",
"-Wall",
"-Werror",
"-Wextra",
],
- local_include_dirs: ["include"],
header_libs: [
"libbase_headers",
"libaudiopolicycommon",
diff --git a/services/audiopolicy/enginedefault/include/AudioPolicyEngineInstance.h b/services/audiopolicy/enginedefault/include/AudioPolicyEngineInstance.h
deleted file mode 100644
index 1e329f0..0000000
--- a/services/audiopolicy/enginedefault/include/AudioPolicyEngineInstance.h
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright (C) 2015 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.
- */
-
-#pragma once
-
-class AudioPolicyManagerInterface;
-
-namespace android
-{
-namespace audio_policy
-{
-
-class Engine;
-
-class EngineInstance
-{
-protected:
- EngineInstance();
-
-public:
- virtual ~EngineInstance();
-
- /**
- * Get Audio Policy Engine instance.
- *
- * @return pointer to Route Manager Instance object.
- */
- static EngineInstance *getInstance();
-
- /**
- * Interface query.
- * The first client of an interface of the policy engine will start the singleton.
- *
- * @tparam RequestedInterface: interface that the client is wishing to retrieve.
- *
- * @return interface handle.
- */
- template <class RequestedInterface>
- RequestedInterface *queryInterface() const;
-
-protected:
- /**
- * Get Audio Policy Engine instance.
- *
- * @return Audio Policy Engine singleton.
- */
- Engine *getEngine() const;
-
-private:
- /* Copy facilities are put private to disable copy. */
- EngineInstance(const EngineInstance &object);
- EngineInstance &operator=(const EngineInstance &object);
-};
-
-/**
- * Limit template instantation to supported type interfaces.
- * Compile time error will claim if invalid interface is requested.
- */
-template <>
-AudioPolicyManagerInterface *EngineInstance::queryInterface() const;
-
-} // namespace audio_policy
-} // namespace android
diff --git a/services/audiopolicy/enginedefault/src/Engine.cpp b/services/audiopolicy/enginedefault/src/Engine.cpp
index 04170ac..c602f3a 100644
--- a/services/audiopolicy/enginedefault/src/Engine.cpp
+++ b/services/audiopolicy/enginedefault/src/Engine.cpp
@@ -762,12 +762,6 @@
AUDIO_FORMAT_DEFAULT);
}
-template <>
-AudioPolicyManagerInterface *Engine::queryInterface()
-{
- return this;
-}
-
} // namespace audio_policy
} // namespace android
diff --git a/services/audiopolicy/enginedefault/src/Engine.h b/services/audiopolicy/enginedefault/src/Engine.h
index d5dfacc..62938cf 100644
--- a/services/audiopolicy/enginedefault/src/Engine.h
+++ b/services/audiopolicy/enginedefault/src/Engine.h
@@ -17,7 +17,7 @@
#pragma once
#include "EngineBase.h"
-#include "AudioPolicyManagerInterface.h"
+#include "EngineInterface.h"
#include <AudioGain.h>
#include <policy.h>
@@ -48,12 +48,9 @@
Engine();
virtual ~Engine() = default;
- template <class RequestedInterface>
- RequestedInterface *queryInterface();
-
private:
///
- /// from EngineBase, so from AudioPolicyManagerInterface
+ /// from EngineBase, so from EngineInterface
///
status_t setForceUse(audio_policy_force_use_t usage,
audio_policy_forced_cfg_t config) override;
diff --git a/services/audiopolicy/enginedefault/src/EngineInstance.cpp b/services/audiopolicy/enginedefault/src/EngineInstance.cpp
index 17e9832..eeb3758 100644
--- a/services/audiopolicy/enginedefault/src/EngineInstance.cpp
+++ b/services/audiopolicy/enginedefault/src/EngineInstance.cpp
@@ -14,41 +14,21 @@
* limitations under the License.
*/
-#include <AudioPolicyManagerInterface.h>
-#include "AudioPolicyEngineInstance.h"
+#include <EngineInterface.h>
#include "Engine.h"
-namespace android
-{
-namespace audio_policy
-{
+namespace android {
+namespace audio_policy {
-EngineInstance::EngineInstance()
+extern "C" EngineInterface* createEngineInstance()
{
+ return new (std::nothrow) Engine();
}
-EngineInstance *EngineInstance::getInstance()
+extern "C" void destroyEngineInstance(EngineInterface *engine)
{
- static EngineInstance instance;
- return &instance;
-}
-
-EngineInstance::~EngineInstance()
-{
-}
-
-Engine *EngineInstance::getEngine() const
-{
- static Engine engine;
- return &engine;
-}
-
-template <>
-AudioPolicyManagerInterface *EngineInstance::queryInterface() const
-{
- return getEngine()->queryInterface<AudioPolicyManagerInterface>();
+ delete static_cast<Engine*>(engine);
}
} // namespace audio_policy
} // namespace android
-
diff --git a/services/audiopolicy/managerdefault/Android.bp b/services/audiopolicy/managerdefault/Android.bp
new file mode 100644
index 0000000..8fbeff9
--- /dev/null
+++ b/services/audiopolicy/managerdefault/Android.bp
@@ -0,0 +1,43 @@
+cc_library_shared {
+ name: "libaudiopolicymanagerdefault",
+
+ srcs: [
+ "AudioPolicyManager.cpp",
+ "EngineLibrary.cpp",
+ ],
+
+ export_include_dirs: ["."],
+
+ shared_libs: [
+ "libcutils",
+ "libdl",
+ "libutils",
+ "liblog",
+ "libaudiopolicy",
+ "libsoundtrigger",
+ "libmedia_helper",
+ "libmediametrics",
+ "libbinder",
+ "libhidlbase",
+ "libxml2",
+ // The default audio policy engine is always present in the system image.
+ // libaudiopolicyengineconfigurable can be built in addition by specifying
+ // a dependency on it in the device makefile. There will be no build time
+ // conflict with libaudiopolicyenginedefault.
+ "libaudiopolicyenginedefault",
+ ],
+
+ header_libs: [
+ "libaudiopolicycommon",
+ "libaudiopolicyengine_interface_headers",
+ "libaudiopolicymanager_interface_headers",
+ ],
+
+ static_libs: ["libaudiopolicycomponents"],
+
+ cflags: [
+ "-Wall",
+ "-Werror",
+ ],
+
+}
diff --git a/services/audiopolicy/managerdefault/Android.mk b/services/audiopolicy/managerdefault/Android.mk
deleted file mode 100644
index c5921c3..0000000
--- a/services/audiopolicy/managerdefault/Android.mk
+++ /dev/null
@@ -1,48 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:= AudioPolicyManager.cpp
-
-LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
-
-LOCAL_SHARED_LIBRARIES := \
- libcutils \
- libutils \
- liblog \
- libaudiopolicy \
- libsoundtrigger
-
-ifeq ($(USE_CONFIGURABLE_AUDIO_POLICY), 1)
-
-LOCAL_SHARED_LIBRARIES += libaudiopolicyengineconfigurable
-
-else
-
-LOCAL_SHARED_LIBRARIES += libaudiopolicyenginedefault
-
-endif # ifeq ($(USE_CONFIGURABLE_AUDIO_POLICY), 1)
-
-LOCAL_C_INCLUDES += \
- $(call include-path-for, audio-utils)
-
-LOCAL_HEADER_LIBRARIES := \
- libaudiopolicycommon \
- libaudiopolicyengine_interface_headers \
- libaudiopolicymanager_interface_headers
-
-LOCAL_STATIC_LIBRARIES := \
- libaudiopolicycomponents
-
-LOCAL_SHARED_LIBRARIES += libmedia_helper
-LOCAL_SHARED_LIBRARIES += libmediametrics
-
-LOCAL_SHARED_LIBRARIES += libbinder libhidlbase libxml2
-
-LOCAL_CFLAGS += -Wall -Werror
-
-LOCAL_MULTILIB := $(AUDIOSERVER_MULTILIB)
-
-LOCAL_MODULE:= libaudiopolicymanagerdefault
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
index a8fd9cd..39f4072 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
@@ -42,8 +42,6 @@
#include <set>
#include <unordered_set>
#include <vector>
-#include <AudioPolicyManagerInterface.h>
-#include <AudioPolicyEngineInstance.h>
#include <cutils/properties.h>
#include <utils/Log.h>
#include <media/AudioParameter.h>
@@ -4304,17 +4302,18 @@
}
status_t AudioPolicyManager::initialize() {
- // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
- audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
- if (!engineInstance) {
- ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
- return NO_INIT;
- }
- // Retrieve the Policy Manager Interface
- mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
- if (mEngine == NULL) {
- ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
- return NO_INIT;
+ {
+ auto engLib = EngineLibrary::load(
+ "libaudiopolicyengine" + getConfig().getEngineLibraryNameSuffix() + ".so");
+ if (!engLib) {
+ ALOGE("%s: Failed to load the engine library", __FUNCTION__);
+ return NO_INIT;
+ }
+ mEngine = engLib->createEngine();
+ if (mEngine == nullptr) {
+ ALOGE("%s: Failed to instantiate the APM engine", __FUNCTION__);
+ return NO_INIT;
+ }
}
mEngine->setObserver(this);
status_t status = mEngine->initCheck();
diff --git a/services/audiopolicy/managerdefault/AudioPolicyManager.h b/services/audiopolicy/managerdefault/AudioPolicyManager.h
index 612bd8f..d88d1ec 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.h
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.h
@@ -34,7 +34,6 @@
#include <media/PatchBuilder.h>
#include "AudioPolicyInterface.h"
-#include <AudioPolicyManagerInterface.h>
#include <AudioPolicyManagerObserver.h>
#include <AudioGain.h>
#include <AudioPolicyConfig.h>
@@ -49,6 +48,7 @@
#include <AudioPolicyMix.h>
#include <EffectDescriptor.h>
#include <SoundTriggerSession.h>
+#include "EngineLibrary.h"
#include "TypeConverter.h"
namespace android {
@@ -752,7 +752,7 @@
uint32_t nextAudioPortGeneration();
// Audio Policy Engine Interface.
- AudioPolicyManagerInterface *mEngine;
+ EngineInstance mEngine;
// Surround formats that are enabled manually. Taken into account when
// "encoded surround" is forced into "manual" mode.
diff --git a/services/audiopolicy/managerdefault/EngineLibrary.cpp b/services/audiopolicy/managerdefault/EngineLibrary.cpp
new file mode 100644
index 0000000..ef699aa
--- /dev/null
+++ b/services/audiopolicy/managerdefault/EngineLibrary.cpp
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2019 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 "APM_EngineLoader"
+
+#include <dlfcn.h>
+#include <utils/Log.h>
+
+#include "EngineLibrary.h"
+
+namespace android {
+
+// static
+std::shared_ptr<EngineLibrary> EngineLibrary::load(std::string libraryPath)
+{
+ std::shared_ptr<EngineLibrary> engLib(new EngineLibrary());
+ return engLib->init(std::move(libraryPath)) ? engLib : nullptr;
+}
+
+EngineLibrary::~EngineLibrary()
+{
+ close();
+}
+
+bool EngineLibrary::init(std::string libraryPath)
+{
+ mLibraryHandle = dlopen(libraryPath.c_str(), 0);
+ if (mLibraryHandle == nullptr) {
+ ALOGE("Could not dlopen %s: %s", libraryPath.c_str(), dlerror());
+ return false;
+ }
+ mCreateEngineInstance = (EngineInterface* (*)())dlsym(mLibraryHandle, "createEngineInstance");
+ mDestroyEngineInstance = (void (*)(EngineInterface*))dlsym(
+ mLibraryHandle, "destroyEngineInstance");
+ if (mCreateEngineInstance == nullptr || mDestroyEngineInstance == nullptr) {
+ ALOGE("Could not find engine interface functions in %s", libraryPath.c_str());
+ close();
+ return false;
+ }
+ ALOGD("Loaded engine from %s", libraryPath.c_str());
+ return true;
+}
+
+EngineInstance EngineLibrary::createEngine()
+{
+ if (mCreateEngineInstance == nullptr || mDestroyEngineInstance == nullptr) {
+ return EngineInstance();
+ }
+ return EngineInstance(mCreateEngineInstance(),
+ [lib = shared_from_this(), destroy = mDestroyEngineInstance] (EngineInterface* e) {
+ destroy(e);
+ });
+}
+
+void EngineLibrary::close()
+{
+ if (mLibraryHandle != nullptr) {
+ dlclose(mLibraryHandle);
+ }
+ mLibraryHandle = nullptr;
+ mCreateEngineInstance = nullptr;
+ mDestroyEngineInstance = nullptr;
+}
+
+} // namespace android
diff --git a/services/audiopolicy/managerdefault/EngineLibrary.h b/services/audiopolicy/managerdefault/EngineLibrary.h
new file mode 100644
index 0000000..f143916
--- /dev/null
+++ b/services/audiopolicy/managerdefault/EngineLibrary.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2019 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.
+ */
+
+#pragma once
+
+#include <functional>
+#include <memory>
+#include <string>
+
+#include <EngineInterface.h>
+
+namespace android {
+
+using EngineInstance = std::unique_ptr<EngineInterface, std::function<void (EngineInterface*)>>;
+
+class EngineLibrary : public std::enable_shared_from_this<EngineLibrary> {
+public:
+ static std::shared_ptr<EngineLibrary> load(std::string libraryPath);
+ ~EngineLibrary();
+
+ EngineLibrary(const EngineLibrary&) = delete;
+ EngineLibrary(EngineLibrary&&) = delete;
+ EngineLibrary& operator=(const EngineLibrary&) = delete;
+ EngineLibrary& operator=(EngineLibrary&&) = delete;
+
+ EngineInstance createEngine();
+
+private:
+ EngineLibrary() = default;
+ bool init(std::string libraryPath);
+ void close();
+
+ void *mLibraryHandle = nullptr;
+ EngineInterface* (*mCreateEngineInstance)() = nullptr;
+ void (*mDestroyEngineInstance)(EngineInterface*) = nullptr;
+};
+
+} // namespace android
diff --git a/services/audiopolicy/tests/audiopolicymanager_tests.cpp b/services/audiopolicy/tests/audiopolicymanager_tests.cpp
index de5670c..e10a716 100644
--- a/services/audiopolicy/tests/audiopolicymanager_tests.cpp
+++ b/services/audiopolicy/tests/audiopolicymanager_tests.cpp
@@ -30,7 +30,16 @@
using namespace android;
-TEST(AudioPolicyManagerTestInit, Failure) {
+TEST(AudioPolicyManagerTestInit, EngineFailure) {
+ AudioPolicyTestClient client;
+ AudioPolicyTestManager manager(&client);
+ manager.getConfig().setDefault();
+ manager.getConfig().setEngineLibraryNameSuffix("non-existent");
+ ASSERT_EQ(NO_INIT, manager.initialize());
+ ASSERT_EQ(NO_INIT, manager.initCheck());
+}
+
+TEST(AudioPolicyManagerTestInit, ClientFailure) {
AudioPolicyTestClient client;
AudioPolicyTestManager manager(&client);
manager.getConfig().setDefault();