annotate src/vamp-hostsdk/host-c.cpp @ 525:8c18bdaad04f c++11-mutex

Avoid simple static allocation of mutex, as it could lead to mutex being destroyed before last adapter that needs to use it (since adapters are usually also static)
author Chris Cannam
date Mon, 09 Sep 2019 10:24:13 +0100
parents 632c662e95e7
children
rev   line source
Chris@390 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@390 2
Chris@390 3 /*
Chris@390 4 Vamp
Chris@390 5
Chris@390 6 An API for audio analysis and feature extraction plugins.
Chris@390 7
Chris@390 8 Centre for Digital Music, Queen Mary, University of London.
Chris@390 9 Copyright 2006-2015 Chris Cannam and QMUL.
Chris@390 10
Chris@390 11 Permission is hereby granted, free of charge, to any person
Chris@390 12 obtaining a copy of this software and associated documentation
Chris@390 13 files (the "Software"), to deal in the Software without
Chris@390 14 restriction, including without limitation the rights to use, copy,
Chris@390 15 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@390 16 of the Software, and to permit persons to whom the Software is
Chris@390 17 furnished to do so, subject to the following conditions:
Chris@390 18
Chris@390 19 The above copyright notice and this permission notice shall be
Chris@390 20 included in all copies or substantial portions of the Software.
Chris@390 21
Chris@390 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@390 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@390 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@390 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@390 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@390 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@390 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@390 29
Chris@390 30 Except as contained in this notice, the names of the Centre for
Chris@390 31 Digital Music; Queen Mary, University of London; and Chris Cannam
Chris@390 32 shall not be used in advertising or otherwise to promote the sale,
Chris@390 33 use or other dealings in this Software without prior written
Chris@390 34 authorization.
Chris@390 35 */
Chris@390 36
Chris@390 37 #include <vamp-hostsdk/host-c.h>
Chris@390 38
Chris@390 39 #include "Files.h"
Chris@390 40
Chris@390 41 #include <map>
Chris@390 42 #include <iostream>
Chris@390 43
Chris@390 44 #include <cstring>
Chris@390 45
Chris@390 46 using namespace std;
Chris@390 47
Chris@390 48 static vector<string> files;
Chris@390 49 static map<string, const char *> cnames;
Chris@390 50 static bool haveFiles = false;
Chris@390 51
Chris@390 52 struct vhLibrary_t {
Chris@390 53 vhLibrary_t(void *h, VampGetPluginDescriptorFunction f)
Chris@390 54 : handle(h), func(f), nplugins(0) { }
Chris@390 55 void *handle;
Chris@390 56 VampGetPluginDescriptorFunction func;
Chris@390 57 int nplugins;
Chris@390 58 };
Chris@390 59
Chris@390 60 static void initFilenames()
Chris@390 61 {
Chris@390 62 if (!haveFiles) {
Chris@390 63 files = Files::listLibraryFiles();
Chris@390 64 for (size_t i = 0; i < files.size(); ++i) {
Chris@390 65 cnames[files[i]] = strdup(Files::lcBasename(files[i]).c_str());
Chris@390 66 }
Chris@390 67 haveFiles = true;
Chris@390 68 }
Chris@390 69 }
Chris@390 70
Chris@390 71 int vhGetLibraryCount()
Chris@390 72 {
Chris@390 73 initFilenames();
Chris@390 74 return int(files.size());
Chris@390 75 }
Chris@390 76
Chris@393 77 const char *vhGetLibraryName(int index)
Chris@390 78 {
Chris@390 79 initFilenames();
Chris@393 80 if (index >= 0 && index < int(files.size())) {
Chris@393 81 return cnames[files[index]];
Chris@390 82 }
Chris@390 83 else return 0;
Chris@390 84 }
Chris@390 85
Chris@393 86 int vhGetLibraryIndex(const char *name)
Chris@390 87 {
Chris@390 88 for (size_t i = 0; i < files.size(); ++i) {
Chris@393 89 if (Files::lcBasename(name) == Files::lcBasename(files[i])) {
Chris@393 90 return i;
Chris@390 91 }
Chris@390 92 }
Chris@393 93 return -1;
Chris@393 94 }
Chris@390 95
Chris@393 96 vhLibrary vhLoadLibrary(int index)
Chris@393 97 {
Chris@393 98 initFilenames();
Chris@393 99 if (index < 0 || index >= int(files.size())) {
Chris@393 100 return 0;
Chris@393 101 }
Chris@393 102
Chris@393 103 string fullPath = files[index];
Chris@393 104 void *lib = Files::loadLibrary(fullPath);
Chris@393 105
Chris@393 106 if (!lib) return 0;
Chris@393 107
Chris@393 108 VampGetPluginDescriptorFunction func =
Chris@393 109 (VampGetPluginDescriptorFunction)Files::lookupInLibrary
Chris@393 110 (lib, "vampGetPluginDescriptor");
Chris@393 111 if (!func) {
Chris@393 112 cerr << "vhLoadLibrary: No vampGetPluginDescriptor function found in library \""
Chris@393 113 << fullPath << "\"" << endl;
Chris@393 114 Files::unloadLibrary(lib);
Chris@393 115 return 0;
Chris@393 116 }
Chris@393 117
Chris@393 118 vhLibrary_t *vhl = new vhLibrary_t(lib, func);
Chris@393 119 while (vhl->func(VAMP_API_VERSION, vhl->nplugins)) {
Chris@393 120 ++vhl->nplugins;
Chris@393 121 }
Chris@393 122 return vhl;
Chris@390 123 }
Chris@390 124
Chris@390 125 int vhGetPluginCount(vhLibrary library)
Chris@390 126 {
Chris@390 127 vhLibrary_t *vhl = static_cast<vhLibrary_t *>(library);
Chris@390 128 if (vhl) return vhl->nplugins;
Chris@390 129 else return 0;
Chris@390 130 }
Chris@390 131
Chris@390 132 const VampPluginDescriptor *vhGetPluginDescriptor(vhLibrary library,
Chris@390 133 int plugin)
Chris@390 134 {
Chris@390 135 vhLibrary_t *vhl = static_cast<vhLibrary_t *>(library);
Chris@390 136 if (vhl && plugin >= 0 && plugin < vhl->nplugins) {
Chris@390 137 return vhl->func(VAMP_API_VERSION, plugin);
Chris@390 138 } else {
Chris@390 139 return 0;
Chris@390 140 }
Chris@390 141 }
Chris@390 142
Chris@390 143 void vhUnloadLibrary(vhLibrary library)
Chris@390 144 {
Chris@390 145 vhLibrary_t *vhl = static_cast<vhLibrary_t *>(library);
Chris@390 146 if (vhl && vhl->handle) {
Chris@390 147 Files::unloadLibrary(vhl->handle);
Chris@390 148 }
Chris@390 149 delete vhl;
Chris@390 150 }
Chris@390 151