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.

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