annotate src/vamp-hostsdk/PluginLoader.cpp @ 466:a94ab90dfd53 vampipe

Remove RequestResponse, PluginConfiguration and PluginStaticData again. I don't think they are proving general enough to be worth adding to this SDK at this point. Will try them out in piper-cpp instead.
author Chris Cannam
date Thu, 13 Oct 2016 17:06:53 +0100
parents 85dadd0d482f
children 0545cd3f1738
rev   line source
cannam@233 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@233 2
cannam@233 3 /*
cannam@233 4 Vamp
cannam@233 5
cannam@233 6 An API for audio analysis and feature extraction plugins.
cannam@233 7
cannam@233 8 Centre for Digital Music, Queen Mary, University of London.
Chris@423 9 Copyright 2006-2016 Chris Cannam and QMUL.
cannam@233 10
cannam@233 11 Permission is hereby granted, free of charge, to any person
cannam@233 12 obtaining a copy of this software and associated documentation
cannam@233 13 files (the "Software"), to deal in the Software without
cannam@233 14 restriction, including without limitation the rights to use, copy,
cannam@233 15 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@233 16 of the Software, and to permit persons to whom the Software is
cannam@233 17 furnished to do so, subject to the following conditions:
cannam@233 18
cannam@233 19 The above copyright notice and this permission notice shall be
cannam@233 20 included in all copies or substantial portions of the Software.
cannam@233 21
cannam@233 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@233 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@233 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@233 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@233 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@233 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@233 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@233 29
cannam@233 30 Except as contained in this notice, the names of the Centre for
cannam@233 31 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@233 32 shall not be used in advertising or otherwise to promote the sale,
cannam@233 33 use or other dealings in this Software without prior written
cannam@233 34 authorization.
cannam@233 35 */
cannam@233 36
cannam@233 37 #include <vamp-hostsdk/PluginLoader.h>
cannam@233 38 #include <vamp-hostsdk/PluginInputDomainAdapter.h>
cannam@233 39 #include <vamp-hostsdk/PluginChannelAdapter.h>
cannam@233 40 #include <vamp-hostsdk/PluginBufferingAdapter.h>
Chris@390 41 #include <vamp-hostsdk/PluginHostAdapter.h>
Chris@390 42
Chris@390 43 #include <vamp/vamp.h>
Chris@390 44
Chris@390 45 #include "Files.h"
cannam@233 46
cannam@233 47 #include <fstream>
cannam@233 48
cannam@233 49 using namespace std;
cannam@233 50
cannam@263 51 _VAMP_SDK_HOSTSPACE_BEGIN(PluginLoader.cpp)
cannam@263 52
cannam@233 53 namespace Vamp {
cannam@233 54
cannam@233 55 namespace HostExt {
cannam@233 56
cannam@233 57 class PluginLoader::Impl
cannam@233 58 {
cannam@233 59 public:
cannam@233 60 Impl();
cannam@233 61 virtual ~Impl();
cannam@233 62
cannam@233 63 PluginKeyList listPlugins();
Chris@456 64
cannam@233 65 Plugin *loadPlugin(PluginKey key,
cannam@233 66 float inputSampleRate,
cannam@233 67 int adapterFlags);
Chris@423 68
cannam@233 69 PluginKey composePluginKey(string libraryName, string identifier);
cannam@233 70
cannam@233 71 PluginCategoryHierarchy getPluginCategory(PluginKey key);
cannam@233 72
cannam@233 73 string getLibraryPathForPlugin(PluginKey key);
cannam@233 74
cannam@233 75 static void setInstanceToClean(PluginLoader *instance);
cannam@233 76
cannam@233 77 protected:
cannam@233 78 class PluginDeletionNotifyAdapter : public PluginWrapper {
cannam@233 79 public:
cannam@233 80 PluginDeletionNotifyAdapter(Plugin *plugin, Impl *loader);
cannam@233 81 virtual ~PluginDeletionNotifyAdapter();
cannam@233 82 protected:
cannam@233 83 Impl *m_loader;
cannam@233 84 };
cannam@233 85
cannam@233 86 class InstanceCleaner {
cannam@233 87 public:
cannam@233 88 InstanceCleaner() : m_instance(0) { }
cannam@233 89 ~InstanceCleaner() { delete m_instance; }
cannam@233 90 void setInstance(PluginLoader *instance) { m_instance = instance; }
cannam@233 91 protected:
cannam@233 92 PluginLoader *m_instance;
cannam@233 93 };
cannam@233 94
cannam@233 95 virtual void pluginDeleted(PluginDeletionNotifyAdapter *adapter);
cannam@233 96
cannam@233 97 map<PluginKey, string> m_pluginLibraryNameMap;
cannam@233 98 bool m_allPluginsEnumerated;
cannam@233 99 void enumeratePlugins(PluginKey forPlugin = "");
cannam@233 100
cannam@233 101 map<PluginKey, PluginCategoryHierarchy> m_taxonomy;
cannam@233 102 void generateTaxonomy();
cannam@233 103
cannam@233 104 map<Plugin *, void *> m_pluginLibraryHandleMap;
cannam@233 105
cannam@233 106 bool decomposePluginKey(PluginKey key,
cannam@233 107 string &libraryName, string &identifier);
cannam@233 108
cannam@233 109 static InstanceCleaner m_cleaner;
cannam@233 110 };
cannam@233 111
cannam@233 112 PluginLoader *
cannam@233 113 PluginLoader::m_instance = 0;
cannam@233 114
cannam@233 115 PluginLoader::Impl::InstanceCleaner
cannam@233 116 PluginLoader::Impl::m_cleaner;
cannam@233 117
cannam@233 118 PluginLoader::PluginLoader()
cannam@233 119 {
cannam@233 120 m_impl = new Impl();
cannam@233 121 }
cannam@233 122
cannam@233 123 PluginLoader::~PluginLoader()
cannam@233 124 {
cannam@233 125 delete m_impl;
cannam@233 126 }
cannam@233 127
cannam@233 128 PluginLoader *
cannam@233 129 PluginLoader::getInstance()
cannam@233 130 {
cannam@233 131 if (!m_instance) {
cannam@233 132 // The cleaner doesn't own the instance, because we leave the
cannam@233 133 // instance pointer in the base class for binary backwards
cannam@233 134 // compatibility reasons and to avoid waste
cannam@233 135 m_instance = new PluginLoader();
cannam@233 136 Impl::setInstanceToClean(m_instance);
cannam@233 137 }
cannam@233 138 return m_instance;
cannam@233 139 }
cannam@233 140
Chris@426 141 PluginLoader::PluginKeyList
cannam@233 142 PluginLoader::listPlugins()
cannam@233 143 {
cannam@233 144 return m_impl->listPlugins();
cannam@233 145 }
cannam@233 146
cannam@233 147 Plugin *
cannam@233 148 PluginLoader::loadPlugin(PluginKey key,
cannam@233 149 float inputSampleRate,
cannam@233 150 int adapterFlags)
cannam@233 151 {
cannam@233 152 return m_impl->loadPlugin(key, inputSampleRate, adapterFlags);
cannam@233 153 }
cannam@233 154
cannam@233 155 PluginLoader::PluginKey
cannam@233 156 PluginLoader::composePluginKey(string libraryName, string identifier)
cannam@233 157 {
cannam@233 158 return m_impl->composePluginKey(libraryName, identifier);
cannam@233 159 }
cannam@233 160
cannam@233 161 PluginLoader::PluginCategoryHierarchy
cannam@233 162 PluginLoader::getPluginCategory(PluginKey key)
cannam@233 163 {
cannam@233 164 return m_impl->getPluginCategory(key);
cannam@233 165 }
cannam@233 166
cannam@233 167 string
cannam@233 168 PluginLoader::getLibraryPathForPlugin(PluginKey key)
cannam@233 169 {
cannam@233 170 return m_impl->getLibraryPathForPlugin(key);
cannam@233 171 }
cannam@233 172
cannam@233 173 PluginLoader::Impl::Impl() :
cannam@233 174 m_allPluginsEnumerated(false)
cannam@233 175 {
cannam@233 176 }
cannam@233 177
cannam@233 178 PluginLoader::Impl::~Impl()
cannam@233 179 {
cannam@233 180 }
cannam@233 181
cannam@233 182 void
cannam@233 183 PluginLoader::Impl::setInstanceToClean(PluginLoader *instance)
cannam@233 184 {
cannam@233 185 m_cleaner.setInstance(instance);
cannam@233 186 }
cannam@233 187
Chris@426 188 PluginLoader::PluginKeyList
cannam@233 189 PluginLoader::Impl::listPlugins()
cannam@233 190 {
cannam@233 191 if (!m_allPluginsEnumerated) enumeratePlugins();
cannam@233 192
cannam@233 193 vector<PluginKey> plugins;
cannam@233 194 for (map<PluginKey, string>::iterator mi = m_pluginLibraryNameMap.begin();
cannam@233 195 mi != m_pluginLibraryNameMap.end(); ++mi) {
cannam@233 196 plugins.push_back(mi->first);
cannam@233 197 }
cannam@233 198
cannam@233 199 return plugins;
cannam@233 200 }
cannam@233 201
cannam@233 202 void
cannam@233 203 PluginLoader::Impl::enumeratePlugins(PluginKey forPlugin)
cannam@233 204 {
cannam@233 205 string libraryName, identifier;
Chris@390 206 vector<string> fullPaths;
Chris@390 207
cannam@233 208 if (forPlugin != "") {
cannam@233 209 if (!decomposePluginKey(forPlugin, libraryName, identifier)) {
cannam@233 210 std::cerr << "WARNING: Vamp::HostExt::PluginLoader: Invalid plugin key \""
cannam@233 211 << forPlugin << "\" in enumerate" << std::endl;
cannam@233 212 return;
cannam@233 213 }
Chris@390 214 fullPaths = Files::listLibraryFilesMatching(libraryName);
Chris@390 215 } else {
Chris@390 216 fullPaths = Files::listLibraryFiles();
cannam@233 217 }
cannam@233 218
Chris@390 219 for (size_t i = 0; i < fullPaths.size(); ++i) {
cannam@233 220
Chris@390 221 string fullPath = fullPaths[i];
Chris@390 222 void *handle = Files::loadLibrary(fullPath);
Chris@390 223 if (!handle) continue;
cannam@233 224
Chris@390 225 VampGetPluginDescriptorFunction fn =
Chris@390 226 (VampGetPluginDescriptorFunction)Files::lookupInLibrary
Chris@390 227 (handle, "vampGetPluginDescriptor");
cannam@233 228
Chris@390 229 if (!fn) {
Chris@390 230 if (forPlugin != "") {
Chris@390 231 cerr << "Vamp::HostExt::PluginLoader: No vampGetPluginDescriptor function found in library \""
cannam@295 232 << fullPath << "\"" << endl;
cannam@295 233 }
Chris@390 234 Files::unloadLibrary(handle);
Chris@390 235 continue;
Chris@390 236 }
cannam@233 237
Chris@390 238 int index = 0;
Chris@390 239 const VampPluginDescriptor *descriptor = 0;
Chris@390 240 bool found = false;
Chris@390 241
Chris@390 242 while ((descriptor = fn(VAMP_API_VERSION, index))) {
Chris@390 243 ++index;
Chris@390 244 if (identifier != "") {
Chris@390 245 if (descriptor->identifier != identifier) continue;
Chris@390 246 }
Chris@390 247 found = true;
Chris@390 248 PluginKey key = composePluginKey(fullPath, descriptor->identifier);
Chris@390 249 // std::cerr << "enumerate: " << key << " (path: " << fullPath << ")" << std::endl;
Chris@390 250 if (m_pluginLibraryNameMap.find(key) ==
Chris@390 251 m_pluginLibraryNameMap.end()) {
Chris@390 252 m_pluginLibraryNameMap[key] = fullPath;
Chris@390 253 }
cannam@233 254 }
Chris@390 255
Chris@390 256 if (!found && forPlugin != "") {
Chris@390 257 cerr << "Vamp::HostExt::PluginLoader: Plugin \""
Chris@390 258 << identifier << "\" not found in library \""
Chris@390 259 << fullPath << "\"" << endl;
Chris@390 260 }
Chris@390 261
Chris@390 262 Files::unloadLibrary(handle);
cannam@233 263 }
cannam@233 264
cannam@233 265 if (forPlugin == "") m_allPluginsEnumerated = true;
cannam@233 266 }
cannam@233 267
cannam@233 268 PluginLoader::PluginKey
cannam@233 269 PluginLoader::Impl::composePluginKey(string libraryName, string identifier)
cannam@233 270 {
Chris@390 271 string basename = Files::lcBasename(libraryName);
cannam@233 272 return basename + ":" + identifier;
cannam@233 273 }
cannam@233 274
cannam@233 275 bool
cannam@233 276 PluginLoader::Impl::decomposePluginKey(PluginKey key,
cannam@233 277 string &libraryName,
cannam@233 278 string &identifier)
cannam@233 279 {
cannam@233 280 string::size_type ki = key.find(':');
cannam@233 281 if (ki == string::npos) {
cannam@233 282 return false;
cannam@233 283 }
cannam@233 284
cannam@233 285 libraryName = key.substr(0, ki);
cannam@233 286 identifier = key.substr(ki + 1);
cannam@233 287 return true;
cannam@233 288 }
cannam@233 289
cannam@233 290 PluginLoader::PluginCategoryHierarchy
cannam@233 291 PluginLoader::Impl::getPluginCategory(PluginKey plugin)
cannam@233 292 {
cannam@233 293 if (m_taxonomy.empty()) generateTaxonomy();
cannam@233 294 if (m_taxonomy.find(plugin) == m_taxonomy.end()) {
cannam@233 295 return PluginCategoryHierarchy();
cannam@233 296 }
cannam@233 297 return m_taxonomy[plugin];
cannam@233 298 }
cannam@233 299
cannam@233 300 string
cannam@233 301 PluginLoader::Impl::getLibraryPathForPlugin(PluginKey plugin)
cannam@233 302 {
cannam@233 303 if (m_pluginLibraryNameMap.find(plugin) == m_pluginLibraryNameMap.end()) {
cannam@233 304 if (m_allPluginsEnumerated) return "";
cannam@233 305 enumeratePlugins(plugin);
cannam@233 306 }
cannam@233 307 if (m_pluginLibraryNameMap.find(plugin) == m_pluginLibraryNameMap.end()) {
cannam@233 308 return "";
cannam@233 309 }
cannam@233 310 return m_pluginLibraryNameMap[plugin];
cannam@233 311 }
cannam@233 312
cannam@233 313 Plugin *
cannam@233 314 PluginLoader::Impl::loadPlugin(PluginKey key,
cannam@233 315 float inputSampleRate, int adapterFlags)
cannam@233 316 {
cannam@233 317 string libname, identifier;
cannam@233 318 if (!decomposePluginKey(key, libname, identifier)) {
cannam@233 319 std::cerr << "Vamp::HostExt::PluginLoader: Invalid plugin key \""
cannam@233 320 << key << "\" in loadPlugin" << std::endl;
cannam@233 321 return 0;
cannam@233 322 }
cannam@233 323
cannam@233 324 string fullPath = getLibraryPathForPlugin(key);
cannam@293 325 if (fullPath == "") {
cannam@295 326 std::cerr << "Vamp::HostExt::PluginLoader: No library found in Vamp path for plugin \"" << key << "\"" << std::endl;
cannam@293 327 return 0;
cannam@293 328 }
cannam@233 329
Chris@390 330 void *handle = Files::loadLibrary(fullPath);
cannam@233 331 if (!handle) return 0;
cannam@233 332
cannam@233 333 VampGetPluginDescriptorFunction fn =
Chris@390 334 (VampGetPluginDescriptorFunction)Files::lookupInLibrary
cannam@233 335 (handle, "vampGetPluginDescriptor");
cannam@233 336
cannam@233 337 if (!fn) {
cannam@293 338 cerr << "Vamp::HostExt::PluginLoader: No vampGetPluginDescriptor function found in library \""
cannam@293 339 << fullPath << "\"" << endl;
Chris@390 340 Files::unloadLibrary(handle);
cannam@233 341 return 0;
cannam@233 342 }
cannam@233 343
cannam@233 344 int index = 0;
cannam@233 345 const VampPluginDescriptor *descriptor = 0;
cannam@233 346
cannam@233 347 while ((descriptor = fn(VAMP_API_VERSION, index))) {
cannam@233 348
cannam@233 349 if (string(descriptor->identifier) == identifier) {
cannam@233 350
cannam@233 351 Vamp::PluginHostAdapter *plugin =
cannam@233 352 new Vamp::PluginHostAdapter(descriptor, inputSampleRate);
cannam@233 353
cannam@233 354 Plugin *adapter = new PluginDeletionNotifyAdapter(plugin, this);
cannam@233 355
cannam@233 356 m_pluginLibraryHandleMap[adapter] = handle;
cannam@233 357
cannam@233 358 if (adapterFlags & ADAPT_INPUT_DOMAIN) {
cannam@233 359 if (adapter->getInputDomain() == Plugin::FrequencyDomain) {
cannam@233 360 adapter = new PluginInputDomainAdapter(adapter);
cannam@233 361 }
cannam@233 362 }
cannam@233 363
cannam@233 364 if (adapterFlags & ADAPT_BUFFER_SIZE) {
cannam@233 365 adapter = new PluginBufferingAdapter(adapter);
cannam@233 366 }
cannam@233 367
cannam@233 368 if (adapterFlags & ADAPT_CHANNEL_COUNT) {
cannam@233 369 adapter = new PluginChannelAdapter(adapter);
cannam@233 370 }
cannam@233 371
cannam@233 372 return adapter;
cannam@233 373 }
cannam@233 374
cannam@233 375 ++index;
cannam@233 376 }
cannam@233 377
cannam@233 378 cerr << "Vamp::HostExt::PluginLoader: Plugin \""
cannam@233 379 << identifier << "\" not found in library \""
cannam@233 380 << fullPath << "\"" << endl;
cannam@233 381
cannam@233 382 return 0;
cannam@233 383 }
cannam@233 384
cannam@233 385 void
cannam@233 386 PluginLoader::Impl::generateTaxonomy()
cannam@233 387 {
cannam@233 388 // cerr << "PluginLoader::Impl::generateTaxonomy" << endl;
cannam@233 389
cannam@233 390 vector<string> path = PluginHostAdapter::getPluginPath();
cannam@233 391 string libfragment = "/lib/";
cannam@233 392 vector<string> catpath;
cannam@233 393
cannam@233 394 string suffix = "cat";
cannam@233 395
cannam@233 396 for (vector<string>::iterator i = path.begin();
cannam@233 397 i != path.end(); ++i) {
cannam@233 398
cannam@233 399 // It doesn't matter that we're using literal forward-slash in
cannam@233 400 // this bit, as it's only relevant if the path contains
cannam@233 401 // "/lib/", which is only meaningful and only plausible on
cannam@233 402 // systems with forward-slash delimiters
cannam@233 403
cannam@233 404 string dir = *i;
cannam@233 405 string::size_type li = dir.find(libfragment);
cannam@233 406
cannam@233 407 if (li != string::npos) {
cannam@233 408 catpath.push_back
cannam@233 409 (dir.substr(0, li)
cannam@233 410 + "/share/"
cannam@233 411 + dir.substr(li + libfragment.length()));
cannam@233 412 }
cannam@233 413
cannam@233 414 catpath.push_back(dir);
cannam@233 415 }
cannam@233 416
cannam@233 417 char buffer[1024];
cannam@233 418
cannam@233 419 for (vector<string>::iterator i = catpath.begin();
cannam@233 420 i != catpath.end(); ++i) {
cannam@233 421
Chris@390 422 vector<string> files = Files::listFiles(*i, suffix);
cannam@233 423
cannam@233 424 for (vector<string>::iterator fi = files.begin();
cannam@233 425 fi != files.end(); ++fi) {
cannam@233 426
Chris@390 427 string filepath = Files::splicePath(*i, *fi);
cannam@233 428 ifstream is(filepath.c_str(), ifstream::in | ifstream::binary);
cannam@233 429
cannam@233 430 if (is.fail()) {
cannam@233 431 // cerr << "failed to open: " << filepath << endl;
cannam@233 432 continue;
cannam@233 433 }
cannam@233 434
cannam@233 435 // cerr << "opened: " << filepath << endl;
cannam@233 436
cannam@233 437 while (!!is.getline(buffer, 1024)) {
cannam@233 438
cannam@233 439 string line(buffer);
cannam@233 440
cannam@233 441 // cerr << "line = " << line << endl;
cannam@233 442
cannam@233 443 string::size_type di = line.find("::");
cannam@233 444 if (di == string::npos) continue;
cannam@233 445
cannam@233 446 string id = line.substr(0, di);
cannam@233 447 string encodedCat = line.substr(di + 2);
cannam@233 448
cannam@233 449 if (id.substr(0, 5) != "vamp:") continue;
cannam@233 450 id = id.substr(5);
cannam@233 451
cannam@233 452 while (encodedCat.length() >= 1 &&
cannam@233 453 encodedCat[encodedCat.length()-1] == '\r') {
cannam@233 454 encodedCat = encodedCat.substr(0, encodedCat.length()-1);
cannam@233 455 }
cannam@233 456
cannam@233 457 // cerr << "id = " << id << ", cat = " << encodedCat << endl;
cannam@233 458
cannam@233 459 PluginCategoryHierarchy category;
cannam@233 460 string::size_type ai;
cannam@233 461 while ((ai = encodedCat.find(" > ")) != string::npos) {
cannam@233 462 category.push_back(encodedCat.substr(0, ai));
cannam@233 463 encodedCat = encodedCat.substr(ai + 3);
cannam@233 464 }
cannam@233 465 if (encodedCat != "") category.push_back(encodedCat);
cannam@233 466
cannam@233 467 m_taxonomy[id] = category;
cannam@233 468 }
cannam@233 469 }
cannam@233 470 }
cannam@233 471 }
cannam@233 472
cannam@233 473 void
cannam@233 474 PluginLoader::Impl::pluginDeleted(PluginDeletionNotifyAdapter *adapter)
cannam@233 475 {
cannam@233 476 void *handle = m_pluginLibraryHandleMap[adapter];
Chris@390 477 if (handle) Files::unloadLibrary(handle);
cannam@233 478 m_pluginLibraryHandleMap.erase(adapter);
cannam@233 479 }
cannam@233 480
cannam@233 481 PluginLoader::Impl::PluginDeletionNotifyAdapter::PluginDeletionNotifyAdapter(Plugin *plugin,
cannam@233 482 Impl *loader) :
cannam@233 483 PluginWrapper(plugin),
cannam@233 484 m_loader(loader)
cannam@233 485 {
cannam@233 486 }
cannam@233 487
cannam@233 488 PluginLoader::Impl::PluginDeletionNotifyAdapter::~PluginDeletionNotifyAdapter()
cannam@233 489 {
cannam@233 490 // We need to delete the plugin before calling pluginDeleted, as
cannam@233 491 // the delete call may require calling through to the descriptor
cannam@233 492 // (for e.g. cleanup) but pluginDeleted may unload the required
cannam@233 493 // library for the call. To prevent a double deletion when our
cannam@233 494 // parent's destructor runs (after this one), be sure to set
cannam@233 495 // m_plugin to 0 after deletion.
cannam@233 496 delete m_plugin;
cannam@233 497 m_plugin = 0;
cannam@233 498
cannam@233 499 if (m_loader) m_loader->pluginDeleted(this);
cannam@233 500 }
cannam@233 501
cannam@233 502 }
cannam@233 503
cannam@233 504 }
cannam@263 505
cannam@263 506 _VAMP_SDK_HOSTSPACE_END(PluginLoader.cpp)
cannam@263 507