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.

CarlaPluginThread.cpp 10KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 0;
  92. //return (uintptr_t)fProcess->pid();
  93. }
  94. void CarlaPluginThread::run()
  95. {
  96. carla_debug("CarlaPluginThread::run()");
  97. if (fProcess == nullptr)
  98. {
  99. fProcess = new ChildProcess;
  100. //fProcess->setProcessChannelMode(QProcess::ForwardedChannels);
  101. }
  102. else if (fProcess->isRunning())
  103. {
  104. carla_stderr("CarlaPluginThread::run() - already running, giving up...");
  105. switch (fMode)
  106. {
  107. case PLUGIN_THREAD_NULL:
  108. case PLUGIN_THREAD_BRIDGE:
  109. break;
  110. case PLUGIN_THREAD_DSSI_GUI:
  111. case PLUGIN_THREAD_LV2_GUI:
  112. case PLUGIN_THREAD_VST_GUI:
  113. fEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, fPlugin->getId(), 0, 0, 0.0f, nullptr);
  114. fProcess->kill();
  115. fProcess = nullptr;
  116. return;
  117. }
  118. }
  119. String name(fPlugin->getName());
  120. if (name.isEmpty())
  121. name = "(none)";
  122. if (fLabel.isEmpty())
  123. fLabel = "\"\"";
  124. StringArray arguments;
  125. #ifndef CARLA_OS_WIN
  126. if (fBinary.endsWith(".exe"))
  127. arguments.add("wine");
  128. #endif
  129. arguments.add(fBinary.buffer());
  130. // use a global mutex to ensure bridge environment is correct
  131. static CarlaMutex sEnvMutex;
  132. switch (fMode)
  133. {
  134. case PLUGIN_THREAD_NULL:
  135. break;
  136. case PLUGIN_THREAD_DSSI_GUI:
  137. /* osc-url */ arguments.add(String(fEngine->getOscServerPathUDP()) + String("/") + String(fPlugin->getId()));
  138. /* filename */ arguments.add(fPlugin->getFilename());
  139. /* label */ arguments.add(fLabel.buffer());
  140. /* ui-title */ arguments.add(name + String(" (GUI)"));
  141. break;
  142. case PLUGIN_THREAD_LV2_GUI:
  143. /* osc-url */ arguments.add(String(fEngine->getOscServerPathUDP()) + String("/") + String(fPlugin->getId()));
  144. /* URI */ arguments.add(fLabel.buffer());
  145. /* ui-URI */ arguments.add(fExtra1.buffer());
  146. /* ui-title */ arguments.add(name + String(" (GUI)"));
  147. break;
  148. case PLUGIN_THREAD_VST_GUI:
  149. /* osc-url */ arguments.add(String(fEngine->getOscServerPathUDP()) + String("/") + String(fPlugin->getId()));
  150. /* filename */ arguments.add(fPlugin->getFilename());
  151. /* ui-title */ arguments.add(name + String(" (GUI)"));
  152. break;
  153. case PLUGIN_THREAD_BRIDGE:
  154. /* osc-url */ arguments.add(String(fEngine->getOscServerPathUDP()) + String("/") + String(fPlugin->getId()));
  155. /* stype */ arguments.add(fExtra1.buffer());
  156. /* filename */ arguments.add(fPlugin->getFilename());
  157. /* name */ arguments.add(name);
  158. /* label */ arguments.add(fLabel.buffer());
  159. /* uniqueId */ arguments.add(String(static_cast<juce::int64>(fPlugin->getUniqueId())));
  160. sEnvMutex.lock();
  161. carla_setenv("ENGINE_BRIDGE_SHM_IDS", fExtra2.buffer());
  162. carla_setenv("ENGINE_BRIDGE_CLIENT_NAME", name.toRawUTF8());
  163. carla_setenv("ENGINE_BRIDGE_OSC_URL", String(String(fEngine->getOscServerPathUDP()) + String("/") + String(fPlugin->getId())).toRawUTF8());
  164. carla_setenv("WINEDEBUG", "-all");
  165. break;
  166. }
  167. carla_stdout("starting app..");
  168. //qWarning() << arguments;
  169. fProcess->start(arguments);
  170. //fProcess->waitForStarted();
  171. switch (fMode)
  172. {
  173. case PLUGIN_THREAD_NULL:
  174. break;
  175. case PLUGIN_THREAD_DSSI_GUI:
  176. case PLUGIN_THREAD_LV2_GUI:
  177. case PLUGIN_THREAD_VST_GUI:
  178. if (fPlugin->waitForOscGuiShow())
  179. {
  180. while (fProcess->isRunning() && ! shouldThreadExit())
  181. carla_sleep(1);
  182. // we only get here if UI was closed or thread asked to exit
  183. if (fProcess->isRunning() && shouldThreadExit())
  184. {
  185. //fProcess->waitForFinished(static_cast<int>(fEngine->getOptions().uiBridgesTimeout));
  186. if (fProcess->isRunning())
  187. {
  188. carla_stdout("CarlaPluginThread::run() - UI refused to close, force kill now");
  189. fProcess->kill();
  190. }
  191. else
  192. {
  193. carla_stdout("CarlaPluginThread::run() - UI auto-closed successfully");
  194. }
  195. }
  196. else if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/)
  197. carla_stderr("CarlaPluginThread::run() - UI crashed while running");
  198. else
  199. carla_stdout("CarlaPluginThread::run() - UI closed cleanly");
  200. fEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, fPlugin->getId(), 0, 0, 0.0f, nullptr);
  201. }
  202. else
  203. {
  204. fProcess->kill();
  205. carla_stdout("CarlaPluginThread::run() - GUI timeout");
  206. fEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, fPlugin->getId(), 0, 0, 0.0f, nullptr);
  207. }
  208. break;
  209. case PLUGIN_THREAD_BRIDGE:
  210. sEnvMutex.unlock();
  211. //fProcess->waitForFinished(-1);
  212. while (fProcess->isRunning() && ! shouldThreadExit())
  213. carla_sleep(1);
  214. // we only get here if bridge crashed or thread asked to exit
  215. if (shouldThreadExit())
  216. {
  217. fProcess->getExitCode(); // TEST
  218. if (fProcess->isRunning())
  219. fProcess->kill();
  220. }
  221. else
  222. {
  223. // forced quit, may have crashed
  224. if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/)
  225. {
  226. carla_stderr("CarlaPluginThread::run() - bridge crashed");
  227. CarlaString errorString("Plugin '" + CarlaString(fPlugin->getName()) + "' has crashed!\n"
  228. "Saving now will lose its current settings.\n"
  229. "Please remove this plugin, and not rely on it from this point.");
  230. fEngine->callback(CarlaBackend::ENGINE_CALLBACK_ERROR, fPlugin->getId(), 0, 0, 0.0f, errorString);
  231. }
  232. }
  233. break;
  234. }
  235. carla_stdout("app finished");
  236. fProcess = nullptr;
  237. }
  238. // -----------------------------------------------------------------------
  239. #if 0
  240. QProcessEnvironment env(QProcessEnvironment::systemEnvironment());
  241. const EngineOptions& options(fEngine->getOptions());
  242. char strBuf[STR_MAX+1];
  243. env.insert("ENGINE_OPTION_UIS_ALWAYS_ON_TOP", options.uisAlwaysOnTop ? "true" : "false");
  244. if (options.maxParameters != 0)
  245. {
  246. std::sprintf(strBuf, "%u", options.maxParameters);
  247. env.insert("ENGINE_OPTION_MAX_PARAMETERS", strBuf);
  248. }
  249. if (options.uiBridgesTimeout != 0)
  250. {
  251. std::sprintf(strBuf, "%u", options.uiBridgesTimeout);
  252. env.insert("ENGINE_OPTION_UI_BRIDGES_TIMEOUT", strBuf);
  253. }
  254. if (options.frontendWinId != 0)
  255. {
  256. std::sprintf(strBuf, P_UINTPTR, options.frontendWinId);
  257. env.insert("ENGINE_OPTION_FRONTEND_WIN_ID", strBuf);
  258. }
  259. if (options.binaryDir != nullptr)
  260. env.insert("ENGINE_OPTION_PATH_BINARIES", options.binaryDir);
  261. if (options.resourceDir != nullptr)
  262. env.insert("ENGINE_OPTION_PATH_RESOURCES", options.resourceDir);
  263. fProcess->setProcessEnvironment(env);
  264. #endif
  265. // -----------------------------------------------------------------------
  266. CARLA_BACKEND_END_NAMESPACE