comparison plugin/FeatureExtractionPluginFactory.cpp @ 298:57d7889f626c

* Unload feature extraction plugin .so when destroyed
author Chris Cannam
date Fri, 28 Sep 2007 14:32:45 +0000
parents 524bcd89743b
children 576be0d0d218
comparison
equal deleted inserted replaced
297:c022976d18e8 298:57d7889f626c
16 #include "FeatureExtractionPluginFactory.h" 16 #include "FeatureExtractionPluginFactory.h"
17 #include "PluginIdentifier.h" 17 #include "PluginIdentifier.h"
18 18
19 #include "vamp/vamp.h" 19 #include "vamp/vamp.h"
20 #include "vamp-sdk/PluginHostAdapter.h" 20 #include "vamp-sdk/PluginHostAdapter.h"
21 #include "vamp-sdk/hostext/PluginWrapper.h"
21 22
22 #include "system/System.h" 23 #include "system/System.h"
23 24
24 #include <QDir> 25 #include <QDir>
25 #include <QFile> 26 #include <QFile>
27 #include <QTextStream> 28 #include <QTextStream>
28 29
29 #include <iostream> 30 #include <iostream>
30 31
31 //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1 32 //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1
33
34 class PluginDeletionNotifyAdapter : public Vamp::HostExt::PluginWrapper {
35 public:
36 PluginDeletionNotifyAdapter(Vamp::Plugin *plugin,
37 FeatureExtractionPluginFactory *factory) :
38 PluginWrapper(plugin), m_factory(factory) { }
39 virtual ~PluginDeletionNotifyAdapter();
40 protected:
41 FeatureExtractionPluginFactory *m_factory;
42 };
43
44 PluginDeletionNotifyAdapter::~PluginDeletionNotifyAdapter()
45 {
46 // see notes in vamp-sdk/hostext/PluginLoader.cpp from which this is drawn
47 Vamp::Plugin *p = m_plugin;
48 delete m_plugin;
49 m_plugin = 0;
50 if (m_factory) m_factory->pluginDeleted(p);
51 }
32 52
33 static FeatureExtractionPluginFactory *_nativeInstance = 0; 53 static FeatureExtractionPluginFactory *_nativeInstance = 0;
34 54
35 FeatureExtractionPluginFactory * 55 FeatureExtractionPluginFactory *
36 FeatureExtractionPluginFactory::instance(QString pluginType) 56 FeatureExtractionPluginFactory::instance(QString pluginType)
274 Vamp::Plugin * 294 Vamp::Plugin *
275 FeatureExtractionPluginFactory::instantiatePlugin(QString identifier, 295 FeatureExtractionPluginFactory::instantiatePlugin(QString identifier,
276 float inputSampleRate) 296 float inputSampleRate)
277 { 297 {
278 Vamp::Plugin *rv = 0; 298 Vamp::Plugin *rv = 0;
299 Vamp::PluginHostAdapter *plugin = 0;
279 300
280 const VampPluginDescriptor *descriptor = 0; 301 const VampPluginDescriptor *descriptor = 0;
281 int index = 0; 302 int index = 0;
282 303
283 QString type, soname, label; 304 QString type, soname, label;
326 if (!descriptor) { 347 if (!descriptor) {
327 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to find plugin \"" << label.toStdString() << "\" in library " << soname.toStdString() << std::endl; 348 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to find plugin \"" << label.toStdString() << "\" in library " << soname.toStdString() << std::endl;
328 goto done; 349 goto done;
329 } 350 }
330 351
331 rv = new Vamp::PluginHostAdapter(descriptor, inputSampleRate); 352 plugin = new Vamp::PluginHostAdapter(descriptor, inputSampleRate);
353
354 if (plugin) {
355 m_handleMap[plugin] = libraryHandle;
356 rv = new PluginDeletionNotifyAdapter(plugin, this);
357 }
332 358
333 // std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Constructed Vamp plugin, rv is " << rv << std::endl; 359 // std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Constructed Vamp plugin, rv is " << rv << std::endl;
334 360
335 //!!! need to dlclose() when plugins from a given library are unloaded 361 //!!! need to dlclose() when plugins from a given library are unloaded
336 362
342 } 368 }
343 369
344 // std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Instantiated plugin " << label.toStdString() << " from library " << soname.toStdString() << ": descriptor " << descriptor << ", rv "<< rv << ", label " << rv->getName() << ", outputs " << rv->getOutputDescriptors().size() << std::endl; 370 // std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Instantiated plugin " << label.toStdString() << " from library " << soname.toStdString() << ": descriptor " << descriptor << ", rv "<< rv << ", label " << rv->getName() << ", outputs " << rv->getOutputDescriptors().size() << std::endl;
345 371
346 return rv; 372 return rv;
373 }
374
375 void
376 FeatureExtractionPluginFactory::pluginDeleted(Vamp::Plugin *plugin)
377 {
378 void *handle = m_handleMap[plugin];
379 if (handle) {
380 std::cerr << "unloading library " << handle << " for plugin " << plugin << std::endl;
381 DLCLOSE(handle);
382 }
383 m_handleMap.erase(plugin);
347 } 384 }
348 385
349 QString 386 QString
350 FeatureExtractionPluginFactory::getPluginCategory(QString identifier) 387 FeatureExtractionPluginFactory::getPluginCategory(QString identifier)
351 { 388 {