cannam@64
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
cannam@64
|
2
|
cannam@64
|
3 /*
|
cannam@64
|
4 Vamp
|
cannam@64
|
5
|
cannam@64
|
6 An API for audio analysis and feature extraction plugins.
|
cannam@64
|
7
|
cannam@64
|
8 Centre for Digital Music, Queen Mary, University of London.
|
cannam@71
|
9 Copyright 2006-2007 Chris Cannam and QMUL.
|
cannam@64
|
10
|
cannam@64
|
11 Permission is hereby granted, free of charge, to any person
|
cannam@64
|
12 obtaining a copy of this software and associated documentation
|
cannam@64
|
13 files (the "Software"), to deal in the Software without
|
cannam@64
|
14 restriction, including without limitation the rights to use, copy,
|
cannam@64
|
15 modify, merge, publish, distribute, sublicense, and/or sell copies
|
cannam@64
|
16 of the Software, and to permit persons to whom the Software is
|
cannam@64
|
17 furnished to do so, subject to the following conditions:
|
cannam@64
|
18
|
cannam@64
|
19 The above copyright notice and this permission notice shall be
|
cannam@64
|
20 included in all copies or substantial portions of the Software.
|
cannam@64
|
21
|
cannam@64
|
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
cannam@64
|
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
cannam@64
|
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
cannam@64
|
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
cannam@64
|
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
cannam@64
|
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
cannam@64
|
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
cannam@64
|
29
|
cannam@64
|
30 Except as contained in this notice, the names of the Centre for
|
cannam@64
|
31 Digital Music; Queen Mary, University of London; and Chris Cannam
|
cannam@64
|
32 shall not be used in advertising or otherwise to promote the sale,
|
cannam@64
|
33 use or other dealings in this Software without prior written
|
cannam@64
|
34 authorization.
|
cannam@64
|
35 */
|
cannam@64
|
36
|
cannam@64
|
37 #include "vamp-sdk/PluginHostAdapter.h"
|
cannam@64
|
38 #include "PluginLoader.h"
|
cannam@64
|
39 #include "PluginInputDomainAdapter.h"
|
cannam@64
|
40 #include "PluginChannelAdapter.h"
|
cannam@64
|
41
|
cannam@64
|
42 #include <fstream>
|
cannam@75
|
43 #include <cctype> // tolower
|
cannam@64
|
44
|
cannam@64
|
45 #ifdef _WIN32
|
cannam@64
|
46
|
cannam@64
|
47 #include <windows.h>
|
cannam@64
|
48 #include <tchar.h>
|
cannam@64
|
49 #define PLUGIN_SUFFIX "dll"
|
cannam@64
|
50
|
cannam@64
|
51 #else /* ! _WIN32 */
|
cannam@64
|
52
|
cannam@64
|
53 #include <dirent.h>
|
cannam@64
|
54 #include <dlfcn.h>
|
cannam@64
|
55
|
cannam@64
|
56 #ifdef __APPLE__
|
cannam@64
|
57 #define PLUGIN_SUFFIX "dylib"
|
cannam@64
|
58 #else /* ! __APPLE__ */
|
cannam@64
|
59 #define PLUGIN_SUFFIX "so"
|
cannam@64
|
60 #endif /* ! __APPLE__ */
|
cannam@64
|
61
|
cannam@64
|
62 #endif /* ! _WIN32 */
|
cannam@64
|
63
|
cannam@64
|
64 using namespace std;
|
cannam@64
|
65
|
cannam@64
|
66 namespace Vamp {
|
cannam@64
|
67
|
cannam@64
|
68 namespace HostExt {
|
cannam@64
|
69
|
cannam@69
|
70 class PluginLoader::Impl
|
cannam@69
|
71 {
|
cannam@69
|
72 public:
|
cannam@72
|
73 Impl();
|
cannam@72
|
74 virtual ~Impl();
|
cannam@69
|
75
|
cannam@69
|
76 PluginKeyList listPlugins();
|
cannam@69
|
77
|
cannam@69
|
78 Plugin *loadPlugin(PluginKey key,
|
cannam@69
|
79 float inputSampleRate,
|
cannam@69
|
80 int adapterFlags);
|
cannam@69
|
81
|
cannam@69
|
82 PluginKey composePluginKey(string libraryName, string identifier);
|
cannam@69
|
83
|
cannam@69
|
84 PluginCategoryHierarchy getPluginCategory(PluginKey key);
|
cannam@69
|
85
|
cannam@72
|
86 string getLibraryPathForPlugin(PluginKey key);
|
cannam@69
|
87
|
cannam@69
|
88 protected:
|
cannam@69
|
89 class PluginDeletionNotifyAdapter : public PluginWrapper {
|
cannam@69
|
90 public:
|
cannam@69
|
91 PluginDeletionNotifyAdapter(Plugin *plugin, Impl *loader);
|
cannam@69
|
92 virtual ~PluginDeletionNotifyAdapter();
|
cannam@69
|
93 protected:
|
cannam@69
|
94 Impl *m_loader;
|
cannam@69
|
95 };
|
cannam@69
|
96
|
cannam@69
|
97 virtual void pluginDeleted(PluginDeletionNotifyAdapter *adapter);
|
cannam@69
|
98
|
cannam@72
|
99 map<PluginKey, string> m_pluginLibraryNameMap;
|
cannam@72
|
100 bool m_allPluginsEnumerated;
|
cannam@72
|
101 void enumeratePlugins(PluginKey forPlugin = "");
|
cannam@69
|
102
|
cannam@72
|
103 map<PluginKey, PluginCategoryHierarchy> m_taxonomy;
|
cannam@69
|
104 void generateTaxonomy();
|
cannam@69
|
105
|
cannam@72
|
106 map<Plugin *, void *> m_pluginLibraryHandleMap;
|
cannam@69
|
107
|
cannam@72
|
108 bool decomposePluginKey(PluginKey key,
|
cannam@72
|
109 string &libraryName, string &identifier);
|
cannam@72
|
110
|
cannam@72
|
111 void *loadLibrary(string path);
|
cannam@69
|
112 void unloadLibrary(void *handle);
|
cannam@69
|
113 void *lookupInLibrary(void *handle, const char *symbol);
|
cannam@69
|
114
|
cannam@72
|
115 string splicePath(string a, string b);
|
cannam@72
|
116 vector<string> listFiles(string dir, string ext);
|
cannam@69
|
117 };
|
cannam@69
|
118
|
cannam@64
|
119 PluginLoader *
|
cannam@64
|
120 PluginLoader::m_instance = 0;
|
cannam@64
|
121
|
cannam@64
|
122 PluginLoader::PluginLoader()
|
cannam@64
|
123 {
|
cannam@69
|
124 m_impl = new Impl();
|
cannam@64
|
125 }
|
cannam@64
|
126
|
cannam@64
|
127 PluginLoader::~PluginLoader()
|
cannam@64
|
128 {
|
cannam@69
|
129 delete m_impl;
|
cannam@64
|
130 }
|
cannam@64
|
131
|
cannam@64
|
132 PluginLoader *
|
cannam@64
|
133 PluginLoader::getInstance()
|
cannam@64
|
134 {
|
cannam@64
|
135 if (!m_instance) m_instance = new PluginLoader();
|
cannam@64
|
136 return m_instance;
|
cannam@64
|
137 }
|
cannam@64
|
138
|
cannam@64
|
139 vector<PluginLoader::PluginKey>
|
cannam@64
|
140 PluginLoader::listPlugins()
|
cannam@64
|
141 {
|
cannam@69
|
142 return m_impl->listPlugins();
|
cannam@69
|
143 }
|
cannam@69
|
144
|
cannam@69
|
145 Plugin *
|
cannam@69
|
146 PluginLoader::loadPlugin(PluginKey key,
|
cannam@69
|
147 float inputSampleRate,
|
cannam@69
|
148 int adapterFlags)
|
cannam@69
|
149 {
|
cannam@69
|
150 return m_impl->loadPlugin(key, inputSampleRate, adapterFlags);
|
cannam@69
|
151 }
|
cannam@69
|
152
|
cannam@69
|
153 PluginLoader::PluginKey
|
cannam@69
|
154 PluginLoader::composePluginKey(string libraryName, string identifier)
|
cannam@69
|
155 {
|
cannam@69
|
156 return m_impl->composePluginKey(libraryName, identifier);
|
cannam@69
|
157 }
|
cannam@69
|
158
|
cannam@69
|
159 PluginLoader::PluginCategoryHierarchy
|
cannam@69
|
160 PluginLoader::getPluginCategory(PluginKey key)
|
cannam@69
|
161 {
|
cannam@69
|
162 return m_impl->getPluginCategory(key);
|
cannam@69
|
163 }
|
cannam@69
|
164
|
cannam@69
|
165 string
|
cannam@69
|
166 PluginLoader::getLibraryPathForPlugin(PluginKey key)
|
cannam@69
|
167 {
|
cannam@69
|
168 return m_impl->getLibraryPathForPlugin(key);
|
cannam@69
|
169 }
|
cannam@72
|
170
|
cannam@72
|
171 PluginLoader::Impl::Impl() :
|
cannam@72
|
172 m_allPluginsEnumerated(false)
|
cannam@72
|
173 {
|
cannam@72
|
174 }
|
cannam@72
|
175
|
cannam@72
|
176 PluginLoader::Impl::~Impl()
|
cannam@72
|
177 {
|
cannam@72
|
178 }
|
cannam@69
|
179
|
cannam@69
|
180 vector<PluginLoader::PluginKey>
|
cannam@69
|
181 PluginLoader::Impl::listPlugins()
|
cannam@69
|
182 {
|
cannam@72
|
183 if (!m_allPluginsEnumerated) enumeratePlugins();
|
cannam@64
|
184
|
cannam@64
|
185 vector<PluginKey> plugins;
|
cannam@72
|
186 for (map<PluginKey, string>::iterator mi = m_pluginLibraryNameMap.begin();
|
cannam@64
|
187 mi != m_pluginLibraryNameMap.end(); ++mi) {
|
cannam@64
|
188 plugins.push_back(mi->first);
|
cannam@64
|
189 }
|
cannam@64
|
190
|
cannam@64
|
191 return plugins;
|
cannam@64
|
192 }
|
cannam@64
|
193
|
cannam@64
|
194 void
|
cannam@72
|
195 PluginLoader::Impl::enumeratePlugins(PluginKey forPlugin)
|
cannam@64
|
196 {
|
cannam@64
|
197 vector<string> path = PluginHostAdapter::getPluginPath();
|
cannam@64
|
198
|
cannam@72
|
199 string libraryName, identifier;
|
cannam@72
|
200 if (forPlugin != "") {
|
cannam@72
|
201 if (!decomposePluginKey(forPlugin, libraryName, identifier)) {
|
cannam@72
|
202 std::cerr << "WARNING: Vamp::HostExt::PluginLoader: Invalid plugin key \""
|
cannam@72
|
203 << forPlugin << "\" in enumerate" << std::endl;
|
cannam@72
|
204 return;
|
cannam@72
|
205 }
|
cannam@72
|
206 }
|
cannam@72
|
207
|
cannam@64
|
208 for (size_t i = 0; i < path.size(); ++i) {
|
cannam@64
|
209
|
cannam@64
|
210 vector<string> files = listFiles(path[i], PLUGIN_SUFFIX);
|
cannam@64
|
211
|
cannam@64
|
212 for (vector<string>::iterator fi = files.begin();
|
cannam@64
|
213 fi != files.end(); ++fi) {
|
cannam@64
|
214
|
cannam@72
|
215 if (libraryName != "") {
|
cannam@73
|
216 // libraryName is lowercased and lacking an extension,
|
cannam@73
|
217 // as it came from the plugin key
|
cannam@73
|
218 string temp = *fi;
|
cannam@73
|
219 for (size_t i = 0; i < temp.length(); ++i) {
|
cannam@75
|
220 temp[i] = tolower(temp[i]);
|
cannam@73
|
221 }
|
cannam@73
|
222 string::size_type pi = temp.find('.');
|
cannam@72
|
223 if (pi == string::npos) {
|
cannam@73
|
224 if (libraryName != temp) continue;
|
cannam@72
|
225 } else {
|
cannam@73
|
226 if (libraryName != temp.substr(0, pi)) continue;
|
cannam@72
|
227 }
|
cannam@72
|
228 }
|
cannam@72
|
229
|
cannam@64
|
230 string fullPath = path[i];
|
cannam@64
|
231 fullPath = splicePath(fullPath, *fi);
|
cannam@64
|
232 void *handle = loadLibrary(fullPath);
|
cannam@64
|
233 if (!handle) continue;
|
cannam@64
|
234
|
cannam@64
|
235 VampGetPluginDescriptorFunction fn =
|
cannam@64
|
236 (VampGetPluginDescriptorFunction)lookupInLibrary
|
cannam@64
|
237 (handle, "vampGetPluginDescriptor");
|
cannam@64
|
238
|
cannam@64
|
239 if (!fn) {
|
cannam@64
|
240 unloadLibrary(handle);
|
cannam@64
|
241 continue;
|
cannam@64
|
242 }
|
cannam@64
|
243
|
cannam@64
|
244 int index = 0;
|
cannam@64
|
245 const VampPluginDescriptor *descriptor = 0;
|
cannam@64
|
246
|
cannam@64
|
247 while ((descriptor = fn(VAMP_API_VERSION, index))) {
|
cannam@72
|
248 ++index;
|
cannam@72
|
249 if (identifier != "") {
|
cannam@72
|
250 if (descriptor->identifier != identifier) continue;
|
cannam@72
|
251 }
|
cannam@64
|
252 PluginKey key = composePluginKey(*fi, descriptor->identifier);
|
cannam@72
|
253 // std::cerr << "enumerate: " << key << " (path: " << fullPath << ")" << std::endl;
|
cannam@64
|
254 if (m_pluginLibraryNameMap.find(key) ==
|
cannam@64
|
255 m_pluginLibraryNameMap.end()) {
|
cannam@64
|
256 m_pluginLibraryNameMap[key] = fullPath;
|
cannam@64
|
257 }
|
cannam@64
|
258 }
|
cannam@64
|
259
|
cannam@64
|
260 unloadLibrary(handle);
|
cannam@64
|
261 }
|
cannam@64
|
262 }
|
cannam@72
|
263
|
cannam@72
|
264 if (forPlugin == "") m_allPluginsEnumerated = true;
|
cannam@64
|
265 }
|
cannam@64
|
266
|
cannam@64
|
267 PluginLoader::PluginKey
|
cannam@69
|
268 PluginLoader::Impl::composePluginKey(string libraryName, string identifier)
|
cannam@64
|
269 {
|
cannam@64
|
270 string basename = libraryName;
|
cannam@64
|
271
|
cannam@64
|
272 string::size_type li = basename.rfind('/');
|
cannam@64
|
273 if (li != string::npos) basename = basename.substr(li + 1);
|
cannam@64
|
274
|
cannam@64
|
275 li = basename.find('.');
|
cannam@64
|
276 if (li != string::npos) basename = basename.substr(0, li);
|
cannam@64
|
277
|
cannam@73
|
278 for (size_t i = 0; i < basename.length(); ++i) {
|
cannam@75
|
279 basename[i] = tolower(basename[i]);
|
cannam@73
|
280 }
|
cannam@73
|
281
|
cannam@64
|
282 return basename + ":" + identifier;
|
cannam@64
|
283 }
|
cannam@64
|
284
|
cannam@72
|
285 bool
|
cannam@72
|
286 PluginLoader::Impl::decomposePluginKey(PluginKey key,
|
cannam@72
|
287 string &libraryName,
|
cannam@72
|
288 string &identifier)
|
cannam@72
|
289 {
|
cannam@72
|
290 string::size_type ki = key.find(':');
|
cannam@72
|
291 if (ki == string::npos) {
|
cannam@72
|
292 return false;
|
cannam@72
|
293 }
|
cannam@72
|
294
|
cannam@72
|
295 libraryName = key.substr(0, ki);
|
cannam@72
|
296 identifier = key.substr(ki + 1);
|
cannam@72
|
297 return true;
|
cannam@72
|
298 }
|
cannam@72
|
299
|
cannam@64
|
300 PluginLoader::PluginCategoryHierarchy
|
cannam@69
|
301 PluginLoader::Impl::getPluginCategory(PluginKey plugin)
|
cannam@64
|
302 {
|
cannam@64
|
303 if (m_taxonomy.empty()) generateTaxonomy();
|
cannam@69
|
304 if (m_taxonomy.find(plugin) == m_taxonomy.end()) {
|
cannam@69
|
305 return PluginCategoryHierarchy();
|
cannam@69
|
306 }
|
cannam@64
|
307 return m_taxonomy[plugin];
|
cannam@64
|
308 }
|
cannam@64
|
309
|
cannam@64
|
310 string
|
cannam@69
|
311 PluginLoader::Impl::getLibraryPathForPlugin(PluginKey plugin)
|
cannam@64
|
312 {
|
cannam@72
|
313 if (m_pluginLibraryNameMap.find(plugin) == m_pluginLibraryNameMap.end()) {
|
cannam@72
|
314 if (m_allPluginsEnumerated) return "";
|
cannam@72
|
315 enumeratePlugins(plugin);
|
cannam@72
|
316 }
|
cannam@72
|
317 if (m_pluginLibraryNameMap.find(plugin) == m_pluginLibraryNameMap.end()) {
|
cannam@72
|
318 return "";
|
cannam@72
|
319 }
|
cannam@64
|
320 return m_pluginLibraryNameMap[plugin];
|
cannam@64
|
321 }
|
cannam@64
|
322
|
cannam@64
|
323 Plugin *
|
cannam@69
|
324 PluginLoader::Impl::loadPlugin(PluginKey key,
|
cannam@69
|
325 float inputSampleRate, int adapterFlags)
|
cannam@64
|
326 {
|
cannam@72
|
327 string libname, identifier;
|
cannam@72
|
328 if (!decomposePluginKey(key, libname, identifier)) {
|
cannam@72
|
329 std::cerr << "Vamp::HostExt::PluginLoader: Invalid plugin key \""
|
cannam@72
|
330 << key << "\" in loadPlugin" << std::endl;
|
cannam@72
|
331 return 0;
|
cannam@72
|
332 }
|
cannam@72
|
333
|
cannam@64
|
334 string fullPath = getLibraryPathForPlugin(key);
|
cannam@64
|
335 if (fullPath == "") return 0;
|
cannam@64
|
336
|
cannam@64
|
337 void *handle = loadLibrary(fullPath);
|
cannam@64
|
338 if (!handle) return 0;
|
cannam@64
|
339
|
cannam@64
|
340 VampGetPluginDescriptorFunction fn =
|
cannam@64
|
341 (VampGetPluginDescriptorFunction)lookupInLibrary
|
cannam@64
|
342 (handle, "vampGetPluginDescriptor");
|
cannam@64
|
343
|
cannam@64
|
344 if (!fn) {
|
cannam@64
|
345 unloadLibrary(handle);
|
cannam@64
|
346 return 0;
|
cannam@64
|
347 }
|
cannam@64
|
348
|
cannam@64
|
349 int index = 0;
|
cannam@64
|
350 const VampPluginDescriptor *descriptor = 0;
|
cannam@64
|
351
|
cannam@64
|
352 while ((descriptor = fn(VAMP_API_VERSION, index))) {
|
cannam@64
|
353
|
cannam@64
|
354 if (string(descriptor->identifier) == identifier) {
|
cannam@64
|
355
|
cannam@64
|
356 Vamp::PluginHostAdapter *plugin =
|
cannam@64
|
357 new Vamp::PluginHostAdapter(descriptor, inputSampleRate);
|
cannam@64
|
358
|
cannam@64
|
359 Plugin *adapter = new PluginDeletionNotifyAdapter(plugin, this);
|
cannam@64
|
360
|
cannam@64
|
361 m_pluginLibraryHandleMap[adapter] = handle;
|
cannam@64
|
362
|
cannam@64
|
363 if (adapterFlags & ADAPT_INPUT_DOMAIN) {
|
cannam@64
|
364 if (adapter->getInputDomain() == Plugin::FrequencyDomain) {
|
cannam@64
|
365 adapter = new PluginInputDomainAdapter(adapter);
|
cannam@64
|
366 }
|
cannam@64
|
367 }
|
cannam@64
|
368
|
cannam@64
|
369 if (adapterFlags & ADAPT_CHANNEL_COUNT) {
|
cannam@64
|
370 adapter = new PluginChannelAdapter(adapter);
|
cannam@64
|
371 }
|
cannam@64
|
372
|
cannam@64
|
373 return adapter;
|
cannam@64
|
374 }
|
cannam@64
|
375
|
cannam@64
|
376 ++index;
|
cannam@64
|
377 }
|
cannam@64
|
378
|
cannam@64
|
379 cerr << "Vamp::HostExt::PluginLoader: Plugin \""
|
cannam@64
|
380 << identifier << "\" not found in library \""
|
cannam@64
|
381 << fullPath << "\"" << endl;
|
cannam@64
|
382
|
cannam@64
|
383 return 0;
|
cannam@64
|
384 }
|
cannam@64
|
385
|
cannam@64
|
386 void
|
cannam@69
|
387 PluginLoader::Impl::generateTaxonomy()
|
cannam@64
|
388 {
|
cannam@69
|
389 // cerr << "PluginLoader::Impl::generateTaxonomy" << endl;
|
cannam@64
|
390
|
cannam@64
|
391 vector<string> path = PluginHostAdapter::getPluginPath();
|
cannam@64
|
392 string libfragment = "/lib/";
|
cannam@64
|
393 vector<string> catpath;
|
cannam@64
|
394
|
cannam@64
|
395 string suffix = "cat";
|
cannam@64
|
396
|
cannam@64
|
397 for (vector<string>::iterator i = path.begin();
|
cannam@64
|
398 i != path.end(); ++i) {
|
cannam@64
|
399
|
cannam@64
|
400 // It doesn't matter that we're using literal forward-slash in
|
cannam@64
|
401 // this bit, as it's only relevant if the path contains
|
cannam@64
|
402 // "/lib/", which is only meaningful and only plausible on
|
cannam@64
|
403 // systems with forward-slash delimiters
|
cannam@64
|
404
|
cannam@64
|
405 string dir = *i;
|
cannam@64
|
406 string::size_type li = dir.find(libfragment);
|
cannam@64
|
407
|
cannam@64
|
408 if (li != string::npos) {
|
cannam@64
|
409 catpath.push_back
|
cannam@64
|
410 (dir.substr(0, li)
|
cannam@64
|
411 + "/share/"
|
cannam@64
|
412 + dir.substr(li + libfragment.length()));
|
cannam@64
|
413 }
|
cannam@64
|
414
|
cannam@64
|
415 catpath.push_back(dir);
|
cannam@64
|
416 }
|
cannam@64
|
417
|
cannam@64
|
418 char buffer[1024];
|
cannam@64
|
419
|
cannam@64
|
420 for (vector<string>::iterator i = catpath.begin();
|
cannam@64
|
421 i != catpath.end(); ++i) {
|
cannam@64
|
422
|
cannam@64
|
423 vector<string> files = listFiles(*i, suffix);
|
cannam@64
|
424
|
cannam@64
|
425 for (vector<string>::iterator fi = files.begin();
|
cannam@64
|
426 fi != files.end(); ++fi) {
|
cannam@64
|
427
|
cannam@64
|
428 string filepath = splicePath(*i, *fi);
|
cannam@64
|
429 ifstream is(filepath.c_str(), ifstream::in | ifstream::binary);
|
cannam@64
|
430
|
cannam@64
|
431 if (is.fail()) {
|
cannam@64
|
432 // cerr << "failed to open: " << filepath << endl;
|
cannam@64
|
433 continue;
|
cannam@64
|
434 }
|
cannam@64
|
435
|
cannam@64
|
436 // cerr << "opened: " << filepath << endl;
|
cannam@64
|
437
|
cannam@64
|
438 while (!!is.getline(buffer, 1024)) {
|
cannam@64
|
439
|
cannam@64
|
440 string line(buffer);
|
cannam@64
|
441
|
cannam@64
|
442 // cerr << "line = " << line << endl;
|
cannam@64
|
443
|
cannam@64
|
444 string::size_type di = line.find("::");
|
cannam@64
|
445 if (di == string::npos) continue;
|
cannam@64
|
446
|
cannam@64
|
447 string id = line.substr(0, di);
|
cannam@64
|
448 string encodedCat = line.substr(di + 2);
|
cannam@64
|
449
|
cannam@64
|
450 if (id.substr(0, 5) != "vamp:") continue;
|
cannam@64
|
451 id = id.substr(5);
|
cannam@64
|
452
|
cannam@64
|
453 while (encodedCat.length() >= 1 &&
|
cannam@64
|
454 encodedCat[encodedCat.length()-1] == '\r') {
|
cannam@64
|
455 encodedCat = encodedCat.substr(0, encodedCat.length()-1);
|
cannam@64
|
456 }
|
cannam@64
|
457
|
cannam@64
|
458 // cerr << "id = " << id << ", cat = " << encodedCat << endl;
|
cannam@64
|
459
|
cannam@64
|
460 PluginCategoryHierarchy category;
|
cannam@64
|
461 string::size_type ai;
|
cannam@64
|
462 while ((ai = encodedCat.find(" > ")) != string::npos) {
|
cannam@64
|
463 category.push_back(encodedCat.substr(0, ai));
|
cannam@64
|
464 encodedCat = encodedCat.substr(ai + 3);
|
cannam@64
|
465 }
|
cannam@64
|
466 if (encodedCat != "") category.push_back(encodedCat);
|
cannam@64
|
467
|
cannam@64
|
468 m_taxonomy[id] = category;
|
cannam@64
|
469 }
|
cannam@64
|
470 }
|
cannam@64
|
471 }
|
cannam@64
|
472 }
|
cannam@64
|
473
|
cannam@64
|
474 void *
|
cannam@69
|
475 PluginLoader::Impl::loadLibrary(string path)
|
cannam@64
|
476 {
|
cannam@64
|
477 void *handle = 0;
|
cannam@64
|
478 #ifdef _WIN32
|
cannam@64
|
479 handle = LoadLibrary(path.c_str());
|
cannam@64
|
480 if (!handle) {
|
cannam@64
|
481 cerr << "Vamp::HostExt::PluginLoader: Unable to load library \""
|
cannam@64
|
482 << path << "\"" << endl;
|
cannam@64
|
483 }
|
cannam@64
|
484 #else
|
cannam@64
|
485 handle = dlopen(path.c_str(), RTLD_LAZY);
|
cannam@64
|
486 if (!handle) {
|
cannam@64
|
487 cerr << "Vamp::HostExt::PluginLoader: Unable to load library \""
|
cannam@64
|
488 << path << "\": " << dlerror() << endl;
|
cannam@64
|
489 }
|
cannam@64
|
490 #endif
|
cannam@64
|
491 return handle;
|
cannam@64
|
492 }
|
cannam@64
|
493
|
cannam@64
|
494 void
|
cannam@69
|
495 PluginLoader::Impl::unloadLibrary(void *handle)
|
cannam@64
|
496 {
|
cannam@64
|
497 #ifdef _WIN32
|
cannam@64
|
498 FreeLibrary((HINSTANCE)handle);
|
cannam@64
|
499 #else
|
cannam@64
|
500 dlclose(handle);
|
cannam@64
|
501 #endif
|
cannam@64
|
502 }
|
cannam@64
|
503
|
cannam@64
|
504 void *
|
cannam@69
|
505 PluginLoader::Impl::lookupInLibrary(void *handle, const char *symbol)
|
cannam@64
|
506 {
|
cannam@64
|
507 #ifdef _WIN32
|
cannam@64
|
508 return (void *)GetProcAddress((HINSTANCE)handle, symbol);
|
cannam@64
|
509 #else
|
cannam@64
|
510 return (void *)dlsym(handle, symbol);
|
cannam@64
|
511 #endif
|
cannam@64
|
512 }
|
cannam@64
|
513
|
cannam@64
|
514 string
|
cannam@69
|
515 PluginLoader::Impl::splicePath(string a, string b)
|
cannam@64
|
516 {
|
cannam@64
|
517 #ifdef _WIN32
|
cannam@64
|
518 return a + "\\" + b;
|
cannam@64
|
519 #else
|
cannam@64
|
520 return a + "/" + b;
|
cannam@64
|
521 #endif
|
cannam@64
|
522 }
|
cannam@64
|
523
|
cannam@64
|
524 vector<string>
|
cannam@69
|
525 PluginLoader::Impl::listFiles(string dir, string extension)
|
cannam@64
|
526 {
|
cannam@64
|
527 vector<string> files;
|
cannam@64
|
528 size_t extlen = extension.length();
|
cannam@64
|
529
|
cannam@64
|
530 #ifdef _WIN32
|
cannam@64
|
531
|
cannam@64
|
532 string expression = dir + "\\*." + extension;
|
cannam@64
|
533 WIN32_FIND_DATA data;
|
cannam@64
|
534 HANDLE fh = FindFirstFile(expression.c_str(), &data);
|
cannam@64
|
535 if (fh == INVALID_HANDLE_VALUE) return files;
|
cannam@64
|
536
|
cannam@64
|
537 bool ok = true;
|
cannam@64
|
538 while (ok) {
|
cannam@64
|
539 files.push_back(data.cFileName);
|
cannam@64
|
540 ok = FindNextFile(fh, &data);
|
cannam@64
|
541 }
|
cannam@64
|
542
|
cannam@64
|
543 FindClose(fh);
|
cannam@64
|
544
|
cannam@64
|
545 #else
|
cannam@64
|
546 DIR *d = opendir(dir.c_str());
|
cannam@64
|
547 if (!d) return files;
|
cannam@64
|
548
|
cannam@64
|
549 struct dirent *e = 0;
|
cannam@64
|
550 while ((e = readdir(d))) {
|
cannam@64
|
551
|
cannam@64
|
552 if (!(e->d_type & DT_REG) || !e->d_name) continue;
|
cannam@64
|
553
|
cannam@64
|
554 size_t len = strlen(e->d_name);
|
cannam@64
|
555 if (len < extlen + 2 ||
|
cannam@64
|
556 e->d_name + len - extlen - 1 != "." + extension) {
|
cannam@64
|
557 continue;
|
cannam@64
|
558 }
|
cannam@64
|
559
|
cannam@64
|
560 files.push_back(e->d_name);
|
cannam@64
|
561 }
|
cannam@64
|
562
|
cannam@64
|
563 closedir(d);
|
cannam@64
|
564 #endif
|
cannam@64
|
565
|
cannam@64
|
566 return files;
|
cannam@64
|
567 }
|
cannam@64
|
568
|
cannam@64
|
569 void
|
cannam@69
|
570 PluginLoader::Impl::pluginDeleted(PluginDeletionNotifyAdapter *adapter)
|
cannam@64
|
571 {
|
cannam@64
|
572 void *handle = m_pluginLibraryHandleMap[adapter];
|
cannam@64
|
573 if (handle) unloadLibrary(handle);
|
cannam@64
|
574 m_pluginLibraryHandleMap.erase(adapter);
|
cannam@64
|
575 }
|
cannam@64
|
576
|
cannam@69
|
577 PluginLoader::Impl::PluginDeletionNotifyAdapter::PluginDeletionNotifyAdapter(Plugin *plugin,
|
cannam@69
|
578 Impl *loader) :
|
cannam@64
|
579 PluginWrapper(plugin),
|
cannam@64
|
580 m_loader(loader)
|
cannam@64
|
581 {
|
cannam@64
|
582 }
|
cannam@64
|
583
|
cannam@69
|
584 PluginLoader::Impl::PluginDeletionNotifyAdapter::~PluginDeletionNotifyAdapter()
|
cannam@64
|
585 {
|
cannam@64
|
586 // We need to delete the plugin before calling pluginDeleted, as
|
cannam@64
|
587 // the delete call may require calling through to the descriptor
|
cannam@64
|
588 // (for e.g. cleanup) but pluginDeleted may unload the required
|
cannam@64
|
589 // library for the call. To prevent a double deletion when our
|
cannam@64
|
590 // parent's destructor runs (after this one), be sure to set
|
cannam@64
|
591 // m_plugin to 0 after deletion.
|
cannam@64
|
592 delete m_plugin;
|
cannam@64
|
593 m_plugin = 0;
|
cannam@64
|
594
|
cannam@64
|
595 if (m_loader) m_loader->pluginDeleted(this);
|
cannam@64
|
596 }
|
cannam@64
|
597
|
cannam@64
|
598 }
|
cannam@64
|
599
|
cannam@64
|
600 }
|