obs-vst.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*****************************************************************************
  2. Copyright (C) 2016-2017 by Colin Edwards.
  3. Additional Code Copyright (C) 2016-2017 by c3r1c3 <c3r1c3@nevermindonline.com>
  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, obs_property_t *property, void *data)
  31. {
  32. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  33. if (vstPlugin && vstPlugin->vstLoaded()) {
  34. QMetaObject::invokeMethod(vstPlugin, "openEditor");
  35. obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS), false);
  36. obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS), true);
  37. }
  38. UNUSED_PARAMETER(props);
  39. UNUSED_PARAMETER(property);
  40. UNUSED_PARAMETER(data);
  41. return true;
  42. }
  43. static bool close_editor_button_clicked(obs_properties_t *props, obs_property_t *property, void *data)
  44. {
  45. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  46. if (vstPlugin && vstPlugin->vstLoaded() && vstPlugin->isEditorOpen()) {
  47. QMetaObject::invokeMethod(vstPlugin, "closeEditor");
  48. obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS), true);
  49. obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS), false);
  50. }
  51. UNUSED_PARAMETER(property);
  52. return true;
  53. }
  54. std::string getFileMD5(const char *file)
  55. {
  56. QFile f(file);
  57. if (f.open(QFile::ReadOnly)) {
  58. QCryptographicHash hash(QCryptographicHash::Md5);
  59. if (hash.addData(&f))
  60. return std::string(hash.result().toHex());
  61. }
  62. return std::string();
  63. }
  64. static const char *vst_name(void *unused)
  65. {
  66. UNUSED_PARAMETER(unused);
  67. return PLUG_IN_NAME;
  68. }
  69. static void vst_destroy(void *data)
  70. {
  71. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  72. QMetaObject::invokeMethod(vstPlugin, "closeEditor");
  73. vstPlugin->deleteLater();
  74. }
  75. static void vst_update(void *data, obs_data_t *settings)
  76. {
  77. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  78. vstPlugin->openInterfaceWhenActive = obs_data_get_bool(settings, OPEN_WHEN_ACTIVE_VST_SETTINGS);
  79. const char *path = obs_data_get_string(settings, "plugin_path");
  80. #ifdef __linux__
  81. // Migrate freedesktop.org Flatpak runtime 21.08 VST paths to 22.08.
  82. if (QFile::exists("/.flatpak-info") && QString(path).startsWith("/app/extensions/Plugins/lxvst")) {
  83. QString newPath(path);
  84. newPath.replace("/app/extensions/Plugins/lxvst", "/app/extensions/Plugins/vst");
  85. obs_data_set_string(settings, "plugin_path", newPath.toStdString().c_str());
  86. path = obs_data_get_string(settings, "plugin_path");
  87. }
  88. #endif
  89. if (!*path) {
  90. vstPlugin->unloadEffect();
  91. return;
  92. }
  93. vstPlugin->loadEffectFromPath(std::string(path));
  94. std::string hash = getFileMD5(path);
  95. const char *chunkHash = obs_data_get_string(settings, "chunk_hash");
  96. const char *chunkData = obs_data_get_string(settings, "chunk_data");
  97. bool chunkHashesMatch = chunkHash && *chunkHash && hash.compare(chunkHash) == 0;
  98. if (chunkData && *chunkData && (chunkHashesMatch || !chunkHash || !*chunkHash)) {
  99. vstPlugin->setChunk(std::string(chunkData));
  100. }
  101. }
  102. static void *vst_create(obs_data_t *settings, obs_source_t *filter)
  103. {
  104. VSTPlugin *vstPlugin = new VSTPlugin(filter);
  105. vst_update(vstPlugin, settings);
  106. return vstPlugin;
  107. }
  108. static void vst_save(void *data, obs_data_t *settings)
  109. {
  110. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  111. obs_data_set_string(settings, "chunk_data", vstPlugin->getChunk().c_str());
  112. obs_data_set_string(settings, "chunk_hash", getFileMD5(vstPlugin->getEffectPath().c_str()).c_str());
  113. }
  114. static struct obs_audio_data *vst_filter_audio(void *data, struct obs_audio_data *audio)
  115. {
  116. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  117. vstPlugin->process(audio);
  118. /*
  119. * OBS can only guarantee getting the filter source's parent and own name
  120. * in this call, so we grab it and return the results for processing
  121. * by the EditorWidget.
  122. */
  123. vstPlugin->getSourceNames();
  124. return audio;
  125. }
  126. static void fill_out_plugins(obs_property_t *list)
  127. {
  128. QStringList dir_list;
  129. #ifdef __APPLE__
  130. dir_list << "/Library/Audio/Plug-Ins/VST/"
  131. << "~/Library/Audio/Plug-ins/VST/";
  132. #elif WIN32
  133. #ifndef _WIN64
  134. HANDLE hProcess = GetCurrentProcess();
  135. BOOL isWow64;
  136. IsWow64Process(hProcess, &isWow64);
  137. if (!isWow64) {
  138. #endif
  139. dir_list << qEnvironmentVariable("ProgramFiles") + "/Steinberg/VstPlugins/"
  140. << qEnvironmentVariable("CommonProgramFiles") + "/Steinberg/Shared Components/"
  141. << qEnvironmentVariable("CommonProgramFiles") + "/VST2"
  142. << qEnvironmentVariable("CommonProgramFiles") + "/Steinberg/VST2"
  143. << qEnvironmentVariable("CommonProgramFiles") + "/VSTPlugins/"
  144. << qEnvironmentVariable("ProgramFiles") + "/VSTPlugins/";
  145. #ifndef _WIN64
  146. } else {
  147. dir_list << qEnvironmentVariable("ProgramFiles(x86)") + "/Steinberg/VstPlugins/"
  148. << qEnvironmentVariable("CommonProgramFiles(x86)") + "/Steinberg/Shared Components/"
  149. << qEnvironmentVariable("CommonProgramFiles(x86)") + "/VST2"
  150. << qEnvironmentVariable("CommonProgramFiles(x86)") + "/VSTPlugins/"
  151. << qEnvironmentVariable("ProgramFiles(x86)") + "/VSTPlugins/";
  152. }
  153. #endif
  154. #elif __linux__
  155. // If the user has set the VST_PATH environmental
  156. // variable, then use it. Else default to a list
  157. // of common locations.
  158. QString vstPathEnv(getenv("VST_PATH"));
  159. if (!vstPathEnv.isNull()) {
  160. dir_list.append(vstPathEnv.split(":"));
  161. } else {
  162. QString home(getenv("HOME"));
  163. // Choose the most common locations
  164. // clang-format off
  165. dir_list << "/usr/lib/vst/"
  166. << "/usr/lib/lxvst/"
  167. << "/usr/lib/linux_vst/"
  168. << "/usr/lib64/vst/"
  169. << "/usr/lib64/lxvst/"
  170. << "/usr/lib64/linux_vst/"
  171. << "/usr/local/lib/vst/"
  172. << "/usr/local/lib/lxvst/"
  173. << "/usr/local/lib/linux_vst/"
  174. << "/usr/local/lib64/vst/"
  175. << "/usr/local/lib64/lxvst/"
  176. << "/usr/local/lib64/linux_vst/"
  177. << home + "/.vst/"
  178. << home + "/.lxvst/";
  179. // clang-format on
  180. }
  181. #endif
  182. QStringList filters;
  183. #ifdef __APPLE__
  184. filters << "*.vst";
  185. #elif WIN32
  186. filters << "*.dll";
  187. #elif __linux__
  188. filters << "*.so"
  189. << "*.o";
  190. #endif
  191. QStringList vst_list;
  192. // Read all plugins into a list...
  193. for (int a = 0; a < dir_list.size(); ++a) {
  194. QDir search_dir(dir_list[a]);
  195. search_dir.setNameFilters(filters);
  196. QDirIterator it(search_dir, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
  197. while (it.hasNext()) {
  198. QString path = it.next();
  199. QString name = it.fileName();
  200. #ifdef __APPLE__
  201. name.remove(".vst", Qt::CaseInsensitive);
  202. #elif WIN32
  203. name.remove(".dll", Qt::CaseInsensitive);
  204. #elif __linux__
  205. name.remove(".so", Qt::CaseInsensitive);
  206. name.remove(".o", Qt::CaseInsensitive);
  207. #endif
  208. name.append("=").append(path);
  209. vst_list << name;
  210. }
  211. }
  212. // Now sort list alphabetically (still case-sensitive though).
  213. std::stable_sort(vst_list.begin(), vst_list.end(), std::less<QString>());
  214. // Now add said list to the plug-in list of OBS
  215. obs_property_list_add_string(list, "{Please select a plug-in}", nullptr);
  216. for (int b = 0; b < vst_list.size(); ++b) {
  217. QString vst_sorted = vst_list[b];
  218. obs_property_list_add_string(list, vst_sorted.left(vst_sorted.indexOf('=')).toStdString().c_str(),
  219. vst_sorted.mid(vst_sorted.indexOf('=') + 1).toStdString().c_str());
  220. }
  221. }
  222. static bool vst_changed(void *data, obs_properties_t *props, obs_property_t *list, obs_data_t *settings)
  223. {
  224. UNUSED_PARAMETER(settings);
  225. UNUSED_PARAMETER(list);
  226. bool open_settings_vis = true;
  227. bool close_settings_vis = false;
  228. if (data) {
  229. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  230. if (!vstPlugin->vstLoaded()) {
  231. close_settings_vis = false;
  232. open_settings_vis = false;
  233. } else {
  234. if (vstPlugin->isEditorOpen()) {
  235. close_settings_vis = true;
  236. open_settings_vis = false;
  237. }
  238. }
  239. }
  240. obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS), open_settings_vis);
  241. obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS), close_settings_vis);
  242. return true;
  243. }
  244. static obs_properties_t *vst_properties(void *data)
  245. {
  246. obs_properties_t *props = obs_properties_create();
  247. obs_property_t *list = obs_properties_add_list(props, "plugin_path", PLUG_IN_NAME, OBS_COMBO_TYPE_LIST,
  248. OBS_COMBO_FORMAT_STRING);
  249. fill_out_plugins(list);
  250. obs_properties_add_button(props, OPEN_VST_SETTINGS, OPEN_VST_TEXT, open_editor_button_clicked);
  251. obs_properties_add_button(props, CLOSE_VST_SETTINGS, CLOSE_VST_TEXT, close_editor_button_clicked);
  252. bool open_settings_vis = true;
  253. bool close_settings_vis = false;
  254. if (data) {
  255. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  256. if (!vstPlugin->vstLoaded()) {
  257. close_settings_vis = false;
  258. open_settings_vis = false;
  259. } else {
  260. if (vstPlugin->isEditorOpen()) {
  261. close_settings_vis = true;
  262. open_settings_vis = false;
  263. }
  264. }
  265. }
  266. obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS), open_settings_vis);
  267. obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS), close_settings_vis);
  268. obs_properties_add_bool(props, OPEN_WHEN_ACTIVE_VST_SETTINGS, OPEN_WHEN_ACTIVE_VST_TEXT);
  269. obs_property_set_modified_callback2(list, vst_changed, data);
  270. return props;
  271. }
  272. bool obs_module_load(void)
  273. {
  274. struct obs_source_info vst_filter = {};
  275. vst_filter.id = "vst_filter";
  276. vst_filter.type = OBS_SOURCE_TYPE_FILTER;
  277. vst_filter.output_flags = OBS_SOURCE_AUDIO;
  278. vst_filter.get_name = vst_name;
  279. vst_filter.create = vst_create;
  280. vst_filter.destroy = vst_destroy;
  281. vst_filter.update = vst_update;
  282. vst_filter.filter_audio = vst_filter_audio;
  283. vst_filter.get_properties = vst_properties;
  284. vst_filter.save = vst_save;
  285. obs_register_source(&vst_filter);
  286. return true;
  287. }