Mercurial > hg > svcore
comparison plugin/FeatureExtractionPluginFactory.cpp @ 1164:0f90a357cb2a
Pull out candidate list
author | Chris Cannam |
---|---|
date | Fri, 08 Jan 2016 15:39:12 +0000 |
parents | c7f1300dbf64 |
children | d57f9344db19 |
comparison
equal
deleted
inserted
replaced
1163:d094598f84bd | 1164:0f90a357cb2a |
---|---|
28 | 28 |
29 #include <iostream> | 29 #include <iostream> |
30 | 30 |
31 #include "base/Profiler.h" | 31 #include "base/Profiler.h" |
32 | 32 |
33 using namespace std; | |
34 | |
33 //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1 | 35 //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1 |
34 | 36 |
35 class PluginDeletionNotifyAdapter : public Vamp::HostExt::PluginWrapper { | 37 class PluginDeletionNotifyAdapter : public Vamp::HostExt::PluginWrapper { |
36 public: | 38 public: |
37 PluginDeletionNotifyAdapter(Vamp::Plugin *plugin, | 39 PluginDeletionNotifyAdapter(Vamp::Plugin *plugin, |
75 QString type, soName, label; | 77 QString type, soName, label; |
76 PluginIdentifier::parseIdentifier(identifier, type, soName, label); | 78 PluginIdentifier::parseIdentifier(identifier, type, soName, label); |
77 return instance(type); | 79 return instance(type); |
78 } | 80 } |
79 | 81 |
80 std::vector<QString> | 82 vector<QString> |
81 FeatureExtractionPluginFactory::getPluginPath() | 83 FeatureExtractionPluginFactory::getPluginPath() |
82 { | 84 { |
83 if (!m_pluginPath.empty()) return m_pluginPath; | 85 if (!m_pluginPath.empty()) return m_pluginPath; |
84 | 86 |
85 std::vector<std::string> p = Vamp::PluginHostAdapter::getPluginPath(); | 87 vector<string> p = Vamp::PluginHostAdapter::getPluginPath(); |
86 for (size_t i = 0; i < p.size(); ++i) m_pluginPath.push_back(p[i].c_str()); | 88 for (size_t i = 0; i < p.size(); ++i) m_pluginPath.push_back(p[i].c_str()); |
87 return m_pluginPath; | 89 return m_pluginPath; |
88 } | 90 } |
89 | 91 |
90 std::vector<QString> | 92 vector<QString> |
91 FeatureExtractionPluginFactory::getAllPluginIdentifiers() | 93 FeatureExtractionPluginFactory::getAllPluginIdentifiers() |
92 { | 94 { |
93 FeatureExtractionPluginFactory *factory; | 95 FeatureExtractionPluginFactory *factory; |
94 std::vector<QString> rv; | 96 vector<QString> rv; |
95 | 97 |
96 factory = instance("vamp"); | 98 factory = instance("vamp"); |
97 if (factory) { | 99 if (factory) { |
98 std::vector<QString> tmp = factory->getPluginIdentifiers(); | 100 vector<QString> tmp = factory->getPluginIdentifiers(); |
99 for (size_t i = 0; i < tmp.size(); ++i) { | 101 for (size_t i = 0; i < tmp.size(); ++i) { |
100 // cerr << "identifier: " << tmp[i] << endl; | 102 // cerr << "identifier: " << tmp[i] << endl; |
101 rv.push_back(tmp[i]); | 103 rv.push_back(tmp[i]); |
102 } | 104 } |
103 } | 105 } |
106 RestoreStartupLocale(); | 108 RestoreStartupLocale(); |
107 | 109 |
108 return rv; | 110 return rv; |
109 } | 111 } |
110 | 112 |
111 std::vector<QString> | 113 vector<QString> |
112 FeatureExtractionPluginFactory::getPluginIdentifiers() | 114 FeatureExtractionPluginFactory::getPluginCandidateFiles() |
113 { | 115 { |
114 Profiler profiler("FeatureExtractionPluginFactory::getPluginIdentifiers"); | 116 vector<QString> path = getPluginPath(); |
115 | 117 vector<QString> candidates; |
116 std::vector<QString> rv; | 118 |
117 std::vector<QString> path = getPluginPath(); | 119 for (QString dirname : path) { |
118 | 120 |
119 for (std::vector<QString>::iterator i = path.begin(); i != path.end(); ++i) { | 121 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE |
120 | 122 SVDEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: scanning directory " << dirname << endl; |
121 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE | 123 #endif |
122 cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: scanning directory " << *i << endl; | 124 |
123 #endif | 125 QDir pluginDir(dirname, PLUGIN_GLOB, |
124 | |
125 QDir pluginDir(*i, PLUGIN_GLOB, | |
126 QDir::Name | QDir::IgnoreCase, | 126 QDir::Name | QDir::IgnoreCase, |
127 QDir::Files | QDir::Readable); | 127 QDir::Files | QDir::Readable); |
128 | 128 |
129 for (unsigned int j = 0; j < pluginDir.count(); ++j) { | 129 for (unsigned int j = 0; j < pluginDir.count(); ++j) { |
130 | |
131 QString soname = pluginDir.filePath(pluginDir[j]); | 130 QString soname = pluginDir.filePath(pluginDir[j]); |
132 | 131 candidates.push_back(soname); |
133 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE | 132 } |
134 cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: trying potential library " << soname << endl; | 133 } |
135 #endif | 134 |
136 | 135 return candidates; |
137 void *libraryHandle = DLOPEN(soname, RTLD_LAZY | RTLD_LOCAL); | 136 } |
137 | |
138 vector<QString> | |
139 FeatureExtractionPluginFactory::getPluginIdentifiers() | |
140 { | |
141 Profiler profiler("FeatureExtractionPluginFactory::getPluginIdentifiers"); | |
142 | |
143 vector<QString> rv; | |
144 vector<QString> candidates = getPluginCandidateFiles(); | |
145 | |
146 for (QString soname : candidates) { | |
147 | |
148 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE | |
149 SVDEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: trying potential library " << soname << endl; | |
150 #endif | |
151 | |
152 void *libraryHandle = DLOPEN(soname, RTLD_LAZY | RTLD_LOCAL); | |
138 | 153 |
139 if (!libraryHandle) { | 154 if (!libraryHandle) { |
140 cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to load library " << soname << ": " << DLERROR() << endl; | 155 cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to load library " << soname << ": " << DLERROR() << endl; |
141 continue; | 156 continue; |
142 } | 157 } |
143 | 158 |
144 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE | 159 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE |
145 cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: It's a library all right, checking for descriptor" << endl; | 160 SVDEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: It's a library all right, checking for descriptor" << endl; |
146 #endif | 161 #endif |
147 | 162 |
148 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction) | 163 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction) |
149 DLSYM(libraryHandle, "vampGetPluginDescriptor"); | 164 DLSYM(libraryHandle, "vampGetPluginDescriptor"); |
150 | 165 |
151 if (!fn) { | 166 if (!fn) { |
152 cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: No descriptor function in " << soname << endl; | 167 cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: No descriptor function in " << soname << endl; |
153 if (DLCLOSE(libraryHandle) != 0) { | |
154 cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname << endl; | |
155 } | |
156 continue; | |
157 } | |
158 | |
159 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE | |
160 cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: Vamp descriptor found" << endl; | |
161 #endif | |
162 | |
163 const VampPluginDescriptor *descriptor = 0; | |
164 int index = 0; | |
165 | |
166 std::map<std::string, int> known; | |
167 bool ok = true; | |
168 | |
169 while ((descriptor = fn(VAMP_API_VERSION, index))) { | |
170 | |
171 if (known.find(descriptor->identifier) != known.end()) { | |
172 cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Plugin library " | |
173 << soname | |
174 << " returns the same plugin identifier \"" | |
175 << descriptor->identifier << "\" at indices " | |
176 << known[descriptor->identifier] << " and " | |
177 << index << endl; | |
178 cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: Avoiding this library (obsolete API?)" << endl; | |
179 ok = false; | |
180 break; | |
181 } else { | |
182 known[descriptor->identifier] = index; | |
183 } | |
184 | |
185 ++index; | |
186 } | |
187 | |
188 if (ok) { | |
189 | |
190 index = 0; | |
191 | |
192 while ((descriptor = fn(VAMP_API_VERSION, index))) { | |
193 | |
194 QString id = PluginIdentifier::createIdentifier | |
195 ("vamp", soname, descriptor->identifier); | |
196 rv.push_back(id); | |
197 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE | |
198 cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: Found plugin id " << id << " at index " << index << endl; | |
199 #endif | |
200 ++index; | |
201 } | |
202 } | |
203 | |
204 if (DLCLOSE(libraryHandle) != 0) { | 168 if (DLCLOSE(libraryHandle) != 0) { |
205 cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname << endl; | 169 cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname << endl; |
206 } | 170 } |
207 } | 171 continue; |
172 } | |
173 | |
174 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE | |
175 SVDEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: Vamp descriptor found" << endl; | |
176 #endif | |
177 | |
178 const VampPluginDescriptor *descriptor = 0; | |
179 int index = 0; | |
180 | |
181 map<string, int> known; | |
182 bool ok = true; | |
183 | |
184 while ((descriptor = fn(VAMP_API_VERSION, index))) { | |
185 | |
186 if (known.find(descriptor->identifier) != known.end()) { | |
187 cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Plugin library " | |
188 << soname | |
189 << " returns the same plugin identifier \"" | |
190 << descriptor->identifier << "\" at indices " | |
191 << known[descriptor->identifier] << " and " | |
192 << index << endl; | |
193 SVDEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: Avoiding this library (obsolete API?)" << endl; | |
194 ok = false; | |
195 break; | |
196 } else { | |
197 known[descriptor->identifier] = index; | |
198 } | |
199 | |
200 ++index; | |
201 } | |
202 | |
203 if (ok) { | |
204 | |
205 index = 0; | |
206 | |
207 while ((descriptor = fn(VAMP_API_VERSION, index))) { | |
208 | |
209 QString id = PluginIdentifier::createIdentifier | |
210 ("vamp", soname, descriptor->identifier); | |
211 rv.push_back(id); | |
212 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE | |
213 SVDEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: Found plugin id " << id << " at index " << index << endl; | |
214 #endif | |
215 ++index; | |
216 } | |
217 } | |
218 | |
219 if (DLCLOSE(libraryHandle) != 0) { | |
220 cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname << endl; | |
221 } | |
208 } | 222 } |
209 | 223 |
210 generateTaxonomy(); | 224 generateTaxonomy(); |
211 | 225 |
212 return rv; | 226 return rv; |
277 if (fi.isAbsolute() && fi.absolutePath() != "") { | 291 if (fi.isAbsolute() && fi.absolutePath() != "") { |
278 file = findPluginFile(soname, fi.absolutePath()); | 292 file = findPluginFile(soname, fi.absolutePath()); |
279 if (file != "") return file; | 293 if (file != "") return file; |
280 } | 294 } |
281 | 295 |
282 std::vector<QString> path = getPluginPath(); | 296 vector<QString> path = getPluginPath(); |
283 for (std::vector<QString>::iterator i = path.begin(); | 297 for (vector<QString>::iterator i = path.begin(); |
284 i != path.end(); ++i) { | 298 i != path.end(); ++i) { |
285 if (*i != "") { | 299 if (*i != "") { |
286 file = findPluginFile(soname, *i); | 300 file = findPluginFile(soname, *i); |
287 if (file != "") return file; | 301 if (file != "") return file; |
288 } | 302 } |
398 } | 412 } |
399 | 413 |
400 void | 414 void |
401 FeatureExtractionPluginFactory::generateTaxonomy() | 415 FeatureExtractionPluginFactory::generateTaxonomy() |
402 { | 416 { |
403 std::vector<QString> pluginPath = getPluginPath(); | 417 vector<QString> pluginPath = getPluginPath(); |
404 std::vector<QString> path; | 418 vector<QString> path; |
405 | 419 |
406 for (size_t i = 0; i < pluginPath.size(); ++i) { | 420 for (size_t i = 0; i < pluginPath.size(); ++i) { |
407 if (pluginPath[i].contains("/lib/")) { | 421 if (pluginPath[i].contains("/lib/")) { |
408 QString p(pluginPath[i]); | 422 QString p(pluginPath[i]); |
409 path.push_back(p); | 423 path.push_back(p); |