cannam@227: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@227: cannam@227: /* cannam@227: Vamp cannam@227: cannam@227: An API for audio analysis and feature extraction plugins. cannam@227: cannam@227: Centre for Digital Music, Queen Mary, University of London. cannam@227: Copyright 2006-2007 Chris Cannam and QMUL. cannam@227: cannam@227: Permission is hereby granted, free of charge, to any person cannam@227: obtaining a copy of this software and associated documentation cannam@227: files (the "Software"), to deal in the Software without cannam@227: restriction, including without limitation the rights to use, copy, cannam@227: modify, merge, publish, distribute, sublicense, and/or sell copies cannam@227: of the Software, and to permit persons to whom the Software is cannam@227: furnished to do so, subject to the following conditions: cannam@227: cannam@227: The above copyright notice and this permission notice shall be cannam@227: included in all copies or substantial portions of the Software. cannam@227: cannam@227: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@227: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@227: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@227: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR cannam@227: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@227: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@227: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@227: cannam@227: Except as contained in this notice, the names of the Centre for cannam@227: Digital Music; Queen Mary, University of London; and Chris Cannam cannam@227: shall not be used in advertising or otherwise to promote the sale, cannam@227: use or other dealings in this Software without prior written cannam@227: authorization. cannam@227: */ cannam@227: cannam@227: #include "vamp-sdk/PluginHostAdapter.h" cannam@227: #include "PluginLoader.h" cannam@227: #include "PluginInputDomainAdapter.h" cannam@227: #include "PluginChannelAdapter.h" cannam@227: #include "PluginBufferingAdapter.h" cannam@227: cannam@227: #include cannam@227: #include // tolower cannam@227: cannam@227: #include cannam@227: cannam@227: #ifdef _WIN32 cannam@227: cannam@227: #include cannam@227: #include cannam@227: #define PLUGIN_SUFFIX "dll" cannam@227: cannam@227: #else /* ! _WIN32 */ cannam@227: cannam@227: #include cannam@227: #include cannam@227: cannam@227: #ifdef __APPLE__ cannam@227: #define PLUGIN_SUFFIX "dylib" cannam@227: #else /* ! __APPLE__ */ cannam@227: #define PLUGIN_SUFFIX "so" cannam@227: #endif /* ! __APPLE__ */ cannam@227: cannam@227: #endif /* ! _WIN32 */ cannam@227: cannam@227: using namespace std; cannam@227: cannam@227: namespace Vamp { cannam@227: cannam@227: namespace HostExt { cannam@227: cannam@227: class PluginLoader::Impl cannam@227: { cannam@227: public: cannam@227: Impl(); cannam@227: virtual ~Impl(); cannam@227: cannam@227: PluginKeyList listPlugins(); cannam@227: cannam@227: Plugin *loadPlugin(PluginKey key, cannam@227: float inputSampleRate, cannam@227: int adapterFlags); cannam@227: cannam@227: PluginKey composePluginKey(string libraryName, string identifier); cannam@227: cannam@227: PluginCategoryHierarchy getPluginCategory(PluginKey key); cannam@227: cannam@227: string getLibraryPathForPlugin(PluginKey key); cannam@227: cannam@227: static void setInstanceToClean(PluginLoader *instance); cannam@227: cannam@227: protected: cannam@227: class PluginDeletionNotifyAdapter : public PluginWrapper { cannam@227: public: cannam@227: PluginDeletionNotifyAdapter(Plugin *plugin, Impl *loader); cannam@227: virtual ~PluginDeletionNotifyAdapter(); cannam@227: protected: cannam@227: Impl *m_loader; cannam@227: }; cannam@227: cannam@227: class InstanceCleaner { cannam@227: public: cannam@227: InstanceCleaner() : m_instance(0) { } cannam@227: ~InstanceCleaner() { delete m_instance; } cannam@227: void setInstance(PluginLoader *instance) { m_instance = instance; } cannam@227: protected: cannam@227: PluginLoader *m_instance; cannam@227: }; cannam@227: cannam@227: virtual void pluginDeleted(PluginDeletionNotifyAdapter *adapter); cannam@227: cannam@227: map m_pluginLibraryNameMap; cannam@227: bool m_allPluginsEnumerated; cannam@227: void enumeratePlugins(PluginKey forPlugin = ""); cannam@227: cannam@227: map m_taxonomy; cannam@227: void generateTaxonomy(); cannam@227: cannam@227: map m_pluginLibraryHandleMap; cannam@227: cannam@227: bool decomposePluginKey(PluginKey key, cannam@227: string &libraryName, string &identifier); cannam@227: cannam@227: void *loadLibrary(string path); cannam@227: void unloadLibrary(void *handle); cannam@227: void *lookupInLibrary(void *handle, const char *symbol); cannam@227: cannam@227: string splicePath(string a, string b); cannam@227: vector listFiles(string dir, string ext); cannam@227: cannam@227: static InstanceCleaner m_cleaner; cannam@227: }; cannam@227: cannam@227: PluginLoader * cannam@227: PluginLoader::m_instance = 0; cannam@227: cannam@227: PluginLoader::Impl::InstanceCleaner cannam@227: PluginLoader::Impl::m_cleaner; cannam@227: cannam@227: PluginLoader::PluginLoader() cannam@227: { cannam@227: m_impl = new Impl(); cannam@227: } cannam@227: cannam@227: PluginLoader::~PluginLoader() cannam@227: { cannam@227: delete m_impl; cannam@227: } cannam@227: cannam@227: PluginLoader * cannam@227: PluginLoader::getInstance() cannam@227: { cannam@227: if (!m_instance) { cannam@227: // The cleaner doesn't own the instance, because we leave the cannam@227: // instance pointer in the base class for binary backwards cannam@227: // compatibility reasons and to avoid waste cannam@227: m_instance = new PluginLoader(); cannam@227: Impl::setInstanceToClean(m_instance); cannam@227: } cannam@227: return m_instance; cannam@227: } cannam@227: cannam@227: vector cannam@227: PluginLoader::listPlugins() cannam@227: { cannam@227: return m_impl->listPlugins(); cannam@227: } cannam@227: cannam@227: Plugin * cannam@227: PluginLoader::loadPlugin(PluginKey key, cannam@227: float inputSampleRate, cannam@227: int adapterFlags) cannam@227: { cannam@227: return m_impl->loadPlugin(key, inputSampleRate, adapterFlags); cannam@227: } cannam@227: cannam@227: PluginLoader::PluginKey cannam@227: PluginLoader::composePluginKey(string libraryName, string identifier) cannam@227: { cannam@227: return m_impl->composePluginKey(libraryName, identifier); cannam@227: } cannam@227: cannam@227: PluginLoader::PluginCategoryHierarchy cannam@227: PluginLoader::getPluginCategory(PluginKey key) cannam@227: { cannam@227: return m_impl->getPluginCategory(key); cannam@227: } cannam@227: cannam@227: string cannam@227: PluginLoader::getLibraryPathForPlugin(PluginKey key) cannam@227: { cannam@227: return m_impl->getLibraryPathForPlugin(key); cannam@227: } cannam@227: cannam@227: PluginLoader::Impl::Impl() : cannam@227: m_allPluginsEnumerated(false) cannam@227: { cannam@227: } cannam@227: cannam@227: PluginLoader::Impl::~Impl() cannam@227: { cannam@227: } cannam@227: cannam@227: void cannam@227: PluginLoader::Impl::setInstanceToClean(PluginLoader *instance) cannam@227: { cannam@227: m_cleaner.setInstance(instance); cannam@227: } cannam@227: cannam@227: vector cannam@227: PluginLoader::Impl::listPlugins() cannam@227: { cannam@227: if (!m_allPluginsEnumerated) enumeratePlugins(); cannam@227: cannam@227: vector plugins; cannam@227: for (map::iterator mi = m_pluginLibraryNameMap.begin(); cannam@227: mi != m_pluginLibraryNameMap.end(); ++mi) { cannam@227: plugins.push_back(mi->first); cannam@227: } cannam@227: cannam@227: return plugins; cannam@227: } cannam@227: cannam@227: void cannam@227: PluginLoader::Impl::enumeratePlugins(PluginKey forPlugin) cannam@227: { cannam@227: vector path = PluginHostAdapter::getPluginPath(); cannam@227: cannam@227: string libraryName, identifier; cannam@227: if (forPlugin != "") { cannam@227: if (!decomposePluginKey(forPlugin, libraryName, identifier)) { cannam@227: std::cerr << "WARNING: Vamp::HostExt::PluginLoader: Invalid plugin key \"" cannam@227: << forPlugin << "\" in enumerate" << std::endl; cannam@227: return; cannam@227: } cannam@227: } cannam@227: cannam@227: for (size_t i = 0; i < path.size(); ++i) { cannam@227: cannam@227: vector files = listFiles(path[i], PLUGIN_SUFFIX); cannam@227: cannam@227: for (vector::iterator fi = files.begin(); cannam@227: fi != files.end(); ++fi) { cannam@227: cannam@227: if (libraryName != "") { cannam@227: // libraryName is lowercased and lacking an extension, cannam@227: // as it came from the plugin key cannam@227: string temp = *fi; cannam@227: for (size_t i = 0; i < temp.length(); ++i) { cannam@227: temp[i] = tolower(temp[i]); cannam@227: } cannam@227: string::size_type pi = temp.find('.'); cannam@227: if (pi == string::npos) { cannam@227: if (libraryName != temp) continue; cannam@227: } else { cannam@227: if (libraryName != temp.substr(0, pi)) continue; cannam@227: } cannam@227: } cannam@227: cannam@227: string fullPath = path[i]; cannam@227: fullPath = splicePath(fullPath, *fi); cannam@227: void *handle = loadLibrary(fullPath); cannam@227: if (!handle) continue; cannam@227: cannam@227: VampGetPluginDescriptorFunction fn = cannam@227: (VampGetPluginDescriptorFunction)lookupInLibrary cannam@227: (handle, "vampGetPluginDescriptor"); cannam@227: cannam@227: if (!fn) { cannam@227: unloadLibrary(handle); cannam@227: continue; cannam@227: } cannam@227: cannam@227: int index = 0; cannam@227: const VampPluginDescriptor *descriptor = 0; cannam@227: cannam@227: while ((descriptor = fn(VAMP_API_VERSION, index))) { cannam@227: ++index; cannam@227: if (identifier != "") { cannam@227: if (descriptor->identifier != identifier) continue; cannam@227: } cannam@227: PluginKey key = composePluginKey(*fi, descriptor->identifier); cannam@227: // std::cerr << "enumerate: " << key << " (path: " << fullPath << ")" << std::endl; cannam@227: if (m_pluginLibraryNameMap.find(key) == cannam@227: m_pluginLibraryNameMap.end()) { cannam@227: m_pluginLibraryNameMap[key] = fullPath; cannam@227: } cannam@227: } cannam@227: cannam@227: unloadLibrary(handle); cannam@227: } cannam@227: } cannam@227: cannam@227: if (forPlugin == "") m_allPluginsEnumerated = true; cannam@227: } cannam@227: cannam@227: PluginLoader::PluginKey cannam@227: PluginLoader::Impl::composePluginKey(string libraryName, string identifier) cannam@227: { cannam@227: string basename = libraryName; cannam@227: cannam@227: string::size_type li = basename.rfind('/'); cannam@227: if (li != string::npos) basename = basename.substr(li + 1); cannam@227: cannam@227: li = basename.find('.'); cannam@227: if (li != string::npos) basename = basename.substr(0, li); cannam@227: cannam@227: for (size_t i = 0; i < basename.length(); ++i) { cannam@227: basename[i] = tolower(basename[i]); cannam@227: } cannam@227: cannam@227: return basename + ":" + identifier; cannam@227: } cannam@227: cannam@227: bool cannam@227: PluginLoader::Impl::decomposePluginKey(PluginKey key, cannam@227: string &libraryName, cannam@227: string &identifier) cannam@227: { cannam@227: string::size_type ki = key.find(':'); cannam@227: if (ki == string::npos) { cannam@227: return false; cannam@227: } cannam@227: cannam@227: libraryName = key.substr(0, ki); cannam@227: identifier = key.substr(ki + 1); cannam@227: return true; cannam@227: } cannam@227: cannam@227: PluginLoader::PluginCategoryHierarchy cannam@227: PluginLoader::Impl::getPluginCategory(PluginKey plugin) cannam@227: { cannam@227: if (m_taxonomy.empty()) generateTaxonomy(); cannam@227: if (m_taxonomy.find(plugin) == m_taxonomy.end()) { cannam@227: return PluginCategoryHierarchy(); cannam@227: } cannam@227: return m_taxonomy[plugin]; cannam@227: } cannam@227: cannam@227: string cannam@227: PluginLoader::Impl::getLibraryPathForPlugin(PluginKey plugin) cannam@227: { cannam@227: if (m_pluginLibraryNameMap.find(plugin) == m_pluginLibraryNameMap.end()) { cannam@227: if (m_allPluginsEnumerated) return ""; cannam@227: enumeratePlugins(plugin); cannam@227: } cannam@227: if (m_pluginLibraryNameMap.find(plugin) == m_pluginLibraryNameMap.end()) { cannam@227: return ""; cannam@227: } cannam@227: return m_pluginLibraryNameMap[plugin]; cannam@227: } cannam@227: cannam@227: Plugin * cannam@227: PluginLoader::Impl::loadPlugin(PluginKey key, cannam@227: float inputSampleRate, int adapterFlags) cannam@227: { cannam@227: string libname, identifier; cannam@227: if (!decomposePluginKey(key, libname, identifier)) { cannam@227: std::cerr << "Vamp::HostExt::PluginLoader: Invalid plugin key \"" cannam@227: << key << "\" in loadPlugin" << std::endl; cannam@227: return 0; cannam@227: } cannam@227: cannam@227: string fullPath = getLibraryPathForPlugin(key); cannam@227: if (fullPath == "") return 0; cannam@227: cannam@227: void *handle = loadLibrary(fullPath); cannam@227: if (!handle) return 0; cannam@227: cannam@227: VampGetPluginDescriptorFunction fn = cannam@227: (VampGetPluginDescriptorFunction)lookupInLibrary cannam@227: (handle, "vampGetPluginDescriptor"); cannam@227: cannam@227: if (!fn) { cannam@227: unloadLibrary(handle); cannam@227: return 0; cannam@227: } cannam@227: cannam@227: int index = 0; cannam@227: const VampPluginDescriptor *descriptor = 0; cannam@227: cannam@227: while ((descriptor = fn(VAMP_API_VERSION, index))) { cannam@227: cannam@227: if (string(descriptor->identifier) == identifier) { cannam@227: cannam@227: Vamp::PluginHostAdapter *plugin = cannam@227: new Vamp::PluginHostAdapter(descriptor, inputSampleRate); cannam@227: cannam@227: Plugin *adapter = new PluginDeletionNotifyAdapter(plugin, this); cannam@227: cannam@227: m_pluginLibraryHandleMap[adapter] = handle; cannam@227: cannam@227: if (adapterFlags & ADAPT_INPUT_DOMAIN) { cannam@227: if (adapter->getInputDomain() == Plugin::FrequencyDomain) { cannam@227: adapter = new PluginInputDomainAdapter(adapter); cannam@227: } cannam@227: } cannam@227: cannam@227: if (adapterFlags & ADAPT_BUFFER_SIZE) { cannam@227: adapter = new PluginBufferingAdapter(adapter); cannam@227: } cannam@227: cannam@227: if (adapterFlags & ADAPT_CHANNEL_COUNT) { cannam@227: adapter = new PluginChannelAdapter(adapter); cannam@227: } cannam@227: cannam@227: return adapter; cannam@227: } cannam@227: cannam@227: ++index; cannam@227: } cannam@227: cannam@227: cerr << "Vamp::HostExt::PluginLoader: Plugin \"" cannam@227: << identifier << "\" not found in library \"" cannam@227: << fullPath << "\"" << endl; cannam@227: cannam@227: return 0; cannam@227: } cannam@227: cannam@227: void cannam@227: PluginLoader::Impl::generateTaxonomy() cannam@227: { cannam@227: // cerr << "PluginLoader::Impl::generateTaxonomy" << endl; cannam@227: cannam@227: vector path = PluginHostAdapter::getPluginPath(); cannam@227: string libfragment = "/lib/"; cannam@227: vector catpath; cannam@227: cannam@227: string suffix = "cat"; cannam@227: cannam@227: for (vector::iterator i = path.begin(); cannam@227: i != path.end(); ++i) { cannam@227: cannam@227: // It doesn't matter that we're using literal forward-slash in cannam@227: // this bit, as it's only relevant if the path contains cannam@227: // "/lib/", which is only meaningful and only plausible on cannam@227: // systems with forward-slash delimiters cannam@227: cannam@227: string dir = *i; cannam@227: string::size_type li = dir.find(libfragment); cannam@227: cannam@227: if (li != string::npos) { cannam@227: catpath.push_back cannam@227: (dir.substr(0, li) cannam@227: + "/share/" cannam@227: + dir.substr(li + libfragment.length())); cannam@227: } cannam@227: cannam@227: catpath.push_back(dir); cannam@227: } cannam@227: cannam@227: char buffer[1024]; cannam@227: cannam@227: for (vector::iterator i = catpath.begin(); cannam@227: i != catpath.end(); ++i) { cannam@227: cannam@227: vector files = listFiles(*i, suffix); cannam@227: cannam@227: for (vector::iterator fi = files.begin(); cannam@227: fi != files.end(); ++fi) { cannam@227: cannam@227: string filepath = splicePath(*i, *fi); cannam@227: ifstream is(filepath.c_str(), ifstream::in | ifstream::binary); cannam@227: cannam@227: if (is.fail()) { cannam@227: // cerr << "failed to open: " << filepath << endl; cannam@227: continue; cannam@227: } cannam@227: cannam@227: // cerr << "opened: " << filepath << endl; cannam@227: cannam@227: while (!!is.getline(buffer, 1024)) { cannam@227: cannam@227: string line(buffer); cannam@227: cannam@227: // cerr << "line = " << line << endl; cannam@227: cannam@227: string::size_type di = line.find("::"); cannam@227: if (di == string::npos) continue; cannam@227: cannam@227: string id = line.substr(0, di); cannam@227: string encodedCat = line.substr(di + 2); cannam@227: cannam@227: if (id.substr(0, 5) != "vamp:") continue; cannam@227: id = id.substr(5); cannam@227: cannam@227: while (encodedCat.length() >= 1 && cannam@227: encodedCat[encodedCat.length()-1] == '\r') { cannam@227: encodedCat = encodedCat.substr(0, encodedCat.length()-1); cannam@227: } cannam@227: cannam@227: // cerr << "id = " << id << ", cat = " << encodedCat << endl; cannam@227: cannam@227: PluginCategoryHierarchy category; cannam@227: string::size_type ai; cannam@227: while ((ai = encodedCat.find(" > ")) != string::npos) { cannam@227: category.push_back(encodedCat.substr(0, ai)); cannam@227: encodedCat = encodedCat.substr(ai + 3); cannam@227: } cannam@227: if (encodedCat != "") category.push_back(encodedCat); cannam@227: cannam@227: m_taxonomy[id] = category; cannam@227: } cannam@227: } cannam@227: } cannam@227: } cannam@227: cannam@227: void * cannam@227: PluginLoader::Impl::loadLibrary(string path) cannam@227: { cannam@227: void *handle = 0; cannam@227: #ifdef _WIN32 cannam@227: handle = LoadLibrary(path.c_str()); cannam@227: if (!handle) { cannam@227: cerr << "Vamp::HostExt::PluginLoader: Unable to load library \"" cannam@227: << path << "\"" << endl; cannam@227: } cannam@227: #else cannam@227: handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_LOCAL); cannam@227: if (!handle) { cannam@227: cerr << "Vamp::HostExt::PluginLoader: Unable to load library \"" cannam@227: << path << "\": " << dlerror() << endl; cannam@227: } cannam@227: #endif cannam@227: return handle; cannam@227: } cannam@227: cannam@227: void cannam@227: PluginLoader::Impl::unloadLibrary(void *handle) cannam@227: { cannam@227: #ifdef _WIN32 cannam@227: FreeLibrary((HINSTANCE)handle); cannam@227: #else cannam@227: dlclose(handle); cannam@227: #endif cannam@227: } cannam@227: cannam@227: void * cannam@227: PluginLoader::Impl::lookupInLibrary(void *handle, const char *symbol) cannam@227: { cannam@227: #ifdef _WIN32 cannam@227: return (void *)GetProcAddress((HINSTANCE)handle, symbol); cannam@227: #else cannam@227: return (void *)dlsym(handle, symbol); cannam@227: #endif cannam@227: } cannam@227: cannam@227: string cannam@227: PluginLoader::Impl::splicePath(string a, string b) cannam@227: { cannam@227: #ifdef _WIN32 cannam@227: return a + "\\" + b; cannam@227: #else cannam@227: return a + "/" + b; cannam@227: #endif cannam@227: } cannam@227: cannam@227: vector cannam@227: PluginLoader::Impl::listFiles(string dir, string extension) cannam@227: { cannam@227: vector files; cannam@227: cannam@227: #ifdef _WIN32 cannam@227: cannam@227: string expression = dir + "\\*." + extension; cannam@227: WIN32_FIND_DATA data; cannam@227: HANDLE fh = FindFirstFile(expression.c_str(), &data); cannam@227: if (fh == INVALID_HANDLE_VALUE) return files; cannam@227: cannam@227: bool ok = true; cannam@227: while (ok) { cannam@227: files.push_back(data.cFileName); cannam@227: ok = FindNextFile(fh, &data); cannam@227: } cannam@227: cannam@227: FindClose(fh); cannam@227: cannam@227: #else cannam@227: cannam@227: size_t extlen = extension.length(); cannam@227: DIR *d = opendir(dir.c_str()); cannam@227: if (!d) return files; cannam@227: cannam@227: struct dirent *e = 0; cannam@227: while ((e = readdir(d))) { cannam@227: cannam@227: if (!e->d_name) continue; cannam@227: cannam@227: size_t len = strlen(e->d_name); cannam@227: if (len < extlen + 2 || cannam@227: e->d_name + len - extlen - 1 != "." + extension) { cannam@227: continue; cannam@227: } cannam@227: cannam@227: files.push_back(e->d_name); cannam@227: } cannam@227: cannam@227: closedir(d); cannam@227: #endif cannam@227: cannam@227: return files; cannam@227: } cannam@227: cannam@227: void cannam@227: PluginLoader::Impl::pluginDeleted(PluginDeletionNotifyAdapter *adapter) cannam@227: { cannam@227: void *handle = m_pluginLibraryHandleMap[adapter]; cannam@227: if (handle) unloadLibrary(handle); cannam@227: m_pluginLibraryHandleMap.erase(adapter); cannam@227: } cannam@227: cannam@227: PluginLoader::Impl::PluginDeletionNotifyAdapter::PluginDeletionNotifyAdapter(Plugin *plugin, cannam@227: Impl *loader) : cannam@227: PluginWrapper(plugin), cannam@227: m_loader(loader) cannam@227: { cannam@227: } cannam@227: cannam@227: PluginLoader::Impl::PluginDeletionNotifyAdapter::~PluginDeletionNotifyAdapter() cannam@227: { cannam@227: // We need to delete the plugin before calling pluginDeleted, as cannam@227: // the delete call may require calling through to the descriptor cannam@227: // (for e.g. cleanup) but pluginDeleted may unload the required cannam@227: // library for the call. To prevent a double deletion when our cannam@227: // parent's destructor runs (after this one), be sure to set cannam@227: // m_plugin to 0 after deletion. cannam@227: delete m_plugin; cannam@227: m_plugin = 0; cannam@227: cannam@227: if (m_loader) m_loader->pluginDeleted(this); cannam@227: } cannam@227: cannam@227: } cannam@227: cannam@227: }