changeset 95:b6ac26b72b59

Implement list, use request-response classes in loader
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 13 Oct 2016 14:31:10 +0100
parents a660dca988f8
children 215c9fb6b7a4
files vamp-capnp/VampnProto.h vamp-client/CapnpClient.h vamp-client/Loader.h vamp-client/client.cpp
diffstat 4 files changed, 114 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/vamp-capnp/VampnProto.h	Thu Oct 13 14:10:55 2016 +0100
+++ b/vamp-capnp/VampnProto.h	Thu Oct 13 14:31:10 2016 +0100
@@ -496,6 +496,19 @@
     }
 
     static void
+    readListResponse(Vamp::HostExt::ListResponse &lr,
+                     const ListResponse::Reader &r) {
+
+        lr.available.clear();
+        auto pp = r.getAvailable();
+        for (const auto &p: pp) {
+            Vamp::HostExt::PluginStaticData psd;
+            readExtractorStaticData(psd, p);
+            lr.available.push_back(psd);
+        }
+    }
+
+    static void
     buildLoadRequest(LoadRequest::Builder &r,
                      const Vamp::HostExt::LoadRequest &req) {
 
@@ -902,13 +915,7 @@
         if (getRequestResponseType(r) != RRType::List) {
             throw std::logic_error("not a list response");
         }
-        resp.available.clear();
-        auto pp = r.getResponse().getList().getAvailable();
-        for (const auto &p: pp) {
-            Vamp::HostExt::PluginStaticData psd;
-            readExtractorStaticData(psd, p);
-            resp.available.push_back(psd);
-        }
+        readListResponse(resp, r.getResponse().getList());
     }
     
     static void
--- a/vamp-client/CapnpClient.h	Thu Oct 13 14:10:55 2016 +0100
+++ b/vamp-client/CapnpClient.h	Thu Oct 13 14:31:10 2016 +0100
@@ -53,71 +53,66 @@
     //!!! 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) {
+
+    // Loader methods:
+
+    Vamp::HostExt::ListResponse
+    listPluginData() override {
 
         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);
+        VampnProto::buildRpcRequest_List(builder);
         ReqId id = getId();
         builder.getId().setNumber(id);
 
+        //!!! pure boilerplate:
         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::LIST, id);
 
-        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();
-    };     
+        Vamp::HostExt::ListResponse lr;
+        VampnProto::readListResponse(lr, reader.getResponse().getList());
+        return lr;
+    }
+    
+    Vamp::HostExt::LoadResponse
+    loadPlugin(const Vamp::HostExt::LoadRequest &req) override {
 
-protected:
+        if (!m_transport->isOK()) {
+            throw std::runtime_error("Piper server failed to start");
+        }
+
+        Vamp::HostExt::LoadResponse resp;
+        PluginHandleMapper::Handle handle = serverLoad(req.pluginKey,
+                                                       req.inputSampleRate,
+                                                       req.adapterFlags,
+                                                       resp.staticData,
+                                                       resp.defaultConfiguration);
+
+        Vamp::Plugin *plugin = new PluginStub(this,
+                                              req.pluginKey,
+                                              req.inputSampleRate,
+                                              req.adapterFlags,
+                                              resp.staticData,
+                                              resp.defaultConfiguration);
+
+        m_mapper.addPlugin(handle, plugin);
+
+        resp.plugin = plugin;
+        return resp;
+    }
+
+    // PluginClient methods:
+    
     virtual
     Vamp::Plugin::OutputList
     configure(PluginStub *plugin,
@@ -298,6 +293,45 @@
             throw std::runtime_error("Wrong response id");
         }
     }
+    
+    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();
+    };     
 
 private:
     SynchronousTransport *m_transport; //!!! I don't own this, but should I?
--- a/vamp-client/Loader.h	Thu Oct 13 14:10:55 2016 +0100
+++ b/vamp-client/Loader.h	Thu Oct 13 14:31:10 2016 +0100
@@ -2,7 +2,7 @@
 #ifndef PIPER_LOADER_H
 #define PIPER_LOADER_H
 
-#include <vamp-hostsdk/Plugin.h>
+#include <vamp-hostsdk/RequestResponse.h>
 
 namespace piper {
 namespace vampclient {
@@ -11,8 +11,12 @@
 {
 public:
     virtual
-    Vamp::Plugin *
-    load(std::string key, float inputSampleRate, int adapterFlags) = 0;
+    Vamp::HostExt::ListResponse
+    listPluginData() = 0;
+    
+    virtual
+    Vamp::HostExt::LoadResponse
+    loadPlugin(const Vamp::HostExt::LoadRequest &) = 0;
 };
 
 }
--- a/vamp-client/client.cpp	Thu Oct 13 14:10:55 2016 +0100
+++ b/vamp-client/client.cpp	Thu Oct 13 14:31:10 2016 +0100
@@ -12,7 +12,19 @@
     piper::vampclient::ProcessQtTransport transport("../bin/piper-vamp-server");
     piper::vampclient::CapnpClient client(&transport);
 
-    Vamp::Plugin *plugin = client.load("vamp-example-plugins:zerocrossing", 16, 0);
+    Vamp::HostExt::ListResponse lr = client.listPluginData();
+    cerr << "Plugins available:" << endl;
+    int i = 1;
+    for (const auto &p: lr.available) {
+        cerr << i++ << ". [" << p.pluginKey << "] " << p.basic.name << endl;
+    }
+    
+    Vamp::HostExt::LoadRequest req;
+    req.pluginKey = "vamp-example-plugins:zerocrossing";
+    req.inputSampleRate = 16;
+    Vamp::HostExt::LoadResponse resp = client.loadPlugin(req);
+    Vamp::Plugin *plugin = resp.plugin;
+    
     if (!plugin->initialise(1, 4, 4)) {
         cerr << "initialisation failed" << endl;
     } else {