Skip to content

Commit

Permalink
Revised AIDL for callback API.
Browse files Browse the repository at this point in the history
Now it does receives `requestProcess()` instead of `notify()` that merely
requests host to call `process()`.

It is part of #73
  • Loading branch information
atsushieno committed Mar 18, 2023
1 parent 9705502 commit 685ed73
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.androidaudioplugin;

oneway interface AudioPluginInterfaceCallback {
void notify(int instanceId, String uri, int size);
void requestProcess(int instanceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class AudioPluginInterfaceImpl : public aidl::org::androidaudioplugin::BnAudioPl

AudioPluginInterfaceImpl() {
plugins = PluginListSnapshot::queryServices();
svc.reset(new PluginService(&plugins));
svc.reset(new PluginService(&plugins, [&](int32_t instanceId) {
if (callback)
callback->requestProcess(instanceId);
}));
aap::PluginServiceList::getInstance()->addBoundServiceInProcess(svc.get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,32 @@ class AndroidPluginClientSystem : public PluginClientSystem {
class AndroidPluginClientConnectionData {
ndk::SpAIBinder spAIBinder;
std::shared_ptr<aidl::org::androidaudioplugin::IAudioPluginInterface> proxy;
std::shared_ptr<aidl::org::androidaudioplugin::IAudioPluginInterfaceCallback> callback;
std::function<::ndk::ScopedAStatus(int32_t)> handle_request_process;

class AudioPluginCallback : public aidl::org::androidaudioplugin::IAudioPluginInterfaceCallbackDefault {
AndroidPluginClientConnectionData* owner;

public:
AudioPluginCallback(AndroidPluginClientConnectionData* owner) : owner(owner) {}
::ndk::ScopedAStatus requestProcess(int32_t in_instanceId) override {
return owner->handleRequestProcess(in_instanceId);
}
};

public:
AndroidPluginClientConnectionData(AIBinder* aiBinder) {
spAIBinder.set(aiBinder);
proxy = aidl::org::androidaudioplugin::BpAudioPluginInterface::fromBinder(spAIBinder);
callback = std::make_shared<AudioPluginCallback>(this);
proxy->setCallback(callback);
}

aidl::org::androidaudioplugin::IAudioPluginInterface *getProxy() { return proxy.get(); }

::ndk::ScopedAStatus handleRequestProcess(int32_t instanceId) {
return handle_request_process(instanceId);
}
};

} // namespace aap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ class IAudioPluginInterfaceCallback : public ::ndk::ICInterface {
IAudioPluginInterfaceCallback();
virtual ~IAudioPluginInterfaceCallback();

static constexpr uint32_t TRANSACTION_notify = FIRST_CALL_TRANSACTION + 0;
static constexpr uint32_t TRANSACTION_requestProcess = FIRST_CALL_TRANSACTION + 0;

static std::shared_ptr<IAudioPluginInterfaceCallback> fromBinder(const ::ndk::SpAIBinder& binder);
static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<IAudioPluginInterfaceCallback>& instance);
static binder_status_t readFromParcel(const AParcel* parcel, std::shared_ptr<IAudioPluginInterfaceCallback>* instance);
static bool setDefaultImpl(const std::shared_ptr<IAudioPluginInterfaceCallback>& impl);
static const std::shared_ptr<IAudioPluginInterfaceCallback>& getDefaultImpl();
virtual ::ndk::ScopedAStatus notify(int32_t in_instanceId, int32_t in_portId, int32_t in_size) = 0;
virtual ::ndk::ScopedAStatus requestProcess(int32_t in_instanceId) = 0;
private:
static std::shared_ptr<IAudioPluginInterfaceCallback> default_impl;
};
class IAudioPluginInterfaceCallbackDefault : public IAudioPluginInterfaceCallback {
public:
::ndk::ScopedAStatus notify(int32_t in_instanceId, int32_t in_portId, int32_t in_size) override;
::ndk::ScopedAStatus requestProcess(int32_t in_instanceId) override;
::ndk::SpAIBinder asBinder() override;
bool isRemote() override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class IAudioPluginInterfaceCallbackDelegator : public BnAudioPluginInterfaceCall
explicit IAudioPluginInterfaceCallbackDelegator(const std::shared_ptr<IAudioPluginInterfaceCallback> &impl) : _impl(impl) {
}

::ndk::ScopedAStatus notify(int32_t in_instanceId, int32_t in_portId, int32_t in_size) override {
return _impl->notify(in_instanceId, in_portId, in_size);
::ndk::ScopedAStatus requestProcess(int32_t in_instanceId) override {
return _impl->requestProcess(in_instanceId);
}
protected:
private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BpAudioPluginInterfaceCallback : public ::ndk::BpCInterface<IAudioPluginIn
explicit BpAudioPluginInterfaceCallback(const ::ndk::SpAIBinder& binder);
virtual ~BpAudioPluginInterfaceCallback();

::ndk::ScopedAStatus notify(int32_t in_instanceId, int32_t in_portId, int32_t in_size) override;
::ndk::ScopedAStatus requestProcess(int32_t in_instanceId) override;
};
} // namespace androidaudioplugin
} // namespace org
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@ static binder_status_t _aidl_org_androidaudioplugin_AudioPluginInterfaceCallback
binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;
std::shared_ptr<BnAudioPluginInterfaceCallback> _aidl_impl = std::static_pointer_cast<BnAudioPluginInterfaceCallback>(::ndk::ICInterface::asInterface(_aidl_binder));
switch (_aidl_code) {
case (FIRST_CALL_TRANSACTION + 0 /*notify*/): {
case (FIRST_CALL_TRANSACTION + 0 /*requestProcess*/): {
int32_t in_instanceId;
int32_t in_portId;
int32_t in_size;

_aidl_ret_status = ::ndk::AParcel_readData(_aidl_in, &in_instanceId);
if (_aidl_ret_status != STATUS_OK) break;

_aidl_ret_status = ::ndk::AParcel_readData(_aidl_in, &in_portId);
if (_aidl_ret_status != STATUS_OK) break;

_aidl_ret_status = ::ndk::AParcel_readData(_aidl_in, &in_size);
if (_aidl_ret_status != STATUS_OK) break;

::ndk::ScopedAStatus _aidl_status = _aidl_impl->notify(in_instanceId, in_portId, in_size);
::ndk::ScopedAStatus _aidl_status = _aidl_impl->requestProcess(in_instanceId);
_aidl_ret_status = STATUS_OK;
break;
}
Expand All @@ -40,7 +32,7 @@ static AIBinder_Class* _g_aidl_org_androidaudioplugin_AudioPluginInterfaceCallba
BpAudioPluginInterfaceCallback::BpAudioPluginInterfaceCallback(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}
BpAudioPluginInterfaceCallback::~BpAudioPluginInterfaceCallback() {}

::ndk::ScopedAStatus BpAudioPluginInterfaceCallback::notify(int32_t in_instanceId, int32_t in_portId, int32_t in_size) {
::ndk::ScopedAStatus BpAudioPluginInterfaceCallback::requestProcess(int32_t in_instanceId) {
binder_status_t _aidl_ret_status = STATUS_OK;
::ndk::ScopedAStatus _aidl_status;
::ndk::ScopedAParcel _aidl_in;
Expand All @@ -52,15 +44,9 @@ ::ndk::ScopedAStatus BpAudioPluginInterfaceCallback::notify(int32_t in_instanceI
_aidl_ret_status = ::ndk::AParcel_writeData(_aidl_in.get(), in_instanceId);
if (_aidl_ret_status != STATUS_OK) goto _aidl_error;

_aidl_ret_status = ::ndk::AParcel_writeData(_aidl_in.get(), in_portId);
if (_aidl_ret_status != STATUS_OK) goto _aidl_error;

_aidl_ret_status = ::ndk::AParcel_writeData(_aidl_in.get(), in_size);
if (_aidl_ret_status != STATUS_OK) goto _aidl_error;

_aidl_ret_status = AIBinder_transact(
asBinder().get(),
(FIRST_CALL_TRANSACTION + 0 /*notify*/),
(FIRST_CALL_TRANSACTION + 0 /*requestProcess*/),
_aidl_in.getR(),
_aidl_out.getR(),
FLAG_ONEWAY
Expand All @@ -69,7 +55,7 @@ ::ndk::ScopedAStatus BpAudioPluginInterfaceCallback::notify(int32_t in_instanceI
#endif // BINDER_STABILITY_SUPPORT
);
if (_aidl_ret_status == STATUS_UNKNOWN_TRANSACTION && IAudioPluginInterfaceCallback::getDefaultImpl()) {
_aidl_status = IAudioPluginInterfaceCallback::getDefaultImpl()->notify(in_instanceId, in_portId, in_size);
_aidl_status = IAudioPluginInterfaceCallback::getDefaultImpl()->requestProcess(in_instanceId);
goto _aidl_status_return;
}
if (_aidl_ret_status != STATUS_OK) goto _aidl_error;
Expand Down Expand Up @@ -129,7 +115,7 @@ const std::shared_ptr<IAudioPluginInterfaceCallback>& IAudioPluginInterfaceCallb
return IAudioPluginInterfaceCallback::default_impl;
}
std::shared_ptr<IAudioPluginInterfaceCallback> IAudioPluginInterfaceCallback::default_impl = nullptr;
::ndk::ScopedAStatus IAudioPluginInterfaceCallbackDefault::notify(int32_t /*in_instanceId*/, int32_t /*in_portId*/, int32_t /*in_size*/) {
::ndk::ScopedAStatus IAudioPluginInterfaceCallbackDefault::requestProcess(int32_t /*in_instanceId*/) {
::ndk::ScopedAStatus _aidl_status;
_aidl_status.set(AStatus_fromStatus(STATUS_UNKNOWN_TRANSACTION));
return _aidl_status;
Expand Down
16 changes: 12 additions & 4 deletions androidaudioplugin/src/main/cpp/core/hosting/audio-plugin-host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "../extensions/midi-service.h"
#include "../extensions/port-config-service.h"
#include "../extensions/gui-service.h"
#include "aap/core/host/plugin-instance.h"


#define AAP_HOST_TAG "AAP_HOST"

Expand Down Expand Up @@ -242,7 +244,7 @@ PluginInstance* PluginHost::instantiateLocalPlugin(const PluginInformation *desc
aap::a_log_f(AAP_LOG_LEVEL_ERROR, AAP_HOST_TAG, "aap::PluginHost: AAP factory entrypoint function %s could not instantiate a plugin.", entrypoint.c_str());
return nullptr;
}
auto instance = new LocalPluginInstance(aapxs_registry.get(), localInstanceIdSerial++, descriptor, pluginFactory, sampleRate);
auto instance = new LocalPluginInstance(this, aapxs_registry.get(), localInstanceIdSerial++, descriptor, pluginFactory, sampleRate);
instances.emplace_back(instance);
return instance;
}
Expand Down Expand Up @@ -463,7 +465,7 @@ void PluginInstance::scanParametersAndBuildList() {

AndroidAudioPluginHost* RemotePluginInstance::getHostFacadeForCompleteInstantiation() {
plugin_host_facade.context = this;
plugin_host_facade.get_extension_data = nullptr; // we shouldn't need it.
plugin_host_facade.get_extension = nullptr; // we shouldn't need it.
return &plugin_host_facade;
}

Expand Down Expand Up @@ -498,8 +500,9 @@ void RemotePluginInstance::prepare(int frameCount) {

//----

LocalPluginInstance::LocalPluginInstance(AAPXSRegistry *aapxsRegistry, int32_t instanceId, const PluginInformation* pluginInformation, AndroidAudioPluginFactory* loadedPluginFactory, int sampleRate)
LocalPluginInstance::LocalPluginInstance(PluginHost *host, AAPXSRegistry *aapxsRegistry, int32_t instanceId, const PluginInformation* pluginInformation, AndroidAudioPluginFactory* loadedPluginFactory, int sampleRate)
: PluginInstance(pluginInformation, loadedPluginFactory, sampleRate),
host(host),
aapxs_registry(aapxsRegistry),
aapxsServiceInstances([&]() { return getPlugin(); }),
standards(this) {
Expand All @@ -509,7 +512,8 @@ LocalPluginInstance::LocalPluginInstance(AAPXSRegistry *aapxsRegistry, int32_t i

AndroidAudioPluginHost* LocalPluginInstance::getHostFacadeForCompleteInstantiation() {
plugin_host_facade.context = this;
plugin_host_facade.get_extension_data = internalGetExtensionData;
plugin_host_facade.get_extension = internalGetExtension;
plugin_host_facade.request_process = internalRequestProcess;
return &plugin_host_facade;
}

Expand Down Expand Up @@ -578,6 +582,10 @@ void LocalPluginInstance::notify_parameters_changed(AndroidAudioPluginHost *host
assert(false); // FIXME: implement
}

void LocalPluginInstance::requestProcessToHost() {
((PluginService*) host)->requestProcessToHost(instance_id);
}

//----

AAPXSClientInstance* RemoteAAPXSManager::setupAAPXSInstance(AAPXSFeature *feature, int32_t dataSize) {
Expand Down
2 changes: 1 addition & 1 deletion androidaudioplugin/src/main/update-binder.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

ANDROID_HOME=~/Android/Sdk
ANDROID_HOME=~/Android/sdk
BUILD_TOOLS_VERSION=33.0.2
THIS_DIR=`dirname $(readlink -f $0)`
AIDL_DIR=$THIS_DIR
Expand Down
9 changes: 6 additions & 3 deletions include/aap/android-audio-plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ struct AndroidAudioPlugin;

struct AndroidAudioPluginHost;

typedef void* (*aap_host_get_extension_data_t) (struct AndroidAudioPluginHost* host, const char *uri);
typedef void* (*aap_host_get_extension_func_t) (struct AndroidAudioPluginHost* host, const char *uri);
typedef void (*aap_host_request_process_func_t) (struct AndroidAudioPluginHost* host);

/**
* Represents a host from plugin's perspective.
Expand All @@ -65,8 +66,10 @@ typedef void* (*aap_host_get_extension_data_t) (struct AndroidAudioPluginHost* h
*/
typedef struct AndroidAudioPluginHost {
void *context;
/** returns the shared data space that the host provides. */
aap_host_get_extension_data_t get_extension_data;
/** returns the host extension that the host provides. */
aap_host_get_extension_func_t get_extension;
/** notify host that it should send process() request at inactive state. */
aap_host_request_process_func_t request_process;
} AndroidAudioPluginHost;

/* function types */
Expand Down
10 changes: 8 additions & 2 deletions include/aap/core/host/plugin-host.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace aap {
{
protected:
PluginListSnapshot* plugin_list{nullptr};

std::vector<PluginInstance*> instances{};
PluginInstance* instantiateLocalPlugin(const PluginInformation *pluginInfo, int sampleRate);

Expand All @@ -59,9 +60,10 @@ namespace aap {


class PluginService : public PluginHost {
std::function<void(int32_t)> plugin_callback;
public:
PluginService(PluginListSnapshot* contextPluginList)
: PluginHost(contextPluginList)
PluginService(PluginListSnapshot* contextPluginList, std::function<void(int32_t)> pluginCallback)
: PluginHost(contextPluginList), plugin_callback(pluginCallback)
{
}

Expand All @@ -70,6 +72,10 @@ namespace aap {
inline LocalPluginInstance* getLocalInstance(int32_t instanceId) {
return (LocalPluginInstance*) getInstanceById(instanceId);
}

void requestProcessToHost(int32_t instanceId) {
plugin_callback(instanceId);
}
};

class PluginClient : public PluginHost {
Expand Down
13 changes: 11 additions & 2 deletions include/aap/core/host/plugin-instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace aap {

class PluginSharedMemoryStore;
class PluginHost;

/**
* The common basis for client RemotePluginInstance and service LocalPluginInstance.
Expand Down Expand Up @@ -174,6 +175,7 @@ namespace aap {
AndroidAudioPlugin *getPlugin() override { return owner->getPlugin(); }
};

PluginHost *host;
AAPXSRegistry *aapxs_registry;
AndroidAudioPluginHost plugin_host_facade{};
AAPXSInstanceMap <AAPXSServiceInstance> aapxsServiceInstances;
Expand All @@ -187,7 +189,7 @@ namespace aap {
static void notify_parameters_changed(AndroidAudioPluginHost *host, AndroidAudioPlugin *plugin);

inline static void *
internalGetExtensionData(AndroidAudioPluginHost *host, const char *uri) {
internalGetExtension(AndroidAudioPluginHost *host, const char *uri) {
if (strcmp(uri, AAP_PLUGIN_INFO_EXTENSION_URI) == 0) {
auto instance = (LocalPluginInstance *) host->context;
instance->host_plugin_info.get = get_plugin_info;
Expand All @@ -201,11 +203,16 @@ namespace aap {
return nullptr;
}

inline static void internalRequestProcess(AndroidAudioPluginHost *host) {
auto instance = (LocalPluginInstance *) host->context;
instance->requestProcessToHost();
}

protected:
AndroidAudioPluginHost *getHostFacadeForCompleteInstantiation() override;

public:
LocalPluginInstance(AAPXSRegistry *aapxsRegistry, int32_t instanceId,
LocalPluginInstance(PluginHost *host, AAPXSRegistry *aapxsRegistry, int32_t instanceId,
const PluginInformation *pluginInformation,
AndroidAudioPluginFactory *loadedPluginFactory, int sampleRate);

Expand Down Expand Up @@ -261,6 +268,8 @@ namespace aap {
plugin->prepare(plugin, getAudioPluginBuffer());
instantiation_state = PLUGIN_INSTANTIATION_STATE_INACTIVE;
}

void requestProcessToHost();
};

class RemotePluginInstance : public PluginInstance {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void sample_plugin_delete(

void sample_plugin_prepare(AndroidAudioPlugin *plugin, aap_buffer_t *buffer) {
auto ctx = (SamplePluginSpecific*) plugin->plugin_specific;
auto ext = (aap_host_plugin_info_extension_t*) ctx->host.get_extension_data(&ctx->host, AAP_PLUGIN_INFO_EXTENSION_URI);
auto ext = (aap_host_plugin_info_extension_t*) ctx->host.get_extension(&ctx->host, AAP_PLUGIN_INFO_EXTENSION_URI);
assert(ext);
auto pluginInfo = ext->get(&ctx->host, PLUGIN_URI);
auto numPorts = pluginInfo.get_port_count(&pluginInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ AndroidAudioPlugin *sample_plugin_new(
handle->midi_protocol = 2; // this is for testing MIDI2 in port.
#endif

auto pluginInfoExt = (aap_host_plugin_info_extension_t*) host->get_extension_data(host, AAP_PLUGIN_INFO_EXTENSION_URI);
auto pluginInfoExt = (aap_host_plugin_info_extension_t*) host->get_extension(host, AAP_PLUGIN_INFO_EXTENSION_URI);
if (pluginInfoExt != nullptr) {
auto info = pluginInfoExt->get(host, pluginUniqueId);
aap::a_log_f(AAP_LOG_LEVEL_INFO, AAP_APP_LOG_TAG, "plugin-info test: displayName: %s", info.display_name(&info));
Expand Down

0 comments on commit 685ed73

Please sign in to comment.