Mercurial > hg > piper-cpp
changeset 94:a660dca988f8
More renaming
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Thu, 13 Oct 2016 14:10:55 +0100 |
parents | fbce91785d35 |
children | b6ac26b72b59 |
files | vamp-client/CapnpClient.h vamp-client/Loader.h vamp-client/Makefile vamp-client/PiperCapnpClient.h vamp-client/PiperClient.h vamp-client/PiperPluginStub.h vamp-client/PiperQProcessTransport.h vamp-client/PluginClient.h vamp-client/PluginStub.h vamp-client/ProcessQtTransport.h vamp-client/SynchronousTransport.h vamp-client/client.cpp vamp-client/client.pro |
diffstat | 13 files changed, 727 insertions(+), 706 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vamp-client/CapnpClient.h Thu Oct 13 14:10:55 2016 +0100 @@ -0,0 +1,310 @@ + +#ifndef PIPER_CAPNP_CLIENT_H +#define PIPER_CAPNP_CLIENT_H + +#include "Loader.h" +#include "PluginClient.h" +#include "PluginStub.h" +#include "SynchronousTransport.h" + +#include "vamp-support/AssignedPluginHandleMapper.h" +#include "vamp-capnp/VampnProto.h" + +#include <capnp/serialize.h> + +namespace piper { +namespace vampclient { + +class CapnpClient : public PluginClient, + public Loader +{ + // unsigned to avoid undefined behaviour on possible wrap + typedef uint32_t ReqId; + + class CompletenessChecker : public MessageCompletenessChecker { + public: + bool isComplete(const std::vector<char> &message) const override { + auto karr = toKJArray(message); + size_t words = karr.size(); + size_t expected = capnp::expectedSizeInWordsFromPrefix(karr); + if (words > expected) { + std::cerr << "WARNING: obtained more data than expected (" + << words << " " << sizeof(capnp::word) + << "-byte words, expected " + << expected << ")" << std::endl; + } + return words >= expected; + } + }; + +public: + CapnpClient(SynchronousTransport *transport) : //!!! ownership? shared ptr? + m_transport(transport), + m_completenessChecker(new CompletenessChecker) { + transport->setCompletenessChecker(m_completenessChecker); + } + + ~CapnpClient() { + delete m_completenessChecker; + } + + //!!! obviously, factor out all repetitive guff + + //!!! list and load are supposed to be called by application code, + //!!! but the rest are only supposed to be called by the plugin -- + //!!! sort out the api here + + Vamp::Plugin * + load(std::string key, float inputSampleRate, int adapterFlags) { + + if (!m_transport->isOK()) { + throw std::runtime_error("Piper server failed to start"); + } + + Vamp::HostExt::PluginStaticData psd; + Vamp::HostExt::PluginConfiguration defaultConfig; + PluginHandleMapper::Handle handle = + serverLoad(key, inputSampleRate, adapterFlags, psd, defaultConfig); + + Vamp::Plugin *plugin = new PluginStub(this, + key, + inputSampleRate, + adapterFlags, + psd, + defaultConfig); + + m_mapper.addPlugin(handle, plugin); + + return plugin; + } + + PluginHandleMapper::Handle + serverLoad(std::string key, float inputSampleRate, int adapterFlags, + Vamp::HostExt::PluginStaticData &psd, + Vamp::HostExt::PluginConfiguration &defaultConfig) { + + Vamp::HostExt::LoadRequest request; + request.pluginKey = key; + request.inputSampleRate = inputSampleRate; + request.adapterFlags = adapterFlags; + + capnp::MallocMessageBuilder message; + RpcRequest::Builder builder = message.initRoot<RpcRequest>(); + + VampnProto::buildRpcRequest_Load(builder, request); + ReqId id = getId(); + builder.getId().setNumber(id); + + auto arr = capnp::messageToFlatArray(message); + + auto responseBuffer = m_transport->call(arr.asChars().begin(), + arr.asChars().size()); + + //!!! ... --> will also need some way to kill this process + //!!! (from another thread) + + auto karr = toKJArray(responseBuffer); + capnp::FlatArrayMessageReader responseMessage(karr); + RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); + + //!!! handle (explicit) error case + + checkResponseType(reader, RpcResponse::Response::Which::LOAD, id); + + const LoadResponse::Reader &lr = reader.getResponse().getLoad(); + VampnProto::readExtractorStaticData(psd, lr.getStaticData()); + VampnProto::readConfiguration(defaultConfig, lr.getDefaultConfiguration()); + return lr.getHandle(); + }; + +protected: + virtual + Vamp::Plugin::OutputList + configure(PluginStub *plugin, + Vamp::HostExt::PluginConfiguration config) override { + + if (!m_transport->isOK()) { + throw std::runtime_error("Piper server failed to start"); + } + + Vamp::HostExt::ConfigurationRequest request; + request.plugin = plugin; + request.configuration = config; + + capnp::MallocMessageBuilder message; + RpcRequest::Builder builder = message.initRoot<RpcRequest>(); + + VampnProto::buildRpcRequest_Configure(builder, request, m_mapper); + ReqId id = getId(); + builder.getId().setNumber(id); + + auto arr = capnp::messageToFlatArray(message); + auto responseBuffer = m_transport->call(arr.asChars().begin(), + arr.asChars().size()); + auto karr = toKJArray(responseBuffer); + capnp::FlatArrayMessageReader responseMessage(karr); + RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); + + //!!! handle (explicit) error case + + checkResponseType(reader, RpcResponse::Response::Which::CONFIGURE, id); + + Vamp::HostExt::ConfigurationResponse cr; + VampnProto::readConfigurationResponse(cr, + reader.getResponse().getConfigure(), + m_mapper); + + return cr.outputs; + }; + + virtual + Vamp::Plugin::FeatureSet + process(PluginStub *plugin, + std::vector<std::vector<float> > inputBuffers, + Vamp::RealTime timestamp) override { + + if (!m_transport->isOK()) { + throw std::runtime_error("Piper server failed to start"); + } + + Vamp::HostExt::ProcessRequest request; + request.plugin = plugin; + request.inputBuffers = inputBuffers; + request.timestamp = timestamp; + + capnp::MallocMessageBuilder message; + RpcRequest::Builder builder = message.initRoot<RpcRequest>(); + + VampnProto::buildRpcRequest_Process(builder, request, m_mapper); + ReqId id = getId(); + builder.getId().setNumber(id); + + auto arr = capnp::messageToFlatArray(message); + auto responseBuffer = m_transport->call(arr.asChars().begin(), + arr.asChars().size()); + auto karr = toKJArray(responseBuffer); + capnp::FlatArrayMessageReader responseMessage(karr); + RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); + + //!!! handle (explicit) error case + + checkResponseType(reader, RpcResponse::Response::Which::PROCESS, id); + + Vamp::HostExt::ProcessResponse pr; + VampnProto::readProcessResponse(pr, + reader.getResponse().getProcess(), + m_mapper); + + return pr.features; + } + + virtual Vamp::Plugin::FeatureSet + finish(PluginStub *plugin) override { + + if (!m_transport->isOK()) { + throw std::runtime_error("Piper server failed to start"); + } + + Vamp::HostExt::FinishRequest request; + request.plugin = plugin; + + capnp::MallocMessageBuilder message; + RpcRequest::Builder builder = message.initRoot<RpcRequest>(); + + VampnProto::buildRpcRequest_Finish(builder, request, m_mapper); + ReqId id = getId(); + builder.getId().setNumber(id); + + auto arr = capnp::messageToFlatArray(message); + auto responseBuffer = m_transport->call(arr.asChars().begin(), + arr.asChars().size()); + auto karr = toKJArray(responseBuffer); + capnp::FlatArrayMessageReader responseMessage(karr); + RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); + + //!!! handle (explicit) error case + + checkResponseType(reader, RpcResponse::Response::Which::FINISH, id); + + Vamp::HostExt::ProcessResponse pr; + VampnProto::readFinishResponse(pr, + reader.getResponse().getFinish(), + m_mapper); + + m_mapper.removePlugin(m_mapper.pluginToHandle(plugin)); + + // Don't delete the plugin. It's the plugin that is supposed + // to be calling us here + + return pr.features; + } + + virtual void + reset(PluginStub *plugin, + Vamp::HostExt::PluginConfiguration config) override { + + // Reload the plugin on the server side, and configure it as requested + + if (!m_transport->isOK()) { + throw std::runtime_error("Piper server failed to start"); + } + + if (m_mapper.havePlugin(plugin)) { + (void)finish(plugin); // server-side unload + } + + Vamp::HostExt::PluginStaticData psd; + Vamp::HostExt::PluginConfiguration defaultConfig; + PluginHandleMapper::Handle handle = + serverLoad(plugin->getPluginKey(), + plugin->getInputSampleRate(), + plugin->getAdapterFlags(), + psd, defaultConfig); + + m_mapper.addPlugin(handle, plugin); + + (void)configure(plugin, config); + } + +private: + AssignedPluginHandleMapper m_mapper; + ReqId getId() { + //!!! todo: mutex + static ReqId m_nextId = 0; + return m_nextId++; + } + + static + kj::Array<capnp::word> + toKJArray(const std::vector<char> &buffer) { + // We could do this whole thing with fewer copies, but let's + // see whether it matters first + size_t wordSize = sizeof(capnp::word); + size_t words = buffer.size() / wordSize; + kj::Array<capnp::word> karr(kj::heapArray<capnp::word>(words)); + memcpy(karr.begin(), buffer.data(), words * wordSize); + return karr; + } + + void + checkResponseType(const RpcResponse::Reader &r, + RpcResponse::Response::Which type, + ReqId id) { + + if (r.getResponse().which() != type) { + throw std::runtime_error("Wrong response type"); + } + if (ReqId(r.getId().getNumber()) != id) { + throw std::runtime_error("Wrong response id"); + } + } + +private: + SynchronousTransport *m_transport; //!!! I don't own this, but should I? + CompletenessChecker *m_completenessChecker; // I own this +}; + +} +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vamp-client/Loader.h Thu Oct 13 14:10:55 2016 +0100 @@ -0,0 +1,21 @@ + +#ifndef PIPER_LOADER_H +#define PIPER_LOADER_H + +#include <vamp-hostsdk/Plugin.h> + +namespace piper { +namespace vampclient { + +class Loader +{ +public: + virtual + Vamp::Plugin * + load(std::string key, float inputSampleRate, int adapterFlags) = 0; +}; + +} +} + +#endif
--- a/vamp-client/Makefile Thu Oct 13 12:07:41 2016 +0100 +++ b/vamp-client/Makefile Thu Oct 13 14:10:55 2016 +0100 @@ -205,10 +205,11 @@ /usr/lib/qt/mkspecs/features/testcase_targets.prf \ /usr/lib/qt/mkspecs/features/yacc.prf \ /usr/lib/qt/mkspecs/features/lex.prf \ - client.pro PiperQProcessTransport.h \ - PiperCapnpClient.h \ - PiperClient.h \ - PiperPluginStub.h \ + client.pro ProcessQtTransport.h \ + CapnpClient.h \ + Loader.h \ + PluginClient.h \ + PluginStub.h \ SynchronousTransport.h client.cpp \ ../vamp-capnp/piper.capnp.c++ QMAKE_TARGET = client @@ -547,7 +548,7 @@ distdir: FORCE @test -d $(DISTDIR) || mkdir -p $(DISTDIR) $(COPY_FILE) --parents $(DIST) $(DISTDIR)/ - $(COPY_FILE) --parents PiperQProcessTransport.h PiperCapnpClient.h PiperClient.h PiperPluginStub.h SynchronousTransport.h $(DISTDIR)/ + $(COPY_FILE) --parents ProcessQtTransport.h CapnpClient.h Loader.h PluginClient.h PluginStub.h SynchronousTransport.h $(DISTDIR)/ $(COPY_FILE) --parents client.cpp ../vamp-capnp/piper.capnp.c++ $(DISTDIR)/ @@ -588,11 +589,7 @@ ####### Compile -../o/client.o: client.cpp PiperQProcessTransport.h \ - SynchronousTransport.h \ - PiperCapnpClient.h \ - PiperClient.h \ - PiperPluginStub.h +../o/client.o: client.cpp $(CXX) -c $(CXXFLAGS) $(INCPATH) -o ../o/client.o client.cpp ../o/piper.capnp.o: ../vamp-capnp/piper.capnp.c++ ../vamp-capnp/piper.capnp.h
--- a/vamp-client/PiperCapnpClient.h Thu Oct 13 12:07:41 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,307 +0,0 @@ - -#ifndef PIPER_CAPNP_CLIENT_H -#define PIPER_CAPNP_CLIENT_H - -#include "PiperClient.h" -#include "PiperPluginStub.h" -#include "SynchronousTransport.h" - -#include "vamp-support/AssignedPluginHandleMapper.h" -#include "vamp-capnp/VampnProto.h" - -#include <capnp/serialize.h> - -namespace piper { //!!! change - -class PiperCapnpClient : public PiperPluginClientInterface, - public PiperLoaderInterface -{ - // unsigned to avoid undefined behaviour on possible wrap - typedef uint32_t ReqId; - - class CompletenessChecker : public MessageCompletenessChecker { - public: - bool isComplete(const std::vector<char> &message) const override { - auto karr = toKJArray(message); - size_t words = karr.size(); - size_t expected = capnp::expectedSizeInWordsFromPrefix(karr); - if (words > expected) { - std::cerr << "WARNING: obtained more data than expected (" - << words << " " << sizeof(capnp::word) - << "-byte words, expected " - << expected << ")" << std::endl; - } - return words >= expected; - } - }; - -public: - PiperCapnpClient(SynchronousTransport *transport) : //!!! ownership? shared ptr? - m_transport(transport), - m_completenessChecker(new CompletenessChecker) { - transport->setCompletenessChecker(m_completenessChecker); - } - - ~PiperCapnpClient() { - delete m_completenessChecker; - } - - //!!! obviously, factor out all repetitive guff - - //!!! list and load are supposed to be called by application code, - //!!! but the rest are only supposed to be called by the plugin -- - //!!! sort out the api here - - Vamp::Plugin * - load(std::string key, float inputSampleRate, int adapterFlags) { - - if (!m_transport->isOK()) { - throw std::runtime_error("Piper server failed to start"); - } - - Vamp::HostExt::PluginStaticData psd; - Vamp::HostExt::PluginConfiguration defaultConfig; - PluginHandleMapper::Handle handle = - serverLoad(key, inputSampleRate, adapterFlags, psd, defaultConfig); - - Vamp::Plugin *plugin = new PiperPluginStub(this, - key, - inputSampleRate, - adapterFlags, - psd, - defaultConfig); - - m_mapper.addPlugin(handle, plugin); - - return plugin; - } - - PluginHandleMapper::Handle - serverLoad(std::string key, float inputSampleRate, int adapterFlags, - Vamp::HostExt::PluginStaticData &psd, - Vamp::HostExt::PluginConfiguration &defaultConfig) { - - Vamp::HostExt::LoadRequest request; - request.pluginKey = key; - request.inputSampleRate = inputSampleRate; - request.adapterFlags = adapterFlags; - - capnp::MallocMessageBuilder message; - RpcRequest::Builder builder = message.initRoot<RpcRequest>(); - - VampnProto::buildRpcRequest_Load(builder, request); - ReqId id = getId(); - builder.getId().setNumber(id); - - auto arr = capnp::messageToFlatArray(message); - - auto responseBuffer = m_transport->call(arr.asChars().begin(), - arr.asChars().size()); - - //!!! ... --> will also need some way to kill this process - //!!! (from another thread) - - auto karr = toKJArray(responseBuffer); - capnp::FlatArrayMessageReader responseMessage(karr); - RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); - - //!!! handle (explicit) error case - - checkResponseType(reader, RpcResponse::Response::Which::LOAD, id); - - const LoadResponse::Reader &lr = reader.getResponse().getLoad(); - VampnProto::readExtractorStaticData(psd, lr.getStaticData()); - VampnProto::readConfiguration(defaultConfig, lr.getDefaultConfiguration()); - return lr.getHandle(); - }; - -protected: - virtual - Vamp::Plugin::OutputList - configure(PiperPluginStub *plugin, - Vamp::HostExt::PluginConfiguration config) override { - - if (!m_transport->isOK()) { - throw std::runtime_error("Piper server failed to start"); - } - - Vamp::HostExt::ConfigurationRequest request; - request.plugin = plugin; - request.configuration = config; - - capnp::MallocMessageBuilder message; - RpcRequest::Builder builder = message.initRoot<RpcRequest>(); - - VampnProto::buildRpcRequest_Configure(builder, request, m_mapper); - ReqId id = getId(); - builder.getId().setNumber(id); - - auto arr = capnp::messageToFlatArray(message); - auto responseBuffer = m_transport->call(arr.asChars().begin(), - arr.asChars().size()); - auto karr = toKJArray(responseBuffer); - capnp::FlatArrayMessageReader responseMessage(karr); - RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); - - //!!! handle (explicit) error case - - checkResponseType(reader, RpcResponse::Response::Which::CONFIGURE, id); - - Vamp::HostExt::ConfigurationResponse cr; - VampnProto::readConfigurationResponse(cr, - reader.getResponse().getConfigure(), - m_mapper); - - return cr.outputs; - }; - - virtual - Vamp::Plugin::FeatureSet - process(PiperPluginStub *plugin, - std::vector<std::vector<float> > inputBuffers, - Vamp::RealTime timestamp) override { - - if (!m_transport->isOK()) { - throw std::runtime_error("Piper server failed to start"); - } - - Vamp::HostExt::ProcessRequest request; - request.plugin = plugin; - request.inputBuffers = inputBuffers; - request.timestamp = timestamp; - - capnp::MallocMessageBuilder message; - RpcRequest::Builder builder = message.initRoot<RpcRequest>(); - - VampnProto::buildRpcRequest_Process(builder, request, m_mapper); - ReqId id = getId(); - builder.getId().setNumber(id); - - auto arr = capnp::messageToFlatArray(message); - auto responseBuffer = m_transport->call(arr.asChars().begin(), - arr.asChars().size()); - auto karr = toKJArray(responseBuffer); - capnp::FlatArrayMessageReader responseMessage(karr); - RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); - - //!!! handle (explicit) error case - - checkResponseType(reader, RpcResponse::Response::Which::PROCESS, id); - - Vamp::HostExt::ProcessResponse pr; - VampnProto::readProcessResponse(pr, - reader.getResponse().getProcess(), - m_mapper); - - return pr.features; - } - - virtual Vamp::Plugin::FeatureSet - finish(PiperPluginStub *plugin) override { - - if (!m_transport->isOK()) { - throw std::runtime_error("Piper server failed to start"); - } - - Vamp::HostExt::FinishRequest request; - request.plugin = plugin; - - capnp::MallocMessageBuilder message; - RpcRequest::Builder builder = message.initRoot<RpcRequest>(); - - VampnProto::buildRpcRequest_Finish(builder, request, m_mapper); - ReqId id = getId(); - builder.getId().setNumber(id); - - auto arr = capnp::messageToFlatArray(message); - auto responseBuffer = m_transport->call(arr.asChars().begin(), - arr.asChars().size()); - auto karr = toKJArray(responseBuffer); - capnp::FlatArrayMessageReader responseMessage(karr); - RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); - - //!!! handle (explicit) error case - - checkResponseType(reader, RpcResponse::Response::Which::FINISH, id); - - Vamp::HostExt::ProcessResponse pr; - VampnProto::readFinishResponse(pr, - reader.getResponse().getFinish(), - m_mapper); - - m_mapper.removePlugin(m_mapper.pluginToHandle(plugin)); - - // Don't delete the plugin. It's the plugin that is supposed - // to be calling us here - - return pr.features; - } - - virtual void - reset(PiperPluginStub *plugin, - Vamp::HostExt::PluginConfiguration config) override { - - // Reload the plugin on the server side, and configure it as requested - - if (!m_transport->isOK()) { - throw std::runtime_error("Piper server failed to start"); - } - - if (m_mapper.havePlugin(plugin)) { - (void)finish(plugin); // server-side unload - } - - Vamp::HostExt::PluginStaticData psd; - Vamp::HostExt::PluginConfiguration defaultConfig; - PluginHandleMapper::Handle handle = - serverLoad(plugin->getPluginKey(), - plugin->getInputSampleRate(), - plugin->getAdapterFlags(), - psd, defaultConfig); - - m_mapper.addPlugin(handle, plugin); - - (void)configure(plugin, config); - } - -private: - AssignedPluginHandleMapper m_mapper; - ReqId getId() { - //!!! todo: mutex - static ReqId m_nextId = 0; - return m_nextId++; - } - - static - kj::Array<capnp::word> - toKJArray(const std::vector<char> &buffer) { - // We could do this whole thing with fewer copies, but let's - // see whether it matters first - size_t wordSize = sizeof(capnp::word); - size_t words = buffer.size() / wordSize; - kj::Array<capnp::word> karr(kj::heapArray<capnp::word>(words)); - memcpy(karr.begin(), buffer.data(), words * wordSize); - return karr; - } - - void - checkResponseType(const RpcResponse::Reader &r, - RpcResponse::Response::Which type, - ReqId id) { - - if (r.getResponse().which() != type) { - throw std::runtime_error("Wrong response type"); - } - if (ReqId(r.getId().getNumber()) != id) { - throw std::runtime_error("Wrong response id"); - } - } - -private: - SynchronousTransport *m_transport; //!!! I don't own this, but should I? - CompletenessChecker *m_completenessChecker; // I own this -}; - -} - -#endif
--- a/vamp-client/PiperClient.h Thu Oct 13 12:07:41 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ - -#ifndef PIPER_CLIENT_H -#define PIPER_CLIENT_H - -#include <vamp-hostsdk/PluginConfiguration.h> - -namespace piper { //!!! change - -class PiperPluginStub; - -class PiperLoaderInterface -{ -public: - virtual - Vamp::Plugin * - load(std::string key, float inputSampleRate, int adapterFlags) = 0; -}; - -class PiperPluginClientInterface -{ - friend class PiperPluginStub; - -protected: - virtual - Vamp::Plugin::OutputList - configure(PiperPluginStub *plugin, - Vamp::HostExt::PluginConfiguration config) = 0; - - virtual - Vamp::Plugin::FeatureSet - process(PiperPluginStub *plugin, - std::vector<std::vector<float> > inputBuffers, - Vamp::RealTime timestamp) = 0; - - virtual Vamp::Plugin::FeatureSet - finish(PiperPluginStub *plugin) = 0; - - virtual - void - reset(PiperPluginStub *plugin, - Vamp::HostExt::PluginConfiguration config) = 0; -}; - -} - -#endif
--- a/vamp-client/PiperPluginStub.h Thu Oct 13 12:07:41 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,240 +0,0 @@ - -#ifndef PIPER_STUB_PLUGIN_H -#define PIPER_STUB_PLUGIN_H - -#include <vamp-hostsdk/Plugin.h> -#include <vamp-hostsdk/PluginLoader.h> -#include <vamp-hostsdk/PluginStaticData.h> -#include <vamp-hostsdk/PluginConfiguration.h> - -#include <cstdint> - -#include "PiperClient.h" - -namespace piper { //!!! should be something else - -class PiperPluginStub : public Vamp::Plugin -{ - enum State { - Loaded, Configured, Finished - }; - -public: - PiperPluginStub(PiperPluginClientInterface *client, - std::string pluginKey, - float inputSampleRate, - int adapterFlags, - Vamp::HostExt::PluginStaticData psd, - Vamp::HostExt::PluginConfiguration defaultConfig) : - Plugin(inputSampleRate), - m_client(client), - m_key(pluginKey), - m_adapterFlags(adapterFlags), - m_state(Loaded), - m_psd(psd), - m_defaultConfig(defaultConfig), - m_config(defaultConfig) - { } - - virtual ~PiperPluginStub() { - if (m_state != Finished) { - (void)m_client->finish(this); - } - } - - virtual std::string getIdentifier() const { - return m_psd.basic.identifier; - } - - virtual std::string getName() const { - return m_psd.basic.name; - } - - virtual std::string getDescription() const { - return m_psd.basic.description; - } - - virtual std::string getMaker() const { - return m_psd.maker; - } - - virtual std::string getCopyright() const { - return m_psd.copyright; - } - - virtual int getPluginVersion() const { - return m_psd.pluginVersion; - } - - virtual ParameterList getParameterDescriptors() const { - return m_psd.parameters; - } - - virtual float getParameter(std::string name) const { - if (m_config.parameterValues.find(name) != m_config.parameterValues.end()) { - return m_config.parameterValues.at(name); - } else { - return 0.f; - } - } - - virtual void setParameter(std::string name, float value) { - if (m_state != Loaded) { - throw std::logic_error("Can't set parameter after plugin initialised"); - } - m_config.parameterValues[name] = value; - } - - virtual ProgramList getPrograms() const { - return m_psd.programs; - } - - virtual std::string getCurrentProgram() const { - return m_config.currentProgram; - } - - virtual void selectProgram(std::string program) { - if (m_state != Loaded) { - throw std::logic_error("Can't select program after plugin initialised"); - } - m_config.currentProgram = program; - } - - virtual bool initialise(size_t inputChannels, - size_t stepSize, - size_t blockSize) { - - if (m_state != Loaded) { - throw std::logic_error("Plugin has already been initialised"); - } - - m_config.channelCount = inputChannels; - m_config.stepSize = stepSize; - m_config.blockSize = blockSize; - - m_outputs = m_client->configure(this, m_config); - - if (!m_outputs.empty()) { - m_state = Configured; - return true; - } else { - return false; - } - } - - virtual void reset() { - - if (m_state == Loaded) { - // reset is a no-op if the plugin hasn't been initialised yet - return; - } - - m_client->reset(this, m_config); - - m_state = Configured; - } - - virtual InputDomain getInputDomain() const { - return m_psd.inputDomain; - } - - virtual size_t getPreferredBlockSize() const { - return m_defaultConfig.blockSize; - } - - virtual size_t getPreferredStepSize() const { - return m_defaultConfig.stepSize; - } - - virtual size_t getMinChannelCount() const { - return m_psd.minChannelCount; - } - - virtual size_t getMaxChannelCount() const { - return m_psd.maxChannelCount; - } - - virtual OutputList getOutputDescriptors() const { - if (m_state == Configured) { - return m_outputs; - } - - //!!! todo: figure out for which hosts (and adapters?) it may - //!!! be a problem that the output descriptors are incomplete - //!!! here. Any such hosts/adapters are broken, but I bet they - //!!! exist - - OutputList staticOutputs; - for (const auto &o: m_psd.basicOutputInfo) { - OutputDescriptor od; - od.identifier = o.identifier; - od.name = o.name; - od.description = o.description; - staticOutputs.push_back(od); - } - return staticOutputs; - } - - virtual FeatureSet process(const float *const *inputBuffers, - Vamp::RealTime timestamp) { - - if (m_state == Loaded) { - throw std::logic_error("Plugin has not been initialised"); - } - if (m_state == Finished) { - throw std::logic_error("Plugin has already been disposed of"); - } - - //!!! ew - std::vector<std::vector<float> > vecbuf; - for (int c = 0; c < m_config.channelCount; ++c) { - vecbuf.push_back(std::vector<float> - (inputBuffers[c], - inputBuffers[c] + m_config.blockSize)); - } - - return m_client->process(this, vecbuf, timestamp); - } - - virtual FeatureSet getRemainingFeatures() { - - if (m_state == Loaded) { - throw std::logic_error("Plugin has not been configured"); - } - if (m_state == Finished) { - throw std::logic_error("Plugin has already been disposed of"); - } - - m_state = Finished; - - return m_client->finish(this); - } - - // Not Plugin methods, but needed by the PiperClient to support reloads: - - virtual float getInputSampleRate() const { - return m_inputSampleRate; - } - - virtual std::string getPluginKey() const { - return m_key; - } - - virtual int getAdapterFlags() const { - return m_adapterFlags; - } - -private: - PiperPluginClientInterface *m_client; - std::string m_key; - int m_adapterFlags; - State m_state; - Vamp::HostExt::PluginStaticData m_psd; - OutputList m_outputs; - Vamp::HostExt::PluginConfiguration m_defaultConfig; - Vamp::HostExt::PluginConfiguration m_config; -}; - -} - -#endif
--- a/vamp-client/PiperQProcessTransport.h Thu Oct 13 12:07:41 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,94 +0,0 @@ - -#ifndef PIPER_QPROCESS_TRANSPORT_H -#define PIPER_QPROCESS_TRANSPORT_H - -#include "SynchronousTransport.h" - -#include <QProcess> -#include <QString> - -#include <iostream> - -namespace piper { //!!! change - -class PiperQProcessTransport : public SynchronousTransport -{ -public: - PiperQProcessTransport(QString processName) : - m_completenessChecker(0) { - m_process = new QProcess(); - m_process->setReadChannel(QProcess::StandardOutput); - m_process->setProcessChannelMode(QProcess::ForwardedErrorChannel); - m_process->start(processName); - if (!m_process->waitForStarted()) { - std::cerr << "server failed to start" << std::endl; - delete m_process; - m_process = nullptr; - } - } - - ~PiperQProcessTransport() { - if (m_process) { - if (m_process->state() != QProcess::NotRunning) { - m_process->closeWriteChannel(); - m_process->waitForFinished(200); - m_process->close(); - m_process->waitForFinished(); - std::cerr << "server exited" << std::endl; - } - delete m_process; - } - } - - void - setCompletenessChecker(MessageCompletenessChecker *checker) { - //!!! ownership? - m_completenessChecker = checker; - } - - bool - isOK() const override { - return m_process != nullptr; - } - - std::vector<char> - call(const char *ptr, size_t size) override { - - if (!m_completenessChecker) { - throw std::logic_error("No completeness checker set on transport"); - } - - m_process->write(ptr, size); - - std::vector<char> buffer; - bool complete = false; - - while (!complete) { - - m_process->waitForReadyRead(1000); - qint64 byteCount = m_process->bytesAvailable(); - - if (!byteCount) { - if (m_process->state() == QProcess::NotRunning) { - std::cerr << "ERROR: Subprocess exited: Load failed" << std::endl; - throw std::runtime_error("Piper server exited unexpectedly"); - } - } else { - size_t formerSize = buffer.size(); - buffer.resize(formerSize + byteCount); - m_process->read(buffer.data() + formerSize, byteCount); - complete = m_completenessChecker->isComplete(buffer); - } - } - - return buffer; - } - -private: - MessageCompletenessChecker *m_completenessChecker; //!!! I don't own this (currently) - QProcess *m_process; // I own this -}; - -} - -#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vamp-client/PluginClient.h Thu Oct 13 14:10:55 2016 +0100 @@ -0,0 +1,39 @@ + +#ifndef PIPER_PLUGIN_CLIENT_H +#define PIPER_PLUGIN_CLIENT_H + +#include <vamp-hostsdk/PluginConfiguration.h> + +namespace piper { +namespace vampclient { + +class PluginStub; + +class PluginClient +{ +public: + virtual + Vamp::Plugin::OutputList + configure(PluginStub *plugin, + Vamp::HostExt::PluginConfiguration config) = 0; + + virtual + Vamp::Plugin::FeatureSet + process(PluginStub *plugin, + std::vector<std::vector<float> > inputBuffers, + Vamp::RealTime timestamp) = 0; + + virtual + Vamp::Plugin::FeatureSet + finish(PluginStub *plugin) = 0; + + virtual + void + reset(PluginStub *plugin, + Vamp::HostExt::PluginConfiguration config) = 0; +}; + +} +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vamp-client/PluginStub.h Thu Oct 13 14:10:55 2016 +0100 @@ -0,0 +1,242 @@ + +#ifndef PIPER_PLUGIN_STUB_H +#define PIPER_PLUGIN_STUB_H + +#include <vamp-hostsdk/Plugin.h> +#include <vamp-hostsdk/PluginLoader.h> +#include <vamp-hostsdk/PluginStaticData.h> +#include <vamp-hostsdk/PluginConfiguration.h> + +#include <cstdint> + +#include "PluginClient.h" + +namespace piper { +namespace vampclient { + +class PluginStub : public Vamp::Plugin +{ + enum State { + Loaded, Configured, Finished + }; + +public: + PluginStub(PluginClient *client, + std::string pluginKey, + float inputSampleRate, + int adapterFlags, + Vamp::HostExt::PluginStaticData psd, + Vamp::HostExt::PluginConfiguration defaultConfig) : + Plugin(inputSampleRate), + m_client(client), + m_key(pluginKey), + m_adapterFlags(adapterFlags), + m_state(Loaded), + m_psd(psd), + m_defaultConfig(defaultConfig), + m_config(defaultConfig) + { } + + virtual ~PluginStub() { + if (m_state != Finished) { + (void)m_client->finish(this); + } + } + + virtual std::string getIdentifier() const { + return m_psd.basic.identifier; + } + + virtual std::string getName() const { + return m_psd.basic.name; + } + + virtual std::string getDescription() const { + return m_psd.basic.description; + } + + virtual std::string getMaker() const { + return m_psd.maker; + } + + virtual std::string getCopyright() const { + return m_psd.copyright; + } + + virtual int getPluginVersion() const { + return m_psd.pluginVersion; + } + + virtual ParameterList getParameterDescriptors() const { + return m_psd.parameters; + } + + virtual float getParameter(std::string name) const { + if (m_config.parameterValues.find(name) != m_config.parameterValues.end()) { + return m_config.parameterValues.at(name); + } else { + return 0.f; + } + } + + virtual void setParameter(std::string name, float value) { + if (m_state != Loaded) { + throw std::logic_error("Can't set parameter after plugin initialised"); + } + m_config.parameterValues[name] = value; + } + + virtual ProgramList getPrograms() const { + return m_psd.programs; + } + + virtual std::string getCurrentProgram() const { + return m_config.currentProgram; + } + + virtual void selectProgram(std::string program) { + if (m_state != Loaded) { + throw std::logic_error("Can't select program after plugin initialised"); + } + m_config.currentProgram = program; + } + + virtual bool initialise(size_t inputChannels, + size_t stepSize, + size_t blockSize) { + + if (m_state != Loaded) { + throw std::logic_error("Plugin has already been initialised"); + } + + m_config.channelCount = inputChannels; + m_config.stepSize = stepSize; + m_config.blockSize = blockSize; + + m_outputs = m_client->configure(this, m_config); + + if (!m_outputs.empty()) { + m_state = Configured; + return true; + } else { + return false; + } + } + + virtual void reset() { + + if (m_state == Loaded) { + // reset is a no-op if the plugin hasn't been initialised yet + return; + } + + m_client->reset(this, m_config); + + m_state = Configured; + } + + virtual InputDomain getInputDomain() const { + return m_psd.inputDomain; + } + + virtual size_t getPreferredBlockSize() const { + return m_defaultConfig.blockSize; + } + + virtual size_t getPreferredStepSize() const { + return m_defaultConfig.stepSize; + } + + virtual size_t getMinChannelCount() const { + return m_psd.minChannelCount; + } + + virtual size_t getMaxChannelCount() const { + return m_psd.maxChannelCount; + } + + virtual OutputList getOutputDescriptors() const { + if (m_state == Configured) { + return m_outputs; + } + + //!!! todo: figure out for which hosts (and adapters?) it may + //!!! be a problem that the output descriptors are incomplete + //!!! here. Any such hosts/adapters are broken, but I bet they + //!!! exist + + OutputList staticOutputs; + for (const auto &o: m_psd.basicOutputInfo) { + OutputDescriptor od; + od.identifier = o.identifier; + od.name = o.name; + od.description = o.description; + staticOutputs.push_back(od); + } + return staticOutputs; + } + + virtual FeatureSet process(const float *const *inputBuffers, + Vamp::RealTime timestamp) { + + if (m_state == Loaded) { + throw std::logic_error("Plugin has not been initialised"); + } + if (m_state == Finished) { + throw std::logic_error("Plugin has already been disposed of"); + } + + //!!! ew + std::vector<std::vector<float> > vecbuf; + for (int c = 0; c < m_config.channelCount; ++c) { + vecbuf.push_back(std::vector<float> + (inputBuffers[c], + inputBuffers[c] + m_config.blockSize)); + } + + return m_client->process(this, vecbuf, timestamp); + } + + virtual FeatureSet getRemainingFeatures() { + + if (m_state == Loaded) { + throw std::logic_error("Plugin has not been configured"); + } + if (m_state == Finished) { + throw std::logic_error("Plugin has already been disposed of"); + } + + m_state = Finished; + + return m_client->finish(this); + } + + // Not Plugin methods, but needed by the PluginClient to support reloads: + + virtual float getInputSampleRate() const { + return m_inputSampleRate; + } + + virtual std::string getPluginKey() const { + return m_key; + } + + virtual int getAdapterFlags() const { + return m_adapterFlags; + } + +private: + PluginClient *m_client; + std::string m_key; + int m_adapterFlags; + State m_state; + Vamp::HostExt::PluginStaticData m_psd; + OutputList m_outputs; + Vamp::HostExt::PluginConfiguration m_defaultConfig; + Vamp::HostExt::PluginConfiguration m_config; +}; + +} +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vamp-client/ProcessQtTransport.h Thu Oct 13 14:10:55 2016 +0100 @@ -0,0 +1,96 @@ + +#ifndef PIPER_PROCESS_QT_TRANSPORT_H +#define PIPER_PROCESS_QT_TRANSPORT_H + +#include "SynchronousTransport.h" + +#include <QProcess> +#include <QString> + +#include <iostream> + +namespace piper { +namespace vampclient { + +class ProcessQtTransport : public SynchronousTransport +{ +public: + ProcessQtTransport(QString processName) : + m_completenessChecker(0) { + m_process = new QProcess(); + m_process->setReadChannel(QProcess::StandardOutput); + m_process->setProcessChannelMode(QProcess::ForwardedErrorChannel); + m_process->start(processName); + if (!m_process->waitForStarted()) { + std::cerr << "server failed to start" << std::endl; + delete m_process; + m_process = nullptr; + } + } + + ~ProcessQtTransport() { + if (m_process) { + if (m_process->state() != QProcess::NotRunning) { + m_process->closeWriteChannel(); + m_process->waitForFinished(200); + m_process->close(); + m_process->waitForFinished(); + std::cerr << "server exited" << std::endl; + } + delete m_process; + } + } + + void + setCompletenessChecker(MessageCompletenessChecker *checker) { + //!!! ownership? + m_completenessChecker = checker; + } + + bool + isOK() const override { + return m_process != nullptr; + } + + std::vector<char> + call(const char *ptr, size_t size) override { + + if (!m_completenessChecker) { + throw std::logic_error("No completeness checker set on transport"); + } + + m_process->write(ptr, size); + + std::vector<char> buffer; + bool complete = false; + + while (!complete) { + + m_process->waitForReadyRead(1000); + qint64 byteCount = m_process->bytesAvailable(); + + if (!byteCount) { + if (m_process->state() == QProcess::NotRunning) { + std::cerr << "ERROR: Subprocess exited: Load failed" << std::endl; + throw std::runtime_error("Piper server exited unexpectedly"); + } + } else { + size_t formerSize = buffer.size(); + buffer.resize(formerSize + byteCount); + m_process->read(buffer.data() + formerSize, byteCount); + complete = m_completenessChecker->isComplete(buffer); + } + } + + return buffer; + } + +private: + MessageCompletenessChecker *m_completenessChecker; //!!! I don't own this (currently) + QProcess *m_process; // I own this +}; + +} +} + +#endif
--- a/vamp-client/SynchronousTransport.h Thu Oct 13 12:07:41 2016 +0100 +++ b/vamp-client/SynchronousTransport.h Thu Oct 13 14:10:55 2016 +0100 @@ -6,6 +6,7 @@ #include <cstdlib> namespace piper { +namespace vampclient { class MessageCompletenessChecker // interface { @@ -32,5 +33,6 @@ }; } +} #endif
--- a/vamp-client/client.cpp Thu Oct 13 12:07:41 2016 +0100 +++ b/vamp-client/client.cpp Thu Oct 13 14:10:55 2016 +0100 @@ -1,6 +1,6 @@ -#include "PiperQProcessTransport.h" -#include "PiperCapnpClient.h" +#include "ProcessQtTransport.h" +#include "CapnpClient.h" #include <stdexcept> @@ -9,9 +9,9 @@ int main(int, char **) { - piper::PiperQProcessTransport transport("../bin/piper-vamp-server"); - piper::PiperCapnpClient client(&transport); - + piper::vampclient::ProcessQtTransport transport("../bin/piper-vamp-server"); + piper::vampclient::CapnpClient client(&transport); + Vamp::Plugin *plugin = client.load("vamp-example-plugins:zerocrossing", 16, 0); if (!plugin->initialise(1, 4, 4)) { cerr << "initialisation failed" << endl;
--- a/vamp-client/client.pro Thu Oct 13 12:07:41 2016 +0100 +++ b/vamp-client/client.pro Thu Oct 13 14:10:55 2016 +0100 @@ -29,10 +29,11 @@ ../vamp-capnp/piper.capnp.c++ HEADERS += \ - PiperQProcessTransport.h \ - PiperCapnpClient.h \ - PiperClient.h \ - PiperPluginStub.h \ + ProcessQtTransport.h \ + CapnpClient.h \ + Loader.h \ + PluginClient.h \ + PluginStub.h \ SynchronousTransport.h