comparison vamp-client/PluginStub.h @ 167:4a37daf5f8b4

Add a failed state to the plugin stub, to prevent it making any further requests after (for example) a process has timed out
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 31 Jan 2017 11:08:31 +0000
parents ff3fd8d1b2dc
children f13dc1db2229
comparison
equal deleted inserted replaced
166:49f606e3653e 167:4a37daf5f8b4
40 #include <vamp-hostsdk/PluginLoader.h> 40 #include <vamp-hostsdk/PluginLoader.h>
41 41
42 #include "vamp-support/PluginStaticData.h" 42 #include "vamp-support/PluginStaticData.h"
43 #include "vamp-support/PluginConfiguration.h" 43 #include "vamp-support/PluginConfiguration.h"
44 44
45 #include "PluginClient.h"
46
45 #include <cstdint> 47 #include <cstdint>
46 48 #include <iostream>
47 #include "PluginClient.h"
48 49
49 namespace piper_vamp { 50 namespace piper_vamp {
50 namespace client { 51 namespace client {
51 52
52 class PluginStub : public Vamp::Plugin 53 class PluginStub : public Vamp::Plugin
53 { 54 {
54 enum State { 55 enum State {
55 Loaded, Configured, Finished 56 Loaded, Configured, Finished, Failed
56 }; 57 };
57 58
58 public: 59 public:
59 PluginStub(PluginClient *client, 60 PluginStub(PluginClient *client,
60 std::string pluginKey, 61 std::string pluginKey,
71 m_defaultConfig(defaultConfig), 72 m_defaultConfig(defaultConfig),
72 m_config(defaultConfig) 73 m_config(defaultConfig)
73 { } 74 { }
74 75
75 virtual ~PluginStub() { 76 virtual ~PluginStub() {
76 if (m_state != Finished) { 77 if (m_state != Finished && m_state != Failed) {
77 (void)m_client->finish(this); 78 try {
79 (void)m_client->finish(this);
80 } catch (const std::exception &e) {
81 // Finish can throw, but our destructor must not
82 std::cerr << "WARNING: PluginStub::~PluginStub: caught exception from finish(): " << e.what() << std::endl;
83 }
78 } 84 }
79 } 85 }
80 86
81 virtual std::string getIdentifier() const { 87 virtual std::string getIdentifier() const {
82 return m_psd.basic.identifier; 88 return m_psd.basic.identifier;
113 return 0.f; 119 return 0.f;
114 } 120 }
115 } 121 }
116 122
117 virtual void setParameter(std::string name, float value) { 123 virtual void setParameter(std::string name, float value) {
124 if (m_state == Failed) {
125 throw std::logic_error("Plugin is in failed state");
126 }
118 if (m_state != Loaded) { 127 if (m_state != Loaded) {
128 m_state = Failed;
119 throw std::logic_error("Can't set parameter after plugin initialised"); 129 throw std::logic_error("Can't set parameter after plugin initialised");
120 } 130 }
121 m_config.parameterValues[name] = value; 131 m_config.parameterValues[name] = value;
122 } 132 }
123 133
128 virtual std::string getCurrentProgram() const { 138 virtual std::string getCurrentProgram() const {
129 return m_config.currentProgram; 139 return m_config.currentProgram;
130 } 140 }
131 141
132 virtual void selectProgram(std::string program) { 142 virtual void selectProgram(std::string program) {
143 if (m_state == Failed) {
144 throw std::logic_error("Plugin is in failed state");
145 }
133 if (m_state != Loaded) { 146 if (m_state != Loaded) {
147 m_state = Failed;
134 throw std::logic_error("Can't select program after plugin initialised"); 148 throw std::logic_error("Can't select program after plugin initialised");
135 } 149 }
136 m_config.currentProgram = program; 150 m_config.currentProgram = program;
137 } 151 }
138 152
139 virtual bool initialise(size_t inputChannels, 153 virtual bool initialise(size_t inputChannels,
140 size_t stepSize, 154 size_t stepSize,
141 size_t blockSize) { 155 size_t blockSize) {
142 156
157 if (m_state == Failed) {
158 throw std::logic_error("Plugin is in failed state");
159 }
143 if (m_state != Loaded) { 160 if (m_state != Loaded) {
161 m_state = Failed;
144 throw std::logic_error("Plugin has already been initialised"); 162 throw std::logic_error("Plugin has already been initialised");
145 } 163 }
146 164
147 m_config.channelCount = int(inputChannels); 165 m_config.channelCount = int(inputChannels);
148 m_config.stepSize = int(stepSize); 166 m_config.stepSize = int(stepSize);
158 } 176 }
159 } 177 }
160 178
161 virtual void reset() { 179 virtual void reset() {
162 180
181 if (m_state == Failed) {
182 throw std::logic_error("Plugin is in failed state");
183 }
163 if (m_state == Loaded) { 184 if (m_state == Loaded) {
164 // reset is a no-op if the plugin hasn't been initialised yet 185 // reset is a no-op if the plugin hasn't been initialised yet
165 return; 186 return;
166 } 187 }
167 188
189 virtual size_t getMaxChannelCount() const { 210 virtual size_t getMaxChannelCount() const {
190 return m_psd.maxChannelCount; 211 return m_psd.maxChannelCount;
191 } 212 }
192 213
193 virtual OutputList getOutputDescriptors() const { 214 virtual OutputList getOutputDescriptors() const {
215
216 if (m_state == Failed) {
217 throw std::logic_error("Plugin is in failed state");
218 }
194 if (m_state == Configured) { 219 if (m_state == Configured) {
195 return m_outputs; 220 return m_outputs;
196 } 221 }
197 222
198 //!!! todo: figure out for which hosts (and adapters?) it may 223 //!!! todo: figure out for which hosts (and adapters?) it may
212 } 237 }
213 238
214 virtual FeatureSet process(const float *const *inputBuffers, 239 virtual FeatureSet process(const float *const *inputBuffers,
215 Vamp::RealTime timestamp) { 240 Vamp::RealTime timestamp) {
216 241
242 if (m_state == Failed) {
243 throw std::logic_error("Plugin is in failed state");
244 }
217 if (m_state == Loaded) { 245 if (m_state == Loaded) {
246 m_state = Failed;
218 throw std::logic_error("Plugin has not been initialised"); 247 throw std::logic_error("Plugin has not been initialised");
219 } 248 }
220 if (m_state == Finished) { 249 if (m_state == Finished) {
250 m_state = Failed;
221 throw std::logic_error("Plugin has already been disposed of"); 251 throw std::logic_error("Plugin has already been disposed of");
222 } 252 }
223 253
224 //!!! ew 254 //!!! ew
225 std::vector<std::vector<float> > vecbuf; 255 std::vector<std::vector<float> > vecbuf;
232 return m_client->process(this, vecbuf, timestamp); 262 return m_client->process(this, vecbuf, timestamp);
233 } 263 }
234 264
235 virtual FeatureSet getRemainingFeatures() { 265 virtual FeatureSet getRemainingFeatures() {
236 266
267 if (m_state == Failed) {
268 throw std::logic_error("Plugin is in failed state");
269 }
237 if (m_state == Loaded) { 270 if (m_state == Loaded) {
271 m_state = Failed;
238 throw std::logic_error("Plugin has not been configured"); 272 throw std::logic_error("Plugin has not been configured");
239 } 273 }
240 if (m_state == Finished) { 274 if (m_state == Finished) {
275 m_state = Failed;
241 throw std::logic_error("Plugin has already been disposed of"); 276 throw std::logic_error("Plugin has already been disposed of");
242 } 277 }
243 278
244 m_state = Finished; 279 m_state = Finished;
245 280