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.

324 lines
10KB

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