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.

176 lines
5.7KB

  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 extra)
  58. {
  59. fBinary = binary;
  60. fLabel = label;
  61. fExtra = extra;
  62. }
  63. void CarlaPluginThread::run()
  64. {
  65. carla_debug("CarlaPluginThread::run()");
  66. if (fProcess == nullptr)
  67. {
  68. fProcess = new QProcess(nullptr);
  69. fProcess->setProcessChannelMode(QProcess::ForwardedChannels);
  70. #ifndef BUILD_BRIDGE
  71. //fProcess->setProcessEnvironment(kEngine->getOptionsAsProcessEnvironment());
  72. #endif
  73. }
  74. QString name(kPlugin->name() ? kPlugin->name() : "(none)");
  75. QStringList arguments;
  76. switch (fMode)
  77. {
  78. case PLUGIN_THREAD_NULL:
  79. break;
  80. case PLUGIN_THREAD_DSSI_GUI:
  81. /* osc_url */ arguments << QString("%1/%2").arg(kEngine->getOscServerPathUDP()).arg(kPlugin->id());
  82. /* filename */ arguments << kPlugin->filename();
  83. /* label */ arguments << (const char*)fLabel;
  84. /* ui-title */ arguments << QString("%1 (GUI)").arg(kPlugin->name());
  85. break;
  86. case PLUGIN_THREAD_LV2_GUI:
  87. /* osc_url */ arguments << QString("%1/%2").arg(kEngine->getOscServerPathTCP()).arg(kPlugin->id());
  88. /* URI */ arguments << (const char*)fLabel;
  89. /* ui-URI */ arguments << (const char*)fExtra;
  90. /* ui-title */ arguments << QString("%1 (GUI)").arg(kPlugin->name());
  91. break;
  92. case PLUGIN_THREAD_VST_GUI:
  93. /* osc_url */ arguments << QString("%1/%2").arg(kEngine->getOscServerPathTCP()).arg(kPlugin->id());
  94. /* filename */ arguments << kPlugin->filename();
  95. /* ui-title */ arguments << QString("%1 (GUI)").arg(kPlugin->name());
  96. break;
  97. case PLUGIN_THREAD_BRIDGE:
  98. /* osc_url */ arguments << QString("%1/%2").arg(kEngine->getOscServerPathTCP()).arg(kPlugin->id());
  99. /* stype */ arguments << (const char*)fExtra;
  100. /* filename */ arguments << kPlugin->filename();
  101. /* name */ arguments << name;
  102. /* label */ arguments << (const char*)fLabel;
  103. break;
  104. }
  105. fProcess->start((const char*)fBinary, arguments);
  106. fProcess->waitForStarted();
  107. switch (fMode)
  108. {
  109. case PLUGIN_THREAD_NULL:
  110. break;
  111. case PLUGIN_THREAD_DSSI_GUI:
  112. case PLUGIN_THREAD_LV2_GUI:
  113. case PLUGIN_THREAD_VST_GUI:
  114. if (kPlugin->waitForOscGuiShow())
  115. {
  116. fProcess->waitForFinished(-1);
  117. if (fProcess->exitCode() == 0)
  118. {
  119. // Hide
  120. kEngine->callback(CarlaBackend::CALLBACK_SHOW_GUI, kPlugin->id(), 0, 0, 0.0f, nullptr);
  121. carla_stdout("CarlaPluginThread::run() - GUI closed");
  122. }
  123. else
  124. {
  125. // Kill
  126. kEngine->callback(CarlaBackend::CALLBACK_SHOW_GUI, kPlugin->id(), -1, 0, 0.0f, nullptr);
  127. carla_stderr("CarlaPluginThread::run() - GUI crashed");
  128. }
  129. }
  130. else
  131. {
  132. carla_debug("CarlaPluginThread::run() - GUI timeout");
  133. kEngine->callback(CarlaBackend::CALLBACK_SHOW_GUI, kPlugin->id(), 0, 0, 0.0f, nullptr);
  134. }
  135. break;
  136. case PLUGIN_THREAD_BRIDGE:
  137. fProcess->waitForFinished(-1);
  138. if (fProcess->exitCode() != 0)
  139. {
  140. carla_stderr("CarlaPluginThread::run() - bridge crashed");
  141. CarlaString errorString("Plugin '" + CarlaString(kPlugin->name()) + "' has crashed!\n"
  142. "Saving now will lose its current settings.\n"
  143. "Please remove this plugin, and not rely on it from this point.");
  144. kEngine->callback(CarlaBackend::CALLBACK_ERROR, kPlugin->id(), 0, 0, 0.0f, (const char*)errorString);
  145. }
  146. break;
  147. }
  148. }
  149. CARLA_BACKEND_END_NAMESPACE