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