Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

349 lines
12KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPlugin.hpp"
  18. #include "CarlaPluginThread.hpp"
  19. #include "CarlaEngine.hpp"
  20. #include "juce_core.h"
  21. using juce::String;
  22. using juce::StringArray;
  23. CARLA_BACKEND_START_NAMESPACE
  24. // -----------------------------------------------------------------------
  25. #ifdef DEBUG
  26. static inline
  27. const char* PluginThreadMode2str(const CarlaPluginThread::Mode mode) noexcept
  28. {
  29. switch (mode)
  30. {
  31. case CarlaPluginThread::PLUGIN_THREAD_NULL:
  32. return "PLUGIN_THREAD_NULL";
  33. case CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI:
  34. return "PLUGIN_THREAD_DSSI_GUI";
  35. case CarlaPluginThread::PLUGIN_THREAD_LV2_GUI:
  36. return "PLUGIN_THREAD_LV2_GUI";
  37. case CarlaPluginThread::PLUGIN_THREAD_VST_GUI:
  38. return "PLUGIN_THREAD_VST_GUI";
  39. case CarlaPluginThread::PLUGIN_THREAD_BRIDGE:
  40. return "PLUGIN_THREAD_BRIDGE";
  41. }
  42. carla_stderr("CarlaPluginThread::PluginThreadMode2str(%i) - invalid mode", mode);
  43. return nullptr;
  44. }
  45. #endif
  46. // -----------------------------------------------------------------------
  47. CarlaPluginThread::CarlaPluginThread(CarlaBackend::CarlaEngine* const engine, CarlaBackend::CarlaPlugin* const plugin, const Mode mode) noexcept
  48. : CarlaThread("CarlaPluginThread"),
  49. fEngine(engine),
  50. fPlugin(plugin),
  51. fMode(mode),
  52. fBinary(),
  53. fLabel(),
  54. fExtra1(),
  55. fExtra2(),
  56. fProcess(nullptr),
  57. leakDetector_CarlaPluginThread()
  58. {
  59. carla_debug("CarlaPluginThread::CarlaPluginThread(%p, %p, %s)", engine, plugin, PluginThreadMode2str(mode));
  60. }
  61. CarlaPluginThread::~CarlaPluginThread() noexcept
  62. {
  63. carla_debug("CarlaPluginThread::~CarlaPluginThread()");
  64. if (fProcess != nullptr)
  65. {
  66. //fProcess.release();
  67. //try {
  68. //delete fProcess;
  69. //} CARLA_SAFE_EXCEPTION("~CarlaPluginThread(): delete ChildProcess");
  70. fProcess = nullptr;
  71. }
  72. }
  73. void CarlaPluginThread::setMode(const CarlaPluginThread::Mode mode) noexcept
  74. {
  75. CARLA_SAFE_ASSERT(! isThreadRunning());
  76. carla_debug("CarlaPluginThread::setMode(%s)", PluginThreadMode2str(mode));
  77. fMode = mode;
  78. }
  79. void CarlaPluginThread::setOscData(const char* const binary, const char* const label, const char* const extra1, const char* const extra2) noexcept
  80. {
  81. CARLA_SAFE_ASSERT(! isThreadRunning());
  82. carla_debug("CarlaPluginThread::setOscData(\"%s\", \"%s\", \"%s\", \"%s\")", binary, label, extra1, extra2);
  83. fBinary = binary;
  84. fLabel = label;
  85. fExtra1 = extra1;
  86. fExtra2 = extra2;
  87. }
  88. uintptr_t CarlaPluginThread::getPid() const
  89. {
  90. CARLA_SAFE_ASSERT_RETURN(fProcess != nullptr, 0);
  91. return (uintptr_t)fProcess->getPID();
  92. }
  93. void CarlaPluginThread::run()
  94. {
  95. carla_debug("CarlaPluginThread::run()");
  96. if (fProcess == nullptr)
  97. {
  98. fProcess = new ChildProcess;
  99. //fProcess->setProcessChannelMode(QProcess::ForwardedChannels);
  100. }
  101. else if (fProcess->isRunning())
  102. {
  103. carla_stderr("CarlaPluginThread::run() - already running, giving up...");
  104. switch (fMode)
  105. {
  106. case PLUGIN_THREAD_NULL:
  107. case PLUGIN_THREAD_BRIDGE:
  108. break;
  109. case PLUGIN_THREAD_DSSI_GUI:
  110. case PLUGIN_THREAD_LV2_GUI:
  111. case PLUGIN_THREAD_VST_GUI:
  112. fEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, fPlugin->getId(), 0, 0, 0.0f, nullptr);
  113. fProcess->kill();
  114. fProcess = nullptr;
  115. return;
  116. }
  117. }
  118. String name(fPlugin->getName());
  119. if (name.isEmpty())
  120. name = "(none)";
  121. if (fLabel.isEmpty())
  122. fLabel = "\"\"";
  123. StringArray arguments;
  124. #ifndef CARLA_OS_WIN
  125. if (fBinary.endsWith(".exe"))
  126. arguments.add("wine");
  127. #endif
  128. arguments.add(fBinary.buffer());
  129. // use a global mutex to ensure bridge environment is correct
  130. static CarlaMutex sEnvMutex;
  131. char strBuf[STR_MAX+1];
  132. strBuf[STR_MAX] = '\0';
  133. const EngineOptions& options(fEngine->getOptions());
  134. sEnvMutex.lock();
  135. std::snprintf(strBuf, STR_MAX, "%f", fEngine->getSampleRate());
  136. carla_setenv("CARLA_SAMPLE_RATE", strBuf);
  137. std::snprintf(strBuf, STR_MAX, "%u", options.maxParameters);
  138. carla_setenv("ENGINE_OPTION_MAX_PARAMETERS", strBuf);
  139. std::snprintf(strBuf, STR_MAX, "%u", options.uiBridgesTimeout);
  140. carla_setenv("ENGINE_OPTION_UI_BRIDGES_TIMEOUT",strBuf);
  141. carla_setenv("ENGINE_OPTION_UIS_ALWAYS_ON_TOP", bool2str(options.uisAlwaysOnTop));
  142. if (options.pathLADSPA != nullptr && options.pathLADSPA[0] != '\0')
  143. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LADSPA", options.pathLADSPA);
  144. if (options.pathDSSI != nullptr && options.pathDSSI[0] != '\0')
  145. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_DSSI", options.pathDSSI);
  146. if (options.pathLV2 != nullptr && options.pathLV2[0] != '\0')
  147. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LV2", options.pathLV2);
  148. if (options.pathVST != nullptr && options.pathVST[0] != '\0')
  149. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST", options.pathVST);
  150. if (options.pathVST3 != nullptr && options.pathVST3[0] != '\0')
  151. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST3", options.pathVST3);
  152. if (options.pathAU != nullptr && options.pathAU[0] != '\0')
  153. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_AU", options.pathAU);
  154. if (options.pathGIG != nullptr && options.pathGIG[0] != '\0')
  155. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_GIG", options.pathGIG);
  156. if (options.pathSF2 != nullptr && options.pathSF2[0] != '\0')
  157. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SF2", options.pathSF2);
  158. if (options.pathSFZ != nullptr && options.pathSFZ[0] != '\0')
  159. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SFZ", options.pathSFZ);
  160. if (options.binaryDir != nullptr && options.binaryDir[0] != '\0')
  161. carla_setenv("ENGINE_OPTION_PATH_BINARIES", options.binaryDir);
  162. if (options.resourceDir != nullptr && options.resourceDir[0] != '\0')
  163. carla_setenv("ENGINE_OPTION_PATH_RESOURCES", options.resourceDir);
  164. std::snprintf(strBuf, STR_MAX, P_UINTPTR, options.frontendWinId);
  165. carla_setenv("ENGINE_OPTION_FRONTEND_WIN_ID", strBuf);
  166. switch (fMode)
  167. {
  168. case PLUGIN_THREAD_NULL:
  169. break;
  170. case PLUGIN_THREAD_DSSI_GUI:
  171. /* osc-url */ arguments.add(String(fEngine->getOscServerPathUDP()) + String("/") + String(fPlugin->getId()));
  172. /* filename */ arguments.add(fPlugin->getFilename());
  173. /* label */ arguments.add(fLabel.buffer());
  174. /* ui-title */ arguments.add(name + String(" (GUI)"));
  175. break;
  176. case PLUGIN_THREAD_LV2_GUI:
  177. /* osc-url */ arguments.add(String(fEngine->getOscServerPathUDP()) + String("/") + String(fPlugin->getId()));
  178. /* URI */ arguments.add(fLabel.buffer());
  179. /* UI URI */ arguments.add(fExtra1.buffer());
  180. /* UI Bundle */ arguments.add(fExtra2.buffer());
  181. /* UI Title */ arguments.add(name + String(" (GUI)"));
  182. break;
  183. case PLUGIN_THREAD_VST_GUI:
  184. /* osc-url */ arguments.add(String(fEngine->getOscServerPathUDP()) + String("/") + String(fPlugin->getId()));
  185. /* filename */ arguments.add(fPlugin->getFilename());
  186. /* ui-title */ arguments.add(name + String(" (GUI)"));
  187. break;
  188. case PLUGIN_THREAD_BRIDGE:
  189. /* osc-url */ arguments.add(String(fEngine->getOscServerPathUDP()) + String("/") + String(fPlugin->getId()));
  190. /* stype */ arguments.add(fExtra1.buffer());
  191. /* filename */ arguments.add(fPlugin->getFilename());
  192. /* name */ arguments.add(name);
  193. /* label */ arguments.add(fLabel.buffer());
  194. /* uniqueId */ arguments.add(String(static_cast<juce::int64>(fPlugin->getUniqueId())));
  195. carla_setenv("ENGINE_BRIDGE_SHM_IDS", fExtra2.buffer());
  196. carla_setenv("ENGINE_BRIDGE_CLIENT_NAME", name.toRawUTF8());
  197. carla_setenv("ENGINE_BRIDGE_OSC_URL", String(String(fEngine->getOscServerPathUDP()) + String("/") + String(fPlugin->getId())).toRawUTF8());
  198. carla_setenv("WINEDEBUG", "-all");
  199. break;
  200. }
  201. carla_stdout("starting app..");
  202. fProcess->start(arguments);
  203. sEnvMutex.unlock();
  204. switch (fMode)
  205. {
  206. case PLUGIN_THREAD_NULL:
  207. break;
  208. case PLUGIN_THREAD_DSSI_GUI:
  209. case PLUGIN_THREAD_LV2_GUI:
  210. case PLUGIN_THREAD_VST_GUI:
  211. if (fPlugin->waitForOscGuiShow())
  212. {
  213. while (fProcess->isRunning() && ! shouldThreadExit())
  214. carla_sleep(1);
  215. // we only get here if UI was closed or thread asked to exit
  216. if (fProcess->isRunning() && shouldThreadExit())
  217. {
  218. fProcess->waitForProcessToFinish(static_cast<int>(fEngine->getOptions().uiBridgesTimeout));
  219. if (fProcess->isRunning())
  220. {
  221. carla_stdout("CarlaPluginThread::run() - UI refused to close, force kill now");
  222. fProcess->kill();
  223. }
  224. else
  225. {
  226. carla_stdout("CarlaPluginThread::run() - UI auto-closed successfully");
  227. }
  228. }
  229. else if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/)
  230. carla_stderr("CarlaPluginThread::run() - UI crashed while running");
  231. else
  232. carla_stdout("CarlaPluginThread::run() - UI closed cleanly");
  233. fEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, fPlugin->getId(), 0, 0, 0.0f, nullptr);
  234. }
  235. else
  236. {
  237. fProcess->kill();
  238. carla_stdout("CarlaPluginThread::run() - GUI timeout");
  239. fEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, fPlugin->getId(), 0, 0, 0.0f, nullptr);
  240. }
  241. break;
  242. case PLUGIN_THREAD_BRIDGE:
  243. //fProcess->waitForFinished(-1);
  244. while (fProcess->isRunning() && ! shouldThreadExit())
  245. carla_sleep(1);
  246. // we only get here if bridge crashed or thread asked to exit
  247. if (fProcess->isRunning() && shouldThreadExit())
  248. {
  249. fProcess->waitForProcessToFinish(2000);
  250. if (fProcess->isRunning())
  251. {
  252. carla_stdout("CarlaPluginThread::run() - bridge refused to close, force kill now");
  253. fProcess->kill();
  254. }
  255. else
  256. {
  257. carla_stdout("CarlaPluginThread::run() - bridge auto-closed successfully");
  258. }
  259. }
  260. else
  261. {
  262. // forced quit, may have crashed
  263. if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/)
  264. {
  265. carla_stderr("CarlaPluginThread::run() - bridge crashed");
  266. CarlaString errorString("Plugin '" + CarlaString(fPlugin->getName()) + "' has crashed!\n"
  267. "Saving now will lose its current settings.\n"
  268. "Please remove this plugin, and not rely on it from this point.");
  269. fEngine->callback(CarlaBackend::ENGINE_CALLBACK_ERROR, fPlugin->getId(), 0, 0, 0.0f, errorString);
  270. }
  271. }
  272. break;
  273. }
  274. carla_stdout("app finished");
  275. fProcess = nullptr;
  276. }
  277. // -----------------------------------------------------------------------
  278. CARLA_BACKEND_END_NAMESPACE