obs-vst.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*****************************************************************************
  2. Copyright (C) 2016-2017 by Colin Edwards.
  3. Additional Code Copyright (C) 2016-2017 by c3r1c3 <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *****************************************************************************/
  15. #include "headers/VSTPlugin.h"
  16. #include <QCryptographicHash>
  17. #define OPEN_VST_SETTINGS "open_vst_settings"
  18. #define CLOSE_VST_SETTINGS "close_vst_settings"
  19. #define OPEN_WHEN_ACTIVE_VST_SETTINGS "open_when_active_vst_settings"
  20. #define PLUG_IN_NAME obs_module_text("VstPlugin")
  21. #define OPEN_VST_TEXT obs_module_text("OpenPluginInterface")
  22. #define CLOSE_VST_TEXT obs_module_text("ClosePluginInterface")
  23. #define OPEN_WHEN_ACTIVE_VST_TEXT obs_module_text("OpenInterfaceWhenActive")
  24. OBS_DECLARE_MODULE()
  25. OBS_MODULE_USE_DEFAULT_LOCALE("obs-vst", "en-US")
  26. MODULE_EXPORT const char *obs_module_description(void)
  27. {
  28. return "VST 2.x Plug-in filter";
  29. }
  30. static bool open_editor_button_clicked(obs_properties_t *props,
  31. obs_property_t *property, void *data)
  32. {
  33. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  34. if (vstPlugin && vstPlugin->vstLoaded()) {
  35. QMetaObject::invokeMethod(vstPlugin, "openEditor");
  36. obs_property_set_visible(
  37. obs_properties_get(props, OPEN_VST_SETTINGS), false);
  38. obs_property_set_visible(
  39. obs_properties_get(props, CLOSE_VST_SETTINGS), true);
  40. }
  41. UNUSED_PARAMETER(props);
  42. UNUSED_PARAMETER(property);
  43. UNUSED_PARAMETER(data);
  44. return true;
  45. }
  46. static bool close_editor_button_clicked(obs_properties_t *props,
  47. obs_property_t *property, void *data)
  48. {
  49. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  50. if (vstPlugin && vstPlugin->vstLoaded() && vstPlugin->isEditorOpen()) {
  51. QMetaObject::invokeMethod(vstPlugin, "closeEditor");
  52. obs_property_set_visible(
  53. obs_properties_get(props, OPEN_VST_SETTINGS), true);
  54. obs_property_set_visible(
  55. obs_properties_get(props, CLOSE_VST_SETTINGS), false);
  56. }
  57. UNUSED_PARAMETER(property);
  58. return true;
  59. }
  60. std::string getFileMD5(const char *file)
  61. {
  62. QFile f(file);
  63. if (f.open(QFile::ReadOnly)) {
  64. QCryptographicHash hash(QCryptographicHash::Md5);
  65. if (hash.addData(&f))
  66. return std::string(hash.result().toHex());
  67. }
  68. return std::string();
  69. }
  70. static const char *vst_name(void *unused)
  71. {
  72. UNUSED_PARAMETER(unused);
  73. return PLUG_IN_NAME;
  74. }
  75. static void vst_destroy(void *data)
  76. {
  77. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  78. QMetaObject::invokeMethod(vstPlugin, "closeEditor");
  79. vstPlugin->deleteLater();
  80. }
  81. static void vst_update(void *data, obs_data_t *settings)
  82. {
  83. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  84. vstPlugin->openInterfaceWhenActive =
  85. obs_data_get_bool(settings, OPEN_WHEN_ACTIVE_VST_SETTINGS);
  86. const char *path = obs_data_get_string(settings, "plugin_path");
  87. #ifdef __linux__
  88. // Migrate freedesktop.org Flatpak runtime 21.08 VST paths to 22.08.
  89. if (QFile::exists("/.flatpak-info") &&
  90. QString(path).startsWith("/app/extensions/Plugins/lxvst")) {
  91. QString newPath(path);
  92. newPath.replace("/app/extensions/Plugins/lxvst",
  93. "/app/extensions/Plugins/vst");
  94. obs_data_set_string(settings, "plugin_path",
  95. newPath.toStdString().c_str());
  96. path = obs_data_get_string(settings, "plugin_path");
  97. }
  98. #endif
  99. if (!*path) {
  100. vstPlugin->unloadEffect();
  101. return;
  102. }
  103. vstPlugin->loadEffectFromPath(std::string(path));
  104. std::string hash = getFileMD5(path);
  105. const char *chunkHash = obs_data_get_string(settings, "chunk_hash");
  106. const char *chunkData = obs_data_get_string(settings, "chunk_data");
  107. bool chunkHashesMatch = chunkHash && *chunkHash &&
  108. hash.compare(chunkHash) == 0;
  109. if (chunkData && *chunkData &&
  110. (chunkHashesMatch || !chunkHash || !*chunkHash)) {
  111. vstPlugin->setChunk(std::string(chunkData));
  112. }
  113. }
  114. static void *vst_create(obs_data_t *settings, obs_source_t *filter)
  115. {
  116. VSTPlugin *vstPlugin = new VSTPlugin(filter);
  117. vst_update(vstPlugin, settings);
  118. return vstPlugin;
  119. }
  120. static void vst_save(void *data, obs_data_t *settings)
  121. {
  122. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  123. obs_data_set_string(settings, "chunk_data",
  124. vstPlugin->getChunk().c_str());
  125. obs_data_set_string(
  126. settings, "chunk_hash",
  127. getFileMD5(vstPlugin->getEffectPath().c_str()).c_str());
  128. }
  129. static struct obs_audio_data *vst_filter_audio(void *data,
  130. struct obs_audio_data *audio)
  131. {
  132. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  133. vstPlugin->process(audio);
  134. /*
  135. * OBS can only guarantee getting the filter source's parent and own name
  136. * in this call, so we grab it and return the results for processing
  137. * by the EditorWidget.
  138. */
  139. vstPlugin->getSourceNames();
  140. return audio;
  141. }
  142. static void fill_out_plugins(obs_property_t *list)
  143. {
  144. QStringList dir_list;
  145. #ifdef __APPLE__
  146. dir_list << "/Library/Audio/Plug-Ins/VST/"
  147. << "~/Library/Audio/Plug-ins/VST/";
  148. #elif WIN32
  149. #ifndef _WIN64
  150. HANDLE hProcess = GetCurrentProcess();
  151. BOOL isWow64;
  152. IsWow64Process(hProcess, &isWow64);
  153. if (!isWow64) {
  154. #endif
  155. dir_list << qEnvironmentVariable("ProgramFiles") +
  156. "/Steinberg/VstPlugins/"
  157. << qEnvironmentVariable("CommonProgramFiles") +
  158. "/Steinberg/Shared Components/"
  159. << qEnvironmentVariable("CommonProgramFiles") + "/VST2"
  160. << qEnvironmentVariable("CommonProgramFiles") +
  161. "/Steinberg/VST2"
  162. << qEnvironmentVariable("CommonProgramFiles") +
  163. "/VSTPlugins/"
  164. << qEnvironmentVariable("ProgramFiles") +
  165. "/VSTPlugins/";
  166. #ifndef _WIN64
  167. } else {
  168. dir_list << qEnvironmentVariable("ProgramFiles(x86)") +
  169. "/Steinberg/VstPlugins/"
  170. << qEnvironmentVariable("CommonProgramFiles(x86)") +
  171. "/Steinberg/Shared Components/"
  172. << qEnvironmentVariable("CommonProgramFiles(x86)") +
  173. "/VST2"
  174. << qEnvironmentVariable("CommonProgramFiles(x86)") +
  175. "/VSTPlugins/"
  176. << qEnvironmentVariable("ProgramFiles(x86)") +
  177. "/VSTPlugins/";
  178. }
  179. #endif
  180. #elif __linux__
  181. // If the user has set the VST_PATH environmental
  182. // variable, then use it. Else default to a list
  183. // of common locations.
  184. QString vstPathEnv(getenv("VST_PATH"));
  185. if (!vstPathEnv.isNull()) {
  186. dir_list.append(vstPathEnv.split(":"));
  187. } else {
  188. QString home(getenv("HOME"));
  189. // Choose the most common locations
  190. // clang-format off
  191. dir_list << "/usr/lib/vst/"
  192. << "/usr/lib/lxvst/"
  193. << "/usr/lib/linux_vst/"
  194. << "/usr/lib64/vst/"
  195. << "/usr/lib64/lxvst/"
  196. << "/usr/lib64/linux_vst/"
  197. << "/usr/local/lib/vst/"
  198. << "/usr/local/lib/lxvst/"
  199. << "/usr/local/lib/linux_vst/"
  200. << "/usr/local/lib64/vst/"
  201. << "/usr/local/lib64/lxvst/"
  202. << "/usr/local/lib64/linux_vst/"
  203. << home + "/.vst/"
  204. << home + "/.lxvst/";
  205. // clang-format on
  206. }
  207. #endif
  208. QStringList filters;
  209. #ifdef __APPLE__
  210. filters << "*.vst";
  211. #elif WIN32
  212. filters << "*.dll";
  213. #elif __linux__
  214. filters << "*.so"
  215. << "*.o";
  216. #endif
  217. QStringList vst_list;
  218. // Read all plugins into a list...
  219. for (int a = 0; a < dir_list.size(); ++a) {
  220. QDir search_dir(dir_list[a]);
  221. search_dir.setNameFilters(filters);
  222. QDirIterator it(search_dir, QDirIterator::Subdirectories);
  223. while (it.hasNext()) {
  224. QString path = it.next();
  225. QString name = it.fileName();
  226. #ifdef __APPLE__
  227. name.remove(".vst", Qt::CaseInsensitive);
  228. #elif WIN32
  229. name.remove(".dll", Qt::CaseInsensitive);
  230. #elif __linux__
  231. name.remove(".so", Qt::CaseInsensitive);
  232. name.remove(".o", Qt::CaseInsensitive);
  233. #endif
  234. name.append("=").append(path);
  235. vst_list << name;
  236. }
  237. }
  238. // Now sort list alphabetically (still case-sensitive though).
  239. std::stable_sort(vst_list.begin(), vst_list.end(),
  240. std::less<QString>());
  241. // Now add said list to the plug-in list of OBS
  242. obs_property_list_add_string(list, "{Please select a plug-in}",
  243. nullptr);
  244. for (int b = 0; b < vst_list.size(); ++b) {
  245. QString vst_sorted = vst_list[b];
  246. obs_property_list_add_string(
  247. list,
  248. vst_sorted.left(vst_sorted.indexOf('='))
  249. .toStdString()
  250. .c_str(),
  251. vst_sorted.mid(vst_sorted.indexOf('=') + 1)
  252. .toStdString()
  253. .c_str());
  254. }
  255. }
  256. static bool vst_changed(void *data, obs_properties_t *props,
  257. obs_property_t *list, obs_data_t *settings)
  258. {
  259. UNUSED_PARAMETER(settings);
  260. UNUSED_PARAMETER(list);
  261. bool open_settings_vis = true;
  262. bool close_settings_vis = false;
  263. if (data) {
  264. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  265. if (!vstPlugin->vstLoaded()) {
  266. close_settings_vis = false;
  267. open_settings_vis = false;
  268. } else {
  269. if (vstPlugin->isEditorOpen()) {
  270. close_settings_vis = true;
  271. open_settings_vis = false;
  272. }
  273. }
  274. }
  275. obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS),
  276. open_settings_vis);
  277. obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS),
  278. close_settings_vis);
  279. return true;
  280. }
  281. static obs_properties_t *vst_properties(void *data)
  282. {
  283. obs_properties_t *props = obs_properties_create();
  284. obs_property_t *list = obs_properties_add_list(props, "plugin_path",
  285. PLUG_IN_NAME,
  286. OBS_COMBO_TYPE_LIST,
  287. OBS_COMBO_FORMAT_STRING);
  288. fill_out_plugins(list);
  289. obs_properties_add_button(props, OPEN_VST_SETTINGS, OPEN_VST_TEXT,
  290. open_editor_button_clicked);
  291. obs_properties_add_button(props, CLOSE_VST_SETTINGS, CLOSE_VST_TEXT,
  292. close_editor_button_clicked);
  293. bool open_settings_vis = true;
  294. bool close_settings_vis = false;
  295. if (data) {
  296. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  297. if (!vstPlugin->vstLoaded()) {
  298. close_settings_vis = false;
  299. open_settings_vis = false;
  300. } else {
  301. if (vstPlugin->isEditorOpen()) {
  302. close_settings_vis = true;
  303. open_settings_vis = false;
  304. }
  305. }
  306. }
  307. obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS),
  308. open_settings_vis);
  309. obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS),
  310. close_settings_vis);
  311. obs_properties_add_bool(props, OPEN_WHEN_ACTIVE_VST_SETTINGS,
  312. OPEN_WHEN_ACTIVE_VST_TEXT);
  313. obs_property_set_modified_callback2(list, vst_changed, data);
  314. return props;
  315. }
  316. bool obs_module_load(void)
  317. {
  318. struct obs_source_info vst_filter = {};
  319. vst_filter.id = "vst_filter";
  320. vst_filter.type = OBS_SOURCE_TYPE_FILTER;
  321. vst_filter.output_flags = OBS_SOURCE_AUDIO;
  322. vst_filter.get_name = vst_name;
  323. vst_filter.create = vst_create;
  324. vst_filter.destroy = vst_destroy;
  325. vst_filter.update = vst_update;
  326. vst_filter.filter_audio = vst_filter_audio;
  327. vst_filter.get_properties = vst_properties;
  328. vst_filter.save = vst_save;
  329. obs_register_source(&vst_filter);
  330. return true;
  331. }