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.

181 lines
5.8KB

  1. /*
  2. * Carla Plugin Thread
  3. * Copyright (C) 2012-2013 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 GPL.txt file
  16. */
  17. #include "CarlaPluginThread.hpp"
  18. #include "CarlaPlugin.hpp"
  19. #include "CarlaEngine.hpp"
  20. #include <QtCore/QProcess>
  21. CARLA_BACKEND_START_NAMESPACE
  22. const char* PluginThreadMode2str(const CarlaPluginThread::Mode mode)
  23. {
  24. switch (mode)
  25. {
  26. case CarlaPluginThread::PLUGIN_THREAD_NULL:
  27. return "PLUGIN_THREAD_NULL";
  28. case CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI:
  29. return "PLUGIN_THREAD_DSSI_GUI";
  30. case CarlaPluginThread::PLUGIN_THREAD_LV2_GUI:
  31. return "PLUGIN_THREAD_LV2_GUI";
  32. case CarlaPluginThread::PLUGIN_THREAD_VST_GUI:
  33. return "PLUGIN_THREAD_VST_GUI";
  34. case CarlaPluginThread::PLUGIN_THREAD_BRIDGE:
  35. return "PLUGIN_THREAD_BRIDGE";
  36. }
  37. carla_stderr("CarlaPluginThread::PluginThreadMode2str(%i) - invalid mode", mode);
  38. return nullptr;
  39. }
  40. CarlaPluginThread::CarlaPluginThread(CarlaBackend::CarlaEngine* const engine, CarlaBackend::CarlaPlugin* const plugin, const Mode mode)
  41. : kEngine(engine),
  42. kPlugin(plugin),
  43. fMode(mode),
  44. fProcess(nullptr)
  45. {
  46. carla_debug("CarlaPluginThread::CarlaPluginThread(plugin:\"%s\", engine:\"%s\", %s)", plugin->name(), engine->getName(), PluginThreadMode2str(mode));
  47. }
  48. CarlaPluginThread::~CarlaPluginThread()
  49. {
  50. if (fProcess != nullptr)
  51. delete fProcess;
  52. }
  53. void CarlaPluginThread::setMode(const CarlaPluginThread::Mode mode)
  54. {
  55. fMode = mode;
  56. }
  57. void CarlaPluginThread::setOscData(const char* const binary, const char* const label, const char* const extra1, const char* const extra2)
  58. {
  59. fBinary = binary;
  60. fLabel = label;
  61. fExtra1 = extra1;
  62. fExtra2 = extra2;
  63. }
  64. void CarlaPluginThread::run()
  65. {
  66. carla_debug("CarlaPluginThread::run()");
  67. if (fProcess == nullptr)
  68. {
  69. fProcess = new QProcess(nullptr);
  70. fProcess->setProcessChannelMode(QProcess::ForwardedChannels);
  71. #ifndef BUILD_BRIDGE
  72. //fProcess->setProcessEnvironment(kEngine->getOptionsAsProcessEnvironment());
  73. #endif
  74. }
  75. QString name(kPlugin->name());
  76. QStringList arguments;
  77. if (name.isEmpty())
  78. name = "(none)";
  79. switch (fMode)
  80. {
  81. case PLUGIN_THREAD_NULL:
  82. break;
  83. case PLUGIN_THREAD_DSSI_GUI:
  84. /* osc-url */ arguments << QString("%1/%2").arg(kEngine->getOscServerPathUDP()).arg(kPlugin->id());
  85. /* filename */ arguments << kPlugin->filename();
  86. /* label */ arguments << (const char*)fLabel;
  87. /* ui-title */ arguments << QString("%1 (GUI)").arg(kPlugin->name());
  88. break;
  89. case PLUGIN_THREAD_LV2_GUI:
  90. /* osc-url */ arguments << QString("%1/%2").arg(kEngine->getOscServerPathTCP()).arg(kPlugin->id());
  91. /* URI */ arguments << (const char*)fLabel;
  92. /* ui-URI */ arguments << (const char*)fExtra1;
  93. /* ui-title */ arguments << QString("%1 (GUI)").arg(kPlugin->name());
  94. break;
  95. case PLUGIN_THREAD_VST_GUI:
  96. /* osc-url */ arguments << QString("%1/%2").arg(kEngine->getOscServerPathTCP()).arg(kPlugin->id());
  97. /* filename */ arguments << kPlugin->filename();
  98. /* ui-title */ arguments << QString("%1 (GUI)").arg(kPlugin->name());
  99. break;
  100. case PLUGIN_THREAD_BRIDGE:
  101. /* osc-url */ arguments << QString("%1/%2").arg(kEngine->getOscServerPathTCP()).arg(kPlugin->id());
  102. /* stype */ arguments << (const char*)fExtra1;
  103. /* filename */ arguments << kPlugin->filename();
  104. /* name */ arguments << name;
  105. /* label */ arguments << (const char*)fLabel;
  106. /* SHM ids */ arguments << (const char*)fExtra2;
  107. break;
  108. }
  109. fProcess->start((const char*)fBinary, arguments);
  110. fProcess->waitForStarted();
  111. switch (fMode)
  112. {
  113. case PLUGIN_THREAD_NULL:
  114. break;
  115. case PLUGIN_THREAD_DSSI_GUI:
  116. case PLUGIN_THREAD_LV2_GUI:
  117. case PLUGIN_THREAD_VST_GUI:
  118. if (kPlugin->waitForOscGuiShow())
  119. {
  120. fProcess->waitForFinished(-1);
  121. if (fProcess->exitCode() == 0)
  122. {
  123. // Hide
  124. kEngine->callback(CarlaBackend::CALLBACK_SHOW_GUI, kPlugin->id(), 0, 0, 0.0f, nullptr);
  125. carla_stdout("CarlaPluginThread::run() - GUI closed");
  126. }
  127. else
  128. {
  129. // Kill
  130. kEngine->callback(CarlaBackend::CALLBACK_SHOW_GUI, kPlugin->id(), -1, 0, 0.0f, nullptr);
  131. carla_stderr("CarlaPluginThread::run() - GUI crashed");
  132. }
  133. }
  134. else
  135. {
  136. carla_debug("CarlaPluginThread::run() - GUI timeout");
  137. kEngine->callback(CarlaBackend::CALLBACK_SHOW_GUI, kPlugin->id(), 0, 0, 0.0f, nullptr);
  138. }
  139. break;
  140. case PLUGIN_THREAD_BRIDGE:
  141. fProcess->waitForFinished(-1);
  142. if (fProcess->exitCode() != 0)
  143. {
  144. carla_stderr("CarlaPluginThread::run() - bridge crashed");
  145. CarlaString errorString("Plugin '" + CarlaString(kPlugin->name()) + "' has crashed!\n"
  146. "Saving now will lose its current settings.\n"
  147. "Please remove this plugin, and not rely on it from this point.");
  148. kEngine->callback(CarlaBackend::CALLBACK_ERROR, kPlugin->id(), 0, 0, 0.0f, (const char*)errorString);
  149. }
  150. break;
  151. }
  152. }
  153. CARLA_BACKEND_END_NAMESPACE