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.

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